full completion feature
[calc.git] / parser.c
CommitLineData
bc97a989 1#include <malloc.h>
f2927108 2#include <math.h>
87621fe1 3#include <stdio.h>
bc97a989 4#include <stdlib.h>
b9c1c40d 5#include <string.h>
bc97a989
LM
6
7#include "debug.h"
bc97a989
LM
8
9#include "parser.h"
10
5075f6ea
LM
11/* global variables */
12
13double answer = 0;
14
6ba1dd0f
LM
15#define STORAGE_SIZE 10
16double storage[STORAGE_SIZE] = {0};
17
bc97a989
LM
18/* compare codes */
19
20int codecmp (char *ref, char *str)
21{
22 int sig;
23
24 while (*ref != '\0') {
0b9cc9b0
LM
25 if (*ref == '\t') {
26 sig = (*str == '.') ? -1 : ((*str >= '0') && (*str <= '9'));
27 } else {
28 sig = *str - *ref;
29 }
bc97a989
LM
30 if (sig != 0) {
31 return (sig > 0) ? 1 : -1;
32 }
0b9cc9b0
LM
33 str++;
34 ref++;
bc97a989
LM
35 }
36
37 return 0;
38}
39
40/* allocate new element */
41
11cda8d7 42element_t *newelement (func_t function, int nbops, int prio)
bc97a989
LM
43{
44 element_t *new = (element_t *) calloc (1, sizeof (element_t));
45 if (new == NULL) {
04d68907 46 VERBOSE (ERROR, fprintf (stderr, "can't allocate memory\n"));
d32ea0b0 47 exit (1);
bc97a989 48 }
62d56da6
LM
49 if (nbops) {
50 new->ops = (element_t **) calloc (nbops, sizeof (element_t *));
51 if (new->ops == NULL) {
62d56da6 52 VERBOSE (ERROR, fprintf (stderr, "can't allocate memory\n"));
d32ea0b0 53 exit (1);
62d56da6 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);
2a688642
LM
92 tmp->value = root->value;
93 for (i = 0; i < root->nbops; i++) {
94 tmp->ops[i] = dupelement (root->ops[i]);
2a688642
LM
95 }
96 return tmp;
97}
98
bc97a989
LM
99/* functions */
100
ca3e2a2f 101#define NB_OPERATORS 14
bc97a989 102keyword_t operators[NB_OPERATORS] = {
11cda8d7
LM
103 { "+\t", Add, 2, 1, 1},
104 { "-\t", Sub, 2, 1, 1},
105 { "*", Mul, 2, 1, 2},
106 { "/", Div, 2, 1, 2},
c47a9298 107 { "%", Mod, 2, 1, 3},
7fe742c9 108 { "^", Pow, 2, 1, 4},
ca3e2a2f
LM
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}
bc97a989
LM
117};
118
9e52bbc5 119#define NB_FUNCTIONS 23
bc97a989 120keyword_t functions[NB_FUNCTIONS] = {
c47a9298
LM
121 { "sqrt", Sqr, 1, 4, 5},
122 { "pow", Pow, 2, 3, 5},
123 { "cos", Cos, 1, 3, 5},
124 { "sin", Sin, 1, 3, 5},
9e52bbc5
LM
125 { "tan", Tan, 1, 3, 5},
126 { "acos", Acos, 1, 4, 5},
127 { "asin", Asin, 1, 4, 5},
471da7c9 128 { "atan", Atan, 1, 4, 5},
89cf0955 129 { "log", Log, 1, 3, 5},
9e52bbc5
LM
130 { "exp", Exp, 1, 3, 5},
131 { "abs", Abs, 1, 3, 5},
132 { "floor", Floor, 1, 5, 5},
133 { "ceil", Ceil, 1, 4, 5},
2a688642 134 { "sto", Store, 2, 3, 5},
6ba1dd0f 135 { "rcl", Recall, 1, 3, 5},
a8cf32ba
LM
136 { "inc", Inc, 1, 3, 5},
137 { "dec", Dec, 1, 3, 5},
ca3e2a2f
LM
138 { "disp", Disp, 0, 4, 9},
139 { "quit", Quit, 0, 4, 9},
140 { "help", Help, 0, 4, 9},
94b4e517 141 { "!", Not, 1, 1, 6},
2a688642 142 { "cond", Cond, 3, 4, 5},
4e3d2e04 143 { "while", While, 2, 5, 5}
471da7c9
LM
144};
145
5075f6ea 146#define NB_CONSTANTS 3
471da7c9 147keyword_t constants[NB_CONSTANTS] = {
5075f6ea
LM
148 { "ans", Ans, 0, 3, 5},
149 { "e", E, 0, 1, 5},
150 { "pi", Pi, 0, 2, 5}
bc97a989
LM
151};
152
db660e60
LM
153#define NB_SYMBOLS 4
154char *symbols[NB_SYMBOLS] = {
155 "(", ")", "{", "}"
156};
157
d2ff8478
LM
158/* subparser function */
159
160element_t *subparser (element_t **proot, char **pstr, func_t func, int nbops, int prio)
161{
162 element_t *new = newelement (func, nbops, prio);
d2ff8478
LM
163 new->ops[0] = *proot;
164 new->ops[1] = parser (*pstr, pstr, new->prio);
686c4bc2 165 if ((new->ops[1] == NULL) || ((new->ops[1] != ERROR_OP) && (new->ops[1]->prio == 9))) {
67b13e3d
LM
166 delelement (new->ops[1]);
167 new->ops[1] = ERROR_OP;
168 }
d2ff8478 169 if (new->ops[1] == ERROR_OP) {
031d7bba
LM
170 delelement (new);
171 *proot = NULL;
d2ff8478
LM
172 return ERROR_OP;
173 }
3b4b0bbe 174 *proot = newelement (Val, 1, 5);
d2ff8478
LM
175 (*proot)->ops[0] = new;
176
177 return *proot;
178}
179
bc97a989
LM
180/* parser function */
181
ef37d966 182element_t *parser (char *str, char **next, int prio)
0b489a77 183{
bc97a989 184 element_t *root = NULL;
85b4a72c 185 int i;
bc97a989 186
04d68907 187 VERBOSE (DEBUG, fprintf (stdout, "Starting parsing\n"));
49223129 188
bc97a989
LM
189 /* main loop */
190 while (*str != '\0') {
191 int found = 0;
192 element_t *new = NULL;
04d68907 193 VERBOSE (INFO, fprintf (stdout, "Processing: %s\n", str));
bc97a989
LM
194
195 /* skip spaces and tabs */
196
197 if ((*str == ' ') || (*str == '\t')) {
198 str++;
199 continue;
200 }
201
124da7fd
LM
202 /* check for open brace */
203
204 if (*str == '{') {
205 VERBOSE (DEBUG, fprintf (stdout, "start processing brace\n"));
62d56da6 206 if (root != NULL) {
124da7fd
LM
207 delelement (root);
208 return ERROR_OP;
209 }
62d56da6
LM
210 element_t **prog = NULL;
211 new = newelement (Prog, 0, 5);
62d56da6
LM
212 root = new;
213 prog = &root;
124da7fd
LM
214
215 do {
124da7fd
LM
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 }
7929d368 221 if (new == ERROR_OP) {
124da7fd 222 delelement (root);
62d56da6 223 return ERROR_OP;
124da7fd 224 }
62d56da6
LM
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;
124da7fd 229 }
62d56da6
LM
230 newprog->ops[(*prog)->nbops] = new;
231 delelement (*prog);
232 (*prog) = newprog;
124da7fd
LM
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
85b4a72c 244 /* check for open bracket */
0b489a77 245
0b489a77 246 if (*str == '(') {
04d68907 247 VERBOSE (DEBUG, fprintf (stdout, "start processing bracket\n"));
0b489a77 248 if (root) {
85b4a72c
LM
249 do {
250 found = 0;
ef37d966 251 new = parser (str + 1, &str, 0);
686c4bc2 252 if ((new == NULL) || ((new != ERROR_OP) && (new->prio == 9))) {
67b13e3d
LM
253 delelement (new);
254 new = ERROR_OP;
255 }
686c4bc2 256 if ((new == NULL) || (new == ERROR_OP)) {
031d7bba 257 delelement (root);
85b4a72c
LM
258 return ERROR_OP;
259 }
260 for (i = 0; i < root->nbops; i++) {
261 if (root->ops[i] == NULL) {
0b489a77
LM
262 root->ops[i] = new;
263 found = 1;
264 break;
265 }
266 }
85b4a72c 267 if (!found) {
031d7bba
LM
268 delelement (new);
269 delelement (root);
85b4a72c
LM
270 return ERROR_OP;
271 }
272 } while (*str == ',');
273 } else {
3b4b0bbe 274 root = newelement (Val, 1, 5);
ef37d966 275 new = parser (str + 1, &str, 0);
686c4bc2 276 if ((new == NULL) || ((new != ERROR_OP) && (new->prio == 9))) {
67b13e3d
LM
277 delelement (new);
278 new = ERROR_OP;
279 }
686c4bc2 280 if ((new == NULL) || (new == ERROR_OP) || (*str == ',')) {
031d7bba
LM
281 delelement (new);
282 delelement (root);
efdfb543
LM
283 return ERROR_OP;
284 }
85b4a72c 285 root->ops[0] = new;
0b489a77 286 }
3c0db5bc
LM
287 if (*str != ')') {
288 delelement (root);
289 return ERROR_OP;
290 }
85b4a72c 291 str++;
04d68907 292 VERBOSE (DEBUG, fprintf (stdout, "stop processing bracket\n"));
efdfb543 293 continue;
49223129 294 }
85b4a72c 295
124da7fd 296 /* check for closing bracket, closing brace or koma */
85b4a72c 297
124da7fd 298 if ((*str == ')') || (*str == '}') || (*str == ',')) {
45a631a8
LM
299 if (prio == -9) {
300 delelement (root);
301 return ERROR_OP;
302 }
49223129 303 if (next != NULL) {
85b4a72c 304 *next = str;
49223129
LM
305 }
306 return root;
0b489a77
LM
307 }
308
bc97a989
LM
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) {
04d68907 314 VERBOSE (DEBUG, fprintf (stdout, "start processing operator\n"));
11cda8d7 315 if (root) {
ef37d966 316 if ((prio) && (prio > operator->prio)) {
04d68907 317 VERBOSE (DEBUG, fprintf (stdout, "stop because operator priority\n"));
ef37d966
LM
318 *next = str;
319 return root;
320 }
321 str += operator->offset;
04d68907 322 VERBOSE (INFO, fprintf (stdout, "Oper: %d\n", operator->func));
d2ff8478 323 if (subparser (&root, &str, operator->func, operator->nbops, operator->prio) == ERROR_OP) {
031d7bba 324 delelement (root);
ef37d966 325 return ERROR_OP;
11cda8d7 326 }
3b4b0bbe 327 } else if (*str == '-') {
d32ea0b0 328 root = newelement (Sig, 1, 9);
49223129 329 } else {
0b489a77 330 return ERROR_OP;
bc97a989 331 }
bc97a989 332 found = 1;
04d68907 333 VERBOSE (DEBUG, fprintf (stdout, "stop processing operator\n"));
bc97a989
LM
334 break;
335 }
336 }
337 if (found) {
bc97a989
LM
338 continue;
339 }
49223129 340
bc97a989
LM
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) {
04d68907 346 VERBOSE (DEBUG, fprintf (stdout, "start processing function\n"));
bc97a989 347 if (root == NULL) {
04d68907 348 VERBOSE (INFO, fprintf (stdout, "Func: %d\n", function->func));
d32ea0b0 349 root = newelement (function->func, function->nbops, function->prio);
031d7bba
LM
350 } else {
351 delelement (root);
0b489a77 352 return ERROR_OP;
bc97a989
LM
353 }
354 str += function->offset;
355 found = 1;
04d68907 356 VERBOSE (DEBUG, fprintf (stdout, "stop processing function\n"));
bc97a989
LM
357 break;
358 }
359 }
360 if (found) {
bc97a989
LM
361 continue;
362 }
363
471da7c9
LM
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) {
5075f6ea 371 VERBOSE (INFO, fprintf (stdout, "Const: %d\n", constant->func));
d32ea0b0 372 root = newelement (constant->func, constant->nbops, constant->prio);
471da7c9
LM
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
bc97a989
LM
387 /* look for number */
388
0b9cc9b0
LM
389 if (((*str >= '0') && (*str <= '9')) ||
390 (*str == '.') || (*str == '+') || (*str == '-')) {
04d68907 391 VERBOSE (DEBUG, fprintf (stdout, "start processing value\n"));
bc97a989 392 char *pt;
fd88e359 393 double value = strtod (str, &pt);
04d68907 394 VERBOSE (INFO, fprintf (stdout, "Value: %f\n", value));
bc97a989 395 if (str != pt) {
ce6627f2 396 if ((root == NULL) || (root->prio == 6)) {
3b4b0bbe 397 new = newelement (Val, 1, 5);
c47a9298 398 new->value = value;
ce6627f2
LM
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 }
c47a9298 415 str = pt;
32741902
LM
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) {
031d7bba 423 delelement (root);
0b489a77 424 return ERROR_OP;
bc97a989 425 }
0b9cc9b0 426 } else {
031d7bba 427 delelement (root);
0b9cc9b0 428 return ERROR_OP;
bc97a989 429 }
bc97a989
LM
430 found = 1;
431 }
04d68907 432 VERBOSE (DEBUG, fprintf (stdout, "stop processing value\n"));
bc97a989
LM
433 }
434
435 /* error */
436
437 if (!found) {
031d7bba 438 delelement (root);
0b489a77 439 return ERROR_OP;
bc97a989
LM
440 }
441
442 }
443
49223129
LM
444 if (next != NULL) {
445 *next = str;
446 }
031d7bba 447
bc97a989
LM
448 return root;
449}
450
451/* print element tree */
452
453void print_element (element_t *root, int level)
454{
455 char *func = NULL;
456 int i;
457
49223129 458 if ((root == NULL) || (root == ERROR_OP)) {
bc97a989 459 return;
49223129
LM
460 }
461
bc97a989 462 for (i = 0; i < level; i++) {
04d68907 463 fprintf (stdout, " ");
bc97a989
LM
464 }
465
466 switch (root->func) {
467 case Val: func = "Value"; break;
89cf0955 468 case Sig: func = "Sign"; break;
bc97a989
LM
469 case Add: func = "Addition"; break;
470 case Sub: func = "Subtraction"; break;
471 case Mul: func = "Multiplication"; break;
472 case Div: func = "Division"; break;
c47a9298 473 case Mod: func = "Modulo"; break;
bc97a989
LM
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;
9e52bbc5
LM
478 case Tan: func = "Tangent"; break;
479 case Acos: func = "Arc Cosine"; break;
480 case Asin: func = "Arc Sine"; break;
471da7c9 481 case Atan: func = "Arc Tangent"; break;
bc97a989
LM
482 case Log: func = "Logarithm"; break;
483 case Exp: func = "Exponantial"; break;
9e52bbc5
LM
484 case Abs: func = "Absolute value"; break;
485 case Ceil: func = "Ceil value"; break;
486 case Floor: func = "Floor value"; break;
6ba1dd0f
LM
487 case Store: func = "Store"; break;
488 case Recall: func = "Recall"; break;
a8cf32ba
LM
489 case Inc: func = "Increase"; break;
490 case Dec: func = "Decrease"; break;
6ba1dd0f 491 case Disp: func = "Display"; break;
471da7c9
LM
492 case Quit: func = "Quit"; break;
493 case Help: func = "Help"; break;
5075f6ea 494 case Ans: func = "Ans"; break;
471da7c9
LM
495 case Pi: func = "Pi"; break;
496 case E: func = "E"; break;
7fe742c9
LM
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;
ca3e2a2f
LM
503 case And: func = "And"; break;
504 case Or: func = "Or"; break;
505 case Not: func = "Not"; break;
94b4e517 506 case Cond: func = "Condition"; break;
2a688642 507 case While: func = "While"; break;
124da7fd 508 case Prog: func = "Program"; break;
bc97a989
LM
509 }
510
04d68907 511 fprintf (stdout, "Function: %s\n", func);
bc97a989 512
85b4a72c 513 if ((root->func == Val) && (root->ops[0] == NULL)) {
bc97a989 514 for (i = 0; i < level; i++) {
04d68907 515 fprintf (stdout, " ");
bc97a989 516 }
04d68907 517 fprintf (stdout, "value: %f\n", root->value);
bc97a989
LM
518 } else {
519 for (i = 0; i < root->nbops; i++) {
520 print_element (root->ops[i], level + 1);
521 }
522 }
523}
524
6ba1dd0f
LM
525/* storage functions */
526
3639df66 527double store (int index, double value)
6ba1dd0f
LM
528{
529 if ((index > 0) && (index <= STORAGE_SIZE)) {
3639df66 530 storage[index - 1] = value;
6ba1dd0f
LM
531 } else {
532 VERBOSE (WARNING, fprintf (stdout, "invalid index (%d) [1, %d]\n", index, STORAGE_SIZE));
533 }
3639df66 534 return value;
6ba1dd0f
LM
535}
536
537double 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
a8cf32ba
LM
547double 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
557double 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
6ba1dd0f
LM
567void 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
2a688642
LM
577/* While do function */
578
579double 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"));
2a688642
LM
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
124da7fd
LM
603/* program function */
604
605double 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
89cf0955
LM
616/* quit function */
617
618void quit (void)
619{
04d68907 620 fprintf (stdout, "bye\n");
89cf0955
LM
621 exit (0);
622}
623
624/* help message */
625
626void help (void)
627{
04d68907 628 fprintf (stdout, "calc is a simple calculator\n\n");
94b4e517
LM
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");
9e52bbc5 635 fprintf (stdout, "mathematic functions:");
b9c1c40d
LM
636 fprintf (stdout, " pow sqrt log exp\n");
637 fprintf (stdout, "trigonometric functions:");
638 fprintf (stdout, " cos sin tan acos asin atan\n");
94b4e517 639 fprintf (stdout, "supported functions:");
9e52bbc5 640 fprintf (stdout, " abs ceil floor\n");
94b4e517 641 fprintf (stdout, "storage functions:");
a8cf32ba 642 fprintf (stdout, " sto rcl inc dec\n");
94b4e517 643 fprintf (stdout, "conditional functions:");
4e3d2e04 644 fprintf (stdout, " cond while\n");
94b4e517
LM
645 fprintf (stdout, "miscellaneous functions:");
646 fprintf (stdout, " quit help\n");
647 fprintf (stdout, "supported constants:");
471da7c9 648 fprintf (stdout, " e pi\n");
89cf0955 649}
3b4b0bbe 650
f2927108
LM
651/* evaluate element tree */
652
89cf0955
LM
653#define MASK_SUB 0x1
654#define MASK_DIV 0x2
655
3b4b0bbe 656double evaluate_element (element_t *root, char mask)
f2927108
LM
657{
658 double op0 = 0, op1 = 0;
3b4b0bbe 659 char nextmask = mask;
f2927108
LM
660
661 if ((root == NULL) || (root == ERROR_OP)) {
04d68907 662 VERBOSE (WARNING, fprintf (stdout, "error while evaluating\n"));
f2927108
LM
663 return 0;
664 }
665
3b4b0bbe
LM
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
f2927108
LM
686 switch (root->func) {
687 case Val:
3b4b0bbe
LM
688 case Sig:
689 op0 = (root->ops[0]) ? evaluate_element (root->ops[0], nextmask) : root->value;
f2927108
LM
690 break;
691 case Add:
692 case Sub:
693 case Mul:
694 case Div:
c47a9298 695 case Mod:
f2927108 696 case Pow:
3639df66 697 case Store:
7fe742c9
LM
698 case Equal:
699 case Diff:
700 case Ge:
701 case Le:
702 case Gt:
703 case Lt:
ca3e2a2f
LM
704 case And:
705 case Or:
f2927108 706 if (root->ops[1]) {
3b4b0bbe 707 op1 = evaluate_element (root->ops[1], nextmask);
3639df66 708 } else if (root->func != Store) {
04d68907 709 VERBOSE (WARNING, fprintf (stdout, "error while evaluating (op[1])\n"));
f2927108
LM
710 return 0;
711 }
712 /* fallthrough */
713 case Sqr:
714 case Cos:
715 case Sin:
9e52bbc5
LM
716 case Tan:
717 case Acos:
718 case Asin:
471da7c9 719 case Atan:
f2927108
LM
720 case Log:
721 case Exp:
9e52bbc5
LM
722 case Abs:
723 case Ceil:
724 case Floor:
6ba1dd0f 725 case Recall:
a8cf32ba
LM
726 case Inc:
727 case Dec:
ca3e2a2f 728 case Not:
94b4e517 729 case Cond:
f2927108 730 if (root->ops[0]) {
3b4b0bbe 731 op0 = evaluate_element (root->ops[0], 0);
f2927108 732 } else {
04d68907 733 VERBOSE (WARNING, fprintf (stdout, "error while evaluating (op[0])\n"));
f2927108
LM
734 return 0;
735 }
89cf0955 736 break;
6ba1dd0f 737 case Disp:
471da7c9
LM
738 case Quit:
739 case Help:
5075f6ea 740 case Ans:
471da7c9
LM
741 case Pi:
742 case E:
124da7fd 743 case Prog:
09d87cae 744 break;
2a688642 745 case While:
09d87cae
LM
746 if (root->ops[0] == NULL) {
747 VERBOSE (WARNING, fprintf (stdout, "error while evaluating (op[0])\n"));
748 return 0;
749 }
89cf0955 750 break;
f2927108
LM
751 }
752
753 switch (root->func) {
3b4b0bbe
LM
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;
c47a9298 760 case Mod: return fmod (op0, op1);
f2927108
LM
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);
9e52bbc5
LM
765 case Tan: return tan (op0);
766 case Acos: return acos (op0);
767 case Asin: return asin (op0);
471da7c9 768 case Atan: return atan (op0);
f2927108
LM
769 case Log: return log (op0);
770 case Exp: return exp (op0);
9e52bbc5
LM
771 case Abs: return fabs (op0);
772 case Ceil: return ceil (op0);
773 case Floor: return floor (op0);
3639df66 774 case Store: return store ((int)op0, (op1) ? op1 : answer);
6ba1dd0f 775 case Recall: return recall ((int)op0);
a8cf32ba
LM
776 case Inc: return increase ((int)op0);
777 case Dec: return decrease ((int)op0);
6ba1dd0f 778 case Disp: display (); break;
471da7c9
LM
779 case Quit: quit (); break;
780 case Help: help (); break;
5075f6ea 781 case Ans: return answer;
471da7c9
LM
782 case Pi: return M_PI;
783 case E: return M_E;
7fe742c9
LM
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;
ca3e2a2f
LM
790 case And: return (op0 != 0) && (op1 != 0);
791 case Or: return (op0 != 0) || (op1 != 0);
792 case Not: return (op0 == 0);
94b4e517
LM
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 }
2a688642 801 case While: return while_do (root->ops[0], root->ops[1]);
124da7fd 802 case Prog: return program_do (root->ops, root->nbops);
f2927108
LM
803 }
804
805 return 0;
806}
807
b9c1c40d
LM
808char **generate_completion_list ()
809{
db660e60
LM
810 int i, j, l = 0;
811 char **list = (char **) calloc (NB_OPERATORS + NB_FUNCTIONS + NB_CONSTANTS + NB_SYMBOLS + 1, sizeof (char *));
b9c1c40d
LM
812 if (list == NULL) {
813 VERBOSE (ERROR, fprintf (stderr, "can't allocate memory\n"));
814 exit (1);
815 }
816
db660e60
LM
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
b9c1c40d
LM
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
db660e60
LM
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
b9c1c40d
LM
858 return (list);
859}
860
861void 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
bc97a989 876/* vim: set ts=4 sw=4 et: */