new operator modulo and fix operator priority for simple addition
[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
72/* parser function */
73
ef37d966 74element_t *parser (char *str, char **next, int prio)
0b489a77 75{
bc97a989 76 element_t *root = NULL;
85b4a72c 77 int i;
bc97a989 78
49223129
LM
79 VERBOSE (DEBUG, PRINTOUT ("Starting parsing\n"));
80
bc97a989
LM
81 /* main loop */
82 while (*str != '\0') {
83 int found = 0;
84 element_t *new = NULL;
49223129 85 VERBOSE (INFO, PRINTOUT ("Processing: %s\n", str));
bc97a989
LM
86
87 /* skip spaces and tabs */
88
89 if ((*str == ' ') || (*str == '\t')) {
90 str++;
91 continue;
92 }
93
85b4a72c 94 /* check for open bracket */
0b489a77 95
0b489a77 96 if (*str == '(') {
49223129 97 VERBOSE (DEBUG, PRINTOUT ("start processing bracket\n"));
0b489a77 98 if (root) {
85b4a72c
LM
99 do {
100 found = 0;
ef37d966 101 new = parser (str + 1, &str, 0);
85b4a72c
LM
102 if (new == ERROR_OP) {
103 return ERROR_OP;
104 }
105 for (i = 0; i < root->nbops; i++) {
106 if (root->ops[i] == NULL) {
0b489a77
LM
107 root->ops[i] = new;
108 found = 1;
109 break;
110 }
111 }
85b4a72c
LM
112 if (!found) {
113 return ERROR_OP;
114 }
115 } while (*str == ',');
116 } else {
11cda8d7 117 root = newelement (Val, 1, 4);
85b4a72c 118 if (root == NULL) {
0b489a77
LM
119 return ERROR_OP;
120 }
ef37d966 121 new = parser (str + 1, &str, 0);
85b4a72c 122 if ((new == ERROR_OP) || (*str == ',')) {
efdfb543
LM
123 return ERROR_OP;
124 }
85b4a72c 125 root->ops[0] = new;
0b489a77 126 }
85b4a72c 127 str++;
49223129 128 VERBOSE (DEBUG, PRINTOUT ("stop processing bracket\n"));
efdfb543 129 continue;
49223129 130 }
85b4a72c
LM
131
132 /* check for closing bracket or koma */
133
134 if ((*str == ')') || (*str == ',')) {
49223129 135 if (next != NULL) {
85b4a72c 136 *next = str;
49223129
LM
137 }
138 return root;
0b489a77
LM
139 }
140
bc97a989
LM
141 /* look for operators */
142
143 for (i = 0; i < NB_OPERATORS; i++) {
144 keyword_t *operator = operators + i;
145 if (codecmp (operator->keyword, str) == 0) {
49223129 146 VERBOSE (DEBUG, PRINTOUT ("start processing operator\n"));
11cda8d7 147 if (root) {
ef37d966 148 if ((prio) && (prio > operator->prio)) {
c47a9298 149 VERBOSE (DEBUG, PRINTOUT ("stop because operator priority\n"));
ef37d966
LM
150 *next = str;
151 return root;
152 }
153 str += operator->offset;
49223129 154 VERBOSE (INFO, PRINTOUT ("Oper: %d\n", operator->func));
11cda8d7 155 new = newelement (operator->func, operator->nbops, operator->prio);
bc97a989 156 if (new == NULL) {
0b489a77 157 return ERROR_OP;
bc97a989 158 }
ef37d966
LM
159 new->ops[0] = root;
160 new->ops[1] = parser (str, &str, new->prio);
161 if (new->ops[1] == ERROR_OP) {
49223129
LM
162 return ERROR_OP;
163 }
ef37d966
LM
164 root = newelement (Val, 1, 4);
165 if (root == ERROR_OP) {
166 return ERROR_OP;
11cda8d7 167 }
ef37d966 168 root->ops[0] = new;
49223129 169 } else {
0b489a77 170 return ERROR_OP;
bc97a989 171 }
bc97a989 172 found = 1;
49223129 173 VERBOSE (DEBUG, PRINTOUT ("stop processing operator\n"));
bc97a989
LM
174 break;
175 }
176 }
177 if (found) {
bc97a989
LM
178 continue;
179 }
49223129 180
bc97a989
LM
181 /* look for functions */
182
183 for (i = 0; i < NB_FUNCTIONS; i++) {
184 keyword_t *function = functions + i;
185 if (codecmp (function->keyword, str) == 0) {
49223129 186 VERBOSE (DEBUG, PRINTOUT ("start processing function\n"));
bc97a989 187 if (root == NULL) {
49223129 188 VERBOSE (INFO, PRINTOUT ("Func: %d\n", function->func));
11cda8d7 189 new = newelement (function->func, function->nbops, function->prio);
bc97a989 190 if (new == NULL) {
0b489a77 191 return ERROR_OP;
bc97a989
LM
192 }
193 root = new;
194 } else {
0b489a77 195 return ERROR_OP;
bc97a989
LM
196 }
197 str += function->offset;
198 found = 1;
49223129 199 VERBOSE (DEBUG, PRINTOUT ("stop processing function\n"));
bc97a989
LM
200 break;
201 }
202 }
203 if (found) {
bc97a989
LM
204 continue;
205 }
206
bc97a989
LM
207 /* look for number */
208
0b9cc9b0
LM
209 if (((*str >= '0') && (*str <= '9')) ||
210 (*str == '.') || (*str == '+') || (*str == '-')) {
49223129 211 VERBOSE (DEBUG, PRINTOUT ("start processing value\n"));
bc97a989
LM
212 char *pt;
213 float value = strtof (str, &pt);
49223129 214 VERBOSE (INFO, PRINTOUT ("Value: %f\n", value));
bc97a989 215 if (str != pt) {
bc97a989 216 if (root == NULL) {
c47a9298
LM
217 new = newelement (Val, 1, 4);
218 if (new == NULL) {
219 return ERROR_OP;
220 }
221 new->value = value;
bc97a989 222 root = new;
c47a9298 223 str = pt;
0b9cc9b0
LM
224 } else if (root->func == Val) {
225 if ((*str == '+') || (*str == '-')) {
c47a9298
LM
226 if ((prio) && (prio > 1)) {
227 VERBOSE (DEBUG, PRINTOUT ("stop because operator priority\n"));
228 *next = str;
229 return root;
230 }
231 new = newelement (Add, 2, 1);
232 if (new == NULL) {
233 return ERROR_OP;
234 }
235 new->ops[0] = root;
236 new->ops[1] = parser (str, &str, new->prio);
237 if (new->ops[1] == ERROR_OP) {
238 return ERROR_OP;
239 }
240 root = newelement (Val, 1, 4);
241 if (root == ERROR_OP) {
0b489a77
LM
242 return ERROR_OP;
243 }
c47a9298 244 root->ops[0] = new;
0b9cc9b0 245 } else {
0b489a77 246 return ERROR_OP;
bc97a989 247 }
0b9cc9b0
LM
248 } else {
249 return ERROR_OP;
bc97a989 250 }
bc97a989
LM
251 found = 1;
252 }
49223129 253 VERBOSE (DEBUG, PRINTOUT ("stop processing value\n"));
bc97a989
LM
254 }
255
256 /* error */
257
258 if (!found) {
0b489a77 259 return ERROR_OP;
bc97a989
LM
260 }
261
262 }
263
49223129
LM
264 if (next != NULL) {
265 *next = str;
266 }
bc97a989
LM
267 return root;
268}
269
270/* print element tree */
271
272void print_element (element_t *root, int level)
273{
274 char *func = NULL;
275 int i;
276
49223129 277 if ((root == NULL) || (root == ERROR_OP)) {
bc97a989 278 return;
49223129
LM
279 }
280
bc97a989
LM
281 for (i = 0; i < level; i++) {
282 PRINTOUT (" ");
283 }
284
285 switch (root->func) {
286 case Val: func = "Value"; break;
287 case Add: func = "Addition"; break;
288 case Sub: func = "Subtraction"; break;
289 case Mul: func = "Multiplication"; break;
290 case Div: func = "Division"; break;
c47a9298 291 case Mod: func = "Modulo"; break;
bc97a989
LM
292 case Pow: func = "Power"; break;
293 case Sqr: func = "Square Root"; break;
294 case Cos: func = "Cosine"; break;
295 case Sin: func = "Sine"; break;
296 case Atn: func = "Arc Tangent"; break;
297 case Log: func = "Logarithm"; break;
298 case Exp: func = "Exponantial"; break;
299 }
300
301 PRINTOUT ("Function: %s\n", func);
302
85b4a72c 303 if ((root->func == Val) && (root->ops[0] == NULL)) {
bc97a989
LM
304 for (i = 0; i < level; i++) {
305 PRINTOUT (" ");
306 }
307 PRINTOUT ("value: %f\n", root->value);
308 } else {
309 for (i = 0; i < root->nbops; i++) {
310 print_element (root->ops[i], level + 1);
311 }
312 }
313}
314
f2927108
LM
315/* evaluate element tree */
316
317double evaluate_element (element_t *root)
318{
319 double op0 = 0, op1 = 0;
320
321 if ((root == NULL) || (root == ERROR_OP)) {
322 VERBOSE (WARNING, PRINTOUT ("error while evaluating\n"));
323 return 0;
324 }
325
326 switch (root->func) {
327 case Val:
328 if (root->ops[0]) {
329 return evaluate_element (root->ops[0]);
330 } else {
331 return root->value;
332 }
333 break;
334 case Add:
335 case Sub:
336 case Mul:
337 case Div:
c47a9298 338 case Mod:
f2927108
LM
339 case Pow:
340 if (root->ops[1]) {
341 op1 = evaluate_element (root->ops[1]);
342 } else {
343 VERBOSE (WARNING, PRINTOUT ("error while evaluating (op[1])\n"));
344 return 0;
345 }
346 /* fallthrough */
347 case Sqr:
348 case Cos:
349 case Sin:
350 case Atn:
351 case Log:
352 case Exp:
353 if (root->ops[0]) {
354 op0 = evaluate_element (root->ops[0]);
355 } else {
356 VERBOSE (WARNING, PRINTOUT ("error while evaluating (op[0])\n"));
357 return 0;
358 }
359 }
360
361 switch (root->func) {
362 case Add: return op0 + op1;
363 case Sub: return op0 - op1;
364 case Mul: return op0 * op1;
365 case Div: return op0 / op1;
c47a9298 366 case Mod: return fmod (op0, op1);
f2927108
LM
367 case Pow: return pow (op0, op1);
368 case Sqr: return sqrt (op0);
369 case Cos: return cos (op0);
370 case Sin: return sin (op0);
371 case Atn: return atan (op0);
372 case Log: return log (op0);
373 case Exp: return exp (op0);
374 default: break;
375 }
376
377 return 0;
378}
379
bc97a989 380/* vim: set ts=4 sw=4 et: */