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 check_under(void)
62 bb_error_msg_and_die("stack underflow");
65 static void push(double a)
67 if (pointer >= STACK_SIZE)
68 bb_error_msg_and_die("stack overflow");
72 static double pop(void)
75 return stack[--pointer];
85 double subtrahend = pop();
87 push(pop() - subtrahend);
95 #if ENABLE_FEATURE_DC_LIBM
96 static void power(void)
98 double topower = pop();
100 push(pow(pop(), topower));
104 static void divide(void)
106 double divisor = pop();
108 push(pop() / divisor);
111 static void mod(void)
115 push((data_t) pop() % d);
118 static void and(void)
120 push((data_t) pop() & (data_t) pop());
125 push((data_t) pop() | (data_t) pop());
128 static void eor(void)
130 push((data_t) pop() ^ (data_t) pop());
133 static void not(void)
135 push(~(data_t) pop());
138 static void set_output_base(void)
140 static const char bases[] ALIGN1 = { 2, 8, 10, 16, 0 };
141 unsigned b = (unsigned)pop();
143 base = *strchrnul(bases, b);
145 bb_error_msg("error, base %u is not supported", b);
150 static void print_base(double print)
156 if (x == print) /* exactly representable as unsigned integer */
157 printf("%"DATA_FMT"u\n", x);
159 printf("%g\n", print);
165 printf("%"DATA_FMT"x\n", x);
168 printf("%"DATA_FMT"o\n", x);
170 default: /* base 2 */
171 i = MAXINT(data_t) - (MAXINT(data_t) >> 1);
172 /* i is 100000...00000 */
179 bb_putchar('1' - !(x & i));
186 static void print_stack_no_pop(void)
188 unsigned i = pointer;
190 print_base(stack[--i]);
193 static void print_no_pop(void)
196 print_base(stack[pointer-1]);
201 void (*function) (void);
204 static const struct op operators[] = {
205 #if ENABLE_FEATURE_DC_LIBM
226 {"f", print_stack_no_pop},
227 {"o", set_output_base},
230 /* Feed the stack machine */
231 static void stack_machine(const char *argument)
238 number = strtod(argument, &end);
239 if (end != argument) {
245 /* We might have matched a digit, eventually advance the argument */
246 argument = skip_whitespace(argument);
248 if (*argument == '\0')
253 char *after_name = is_prefixed_with(argument, o->name);
255 argument = after_name;
260 } while (o != operators + ARRAY_SIZE(operators));
262 bb_error_msg_and_die("syntax error at '%s'", argument);
265 int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
266 int dc_main(int argc UNUSED_PARAM, char **argv)
272 /* take stuff from stdin if no args are given */
274 while ((line = xmalloc_fgetline(stdin)) != NULL) {
280 stack_machine(*argv);