0391246e5e6b9334e9b545d7b6e44a91baebf9ac
[calc.git] / parser.c
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
11 int codecmp (char *ref, char *str)
12 {
13 int sig;
14
15 while (*ref != '\0') {
16 if (*ref == '\t') {
17 sig = (*str == '.') ? -1 : ((*str >= '0') && (*str <= '9'));
18 } else {
19 sig = *str - *ref;
20 }
21 if (sig != 0) {
22 return (sig > 0) ? 1 : -1;
23 }
24 str++;
25 ref++;
26 }
27
28 return 0;
29 }
30
31 /* allocate new element */
32
33 element_t *newelement (func_t function, int nbops, int prio)
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;
42 new->prio = prio;
43
44 return new;
45 }
46
47 /* functions */
48
49 #define NB_OPERATORS 5
50
51 keyword_t operators[NB_OPERATORS] = {
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}
57 };
58
59 #define NB_FUNCTIONS 7
60 keyword_t functions[NB_FUNCTIONS] = {
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}
68 };
69
70 /* parser function */
71
72 element_t *parser (char *str, char **next)
73 {
74 element_t *root = NULL;
75 int i;
76
77 VERBOSE (DEBUG, PRINTOUT ("Starting parsing\n"));
78
79 /* main loop */
80 while (*str != '\0') {
81 int found = 0;
82 element_t *new = NULL;
83 VERBOSE (INFO, PRINTOUT ("Processing: %s\n", str));
84
85 /* skip spaces and tabs */
86
87 if ((*str == ' ') || (*str == '\t')) {
88 str++;
89 continue;
90 }
91
92 /* check for open bracket */
93
94 if (*str == '(') {
95 VERBOSE (DEBUG, PRINTOUT ("start processing bracket\n"));
96 if (root) {
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) {
105 root->ops[i] = new;
106 found = 1;
107 break;
108 }
109 }
110 if (!found) {
111 return ERROR_OP;
112 }
113 } while (*str == ',');
114 } else {
115 root = newelement (Val, 1, 4);
116 if (root == NULL) {
117 return ERROR_OP;
118 }
119 new = parser (str + 1, &str);
120 if ((new == ERROR_OP) || (*str == ',')) {
121 return ERROR_OP;
122 }
123 root->ops[0] = new;
124 }
125 str++;
126 VERBOSE (DEBUG, PRINTOUT ("stop processing bracket\n"));
127 continue;
128 }
129
130 /* check for closing bracket or koma */
131
132 if ((*str == ')') || (*str == ',')) {
133 if (next != NULL) {
134 *next = str;
135 }
136 return root;
137 }
138
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) {
144 VERBOSE (DEBUG, PRINTOUT ("start processing operator\n"));
145 str += operator->offset;
146 if (root) {
147 VERBOSE (INFO, PRINTOUT ("Oper: %d\n", operator->func));
148 new = newelement (operator->func, operator->nbops, operator->prio);
149 if (new == NULL) {
150 return ERROR_OP;
151 }
152 element_t *next = parser (str, &str);
153 if (next == ERROR_OP) {
154 return ERROR_OP;
155 }
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 }
168 } else {
169 return ERROR_OP;
170 }
171 found = 1;
172 VERBOSE (DEBUG, PRINTOUT ("stop processing operator\n"));
173 break;
174 }
175 }
176 if (found) {
177 continue;
178 }
179
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) {
185 VERBOSE (DEBUG, PRINTOUT ("start processing function\n"));
186 if (root == NULL) {
187 VERBOSE (INFO, PRINTOUT ("Func: %d\n", function->func));
188 new = newelement (function->func, function->nbops, function->prio);
189 if (new == NULL) {
190 return ERROR_OP;
191 }
192 root = new;
193 } else {
194 return ERROR_OP;
195 }
196 str += function->offset;
197 found = 1;
198 VERBOSE (DEBUG, PRINTOUT ("stop processing function\n"));
199 break;
200 }
201 }
202 if (found) {
203 continue;
204 }
205
206 /* look for number */
207
208 if (((*str >= '0') && (*str <= '9')) ||
209 (*str == '.') || (*str == '+') || (*str == '-')) {
210 VERBOSE (DEBUG, PRINTOUT ("start processing value\n"));
211 char *pt;
212 float value = strtof (str, &pt);
213 VERBOSE (INFO, PRINTOUT ("Value: %f\n", value));
214 if (str != pt) {
215 new = newelement (Val, 1, 4);
216 if (new == NULL) {
217 return ERROR_OP;
218 }
219 new->value = value;
220 if (root == NULL) {
221 root = new;
222 } else if (root->func == Val) {
223 if ((*str == '+') || (*str == '-')) {
224 element_t *add = newelement (Add, 2, 1);
225 if (add == NULL) {
226 return ERROR_OP;
227 }
228 add->ops[0] = root;
229 add->ops[1] = new;
230 root = add;
231 } else {
232 return ERROR_OP;
233 }
234 } else {
235 return ERROR_OP;
236 }
237 str = pt;
238 found = 1;
239 }
240 VERBOSE (DEBUG, PRINTOUT ("stop processing value\n"));
241 }
242
243 /* error */
244
245 if (!found) {
246 return ERROR_OP;
247 }
248
249 }
250
251 if (next != NULL) {
252 *next = str;
253 }
254 return root;
255 }
256
257 /* print element tree */
258
259 void print_element (element_t *root, int level)
260 {
261 char *func = NULL;
262 int i;
263
264 if ((root == NULL) || (root == ERROR_OP)) {
265 return;
266 }
267
268 for (i = 0; i < level; i++) {
269 PRINTOUT (" ");
270 }
271
272 switch (root->func) {
273 case Val: func = "Value"; break;
274 case Set: func = "Set"; break;
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
290 if ((root->func == Val) && (root->ops[0] == NULL)) {
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: */