1 /* vi: set sw=4 ts=4: */
3 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
9 //usage:#define dc_trivial_usage
10 //usage: "EXPRESSION..."
12 //usage:#define dc_full_usage "\n\n"
13 //usage: "Tiny RPN calculator. Operations:\n"
14 //usage: "+, add, -, sub, *, mul, /, div, %, mod, "IF_FEATURE_DC_LIBM("**, exp, ")"and, or, not, xor,\n"
15 //usage: "p - print top of the stack (without popping),\n"
16 //usage: "f - print entire stack,\n"
17 //usage: "o - pop the value and set output radix (must be 10, 16, 8 or 2).\n"
18 //usage: "Examples: 'dc 2 2 add p' -> 4, 'dc 8 8 mul 2 2 + / p' -> 16"
20 //usage:#define dc_example_usage
21 //usage: "$ dc 2 2 + p\n"
23 //usage: "$ dc 8 8 \\* 2 2 + / p\n"
25 //usage: "$ dc 0 1 and p\n"
27 //usage: "$ dc 0 1 or p\n"
29 //usage: "$ echo 72 9 div 8 mul p | dc\n"
33 typedef unsigned data_t;
36 typedef unsigned long data_t;
39 typedef unsigned long long data_t;
49 enum { STACK_SIZE = (COMMON_BUFSIZE - offsetof(struct globals, stack)) / sizeof(double) };
50 #define G (*(struct globals*)&bb_common_bufsiz1)
51 #define pointer (G.pointer )
52 #define base (G.base )
53 #define stack (G.stack )
54 #define INIT_G() do { \
59 static void push(double a)
61 if (pointer >= STACK_SIZE)
62 bb_error_msg_and_die("stack overflow");
66 static double pop(void)
69 bb_error_msg_and_die("stack underflow");
70 return stack[--pointer];
80 double subtrahend = pop();
82 push(pop() - subtrahend);
90 #if ENABLE_FEATURE_DC_LIBM
91 static void power(void)
93 double topower = pop();
95 push(pow(pop(), topower));
99 static void divide(void)
101 double divisor = pop();
103 push(pop() / divisor);
106 static void mod(void)
110 push((data_t) pop() % d);
113 static void and(void)
115 push((data_t) pop() & (data_t) pop());
120 push((data_t) pop() | (data_t) pop());
123 static void eor(void)
125 push((data_t) pop() ^ (data_t) pop());
128 static void not(void)
130 push(~(data_t) pop());
133 static void set_output_base(void)
135 static const char bases[] ALIGN1 = { 2, 8, 10, 16, 0 };
136 unsigned b = (unsigned)pop();
138 base = *strchrnul(bases, b);
140 bb_error_msg("error, base %u is not supported", b);
145 static void print_base(double print)
151 if (x == print) /* exactly representable as unsigned integer */
152 printf("%"DATA_FMT"u\n", x);
154 printf("%g\n", print);
160 printf("%"DATA_FMT"x\n", x);
163 printf("%"DATA_FMT"o\n", x);
165 default: /* base 2 */
166 i = MAXINT(data_t) - (MAXINT(data_t) >> 1);
167 /* i is 100000...00000 */
174 bb_putchar('1' - !(x & i));
181 static void print_stack_no_pop(void)
183 unsigned i = pointer;
185 print_base(stack[--i]);
188 static void print_no_pop(void)
190 print_base(stack[pointer-1]);
195 void (*function) (void);
198 static const struct op operators[] = {
207 #if ENABLE_FEATURE_DC_LIBM
220 {"f", print_stack_no_pop},
221 {"o", set_output_base},
224 static void stack_machine(const char *argument)
230 d = strtod(argument, &end);
231 if (end != argument && *end == '\0') {
238 if (strcmp(o->name, argument) == 0) {
243 } while (o != operators + ARRAY_SIZE(operators));
245 bb_error_msg_and_die("syntax error at '%s'", argument);
248 int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
249 int dc_main(int argc UNUSED_PARAM, char **argv)
255 /* take stuff from stdin if no args are given */
259 while ((line = xmalloc_fgetline(stdin)) != NULL) {
262 token = skip_whitespace(cursor);
265 cursor = skip_non_whitespace(token);
268 stack_machine(token);
273 // why? it breaks "dc -2 2 + p"
274 //if (argv[0][0] == '-')
277 stack_machine(*argv);