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