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