fix some error cases
[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 if (nbops) {
49 new->ops = (element_t **) calloc (nbops, sizeof (element_t *));
50 if (new->ops == NULL) {
51 free (new);
52 VERBOSE (ERROR, fprintf (stderr, "can't allocate memory\n"));
53 return NULL;
54 }
55 }
56 new->func = function;
57 new->nbops = nbops;
58 new->prio = prio;
59
60 return new;
61 }
62
63 /* desallocate element */
64
65 void delelement (element_t *root)
66 {
67 if ((root != NULL) && (root != ERROR_OP)) {
68 int i;
69 for (i = 0; i < root->nbops; i++) {
70 if ((root->ops[i] != NULL) && (root->ops[i] != ERROR_OP)) {
71 delelement (root->ops[i]);
72 }
73 }
74 if (root->nbops) {
75 free (root->ops);
76 }
77 free (root);
78 }
79 }
80
81 /* duplicate element */
82
83 element_t *dupelement (element_t *root)
84 {
85 element_t *tmp = NULL;
86 int i;
87
88 if ((root == NULL) || (root == ERROR_OP)) {
89 return root;
90 }
91 tmp = newelement (root->func, root->nbops, root->prio);
92 if (tmp == NULL) {
93 return ERROR_OP;
94 }
95 tmp->value = root->value;
96 for (i = 0; i < root->nbops; i++) {
97 tmp->ops[i] = dupelement (root->ops[i]);
98 if (tmp->ops[i] == ERROR_OP) {
99 delelement (tmp);
100 return ERROR_OP;
101 }
102 }
103 return tmp;
104 }
105
106 /* functions */
107
108 #define NB_OPERATORS 14
109 keyword_t operators[NB_OPERATORS] = {
110 { "+\t", Add, 2, 1, 1},
111 { "-\t", Sub, 2, 1, 1},
112 { "*", Mul, 2, 1, 2},
113 { "/", Div, 2, 1, 2},
114 { "%", Mod, 2, 1, 3},
115 { "^", Pow, 2, 1, 4},
116 { "==", Equal, 2, 2, -1},
117 { "!=", Diff, 2, 2, -1},
118 { ">=", Ge, 2, 2, -1},
119 { "<=", Le, 2, 2, -1},
120 { ">", Gt, 2, 1, -1},
121 { "<", Lt, 2, 1, -1},
122 { "&", And, 2, 1, -2},
123 { "|", Or, 2, 1, -2}
124 };
125
126 #define NB_FUNCTIONS 17
127 keyword_t functions[NB_FUNCTIONS] = {
128 { "sqrt", Sqr, 1, 4, 5},
129 { "pow", Pow, 2, 3, 5},
130 { "cos", Cos, 1, 3, 5},
131 { "sin", Sin, 1, 3, 5},
132 { "atan", Atan, 1, 4, 5},
133 { "exp", Exp, 1, 3, 5},
134 { "log", Log, 1, 3, 5},
135 { "sto", Store, 2, 3, 5},
136 { "rcl", Recall, 1, 3, 5},
137 { "inc", Inc, 1, 3, 5},
138 { "dec", Dec, 1, 3, 5},
139 { "disp", Disp, 0, 4, 9},
140 { "quit", Quit, 0, 4, 9},
141 { "help", Help, 0, 4, 9},
142 { "!", Not, 1, 1, 6},
143 { "cond", Cond, 3, 4, 5},
144 { "whl", While, 2, 3, 5}
145 };
146
147 #define NB_CONSTANTS 3
148 keyword_t constants[NB_CONSTANTS] = {
149 { "ans", Ans, 0, 3, 5},
150 { "e", E, 0, 1, 5},
151 { "pi", Pi, 0, 2, 5}
152 };
153
154 /* subparser function */
155
156 element_t *subparser (element_t **proot, char **pstr, func_t func, int nbops, int prio)
157 {
158 element_t *new = newelement (func, nbops, prio);
159 if (new == NULL) {
160 return ERROR_OP;
161 }
162 new->ops[0] = *proot;
163 new->ops[1] = parser (*pstr, pstr, new->prio);
164 if ((new->ops[1] == NULL) || ((new->ops[1] != ERROR_OP) && (new->ops[1]->prio == 9))) {
165 delelement (new->ops[1]);
166 new->ops[1] = ERROR_OP;
167 }
168 if (new->ops[1] == ERROR_OP) {
169 delelement (new);
170 *proot = NULL;
171 return ERROR_OP;
172 }
173 *proot = newelement (Val, 1, 5);
174 if (*proot == NULL) {
175 delelement (new);
176 return ERROR_OP;
177 }
178 (*proot)->ops[0] = new;
179
180 return *proot;
181 }
182
183 /* parser function */
184
185 element_t *parser (char *str, char **next, int prio)
186 {
187 element_t *root = NULL;
188 int i;
189
190 VERBOSE (DEBUG, fprintf (stdout, "Starting parsing\n"));
191
192 /* main loop */
193 while (*str != '\0') {
194 int found = 0;
195 element_t *new = NULL;
196 VERBOSE (INFO, fprintf (stdout, "Processing: %s\n", str));
197
198 /* skip spaces and tabs */
199
200 if ((*str == ' ') || (*str == '\t')) {
201 str++;
202 continue;
203 }
204
205 /* check for open brace */
206
207 if (*str == '{') {
208 VERBOSE (DEBUG, fprintf (stdout, "start processing brace\n"));
209 if (root != NULL) {
210 delelement (root);
211 return ERROR_OP;
212 }
213 element_t **prog = NULL;
214 new = newelement (Prog, 0, 5);
215 if (new == NULL) {
216 delelement (root);
217 return ERROR_OP;
218 }
219 root = new;
220 prog = &root;
221
222 do {
223 new = parser (str + 1, &str, 0);
224 if ((new == NULL) || ((new != ERROR_OP) && (new->prio == 9))) {
225 delelement (new);
226 new = ERROR_OP;
227 }
228 if (new == ERROR_OP) {
229 delelement (root);
230 return ERROR_OP;
231 }
232 element_t *newprog = newelement (Prog, (*prog)->nbops + 1, 5);
233 for (i = 0; i < (*prog)->nbops; i++) {
234 newprog->ops[i] = (*prog)->ops[i];
235 (*prog)->ops[i] = NULL;
236 }
237 newprog->ops[(*prog)->nbops] = new;
238 delelement (*prog);
239 (*prog) = newprog;
240 } while (*str == ',');
241
242 if (*str != '}') {
243 delelement (root);
244 return ERROR_OP;
245 }
246 str++;
247 VERBOSE (DEBUG, fprintf (stdout, "stop processing brace\n"));
248 continue;
249 }
250
251 /* check for open bracket */
252
253 if (*str == '(') {
254 VERBOSE (DEBUG, fprintf (stdout, "start processing bracket\n"));
255 if (root) {
256 do {
257 found = 0;
258 new = parser (str + 1, &str, 0);
259 if ((new == NULL) || ((new != ERROR_OP) && (new->prio == 9))) {
260 delelement (new);
261 new = ERROR_OP;
262 }
263 if ((new == NULL) || (new == ERROR_OP)) {
264 delelement (root);
265 return ERROR_OP;
266 }
267 for (i = 0; i < root->nbops; i++) {
268 if (root->ops[i] == NULL) {
269 root->ops[i] = new;
270 found = 1;
271 break;
272 }
273 }
274 if (!found) {
275 delelement (new);
276 delelement (root);
277 return ERROR_OP;
278 }
279 } while (*str == ',');
280 } else {
281 root = newelement (Val, 1, 5);
282 if (root == NULL) {
283 return ERROR_OP;
284 }
285 new = parser (str + 1, &str, 0);
286 if ((new == NULL) || ((new != ERROR_OP) && (new->prio == 9))) {
287 delelement (new);
288 new = ERROR_OP;
289 }
290 if ((new == NULL) || (new == ERROR_OP) || (*str == ',')) {
291 delelement (new);
292 delelement (root);
293 return ERROR_OP;
294 }
295 root->ops[0] = new;
296 }
297 if (*str != ')') {
298 delelement (root);
299 return ERROR_OP;
300 }
301 str++;
302 VERBOSE (DEBUG, fprintf (stdout, "stop processing bracket\n"));
303 continue;
304 }
305
306 /* check for closing bracket, closing brace or koma */
307
308 if ((*str == ')') || (*str == '}') || (*str == ',')) {
309 if (prio == -9) {
310 delelement (root);
311 return ERROR_OP;
312 }
313 if (next != NULL) {
314 *next = str;
315 }
316 return root;
317 }
318
319 /* look for operators */
320
321 for (i = 0; i < NB_OPERATORS; i++) {
322 keyword_t *operator = operators + i;
323 if (codecmp (operator->keyword, str) == 0) {
324 VERBOSE (DEBUG, fprintf (stdout, "start processing operator\n"));
325 if (root) {
326 if ((prio) && (prio > operator->prio)) {
327 VERBOSE (DEBUG, fprintf (stdout, "stop because operator priority\n"));
328 *next = str;
329 return root;
330 }
331 str += operator->offset;
332 VERBOSE (INFO, fprintf (stdout, "Oper: %d\n", operator->func));
333 if (subparser (&root, &str, operator->func, operator->nbops, operator->prio) == ERROR_OP) {
334 delelement (root);
335 return ERROR_OP;
336 }
337 } else if (*str == '-') {
338 new = newelement (Sig, 1, 9);
339 if (new == NULL) {
340 return ERROR_OP;
341 }
342 root = new;
343 } else {
344 return ERROR_OP;
345 }
346 found = 1;
347 VERBOSE (DEBUG, fprintf (stdout, "stop processing operator\n"));
348 break;
349 }
350 }
351 if (found) {
352 continue;
353 }
354
355 /* look for functions */
356
357 for (i = 0; i < NB_FUNCTIONS; i++) {
358 keyword_t *function = functions + i;
359 if (codecmp (function->keyword, str) == 0) {
360 VERBOSE (DEBUG, fprintf (stdout, "start processing function\n"));
361 if (root == NULL) {
362 VERBOSE (INFO, fprintf (stdout, "Func: %d\n", function->func));
363 new = newelement (function->func, function->nbops, function->prio);
364 if (new == NULL) {
365 return ERROR_OP;
366 }
367 root = new;
368 } else {
369 delelement (root);
370 return ERROR_OP;
371 }
372 str += function->offset;
373 found = 1;
374 VERBOSE (DEBUG, fprintf (stdout, "stop processing function\n"));
375 break;
376 }
377 }
378 if (found) {
379 continue;
380 }
381
382 /* look for constant */
383
384 for (i = 0; i < NB_CONSTANTS; i++) {
385 keyword_t *constant = constants + i;
386 if (codecmp (constant->keyword, str) == 0) {
387 VERBOSE (DEBUG, fprintf (stdout, "start processing constant\n"));
388 if (root == NULL) {
389 VERBOSE (INFO, fprintf (stdout, "Const: %d\n", constant->func));
390 new = newelement (constant->func, constant->nbops, constant->prio);
391 if (new == NULL) {
392 return ERROR_OP;
393 }
394 root = new;
395 } else {
396 delelement (root);
397 return ERROR_OP;
398 }
399 str += constant->offset;
400 found = 1;
401 VERBOSE (DEBUG, fprintf (stdout, "stop processing constant\n"));
402 break;
403 }
404 }
405 if (found) {
406 continue;
407 }
408
409 /* look for number */
410
411 if (((*str >= '0') && (*str <= '9')) ||
412 (*str == '.') || (*str == '+') || (*str == '-')) {
413 VERBOSE (DEBUG, fprintf (stdout, "start processing value\n"));
414 char *pt;
415 double value = strtod (str, &pt);
416 VERBOSE (INFO, fprintf (stdout, "Value: %f\n", value));
417 if (str != pt) {
418 if ((root == NULL) || (root->prio == 6)) {
419 new = newelement (Val, 1, 5);
420 if (new == NULL) {
421 return ERROR_OP;
422 }
423 new->value = value;
424 if (root == NULL) {
425 root = new;
426 } else {
427 for (i = 0; i < root->nbops; i++) {
428 if (root->ops[i] == NULL) {
429 root->ops[i] = new;
430 found = 1;
431 break;
432 }
433 }
434 if (!found) {
435 delelement (new);
436 delelement (root);
437 return ERROR_OP;
438 }
439 }
440 str = pt;
441 } else if ((*str == '+') || (*str == '-')) {
442 if ((prio) && (prio > 1)) {
443 VERBOSE (DEBUG, fprintf (stdout, "stop because operator priority\n"));
444 *next = str;
445 return root;
446 }
447 if (subparser (&root, &str, Add, 2, 1) == ERROR_OP) {
448 delelement (root);
449 return ERROR_OP;
450 }
451 } else {
452 delelement (root);
453 return ERROR_OP;
454 }
455 found = 1;
456 }
457 VERBOSE (DEBUG, fprintf (stdout, "stop processing value\n"));
458 }
459
460 /* error */
461
462 if (!found) {
463 delelement (root);
464 return ERROR_OP;
465 }
466
467 }
468
469 if (next != NULL) {
470 *next = str;
471 }
472
473 return root;
474 }
475
476 /* print element tree */
477
478 void print_element (element_t *root, int level)
479 {
480 char *func = NULL;
481 int i;
482
483 if ((root == NULL) || (root == ERROR_OP)) {
484 return;
485 }
486
487 for (i = 0; i < level; i++) {
488 fprintf (stdout, " ");
489 }
490
491 switch (root->func) {
492 case Val: func = "Value"; break;
493 case Sig: func = "Sign"; break;
494 case Add: func = "Addition"; break;
495 case Sub: func = "Subtraction"; break;
496 case Mul: func = "Multiplication"; break;
497 case Div: func = "Division"; break;
498 case Mod: func = "Modulo"; break;
499 case Pow: func = "Power"; break;
500 case Sqr: func = "Square Root"; break;
501 case Cos: func = "Cosine"; break;
502 case Sin: func = "Sine"; break;
503 case Atan: func = "Arc Tangent"; break;
504 case Log: func = "Logarithm"; break;
505 case Exp: func = "Exponantial"; break;
506 case Store: func = "Store"; break;
507 case Recall: func = "Recall"; break;
508 case Inc: func = "Increase"; break;
509 case Dec: func = "Decrease"; break;
510 case Disp: func = "Display"; break;
511 case Quit: func = "Quit"; break;
512 case Help: func = "Help"; break;
513 case Ans: func = "Ans"; break;
514 case Pi: func = "Pi"; break;
515 case E: func = "E"; break;
516 case Equal: func = "Equal"; break;
517 case Diff: func = "Different"; break;
518 case Ge: func = "Greater or equal"; break;
519 case Le: func = "Lesser or equal"; break;
520 case Gt: func = "Greater"; break;
521 case Lt: func = "Lesser"; break;
522 case And: func = "And"; break;
523 case Or: func = "Or"; break;
524 case Not: func = "Not"; break;
525 case Cond: func = "Condition"; break;
526 case While: func = "While"; break;
527 case Prog: func = "Program"; break;
528 }
529
530 fprintf (stdout, "Function: %s\n", func);
531
532 if ((root->func == Val) && (root->ops[0] == NULL)) {
533 for (i = 0; i < level; i++) {
534 fprintf (stdout, " ");
535 }
536 fprintf (stdout, "value: %f\n", root->value);
537 } else {
538 for (i = 0; i < root->nbops; i++) {
539 print_element (root->ops[i], level + 1);
540 }
541 }
542 }
543
544 /* storage functions */
545
546 double store (int index, double value)
547 {
548 if ((index > 0) && (index <= STORAGE_SIZE)) {
549 storage[index - 1] = value;
550 } else {
551 VERBOSE (WARNING, fprintf (stdout, "invalid index (%d) [1, %d]\n", index, STORAGE_SIZE));
552 }
553 return value;
554 }
555
556 double recall (int index)
557 {
558 if ((index > 0) && (index <= STORAGE_SIZE)) {
559 return storage[index - 1];
560 } else {
561 VERBOSE (WARNING, fprintf (stdout, "invalid index (%d) [1, %d]\n", index, STORAGE_SIZE));
562 }
563 return 0;
564 }
565
566 double increase (int index)
567 {
568 if ((index > 0) && (index <= STORAGE_SIZE)) {
569 return storage[index - 1]++;
570 } else {
571 VERBOSE (WARNING, fprintf (stdout, "invalid index (%d) [1, %d]\n", index, STORAGE_SIZE));
572 }
573 return 0;
574 }
575
576 double decrease (int index)
577 {
578 if ((index > 0) && (index <= STORAGE_SIZE)) {
579 return storage[index - 1]--;
580 } else {
581 VERBOSE (WARNING, fprintf (stdout, "invalid index (%d) [1, %d]\n", index, STORAGE_SIZE));
582 }
583 return 0;
584 }
585
586 void display (void)
587 {
588 int i;
589 fprintf (stdout, "storage:");
590 for (i = 0; i < STORAGE_SIZE; i++) {
591 fprintf (stdout, " %g", storage[i]);
592 }
593 fprintf (stdout, "\n");
594 }
595
596 /* While do function */
597
598 double while_do (element_t *cond, element_t *action)
599 {
600 double ret = 0;
601 element_t *temp = NULL;
602
603 VERBOSE (DEBUG, fprintf (stdout, "starting while loop\n"));
604 if (cond == NULL) {
605 return ret;
606 }
607 while (1) {
608 VERBOSE (DEBUG, fprintf (stdout, "loop...\n"));
609
610 temp = dupelement (cond);
611 if (!evaluate_element (temp, 0)) {
612 break;
613 }
614 if (action) {
615 temp = dupelement (action);
616 ret = evaluate_element (temp, 0);
617 }
618 }
619
620 VERBOSE (DEBUG, fprintf (stdout, "ending while loop\n"));
621
622 return ret;
623 }
624
625 /* program function */
626
627 double program_do (element_t **prog, int nbcalls)
628 {
629 double ret = 0;
630 int i;
631 for (i = 0; i < nbcalls; i++) {
632 ret = evaluate_element (prog[i], 0);
633 prog[i] = NULL;
634 }
635 return ret;
636 }
637
638 /* quit function */
639
640 void quit (void)
641 {
642 fprintf (stdout, "bye\n");
643 exit (0);
644 }
645
646 /* help message */
647
648 void help (void)
649 {
650 fprintf (stdout, "calc is a simple calculator\n\n");
651 fprintf (stdout, "supported operators:");
652 fprintf (stdout, " + - * / %% ^\n");
653 fprintf (stdout, "camparison operators:");
654 fprintf (stdout, " == != >= <= > <\n");
655 fprintf (stdout, "logical operators:");
656 fprintf (stdout, " & | !\n");
657 fprintf (stdout, "supported functions:");
658 fprintf (stdout, " pow sqrt cos sin atan log exp\n");
659 fprintf (stdout, "storage functions:");
660 fprintf (stdout, " sto rcl inc dec\n");
661 fprintf (stdout, "conditional functions:");
662 fprintf (stdout, " cond\n");
663 fprintf (stdout, "miscellaneous functions:");
664 fprintf (stdout, " quit help\n");
665 fprintf (stdout, "supported constants:");
666 fprintf (stdout, " e pi\n");
667 }
668
669 /* evaluate element tree */
670
671 #define MASK_SUB 0x1
672 #define MASK_DIV 0x2
673
674 double evaluate_element (element_t *root, char mask)
675 {
676 double op0 = 0, op1 = 0;
677 char nextmask = mask;
678
679 if ((root == NULL) || (root == ERROR_OP)) {
680 VERBOSE (WARNING, fprintf (stdout, "error while evaluating\n"));
681 return 0;
682 }
683
684 /* mask to manage sub operator sub and div */
685 switch (root->func) {
686 case Add:
687 nextmask &= ~MASK_SUB;
688 nextmask &= ~MASK_DIV;
689 break;
690 case Sub:
691 nextmask |= MASK_SUB;
692 nextmask &= ~MASK_DIV;
693 break;
694 case Mul:
695 nextmask &= ~MASK_DIV;
696 break;
697 case Div:
698 nextmask |= MASK_DIV;
699 break;
700 default:
701 nextmask = mask;
702 }
703
704 switch (root->func) {
705 case Val:
706 case Sig:
707 op0 = (root->ops[0]) ? evaluate_element (root->ops[0], nextmask) : root->value;
708 break;
709 case Add:
710 case Sub:
711 case Mul:
712 case Div:
713 case Mod:
714 case Pow:
715 case Store:
716 case Equal:
717 case Diff:
718 case Ge:
719 case Le:
720 case Gt:
721 case Lt:
722 case And:
723 case Or:
724 if (root->ops[1]) {
725 op1 = evaluate_element (root->ops[1], nextmask);
726 } else if (root->func != Store) {
727 VERBOSE (WARNING, fprintf (stdout, "error while evaluating (op[1])\n"));
728 return 0;
729 }
730 /* fallthrough */
731 case Sqr:
732 case Cos:
733 case Sin:
734 case Atan:
735 case Log:
736 case Exp:
737 case Recall:
738 case Inc:
739 case Dec:
740 case Not:
741 case Cond:
742 if (root->ops[0]) {
743 op0 = evaluate_element (root->ops[0], 0);
744 } else {
745 VERBOSE (WARNING, fprintf (stdout, "error while evaluating (op[0])\n"));
746 return 0;
747 }
748 break;
749 case Disp:
750 case Quit:
751 case Help:
752 case Ans:
753 case Pi:
754 case E:
755 case Prog:
756 break;
757 case While:
758 if (root->ops[0] == NULL) {
759 VERBOSE (WARNING, fprintf (stdout, "error while evaluating (op[0])\n"));
760 return 0;
761 }
762 break;
763 }
764
765 switch (root->func) {
766 case Val: return op0;
767 case Sig: return -op0;
768 case Add: return ((mask & MASK_SUB) == 0) ? op0 + op1 : op0 - op1;
769 case Sub: return ((mask & MASK_SUB) == 0) ? op0 - op1 : op0 + op1;
770 case Mul: return ((mask & MASK_DIV) == 0) ? op0 * op1 : op0 / op1;
771 case Div: return ((mask & MASK_DIV) == 0) ? op0 / op1 : op0 * op1;
772 case Mod: return fmod (op0, op1);
773 case Pow: return pow (op0, op1);
774 case Sqr: return sqrt (op0);
775 case Cos: return cos (op0);
776 case Sin: return sin (op0);
777 case Atan: return atan (op0);
778 case Log: return log (op0);
779 case Exp: return exp (op0);
780 case Store: return store ((int)op0, (op1) ? op1 : answer);
781 case Recall: return recall ((int)op0);
782 case Inc: return increase ((int)op0);
783 case Dec: return decrease ((int)op0);
784 case Disp: display (); break;
785 case Quit: quit (); break;
786 case Help: help (); break;
787 case Ans: return answer;
788 case Pi: return M_PI;
789 case E: return M_E;
790 case Equal: return op0 == op1;
791 case Diff: return op0 != op1;
792 case Ge: return op0 >= op1;
793 case Le: return op0 <= op1;
794 case Gt: return op0 > op1;
795 case Lt: return op0 < op1;
796 case And: return (op0 != 0) && (op1 != 0);
797 case Or: return (op0 != 0) || (op1 != 0);
798 case Not: return (op0 == 0);
799 case Cond:
800 if ((op0) && (root->ops[1])) {
801 return evaluate_element (root->ops[1], 0);
802 } else if ((!op0) && (root->ops[2])) {
803 return evaluate_element (root->ops[2], 0);
804 } else {
805 return 0;
806 }
807 case While: return while_do (root->ops[0], root->ops[1]);
808 case Prog: return program_do (root->ops, root->nbops);
809 }
810
811 return 0;
812 }
813
814 /* vim: set ts=4 sw=4 et: */