4b4fab7678dd65352e28a2922fe2e94a56ba4810
[calc.git] / parser.c
1 #include <malloc.h>
2 #include <math.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6
7 #include "debug.h"
8
9 #include "parser.h"
10
11 /* global variables */
12
13 double answer = 0;
14
15 #define DEFAULT_STORAGE_SIZE 10
16 int storage_size = -1;
17
18 double *storage = NULL;
19
20 #define DEFAULT_FORMAT "=> %.6g\n"
21 char *format = NULL;
22 char *minform = NULL;
23
24 workspace_t *programs = NULL;
25 int nb_programs = 0;
26
27 /* compare codes */
28
29 int codecmp (char *ref, char *str)
30 {
31 int sig;
32
33 while (*ref != '\0') {
34 if (*ref == '\t') {
35 sig = (*str == '.') ? -1 : ((*str >= '0') && (*str <= '9'));
36 } else {
37 sig = *str - *ref;
38 }
39 if (sig != 0) {
40 return (sig > 0) ? 1 : -1;
41 }
42 str++;
43 ref++;
44 }
45
46 return 0;
47 }
48
49 /* allocate new element */
50
51 element_t *newelement (func_t function, int nbops, int prio)
52 {
53 element_t *new = (element_t *) calloc (1, sizeof (element_t));
54 if (new == NULL) {
55 VERBOSE (ERROR, fprintf (stderr, "can't allocate memory\n"));
56 exit (1);
57 }
58 if (nbops) {
59 new->ops = (element_t **) calloc (nbops, sizeof (element_t *));
60 if (new->ops == NULL) {
61 VERBOSE (ERROR, fprintf (stderr, "can't allocate memory\n"));
62 exit (1);
63 }
64 }
65 new->func = function;
66 new->nbops = nbops;
67 new->prio = prio;
68
69 return new;
70 }
71
72 /* desallocate element */
73
74 void delelement (element_t *root)
75 {
76 if ((root != NULL) && (root != ERROR_OP)) {
77 int i;
78 for (i = 0; i < root->nbops; i++) {
79 if ((root->ops[i] != NULL) && (root->ops[i] != ERROR_OP)) {
80 delelement (root->ops[i]);
81 }
82 }
83 if (root->nbops) {
84 free (root->ops);
85 }
86 free (root);
87 }
88 }
89
90 /* duplicate element */
91
92 element_t *dupelement (element_t *root)
93 {
94 element_t *tmp = NULL;
95 int i;
96
97 if ((root == NULL) || (root == ERROR_OP)) {
98 return root;
99 }
100 tmp = newelement (root->func, root->nbops, root->prio);
101 tmp->value = root->value;
102 for (i = 0; i < root->nbops; i++) {
103 tmp->ops[i] = dupelement (root->ops[i]);
104 }
105 return tmp;
106 }
107
108 /* functions */
109
110 #define NB_OPERATORS 14
111 keyword_t operators[NB_OPERATORS] = {
112 { "+\t", Add, 2, 1, 1},
113 { "-\t", Sub, 2, 1, 1},
114 { "*", Mul, 2, 1, 2},
115 { "/", Div, 2, 1, 2},
116 { "%", Mod, 2, 1, 3},
117 { "^", Pow, 2, 1, 4},
118 { "==", Equal, 2, 2, -1},
119 { "!=", Diff, 2, 2, -1},
120 { ">=", Ge, 2, 2, -1},
121 { "<=", Le, 2, 2, -1},
122 { ">", Gt, 2, 1, -1},
123 { "<", Lt, 2, 1, -1},
124 { "&", And, 2, 1, -2},
125 { "|", Or, 2, 1, -2}
126 };
127
128 #define NB_FUNCTIONS 31
129 keyword_t functions[NB_FUNCTIONS] = {
130 { "sqrt", Sqr, 1, 4, 5},
131 { "pow", Pow, 2, 3, 5},
132 { "cos", Cos, 1, 3, 5},
133 { "sin", Sin, 1, 3, 5},
134 { "tan", Tan, 1, 3, 5},
135 { "acos", Acos, 1, 4, 5},
136 { "asin", Asin, 1, 4, 5},
137 { "atan", Atan, 1, 4, 5},
138 { "log", Log, 1, 3, 5},
139 { "exp", Exp, 1, 3, 5},
140 { "abs", Abs, 1, 3, 5},
141 { "floor", Floor, 1, 5, 5},
142 { "ceil", Ceil, 1, 4, 5},
143 { "sto", Store, 2, 3, 5},
144 { "rcl", Recall, 1, 3, 5},
145 { "inc", Inc, 1, 3, 5},
146 { "dec", Dec, 1, 3, 5},
147 { "disp", Disp, 0, 4, 9},
148 { "mem", Mem, 1, 3, 9},
149 { "clr", Clear, 0, 3, 9},
150 { "quit", Quit, 0, 4, 9},
151 { "help", Help, 0, 4, 9},
152 { "!", Not, 1, 1, 6},
153 { "cond", Cond, 3, 4, 5},
154 { "while", While, 2, 5, 5},
155 { "print", Print, 1, 5, 5},
156 { "prog", Prog, 3, 4, 9},
157 { "call", Call, 10, 4, 5},
158 { "ls", List, 0, 2, 9},
159 { "edit", Edit, 1, 4, 9},
160 { "del", Del, 1, 3, 9}
161 };
162
163 #define NB_CONSTANTS 3
164 keyword_t constants[NB_CONSTANTS] = {
165 { "ans", Ans, 0, 3, 5},
166 { "e", E, 0, 1, 5},
167 { "pi", Pi, 0, 2, 5}
168 };
169
170 #define NB_SYMBOLS 4
171 char *symbols[NB_SYMBOLS] = {
172 "(", ")", "{", "}"
173 };
174
175 /* subparser function */
176
177 element_t *subparser (element_t **proot, char **pstr, func_t func, int nbops, int prio)
178 {
179 element_t *new = newelement (func, nbops, prio);
180 new->ops[0] = *proot;
181 new->ops[1] = parser (*pstr, pstr, new->prio);
182 if ((new->ops[1] == NULL) || ((new->ops[1] != ERROR_OP) && (new->ops[1]->prio == 9))) {
183 delelement (new->ops[1]);
184 new->ops[1] = ERROR_OP;
185 }
186 if (new->ops[1] == ERROR_OP) {
187 delelement (new);
188 *proot = NULL;
189 return ERROR_OP;
190 }
191 *proot = newelement (Val, 1, 5);
192 (*proot)->ops[0] = new;
193
194 return *proot;
195 }
196
197 /* parser function */
198
199 element_t *parser (char *str, char **next, int prio)
200 {
201 element_t *root = NULL;
202 int i;
203
204 VERBOSE (DEBUG, fprintf (stdout, "Starting parsing\n"));
205
206 /* main loop */
207 while (*str != '\0') {
208 int found = 0;
209 element_t *new = NULL;
210 VERBOSE (INFO, fprintf (stdout, "Processing: %s\n", str));
211
212 /* end without printing */
213
214 if (*str == ';') {
215 if (root) {
216 root->hidden = 1;
217 }
218 break;
219 }
220
221 /* skip spaces and tabs */
222
223 if ((*str == ' ') || (*str == '\t')) {
224 str++;
225 continue;
226 }
227
228 /* check for open brace */
229
230 if (*str == '{') {
231 VERBOSE (DEBUG, fprintf (stdout, "start processing brace\n"));
232 if (root != NULL) {
233 delelement (root);
234 return ERROR_OP;
235 }
236 element_t **prog = NULL;
237 new = newelement (Code, 0, 5);
238 root = new;
239 prog = &root;
240
241 do {
242 new = parser (str + 1, &str, 0);
243 if ((new == NULL) || ((new != ERROR_OP) && (new->prio == 9))) {
244 delelement (new);
245 new = ERROR_OP;
246 }
247 if (new == ERROR_OP) {
248 delelement (root);
249 return ERROR_OP;
250 }
251 element_t *newprog = newelement (Code, (*prog)->nbops + 1, 5);
252 for (i = 0; i < (*prog)->nbops; i++) {
253 newprog->ops[i] = (*prog)->ops[i];
254 (*prog)->ops[i] = NULL;
255 }
256 newprog->ops[(*prog)->nbops] = new;
257 delelement (*prog);
258 (*prog) = newprog;
259 } while (*str == ',');
260
261 if (*str != '}') {
262 delelement (root);
263 return ERROR_OP;
264 }
265 str++;
266 VERBOSE (DEBUG, fprintf (stdout, "stop processing brace\n"));
267 continue;
268 }
269
270 /* check for open bracket */
271
272 if (*str == '(') {
273 VERBOSE (DEBUG, fprintf (stdout, "start processing bracket\n"));
274 if (root) {
275 do {
276 found = 0;
277 new = parser (str + 1, &str, 0);
278 if ((new == NULL) || ((new != ERROR_OP) && (new->prio == 9))) {
279 delelement (new);
280 new = ERROR_OP;
281 }
282 if ((new == NULL) || (new == ERROR_OP)) {
283 delelement (root);
284 return ERROR_OP;
285 }
286 for (i = 0; i < root->nbops; i++) {
287 if (root->ops[i] == NULL) {
288 root->ops[i] = new;
289 found = 1;
290 break;
291 }
292 }
293 if (!found) {
294 delelement (new);
295 delelement (root);
296 return ERROR_OP;
297 }
298 } while (*str == ',');
299 } else {
300 root = newelement (Val, 1, 5);
301 new = parser (str + 1, &str, 0);
302 if ((new == NULL) || ((new != ERROR_OP) && (new->prio == 9))) {
303 delelement (new);
304 new = ERROR_OP;
305 }
306 if ((new == NULL) || (new == ERROR_OP) || (*str == ',')) {
307 delelement (new);
308 delelement (root);
309 return ERROR_OP;
310 }
311 root->ops[0] = new;
312 }
313 if (*str != ')') {
314 delelement (root);
315 return ERROR_OP;
316 }
317 str++;
318 VERBOSE (DEBUG, fprintf (stdout, "stop processing bracket\n"));
319 continue;
320 }
321
322 /* check for closing bracket, closing brace or koma */
323
324 if ((*str == ')') || (*str == '}') || (*str == ',')) {
325 if (prio == -9) {
326 delelement (root);
327 return ERROR_OP;
328 }
329 if (next != NULL) {
330 *next = str;
331 }
332 return root;
333 }
334
335 /* look for operators */
336
337 for (i = 0; i < NB_OPERATORS; i++) {
338 keyword_t *operator = operators + i;
339 if (codecmp (operator->keyword, str) == 0) {
340 VERBOSE (DEBUG, fprintf (stdout, "start processing operator\n"));
341 if (root) {
342 if ((prio) && (prio > operator->prio)) {
343 VERBOSE (DEBUG, fprintf (stdout, "stop because operator priority\n"));
344 *next = str;
345 return root;
346 }
347 str += operator->offset;
348 VERBOSE (INFO, fprintf (stdout, "Oper: %d\n", operator->func));
349 if (subparser (&root, &str, operator->func, operator->nbops, operator->prio) == ERROR_OP) {
350 delelement (root);
351 return ERROR_OP;
352 }
353 } else if (*str == '-') {
354 root = newelement (Sig, 1, 9);
355 } else {
356 return ERROR_OP;
357 }
358 found = 1;
359 VERBOSE (DEBUG, fprintf (stdout, "stop processing operator\n"));
360 break;
361 }
362 }
363 if (found) {
364 continue;
365 }
366
367 /* look for functions */
368
369 for (i = 0; i < NB_FUNCTIONS; i++) {
370 keyword_t *function = functions + i;
371 if (codecmp (function->keyword, str) == 0) {
372 VERBOSE (DEBUG, fprintf (stdout, "start processing function\n"));
373 if (root == NULL) {
374 VERBOSE (INFO, fprintf (stdout, "Func: %d\n", function->func));
375 root = newelement (function->func, function->nbops, function->prio);
376 } else {
377 delelement (root);
378 return ERROR_OP;
379 }
380 str += function->offset;
381 found = 1;
382 VERBOSE (DEBUG, fprintf (stdout, "stop processing function\n"));
383 break;
384 }
385 }
386 if (found) {
387 continue;
388 }
389
390 /* look for constant */
391
392 for (i = 0; i < NB_CONSTANTS; i++) {
393 keyword_t *constant = constants + i;
394 if (codecmp (constant->keyword, str) == 0) {
395 VERBOSE (DEBUG, fprintf (stdout, "start processing constant\n"));
396 if (root == NULL) {
397 VERBOSE (INFO, fprintf (stdout, "Const: %d\n", constant->func));
398 root = newelement (constant->func, constant->nbops, constant->prio);
399 } else {
400 delelement (root);
401 return ERROR_OP;
402 }
403 str += constant->offset;
404 found = 1;
405 VERBOSE (DEBUG, fprintf (stdout, "stop processing constant\n"));
406 break;
407 }
408 }
409 if (found) {
410 continue;
411 }
412
413 /* look for number */
414
415 if (((*str >= '0') && (*str <= '9')) ||
416 (*str == '.') || (*str == '+') || (*str == '-')) {
417 VERBOSE (DEBUG, fprintf (stdout, "start processing value\n"));
418 char *pt;
419 double value = strtod (str, &pt);
420 VERBOSE (INFO, fprintf (stdout, "Value: %f\n", value));
421 if (str != pt) {
422 if ((root == NULL) || (root->prio == 6)) {
423 new = newelement (Val, 1, 5);
424 new->value = value;
425 if (root == NULL) {
426 root = new;
427 } else {
428 for (i = 0; i < root->nbops; i++) {
429 if (root->ops[i] == NULL) {
430 root->ops[i] = new;
431 found = 1;
432 break;
433 }
434 }
435 if (!found) {
436 delelement (new);
437 delelement (root);
438 return ERROR_OP;
439 }
440 }
441 str = pt;
442 } else if ((*str == '+') || (*str == '-')) {
443 if ((prio) && (prio > 1)) {
444 VERBOSE (DEBUG, fprintf (stdout, "stop because operator priority\n"));
445 *next = str;
446 return root;
447 }
448 if (subparser (&root, &str, Add, 2, 1) == ERROR_OP) {
449 delelement (root);
450 return ERROR_OP;
451 }
452 } else {
453 delelement (root);
454 return ERROR_OP;
455 }
456 found = 1;
457 }
458 VERBOSE (DEBUG, fprintf (stdout, "stop processing value\n"));
459 }
460
461 /* error */
462
463 if (!found) {
464 delelement (root);
465 return ERROR_OP;
466 }
467
468 }
469
470 if (next != NULL) {
471 *next = str;
472 }
473
474 return root;
475 }
476
477 /* print element tree */
478
479 void print_element (element_t *root, int level)
480 {
481 char *func = NULL;
482 int i;
483
484 if ((root == NULL) || (root == ERROR_OP)) {
485 return;
486 }
487
488 for (i = 0; i < level; i++) {
489 fprintf (stdout, " ");
490 }
491
492 switch (root->func) {
493 case Val: func = "Value"; break;
494 case Sig: func = "Sign"; break;
495 case Add: func = "Addition"; break;
496 case Sub: func = "Subtraction"; break;
497 case Mul: func = "Multiplication"; break;
498 case Div: func = "Division"; break;
499 case Mod: func = "Modulo"; break;
500 case Pow: func = "Power"; break;
501 case Sqr: func = "Square Root"; break;
502 case Cos: func = "Cosine"; break;
503 case Sin: func = "Sine"; break;
504 case Tan: func = "Tangent"; break;
505 case Acos: func = "Arc Cosine"; break;
506 case Asin: func = "Arc Sine"; break;
507 case Atan: func = "Arc Tangent"; break;
508 case Log: func = "Logarithm"; break;
509 case Exp: func = "Exponantial"; break;
510 case Abs: func = "Absolute value"; break;
511 case Ceil: func = "Ceil value"; break;
512 case Floor: func = "Floor value"; break;
513 case Store: func = "Store"; break;
514 case Recall: func = "Recall"; break;
515 case Inc: func = "Increase"; break;
516 case Dec: func = "Decrease"; break;
517 case Disp: func = "Display"; break;
518 case Mem: func = "Memory"; break;
519 case Clear: func = "Clear"; break;
520 case Quit: func = "Quit"; break;
521 case Help: func = "Help"; break;
522 case Ans: func = "Ans"; break;
523 case Pi: func = "Pi"; break;
524 case E: func = "E"; break;
525 case Equal: func = "Equal"; break;
526 case Diff: func = "Different"; break;
527 case Ge: func = "Greater or equal"; break;
528 case Le: func = "Lesser or equal"; break;
529 case Gt: func = "Greater"; break;
530 case Lt: func = "Lesser"; break;
531 case And: func = "And"; break;
532 case Or: func = "Or"; break;
533 case Not: func = "Not"; break;
534 case Cond: func = "Condition"; break;
535 case While: func = "While"; break;
536 case Code: func = "Code"; break;
537 case Print: func = "Print"; break;
538 case Prog: func = "Program"; break;
539 case Call: func = "Call"; break;
540 case List: func = "List"; break;
541 case Edit: func = "Edit"; break;
542 case Del: func = "Del"; break;
543 }
544
545 fprintf (stdout, "Function: %s\n", func);
546
547 if ((root->func == Val) && (root->ops[0] == NULL)) {
548 for (i = 0; i < level; i++) {
549 fprintf (stdout, " ");
550 }
551 fprintf (stdout, "value: %f\n", root->value);
552 } else {
553 for (i = 0; i < root->nbops; i++) {
554 print_element (root->ops[i], level + 1);
555 }
556 }
557 }
558
559 /* storage functions */
560
561 void memory (int nb)
562 {
563 int i, l;
564 double *tmp = NULL;
565 if (nb != storage_size) {
566 l = (nb < storage_size) ? nb : storage_size;
567 tmp = (double *) calloc (nb, sizeof (double));
568 if (tmp == NULL) {
569 VERBOSE (ERROR, fprintf (stderr, "can't allocate memory\n"));
570 exit (1);
571 }
572 for (i = 0; i < l; i++) {
573 tmp[i] = storage[i];
574 }
575 if (storage != NULL) {
576 free (storage);
577 }
578 storage = tmp;
579 storage_size = nb;
580 }
581 }
582
583 double store (int index, double value)
584 {
585 if (storage_size == -1) {
586 memory (DEFAULT_STORAGE_SIZE);
587 }
588 if ((index > 0) && (index <= storage_size)) {
589 storage[index - 1] = value;
590 } else {
591 VERBOSE (WARNING, fprintf (stdout, "invalid index (%d) [%d, %d]\n", index, (storage_size) ? 1 : 0, storage_size));
592 }
593 return value;
594 }
595
596 double recall (int index)
597 {
598 if (storage_size == -1) {
599 memory (DEFAULT_STORAGE_SIZE);
600 }
601 if ((index > 0) && (index <= storage_size)) {
602 return storage[index - 1];
603 } else {
604 VERBOSE (WARNING, fprintf (stdout, "invalid index (%d) [%d, %d]\n", index, (storage_size) ? 1 : 0, storage_size));
605 }
606 return 0;
607 }
608
609 double increase (int index)
610 {
611 if (storage_size == -1) {
612 memory (DEFAULT_STORAGE_SIZE);
613 }
614 if ((index > 0) && (index <= storage_size)) {
615 return storage[index - 1]++;
616 } else {
617 VERBOSE (WARNING, fprintf (stdout, "invalid index (%d) [%d, %d]\n", index, (storage_size) ? 1 : 0, storage_size));
618 }
619 return 0;
620 }
621
622 double decrease (int index)
623 {
624 if (storage_size == -1) {
625 memory (DEFAULT_STORAGE_SIZE);
626 }
627 if ((index > 0) && (index <= storage_size)) {
628 return storage[index - 1]--;
629 } else {
630 VERBOSE (WARNING, fprintf (stdout, "invalid index (%d) [%d, %d]\n", index, (storage_size) ? 1 : 0, storage_size));
631 }
632 return 0;
633 }
634
635 void display (void)
636 {
637 int i;
638 if (storage_size == -1) {
639 memory (DEFAULT_STORAGE_SIZE);
640 }
641 fprintf (stdout, "storage:");
642 for (i = 0; i < storage_size; i++) {
643 fprintf (stdout, " ");
644 fprintf (stdout, minform, storage[i]);
645 }
646 fprintf (stdout, "\n");
647 }
648
649 void clear ()
650 {
651 int i;
652 for (i = 0; i < storage_size; i++) {
653 storage[i] = 0;
654 }
655 }
656
657 /* While do function */
658
659 double while_do (element_t *cond, element_t *action)
660 {
661 double ret = 0;
662 element_t *temp = NULL;
663
664 VERBOSE (DEBUG, fprintf (stdout, "starting while loop\n"));
665 while (1) {
666 VERBOSE (DEBUG, fprintf (stdout, "loop...\n"));
667
668 temp = dupelement (cond);
669 if (!evaluate_element (temp, 0)) {
670 break;
671 }
672 if (action) {
673 temp = dupelement (action);
674 ret = evaluate_element (temp, 0);
675 }
676 }
677
678 VERBOSE (DEBUG, fprintf (stdout, "ending while loop\n"));
679
680 return ret;
681 }
682
683 /* program function */
684
685 double execute_code (element_t **prog, int nbcalls)
686 {
687 double ret = 0;
688 int i;
689 for (i = 0; i < nbcalls; i++) {
690 ret = evaluate_element (prog[i], 0);
691 prog[i] = NULL;
692 }
693 return ret;
694 }
695
696 /* print function */
697
698 void set_format (char *prompt, int precision)
699 {
700 char buffer[128] = {0};
701 free_format ();
702 sprintf (buffer, "%s%%.%dg\n", prompt, precision);
703 format = strdup (buffer);
704 sprintf (buffer, "%%.%dg", precision);
705 minform = strdup (buffer);
706 }
707
708 void free_format ()
709 {
710 if (format) {
711 free (format);
712 format = NULL;
713 }
714 if (minform) {
715 free (minform);
716 minform = NULL;
717 }
718 }
719
720 double print (double value)
721 {
722 fprintf (stdout, format ? format : DEFAULT_FORMAT, value);
723 fflush (stdout);
724 return value;
725 }
726
727 /* quit function */
728
729 void quit (void)
730 {
731 fprintf (stdout, "bye\n");
732 exit (0);
733 }
734
735 /* program function */
736
737 void prog (int id, int nbmems, element_t *root)
738 {
739 int i, n = -1;
740
741 if (programs == NULL) {
742
743 /* initial memory allocation */
744 programs = (workspace_t *) calloc (1, sizeof (workspace_t));
745 if (programs == NULL) {
746 VERBOSE (ERROR, fprintf (stderr, "can't allocate memory\n"));
747 exit (1);
748 }
749 nb_programs = 1;
750 n = 0;
751
752 } else {
753
754 /* look for existing program */
755 for (i = 0; i < nb_programs; i++) {
756 if ((programs + i)->id == id) {
757 n = i;
758 break;
759 }
760 }
761 if (n == -1) {
762
763 /* new program */
764 n = nb_programs++;
765 programs = (workspace_t *) realloc (programs, nb_programs * sizeof (workspace_t));
766 } else {
767
768 /* clean old program */
769 if ((programs + n)->storage) {
770 free ((programs + n)->storage);
771 }
772 if ((programs + n)->root) {
773 delelement ((programs + n)->root);
774 }
775 }
776 }
777
778 /* set program */
779 (programs + n)->id = id;
780 (programs + n)->answer = 0;
781 (programs + n)->storage = (double *) calloc (nbmems, sizeof (double));
782 if ((programs + n)->storage == NULL) {
783 VERBOSE (ERROR, fprintf (stderr, "can't allocate memory\n"));
784 exit (1);
785 }
786 (programs + n)->storage_size = nbmems;
787 (programs + n)->root = dupelement (root);
788 }
789
790 double call (int id, int nbops, element_t **ops)
791 {
792 workspace_t tmp = {0};
793 int i, n = -1;
794 double ret = 0;
795
796 if (programs) {
797
798 /* look for program */
799 for (i = 0; i < nb_programs; i++) {
800 if ((programs + i)->id == id) {
801 n = i;
802 break;
803 }
804 }
805 if (n == -1) {
806 VERBOSE (WARNING, fprintf (stdout, "error unknown program (%d)\n", id));
807 return 0;
808 }
809
810 /* store context */
811 tmp.answer = answer;
812 tmp.storage = storage;
813 tmp.storage_size = storage_size;
814
815 /* change context */
816 answer = 0;
817 storage = (programs + n)->storage;
818 storage_size = (programs + n)->storage_size;
819 if (nbops > storage_size) {
820 storage = realloc (storage, nbops * sizeof (double));
821 storage_size = nbops;
822 }
823 for (i = 0; i < nbops; i++) {
824 double val = evaluate_element (ops[i], 0);
825 store (i + 1, val);
826 }
827
828 /* evaluate program */
829 element_t *elements = dupelement ((programs + n)->root);
830 ret = evaluate_element (elements, 0);
831
832 /* restore context */
833 answer = tmp.answer;
834 storage = tmp.storage;
835 storage_size = tmp.storage_size;
836 }
837
838 return ret;
839 }
840
841 void list ()
842 {
843 int i;
844 if (programs != NULL) {
845 fprintf (stdout, "programs:");
846 for (i = 0; i < nb_programs; i++) {
847 fprintf (stdout, " %d", (programs + i)->id);
848 }
849 fprintf (stdout, "\n");
850 }
851 }
852
853 void edit (int id)
854 { }
855
856 void del (int id)
857 { }
858
859 /* help message */
860
861 void help (void)
862 {
863 fprintf (stdout, "calc is a simple calculator\n\n");
864 fprintf (stdout, "supported operators:");
865 fprintf (stdout, " + - * / %% ^\n");
866 fprintf (stdout, "camparison operators:");
867 fprintf (stdout, " == != >= <= > <\n");
868 fprintf (stdout, "logical operators:");
869 fprintf (stdout, " & | !\n");
870 fprintf (stdout, "mathematic functions:");
871 fprintf (stdout, " pow sqrt log exp\n");
872 fprintf (stdout, "trig. func.:");
873 fprintf (stdout, " cos sin tan acos asin atan\n");
874 fprintf (stdout, "supported functions:");
875 fprintf (stdout, " abs ceil floor\n");
876 fprintf (stdout, "storage functions:");
877 fprintf (stdout, " mem sto rcl inc dec disp\n");
878 fprintf (stdout, "prog. functions:");
879 fprintf (stdout, " cond while print {} ;\n");
880 fprintf (stdout, "misc. functions:");
881 fprintf (stdout, " quit help\n");
882 fprintf (stdout, "supported constants:");
883 fprintf (stdout, " e pi\n");
884 }
885
886 /* evaluate element tree */
887
888 #define MASK_SUB 0x1
889 #define MASK_DIV 0x2
890
891 double evaluate_element (element_t *root, char mask)
892 {
893 double op0 = 0, op1 = 0;
894 char nextmask = mask;
895 int i, nb;
896
897 if ((root == NULL) || (root == ERROR_OP)) {
898 VERBOSE (WARNING, fprintf (stdout, "error while evaluating\n"));
899 return 0;
900 }
901
902 /* mask to manage sub operator sub and div */
903 switch (root->func) {
904 case Add:
905 nextmask &= ~MASK_SUB;
906 nextmask &= ~MASK_DIV;
907 break;
908 case Sub:
909 nextmask |= MASK_SUB;
910 nextmask &= ~MASK_DIV;
911 break;
912 case Mul:
913 nextmask &= ~MASK_DIV;
914 break;
915 case Div:
916 nextmask |= MASK_DIV;
917 break;
918 default:
919 nextmask = mask;
920 }
921
922 switch (root->func) {
923 case Val:
924 case Sig:
925 op0 = (root->ops[0]) ? evaluate_element (root->ops[0], nextmask) : root->value;
926 break;
927 case Add:
928 case Sub:
929 case Mul:
930 case Div:
931 case Mod:
932 case Pow:
933 case Store:
934 case Equal:
935 case Diff:
936 case Ge:
937 case Le:
938 case Gt:
939 case Lt:
940 case And:
941 case Or:
942 case Prog:
943 if (root->ops[1]) {
944 op1 = evaluate_element (root->ops[1], nextmask);
945 } else if (root->func != Store) {
946 VERBOSE (WARNING, fprintf (stdout, "error while evaluating (op[1])\n"));
947 return 0;
948 }
949 /* fallthrough */
950 case Sqr:
951 case Cos:
952 case Sin:
953 case Tan:
954 case Acos:
955 case Asin:
956 case Atan:
957 case Log:
958 case Exp:
959 case Abs:
960 case Ceil:
961 case Floor:
962 case Recall:
963 case Inc:
964 case Dec:
965 case Not:
966 case Mem:
967 case Cond:
968 case Call:
969 case Edit:
970 case Del:
971 if (root->ops[0]) {
972 op0 = evaluate_element (root->ops[0], 0);
973 } else {
974 VERBOSE (WARNING, fprintf (stdout, "error while evaluating (op[0])\n"));
975 return 0;
976 }
977 break;
978 case Disp:
979 case Clear:
980 case Quit:
981 case Help:
982 case Ans:
983 case Pi:
984 case E:
985 case Code:
986 case List:
987 break;
988 case While:
989 if (root->ops[0] == NULL) {
990 VERBOSE (WARNING, fprintf (stdout, "error while evaluating (op[0])\n"));
991 return 0;
992 }
993 break;
994 case Print:
995 op0 = (root->ops[0]) ? evaluate_element (root->ops[0], 0) : answer;
996 break;
997 }
998
999 switch (root->func) {
1000 case Val: return op0;
1001 case Sig: return -op0;
1002 case Add: return ((mask & MASK_SUB) == 0) ? op0 + op1 : op0 - op1;
1003 case Sub: return ((mask & MASK_SUB) == 0) ? op0 - op1 : op0 + op1;
1004 case Mul: return ((mask & MASK_DIV) == 0) ? op0 * op1 : op0 / op1;
1005 case Div: return ((mask & MASK_DIV) == 0) ? op0 / op1 : op0 * op1;
1006 case Mod: return fmod (op0, op1);
1007 case Pow: return pow (op0, op1);
1008 case Sqr: return sqrt (op0);
1009 case Cos: return cos (op0);
1010 case Sin: return sin (op0);
1011 case Tan: return tan (op0);
1012 case Acos: return acos (op0);
1013 case Asin: return asin (op0);
1014 case Atan: return atan (op0);
1015 case Log: return log (op0);
1016 case Exp: return exp (op0);
1017 case Abs: return fabs (op0);
1018 case Ceil: return ceil (op0);
1019 case Floor: return floor (op0);
1020 case Store: return store ((int)op0, (op1) ? op1 : answer);
1021 case Recall: return recall ((int)op0);
1022 case Inc: return increase ((int)op0);
1023 case Dec: return decrease ((int)op0);
1024 case Disp: display (); break;
1025 case Mem: memory ((int)op0); break;
1026 case Clear: clear (); break;
1027 case Quit: quit (); break;
1028 case Help: help (); break;
1029 case Ans: return answer;
1030 case Pi: return M_PI;
1031 case E: return M_E;
1032 case Equal: return op0 == op1;
1033 case Diff: return op0 != op1;
1034 case Ge: return op0 >= op1;
1035 case Le: return op0 <= op1;
1036 case Gt: return op0 > op1;
1037 case Lt: return op0 < op1;
1038 case And: return (op0 != 0) && (op1 != 0);
1039 case Or: return (op0 != 0) || (op1 != 0);
1040 case Not: return (op0 == 0);
1041 case Cond:
1042 if ((op0) && (root->ops[1])) {
1043 return evaluate_element (root->ops[1], 0);
1044 } else if ((!op0) && (root->ops[2])) {
1045 return evaluate_element (root->ops[2], 0);
1046 } else {
1047 return 0;
1048 }
1049 case While: return while_do (root->ops[0], root->ops[1]);
1050 case Code: return execute_code (root->ops, root->nbops);
1051 case Print: return print (op0);
1052 case Prog: prog ((int)op0, (int)op1, root->ops[2]); break;
1053 case Call:
1054 for (i = 1, nb =0; i < root->nbops; i++) {
1055 if (root->ops[i]) {
1056 nb++;
1057 }
1058 }
1059 return call ((int)op0, nb, root->ops + 1);
1060 case List: list (); break;
1061 case Edit: edit ((int)op0); break;
1062 case Del: del ((int)op0); break;
1063 }
1064
1065 return 0;
1066 }
1067
1068 char **generate_completion_list ()
1069 {
1070 int i, j, l = 0;
1071 char **list = (char **) calloc (NB_OPERATORS + NB_FUNCTIONS + NB_CONSTANTS + NB_SYMBOLS + 1, sizeof (char *));
1072 if (list == NULL) {
1073 VERBOSE (ERROR, fprintf (stderr, "can't allocate memory\n"));
1074 exit (1);
1075 }
1076
1077 for (i = 0; i < NB_OPERATORS; i++) {
1078 list[l] = strdup ((operators + i)->keyword);
1079 for (j = 0; j < (int)strlen (list[l]); j++) {
1080 if (list[i][j] == '\t') {
1081 list[i][j] = '\0';
1082 }
1083 }
1084 if (list[l] != NULL) {
1085 l++;
1086 }
1087 }
1088
1089 for (i = 0; i < NB_FUNCTIONS; i++) {
1090 list[l] = strdup ((functions + i)->keyword);
1091 if (list[l] != NULL) {
1092 l++;
1093 }
1094 }
1095
1096 for (i = 0; i < NB_CONSTANTS; i++) {
1097 list[l] = strdup ((constants + i)->keyword);
1098 if (list[l] != NULL) {
1099 l++;
1100 }
1101 }
1102
1103 for (i = 0; i < NB_SYMBOLS; i++) {
1104 list[l] = strdup (symbols[i]);
1105 if (list[l] != NULL) {
1106 l++;
1107 }
1108 }
1109
1110 return (list);
1111 }
1112
1113 void free_completion_list (char **list)
1114 {
1115 int i;
1116
1117 if (list) {
1118 for (i = 0; i < NB_FUNCTIONS + NB_CONSTANTS; i++) {
1119 if (list[i] != NULL) {
1120 free (list[i]);
1121 }
1122 }
1123 free (list);
1124 }
1125 }
1126
1127
1128 /* vim: set ts=4 sw=4 et: */