diff options
Diffstat (limited to 'math.c')
-rw-r--r-- | math.c | 21 |
1 files changed, 13 insertions, 8 deletions
@@ -7,7 +7,10 @@ | |||
7 | 7 | ||
8 | /* Tiny RPN calculator, because "expr" didn't give me bitwise operations. */ | 8 | /* Tiny RPN calculator, because "expr" didn't give me bitwise operations. */ |
9 | 9 | ||
10 | static const char math_usage[] = "math expression ..."; | 10 | static const char math_usage[] = "math expression ...\n\n" |
11 | "This is a Tiny RPN calculator that understands the\n" | ||
12 | "following operations: +, -, /, *, and, or, not, eor.\n" | ||
13 | "i.e. 'math 2 2 add' -> 4, and 'math 8 8 \\* 2 2 + /' -> 16\n"; | ||
11 | 14 | ||
12 | static double stack[100]; | 15 | static double stack[100]; |
13 | static unsigned int pointer; | 16 | static unsigned int pointer; |
@@ -85,14 +88,14 @@ struct op { | |||
85 | }; | 88 | }; |
86 | 89 | ||
87 | static const struct op operators[] = { | 90 | static const struct op operators[] = { |
88 | {"add", add}, | 91 | {"+", add}, |
92 | {"-", sub}, | ||
93 | {"*", mul}, | ||
94 | {"/", divide}, | ||
89 | {"and", and}, | 95 | {"and", and}, |
90 | {"div", divide}, | ||
91 | {"eor", eor}, | ||
92 | {"mul", mul}, | ||
93 | {"not", not}, | ||
94 | {"or", or}, | 96 | {"or", or}, |
95 | {"sub", sub}, | 97 | {"not", not}, |
98 | {"eor", eor}, | ||
96 | {0, 0} | 99 | {0, 0} |
97 | }; | 100 | }; |
98 | 101 | ||
@@ -127,11 +130,13 @@ static void stack_machine(const char *argument) | |||
127 | 130 | ||
128 | int math_main(int argc, char **argv) | 131 | int math_main(int argc, char **argv) |
129 | { | 132 | { |
133 | if (argc < 1 || *argv[1]=='-') | ||
134 | usage(math_usage); | ||
130 | while (argc >= 2) { | 135 | while (argc >= 2) { |
131 | stack_machine(argv[1]); | 136 | stack_machine(argv[1]); |
132 | argv++; | 137 | argv++; |
133 | argc--; | 138 | argc--; |
134 | } | 139 | } |
135 | stack_machine(0); | 140 | stack_machine(0); |
136 | return 0; | 141 | exit( TRUE); |
137 | } | 142 | } |