partial 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
0b489a77
LM
72element_t *parser (char *str, char **next)
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;
99 new = parser (str + 1, &str);
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 }
85b4a72c
LM
119 new = parser (str + 1, &str);
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
LM
144 VERBOSE (DEBUG, PRINTOUT ("start processing operator\n"));
145 str += operator->offset;
11cda8d7 146 if (root) {
49223129 147 VERBOSE (INFO, PRINTOUT ("Oper: %d\n", operator->func));
11cda8d7 148 new = newelement (operator->func, operator->nbops, operator->prio);
bc97a989 149 if (new == NULL) {
0b489a77 150 return ERROR_OP;
bc97a989 151 }
11cda8d7
LM
152 element_t *next = parser (str, &str);
153 if (next == ERROR_OP) {
49223129
LM
154 return ERROR_OP;
155 }
11cda8d7
LM
156 VERBOSE (DEBUG, PRINTOUT ("Priorities: %d > %d -> %s\n", new->prio, next->prio, (new->prio > next->prio) ? "True" : "False"));
157 if (new->prio > next->prio) {
158 VERBOSE (DEBUG, PRINTOUT ("Take account of priority\n"));
159 new->ops[0] = root;
160 new->ops[1] = next->ops[0];
161 next->ops[0] = new;
162 root = next;
163 } else {
164 new->ops[0] = root;
165 new->ops[1] = next;
166 root = new;
167 }
49223129 168 } else {
0b489a77 169 return ERROR_OP;
bc97a989 170 }
bc97a989 171 found = 1;
49223129 172 VERBOSE (DEBUG, PRINTOUT ("stop processing operator\n"));
bc97a989
LM
173 break;
174 }
175 }
176 if (found) {
bc97a989
LM
177 continue;
178 }
49223129 179
bc97a989
LM
180 /* look for functions */
181
182 for (i = 0; i < NB_FUNCTIONS; i++) {
183 keyword_t *function = functions + i;
184 if (codecmp (function->keyword, str) == 0) {
49223129 185 VERBOSE (DEBUG, PRINTOUT ("start processing function\n"));
bc97a989 186 if (root == NULL) {
49223129 187 VERBOSE (INFO, PRINTOUT ("Func: %d\n", function->func));
11cda8d7 188 new = newelement (function->func, function->nbops, function->prio);
bc97a989 189 if (new == NULL) {
0b489a77 190 return ERROR_OP;
bc97a989
LM
191 }
192 root = new;
193 } else {
0b489a77 194 return ERROR_OP;
bc97a989
LM
195 }
196 str += function->offset;
197 found = 1;
49223129 198 VERBOSE (DEBUG, PRINTOUT ("stop processing function\n"));
bc97a989
LM
199 break;
200 }
201 }
202 if (found) {
bc97a989
LM
203 continue;
204 }
205
bc97a989
LM
206 /* look for number */
207
0b9cc9b0
LM
208 if (((*str >= '0') && (*str <= '9')) ||
209 (*str == '.') || (*str == '+') || (*str == '-')) {
49223129 210 VERBOSE (DEBUG, PRINTOUT ("start processing value\n"));
bc97a989
LM
211 char *pt;
212 float value = strtof (str, &pt);
49223129 213 VERBOSE (INFO, PRINTOUT ("Value: %f\n", value));
bc97a989 214 if (str != pt) {
11cda8d7 215 new = newelement (Val, 1, 4);
bc97a989 216 if (new == NULL) {
0b489a77 217 return ERROR_OP;
bc97a989 218 }
49223129 219 new->value = value;
bc97a989
LM
220 if (root == NULL) {
221 root = new;
0b9cc9b0
LM
222 } else if (root->func == Val) {
223 if ((*str == '+') || (*str == '-')) {
11cda8d7 224 element_t *add = newelement (Add, 2, 1);
0b9cc9b0 225 if (add == NULL) {
0b489a77
LM
226 return ERROR_OP;
227 }
0b9cc9b0
LM
228 add->ops[0] = root;
229 add->ops[1] = new;
230 root = add;
231 } else {
0b489a77 232 return ERROR_OP;
bc97a989 233 }
0b9cc9b0
LM
234 } else {
235 return ERROR_OP;
bc97a989
LM
236 }
237 str = pt;
238 found = 1;
239 }
49223129 240 VERBOSE (DEBUG, PRINTOUT ("stop processing value\n"));
bc97a989
LM
241 }
242
243 /* error */
244
245 if (!found) {
0b489a77 246 return ERROR_OP;
bc97a989
LM
247 }
248
249 }
250
49223129
LM
251 if (next != NULL) {
252 *next = str;
253 }
bc97a989
LM
254 return root;
255}
256
257/* print element tree */
258
259void print_element (element_t *root, int level)
260{
261 char *func = NULL;
262 int i;
263
49223129 264 if ((root == NULL) || (root == ERROR_OP)) {
bc97a989 265 return;
49223129
LM
266 }
267
bc97a989
LM
268 for (i = 0; i < level; i++) {
269 PRINTOUT (" ");
270 }
271
272 switch (root->func) {
273 case Val: func = "Value"; break;
0b489a77 274 case Set: func = "Set"; break;
bc97a989
LM
275 case Add: func = "Addition"; break;
276 case Sub: func = "Subtraction"; break;
277 case Mul: func = "Multiplication"; break;
278 case Div: func = "Division"; break;
279 case Pow: func = "Power"; break;
280 case Sqr: func = "Square Root"; break;
281 case Cos: func = "Cosine"; break;
282 case Sin: func = "Sine"; break;
283 case Atn: func = "Arc Tangent"; break;
284 case Log: func = "Logarithm"; break;
285 case Exp: func = "Exponantial"; break;
286 }
287
288 PRINTOUT ("Function: %s\n", func);
289
85b4a72c 290 if ((root->func == Val) && (root->ops[0] == NULL)) {
bc97a989
LM
291 for (i = 0; i < level; i++) {
292 PRINTOUT (" ");
293 }
294 PRINTOUT ("value: %f\n", root->value);
295 } else {
296 for (i = 0; i < root->nbops; i++) {
297 print_element (root->ops[i], level + 1);
298 }
299 }
300}
301
302/* vim: set ts=4 sw=4 et: */