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