1 /* vi: set sw=4 ts=4: */
3 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
9 //config: Dc is a reverse-polish desk calculator which supports unlimited
10 //config: precision arithmetic.
12 //config:config FEATURE_DC_LIBM
13 //config: bool "Enable power and exp functions (requires libm)"
15 //config: depends on DC
17 //config: Enable power and exp functions.
18 //config: NOTE: This will require libm to be present for linking.
20 //usage:#define dc_trivial_usage
21 //usage: "EXPRESSION..."
23 //usage:#define dc_full_usage "\n\n"
24 //usage: "Tiny RPN calculator. Operations:\n"
25 //usage: "+, add, -, sub, *, mul, /, div, %, mod, "IF_FEATURE_DC_LIBM("**, exp, ")"and, or, not, xor,\n"
26 //usage: "p - print top of the stack (without popping),\n"
27 //usage: "f - print entire stack,\n"
28 //usage: "o - pop the value and set output radix (must be 10, 16, 8 or 2).\n"
29 //usage: "Examples: 'dc 2 2 add p' -> 4, 'dc 8 8 mul 2 2 + / p' -> 16"
31 //usage:#define dc_example_usage
32 //usage: "$ dc 2 2 + p\n"
34 //usage: "$ dc 8 8 \\* 2 2 + / p\n"
36 //usage: "$ dc 0 1 and p\n"
38 //usage: "$ dc 0 1 or p\n"
40 //usage: "$ echo 72 9 div 8 mul p | dc\n"
44 #include "common_bufsiz.h"
48 typedef unsigned data_t;
51 typedef unsigned long data_t;
54 typedef unsigned long long data_t;
64 enum { STACK_SIZE = (COMMON_BUFSIZE - offsetof(struct globals, stack)) / sizeof(double) };
65 #define G (*(struct globals*)bb_common_bufsiz1)
66 #define pointer (G.pointer )
67 #define base (G.base )
68 #define stack (G.stack )
69 #define INIT_G() do { \
70 setup_common_bufsiz(); \
75 static void check_under(void)
78 bb_error_msg_and_die("stack underflow");
81 static void push(double a)
83 if (pointer >= STACK_SIZE)
84 bb_error_msg_and_die("stack overflow");
88 static double pop(void)
91 return stack[--pointer];
101 double subtrahend = pop();
103 push(pop() - subtrahend);
106 static void mul(void)
111 #if ENABLE_FEATURE_DC_LIBM
112 static void power(void)
114 double topower = pop();
116 push(pow(pop(), topower));
120 static void divide(void)
122 double divisor = pop();
124 push(pop() / divisor);
127 static void mod(void)
131 push((data_t) pop() % d);
134 static void and(void)
136 push((data_t) pop() & (data_t) pop());
141 push((data_t) pop() | (data_t) pop());
144 static void eor(void)
146 push((data_t) pop() ^ (data_t) pop());
149 static void not(void)
151 push(~(data_t) pop());
154 static void set_output_base(void)
156 static const char bases[] ALIGN1 = { 2, 8, 10, 16, 0 };
157 unsigned b = (unsigned)pop();
159 base = *strchrnul(bases, b);
161 bb_error_msg("error, base %u is not supported", b);
166 static void print_base(double print)
172 if (x == print) /* exactly representable as unsigned integer */
173 printf("%"DATA_FMT"u\n", x);
175 printf("%g\n", print);
181 printf("%"DATA_FMT"x\n", x);
184 printf("%"DATA_FMT"o\n", x);
186 default: /* base 2 */
187 i = MAXINT(data_t) - (MAXINT(data_t) >> 1);
188 /* i is 100000...00000 */
195 bb_putchar('1' - !(x & i));
202 static void print_stack_no_pop(void)
204 unsigned i = pointer;
206 print_base(stack[--i]);
209 static void print_no_pop(void)
212 print_base(stack[pointer-1]);
217 void (*function) (void);
220 static const struct op operators[] = {
221 #if ENABLE_FEATURE_DC_LIBM
242 {"f", print_stack_no_pop},
243 {"o", set_output_base},
246 /* Feed the stack machine */
247 static void stack_machine(const char *argument)
254 number = strtod(argument, &end);
255 if (end != argument) {
261 /* We might have matched a digit, eventually advance the argument */
262 argument = skip_whitespace(argument);
264 if (*argument == '\0')
269 char *after_name = is_prefixed_with(argument, o->name);
271 argument = after_name;
276 } while (o != operators + ARRAY_SIZE(operators));
278 bb_error_msg_and_die("syntax error at '%s'", argument);
281 int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
282 int dc_main(int argc UNUSED_PARAM, char **argv)
288 /* take stuff from stdin if no args are given */
290 while ((line = xmalloc_fgetline(stdin)) != NULL) {
296 stack_machine(*argv);