new operator modulo and fix operator priority for simple addition
[calc.git] / parser.c
1 #include <malloc.h>
2 #include <math.h>
3 #include <stdlib.h>
4
5 #include "debug.h"
6 #include "fdprintf.h"
7
8 #include "parser.h"
9
10 /* compare codes */
11
12 int codecmp (char *ref, char *str)
13 {
14 int sig;
15
16 while (*ref != '\0') {
17 if (*ref == '\t') {
18 sig = (*str == '.') ? -1 : ((*str >= '0') && (*str <= '9'));
19 } else {
20 sig = *str - *ref;
21 }
22 if (sig != 0) {
23 return (sig > 0) ? 1 : -1;
24 }
25 str++;
26 ref++;
27 }
28
29 return 0;
30 }
31
32 /* allocate new element */
33
34 element_t *newelement (func_t function, int nbops, int prio)
35 {
36 element_t *new = (element_t *) calloc (1, sizeof (element_t));
37 if (new == NULL) {
38 VERBOSE (ERROR, fdprintf (stdfderr, "can't allocate memory\n"));
39 return NULL;
40 }
41 new->func = function;
42 new->nbops = nbops;
43 new->prio = prio;
44
45 return new;
46 }
47
48 /* functions */
49
50 #define NB_OPERATORS 6
51
52 keyword_t operators[NB_OPERATORS] = {
53 { "+\t", Add, 2, 1, 1},
54 { "-\t", Sub, 2, 1, 1},
55 { "*", Mul, 2, 1, 2},
56 { "/", Div, 2, 1, 2},
57 { "%", Mod, 2, 1, 3},
58 { "^", Pow, 2, 1, 4}
59 };
60
61 #define NB_FUNCTIONS 7
62 keyword_t functions[NB_FUNCTIONS] = {
63 { "sqrt", Sqr, 1, 4, 5},
64 { "pow", Pow, 2, 3, 5},
65 { "cos", Cos, 1, 3, 5},
66 { "sin", Sin, 1, 3, 5},
67 { "atan", Atn, 1, 4, 5},
68 { "exp", Exp, 1, 3, 5},
69 { "log", Log, 1, 3, 5}
70 };
71
72 /* parser function */
73
74 element_t *parser (char *str, char **next, int prio)
75 {
76 element_t *root = NULL;
77 int i;
78
79 VERBOSE (DEBUG, PRINTOUT ("Starting parsing\n"));
80
81 /* main loop */
82 while (*str != '\0') {
83 int found = 0;
84 element_t *new = NULL;
85 VERBOSE (INFO, PRINTOUT ("Processing: %s\n", str));
86
87 /* skip spaces and tabs */
88
89 if ((*str == ' ') || (*str == '\t')) {
90 str++;
91 continue;
92 }
93
94 /* check for open bracket */
95
96 if (*str == '(') {
97 VERBOSE (DEBUG, PRINTOUT ("start processing bracket\n"));
98 if (root) {
99 do {
100 found = 0;
101 new = parser (str + 1, &str, 0);
102 if (new == ERROR_OP) {
103 return ERROR_OP;
104 }
105 for (i = 0; i < root->nbops; i++) {
106 if (root->ops[i] == NULL) {
107 root->ops[i] = new;
108 found = 1;
109 break;
110 }
111 }
112 if (!found) {
113 return ERROR_OP;
114 }
115 } while (*str == ',');
116 } else {
117 root = newelement (Val, 1, 4);
118 if (root == NULL) {
119 return ERROR_OP;
120 }
121 new = parser (str + 1, &str, 0);
122 if ((new == ERROR_OP) || (*str == ',')) {
123 return ERROR_OP;
124 }
125 root->ops[0] = new;
126 }
127 str++;
128 VERBOSE (DEBUG, PRINTOUT ("stop processing bracket\n"));
129 continue;
130 }
131
132 /* check for closing bracket or koma */
133
134 if ((*str == ')') || (*str == ',')) {
135 if (next != NULL) {
136 *next = str;
137 }
138 return root;
139 }
140
141 /* look for operators */
142
143 for (i = 0; i < NB_OPERATORS; i++) {
144 keyword_t *operator = operators + i;
145 if (codecmp (operator->keyword, str) == 0) {
146 VERBOSE (DEBUG, PRINTOUT ("start processing operator\n"));
147 if (root) {
148 if ((prio) && (prio > operator->prio)) {
149 VERBOSE (DEBUG, PRINTOUT ("stop because operator priority\n"));
150 *next = str;
151 return root;
152 }
153 str += operator->offset;
154 VERBOSE (INFO, PRINTOUT ("Oper: %d\n", operator->func));
155 new = newelement (operator->func, operator->nbops, operator->prio);
156 if (new == NULL) {
157 return ERROR_OP;
158 }
159 new->ops[0] = root;
160 new->ops[1] = parser (str, &str, new->prio);
161 if (new->ops[1] == ERROR_OP) {
162 return ERROR_OP;
163 }
164 root = newelement (Val, 1, 4);
165 if (root == ERROR_OP) {
166 return ERROR_OP;
167 }
168 root->ops[0] = new;
169 } else {
170 return ERROR_OP;
171 }
172 found = 1;
173 VERBOSE (DEBUG, PRINTOUT ("stop processing operator\n"));
174 break;
175 }
176 }
177 if (found) {
178 continue;
179 }
180
181 /* look for functions */
182
183 for (i = 0; i < NB_FUNCTIONS; i++) {
184 keyword_t *function = functions + i;
185 if (codecmp (function->keyword, str) == 0) {
186 VERBOSE (DEBUG, PRINTOUT ("start processing function\n"));
187 if (root == NULL) {
188 VERBOSE (INFO, PRINTOUT ("Func: %d\n", function->func));
189 new = newelement (function->func, function->nbops, function->prio);
190 if (new == NULL) {
191 return ERROR_OP;
192 }
193 root = new;
194 } else {
195 return ERROR_OP;
196 }
197 str += function->offset;
198 found = 1;
199 VERBOSE (DEBUG, PRINTOUT ("stop processing function\n"));
200 break;
201 }
202 }
203 if (found) {
204 continue;
205 }
206
207 /* look for number */
208
209 if (((*str >= '0') && (*str <= '9')) ||
210 (*str == '.') || (*str == '+') || (*str == '-')) {
211 VERBOSE (DEBUG, PRINTOUT ("start processing value\n"));
212 char *pt;
213 float value = strtof (str, &pt);
214 VERBOSE (INFO, PRINTOUT ("Value: %f\n", value));
215 if (str != pt) {
216 if (root == NULL) {
217 new = newelement (Val, 1, 4);
218 if (new == NULL) {
219 return ERROR_OP;
220 }
221 new->value = value;
222 root = new;
223 str = pt;
224 } else if (root->func == Val) {
225 if ((*str == '+') || (*str == '-')) {
226 if ((prio) && (prio > 1)) {
227 VERBOSE (DEBUG, PRINTOUT ("stop because operator priority\n"));
228 *next = str;
229 return root;
230 }
231 new = newelement (Add, 2, 1);
232 if (new == NULL) {
233 return ERROR_OP;
234 }
235 new->ops[0] = root;
236 new->ops[1] = parser (str, &str, new->prio);
237 if (new->ops[1] == ERROR_OP) {
238 return ERROR_OP;
239 }
240 root = newelement (Val, 1, 4);
241 if (root == ERROR_OP) {
242 return ERROR_OP;
243 }
244 root->ops[0] = new;
245 } else {
246 return ERROR_OP;
247 }
248 } else {
249 return ERROR_OP;
250 }
251 found = 1;
252 }
253 VERBOSE (DEBUG, PRINTOUT ("stop processing value\n"));
254 }
255
256 /* error */
257
258 if (!found) {
259 return ERROR_OP;
260 }
261
262 }
263
264 if (next != NULL) {
265 *next = str;
266 }
267 return root;
268 }
269
270 /* print element tree */
271
272 void print_element (element_t *root, int level)
273 {
274 char *func = NULL;
275 int i;
276
277 if ((root == NULL) || (root == ERROR_OP)) {
278 return;
279 }
280
281 for (i = 0; i < level; i++) {
282 PRINTOUT (" ");
283 }
284
285 switch (root->func) {
286 case Val: func = "Value"; break;
287 case Add: func = "Addition"; break;
288 case Sub: func = "Subtraction"; break;
289 case Mul: func = "Multiplication"; break;
290 case Div: func = "Division"; break;
291 case Mod: func = "Modulo"; break;
292 case Pow: func = "Power"; break;
293 case Sqr: func = "Square Root"; break;
294 case Cos: func = "Cosine"; break;
295 case Sin: func = "Sine"; break;
296 case Atn: func = "Arc Tangent"; break;
297 case Log: func = "Logarithm"; break;
298 case Exp: func = "Exponantial"; break;
299 }
300
301 PRINTOUT ("Function: %s\n", func);
302
303 if ((root->func == Val) && (root->ops[0] == NULL)) {
304 for (i = 0; i < level; i++) {
305 PRINTOUT (" ");
306 }
307 PRINTOUT ("value: %f\n", root->value);
308 } else {
309 for (i = 0; i < root->nbops; i++) {
310 print_element (root->ops[i], level + 1);
311 }
312 }
313 }
314
315 /* evaluate element tree */
316
317 double evaluate_element (element_t *root)
318 {
319 double op0 = 0, op1 = 0;
320
321 if ((root == NULL) || (root == ERROR_OP)) {
322 VERBOSE (WARNING, PRINTOUT ("error while evaluating\n"));
323 return 0;
324 }
325
326 switch (root->func) {
327 case Val:
328 if (root->ops[0]) {
329 return evaluate_element (root->ops[0]);
330 } else {
331 return root->value;
332 }
333 break;
334 case Add:
335 case Sub:
336 case Mul:
337 case Div:
338 case Mod:
339 case Pow:
340 if (root->ops[1]) {
341 op1 = evaluate_element (root->ops[1]);
342 } else {
343 VERBOSE (WARNING, PRINTOUT ("error while evaluating (op[1])\n"));
344 return 0;
345 }
346 /* fallthrough */
347 case Sqr:
348 case Cos:
349 case Sin:
350 case Atn:
351 case Log:
352 case Exp:
353 if (root->ops[0]) {
354 op0 = evaluate_element (root->ops[0]);
355 } else {
356 VERBOSE (WARNING, PRINTOUT ("error while evaluating (op[0])\n"));
357 return 0;
358 }
359 }
360
361 switch (root->func) {
362 case Add: return op0 + op1;
363 case Sub: return op0 - op1;
364 case Mul: return op0 * op1;
365 case Div: return op0 / op1;
366 case Mod: return fmod (op0, op1);
367 case Pow: return pow (op0, op1);
368 case Sqr: return sqrt (op0);
369 case Cos: return cos (op0);
370 case Sin: return sin (op0);
371 case Atn: return atan (op0);
372 case Log: return log (op0);
373 case Exp: return exp (op0);
374 default: break;
375 }
376
377 return 0;
378 }
379
380 /* vim: set ts=4 sw=4 et: */