dc.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 push(double a)
  53. {
  54. if (pointer >= STACK_SIZE)
  55. bb_error_msg_and_die("stack overflow");
  56. stack[pointer++] = a;
  57. }
  58. static double pop(void)
  59. {
  60. if (pointer == 0)
  61. bb_error_msg_and_die("stack underflow");
  62. return stack[--pointer];
  63. }
  64. static void add(void)
  65. {
  66. push(pop() + pop());
  67. }
  68. static void sub(void)
  69. {
  70. double subtrahend = pop();
  71. push(pop() - subtrahend);
  72. }
  73. static void mul(void)
  74. {
  75. push(pop() * pop());
  76. }
  77. #if ENABLE_FEATURE_DC_LIBM
  78. static void power(void)
  79. {
  80. double topower = pop();
  81. push(pow(pop(), topower));
  82. }
  83. #endif
  84. static void divide(void)
  85. {
  86. double divisor = pop();
  87. push(pop() / divisor);
  88. }
  89. static void mod(void)
  90. {
  91. data_t d = pop();
  92. push((data_t) pop() % d);
  93. }
  94. static void and(void)
  95. {
  96. push((data_t) pop() & (data_t) pop());
  97. }
  98. static void or(void)
  99. {
  100. push((data_t) pop() | (data_t) pop());
  101. }
  102. static void eor(void)
  103. {
  104. push((data_t) pop() ^ (data_t) pop());
  105. }
  106. static void not(void)
  107. {
  108. push(~(data_t) pop());
  109. }
  110. static void set_output_base(void)
  111. {
  112. static const char bases[] ALIGN1 = { 2, 8, 10, 16, 0 };
  113. unsigned b = (unsigned)pop();
  114. base = *strchrnul(bases, b);
  115. if (base == 0) {
  116. bb_error_msg("error, base %u is not supported", b);
  117. base = 10;
  118. }
  119. }
  120. static void print_base(double print)
  121. {
  122. data_t x, i;
  123. x = (data_t) print;
  124. if (base == 10) {
  125. if (x == print) /* exactly representable as unsigned integer */
  126. printf("%"DATA_FMT"u\n", x);
  127. else
  128. printf("%g\n", print);
  129. return;
  130. }
  131. switch (base) {
  132. case 16:
  133. printf("%"DATA_FMT"x\n", x);
  134. break;
  135. case 8:
  136. printf("%"DATA_FMT"o\n", x);
  137. break;
  138. default: /* base 2 */
  139. i = MAXINT(data_t) - (MAXINT(data_t) >> 1);
  140. /* i is 100000...00000 */
  141. do {
  142. if (x & i)
  143. break;
  144. i >>= 1;
  145. } while (i > 1);
  146. do {
  147. bb_putchar('1' - !(x & i));
  148. i >>= 1;
  149. } while (i);
  150. bb_putchar('\n');
  151. }
  152. }
  153. static void print_stack_no_pop(void)
  154. {
  155. unsigned i = pointer;
  156. while (i)
  157. print_base(stack[--i]);
  158. }
  159. static void print_no_pop(void)
  160. {
  161. print_base(stack[pointer-1]);
  162. }
  163. struct op {
  164. const char name[4];
  165. void (*function) (void);
  166. };
  167. static const struct op operators[] = {
  168. #if ENABLE_FEATURE_DC_LIBM
  169. {"**", power},
  170. {"exp", power},
  171. {"pow", power},
  172. #endif
  173. {"%", mod},
  174. {"mod", mod},
  175. {"and", and},
  176. {"or", or},
  177. {"not", not},
  178. {"eor", eor},
  179. {"xor", eor},
  180. {"+", add},
  181. {"add", add},
  182. {"-", sub},
  183. {"sub", sub},
  184. {"*", mul},
  185. {"mul", mul},
  186. {"/", divide},
  187. {"div", divide},
  188. {"p", print_no_pop},
  189. {"f", print_stack_no_pop},
  190. {"o", set_output_base},
  191. };
  192. /* Feed the stack machine */
  193. static void stack_machine(const char *argument)
  194. {
  195. char *end;
  196. double number;
  197. const struct op *o;
  198. next:
  199. number = strtod(argument, &end);
  200. if (end != argument) {
  201. argument = end;
  202. push(number);
  203. goto next;
  204. }
  205. /* We might have matched a digit, eventually advance the argument */
  206. argument = skip_whitespace(argument);
  207. if (*argument == '\0')
  208. return;
  209. o = operators;
  210. do {
  211. const size_t name_len = strlen(o->name);
  212. if (strncmp(o->name, argument, name_len) == 0) {
  213. argument += name_len;
  214. o->function();
  215. goto next;
  216. }
  217. o++;
  218. } while (o != operators + ARRAY_SIZE(operators));
  219. bb_error_msg_and_die("syntax error at '%s'", argument);
  220. }
  221. int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  222. int dc_main(int argc UNUSED_PARAM, char **argv)
  223. {
  224. INIT_G();
  225. argv++;
  226. if (!argv[0]) {
  227. /* take stuff from stdin if no args are given */
  228. char *line;
  229. while ((line = xmalloc_fgetline(stdin)) != NULL) {
  230. stack_machine(line);
  231. free(line);
  232. }
  233. } else {
  234. do {
  235. stack_machine(*argv);
  236. } while (*++argv);
  237. }
  238. return EXIT_SUCCESS;
  239. }