fix comparison operator priority
[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 }
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
57void 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
bc97a989
LM
70/* functions */
71
7fe742c9 72#define NB_OPERATORS 12
bc97a989
LM
73
74keyword_t operators[NB_OPERATORS] = {
11cda8d7
LM
75 { "+\t", Add, 2, 1, 1},
76 { "-\t", Sub, 2, 1, 1},
77 { "*", Mul, 2, 1, 2},
78 { "/", Div, 2, 1, 2},
c47a9298 79 { "%", Mod, 2, 1, 3},
7fe742c9 80 { "^", Pow, 2, 1, 4},
319b7056
LM
81 { "==", Equal, 2, 2, 0},
82 { "!=", Diff, 2, 2, 0},
83 { ">=", Ge, 2, 2, 0},
84 { "<=", Le, 2, 2, 0},
85 { ">", Gt, 2, 1, 0},
86 { "<", Lt, 2, 1, 0}
bc97a989
LM
87};
88
6ba1dd0f 89#define NB_FUNCTIONS 12
bc97a989 90keyword_t functions[NB_FUNCTIONS] = {
c47a9298
LM
91 { "sqrt", Sqr, 1, 4, 5},
92 { "pow", Pow, 2, 3, 5},
93 { "cos", Cos, 1, 3, 5},
94 { "sin", Sin, 1, 3, 5},
471da7c9 95 { "atan", Atan, 1, 4, 5},
c47a9298 96 { "exp", Exp, 1, 3, 5},
89cf0955 97 { "log", Log, 1, 3, 5},
3639df66 98 { "sto", Store, 2, 3, 5},
6ba1dd0f 99 { "rcl", Recall, 1, 3, 5},
7fe742c9 100 { "disp", Disp, 0, 4, 5},
471da7c9
LM
101 { "quit", Quit, 0, 4, 5},
102 { "help", Help, 0, 4, 5}
103};
104
5075f6ea 105#define NB_CONSTANTS 3
471da7c9 106keyword_t constants[NB_CONSTANTS] = {
5075f6ea
LM
107 { "ans", Ans, 0, 3, 5},
108 { "e", E, 0, 1, 5},
109 { "pi", Pi, 0, 2, 5}
bc97a989
LM
110};
111
d2ff8478
LM
112/* subparser function */
113
114element_t *subparser (element_t **proot, char **pstr, func_t func, int nbops, int prio)
115{
116 element_t *new = newelement (func, nbops, prio);
117 if (new == NULL) {
118 return ERROR_OP;
119 }
120 new->ops[0] = *proot;
121 new->ops[1] = parser (*pstr, pstr, new->prio);
122 if (new->ops[1] == ERROR_OP) {
031d7bba
LM
123 delelement (new);
124 *proot = NULL;
d2ff8478
LM
125 return ERROR_OP;
126 }
3b4b0bbe 127 *proot = newelement (Val, 1, 5);
031d7bba
LM
128 if (*proot == NULL) {
129 delelement (new);
d2ff8478
LM
130 return ERROR_OP;
131 }
132 (*proot)->ops[0] = new;
133
134 return *proot;
135}
136
bc97a989
LM
137/* parser function */
138
ef37d966 139element_t *parser (char *str, char **next, int prio)
0b489a77 140{
bc97a989 141 element_t *root = NULL;
85b4a72c 142 int i;
bc97a989 143
04d68907 144 VERBOSE (DEBUG, fprintf (stdout, "Starting parsing\n"));
49223129 145
bc97a989
LM
146 /* main loop */
147 while (*str != '\0') {
148 int found = 0;
149 element_t *new = NULL;
04d68907 150 VERBOSE (INFO, fprintf (stdout, "Processing: %s\n", str));
bc97a989
LM
151
152 /* skip spaces and tabs */
153
154 if ((*str == ' ') || (*str == '\t')) {
155 str++;
156 continue;
157 }
158
85b4a72c 159 /* check for open bracket */
0b489a77 160
0b489a77 161 if (*str == '(') {
04d68907 162 VERBOSE (DEBUG, fprintf (stdout, "start processing bracket\n"));
0b489a77 163 if (root) {
85b4a72c
LM
164 do {
165 found = 0;
ef37d966 166 new = parser (str + 1, &str, 0);
85b4a72c 167 if (new == ERROR_OP) {
031d7bba 168 delelement (root);
85b4a72c
LM
169 return ERROR_OP;
170 }
171 for (i = 0; i < root->nbops; i++) {
172 if (root->ops[i] == NULL) {
0b489a77
LM
173 root->ops[i] = new;
174 found = 1;
175 break;
176 }
177 }
85b4a72c 178 if (!found) {
031d7bba
LM
179 delelement (new);
180 delelement (root);
85b4a72c
LM
181 return ERROR_OP;
182 }
183 } while (*str == ',');
184 } else {
3b4b0bbe 185 root = newelement (Val, 1, 5);
85b4a72c 186 if (root == NULL) {
0b489a77
LM
187 return ERROR_OP;
188 }
ef37d966 189 new = parser (str + 1, &str, 0);
85b4a72c 190 if ((new == ERROR_OP) || (*str == ',')) {
031d7bba
LM
191 delelement (new);
192 delelement (root);
efdfb543
LM
193 return ERROR_OP;
194 }
85b4a72c 195 root->ops[0] = new;
0b489a77 196 }
85b4a72c 197 str++;
04d68907 198 VERBOSE (DEBUG, fprintf (stdout, "stop processing bracket\n"));
efdfb543 199 continue;
49223129 200 }
85b4a72c
LM
201
202 /* check for closing bracket or koma */
203
204 if ((*str == ')') || (*str == ',')) {
49223129 205 if (next != NULL) {
85b4a72c 206 *next = str;
49223129
LM
207 }
208 return root;
0b489a77
LM
209 }
210
bc97a989
LM
211 /* look for operators */
212
213 for (i = 0; i < NB_OPERATORS; i++) {
214 keyword_t *operator = operators + i;
215 if (codecmp (operator->keyword, str) == 0) {
04d68907 216 VERBOSE (DEBUG, fprintf (stdout, "start processing operator\n"));
11cda8d7 217 if (root) {
ef37d966 218 if ((prio) && (prio > operator->prio)) {
04d68907 219 VERBOSE (DEBUG, fprintf (stdout, "stop because operator priority\n"));
ef37d966
LM
220 *next = str;
221 return root;
222 }
223 str += operator->offset;
04d68907 224 VERBOSE (INFO, fprintf (stdout, "Oper: %d\n", operator->func));
d2ff8478 225 if (subparser (&root, &str, operator->func, operator->nbops, operator->prio) == ERROR_OP) {
031d7bba 226 delelement (root);
ef37d966 227 return ERROR_OP;
11cda8d7 228 }
3b4b0bbe
LM
229 } else if (*str == '-') {
230 new = newelement (Sig, 1, 9);
231 if (new == NULL) {
232 return ERROR_OP;
233 }
234 root = new;
49223129 235 } else {
0b489a77 236 return ERROR_OP;
bc97a989 237 }
bc97a989 238 found = 1;
04d68907 239 VERBOSE (DEBUG, fprintf (stdout, "stop processing operator\n"));
bc97a989
LM
240 break;
241 }
242 }
243 if (found) {
bc97a989
LM
244 continue;
245 }
49223129 246
bc97a989
LM
247 /* look for functions */
248
249 for (i = 0; i < NB_FUNCTIONS; i++) {
250 keyword_t *function = functions + i;
251 if (codecmp (function->keyword, str) == 0) {
04d68907 252 VERBOSE (DEBUG, fprintf (stdout, "start processing function\n"));
bc97a989 253 if (root == NULL) {
04d68907 254 VERBOSE (INFO, fprintf (stdout, "Func: %d\n", function->func));
11cda8d7 255 new = newelement (function->func, function->nbops, function->prio);
bc97a989 256 if (new == NULL) {
0b489a77 257 return ERROR_OP;
bc97a989
LM
258 }
259 root = new;
031d7bba
LM
260 } else {
261 delelement (root);
0b489a77 262 return ERROR_OP;
bc97a989
LM
263 }
264 str += function->offset;
265 found = 1;
04d68907 266 VERBOSE (DEBUG, fprintf (stdout, "stop processing function\n"));
bc97a989
LM
267 break;
268 }
269 }
270 if (found) {
bc97a989
LM
271 continue;
272 }
273
471da7c9
LM
274 /* look for constant */
275
276 for (i = 0; i < NB_CONSTANTS; i++) {
277 keyword_t *constant = constants + i;
278 if (codecmp (constant->keyword, str) == 0) {
279 VERBOSE (DEBUG, fprintf (stdout, "start processing constant\n"));
280 if (root == NULL) {
5075f6ea 281 VERBOSE (INFO, fprintf (stdout, "Const: %d\n", constant->func));
471da7c9
LM
282 new = newelement (constant->func, constant->nbops, constant->prio);
283 if (new == NULL) {
284 return ERROR_OP;
285 }
286 root = new;
287 } else {
288 delelement (root);
289 return ERROR_OP;
290 }
291 str += constant->offset;
292 found = 1;
293 VERBOSE (DEBUG, fprintf (stdout, "stop processing constant\n"));
294 break;
295 }
296 }
297 if (found) {
298 continue;
299 }
300
bc97a989
LM
301 /* look for number */
302
0b9cc9b0
LM
303 if (((*str >= '0') && (*str <= '9')) ||
304 (*str == '.') || (*str == '+') || (*str == '-')) {
04d68907 305 VERBOSE (DEBUG, fprintf (stdout, "start processing value\n"));
bc97a989 306 char *pt;
fd88e359 307 double value = strtod (str, &pt);
04d68907 308 VERBOSE (INFO, fprintf (stdout, "Value: %f\n", value));
bc97a989 309 if (str != pt) {
bc97a989 310 if (root == NULL) {
3b4b0bbe 311 new = newelement (Val, 1, 5);
c47a9298
LM
312 if (new == NULL) {
313 return ERROR_OP;
314 }
315 new->value = value;
bc97a989 316 root = new;
c47a9298 317 str = pt;
32741902
LM
318 } else if ((*str == '+') || (*str == '-')) {
319 if ((prio) && (prio > 1)) {
320 VERBOSE (DEBUG, fprintf (stdout, "stop because operator priority\n"));
321 *next = str;
322 return root;
323 }
324 if (subparser (&root, &str, Add, 2, 1) == ERROR_OP) {
031d7bba 325 delelement (root);
0b489a77 326 return ERROR_OP;
bc97a989 327 }
0b9cc9b0 328 } else {
031d7bba 329 delelement (root);
0b9cc9b0 330 return ERROR_OP;
bc97a989 331 }
bc97a989
LM
332 found = 1;
333 }
04d68907 334 VERBOSE (DEBUG, fprintf (stdout, "stop processing value\n"));
bc97a989
LM
335 }
336
337 /* error */
338
339 if (!found) {
031d7bba 340 delelement (root);
0b489a77 341 return ERROR_OP;
bc97a989
LM
342 }
343
344 }
345
49223129
LM
346 if (next != NULL) {
347 *next = str;
348 }
031d7bba 349
bc97a989
LM
350 return root;
351}
352
353/* print element tree */
354
355void print_element (element_t *root, int level)
356{
357 char *func = NULL;
358 int i;
359
49223129 360 if ((root == NULL) || (root == ERROR_OP)) {
bc97a989 361 return;
49223129
LM
362 }
363
bc97a989 364 for (i = 0; i < level; i++) {
04d68907 365 fprintf (stdout, " ");
bc97a989
LM
366 }
367
368 switch (root->func) {
369 case Val: func = "Value"; break;
89cf0955 370 case Sig: func = "Sign"; break;
bc97a989
LM
371 case Add: func = "Addition"; break;
372 case Sub: func = "Subtraction"; break;
373 case Mul: func = "Multiplication"; break;
374 case Div: func = "Division"; break;
c47a9298 375 case Mod: func = "Modulo"; break;
bc97a989
LM
376 case Pow: func = "Power"; break;
377 case Sqr: func = "Square Root"; break;
378 case Cos: func = "Cosine"; break;
379 case Sin: func = "Sine"; break;
471da7c9 380 case Atan: func = "Arc Tangent"; break;
bc97a989
LM
381 case Log: func = "Logarithm"; break;
382 case Exp: func = "Exponantial"; break;
6ba1dd0f
LM
383 case Store: func = "Store"; break;
384 case Recall: func = "Recall"; break;
385 case Disp: func = "Display"; break;
471da7c9
LM
386 case Quit: func = "Quit"; break;
387 case Help: func = "Help"; break;
5075f6ea 388 case Ans: func = "Ans"; break;
471da7c9
LM
389 case Pi: func = "Pi"; break;
390 case E: func = "E"; break;
7fe742c9
LM
391 case Equal: func = "Equal"; break;
392 case Diff: func = "Different"; break;
393 case Ge: func = "Greater or equal"; break;
394 case Le: func = "Lesser or equal"; break;
395 case Gt: func = "Greater"; break;
396 case Lt: func = "Lesser"; break;
bc97a989
LM
397 }
398
04d68907 399 fprintf (stdout, "Function: %s\n", func);
bc97a989 400
85b4a72c 401 if ((root->func == Val) && (root->ops[0] == NULL)) {
bc97a989 402 for (i = 0; i < level; i++) {
04d68907 403 fprintf (stdout, " ");
bc97a989 404 }
04d68907 405 fprintf (stdout, "value: %f\n", root->value);
bc97a989
LM
406 } else {
407 for (i = 0; i < root->nbops; i++) {
408 print_element (root->ops[i], level + 1);
409 }
410 }
411}
412
6ba1dd0f
LM
413/* storage functions */
414
3639df66 415double store (int index, double value)
6ba1dd0f
LM
416{
417 if ((index > 0) && (index <= STORAGE_SIZE)) {
3639df66 418 storage[index - 1] = value;
6ba1dd0f
LM
419 } else {
420 VERBOSE (WARNING, fprintf (stdout, "invalid index (%d) [1, %d]\n", index, STORAGE_SIZE));
421 }
3639df66 422 return value;
6ba1dd0f
LM
423}
424
425double recall (int index)
426{
427 if ((index > 0) && (index <= STORAGE_SIZE)) {
428 return storage[index - 1];
429 } else {
430 VERBOSE (WARNING, fprintf (stdout, "invalid index (%d) [1, %d]\n", index, STORAGE_SIZE));
431 }
432 return 0;
433}
434
435void display (void)
436{
437 int i;
438 fprintf (stdout, "storage:");
439 for (i = 0; i < STORAGE_SIZE; i++) {
440 fprintf (stdout, " %g", storage[i]);
441 }
442 fprintf (stdout, "\n");
443}
444
89cf0955
LM
445/* quit function */
446
447void quit (void)
448{
04d68907 449 fprintf (stdout, "bye\n");
89cf0955
LM
450 exit (0);
451}
452
453/* help message */
454
455void help (void)
456{
04d68907
LM
457 fprintf (stdout, "calc is a simple calculator\n\n");
458 fprintf (stdout, "supported operators:\n");
459 fprintf (stdout, " + - * / %% ^\n\n");
7fe742c9
LM
460 fprintf (stdout, "camparison operators:\n");
461 fprintf (stdout, " == != >= <= > <\n\n");
04d68907
LM
462 fprintf (stdout, "supported functions:\n");
463 fprintf (stdout, " pow sqrt cos sin atan log exp\n\n");
6ba1dd0f
LM
464 fprintf (stdout, "storage functions:\n");
465 fprintf (stdout, " sto rcl\n\n");
04d68907 466 fprintf (stdout, "miscellaneous functions:\n");
471da7c9
LM
467 fprintf (stdout, " quit help\n\n");
468 fprintf (stdout, "supported constants:\n");
469 fprintf (stdout, " e pi\n");
89cf0955 470}
3b4b0bbe 471
f2927108
LM
472/* evaluate element tree */
473
89cf0955
LM
474#define MASK_SUB 0x1
475#define MASK_DIV 0x2
476
3b4b0bbe 477double evaluate_element (element_t *root, char mask)
f2927108
LM
478{
479 double op0 = 0, op1 = 0;
3b4b0bbe 480 char nextmask = mask;
f2927108
LM
481
482 if ((root == NULL) || (root == ERROR_OP)) {
04d68907 483 VERBOSE (WARNING, fprintf (stdout, "error while evaluating\n"));
f2927108
LM
484 return 0;
485 }
486
3b4b0bbe
LM
487 /* mask to manage sub operator sub and div */
488 switch (root->func) {
489 case Add:
490 nextmask &= ~MASK_SUB;
491 nextmask &= ~MASK_DIV;
492 break;
493 case Sub:
494 nextmask |= MASK_SUB;
495 nextmask &= ~MASK_DIV;
496 break;
497 case Mul:
498 nextmask &= ~MASK_DIV;
499 break;
500 case Div:
501 nextmask |= MASK_DIV;
502 break;
503 default:
504 nextmask = mask;
505 }
506
f2927108
LM
507 switch (root->func) {
508 case Val:
3b4b0bbe
LM
509 case Sig:
510 op0 = (root->ops[0]) ? evaluate_element (root->ops[0], nextmask) : root->value;
f2927108
LM
511 break;
512 case Add:
513 case Sub:
514 case Mul:
515 case Div:
c47a9298 516 case Mod:
f2927108 517 case Pow:
3639df66 518 case Store:
7fe742c9
LM
519 case Equal:
520 case Diff:
521 case Ge:
522 case Le:
523 case Gt:
524 case Lt:
f2927108 525 if (root->ops[1]) {
3b4b0bbe 526 op1 = evaluate_element (root->ops[1], nextmask);
3639df66 527 } else if (root->func != Store) {
04d68907 528 VERBOSE (WARNING, fprintf (stdout, "error while evaluating (op[1])\n"));
f2927108
LM
529 return 0;
530 }
531 /* fallthrough */
532 case Sqr:
533 case Cos:
534 case Sin:
471da7c9 535 case Atan:
f2927108
LM
536 case Log:
537 case Exp:
6ba1dd0f 538 case Recall:
f2927108 539 if (root->ops[0]) {
3b4b0bbe 540 op0 = evaluate_element (root->ops[0], 0);
f2927108 541 } else {
04d68907 542 VERBOSE (WARNING, fprintf (stdout, "error while evaluating (op[0])\n"));
f2927108
LM
543 return 0;
544 }
89cf0955 545 break;
6ba1dd0f 546 case Disp:
471da7c9
LM
547 case Quit:
548 case Help:
5075f6ea 549 case Ans:
471da7c9
LM
550 case Pi:
551 case E:
89cf0955 552 break;
f2927108
LM
553 }
554
555 switch (root->func) {
3b4b0bbe
LM
556 case Val: return op0;
557 case Sig: return -op0;
558 case Add: return ((mask & MASK_SUB) == 0) ? op0 + op1 : op0 - op1;
559 case Sub: return ((mask & MASK_SUB) == 0) ? op0 - op1 : op0 + op1;
560 case Mul: return ((mask & MASK_DIV) == 0) ? op0 * op1 : op0 / op1;
561 case Div: return ((mask & MASK_DIV) == 0) ? op0 / op1 : op0 * op1;
c47a9298 562 case Mod: return fmod (op0, op1);
f2927108
LM
563 case Pow: return pow (op0, op1);
564 case Sqr: return sqrt (op0);
565 case Cos: return cos (op0);
566 case Sin: return sin (op0);
471da7c9 567 case Atan: return atan (op0);
f2927108
LM
568 case Log: return log (op0);
569 case Exp: return exp (op0);
3639df66 570 case Store: return store ((int)op0, (op1) ? op1 : answer);
6ba1dd0f
LM
571 case Recall: return recall ((int)op0);
572 case Disp: display (); break;
471da7c9
LM
573 case Quit: quit (); break;
574 case Help: help (); break;
5075f6ea 575 case Ans: return answer;
471da7c9
LM
576 case Pi: return M_PI;
577 case E: return M_E;
7fe742c9
LM
578 case Equal: return op0 == op1;
579 case Diff: return op0 != op1;
580 case Ge: return op0 >= op1;
581 case Le: return op0 <= op1;
582 case Gt: return op0 > op1;
583 case Lt: return op0 < op1;
f2927108
LM
584 }
585
586 return 0;
587}
588
bc97a989 589/* vim: set ts=4 sw=4 et: */