fix some error cases
[calc.git] / parser.c
CommitLineData
bc97a989 1#include <malloc.h>
f2927108 2#include <math.h>
87621fe1 3#include <stdio.h>
bc97a989
LM
4#include <stdlib.h>
5
6#include "debug.h"
bc97a989
LM
7
8#include "parser.h"
9
5075f6ea
LM
10/* global variables */
11
12double answer = 0;
13
6ba1dd0f
LM
14#define STORAGE_SIZE 10
15double storage[STORAGE_SIZE] = {0};
16
bc97a989
LM
17/* compare codes */
18
19int codecmp (char *ref, char *str)
20{
21 int sig;
22
23 while (*ref != '\0') {
0b9cc9b0
LM
24 if (*ref == '\t') {
25 sig = (*str == '.') ? -1 : ((*str >= '0') && (*str <= '9'));
26 } else {
27 sig = *str - *ref;
28 }
bc97a989
LM
29 if (sig != 0) {
30 return (sig > 0) ? 1 : -1;
31 }
0b9cc9b0
LM
32 str++;
33 ref++;
bc97a989
LM
34 }
35
36 return 0;
37}
38
39/* allocate new element */
40
11cda8d7 41element_t *newelement (func_t function, int nbops, int prio)
bc97a989
LM
42{
43 element_t *new = (element_t *) calloc (1, sizeof (element_t));
44 if (new == NULL) {
04d68907 45 VERBOSE (ERROR, fprintf (stderr, "can't allocate memory\n"));
bc97a989
LM
46 return NULL;
47 }
62d56da6
LM
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 }
0c95a3d3 55 }
bc97a989
LM
56 new->func = function;
57 new->nbops = nbops;
11cda8d7 58 new->prio = prio;
bc97a989
LM
59
60 return new;
61}
62
031d7bba
LM
63/* desallocate element */
64
65void delelement (element_t *root)
66{
031d7bba 67 if ((root != NULL) && (root != ERROR_OP)) {
0c95a3d3 68 int i;
031d7bba
LM
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 }
0c95a3d3
LM
74 if (root->nbops) {
75 free (root->ops);
76 }
031d7bba
LM
77 free (root);
78 }
79}
80
2a688642
LM
81/* duplicate element */
82
83element_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
bc97a989
LM
106/* functions */
107
ca3e2a2f 108#define NB_OPERATORS 14
bc97a989 109keyword_t operators[NB_OPERATORS] = {
11cda8d7
LM
110 { "+\t", Add, 2, 1, 1},
111 { "-\t", Sub, 2, 1, 1},
112 { "*", Mul, 2, 1, 2},
113 { "/", Div, 2, 1, 2},
c47a9298 114 { "%", Mod, 2, 1, 3},
7fe742c9 115 { "^", Pow, 2, 1, 4},
ca3e2a2f
LM
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}
bc97a989
LM
124};
125
2a688642 126#define NB_FUNCTIONS 17
bc97a989 127keyword_t functions[NB_FUNCTIONS] = {
c47a9298
LM
128 { "sqrt", Sqr, 1, 4, 5},
129 { "pow", Pow, 2, 3, 5},
130 { "cos", Cos, 1, 3, 5},
131 { "sin", Sin, 1, 3, 5},
471da7c9 132 { "atan", Atan, 1, 4, 5},
c47a9298 133 { "exp", Exp, 1, 3, 5},
89cf0955 134 { "log", Log, 1, 3, 5},
2a688642 135 { "sto", Store, 2, 3, 5},
6ba1dd0f 136 { "rcl", Recall, 1, 3, 5},
a8cf32ba
LM
137 { "inc", Inc, 1, 3, 5},
138 { "dec", Dec, 1, 3, 5},
ca3e2a2f
LM
139 { "disp", Disp, 0, 4, 9},
140 { "quit", Quit, 0, 4, 9},
141 { "help", Help, 0, 4, 9},
94b4e517 142 { "!", Not, 1, 1, 6},
2a688642
LM
143 { "cond", Cond, 3, 4, 5},
144 { "whl", While, 2, 3, 5}
471da7c9
LM
145};
146
5075f6ea 147#define NB_CONSTANTS 3
471da7c9 148keyword_t constants[NB_CONSTANTS] = {
5075f6ea
LM
149 { "ans", Ans, 0, 3, 5},
150 { "e", E, 0, 1, 5},
151 { "pi", Pi, 0, 2, 5}
bc97a989
LM
152};
153
d2ff8478
LM
154/* subparser function */
155
156element_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);
686c4bc2 164 if ((new->ops[1] == NULL) || ((new->ops[1] != ERROR_OP) && (new->ops[1]->prio == 9))) {
67b13e3d
LM
165 delelement (new->ops[1]);
166 new->ops[1] = ERROR_OP;
167 }
d2ff8478 168 if (new->ops[1] == ERROR_OP) {
031d7bba
LM
169 delelement (new);
170 *proot = NULL;
d2ff8478
LM
171 return ERROR_OP;
172 }
3b4b0bbe 173 *proot = newelement (Val, 1, 5);
031d7bba
LM
174 if (*proot == NULL) {
175 delelement (new);
d2ff8478
LM
176 return ERROR_OP;
177 }
178 (*proot)->ops[0] = new;
179
180 return *proot;
181}
182
bc97a989
LM
183/* parser function */
184
ef37d966 185element_t *parser (char *str, char **next, int prio)
0b489a77 186{
bc97a989 187 element_t *root = NULL;
85b4a72c 188 int i;
bc97a989 189
04d68907 190 VERBOSE (DEBUG, fprintf (stdout, "Starting parsing\n"));
49223129 191
bc97a989
LM
192 /* main loop */
193 while (*str != '\0') {
194 int found = 0;
195 element_t *new = NULL;
04d68907 196 VERBOSE (INFO, fprintf (stdout, "Processing: %s\n", str));
bc97a989
LM
197
198 /* skip spaces and tabs */
199
200 if ((*str == ' ') || (*str == '\t')) {
201 str++;
202 continue;
203 }
204
124da7fd
LM
205 /* check for open brace */
206
207 if (*str == '{') {
208 VERBOSE (DEBUG, fprintf (stdout, "start processing brace\n"));
62d56da6 209 if (root != NULL) {
124da7fd
LM
210 delelement (root);
211 return ERROR_OP;
212 }
62d56da6
LM
213 element_t **prog = NULL;
214 new = newelement (Prog, 0, 5);
215 if (new == NULL) {
216 delelement (root);
217 return ERROR_OP;
124da7fd 218 }
62d56da6
LM
219 root = new;
220 prog = &root;
124da7fd
LM
221
222 do {
124da7fd
LM
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 }
7929d368 228 if (new == ERROR_OP) {
124da7fd 229 delelement (root);
62d56da6 230 return ERROR_OP;
124da7fd 231 }
62d56da6
LM
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;
124da7fd 236 }
62d56da6
LM
237 newprog->ops[(*prog)->nbops] = new;
238 delelement (*prog);
239 (*prog) = newprog;
124da7fd
LM
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
85b4a72c 251 /* check for open bracket */
0b489a77 252
0b489a77 253 if (*str == '(') {
04d68907 254 VERBOSE (DEBUG, fprintf (stdout, "start processing bracket\n"));
0b489a77 255 if (root) {
85b4a72c
LM
256 do {
257 found = 0;
ef37d966 258 new = parser (str + 1, &str, 0);
686c4bc2 259 if ((new == NULL) || ((new != ERROR_OP) && (new->prio == 9))) {
67b13e3d
LM
260 delelement (new);
261 new = ERROR_OP;
262 }
686c4bc2 263 if ((new == NULL) || (new == ERROR_OP)) {
031d7bba 264 delelement (root);
85b4a72c
LM
265 return ERROR_OP;
266 }
267 for (i = 0; i < root->nbops; i++) {
268 if (root->ops[i] == NULL) {
0b489a77
LM
269 root->ops[i] = new;
270 found = 1;
271 break;
272 }
273 }
85b4a72c 274 if (!found) {
031d7bba
LM
275 delelement (new);
276 delelement (root);
85b4a72c
LM
277 return ERROR_OP;
278 }
279 } while (*str == ',');
280 } else {
3b4b0bbe 281 root = newelement (Val, 1, 5);
85b4a72c 282 if (root == NULL) {
0b489a77
LM
283 return ERROR_OP;
284 }
ef37d966 285 new = parser (str + 1, &str, 0);
686c4bc2 286 if ((new == NULL) || ((new != ERROR_OP) && (new->prio == 9))) {
67b13e3d
LM
287 delelement (new);
288 new = ERROR_OP;
289 }
686c4bc2 290 if ((new == NULL) || (new == ERROR_OP) || (*str == ',')) {
031d7bba
LM
291 delelement (new);
292 delelement (root);
efdfb543
LM
293 return ERROR_OP;
294 }
85b4a72c 295 root->ops[0] = new;
0b489a77 296 }
3c0db5bc
LM
297 if (*str != ')') {
298 delelement (root);
299 return ERROR_OP;
300 }
85b4a72c 301 str++;
04d68907 302 VERBOSE (DEBUG, fprintf (stdout, "stop processing bracket\n"));
efdfb543 303 continue;
49223129 304 }
85b4a72c 305
124da7fd 306 /* check for closing bracket, closing brace or koma */
85b4a72c 307
124da7fd 308 if ((*str == ')') || (*str == '}') || (*str == ',')) {
45a631a8
LM
309 if (prio == -9) {
310 delelement (root);
311 return ERROR_OP;
312 }
49223129 313 if (next != NULL) {
85b4a72c 314 *next = str;
49223129
LM
315 }
316 return root;
0b489a77
LM
317 }
318
bc97a989
LM
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) {
04d68907 324 VERBOSE (DEBUG, fprintf (stdout, "start processing operator\n"));
11cda8d7 325 if (root) {
ef37d966 326 if ((prio) && (prio > operator->prio)) {
04d68907 327 VERBOSE (DEBUG, fprintf (stdout, "stop because operator priority\n"));
ef37d966
LM
328 *next = str;
329 return root;
330 }
331 str += operator->offset;
04d68907 332 VERBOSE (INFO, fprintf (stdout, "Oper: %d\n", operator->func));
d2ff8478 333 if (subparser (&root, &str, operator->func, operator->nbops, operator->prio) == ERROR_OP) {
031d7bba 334 delelement (root);
ef37d966 335 return ERROR_OP;
11cda8d7 336 }
3b4b0bbe
LM
337 } else if (*str == '-') {
338 new = newelement (Sig, 1, 9);
339 if (new == NULL) {
340 return ERROR_OP;
341 }
342 root = new;
49223129 343 } else {
0b489a77 344 return ERROR_OP;
bc97a989 345 }
bc97a989 346 found = 1;
04d68907 347 VERBOSE (DEBUG, fprintf (stdout, "stop processing operator\n"));
bc97a989
LM
348 break;
349 }
350 }
351 if (found) {
bc97a989
LM
352 continue;
353 }
49223129 354
bc97a989
LM
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) {
04d68907 360 VERBOSE (DEBUG, fprintf (stdout, "start processing function\n"));
bc97a989 361 if (root == NULL) {
04d68907 362 VERBOSE (INFO, fprintf (stdout, "Func: %d\n", function->func));
11cda8d7 363 new = newelement (function->func, function->nbops, function->prio);
bc97a989 364 if (new == NULL) {
0b489a77 365 return ERROR_OP;
bc97a989
LM
366 }
367 root = new;
031d7bba
LM
368 } else {
369 delelement (root);
0b489a77 370 return ERROR_OP;
bc97a989
LM
371 }
372 str += function->offset;
373 found = 1;
04d68907 374 VERBOSE (DEBUG, fprintf (stdout, "stop processing function\n"));
bc97a989
LM
375 break;
376 }
377 }
378 if (found) {
bc97a989
LM
379 continue;
380 }
381
471da7c9
LM
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) {
5075f6ea 389 VERBOSE (INFO, fprintf (stdout, "Const: %d\n", constant->func));
471da7c9
LM
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
bc97a989
LM
409 /* look for number */
410
0b9cc9b0
LM
411 if (((*str >= '0') && (*str <= '9')) ||
412 (*str == '.') || (*str == '+') || (*str == '-')) {
04d68907 413 VERBOSE (DEBUG, fprintf (stdout, "start processing value\n"));
bc97a989 414 char *pt;
fd88e359 415 double value = strtod (str, &pt);
04d68907 416 VERBOSE (INFO, fprintf (stdout, "Value: %f\n", value));
bc97a989 417 if (str != pt) {
ce6627f2 418 if ((root == NULL) || (root->prio == 6)) {
3b4b0bbe 419 new = newelement (Val, 1, 5);
c47a9298
LM
420 if (new == NULL) {
421 return ERROR_OP;
422 }
423 new->value = value;
ce6627f2
LM
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 }
c47a9298 440 str = pt;
32741902
LM
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) {
031d7bba 448 delelement (root);
0b489a77 449 return ERROR_OP;
bc97a989 450 }
0b9cc9b0 451 } else {
031d7bba 452 delelement (root);
0b9cc9b0 453 return ERROR_OP;
bc97a989 454 }
bc97a989
LM
455 found = 1;
456 }
04d68907 457 VERBOSE (DEBUG, fprintf (stdout, "stop processing value\n"));
bc97a989
LM
458 }
459
460 /* error */
461
462 if (!found) {
031d7bba 463 delelement (root);
0b489a77 464 return ERROR_OP;
bc97a989
LM
465 }
466
467 }
468
49223129
LM
469 if (next != NULL) {
470 *next = str;
471 }
031d7bba 472
bc97a989
LM
473 return root;
474}
475
476/* print element tree */
477
478void print_element (element_t *root, int level)
479{
480 char *func = NULL;
481 int i;
482
49223129 483 if ((root == NULL) || (root == ERROR_OP)) {
bc97a989 484 return;
49223129
LM
485 }
486
bc97a989 487 for (i = 0; i < level; i++) {
04d68907 488 fprintf (stdout, " ");
bc97a989
LM
489 }
490
491 switch (root->func) {
492 case Val: func = "Value"; break;
89cf0955 493 case Sig: func = "Sign"; break;
bc97a989
LM
494 case Add: func = "Addition"; break;
495 case Sub: func = "Subtraction"; break;
496 case Mul: func = "Multiplication"; break;
497 case Div: func = "Division"; break;
c47a9298 498 case Mod: func = "Modulo"; break;
bc97a989
LM
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;
471da7c9 503 case Atan: func = "Arc Tangent"; break;
bc97a989
LM
504 case Log: func = "Logarithm"; break;
505 case Exp: func = "Exponantial"; break;
6ba1dd0f
LM
506 case Store: func = "Store"; break;
507 case Recall: func = "Recall"; break;
a8cf32ba
LM
508 case Inc: func = "Increase"; break;
509 case Dec: func = "Decrease"; break;
6ba1dd0f 510 case Disp: func = "Display"; break;
471da7c9
LM
511 case Quit: func = "Quit"; break;
512 case Help: func = "Help"; break;
5075f6ea 513 case Ans: func = "Ans"; break;
471da7c9
LM
514 case Pi: func = "Pi"; break;
515 case E: func = "E"; break;
7fe742c9
LM
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;
ca3e2a2f
LM
522 case And: func = "And"; break;
523 case Or: func = "Or"; break;
524 case Not: func = "Not"; break;
94b4e517 525 case Cond: func = "Condition"; break;
2a688642 526 case While: func = "While"; break;
124da7fd 527 case Prog: func = "Program"; break;
bc97a989
LM
528 }
529
04d68907 530 fprintf (stdout, "Function: %s\n", func);
bc97a989 531
85b4a72c 532 if ((root->func == Val) && (root->ops[0] == NULL)) {
bc97a989 533 for (i = 0; i < level; i++) {
04d68907 534 fprintf (stdout, " ");
bc97a989 535 }
04d68907 536 fprintf (stdout, "value: %f\n", root->value);
bc97a989
LM
537 } else {
538 for (i = 0; i < root->nbops; i++) {
539 print_element (root->ops[i], level + 1);
540 }
541 }
542}
543
6ba1dd0f
LM
544/* storage functions */
545
3639df66 546double store (int index, double value)
6ba1dd0f
LM
547{
548 if ((index > 0) && (index <= STORAGE_SIZE)) {
3639df66 549 storage[index - 1] = value;
6ba1dd0f
LM
550 } else {
551 VERBOSE (WARNING, fprintf (stdout, "invalid index (%d) [1, %d]\n", index, STORAGE_SIZE));
552 }
3639df66 553 return value;
6ba1dd0f
LM
554}
555
556double 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
a8cf32ba
LM
566double 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
576double 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
6ba1dd0f
LM
586void 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
2a688642
LM
596/* While do function */
597
598double 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
124da7fd
LM
625/* program function */
626
627double 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
89cf0955
LM
638/* quit function */
639
640void quit (void)
641{
04d68907 642 fprintf (stdout, "bye\n");
89cf0955
LM
643 exit (0);
644}
645
646/* help message */
647
648void help (void)
649{
04d68907 650 fprintf (stdout, "calc is a simple calculator\n\n");
94b4e517
LM
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:");
a8cf32ba 660 fprintf (stdout, " sto rcl inc dec\n");
94b4e517
LM
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:");
471da7c9 666 fprintf (stdout, " e pi\n");
89cf0955 667}
3b4b0bbe 668
f2927108
LM
669/* evaluate element tree */
670
89cf0955
LM
671#define MASK_SUB 0x1
672#define MASK_DIV 0x2
673
3b4b0bbe 674double evaluate_element (element_t *root, char mask)
f2927108
LM
675{
676 double op0 = 0, op1 = 0;
3b4b0bbe 677 char nextmask = mask;
f2927108
LM
678
679 if ((root == NULL) || (root == ERROR_OP)) {
04d68907 680 VERBOSE (WARNING, fprintf (stdout, "error while evaluating\n"));
f2927108
LM
681 return 0;
682 }
683
3b4b0bbe
LM
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
f2927108
LM
704 switch (root->func) {
705 case Val:
3b4b0bbe
LM
706 case Sig:
707 op0 = (root->ops[0]) ? evaluate_element (root->ops[0], nextmask) : root->value;
f2927108
LM
708 break;
709 case Add:
710 case Sub:
711 case Mul:
712 case Div:
c47a9298 713 case Mod:
f2927108 714 case Pow:
3639df66 715 case Store:
7fe742c9
LM
716 case Equal:
717 case Diff:
718 case Ge:
719 case Le:
720 case Gt:
721 case Lt:
ca3e2a2f
LM
722 case And:
723 case Or:
f2927108 724 if (root->ops[1]) {
3b4b0bbe 725 op1 = evaluate_element (root->ops[1], nextmask);
3639df66 726 } else if (root->func != Store) {
04d68907 727 VERBOSE (WARNING, fprintf (stdout, "error while evaluating (op[1])\n"));
f2927108
LM
728 return 0;
729 }
730 /* fallthrough */
731 case Sqr:
732 case Cos:
733 case Sin:
471da7c9 734 case Atan:
f2927108
LM
735 case Log:
736 case Exp:
6ba1dd0f 737 case Recall:
a8cf32ba
LM
738 case Inc:
739 case Dec:
ca3e2a2f 740 case Not:
94b4e517 741 case Cond:
f2927108 742 if (root->ops[0]) {
3b4b0bbe 743 op0 = evaluate_element (root->ops[0], 0);
f2927108 744 } else {
04d68907 745 VERBOSE (WARNING, fprintf (stdout, "error while evaluating (op[0])\n"));
f2927108
LM
746 return 0;
747 }
89cf0955 748 break;
6ba1dd0f 749 case Disp:
471da7c9
LM
750 case Quit:
751 case Help:
5075f6ea 752 case Ans:
471da7c9
LM
753 case Pi:
754 case E:
124da7fd 755 case Prog:
09d87cae 756 break;
2a688642 757 case While:
09d87cae
LM
758 if (root->ops[0] == NULL) {
759 VERBOSE (WARNING, fprintf (stdout, "error while evaluating (op[0])\n"));
760 return 0;
761 }
89cf0955 762 break;
f2927108
LM
763 }
764
765 switch (root->func) {
3b4b0bbe
LM
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;
c47a9298 772 case Mod: return fmod (op0, op1);
f2927108
LM
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);
471da7c9 777 case Atan: return atan (op0);
f2927108
LM
778 case Log: return log (op0);
779 case Exp: return exp (op0);
3639df66 780 case Store: return store ((int)op0, (op1) ? op1 : answer);
6ba1dd0f 781 case Recall: return recall ((int)op0);
a8cf32ba
LM
782 case Inc: return increase ((int)op0);
783 case Dec: return decrease ((int)op0);
6ba1dd0f 784 case Disp: display (); break;
471da7c9
LM
785 case Quit: quit (); break;
786 case Help: help (); break;
5075f6ea 787 case Ans: return answer;
471da7c9
LM
788 case Pi: return M_PI;
789 case E: return M_E;
7fe742c9
LM
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;
ca3e2a2f
LM
796 case And: return (op0 != 0) && (op1 != 0);
797 case Or: return (op0 != 0) || (op1 != 0);
798 case Not: return (op0 == 0);
94b4e517
LM
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 }
2a688642 807 case While: return while_do (root->ops[0], root->ops[1]);
124da7fd 808 case Prog: return program_do (root->ops, root->nbops);
f2927108
LM
809 }
810
811 return 0;
812}
813
bc97a989 814/* vim: set ts=4 sw=4 et: */