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 { \
60 static void check_under(void)
63 bb_error_msg_and_die("stack underflow");
66 static void push(double a)
68 if (pointer >= STACK_SIZE)
69 bb_error_msg_and_die("stack overflow");
73 static double pop(void)
76 return stack[--pointer];
86 double subtrahend = pop();
88 push(pop() - subtrahend);
96 #if ENABLE_FEATURE_DC_LIBM
97 static void power(void)
99 double topower = pop();
101 push(pow(pop(), topower));
105 static void divide(void)
107 double divisor = pop();
109 push(pop() / divisor);
112 static void mod(void)
116 push((data_t) pop() % d);
119 static void and(void)
121 push((data_t) pop() & (data_t) pop());
126 push((data_t) pop() | (data_t) pop());
129 static void eor(void)
131 push((data_t) pop() ^ (data_t) pop());
134 static void not(void)
136 push(~(data_t) pop());
139 static void set_output_base(void)
141 static const char bases[] ALIGN1 = { 2, 8, 10, 16, 0 };
142 unsigned b = (unsigned)pop();
144 base = *strchrnul(bases, b);
146 bb_error_msg("error, base %u is not supported", b);
151 static void print_base(double print)
157 if (x == print) /* exactly representable as unsigned integer */
158 printf("%"DATA_FMT"u\n", x);
160 printf("%g\n", print);
166 printf("%"DATA_FMT"x\n", x);
169 printf("%"DATA_FMT"o\n", x);
171 default: /* base 2 */
172 i = MAXINT(data_t) - (MAXINT(data_t) >> 1);
173 /* i is 100000...00000 */
180 bb_putchar('1' - !(x & i));
187 static void print_stack_no_pop(void)
189 unsigned i = pointer;
191 print_base(stack[--i]);
194 static void print_no_pop(void)
197 print_base(stack[pointer-1]);
202 void (*function) (void);
205 static const struct op operators[] = {
206 #if ENABLE_FEATURE_DC_LIBM
227 {"f", print_stack_no_pop},
228 {"o", set_output_base},
231 /* Feed the stack machine */
232 static void stack_machine(const char *argument)
239 number = strtod(argument, &end);
240 if (end != argument) {
246 /* We might have matched a digit, eventually advance the argument */
247 argument = skip_whitespace(argument);
249 if (*argument == '\0')
254 char *after_name = is_prefixed_with(argument, o->name);
256 argument = after_name;
261 } while (o != operators + ARRAY_SIZE(operators));
263 bb_error_msg_and_die("syntax error at '%s'", argument);
266 int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
267 int dc_main(int argc UNUSED_PARAM, char **argv)
273 /* take stuff from stdin if no args are given */
275 while ((line = xmalloc_fgetline(stdin)) != NULL) {
281 stack_machine(*argv);