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