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