add covering rule
[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 }
85 *proot = newelement (Val, 1, 4);
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 {
11cda8d7 139 root = newelement (Val, 1, 4);
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 }
49223129 180 } else {
0b489a77 181 return ERROR_OP;
bc97a989 182 }
bc97a989 183 found = 1;
49223129 184 VERBOSE (DEBUG, PRINTOUT ("stop processing operator\n"));
bc97a989
LM
185 break;
186 }
187 }
188 if (found) {
bc97a989
LM
189 continue;
190 }
49223129 191
bc97a989
LM
192 /* look for functions */
193
194 for (i = 0; i < NB_FUNCTIONS; i++) {
195 keyword_t *function = functions + i;
196 if (codecmp (function->keyword, str) == 0) {
49223129 197 VERBOSE (DEBUG, PRINTOUT ("start processing function\n"));
bc97a989 198 if (root == NULL) {
49223129 199 VERBOSE (INFO, PRINTOUT ("Func: %d\n", function->func));
11cda8d7 200 new = newelement (function->func, function->nbops, function->prio);
bc97a989 201 if (new == NULL) {
0b489a77 202 return ERROR_OP;
bc97a989
LM
203 }
204 root = new;
205 } else {
0b489a77 206 return ERROR_OP;
bc97a989
LM
207 }
208 str += function->offset;
209 found = 1;
49223129 210 VERBOSE (DEBUG, PRINTOUT ("stop processing function\n"));
bc97a989
LM
211 break;
212 }
213 }
214 if (found) {
bc97a989
LM
215 continue;
216 }
217
bc97a989
LM
218 /* look for number */
219
0b9cc9b0
LM
220 if (((*str >= '0') && (*str <= '9')) ||
221 (*str == '.') || (*str == '+') || (*str == '-')) {
49223129 222 VERBOSE (DEBUG, PRINTOUT ("start processing value\n"));
bc97a989
LM
223 char *pt;
224 float value = strtof (str, &pt);
49223129 225 VERBOSE (INFO, PRINTOUT ("Value: %f\n", value));
bc97a989 226 if (str != pt) {
bc97a989 227 if (root == NULL) {
c47a9298
LM
228 new = newelement (Val, 1, 4);
229 if (new == NULL) {
230 return ERROR_OP;
231 }
232 new->value = value;
bc97a989 233 root = new;
c47a9298 234 str = pt;
0b9cc9b0
LM
235 } else if (root->func == Val) {
236 if ((*str == '+') || (*str == '-')) {
c47a9298
LM
237 if ((prio) && (prio > 1)) {
238 VERBOSE (DEBUG, PRINTOUT ("stop because operator priority\n"));
239 *next = str;
240 return root;
241 }
d2ff8478 242 if (subparser (&root, &str, Add, 2, 1) == ERROR_OP) {
0b489a77
LM
243 return ERROR_OP;
244 }
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: */