fdabef2bf15e6c3848c87824b6a55b44fbc18169
[calc.git] / parser.c
1 #include <malloc.h>
2 #include <math.h>
3 #include <stdlib.h>
4
5 #include "debug.h"
6 #include "fdprintf.h"
7
8 #include "parser.h"
9
10 /* compare codes */
11
12 int codecmp (char *ref, char *str)
13 {
14 int sig;
15
16 while (*ref != '\0') {
17 if (*ref == '\t') {
18 sig = (*str == '.') ? -1 : ((*str >= '0') && (*str <= '9'));
19 } else {
20 sig = *str - *ref;
21 }
22 if (sig != 0) {
23 return (sig > 0) ? 1 : -1;
24 }
25 str++;
26 ref++;
27 }
28
29 return 0;
30 }
31
32 /* allocate new element */
33
34 element_t *newelement (func_t function, int nbops, int prio)
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;
43 new->prio = prio;
44
45 return new;
46 }
47
48 /* functions */
49
50 #define NB_OPERATORS 6
51
52 keyword_t operators[NB_OPERATORS] = {
53 { "+\t", Add, 2, 1, 1},
54 { "-\t", Sub, 2, 1, 1},
55 { "*", Mul, 2, 1, 2},
56 { "/", Div, 2, 1, 2},
57 { "%", Mod, 2, 1, 3},
58 { "^", Pow, 2, 1, 4}
59 };
60
61 #define NB_FUNCTIONS 7
62 keyword_t functions[NB_FUNCTIONS] = {
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}
70 };
71
72 /* subparser function */
73
74 element_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
94 /* parser function */
95
96 element_t *parser (char *str, char **next, int prio)
97 {
98 element_t *root = NULL;
99 int i;
100
101 VERBOSE (DEBUG, PRINTOUT ("Starting parsing\n"));
102
103 /* main loop */
104 while (*str != '\0') {
105 int found = 0;
106 element_t *new = NULL;
107 VERBOSE (INFO, PRINTOUT ("Processing: %s\n", str));
108
109 /* skip spaces and tabs */
110
111 if ((*str == ' ') || (*str == '\t')) {
112 str++;
113 continue;
114 }
115
116 /* check for open bracket */
117
118 if (*str == '(') {
119 VERBOSE (DEBUG, PRINTOUT ("start processing bracket\n"));
120 if (root) {
121 do {
122 found = 0;
123 new = parser (str + 1, &str, 0);
124 if (new == ERROR_OP) {
125 return ERROR_OP;
126 }
127 for (i = 0; i < root->nbops; i++) {
128 if (root->ops[i] == NULL) {
129 root->ops[i] = new;
130 found = 1;
131 break;
132 }
133 }
134 if (!found) {
135 return ERROR_OP;
136 }
137 } while (*str == ',');
138 } else {
139 root = newelement (Val, 1, 4);
140 if (root == NULL) {
141 return ERROR_OP;
142 }
143 new = parser (str + 1, &str, 0);
144 if ((new == ERROR_OP) || (*str == ',')) {
145 return ERROR_OP;
146 }
147 root->ops[0] = new;
148 }
149 str++;
150 VERBOSE (DEBUG, PRINTOUT ("stop processing bracket\n"));
151 continue;
152 }
153
154 /* check for closing bracket or koma */
155
156 if ((*str == ')') || (*str == ',')) {
157 if (next != NULL) {
158 *next = str;
159 }
160 return root;
161 }
162
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) {
168 VERBOSE (DEBUG, PRINTOUT ("start processing operator\n"));
169 if (root) {
170 if ((prio) && (prio > operator->prio)) {
171 VERBOSE (DEBUG, PRINTOUT ("stop because operator priority\n"));
172 *next = str;
173 return root;
174 }
175 str += operator->offset;
176 VERBOSE (INFO, PRINTOUT ("Oper: %d\n", operator->func));
177 if (subparser (&root, &str, operator->func, operator->nbops, operator->prio) == ERROR_OP) {
178 return ERROR_OP;
179 }
180 } else {
181 return ERROR_OP;
182 }
183 found = 1;
184 VERBOSE (DEBUG, PRINTOUT ("stop processing operator\n"));
185 break;
186 }
187 }
188 if (found) {
189 continue;
190 }
191
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) {
197 VERBOSE (DEBUG, PRINTOUT ("start processing function\n"));
198 if (root == NULL) {
199 VERBOSE (INFO, PRINTOUT ("Func: %d\n", function->func));
200 new = newelement (function->func, function->nbops, function->prio);
201 if (new == NULL) {
202 return ERROR_OP;
203 }
204 root = new;
205 } else {
206 return ERROR_OP;
207 }
208 str += function->offset;
209 found = 1;
210 VERBOSE (DEBUG, PRINTOUT ("stop processing function\n"));
211 break;
212 }
213 }
214 if (found) {
215 continue;
216 }
217
218 /* look for number */
219
220 if (((*str >= '0') && (*str <= '9')) ||
221 (*str == '.') || (*str == '+') || (*str == '-')) {
222 VERBOSE (DEBUG, PRINTOUT ("start processing value\n"));
223 char *pt;
224 float value = strtof (str, &pt);
225 VERBOSE (INFO, PRINTOUT ("Value: %f\n", value));
226 if (str != pt) {
227 if (root == NULL) {
228 new = newelement (Val, 1, 4);
229 if (new == NULL) {
230 return ERROR_OP;
231 }
232 new->value = value;
233 root = new;
234 str = pt;
235 } else if (root->func == Val) {
236 if ((*str == '+') || (*str == '-')) {
237 if ((prio) && (prio > 1)) {
238 VERBOSE (DEBUG, PRINTOUT ("stop because operator priority\n"));
239 *next = str;
240 return root;
241 }
242 if (subparser (&root, &str, Add, 2, 1) == ERROR_OP) {
243 return ERROR_OP;
244 }
245 } else {
246 return ERROR_OP;
247 }
248 } else {
249 return ERROR_OP;
250 }
251 found = 1;
252 }
253 VERBOSE (DEBUG, PRINTOUT ("stop processing value\n"));
254 }
255
256 /* error */
257
258 if (!found) {
259 return ERROR_OP;
260 }
261
262 }
263
264 if (next != NULL) {
265 *next = str;
266 }
267 return root;
268 }
269
270 /* print element tree */
271
272 void print_element (element_t *root, int level)
273 {
274 char *func = NULL;
275 int i;
276
277 if ((root == NULL) || (root == ERROR_OP)) {
278 return;
279 }
280
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;
291 case Mod: func = "Modulo"; break;
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
303 if ((root->func == Val) && (root->ops[0] == NULL)) {
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
315 /* evaluate element tree */
316
317 double 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:
338 case Mod:
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;
366 case Mod: return fmod (op0, op1);
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
380 /* vim: set ts=4 sw=4 et: */