add comparison operators
[calc.git] / parser.c
1 #include <malloc.h>
2 #include <math.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5
6 #include "debug.h"
7
8 #include "parser.h"
9
10 /* global variables */
11
12 double answer = 0;
13
14 #define STORAGE_SIZE 10
15 double storage[STORAGE_SIZE] = {0};
16
17 /* compare codes */
18
19 int codecmp (char *ref, char *str)
20 {
21 int sig;
22
23 while (*ref != '\0') {
24 if (*ref == '\t') {
25 sig = (*str == '.') ? -1 : ((*str >= '0') && (*str <= '9'));
26 } else {
27 sig = *str - *ref;
28 }
29 if (sig != 0) {
30 return (sig > 0) ? 1 : -1;
31 }
32 str++;
33 ref++;
34 }
35
36 return 0;
37 }
38
39 /* allocate new element */
40
41 element_t *newelement (func_t function, int nbops, int prio)
42 {
43 element_t *new = (element_t *) calloc (1, sizeof (element_t));
44 if (new == NULL) {
45 VERBOSE (ERROR, fprintf (stderr, "can't allocate memory\n"));
46 return NULL;
47 }
48 new->func = function;
49 new->nbops = nbops;
50 new->prio = prio;
51
52 return new;
53 }
54
55 /* desallocate element */
56
57 void delelement (element_t *root)
58 {
59 int i;
60 if ((root != NULL) && (root != ERROR_OP)) {
61 for (i = 0; i < root->nbops; i++) {
62 if ((root->ops[i] != NULL) && (root->ops[i] != ERROR_OP)) {
63 delelement (root->ops[i]);
64 }
65 }
66 free (root);
67 }
68 }
69
70 /* functions */
71
72 #define NB_OPERATORS 12
73
74 keyword_t operators[NB_OPERATORS] = {
75 { "+\t", Add, 2, 1, 1},
76 { "-\t", Sub, 2, 1, 1},
77 { "*", Mul, 2, 1, 2},
78 { "/", Div, 2, 1, 2},
79 { "%", Mod, 2, 1, 3},
80 { "^", Pow, 2, 1, 4},
81 { "==", Equal, 2, 2, 6},
82 { "!=", Diff, 2, 2, 6},
83 { ">=", Ge, 2, 2, 6},
84 { "<=", Le, 2, 2, 6},
85 { ">", Gt, 2, 1, 6},
86 { "<", Lt, 2, 1, 6}
87 };
88
89 #define NB_FUNCTIONS 12
90 keyword_t functions[NB_FUNCTIONS] = {
91 { "sqrt", Sqr, 1, 4, 5},
92 { "pow", Pow, 2, 3, 5},
93 { "cos", Cos, 1, 3, 5},
94 { "sin", Sin, 1, 3, 5},
95 { "atan", Atan, 1, 4, 5},
96 { "exp", Exp, 1, 3, 5},
97 { "log", Log, 1, 3, 5},
98 { "sto", Store, 2, 3, 5},
99 { "rcl", Recall, 1, 3, 5},
100 { "disp", Disp, 0, 4, 5},
101 { "quit", Quit, 0, 4, 5},
102 { "help", Help, 0, 4, 5}
103 };
104
105 #define NB_CONSTANTS 3
106 keyword_t constants[NB_CONSTANTS] = {
107 { "ans", Ans, 0, 3, 5},
108 { "e", E, 0, 1, 5},
109 { "pi", Pi, 0, 2, 5}
110 };
111
112 /* subparser function */
113
114 element_t *subparser (element_t **proot, char **pstr, func_t func, int nbops, int prio)
115 {
116 element_t *new = newelement (func, nbops, prio);
117 if (new == NULL) {
118 return ERROR_OP;
119 }
120 new->ops[0] = *proot;
121 new->ops[1] = parser (*pstr, pstr, new->prio);
122 if (new->ops[1] == ERROR_OP) {
123 delelement (new);
124 *proot = NULL;
125 return ERROR_OP;
126 }
127 *proot = newelement (Val, 1, 5);
128 if (*proot == NULL) {
129 delelement (new);
130 return ERROR_OP;
131 }
132 (*proot)->ops[0] = new;
133
134 return *proot;
135 }
136
137 /* parser function */
138
139 element_t *parser (char *str, char **next, int prio)
140 {
141 element_t *root = NULL;
142 int i;
143
144 VERBOSE (DEBUG, fprintf (stdout, "Starting parsing\n"));
145
146 /* main loop */
147 while (*str != '\0') {
148 int found = 0;
149 element_t *new = NULL;
150 VERBOSE (INFO, fprintf (stdout, "Processing: %s\n", str));
151
152 /* skip spaces and tabs */
153
154 if ((*str == ' ') || (*str == '\t')) {
155 str++;
156 continue;
157 }
158
159 /* check for open bracket */
160
161 if (*str == '(') {
162 VERBOSE (DEBUG, fprintf (stdout, "start processing bracket\n"));
163 if (root) {
164 do {
165 found = 0;
166 new = parser (str + 1, &str, 0);
167 if (new == ERROR_OP) {
168 delelement (root);
169 return ERROR_OP;
170 }
171 for (i = 0; i < root->nbops; i++) {
172 if (root->ops[i] == NULL) {
173 root->ops[i] = new;
174 found = 1;
175 break;
176 }
177 }
178 if (!found) {
179 delelement (new);
180 delelement (root);
181 return ERROR_OP;
182 }
183 } while (*str == ',');
184 } else {
185 root = newelement (Val, 1, 5);
186 if (root == NULL) {
187 return ERROR_OP;
188 }
189 new = parser (str + 1, &str, 0);
190 if ((new == ERROR_OP) || (*str == ',')) {
191 delelement (new);
192 delelement (root);
193 return ERROR_OP;
194 }
195 root->ops[0] = new;
196 }
197 str++;
198 VERBOSE (DEBUG, fprintf (stdout, "stop processing bracket\n"));
199 continue;
200 }
201
202 /* check for closing bracket or koma */
203
204 if ((*str == ')') || (*str == ',')) {
205 if (next != NULL) {
206 *next = str;
207 }
208 return root;
209 }
210
211 /* look for operators */
212
213 for (i = 0; i < NB_OPERATORS; i++) {
214 keyword_t *operator = operators + i;
215 if (codecmp (operator->keyword, str) == 0) {
216 VERBOSE (DEBUG, fprintf (stdout, "start processing operator\n"));
217 if (root) {
218 if ((prio) && (prio > operator->prio)) {
219 VERBOSE (DEBUG, fprintf (stdout, "stop because operator priority\n"));
220 *next = str;
221 return root;
222 }
223 str += operator->offset;
224 VERBOSE (INFO, fprintf (stdout, "Oper: %d\n", operator->func));
225 if (subparser (&root, &str, operator->func, operator->nbops, operator->prio) == ERROR_OP) {
226 delelement (root);
227 return ERROR_OP;
228 }
229 } else if (*str == '-') {
230 new = newelement (Sig, 1, 9);
231 if (new == NULL) {
232 return ERROR_OP;
233 }
234 root = new;
235 } else {
236 return ERROR_OP;
237 }
238 found = 1;
239 VERBOSE (DEBUG, fprintf (stdout, "stop processing operator\n"));
240 break;
241 }
242 }
243 if (found) {
244 continue;
245 }
246
247 /* look for functions */
248
249 for (i = 0; i < NB_FUNCTIONS; i++) {
250 keyword_t *function = functions + i;
251 if (codecmp (function->keyword, str) == 0) {
252 VERBOSE (DEBUG, fprintf (stdout, "start processing function\n"));
253 if (root == NULL) {
254 VERBOSE (INFO, fprintf (stdout, "Func: %d\n", function->func));
255 new = newelement (function->func, function->nbops, function->prio);
256 if (new == NULL) {
257 return ERROR_OP;
258 }
259 root = new;
260 } else {
261 delelement (root);
262 return ERROR_OP;
263 }
264 str += function->offset;
265 found = 1;
266 VERBOSE (DEBUG, fprintf (stdout, "stop processing function\n"));
267 break;
268 }
269 }
270 if (found) {
271 continue;
272 }
273
274 /* look for constant */
275
276 for (i = 0; i < NB_CONSTANTS; i++) {
277 keyword_t *constant = constants + i;
278 if (codecmp (constant->keyword, str) == 0) {
279 VERBOSE (DEBUG, fprintf (stdout, "start processing constant\n"));
280 if (root == NULL) {
281 VERBOSE (INFO, fprintf (stdout, "Const: %d\n", constant->func));
282 new = newelement (constant->func, constant->nbops, constant->prio);
283 if (new == NULL) {
284 return ERROR_OP;
285 }
286 root = new;
287 } else {
288 delelement (root);
289 return ERROR_OP;
290 }
291 str += constant->offset;
292 found = 1;
293 VERBOSE (DEBUG, fprintf (stdout, "stop processing constant\n"));
294 break;
295 }
296 }
297 if (found) {
298 continue;
299 }
300
301 /* look for number */
302
303 if (((*str >= '0') && (*str <= '9')) ||
304 (*str == '.') || (*str == '+') || (*str == '-')) {
305 VERBOSE (DEBUG, fprintf (stdout, "start processing value\n"));
306 char *pt;
307 double value = strtod (str, &pt);
308 VERBOSE (INFO, fprintf (stdout, "Value: %f\n", value));
309 if (str != pt) {
310 if (root == NULL) {
311 new = newelement (Val, 1, 5);
312 if (new == NULL) {
313 return ERROR_OP;
314 }
315 new->value = value;
316 root = new;
317 str = pt;
318 } else if ((*str == '+') || (*str == '-')) {
319 if ((prio) && (prio > 1)) {
320 VERBOSE (DEBUG, fprintf (stdout, "stop because operator priority\n"));
321 *next = str;
322 return root;
323 }
324 if (subparser (&root, &str, Add, 2, 1) == ERROR_OP) {
325 delelement (root);
326 return ERROR_OP;
327 }
328 } else {
329 delelement (root);
330 return ERROR_OP;
331 }
332 found = 1;
333 }
334 VERBOSE (DEBUG, fprintf (stdout, "stop processing value\n"));
335 }
336
337 /* error */
338
339 if (!found) {
340 delelement (root);
341 return ERROR_OP;
342 }
343
344 }
345
346 if (next != NULL) {
347 *next = str;
348 }
349
350 return root;
351 }
352
353 /* print element tree */
354
355 void print_element (element_t *root, int level)
356 {
357 char *func = NULL;
358 int i;
359
360 if ((root == NULL) || (root == ERROR_OP)) {
361 return;
362 }
363
364 for (i = 0; i < level; i++) {
365 fprintf (stdout, " ");
366 }
367
368 switch (root->func) {
369 case Val: func = "Value"; break;
370 case Sig: func = "Sign"; break;
371 case Add: func = "Addition"; break;
372 case Sub: func = "Subtraction"; break;
373 case Mul: func = "Multiplication"; break;
374 case Div: func = "Division"; break;
375 case Mod: func = "Modulo"; break;
376 case Pow: func = "Power"; break;
377 case Sqr: func = "Square Root"; break;
378 case Cos: func = "Cosine"; break;
379 case Sin: func = "Sine"; break;
380 case Atan: func = "Arc Tangent"; break;
381 case Log: func = "Logarithm"; break;
382 case Exp: func = "Exponantial"; break;
383 case Store: func = "Store"; break;
384 case Recall: func = "Recall"; break;
385 case Disp: func = "Display"; break;
386 case Quit: func = "Quit"; break;
387 case Help: func = "Help"; break;
388 case Ans: func = "Ans"; break;
389 case Pi: func = "Pi"; break;
390 case E: func = "E"; break;
391 case Equal: func = "Equal"; break;
392 case Diff: func = "Different"; break;
393 case Ge: func = "Greater or equal"; break;
394 case Le: func = "Lesser or equal"; break;
395 case Gt: func = "Greater"; break;
396 case Lt: func = "Lesser"; break;
397 }
398
399 fprintf (stdout, "Function: %s\n", func);
400
401 if ((root->func == Val) && (root->ops[0] == NULL)) {
402 for (i = 0; i < level; i++) {
403 fprintf (stdout, " ");
404 }
405 fprintf (stdout, "value: %f\n", root->value);
406 } else {
407 for (i = 0; i < root->nbops; i++) {
408 print_element (root->ops[i], level + 1);
409 }
410 }
411 }
412
413 /* storage functions */
414
415 double store (int index, double value)
416 {
417 if ((index > 0) && (index <= STORAGE_SIZE)) {
418 storage[index - 1] = value;
419 } else {
420 VERBOSE (WARNING, fprintf (stdout, "invalid index (%d) [1, %d]\n", index, STORAGE_SIZE));
421 }
422 return value;
423 }
424
425 double recall (int index)
426 {
427 if ((index > 0) && (index <= STORAGE_SIZE)) {
428 return storage[index - 1];
429 } else {
430 VERBOSE (WARNING, fprintf (stdout, "invalid index (%d) [1, %d]\n", index, STORAGE_SIZE));
431 }
432 return 0;
433 }
434
435 void display (void)
436 {
437 int i;
438 fprintf (stdout, "storage:");
439 for (i = 0; i < STORAGE_SIZE; i++) {
440 fprintf (stdout, " %g", storage[i]);
441 }
442 fprintf (stdout, "\n");
443 }
444
445 /* quit function */
446
447 void quit (void)
448 {
449 fprintf (stdout, "bye\n");
450 exit (0);
451 }
452
453 /* help message */
454
455 void help (void)
456 {
457 fprintf (stdout, "calc is a simple calculator\n\n");
458 fprintf (stdout, "supported operators:\n");
459 fprintf (stdout, " + - * / %% ^\n\n");
460 fprintf (stdout, "camparison operators:\n");
461 fprintf (stdout, " == != >= <= > <\n\n");
462 fprintf (stdout, "supported functions:\n");
463 fprintf (stdout, " pow sqrt cos sin atan log exp\n\n");
464 fprintf (stdout, "storage functions:\n");
465 fprintf (stdout, " sto rcl\n\n");
466 fprintf (stdout, "miscellaneous functions:\n");
467 fprintf (stdout, " quit help\n\n");
468 fprintf (stdout, "supported constants:\n");
469 fprintf (stdout, " e pi\n");
470 }
471
472 /* evaluate element tree */
473
474 #define MASK_SUB 0x1
475 #define MASK_DIV 0x2
476
477 double evaluate_element (element_t *root, char mask)
478 {
479 double op0 = 0, op1 = 0;
480 char nextmask = mask;
481
482 if ((root == NULL) || (root == ERROR_OP)) {
483 VERBOSE (WARNING, fprintf (stdout, "error while evaluating\n"));
484 return 0;
485 }
486
487 /* mask to manage sub operator sub and div */
488 switch (root->func) {
489 case Add:
490 nextmask &= ~MASK_SUB;
491 nextmask &= ~MASK_DIV;
492 break;
493 case Sub:
494 nextmask |= MASK_SUB;
495 nextmask &= ~MASK_DIV;
496 break;
497 case Mul:
498 nextmask &= ~MASK_DIV;
499 break;
500 case Div:
501 nextmask |= MASK_DIV;
502 break;
503 default:
504 nextmask = mask;
505 }
506
507 switch (root->func) {
508 case Val:
509 case Sig:
510 op0 = (root->ops[0]) ? evaluate_element (root->ops[0], nextmask) : root->value;
511 break;
512 case Add:
513 case Sub:
514 case Mul:
515 case Div:
516 case Mod:
517 case Pow:
518 case Store:
519 case Equal:
520 case Diff:
521 case Ge:
522 case Le:
523 case Gt:
524 case Lt:
525 if (root->ops[1]) {
526 op1 = evaluate_element (root->ops[1], nextmask);
527 } else if (root->func != Store) {
528 VERBOSE (WARNING, fprintf (stdout, "error while evaluating (op[1])\n"));
529 return 0;
530 }
531 /* fallthrough */
532 case Sqr:
533 case Cos:
534 case Sin:
535 case Atan:
536 case Log:
537 case Exp:
538 case Recall:
539 if (root->ops[0]) {
540 op0 = evaluate_element (root->ops[0], 0);
541 } else {
542 VERBOSE (WARNING, fprintf (stdout, "error while evaluating (op[0])\n"));
543 return 0;
544 }
545 break;
546 case Disp:
547 case Quit:
548 case Help:
549 case Ans:
550 case Pi:
551 case E:
552 break;
553 }
554
555 switch (root->func) {
556 case Val: return op0;
557 case Sig: return -op0;
558 case Add: return ((mask & MASK_SUB) == 0) ? op0 + op1 : op0 - op1;
559 case Sub: return ((mask & MASK_SUB) == 0) ? op0 - op1 : op0 + op1;
560 case Mul: return ((mask & MASK_DIV) == 0) ? op0 * op1 : op0 / op1;
561 case Div: return ((mask & MASK_DIV) == 0) ? op0 / op1 : op0 * op1;
562 case Mod: return fmod (op0, op1);
563 case Pow: return pow (op0, op1);
564 case Sqr: return sqrt (op0);
565 case Cos: return cos (op0);
566 case Sin: return sin (op0);
567 case Atan: return atan (op0);
568 case Log: return log (op0);
569 case Exp: return exp (op0);
570 case Store: return store ((int)op0, (op1) ? op1 : answer);
571 case Recall: return recall ((int)op0);
572 case Disp: display (); break;
573 case Quit: quit (); break;
574 case Help: help (); break;
575 case Ans: return answer;
576 case Pi: return M_PI;
577 case E: return M_E;
578 case Equal: return op0 == op1;
579 case Diff: return op0 != op1;
580 case Ge: return op0 >= op1;
581 case Le: return op0 <= op1;
582 case Gt: return op0 > op1;
583 case Lt: return op0 < op1;
584 }
585
586 return 0;
587 }
588
589 /* vim: set ts=4 sw=4 et: */