f8a7367a8f5a1812a51257b3551c1e2c7624c4f9
[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 (next != NULL) {
310 *next = str;
311 }
312 return root;
313 }
314
315 /* look for operators */
316
317 for (i = 0; i < NB_OPERATORS; i++) {
318 keyword_t *operator = operators + i;
319 if (codecmp (operator->keyword, str) == 0) {
320 VERBOSE (DEBUG, fprintf (stdout, "start processing operator\n"));
321 if (root) {
322 if ((prio) && (prio > operator->prio)) {
323 VERBOSE (DEBUG, fprintf (stdout, "stop because operator priority\n"));
324 *next = str;
325 return root;
326 }
327 str += operator->offset;
328 VERBOSE (INFO, fprintf (stdout, "Oper: %d\n", operator->func));
329 if (subparser (&root, &str, operator->func, operator->nbops, operator->prio) == ERROR_OP) {
330 delelement (root);
331 return ERROR_OP;
332 }
333 } else if (*str == '-') {
334 new = newelement (Sig, 1, 9);
335 if (new == NULL) {
336 return ERROR_OP;
337 }
338 root = new;
339 } else {
340 return ERROR_OP;
341 }
342 found = 1;
343 VERBOSE (DEBUG, fprintf (stdout, "stop processing operator\n"));
344 break;
345 }
346 }
347 if (found) {
348 continue;
349 }
350
351 /* look for functions */
352
353 for (i = 0; i < NB_FUNCTIONS; i++) {
354 keyword_t *function = functions + i;
355 if (codecmp (function->keyword, str) == 0) {
356 VERBOSE (DEBUG, fprintf (stdout, "start processing function\n"));
357 if (root == NULL) {
358 VERBOSE (INFO, fprintf (stdout, "Func: %d\n", function->func));
359 new = newelement (function->func, function->nbops, function->prio);
360 if (new == NULL) {
361 return ERROR_OP;
362 }
363 root = new;
364 } else {
365 delelement (root);
366 return ERROR_OP;
367 }
368 str += function->offset;
369 found = 1;
370 VERBOSE (DEBUG, fprintf (stdout, "stop processing function\n"));
371 break;
372 }
373 }
374 if (found) {
375 continue;
376 }
377
378 /* look for constant */
379
380 for (i = 0; i < NB_CONSTANTS; i++) {
381 keyword_t *constant = constants + i;
382 if (codecmp (constant->keyword, str) == 0) {
383 VERBOSE (DEBUG, fprintf (stdout, "start processing constant\n"));
384 if (root == NULL) {
385 VERBOSE (INFO, fprintf (stdout, "Const: %d\n", constant->func));
386 new = newelement (constant->func, constant->nbops, constant->prio);
387 if (new == NULL) {
388 return ERROR_OP;
389 }
390 root = new;
391 } else {
392 delelement (root);
393 return ERROR_OP;
394 }
395 str += constant->offset;
396 found = 1;
397 VERBOSE (DEBUG, fprintf (stdout, "stop processing constant\n"));
398 break;
399 }
400 }
401 if (found) {
402 continue;
403 }
404
405 /* look for number */
406
407 if (((*str >= '0') && (*str <= '9')) ||
408 (*str == '.') || (*str == '+') || (*str == '-')) {
409 VERBOSE (DEBUG, fprintf (stdout, "start processing value\n"));
410 char *pt;
411 double value = strtod (str, &pt);
412 VERBOSE (INFO, fprintf (stdout, "Value: %f\n", value));
413 if (str != pt) {
414 if ((root == NULL) || (root->prio == 6)) {
415 new = newelement (Val, 1, 5);
416 if (new == NULL) {
417 return ERROR_OP;
418 }
419 new->value = value;
420 if (root == NULL) {
421 root = new;
422 } else {
423 for (i = 0; i < root->nbops; i++) {
424 if (root->ops[i] == NULL) {
425 root->ops[i] = new;
426 found = 1;
427 break;
428 }
429 }
430 if (!found) {
431 delelement (new);
432 delelement (root);
433 return ERROR_OP;
434 }
435 }
436 str = pt;
437 } else if ((*str == '+') || (*str == '-')) {
438 if ((prio) && (prio > 1)) {
439 VERBOSE (DEBUG, fprintf (stdout, "stop because operator priority\n"));
440 *next = str;
441 return root;
442 }
443 if (subparser (&root, &str, Add, 2, 1) == ERROR_OP) {
444 delelement (root);
445 return ERROR_OP;
446 }
447 } else {
448 delelement (root);
449 return ERROR_OP;
450 }
451 found = 1;
452 }
453 VERBOSE (DEBUG, fprintf (stdout, "stop processing value\n"));
454 }
455
456 /* error */
457
458 if (!found) {
459 delelement (root);
460 return ERROR_OP;
461 }
462
463 }
464
465 if (next != NULL) {
466 *next = str;
467 }
468
469 return root;
470 }
471
472 /* print element tree */
473
474 void print_element (element_t *root, int level)
475 {
476 char *func = NULL;
477 int i;
478
479 if ((root == NULL) || (root == ERROR_OP)) {
480 return;
481 }
482
483 for (i = 0; i < level; i++) {
484 fprintf (stdout, " ");
485 }
486
487 switch (root->func) {
488 case Val: func = "Value"; break;
489 case Sig: func = "Sign"; break;
490 case Add: func = "Addition"; break;
491 case Sub: func = "Subtraction"; break;
492 case Mul: func = "Multiplication"; break;
493 case Div: func = "Division"; break;
494 case Mod: func = "Modulo"; break;
495 case Pow: func = "Power"; break;
496 case Sqr: func = "Square Root"; break;
497 case Cos: func = "Cosine"; break;
498 case Sin: func = "Sine"; break;
499 case Atan: func = "Arc Tangent"; break;
500 case Log: func = "Logarithm"; break;
501 case Exp: func = "Exponantial"; break;
502 case Store: func = "Store"; break;
503 case Recall: func = "Recall"; break;
504 case Inc: func = "Increase"; break;
505 case Dec: func = "Decrease"; break;
506 case Disp: func = "Display"; break;
507 case Quit: func = "Quit"; break;
508 case Help: func = "Help"; break;
509 case Ans: func = "Ans"; break;
510 case Pi: func = "Pi"; break;
511 case E: func = "E"; break;
512 case Equal: func = "Equal"; break;
513 case Diff: func = "Different"; break;
514 case Ge: func = "Greater or equal"; break;
515 case Le: func = "Lesser or equal"; break;
516 case Gt: func = "Greater"; break;
517 case Lt: func = "Lesser"; break;
518 case And: func = "And"; break;
519 case Or: func = "Or"; break;
520 case Not: func = "Not"; break;
521 case Cond: func = "Condition"; break;
522 case While: func = "While"; break;
523 case Prog: func = "Program"; break;
524 }
525
526 fprintf (stdout, "Function: %s\n", func);
527
528 if ((root->func == Val) && (root->ops[0] == NULL)) {
529 for (i = 0; i < level; i++) {
530 fprintf (stdout, " ");
531 }
532 fprintf (stdout, "value: %f\n", root->value);
533 } else {
534 for (i = 0; i < root->nbops; i++) {
535 print_element (root->ops[i], level + 1);
536 }
537 }
538 }
539
540 /* storage functions */
541
542 double store (int index, double value)
543 {
544 if ((index > 0) && (index <= STORAGE_SIZE)) {
545 storage[index - 1] = value;
546 } else {
547 VERBOSE (WARNING, fprintf (stdout, "invalid index (%d) [1, %d]\n", index, STORAGE_SIZE));
548 }
549 return value;
550 }
551
552 double recall (int index)
553 {
554 if ((index > 0) && (index <= STORAGE_SIZE)) {
555 return storage[index - 1];
556 } else {
557 VERBOSE (WARNING, fprintf (stdout, "invalid index (%d) [1, %d]\n", index, STORAGE_SIZE));
558 }
559 return 0;
560 }
561
562 double increase (int index)
563 {
564 if ((index > 0) && (index <= STORAGE_SIZE)) {
565 return storage[index - 1]++;
566 } else {
567 VERBOSE (WARNING, fprintf (stdout, "invalid index (%d) [1, %d]\n", index, STORAGE_SIZE));
568 }
569 return 0;
570 }
571
572 double decrease (int index)
573 {
574 if ((index > 0) && (index <= STORAGE_SIZE)) {
575 return storage[index - 1]--;
576 } else {
577 VERBOSE (WARNING, fprintf (stdout, "invalid index (%d) [1, %d]\n", index, STORAGE_SIZE));
578 }
579 return 0;
580 }
581
582 void display (void)
583 {
584 int i;
585 fprintf (stdout, "storage:");
586 for (i = 0; i < STORAGE_SIZE; i++) {
587 fprintf (stdout, " %g", storage[i]);
588 }
589 fprintf (stdout, "\n");
590 }
591
592 /* While do function */
593
594 double while_do (element_t *cond, element_t *action)
595 {
596 double ret = 0;
597 element_t *temp = NULL;
598
599 VERBOSE (DEBUG, fprintf (stdout, "starting while loop\n"));
600 if (cond == NULL) {
601 return ret;
602 }
603 while (1) {
604 VERBOSE (DEBUG, fprintf (stdout, "loop...\n"));
605
606 temp = dupelement (cond);
607 if (!evaluate_element (temp, 0)) {
608 break;
609 }
610 if (action) {
611 temp = dupelement (action);
612 ret = evaluate_element (temp, 0);
613 }
614 }
615
616 VERBOSE (DEBUG, fprintf (stdout, "ending while loop\n"));
617
618 return ret;
619 }
620
621 /* program function */
622
623 double program_do (element_t **prog, int nbcalls)
624 {
625 double ret = 0;
626 int i;
627 for (i = 0; i < nbcalls; i++) {
628 ret = evaluate_element (prog[i], 0);
629 prog[i] = NULL;
630 }
631 return ret;
632 }
633
634 /* quit function */
635
636 void quit (void)
637 {
638 fprintf (stdout, "bye\n");
639 exit (0);
640 }
641
642 /* help message */
643
644 void help (void)
645 {
646 fprintf (stdout, "calc is a simple calculator\n\n");
647 fprintf (stdout, "supported operators:");
648 fprintf (stdout, " + - * / %% ^\n");
649 fprintf (stdout, "camparison operators:");
650 fprintf (stdout, " == != >= <= > <\n");
651 fprintf (stdout, "logical operators:");
652 fprintf (stdout, " & | !\n");
653 fprintf (stdout, "supported functions:");
654 fprintf (stdout, " pow sqrt cos sin atan log exp\n");
655 fprintf (stdout, "storage functions:");
656 fprintf (stdout, " sto rcl inc dec\n");
657 fprintf (stdout, "conditional functions:");
658 fprintf (stdout, " cond\n");
659 fprintf (stdout, "miscellaneous functions:");
660 fprintf (stdout, " quit help\n");
661 fprintf (stdout, "supported constants:");
662 fprintf (stdout, " e pi\n");
663 }
664
665 /* evaluate element tree */
666
667 #define MASK_SUB 0x1
668 #define MASK_DIV 0x2
669
670 double evaluate_element (element_t *root, char mask)
671 {
672 double op0 = 0, op1 = 0;
673 char nextmask = mask;
674
675 if ((root == NULL) || (root == ERROR_OP)) {
676 VERBOSE (WARNING, fprintf (stdout, "error while evaluating\n"));
677 return 0;
678 }
679
680 /* mask to manage sub operator sub and div */
681 switch (root->func) {
682 case Add:
683 nextmask &= ~MASK_SUB;
684 nextmask &= ~MASK_DIV;
685 break;
686 case Sub:
687 nextmask |= MASK_SUB;
688 nextmask &= ~MASK_DIV;
689 break;
690 case Mul:
691 nextmask &= ~MASK_DIV;
692 break;
693 case Div:
694 nextmask |= MASK_DIV;
695 break;
696 default:
697 nextmask = mask;
698 }
699
700 switch (root->func) {
701 case Val:
702 case Sig:
703 op0 = (root->ops[0]) ? evaluate_element (root->ops[0], nextmask) : root->value;
704 break;
705 case Add:
706 case Sub:
707 case Mul:
708 case Div:
709 case Mod:
710 case Pow:
711 case Store:
712 case Equal:
713 case Diff:
714 case Ge:
715 case Le:
716 case Gt:
717 case Lt:
718 case And:
719 case Or:
720 if (root->ops[1]) {
721 op1 = evaluate_element (root->ops[1], nextmask);
722 } else if (root->func != Store) {
723 VERBOSE (WARNING, fprintf (stdout, "error while evaluating (op[1])\n"));
724 return 0;
725 }
726 /* fallthrough */
727 case Sqr:
728 case Cos:
729 case Sin:
730 case Atan:
731 case Log:
732 case Exp:
733 case Recall:
734 case Inc:
735 case Dec:
736 case Not:
737 case Cond:
738 if (root->ops[0]) {
739 op0 = evaluate_element (root->ops[0], 0);
740 } else {
741 VERBOSE (WARNING, fprintf (stdout, "error while evaluating (op[0])\n"));
742 return 0;
743 }
744 break;
745 case Disp:
746 case Quit:
747 case Help:
748 case Ans:
749 case Pi:
750 case E:
751 case Prog:
752 break;
753 case While:
754 if (root->ops[0] == NULL) {
755 VERBOSE (WARNING, fprintf (stdout, "error while evaluating (op[0])\n"));
756 return 0;
757 }
758 break;
759 }
760
761 switch (root->func) {
762 case Val: return op0;
763 case Sig: return -op0;
764 case Add: return ((mask & MASK_SUB) == 0) ? op0 + op1 : op0 - op1;
765 case Sub: return ((mask & MASK_SUB) == 0) ? op0 - op1 : op0 + op1;
766 case Mul: return ((mask & MASK_DIV) == 0) ? op0 * op1 : op0 / op1;
767 case Div: return ((mask & MASK_DIV) == 0) ? op0 / op1 : op0 * op1;
768 case Mod: return fmod (op0, op1);
769 case Pow: return pow (op0, op1);
770 case Sqr: return sqrt (op0);
771 case Cos: return cos (op0);
772 case Sin: return sin (op0);
773 case Atan: return atan (op0);
774 case Log: return log (op0);
775 case Exp: return exp (op0);
776 case Store: return store ((int)op0, (op1) ? op1 : answer);
777 case Recall: return recall ((int)op0);
778 case Inc: return increase ((int)op0);
779 case Dec: return decrease ((int)op0);
780 case Disp: display (); break;
781 case Quit: quit (); break;
782 case Help: help (); break;
783 case Ans: return answer;
784 case Pi: return M_PI;
785 case E: return M_E;
786 case Equal: return op0 == op1;
787 case Diff: return op0 != op1;
788 case Ge: return op0 >= op1;
789 case Le: return op0 <= op1;
790 case Gt: return op0 > op1;
791 case Lt: return op0 < op1;
792 case And: return (op0 != 0) && (op1 != 0);
793 case Or: return (op0 != 0) || (op1 != 0);
794 case Not: return (op0 == 0);
795 case Cond:
796 if ((op0) && (root->ops[1])) {
797 return evaluate_element (root->ops[1], 0);
798 } else if ((!op0) && (root->ops[2])) {
799 return evaluate_element (root->ops[2], 0);
800 } else {
801 return 0;
802 }
803 case While: return while_do (root->ops[0], root->ops[1]);
804 case Prog: return program_do (root->ops, root->nbops);
805 }
806
807 return 0;
808 }
809
810 /* vim: set ts=4 sw=4 et: */