1 /* vi: set sw=4 ts=4: */
3 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
7 #include "common_bufsiz.h"
10 //usage:#define dc_trivial_usage
11 //usage: "EXPRESSION..."
13 //usage:#define dc_full_usage "\n\n"
14 //usage: "Tiny RPN calculator. Operations:\n"
15 //usage: "+, add, -, sub, *, mul, /, div, %, mod, "IF_FEATURE_DC_LIBM("**, exp, ")"and, or, not, xor,\n"
16 //usage: "p - print top of the stack (without popping),\n"
17 //usage: "f - print entire stack,\n"
18 //usage: "o - pop the value and set output radix (must be 10, 16, 8 or 2).\n"
19 //usage: "Examples: 'dc 2 2 add p' -> 4, 'dc 8 8 mul 2 2 + / p' -> 16"
21 //usage:#define dc_example_usage
22 //usage: "$ dc 2 2 + p\n"
24 //usage: "$ dc 8 8 \\* 2 2 + / p\n"
26 //usage: "$ dc 0 1 and p\n"
28 //usage: "$ dc 0 1 or p\n"
30 //usage: "$ echo 72 9 div 8 mul p | dc\n"
34 typedef unsigned data_t;
37 typedef unsigned long data_t;
40 typedef unsigned long long data_t;
50 enum { STACK_SIZE = (COMMON_BUFSIZE - offsetof(struct globals, stack)) / sizeof(double) };
51 #define G (*(struct globals*)bb_common_bufsiz1)
52 #define pointer (G.pointer )
53 #define base (G.base )
54 #define stack (G.stack )
55 #define INIT_G() do { \
56 setup_common_bufsiz(); \
61 static void check_under(void)
64 bb_error_msg_and_die("stack underflow");
67 static void push(double a)
69 if (pointer >= STACK_SIZE)
70 bb_error_msg_and_die("stack overflow");
74 static double pop(void)
77 return stack[--pointer];
87 double subtrahend = pop();
89 push(pop() - subtrahend);
97 #if ENABLE_FEATURE_DC_LIBM
98 static void power(void)
100 double topower = pop();
102 push(pow(pop(), topower));
106 static void divide(void)
108 double divisor = pop();
110 push(pop() / divisor);
113 static void mod(void)
117 push((data_t) pop() % d);
120 static void and(void)
122 push((data_t) pop() & (data_t) pop());
127 push((data_t) pop() | (data_t) pop());
130 static void eor(void)
132 push((data_t) pop() ^ (data_t) pop());
135 static void not(void)
137 push(~(data_t) pop());
140 static void set_output_base(void)
142 static const char bases[] ALIGN1 = { 2, 8, 10, 16, 0 };
143 unsigned b = (unsigned)pop();
145 base = *strchrnul(bases, b);
147 bb_error_msg("error, base %u is not supported", b);
152 static void print_base(double print)
158 if (x == print) /* exactly representable as unsigned integer */
159 printf("%"DATA_FMT"u\n", x);
161 printf("%g\n", print);
167 printf("%"DATA_FMT"x\n", x);
170 printf("%"DATA_FMT"o\n", x);
172 default: /* base 2 */
173 i = MAXINT(data_t) - (MAXINT(data_t) >> 1);
174 /* i is 100000...00000 */
181 bb_putchar('1' - !(x & i));
188 static void print_stack_no_pop(void)
190 unsigned i = pointer;
192 print_base(stack[--i]);
195 static void print_no_pop(void)
198 print_base(stack[pointer-1]);
203 void (*function) (void);
206 static const struct op operators[] = {
207 #if ENABLE_FEATURE_DC_LIBM
228 {"f", print_stack_no_pop},
229 {"o", set_output_base},
232 /* Feed the stack machine */
233 static void stack_machine(const char *argument)
240 number = strtod(argument, &end);
241 if (end != argument) {
247 /* We might have matched a digit, eventually advance the argument */
248 argument = skip_whitespace(argument);
250 if (*argument == '\0')
255 char *after_name = is_prefixed_with(argument, o->name);
257 argument = after_name;
262 } while (o != operators + ARRAY_SIZE(operators));
264 bb_error_msg_and_die("syntax error at '%s'", argument);
267 int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
268 int dc_main(int argc UNUSED_PARAM, char **argv)
274 /* take stuff from stdin if no args are given */
276 while ((line = xmalloc_fgetline(stdin)) != NULL) {
282 stack_machine(*argv);