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