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