efficient priority solution
[calc.git] / parser.c
CommitLineData
bc97a989
LM
1#include <malloc.h>
2#include <stdlib.h>
3
4#include "debug.h"
5#include "fdprintf.h"
6
7#include "parser.h"
8
9/* compare codes */
10
11int codecmp (char *ref, char *str)
12{
13 int sig;
14
15 while (*ref != '\0') {
0b9cc9b0
LM
16 if (*ref == '\t') {
17 sig = (*str == '.') ? -1 : ((*str >= '0') && (*str <= '9'));
18 } else {
19 sig = *str - *ref;
20 }
bc97a989
LM
21 if (sig != 0) {
22 return (sig > 0) ? 1 : -1;
23 }
0b9cc9b0
LM
24 str++;
25 ref++;
bc97a989
LM
26 }
27
28 return 0;
29}
30
31/* allocate new element */
32
11cda8d7 33element_t *newelement (func_t function, int nbops, int prio)
bc97a989
LM
34{
35 element_t *new = (element_t *) calloc (1, sizeof (element_t));
36 if (new == NULL) {
37 VERBOSE (ERROR, fdprintf (stdfderr, "can't allocate memory\n"));
38 return NULL;
39 }
40 new->func = function;
41 new->nbops = nbops;
11cda8d7 42 new->prio = prio;
bc97a989
LM
43
44 return new;
45}
46
47/* functions */
48
0b9cc9b0 49#define NB_OPERATORS 5
bc97a989
LM
50
51keyword_t operators[NB_OPERATORS] = {
11cda8d7
LM
52 { "+\t", Add, 2, 1, 1},
53 { "-\t", Sub, 2, 1, 1},
54 { "*", Mul, 2, 1, 2},
55 { "/", Div, 2, 1, 2},
56 { "^", Pow, 2, 1, 3}
bc97a989
LM
57};
58
59#define NB_FUNCTIONS 7
60keyword_t functions[NB_FUNCTIONS] = {
11cda8d7
LM
61 { "sqrt", Sqr, 1, 4, 4},
62 { "pow", Pow, 2, 3, 4},
63 { "cos", Cos, 1, 3, 4},
64 { "sin", Sin, 1, 3, 4},
65 { "atan", Atn, 1, 4, 4},
66 { "exp", Exp, 1, 3, 4},
67 { "log", Log, 1, 3, 4}
bc97a989
LM
68};
69
70/* parser function */
71
ef37d966 72element_t *parser (char *str, char **next, int prio)
0b489a77 73{
bc97a989 74 element_t *root = NULL;
85b4a72c 75 int i;
bc97a989 76
49223129
LM
77 VERBOSE (DEBUG, PRINTOUT ("Starting parsing\n"));
78
bc97a989
LM
79 /* main loop */
80 while (*str != '\0') {
81 int found = 0;
82 element_t *new = NULL;
49223129 83 VERBOSE (INFO, PRINTOUT ("Processing: %s\n", str));
bc97a989
LM
84
85 /* skip spaces and tabs */
86
87 if ((*str == ' ') || (*str == '\t')) {
88 str++;
89 continue;
90 }
91
85b4a72c 92 /* check for open bracket */
0b489a77 93
0b489a77 94 if (*str == '(') {
49223129 95 VERBOSE (DEBUG, PRINTOUT ("start processing bracket\n"));
0b489a77 96 if (root) {
85b4a72c
LM
97 do {
98 found = 0;
ef37d966 99 new = parser (str + 1, &str, 0);
85b4a72c
LM
100 if (new == ERROR_OP) {
101 return ERROR_OP;
102 }
103 for (i = 0; i < root->nbops; i++) {
104 if (root->ops[i] == NULL) {
0b489a77
LM
105 root->ops[i] = new;
106 found = 1;
107 break;
108 }
109 }
85b4a72c
LM
110 if (!found) {
111 return ERROR_OP;
112 }
113 } while (*str == ',');
114 } else {
11cda8d7 115 root = newelement (Val, 1, 4);
85b4a72c 116 if (root == NULL) {
0b489a77
LM
117 return ERROR_OP;
118 }
ef37d966 119 new = parser (str + 1, &str, 0);
85b4a72c 120 if ((new == ERROR_OP) || (*str == ',')) {
efdfb543
LM
121 return ERROR_OP;
122 }
85b4a72c 123 root->ops[0] = new;
0b489a77 124 }
85b4a72c 125 str++;
49223129 126 VERBOSE (DEBUG, PRINTOUT ("stop processing bracket\n"));
efdfb543 127 continue;
49223129 128 }
85b4a72c
LM
129
130 /* check for closing bracket or koma */
131
132 if ((*str == ')') || (*str == ',')) {
49223129 133 if (next != NULL) {
85b4a72c 134 *next = str;
49223129
LM
135 }
136 return root;
0b489a77
LM
137 }
138
bc97a989
LM
139 /* look for operators */
140
141 for (i = 0; i < NB_OPERATORS; i++) {
142 keyword_t *operator = operators + i;
143 if (codecmp (operator->keyword, str) == 0) {
49223129 144 VERBOSE (DEBUG, PRINTOUT ("start processing operator\n"));
11cda8d7 145 if (root) {
ef37d966
LM
146 if ((prio) && (prio > operator->prio)) {
147 VERBOSE (DEBUG, PRINTOUT ("stop processing operator because operator priority\n"));
148 *next = str;
149 return root;
150 }
151 str += operator->offset;
49223129 152 VERBOSE (INFO, PRINTOUT ("Oper: %d\n", operator->func));
11cda8d7 153 new = newelement (operator->func, operator->nbops, operator->prio);
bc97a989 154 if (new == NULL) {
0b489a77 155 return ERROR_OP;
bc97a989 156 }
ef37d966
LM
157 new->ops[0] = root;
158 new->ops[1] = parser (str, &str, new->prio);
159 if (new->ops[1] == ERROR_OP) {
49223129
LM
160 return ERROR_OP;
161 }
ef37d966
LM
162 root = newelement (Val, 1, 4);
163 if (root == ERROR_OP) {
164 return ERROR_OP;
11cda8d7 165 }
ef37d966 166 root->ops[0] = new;
49223129 167 } else {
0b489a77 168 return ERROR_OP;
bc97a989 169 }
bc97a989 170 found = 1;
49223129 171 VERBOSE (DEBUG, PRINTOUT ("stop processing operator\n"));
bc97a989
LM
172 break;
173 }
174 }
175 if (found) {
bc97a989
LM
176 continue;
177 }
49223129 178
bc97a989
LM
179 /* look for functions */
180
181 for (i = 0; i < NB_FUNCTIONS; i++) {
182 keyword_t *function = functions + i;
183 if (codecmp (function->keyword, str) == 0) {
49223129 184 VERBOSE (DEBUG, PRINTOUT ("start processing function\n"));
bc97a989 185 if (root == NULL) {
49223129 186 VERBOSE (INFO, PRINTOUT ("Func: %d\n", function->func));
11cda8d7 187 new = newelement (function->func, function->nbops, function->prio);
bc97a989 188 if (new == NULL) {
0b489a77 189 return ERROR_OP;
bc97a989
LM
190 }
191 root = new;
192 } else {
0b489a77 193 return ERROR_OP;
bc97a989
LM
194 }
195 str += function->offset;
196 found = 1;
49223129 197 VERBOSE (DEBUG, PRINTOUT ("stop processing function\n"));
bc97a989
LM
198 break;
199 }
200 }
201 if (found) {
bc97a989
LM
202 continue;
203 }
204
bc97a989
LM
205 /* look for number */
206
0b9cc9b0
LM
207 if (((*str >= '0') && (*str <= '9')) ||
208 (*str == '.') || (*str == '+') || (*str == '-')) {
49223129 209 VERBOSE (DEBUG, PRINTOUT ("start processing value\n"));
bc97a989
LM
210 char *pt;
211 float value = strtof (str, &pt);
49223129 212 VERBOSE (INFO, PRINTOUT ("Value: %f\n", value));
bc97a989 213 if (str != pt) {
11cda8d7 214 new = newelement (Val, 1, 4);
bc97a989 215 if (new == NULL) {
0b489a77 216 return ERROR_OP;
bc97a989 217 }
49223129 218 new->value = value;
bc97a989
LM
219 if (root == NULL) {
220 root = new;
0b9cc9b0
LM
221 } else if (root->func == Val) {
222 if ((*str == '+') || (*str == '-')) {
11cda8d7 223 element_t *add = newelement (Add, 2, 1);
0b9cc9b0 224 if (add == NULL) {
0b489a77
LM
225 return ERROR_OP;
226 }
0b9cc9b0
LM
227 add->ops[0] = root;
228 add->ops[1] = new;
229 root = add;
230 } else {
0b489a77 231 return ERROR_OP;
bc97a989 232 }
0b9cc9b0
LM
233 } else {
234 return ERROR_OP;
bc97a989
LM
235 }
236 str = pt;
237 found = 1;
238 }
49223129 239 VERBOSE (DEBUG, PRINTOUT ("stop processing value\n"));
bc97a989
LM
240 }
241
242 /* error */
243
244 if (!found) {
0b489a77 245 return ERROR_OP;
bc97a989
LM
246 }
247
248 }
249
49223129
LM
250 if (next != NULL) {
251 *next = str;
252 }
bc97a989
LM
253 return root;
254}
255
256/* print element tree */
257
258void print_element (element_t *root, int level)
259{
260 char *func = NULL;
261 int i;
262
49223129 263 if ((root == NULL) || (root == ERROR_OP)) {
bc97a989 264 return;
49223129
LM
265 }
266
bc97a989
LM
267 for (i = 0; i < level; i++) {
268 PRINTOUT (" ");
269 }
270
271 switch (root->func) {
272 case Val: func = "Value"; break;
0b489a77 273 case Set: func = "Set"; break;
bc97a989
LM
274 case Add: func = "Addition"; break;
275 case Sub: func = "Subtraction"; break;
276 case Mul: func = "Multiplication"; break;
277 case Div: func = "Division"; break;
278 case Pow: func = "Power"; break;
279 case Sqr: func = "Square Root"; break;
280 case Cos: func = "Cosine"; break;
281 case Sin: func = "Sine"; break;
282 case Atn: func = "Arc Tangent"; break;
283 case Log: func = "Logarithm"; break;
284 case Exp: func = "Exponantial"; break;
285 }
286
287 PRINTOUT ("Function: %s\n", func);
288
85b4a72c 289 if ((root->func == Val) && (root->ops[0] == NULL)) {
bc97a989
LM
290 for (i = 0; i < level; i++) {
291 PRINTOUT (" ");
292 }
293 PRINTOUT ("value: %f\n", root->value);
294 } else {
295 for (i = 0; i < root->nbops; i++) {
296 print_element (root->ops[i], level + 1);
297 }
298 }
299}
300
301/* vim: set ts=4 sw=4 et: */