3
0

dc.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  4. */
  5. #include "libbb.h"
  6. #include <math.h>
  7. //usage:#define dc_trivial_usage
  8. //usage: "EXPRESSION..."
  9. //usage:
  10. //usage:#define dc_full_usage "\n\n"
  11. //usage: "Tiny RPN calculator. Operations:\n"
  12. //usage: "+, add, -, sub, *, mul, /, div, %, mod, "IF_FEATURE_DC_LIBM("**, exp, ")"and, or, not, xor,\n"
  13. //usage: "p - print top of the stack (without popping),\n"
  14. //usage: "f - print entire stack,\n"
  15. //usage: "o - pop the value and set output radix (must be 10, 16, 8 or 2).\n"
  16. //usage: "Examples: 'dc 2 2 add p' -> 4, 'dc 8 8 mul 2 2 + / p' -> 16"
  17. //usage:
  18. //usage:#define dc_example_usage
  19. //usage: "$ dc 2 2 + p\n"
  20. //usage: "4\n"
  21. //usage: "$ dc 8 8 \\* 2 2 + / p\n"
  22. //usage: "16\n"
  23. //usage: "$ dc 0 1 and p\n"
  24. //usage: "0\n"
  25. //usage: "$ dc 0 1 or p\n"
  26. //usage: "1\n"
  27. //usage: "$ echo 72 9 div 8 mul p | dc\n"
  28. //usage: "64\n"
  29. #if 0
  30. typedef unsigned data_t;
  31. #define DATA_FMT ""
  32. #elif 0
  33. typedef unsigned long data_t;
  34. #define DATA_FMT "l"
  35. #else
  36. typedef unsigned long long data_t;
  37. #define DATA_FMT "ll"
  38. #endif
  39. struct globals {
  40. unsigned pointer;
  41. unsigned base;
  42. double stack[1];
  43. } FIX_ALIASING;
  44. enum { STACK_SIZE = (COMMON_BUFSIZE - offsetof(struct globals, stack)) / sizeof(double) };
  45. #define G (*(struct globals*)&bb_common_bufsiz1)
  46. #define pointer (G.pointer )
  47. #define base (G.base )
  48. #define stack (G.stack )
  49. #define INIT_G() do { \
  50. base = 10; \
  51. } while (0)
  52. static void check_under(void)
  53. {
  54. if (pointer == 0)
  55. bb_error_msg_and_die("stack underflow");
  56. }
  57. static void push(double a)
  58. {
  59. if (pointer >= STACK_SIZE)
  60. bb_error_msg_and_die("stack overflow");
  61. stack[pointer++] = a;
  62. }
  63. static double pop(void)
  64. {
  65. check_under();
  66. return stack[--pointer];
  67. }
  68. static void add(void)
  69. {
  70. push(pop() + pop());
  71. }
  72. static void sub(void)
  73. {
  74. double subtrahend = pop();
  75. push(pop() - subtrahend);
  76. }
  77. static void mul(void)
  78. {
  79. push(pop() * pop());
  80. }
  81. #if ENABLE_FEATURE_DC_LIBM
  82. static void power(void)
  83. {
  84. double topower = pop();
  85. push(pow(pop(), topower));
  86. }
  87. #endif
  88. static void divide(void)
  89. {
  90. double divisor = pop();
  91. push(pop() / divisor);
  92. }
  93. static void mod(void)
  94. {
  95. data_t d = pop();
  96. push((data_t) pop() % d);
  97. }
  98. static void and(void)
  99. {
  100. push((data_t) pop() & (data_t) pop());
  101. }
  102. static void or(void)
  103. {
  104. push((data_t) pop() | (data_t) pop());
  105. }
  106. static void eor(void)
  107. {
  108. push((data_t) pop() ^ (data_t) pop());
  109. }
  110. static void not(void)
  111. {
  112. push(~(data_t) pop());
  113. }
  114. static void set_output_base(void)
  115. {
  116. static const char bases[] ALIGN1 = { 2, 8, 10, 16, 0 };
  117. unsigned b = (unsigned)pop();
  118. base = *strchrnul(bases, b);
  119. if (base == 0) {
  120. bb_error_msg("error, base %u is not supported", b);
  121. base = 10;
  122. }
  123. }
  124. static void print_base(double print)
  125. {
  126. data_t x, i;
  127. x = (data_t) print;
  128. if (base == 10) {
  129. if (x == print) /* exactly representable as unsigned integer */
  130. printf("%"DATA_FMT"u\n", x);
  131. else
  132. printf("%g\n", print);
  133. return;
  134. }
  135. switch (base) {
  136. case 16:
  137. printf("%"DATA_FMT"x\n", x);
  138. break;
  139. case 8:
  140. printf("%"DATA_FMT"o\n", x);
  141. break;
  142. default: /* base 2 */
  143. i = MAXINT(data_t) - (MAXINT(data_t) >> 1);
  144. /* i is 100000...00000 */
  145. do {
  146. if (x & i)
  147. break;
  148. i >>= 1;
  149. } while (i > 1);
  150. do {
  151. bb_putchar('1' - !(x & i));
  152. i >>= 1;
  153. } while (i);
  154. bb_putchar('\n');
  155. }
  156. }
  157. static void print_stack_no_pop(void)
  158. {
  159. unsigned i = pointer;
  160. while (i)
  161. print_base(stack[--i]);
  162. }
  163. static void print_no_pop(void)
  164. {
  165. check_under();
  166. print_base(stack[pointer-1]);
  167. }
  168. struct op {
  169. const char name[4];
  170. void (*function) (void);
  171. };
  172. static const struct op operators[] = {
  173. #if ENABLE_FEATURE_DC_LIBM
  174. {"**", power},
  175. {"exp", power},
  176. {"pow", power},
  177. #endif
  178. {"%", mod},
  179. {"mod", mod},
  180. {"and", and},
  181. {"or", or},
  182. {"not", not},
  183. {"eor", eor},
  184. {"xor", eor},
  185. {"+", add},
  186. {"add", add},
  187. {"-", sub},
  188. {"sub", sub},
  189. {"*", mul},
  190. {"mul", mul},
  191. {"/", divide},
  192. {"div", divide},
  193. {"p", print_no_pop},
  194. {"f", print_stack_no_pop},
  195. {"o", set_output_base},
  196. };
  197. /* Feed the stack machine */
  198. static void stack_machine(const char *argument)
  199. {
  200. char *end;
  201. double number;
  202. const struct op *o;
  203. next:
  204. number = strtod(argument, &end);
  205. if (end != argument) {
  206. argument = end;
  207. push(number);
  208. goto next;
  209. }
  210. /* We might have matched a digit, eventually advance the argument */
  211. argument = skip_whitespace(argument);
  212. if (*argument == '\0')
  213. return;
  214. o = operators;
  215. do {
  216. char *after_name = is_prefixed_with(argument, o->name);
  217. if (after_name) {
  218. argument = after_name;
  219. o->function();
  220. goto next;
  221. }
  222. o++;
  223. } while (o != operators + ARRAY_SIZE(operators));
  224. bb_error_msg_and_die("syntax error at '%s'", argument);
  225. }
  226. int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  227. int dc_main(int argc UNUSED_PARAM, char **argv)
  228. {
  229. INIT_G();
  230. argv++;
  231. if (!argv[0]) {
  232. /* take stuff from stdin if no args are given */
  233. char *line;
  234. while ((line = xmalloc_fgetline(stdin)) != NULL) {
  235. stack_machine(line);
  236. free(line);
  237. }
  238. } else {
  239. do {
  240. stack_machine(*argv);
  241. } while (*++argv);
  242. }
  243. return EXIT_SUCCESS;
  244. }