Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 2 | #include "internal.h" |
John Beppu | 5db60a7 | 2000-06-12 22:59:12 +0000 | [diff] [blame] | 3 | #include <ctype.h> |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 4 | #include <stdio.h> |
| 5 | #include <stdlib.h> |
| 6 | #include <unistd.h> |
| 7 | #include <math.h> |
| 8 | |
| 9 | /* Tiny RPN calculator, because "expr" didn't give me bitwise operations. */ |
| 10 | |
John Beppu | 0021679 | 2000-06-21 19:06:16 +0000 | [diff] [blame^] | 11 | static const char dc_usage[] = "math expression ...\n" |
Erik Andersen | 7ab9c7e | 2000-05-12 19:41:47 +0000 | [diff] [blame] | 12 | #ifndef BB_FEATURE_TRIVIAL_HELP |
| 13 | "\nThis is a Tiny RPN calculator that understands the\n" |
Erik Andersen | 5e1189e | 2000-04-15 16:34:54 +0000 | [diff] [blame] | 14 | "following operations: +, -, /, *, and, or, not, eor.\n" |
Erik Andersen | 7ab9c7e | 2000-05-12 19:41:47 +0000 | [diff] [blame] | 15 | "i.e. 'math 2 2 add' -> 4, and 'math 8 8 \\* 2 2 + /' -> 16\n" |
| 16 | #endif |
| 17 | ; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 18 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 19 | static double stack[100]; |
| 20 | static unsigned int pointer; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 21 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 22 | static void push(double a) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 23 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 24 | if (pointer >= (sizeof(stack) / sizeof(*stack))) { |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 25 | fprintf(stderr, "math: stack overflow\n"); |
| 26 | exit(-1); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 27 | } else |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 28 | stack[pointer++] = a; |
| 29 | } |
| 30 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 31 | static double pop() |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 32 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 33 | if (pointer == 0) { |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 34 | fprintf(stderr, "math: stack underflow\n"); |
| 35 | exit(-1); |
| 36 | } |
| 37 | return stack[--pointer]; |
| 38 | } |
| 39 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 40 | static void add() |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 41 | { |
| 42 | push(pop() + pop()); |
| 43 | } |
| 44 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 45 | static void sub() |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 46 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 47 | double subtrahend = pop(); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 48 | |
| 49 | push(pop() - subtrahend); |
| 50 | } |
| 51 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 52 | static void mul() |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 53 | { |
| 54 | push(pop() * pop()); |
| 55 | } |
| 56 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 57 | static void divide() |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 58 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 59 | double divisor = pop(); |
| 60 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 61 | push(pop() / divisor); |
| 62 | } |
| 63 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 64 | static void and() |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 65 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 66 | push((unsigned int) pop() & (unsigned int) pop()); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 69 | static void or() |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 70 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 71 | push((unsigned int) pop() | (unsigned int) pop()); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 74 | static void eor() |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 75 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 76 | push((unsigned int) pop() ^ (unsigned int) pop()); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 79 | static void not() |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 80 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 81 | push(~(unsigned int) pop()); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 84 | static void print() |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 85 | { |
| 86 | printf("%g\n", pop()); |
| 87 | } |
| 88 | |
| 89 | struct op { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 90 | const char *name; |
| 91 | void (*function) (); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 92 | }; |
| 93 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 94 | static const struct op operators[] = { |
John Beppu | c035254 | 2000-06-21 18:00:46 +0000 | [diff] [blame] | 95 | {"+", add}, |
| 96 | {"add", add}, |
| 97 | {"-", sub}, |
| 98 | {"sub", sub}, |
| 99 | {"*", mul}, |
| 100 | {"mul", mul}, |
| 101 | {"/", divide}, |
| 102 | {"div", divide}, |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 103 | {"and", and}, |
John Beppu | c035254 | 2000-06-21 18:00:46 +0000 | [diff] [blame] | 104 | {"or", or}, |
Erik Andersen | 5e1189e | 2000-04-15 16:34:54 +0000 | [diff] [blame] | 105 | {"not", not}, |
| 106 | {"eor", eor}, |
John Beppu | c035254 | 2000-06-21 18:00:46 +0000 | [diff] [blame] | 107 | {0, 0} |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 108 | }; |
| 109 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 110 | static void stack_machine(const char *argument) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 111 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 112 | char *endPointer = 0; |
| 113 | double d; |
| 114 | const struct op *o = operators; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 115 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 116 | if (argument == 0) { |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 117 | print(); |
| 118 | return; |
| 119 | } |
| 120 | |
| 121 | d = strtod(argument, &endPointer); |
| 122 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 123 | if (endPointer != argument) { |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 124 | push(d); |
| 125 | return; |
| 126 | } |
| 127 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 128 | while (o->name != 0) { |
| 129 | if (strcmp(o->name, argument) == 0) { |
| 130 | (*(o->function)) (); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 131 | return; |
| 132 | } |
| 133 | o++; |
| 134 | } |
| 135 | fprintf(stderr, "math: %s: syntax error.\n", argument); |
| 136 | exit(-1); |
| 137 | } |
| 138 | |
John Beppu | 5db60a7 | 2000-06-12 22:59:12 +0000 | [diff] [blame] | 139 | /* return pointer to next token in buffer and set *buffer to one char |
| 140 | * past the end of the above mentioned token |
| 141 | */ |
| 142 | static char *get_token(char **buffer) |
| 143 | { |
| 144 | char *start = NULL; |
| 145 | char *current = *buffer; |
| 146 | |
| 147 | while (isspace(*current)) { current++; } |
| 148 | if (*current != 0) { |
| 149 | start = current; |
| 150 | while (!isspace(*current) && current != 0) { current++; } |
| 151 | *buffer = current; |
| 152 | } |
| 153 | return start; |
| 154 | } |
| 155 | |
| 156 | /* In Perl one might say, scalar m|\s*(\S+)\s*|g */ |
| 157 | static int number_of_tokens(char *buffer) |
| 158 | { |
| 159 | int i = 0; |
| 160 | char *b = buffer; |
| 161 | while (get_token(&b)) { i++; } |
| 162 | return i; |
| 163 | } |
| 164 | |
John Beppu | 0021679 | 2000-06-21 19:06:16 +0000 | [diff] [blame^] | 165 | int dc_main(int argc, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 166 | { |
John Beppu | 5db60a7 | 2000-06-12 22:59:12 +0000 | [diff] [blame] | 167 | /* take stuff from stdin if no args are given */ |
| 168 | if (argc <= 1) { |
| 169 | int i, len; |
| 170 | char *line = NULL; |
| 171 | char *cursor = NULL; |
| 172 | char *token = NULL; |
| 173 | while ((line = cstring_lineFromFile(stdin))) { |
| 174 | cursor = line; |
| 175 | len = number_of_tokens(line); |
| 176 | for (i = 0; i < len; i++) { |
| 177 | token = get_token(&cursor); |
| 178 | *cursor++ = 0; |
| 179 | stack_machine(token); |
| 180 | } |
| 181 | free(line); |
| 182 | } |
| 183 | } else { |
| 184 | if (*argv[1]=='-') |
John Beppu | 0021679 | 2000-06-21 19:06:16 +0000 | [diff] [blame^] | 185 | usage(dc_usage); |
John Beppu | 5db60a7 | 2000-06-12 22:59:12 +0000 | [diff] [blame] | 186 | while (argc >= 2) { |
| 187 | stack_machine(argv[1]); |
| 188 | argv++; |
| 189 | argc--; |
| 190 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 191 | } |
| 192 | stack_machine(0); |
Eric Andersen | b610615 | 2000-06-19 17:25:40 +0000 | [diff] [blame] | 193 | return( TRUE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 194 | } |