7 /* Tiny RPN calculator, because "expr" didn't give me bitwise operations. */
9 static const char math_usage[] = "math expression ...";
11 static double stack[100];
12 static unsigned int pointer;
17 if ( pointer >= (sizeof(stack) / sizeof(*stack)) ) {
18 fprintf(stderr, "math: stack overflow\n");
29 fprintf(stderr, "math: stack underflow\n");
32 return stack[--pointer];
44 double subtrahend = pop();
46 push(pop() - subtrahend);
58 double divisor = pop();
59 push(pop() / divisor);
65 push((unsigned int)pop() & (unsigned int)pop());
71 push((unsigned int)pop() | (unsigned int)pop());
77 push((unsigned int)pop() ^ (unsigned int)pop());
83 push(~(unsigned int)pop());
89 printf("%g\n", pop());
97 static const struct op operators[] = {
110 stack_machine(const char * argument)
112 char * endPointer = 0;
114 const struct op * o = operators;
116 if ( argument == 0 ) {
121 d = strtod(argument, &endPointer);
123 if ( endPointer != argument ) {
128 while ( o->name != 0 ) {
129 if ( strcmp(o->name, argument) == 0 ) {
135 fprintf(stderr, "math: %s: syntax error.\n", argument);
140 math_main(int argc, char * * argv)
142 while ( argc >= 2 ) {
143 stack_machine(argv[1]);