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