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