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