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