diff options
Diffstat (limited to 'math.c')
-rw-r--r-- | math.c | 106 |
1 files changed, 47 insertions, 59 deletions
@@ -1,3 +1,4 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
1 | #include "internal.h" | 2 | #include "internal.h" |
2 | #include <stdio.h> | 3 | #include <stdio.h> |
3 | #include <stdlib.h> | 4 | #include <stdlib.h> |
@@ -8,126 +9,114 @@ | |||
8 | 9 | ||
9 | static const char math_usage[] = "math expression ..."; | 10 | static const char math_usage[] = "math expression ..."; |
10 | 11 | ||
11 | static double stack[100]; | 12 | static double stack[100]; |
12 | static unsigned int pointer; | 13 | static unsigned int pointer; |
13 | 14 | ||
14 | static void | 15 | static void push(double a) |
15 | push(double a) | ||
16 | { | 16 | { |
17 | if ( pointer >= (sizeof(stack) / sizeof(*stack)) ) { | 17 | if (pointer >= (sizeof(stack) / sizeof(*stack))) { |
18 | fprintf(stderr, "math: stack overflow\n"); | 18 | fprintf(stderr, "math: stack overflow\n"); |
19 | exit(-1); | 19 | exit(-1); |
20 | } | 20 | } else |
21 | else | ||
22 | stack[pointer++] = a; | 21 | stack[pointer++] = a; |
23 | } | 22 | } |
24 | 23 | ||
25 | static double | 24 | static double pop() |
26 | pop() | ||
27 | { | 25 | { |
28 | if ( pointer == 0 ) { | 26 | if (pointer == 0) { |
29 | fprintf(stderr, "math: stack underflow\n"); | 27 | fprintf(stderr, "math: stack underflow\n"); |
30 | exit(-1); | 28 | exit(-1); |
31 | } | 29 | } |
32 | return stack[--pointer]; | 30 | return stack[--pointer]; |
33 | } | 31 | } |
34 | 32 | ||
35 | static void | 33 | static void add() |
36 | add() | ||
37 | { | 34 | { |
38 | push(pop() + pop()); | 35 | push(pop() + pop()); |
39 | } | 36 | } |
40 | 37 | ||
41 | static void | 38 | static void sub() |
42 | sub() | ||
43 | { | 39 | { |
44 | double subtrahend = pop(); | 40 | double subtrahend = pop(); |
45 | 41 | ||
46 | push(pop() - subtrahend); | 42 | push(pop() - subtrahend); |
47 | } | 43 | } |
48 | 44 | ||
49 | static void | 45 | static void mul() |
50 | mul() | ||
51 | { | 46 | { |
52 | push(pop() * pop()); | 47 | push(pop() * pop()); |
53 | } | 48 | } |
54 | 49 | ||
55 | static void | 50 | static void divide() |
56 | divide() | ||
57 | { | 51 | { |
58 | double divisor = pop(); | 52 | double divisor = pop(); |
53 | |||
59 | push(pop() / divisor); | 54 | push(pop() / divisor); |
60 | } | 55 | } |
61 | 56 | ||
62 | static void | 57 | static void and() |
63 | and() | ||
64 | { | 58 | { |
65 | push((unsigned int)pop() & (unsigned int)pop()); | 59 | push((unsigned int) pop() & (unsigned int) pop()); |
66 | } | 60 | } |
67 | 61 | ||
68 | static void | 62 | static void or() |
69 | or() | ||
70 | { | 63 | { |
71 | push((unsigned int)pop() | (unsigned int)pop()); | 64 | push((unsigned int) pop() | (unsigned int) pop()); |
72 | } | 65 | } |
73 | 66 | ||
74 | static void | 67 | static void eor() |
75 | eor() | ||
76 | { | 68 | { |
77 | push((unsigned int)pop() ^ (unsigned int)pop()); | 69 | push((unsigned int) pop() ^ (unsigned int) pop()); |
78 | } | 70 | } |
79 | 71 | ||
80 | static void | 72 | static void not() |
81 | not() | ||
82 | { | 73 | { |
83 | push(~(unsigned int)pop()); | 74 | push(~(unsigned int) pop()); |
84 | } | 75 | } |
85 | 76 | ||
86 | static void | 77 | static void print() |
87 | print() | ||
88 | { | 78 | { |
89 | printf("%g\n", pop()); | 79 | printf("%g\n", pop()); |
90 | } | 80 | } |
91 | 81 | ||
92 | struct op { | 82 | struct op { |
93 | const char * name; | 83 | const char *name; |
94 | void (*function)(); | 84 | void (*function) (); |
95 | }; | 85 | }; |
96 | 86 | ||
97 | static const struct op operators[] = { | 87 | static const struct op operators[] = { |
98 | { "add", add }, | 88 | {"add", add}, |
99 | { "and", and }, | 89 | {"and", and}, |
100 | { "div", divide }, | 90 | {"div", divide}, |
101 | { "eor", eor }, | 91 | {"eor", eor}, |
102 | { "mul", mul }, | 92 | {"mul", mul}, |
103 | { "not", not }, | 93 | {"not", not}, |
104 | { "or", or }, | 94 | {"or", or}, |
105 | { "sub", sub }, | 95 | {"sub", sub}, |
106 | { 0, 0 } | 96 | {0, 0} |
107 | }; | 97 | }; |
108 | 98 | ||
109 | static void | 99 | static void stack_machine(const char *argument) |
110 | stack_machine(const char * argument) | ||
111 | { | 100 | { |
112 | char * endPointer = 0; | 101 | char *endPointer = 0; |
113 | double d; | 102 | double d; |
114 | const struct op * o = operators; | 103 | const struct op *o = operators; |
115 | 104 | ||
116 | if ( argument == 0 ) { | 105 | if (argument == 0) { |
117 | print(); | 106 | print(); |
118 | return; | 107 | return; |
119 | } | 108 | } |
120 | 109 | ||
121 | d = strtod(argument, &endPointer); | 110 | d = strtod(argument, &endPointer); |
122 | 111 | ||
123 | if ( endPointer != argument ) { | 112 | if (endPointer != argument) { |
124 | push(d); | 113 | push(d); |
125 | return; | 114 | return; |
126 | } | 115 | } |
127 | 116 | ||
128 | while ( o->name != 0 ) { | 117 | while (o->name != 0) { |
129 | if ( strcmp(o->name, argument) == 0 ) { | 118 | if (strcmp(o->name, argument) == 0) { |
130 | (*(o->function))(); | 119 | (*(o->function)) (); |
131 | return; | 120 | return; |
132 | } | 121 | } |
133 | o++; | 122 | o++; |
@@ -136,10 +125,9 @@ stack_machine(const char * argument) | |||
136 | exit(-1); | 125 | exit(-1); |
137 | } | 126 | } |
138 | 127 | ||
139 | int | 128 | int math_main(int argc, char **argv) |
140 | math_main(int argc, char * * argv) | ||
141 | { | 129 | { |
142 | while ( argc >= 2 ) { | 130 | while (argc >= 2) { |
143 | stack_machine(argv[1]); | 131 | stack_machine(argv[1]); |
144 | argv++; | 132 | argv++; |
145 | argc--; | 133 | argc--; |