add some coverage tests
[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 == ',')) {
49223129 309 if (next != NULL) {
85b4a72c 310 *next = str;
49223129
LM
311 }
312 return root;
0b489a77
LM
313 }
314
bc97a989
LM
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) {
04d68907 320 VERBOSE (DEBUG, fprintf (stdout, "start processing operator\n"));
11cda8d7 321 if (root) {
ef37d966 322 if ((prio) && (prio > operator->prio)) {
04d68907 323 VERBOSE (DEBUG, fprintf (stdout, "stop because operator priority\n"));
ef37d966
LM
324 *next = str;
325 return root;
326 }
327 str += operator->offset;
04d68907 328 VERBOSE (INFO, fprintf (stdout, "Oper: %d\n", operator->func));
d2ff8478 329 if (subparser (&root, &str, operator->func, operator->nbops, operator->prio) == ERROR_OP) {
031d7bba 330 delelement (root);
ef37d966 331 return ERROR_OP;
11cda8d7 332 }
3b4b0bbe
LM
333 } else if (*str == '-') {
334 new = newelement (Sig, 1, 9);
335 if (new == NULL) {
336 return ERROR_OP;
337 }
338 root = new;
49223129 339 } else {
0b489a77 340 return ERROR_OP;
bc97a989 341 }
bc97a989 342 found = 1;
04d68907 343 VERBOSE (DEBUG, fprintf (stdout, "stop processing operator\n"));
bc97a989
LM
344 break;
345 }
346 }
347 if (found) {
bc97a989
LM
348 continue;
349 }
49223129 350
bc97a989
LM
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) {
04d68907 356 VERBOSE (DEBUG, fprintf (stdout, "start processing function\n"));
bc97a989 357 if (root == NULL) {
04d68907 358 VERBOSE (INFO, fprintf (stdout, "Func: %d\n", function->func));
11cda8d7 359 new = newelement (function->func, function->nbops, function->prio);
bc97a989 360 if (new == NULL) {
0b489a77 361 return ERROR_OP;
bc97a989
LM
362 }
363 root = new;
031d7bba
LM
364 } else {
365 delelement (root);
0b489a77 366 return ERROR_OP;
bc97a989
LM
367 }
368 str += function->offset;
369 found = 1;
04d68907 370 VERBOSE (DEBUG, fprintf (stdout, "stop processing function\n"));
bc97a989
LM
371 break;
372 }
373 }
374 if (found) {
bc97a989
LM
375 continue;
376 }
377
471da7c9
LM
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) {
5075f6ea 385 VERBOSE (INFO, fprintf (stdout, "Const: %d\n", constant->func));
471da7c9
LM
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
bc97a989
LM
405 /* look for number */
406
0b9cc9b0
LM
407 if (((*str >= '0') && (*str <= '9')) ||
408 (*str == '.') || (*str == '+') || (*str == '-')) {
04d68907 409 VERBOSE (DEBUG, fprintf (stdout, "start processing value\n"));
bc97a989 410 char *pt;
fd88e359 411 double value = strtod (str, &pt);
04d68907 412 VERBOSE (INFO, fprintf (stdout, "Value: %f\n", value));
bc97a989 413 if (str != pt) {
ce6627f2 414 if ((root == NULL) || (root->prio == 6)) {
3b4b0bbe 415 new = newelement (Val, 1, 5);
c47a9298
LM
416 if (new == NULL) {
417 return ERROR_OP;
418 }
419 new->value = value;
ce6627f2
LM
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 }
c47a9298 436 str = pt;
32741902
LM
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) {
031d7bba 444 delelement (root);
0b489a77 445 return ERROR_OP;
bc97a989 446 }
0b9cc9b0 447 } else {
031d7bba 448 delelement (root);
0b9cc9b0 449 return ERROR_OP;
bc97a989 450 }
bc97a989
LM
451 found = 1;
452 }
04d68907 453 VERBOSE (DEBUG, fprintf (stdout, "stop processing value\n"));
bc97a989
LM
454 }
455
456 /* error */
457
458 if (!found) {
031d7bba 459 delelement (root);
0b489a77 460 return ERROR_OP;
bc97a989
LM
461 }
462
463 }
464
49223129
LM
465 if (next != NULL) {
466 *next = str;
467 }
031d7bba 468
bc97a989
LM
469 return root;
470}
471
472/* print element tree */
473
474void print_element (element_t *root, int level)
475{
476 char *func = NULL;
477 int i;
478
49223129 479 if ((root == NULL) || (root == ERROR_OP)) {
bc97a989 480 return;
49223129
LM
481 }
482
bc97a989 483 for (i = 0; i < level; i++) {
04d68907 484 fprintf (stdout, " ");
bc97a989
LM
485 }
486
487 switch (root->func) {
488 case Val: func = "Value"; break;
89cf0955 489 case Sig: func = "Sign"; break;
bc97a989
LM
490 case Add: func = "Addition"; break;
491 case Sub: func = "Subtraction"; break;
492 case Mul: func = "Multiplication"; break;
493 case Div: func = "Division"; break;
c47a9298 494 case Mod: func = "Modulo"; break;
bc97a989
LM
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;
471da7c9 499 case Atan: func = "Arc Tangent"; break;
bc97a989
LM
500 case Log: func = "Logarithm"; break;
501 case Exp: func = "Exponantial"; break;
6ba1dd0f
LM
502 case Store: func = "Store"; break;
503 case Recall: func = "Recall"; break;
a8cf32ba
LM
504 case Inc: func = "Increase"; break;
505 case Dec: func = "Decrease"; break;
6ba1dd0f 506 case Disp: func = "Display"; break;
471da7c9
LM
507 case Quit: func = "Quit"; break;
508 case Help: func = "Help"; break;
5075f6ea 509 case Ans: func = "Ans"; break;
471da7c9
LM
510 case Pi: func = "Pi"; break;
511 case E: func = "E"; break;
7fe742c9
LM
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;
ca3e2a2f
LM
518 case And: func = "And"; break;
519 case Or: func = "Or"; break;
520 case Not: func = "Not"; break;
94b4e517 521 case Cond: func = "Condition"; break;
2a688642 522 case While: func = "While"; break;
124da7fd 523 case Prog: func = "Program"; break;
bc97a989
LM
524 }
525
04d68907 526 fprintf (stdout, "Function: %s\n", func);
bc97a989 527
85b4a72c 528 if ((root->func == Val) && (root->ops[0] == NULL)) {
bc97a989 529 for (i = 0; i < level; i++) {
04d68907 530 fprintf (stdout, " ");
bc97a989 531 }
04d68907 532 fprintf (stdout, "value: %f\n", root->value);
bc97a989
LM
533 } else {
534 for (i = 0; i < root->nbops; i++) {
535 print_element (root->ops[i], level + 1);
536 }
537 }
538}
539
6ba1dd0f
LM
540/* storage functions */
541
3639df66 542double store (int index, double value)
6ba1dd0f
LM
543{
544 if ((index > 0) && (index <= STORAGE_SIZE)) {
3639df66 545 storage[index - 1] = value;
6ba1dd0f
LM
546 } else {
547 VERBOSE (WARNING, fprintf (stdout, "invalid index (%d) [1, %d]\n", index, STORAGE_SIZE));
548 }
3639df66 549 return value;
6ba1dd0f
LM
550}
551
552double 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
a8cf32ba
LM
562double 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
572double 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
6ba1dd0f
LM
582void 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
2a688642
LM
592/* While do function */
593
594double 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
124da7fd
LM
621/* program function */
622
623double 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
89cf0955
LM
634/* quit function */
635
636void quit (void)
637{
04d68907 638 fprintf (stdout, "bye\n");
89cf0955
LM
639 exit (0);
640}
641
642/* help message */
643
644void help (void)
645{
04d68907 646 fprintf (stdout, "calc is a simple calculator\n\n");
94b4e517
LM
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:");
a8cf32ba 656 fprintf (stdout, " sto rcl inc dec\n");
94b4e517
LM
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:");
471da7c9 662 fprintf (stdout, " e pi\n");
89cf0955 663}
3b4b0bbe 664
f2927108
LM
665/* evaluate element tree */
666
89cf0955
LM
667#define MASK_SUB 0x1
668#define MASK_DIV 0x2
669
3b4b0bbe 670double evaluate_element (element_t *root, char mask)
f2927108
LM
671{
672 double op0 = 0, op1 = 0;
3b4b0bbe 673 char nextmask = mask;
f2927108
LM
674
675 if ((root == NULL) || (root == ERROR_OP)) {
04d68907 676 VERBOSE (WARNING, fprintf (stdout, "error while evaluating\n"));
f2927108
LM
677 return 0;
678 }
679
3b4b0bbe
LM
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
f2927108
LM
700 switch (root->func) {
701 case Val:
3b4b0bbe
LM
702 case Sig:
703 op0 = (root->ops[0]) ? evaluate_element (root->ops[0], nextmask) : root->value;
f2927108
LM
704 break;
705 case Add:
706 case Sub:
707 case Mul:
708 case Div:
c47a9298 709 case Mod:
f2927108 710 case Pow:
3639df66 711 case Store:
7fe742c9
LM
712 case Equal:
713 case Diff:
714 case Ge:
715 case Le:
716 case Gt:
717 case Lt:
ca3e2a2f
LM
718 case And:
719 case Or:
f2927108 720 if (root->ops[1]) {
3b4b0bbe 721 op1 = evaluate_element (root->ops[1], nextmask);
3639df66 722 } else if (root->func != Store) {
04d68907 723 VERBOSE (WARNING, fprintf (stdout, "error while evaluating (op[1])\n"));
f2927108
LM
724 return 0;
725 }
726 /* fallthrough */
727 case Sqr:
728 case Cos:
729 case Sin:
471da7c9 730 case Atan:
f2927108
LM
731 case Log:
732 case Exp:
6ba1dd0f 733 case Recall:
a8cf32ba
LM
734 case Inc:
735 case Dec:
ca3e2a2f 736 case Not:
94b4e517 737 case Cond:
f2927108 738 if (root->ops[0]) {
3b4b0bbe 739 op0 = evaluate_element (root->ops[0], 0);
f2927108 740 } else {
04d68907 741 VERBOSE (WARNING, fprintf (stdout, "error while evaluating (op[0])\n"));
f2927108
LM
742 return 0;
743 }
89cf0955 744 break;
6ba1dd0f 745 case Disp:
471da7c9
LM
746 case Quit:
747 case Help:
5075f6ea 748 case Ans:
471da7c9
LM
749 case Pi:
750 case E:
124da7fd 751 case Prog:
09d87cae 752 break;
2a688642 753 case While:
09d87cae
LM
754 if (root->ops[0] == NULL) {
755 VERBOSE (WARNING, fprintf (stdout, "error while evaluating (op[0])\n"));
756 return 0;
757 }
89cf0955 758 break;
f2927108
LM
759 }
760
761 switch (root->func) {
3b4b0bbe
LM
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;
c47a9298 768 case Mod: return fmod (op0, op1);
f2927108
LM
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);
471da7c9 773 case Atan: return atan (op0);
f2927108
LM
774 case Log: return log (op0);
775 case Exp: return exp (op0);
3639df66 776 case Store: return store ((int)op0, (op1) ? op1 : answer);
6ba1dd0f 777 case Recall: return recall ((int)op0);
a8cf32ba
LM
778 case Inc: return increase ((int)op0);
779 case Dec: return decrease ((int)op0);
6ba1dd0f 780 case Disp: display (); break;
471da7c9
LM
781 case Quit: quit (); break;
782 case Help: help (); break;
5075f6ea 783 case Ans: return answer;
471da7c9
LM
784 case Pi: return M_PI;
785 case E: return M_E;
7fe742c9
LM
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;
ca3e2a2f
LM
792 case And: return (op0 != 0) && (op1 != 0);
793 case Or: return (op0 != 0) || (op1 != 0);
794 case Not: return (op0 == 0);
94b4e517
LM
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 }
2a688642 803 case While: return while_do (root->ops[0], root->ops[1]);
124da7fd 804 case Prog: return program_do (root->ops, root->nbops);
f2927108
LM
805 }
806
807 return 0;
808}
809
bc97a989 810/* vim: set ts=4 sw=4 et: */