1 /* vi: set sw=4 ts=4: */
3 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
6 /* config/applet/usage bits are in bc.c */
9 //#include "common_bufsiz.h"
13 typedef unsigned data_t;
16 typedef unsigned long data_t;
19 typedef unsigned long long data_t;
29 enum { STACK_SIZE = (COMMON_BUFSIZE - offsetof(struct globals, stack)) / sizeof(double) };
30 #define G (*(struct globals*)bb_common_bufsiz1)
31 #define pointer (G.pointer )
32 #define base (G.base )
33 #define stack (G.stack )
34 #define INIT_G() do { \
35 setup_common_bufsiz(); \
40 static void check_under(void)
43 bb_error_msg_and_die("stack underflow");
46 static void push(double a)
48 if (pointer >= STACK_SIZE)
49 bb_error_msg_and_die("stack overflow");
53 static double pop(void)
56 return stack[--pointer];
66 double subtrahend = pop();
68 push(pop() - subtrahend);
76 #if ENABLE_FEATURE_DC_LIBM
77 static void power(void)
79 double topower = pop();
81 push(pow(pop(), topower));
85 static void divide(void)
87 double divisor = pop();
89 push(pop() / divisor);
96 push((data_t) pop() % d);
101 push((data_t) pop() & (data_t) pop());
106 push((data_t) pop() | (data_t) pop());
109 static void eor(void)
111 push((data_t) pop() ^ (data_t) pop());
114 static void not(void)
116 push(~(data_t) pop());
119 static void set_output_base(void)
121 static const char bases[] ALIGN1 = { 2, 8, 10, 16, 0 };
122 unsigned b = (unsigned)pop();
124 base = *strchrnul(bases, b);
126 bb_error_msg("error, base %u is not supported", b);
131 static void print_base(double print)
137 if (x == print) /* exactly representable as unsigned integer */
138 printf("%"DATA_FMT"u\n", x);
140 printf("%g\n", print);
146 printf("%"DATA_FMT"x\n", x);
149 printf("%"DATA_FMT"o\n", x);
151 default: /* base 2 */
152 i = MAXINT(data_t) - (MAXINT(data_t) >> 1);
153 /* i is 100000...00000 */
160 bb_putchar('1' - !(x & i));
167 static void print_stack_no_pop(void)
169 unsigned i = pointer;
171 print_base(stack[--i]);
174 static void print_no_pop(void)
177 print_base(stack[pointer-1]);
182 void (*function) (void);
185 static const struct op operators[] = {
186 #if ENABLE_FEATURE_DC_LIBM
207 {"f", print_stack_no_pop},
208 {"o", set_output_base},
211 /* Feed the stack machine */
212 static void stack_machine(const char *argument)
219 number = strtod(argument, &end);
220 if (end != argument) {
226 /* We might have matched a digit, eventually advance the argument */
227 argument = skip_whitespace(argument);
229 if (*argument == '\0')
234 char *after_name = is_prefixed_with(argument, o->name);
236 argument = after_name;
241 } while (o != operators + ARRAY_SIZE(operators));
243 bb_error_msg_and_die("syntax error at '%s'", argument);
246 int dc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
247 int dc_main(int argc UNUSED_PARAM, char **argv)
251 //TODO: fix this, should take: dc -eSCRIPT -fFILE FILE
254 /* take stuff from stdin if no args are given */
256 while ((line = xmalloc_fgetline(stdin)) != NULL) {
262 stack_machine(*argv);