correct some tests
[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
bc97a989
LM
14/* compare codes */
15
16int codecmp (char *ref, char *str)
17{
18 int sig;
19
20 while (*ref != '\0') {
0b9cc9b0
LM
21 if (*ref == '\t') {
22 sig = (*str == '.') ? -1 : ((*str >= '0') && (*str <= '9'));
23 } else {
24 sig = *str - *ref;
25 }
bc97a989
LM
26 if (sig != 0) {
27 return (sig > 0) ? 1 : -1;
28 }
0b9cc9b0
LM
29 str++;
30 ref++;
bc97a989
LM
31 }
32
33 return 0;
34}
35
36/* allocate new element */
37
11cda8d7 38element_t *newelement (func_t function, int nbops, int prio)
bc97a989
LM
39{
40 element_t *new = (element_t *) calloc (1, sizeof (element_t));
41 if (new == NULL) {
04d68907 42 VERBOSE (ERROR, fprintf (stderr, "can't allocate memory\n"));
bc97a989
LM
43 return NULL;
44 }
45 new->func = function;
46 new->nbops = nbops;
11cda8d7 47 new->prio = prio;
bc97a989
LM
48
49 return new;
50}
51
031d7bba
LM
52/* desallocate element */
53
54void delelement (element_t *root)
55{
56 int i;
57 if ((root != NULL) && (root != ERROR_OP)) {
58 for (i = 0; i < root->nbops; i++) {
59 if ((root->ops[i] != NULL) && (root->ops[i] != ERROR_OP)) {
60 delelement (root->ops[i]);
61 }
62 }
63 free (root);
64 }
65}
66
bc97a989
LM
67/* functions */
68
c47a9298 69#define NB_OPERATORS 6
bc97a989
LM
70
71keyword_t operators[NB_OPERATORS] = {
11cda8d7
LM
72 { "+\t", Add, 2, 1, 1},
73 { "-\t", Sub, 2, 1, 1},
74 { "*", Mul, 2, 1, 2},
75 { "/", Div, 2, 1, 2},
c47a9298
LM
76 { "%", Mod, 2, 1, 3},
77 { "^", Pow, 2, 1, 4}
bc97a989
LM
78};
79
89cf0955 80#define NB_FUNCTIONS 9
bc97a989 81keyword_t functions[NB_FUNCTIONS] = {
c47a9298
LM
82 { "sqrt", Sqr, 1, 4, 5},
83 { "pow", Pow, 2, 3, 5},
84 { "cos", Cos, 1, 3, 5},
85 { "sin", Sin, 1, 3, 5},
471da7c9 86 { "atan", Atan, 1, 4, 5},
c47a9298 87 { "exp", Exp, 1, 3, 5},
89cf0955 88 { "log", Log, 1, 3, 5},
471da7c9
LM
89 { "quit", Quit, 0, 4, 5},
90 { "help", Help, 0, 4, 5}
91};
92
5075f6ea 93#define NB_CONSTANTS 3
471da7c9 94keyword_t constants[NB_CONSTANTS] = {
5075f6ea
LM
95 { "ans", Ans, 0, 3, 5},
96 { "e", E, 0, 1, 5},
97 { "pi", Pi, 0, 2, 5}
bc97a989
LM
98};
99
d2ff8478
LM
100/* subparser function */
101
102element_t *subparser (element_t **proot, char **pstr, func_t func, int nbops, int prio)
103{
104 element_t *new = newelement (func, nbops, prio);
105 if (new == NULL) {
106 return ERROR_OP;
107 }
108 new->ops[0] = *proot;
109 new->ops[1] = parser (*pstr, pstr, new->prio);
110 if (new->ops[1] == ERROR_OP) {
031d7bba
LM
111 delelement (new);
112 *proot = NULL;
d2ff8478
LM
113 return ERROR_OP;
114 }
3b4b0bbe 115 *proot = newelement (Val, 1, 5);
031d7bba
LM
116 if (*proot == NULL) {
117 delelement (new);
d2ff8478
LM
118 return ERROR_OP;
119 }
120 (*proot)->ops[0] = new;
121
122 return *proot;
123}
124
bc97a989
LM
125/* parser function */
126
ef37d966 127element_t *parser (char *str, char **next, int prio)
0b489a77 128{
bc97a989 129 element_t *root = NULL;
85b4a72c 130 int i;
bc97a989 131
04d68907 132 VERBOSE (DEBUG, fprintf (stdout, "Starting parsing\n"));
49223129 133
bc97a989
LM
134 /* main loop */
135 while (*str != '\0') {
136 int found = 0;
137 element_t *new = NULL;
04d68907 138 VERBOSE (INFO, fprintf (stdout, "Processing: %s\n", str));
bc97a989
LM
139
140 /* skip spaces and tabs */
141
142 if ((*str == ' ') || (*str == '\t')) {
143 str++;
144 continue;
145 }
146
85b4a72c 147 /* check for open bracket */
0b489a77 148
0b489a77 149 if (*str == '(') {
04d68907 150 VERBOSE (DEBUG, fprintf (stdout, "start processing bracket\n"));
0b489a77 151 if (root) {
85b4a72c
LM
152 do {
153 found = 0;
ef37d966 154 new = parser (str + 1, &str, 0);
85b4a72c 155 if (new == ERROR_OP) {
031d7bba 156 delelement (root);
85b4a72c
LM
157 return ERROR_OP;
158 }
159 for (i = 0; i < root->nbops; i++) {
160 if (root->ops[i] == NULL) {
0b489a77
LM
161 root->ops[i] = new;
162 found = 1;
163 break;
164 }
165 }
85b4a72c 166 if (!found) {
031d7bba
LM
167 delelement (new);
168 delelement (root);
85b4a72c
LM
169 return ERROR_OP;
170 }
171 } while (*str == ',');
172 } else {
3b4b0bbe 173 root = newelement (Val, 1, 5);
85b4a72c 174 if (root == NULL) {
0b489a77
LM
175 return ERROR_OP;
176 }
ef37d966 177 new = parser (str + 1, &str, 0);
85b4a72c 178 if ((new == ERROR_OP) || (*str == ',')) {
031d7bba
LM
179 delelement (new);
180 delelement (root);
efdfb543
LM
181 return ERROR_OP;
182 }
85b4a72c 183 root->ops[0] = new;
0b489a77 184 }
85b4a72c 185 str++;
04d68907 186 VERBOSE (DEBUG, fprintf (stdout, "stop processing bracket\n"));
efdfb543 187 continue;
49223129 188 }
85b4a72c
LM
189
190 /* check for closing bracket or koma */
191
192 if ((*str == ')') || (*str == ',')) {
49223129 193 if (next != NULL) {
85b4a72c 194 *next = str;
49223129
LM
195 }
196 return root;
0b489a77
LM
197 }
198
bc97a989
LM
199 /* look for operators */
200
201 for (i = 0; i < NB_OPERATORS; i++) {
202 keyword_t *operator = operators + i;
203 if (codecmp (operator->keyword, str) == 0) {
04d68907 204 VERBOSE (DEBUG, fprintf (stdout, "start processing operator\n"));
11cda8d7 205 if (root) {
ef37d966 206 if ((prio) && (prio > operator->prio)) {
04d68907 207 VERBOSE (DEBUG, fprintf (stdout, "stop because operator priority\n"));
ef37d966
LM
208 *next = str;
209 return root;
210 }
211 str += operator->offset;
04d68907 212 VERBOSE (INFO, fprintf (stdout, "Oper: %d\n", operator->func));
d2ff8478 213 if (subparser (&root, &str, operator->func, operator->nbops, operator->prio) == ERROR_OP) {
031d7bba 214 delelement (root);
ef37d966 215 return ERROR_OP;
11cda8d7 216 }
3b4b0bbe
LM
217 } else if (*str == '-') {
218 new = newelement (Sig, 1, 9);
219 if (new == NULL) {
220 return ERROR_OP;
221 }
222 root = new;
49223129 223 } else {
0b489a77 224 return ERROR_OP;
bc97a989 225 }
bc97a989 226 found = 1;
04d68907 227 VERBOSE (DEBUG, fprintf (stdout, "stop processing operator\n"));
bc97a989
LM
228 break;
229 }
230 }
231 if (found) {
bc97a989
LM
232 continue;
233 }
49223129 234
bc97a989
LM
235 /* look for functions */
236
237 for (i = 0; i < NB_FUNCTIONS; i++) {
238 keyword_t *function = functions + i;
239 if (codecmp (function->keyword, str) == 0) {
04d68907 240 VERBOSE (DEBUG, fprintf (stdout, "start processing function\n"));
bc97a989 241 if (root == NULL) {
04d68907 242 VERBOSE (INFO, fprintf (stdout, "Func: %d\n", function->func));
11cda8d7 243 new = newelement (function->func, function->nbops, function->prio);
bc97a989 244 if (new == NULL) {
0b489a77 245 return ERROR_OP;
bc97a989
LM
246 }
247 root = new;
031d7bba
LM
248 } else {
249 delelement (root);
0b489a77 250 return ERROR_OP;
bc97a989
LM
251 }
252 str += function->offset;
253 found = 1;
04d68907 254 VERBOSE (DEBUG, fprintf (stdout, "stop processing function\n"));
bc97a989
LM
255 break;
256 }
257 }
258 if (found) {
bc97a989
LM
259 continue;
260 }
261
471da7c9
LM
262 /* look for constant */
263
264 for (i = 0; i < NB_CONSTANTS; i++) {
265 keyword_t *constant = constants + i;
266 if (codecmp (constant->keyword, str) == 0) {
267 VERBOSE (DEBUG, fprintf (stdout, "start processing constant\n"));
268 if (root == NULL) {
5075f6ea 269 VERBOSE (INFO, fprintf (stdout, "Const: %d\n", constant->func));
471da7c9
LM
270 new = newelement (constant->func, constant->nbops, constant->prio);
271 if (new == NULL) {
272 return ERROR_OP;
273 }
274 root = new;
275 } else {
276 delelement (root);
277 return ERROR_OP;
278 }
279 str += constant->offset;
280 found = 1;
281 VERBOSE (DEBUG, fprintf (stdout, "stop processing constant\n"));
282 break;
283 }
284 }
285 if (found) {
286 continue;
287 }
288
bc97a989
LM
289 /* look for number */
290
0b9cc9b0
LM
291 if (((*str >= '0') && (*str <= '9')) ||
292 (*str == '.') || (*str == '+') || (*str == '-')) {
04d68907 293 VERBOSE (DEBUG, fprintf (stdout, "start processing value\n"));
bc97a989 294 char *pt;
fd88e359 295 double value = strtod (str, &pt);
04d68907 296 VERBOSE (INFO, fprintf (stdout, "Value: %f\n", value));
bc97a989 297 if (str != pt) {
bc97a989 298 if (root == NULL) {
3b4b0bbe 299 new = newelement (Val, 1, 5);
c47a9298
LM
300 if (new == NULL) {
301 return ERROR_OP;
302 }
303 new->value = value;
bc97a989 304 root = new;
c47a9298 305 str = pt;
32741902
LM
306 } else if ((*str == '+') || (*str == '-')) {
307 if ((prio) && (prio > 1)) {
308 VERBOSE (DEBUG, fprintf (stdout, "stop because operator priority\n"));
309 *next = str;
310 return root;
311 }
312 if (subparser (&root, &str, Add, 2, 1) == ERROR_OP) {
031d7bba 313 delelement (root);
0b489a77 314 return ERROR_OP;
bc97a989 315 }
0b9cc9b0 316 } else {
031d7bba 317 delelement (root);
0b9cc9b0 318 return ERROR_OP;
bc97a989 319 }
bc97a989
LM
320 found = 1;
321 }
04d68907 322 VERBOSE (DEBUG, fprintf (stdout, "stop processing value\n"));
bc97a989
LM
323 }
324
325 /* error */
326
327 if (!found) {
031d7bba 328 delelement (root);
0b489a77 329 return ERROR_OP;
bc97a989
LM
330 }
331
332 }
333
49223129
LM
334 if (next != NULL) {
335 *next = str;
336 }
031d7bba 337
bc97a989
LM
338 return root;
339}
340
341/* print element tree */
342
343void print_element (element_t *root, int level)
344{
345 char *func = NULL;
346 int i;
347
49223129 348 if ((root == NULL) || (root == ERROR_OP)) {
bc97a989 349 return;
49223129
LM
350 }
351
bc97a989 352 for (i = 0; i < level; i++) {
04d68907 353 fprintf (stdout, " ");
bc97a989
LM
354 }
355
356 switch (root->func) {
357 case Val: func = "Value"; break;
89cf0955 358 case Sig: func = "Sign"; break;
bc97a989
LM
359 case Add: func = "Addition"; break;
360 case Sub: func = "Subtraction"; break;
361 case Mul: func = "Multiplication"; break;
362 case Div: func = "Division"; break;
c47a9298 363 case Mod: func = "Modulo"; break;
bc97a989
LM
364 case Pow: func = "Power"; break;
365 case Sqr: func = "Square Root"; break;
366 case Cos: func = "Cosine"; break;
367 case Sin: func = "Sine"; break;
471da7c9 368 case Atan: func = "Arc Tangent"; break;
bc97a989
LM
369 case Log: func = "Logarithm"; break;
370 case Exp: func = "Exponantial"; break;
471da7c9
LM
371 case Quit: func = "Quit"; break;
372 case Help: func = "Help"; break;
5075f6ea 373 case Ans: func = "Ans"; break;
471da7c9
LM
374 case Pi: func = "Pi"; break;
375 case E: func = "E"; break;
bc97a989
LM
376 }
377
04d68907 378 fprintf (stdout, "Function: %s\n", func);
bc97a989 379
85b4a72c 380 if ((root->func == Val) && (root->ops[0] == NULL)) {
bc97a989 381 for (i = 0; i < level; i++) {
04d68907 382 fprintf (stdout, " ");
bc97a989 383 }
04d68907 384 fprintf (stdout, "value: %f\n", root->value);
bc97a989
LM
385 } else {
386 for (i = 0; i < root->nbops; i++) {
387 print_element (root->ops[i], level + 1);
388 }
389 }
390}
391
89cf0955
LM
392/* quit function */
393
394void quit (void)
395{
04d68907 396 fprintf (stdout, "bye\n");
89cf0955
LM
397 exit (0);
398}
399
400/* help message */
401
402void help (void)
403{
04d68907
LM
404 fprintf (stdout, "calc is a simple calculator\n\n");
405 fprintf (stdout, "supported operators:\n");
406 fprintf (stdout, " + - * / %% ^\n\n");
407 fprintf (stdout, "supported functions:\n");
408 fprintf (stdout, " pow sqrt cos sin atan log exp\n\n");
409 fprintf (stdout, "miscellaneous functions:\n");
471da7c9
LM
410 fprintf (stdout, " quit help\n\n");
411 fprintf (stdout, "supported constants:\n");
412 fprintf (stdout, " e pi\n");
89cf0955 413}
3b4b0bbe 414
f2927108
LM
415/* evaluate element tree */
416
89cf0955
LM
417#define MASK_SUB 0x1
418#define MASK_DIV 0x2
419
3b4b0bbe 420double evaluate_element (element_t *root, char mask)
f2927108
LM
421{
422 double op0 = 0, op1 = 0;
3b4b0bbe 423 char nextmask = mask;
f2927108
LM
424
425 if ((root == NULL) || (root == ERROR_OP)) {
04d68907 426 VERBOSE (WARNING, fprintf (stdout, "error while evaluating\n"));
f2927108
LM
427 return 0;
428 }
429
3b4b0bbe
LM
430 /* mask to manage sub operator sub and div */
431 switch (root->func) {
432 case Add:
433 nextmask &= ~MASK_SUB;
434 nextmask &= ~MASK_DIV;
435 break;
436 case Sub:
437 nextmask |= MASK_SUB;
438 nextmask &= ~MASK_DIV;
439 break;
440 case Mul:
441 nextmask &= ~MASK_DIV;
442 break;
443 case Div:
444 nextmask |= MASK_DIV;
445 break;
446 default:
447 nextmask = mask;
448 }
449
f2927108
LM
450 switch (root->func) {
451 case Val:
3b4b0bbe
LM
452 case Sig:
453 op0 = (root->ops[0]) ? evaluate_element (root->ops[0], nextmask) : root->value;
f2927108
LM
454 break;
455 case Add:
456 case Sub:
457 case Mul:
458 case Div:
c47a9298 459 case Mod:
f2927108
LM
460 case Pow:
461 if (root->ops[1]) {
3b4b0bbe 462 op1 = evaluate_element (root->ops[1], nextmask);
f2927108 463 } else {
04d68907 464 VERBOSE (WARNING, fprintf (stdout, "error while evaluating (op[1])\n"));
f2927108
LM
465 return 0;
466 }
467 /* fallthrough */
468 case Sqr:
469 case Cos:
470 case Sin:
471da7c9 471 case Atan:
f2927108
LM
472 case Log:
473 case Exp:
474 if (root->ops[0]) {
3b4b0bbe 475 op0 = evaluate_element (root->ops[0], 0);
f2927108 476 } else {
04d68907 477 VERBOSE (WARNING, fprintf (stdout, "error while evaluating (op[0])\n"));
f2927108
LM
478 return 0;
479 }
89cf0955 480 break;
471da7c9
LM
481 case Quit:
482 case Help:
5075f6ea 483 case Ans:
471da7c9
LM
484 case Pi:
485 case E:
89cf0955 486 break;
f2927108
LM
487 }
488
489 switch (root->func) {
3b4b0bbe
LM
490 case Val: return op0;
491 case Sig: return -op0;
492 case Add: return ((mask & MASK_SUB) == 0) ? op0 + op1 : op0 - op1;
493 case Sub: return ((mask & MASK_SUB) == 0) ? op0 - op1 : op0 + op1;
494 case Mul: return ((mask & MASK_DIV) == 0) ? op0 * op1 : op0 / op1;
495 case Div: return ((mask & MASK_DIV) == 0) ? op0 / op1 : op0 * op1;
c47a9298 496 case Mod: return fmod (op0, op1);
f2927108
LM
497 case Pow: return pow (op0, op1);
498 case Sqr: return sqrt (op0);
499 case Cos: return cos (op0);
500 case Sin: return sin (op0);
471da7c9 501 case Atan: return atan (op0);
f2927108
LM
502 case Log: return log (op0);
503 case Exp: return exp (op0);
471da7c9
LM
504 case Quit: quit (); break;
505 case Help: help (); break;
5075f6ea 506 case Ans: return answer;
471da7c9
LM
507 case Pi: return M_PI;
508 case E: return M_E;
f2927108
LM
509 }
510
511 return 0;
512}
513
bc97a989 514/* vim: set ts=4 sw=4 et: */