334
|
1 /*
|
|
2 lwexpr.c
|
|
3
|
|
4 Copyright © 2010 William Astle
|
|
5
|
|
6 This file is part of LWTOOLS.
|
|
7
|
|
8 LWTOOLS is free software: you can redistribute it and/or modify it under the
|
|
9 terms of the GNU General Public License as published by the Free Software
|
|
10 Foundation, either version 3 of the License, or (at your option) any later
|
|
11 version.
|
|
12
|
|
13 This program is distributed in the hope that it will be useful, but WITHOUT
|
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
16 more details.
|
|
17
|
|
18 You should have received a copy of the GNU General Public License along with
|
|
19 this program. If not, see <http://www.gnu.org/licenses/>.
|
|
20 */
|
|
21
|
|
22 #include <config.h>
|
|
23
|
|
24 #include <stdarg.h>
|
|
25 #include <stdio.h>
|
|
26 #include <string.h>
|
|
27
|
|
28 #define ___lw_expr_c_seen___
|
|
29 #include "lw_alloc.h"
|
|
30 #include "lw_expr.h"
|
|
31 #include "lw_error.h"
|
|
32 #include "lw_string.h"
|
|
33
|
342
|
34 static lw_expr_fn_t *evaluate_special = NULL;
|
|
35 static lw_expr_fn2_t *evaluate_var = NULL;
|
337
|
36
|
342
|
37 void lw_expr_set_special_handler(lw_expr_fn_t *fn)
|
337
|
38 {
|
|
39 evaluate_special = fn;
|
|
40 }
|
|
41
|
342
|
42 void lw_expr_set_var_handler(lw_expr_fn2_t *fn)
|
337
|
43 {
|
|
44 evaluate_var = fn;
|
|
45 }
|
|
46
|
334
|
47 lw_expr_t lw_expr_create(void)
|
|
48 {
|
|
49 lw_expr_t r;
|
|
50
|
|
51 r = lw_alloc(sizeof(struct lw_expr_priv));
|
|
52 r -> operands = NULL;
|
|
53
|
|
54 return r;
|
|
55 }
|
|
56
|
|
57 void lw_expr_destroy(lw_expr_t E)
|
|
58 {
|
337
|
59 struct lw_expr_opers *o;
|
|
60 for (o = E -> operands; o; o = o -> next)
|
|
61 lw_expr_destroy(o -> p);
|
|
62 if (E -> type == lw_expr_type_var)
|
|
63 lw_free(E -> value2);
|
|
64 lw_free(E);
|
334
|
65 }
|
|
66
|
337
|
67 /* actually duplicates the entire expression */
|
|
68 void lw_expr_add_operand(lw_expr_t E, lw_expr_t O);
|
334
|
69 lw_expr_t lw_expr_copy(lw_expr_t E)
|
|
70 {
|
337
|
71 lw_expr_t r, t;
|
|
72 struct lw_expr_opers *o;
|
|
73
|
|
74 r = lw_alloc(sizeof(struct lw_expr_priv));
|
|
75 *r = *E;
|
|
76 r -> operands = NULL;
|
|
77
|
|
78 if (E -> type == lw_expr_type_var)
|
|
79 r -> value2 = lw_strdup(E -> value2);
|
|
80 for (o = E -> operands; o; o = o -> next)
|
|
81 {
|
|
82 lw_expr_add_operand(r, lw_expr_copy(o -> p));
|
|
83 }
|
|
84
|
|
85 return r;
|
334
|
86 }
|
|
87
|
|
88 void lw_expr_add_operand(lw_expr_t E, lw_expr_t O)
|
|
89 {
|
|
90 struct lw_expr_opers *o, *t;
|
|
91
|
|
92 o = lw_alloc(sizeof(struct lw_expr_opers));
|
|
93 o -> p = lw_expr_copy(O);
|
|
94 o -> next = NULL;
|
|
95 for (t = E -> operands; t && t -> next; t = t -> next)
|
|
96 /* do nothing */ ;
|
|
97
|
|
98 if (t)
|
|
99 t -> next = o;
|
|
100 else
|
|
101 E -> operands = o;
|
|
102 }
|
|
103
|
335
|
104 lw_expr_t lw_expr_build_aux(int exprtype, va_list args)
|
334
|
105 {
|
|
106 lw_expr_t r;
|
|
107 int t;
|
|
108 void *p;
|
|
109
|
|
110 lw_expr_t te1, te2;
|
|
111
|
|
112 r = lw_expr_create();
|
|
113
|
|
114 switch (exprtype)
|
|
115 {
|
|
116 case lw_expr_type_int:
|
|
117 t = va_arg(args, int);
|
|
118 r -> type = lw_expr_type_int;
|
|
119 r -> value = t;
|
|
120 break;
|
|
121
|
|
122 case lw_expr_type_var:
|
|
123 p = va_arg(args, char *);
|
|
124 r -> type = lw_expr_type_var;
|
|
125 r -> value2 = lw_strdup(p);
|
|
126 break;
|
|
127
|
|
128 case lw_expr_type_special:
|
|
129 t = va_arg(args, int);
|
|
130 p = va_arg(args, char *);
|
|
131 r -> type = lw_expr_type_special;
|
337
|
132 r -> value = t;
|
334
|
133 r -> value2 = p;
|
|
134 break;
|
|
135
|
|
136 case lw_expr_type_oper:
|
|
137 t = va_arg(args, int);
|
|
138 te1 = va_arg(args, lw_expr_t);
|
|
139 if (t != lw_expr_oper_com && t != lw_expr_oper_neg)
|
|
140 te2 = va_arg(args, lw_expr_t);
|
|
141 else
|
|
142 te2 = NULL;
|
|
143
|
|
144 r -> type = lw_expr_type_oper;
|
|
145 r -> value = t;
|
|
146 lw_expr_add_operand(r, te1);
|
|
147 lw_expr_add_operand(r, te2);
|
|
148 break;
|
|
149
|
|
150 default:
|
|
151 lw_error("Invalid expression type specified to lw_expr_build");
|
|
152 }
|
|
153
|
335
|
154 return r;
|
|
155 }
|
|
156
|
|
157 lw_expr_t lw_expr_build(int exprtype, ...)
|
|
158 {
|
|
159 va_list args;
|
|
160 lw_expr_t r;
|
|
161
|
|
162 va_start(args, exprtype);
|
|
163 r = lw_expr_build_aux(exprtype, args);
|
|
164 va_end(args);
|
|
165 return r;
|
|
166 }
|
|
167
|
334
|
168 void lw_expr_print(lw_expr_t E)
|
|
169 {
|
|
170 struct lw_expr_opers *o;
|
335
|
171 int c = 0;
|
|
172
|
334
|
173 for (o = E -> operands; o; o = o -> next)
|
|
174 {
|
335
|
175 c++;
|
334
|
176 lw_expr_print(o -> p);
|
|
177 }
|
|
178
|
|
179 switch (E -> type)
|
|
180 {
|
|
181 case lw_expr_type_int:
|
|
182 printf("%d ", E -> value);
|
|
183 break;
|
335
|
184
|
|
185 case lw_expr_type_var:
|
|
186 printf("V(%s) ", (char *)(E -> value2));
|
|
187 break;
|
|
188
|
|
189 case lw_expr_type_special:
|
|
190 printf("S(%d,%p) ", E -> value, E -> value2);
|
|
191 break;
|
|
192
|
334
|
193 case lw_expr_type_oper:
|
335
|
194 printf("[%d]", c);
|
334
|
195 switch (E -> value)
|
|
196 {
|
|
197 case lw_expr_oper_plus:
|
|
198 printf("+ ");
|
|
199 break;
|
|
200
|
|
201 case lw_expr_oper_minus:
|
|
202 printf("- ");
|
|
203 break;
|
|
204
|
|
205 case lw_expr_oper_times:
|
|
206 printf("* ");
|
|
207 break;
|
|
208
|
|
209 case lw_expr_oper_divide:
|
|
210 printf("/ ");
|
|
211 break;
|
|
212
|
|
213 case lw_expr_oper_mod:
|
|
214 printf("%% ");
|
|
215 break;
|
|
216
|
|
217 case lw_expr_oper_intdiv:
|
|
218 printf("\\ ");
|
|
219 break;
|
|
220
|
|
221 case lw_expr_oper_bwand:
|
|
222 printf("BWAND ");
|
|
223 break;
|
|
224
|
|
225 case lw_expr_oper_bwor:
|
|
226 printf("BWOR ");
|
|
227 break;
|
|
228
|
|
229 case lw_expr_oper_bwxor:
|
|
230 printf("BWXOR ");
|
|
231 break;
|
|
232
|
|
233 case lw_expr_oper_and:
|
|
234 printf("AND ");
|
|
235 break;
|
|
236
|
|
237 case lw_expr_oper_or:
|
|
238 printf("OR ");
|
|
239 break;
|
|
240
|
|
241 case lw_expr_oper_neg:
|
|
242 printf("NEG ");
|
|
243 break;
|
|
244
|
|
245 case lw_expr_oper_com:
|
|
246 printf("COM ");
|
|
247 break;
|
|
248
|
|
249 default:
|
|
250 printf("OPER ");
|
|
251 break;
|
|
252 }
|
|
253 break;
|
|
254 default:
|
|
255 printf("ERR ");
|
|
256 break;
|
|
257 }
|
|
258 }
|
|
259
|
|
260 /*
|
|
261 Return:
|
|
262 nonzero if expressions are the same (identical pointers or matching values)
|
|
263 zero if expressions are not the same
|
|
264
|
|
265 */
|
|
266 int lw_expr_compare(lw_expr_t E1, lw_expr_t E2)
|
|
267 {
|
|
268 struct lw_expr_opers *o1, *o2;
|
|
269
|
|
270 if (E1 == E2)
|
|
271 return 1;
|
|
272
|
|
273 if (!(E1 -> type == E2 -> type && E1 -> value == E2 -> value))
|
|
274 return 0;
|
|
275
|
|
276 if (E1 -> type == lw_expr_type_var)
|
|
277 {
|
|
278 if (!strcmp(E1 -> value2, E2 -> value2))
|
|
279 return 1;
|
|
280 else
|
|
281 return 0;
|
|
282 }
|
|
283
|
|
284 if (E1 -> type == lw_expr_type_special)
|
|
285 {
|
|
286 if (E1 -> value2 == E2 -> value2)
|
|
287 return 1;
|
|
288 else
|
|
289 return 0;
|
|
290 }
|
|
291
|
|
292 for (o1 = E1 -> operands, o2 = E2 -> operands; o1 && o2; o1 = o1 -> next, o2 = o2 -> next)
|
|
293 if (lw_expr_compare(o1 -> p, o2 -> p) == 0)
|
|
294 return 0;
|
|
295 if (o1 || o2)
|
|
296 return 0;
|
|
297
|
|
298 return 1;
|
|
299 }
|
|
300
|
|
301 /* return true if E is an operator of type oper */
|
|
302 int lw_expr_isoper(lw_expr_t E, int oper)
|
|
303 {
|
|
304 if (E -> type == lw_expr_type_oper && E -> value == oper)
|
|
305 return 1;
|
|
306 return 0;
|
|
307 }
|
335
|
308
|
|
309
|
|
310 void lw_expr_simplify_sortconstfirst(lw_expr_t E)
|
|
311 {
|
|
312 struct lw_expr_opers *o;
|
|
313
|
|
314 for (o = E -> operands; o; o = o -> next)
|
|
315 lw_expr_simplify_sortconstfirst(o -> p);
|
|
316
|
|
317 for (o = E -> operands; o; o = o -> next)
|
|
318 {
|
|
319 if (o -> p -> type == lw_expr_type_int && o != E -> operands)
|
|
320 {
|
|
321 struct lw_expr_opers *o2;
|
|
322 for (o2 = E -> operands; o2 -> next != o; o2 = o2 -> next)
|
|
323 /* do nothing */ ;
|
|
324 o2 -> next = o -> next;
|
|
325 o -> next = E -> operands;
|
|
326 E -> operands = o;
|
|
327 o = o2;
|
|
328 }
|
|
329 }
|
|
330 }
|
|
331
|
336
|
332 void lw_expr_sortoperandlist(struct lw_expr_opers **o)
|
|
333 {
|
|
334 fprintf(stderr, "lw_expr_sortoperandlist() not yet implemented\n");
|
|
335 }
|
|
336
|
335
|
337 // return 1 if the operand lists match, 0 if not
|
|
338 // may re-order the argument lists
|
|
339 int lw_expr_simplify_compareoperandlist(struct lw_expr_opers **ol1, struct lw_expr_opers **ol2)
|
|
340 {
|
|
341 struct lw_expr_opers *o1, *o2;
|
|
342
|
|
343 lw_expr_sortoperandlist(ol1);
|
|
344 lw_expr_sortoperandlist(ol2);
|
|
345
|
|
346 for (o1 = *ol1, o2 = *ol2; o1 && o2; o1 = o1 -> next, o2 = o2 -> next)
|
|
347 {
|
|
348 if (!lw_expr_compare(o1 -> p, o2 -> p))
|
|
349 return 0;
|
|
350 }
|
|
351 if (o1 || o2)
|
|
352 return 0;
|
|
353 return 1;
|
|
354 }
|
|
355
|
|
356 void lw_expr_simplify(lw_expr_t E)
|
|
357 {
|
|
358 struct lw_expr_opers *o;
|
|
359
|
337
|
360 again:
|
|
361 // try to resolve non-constant terms to constants here
|
|
362 if (E -> type == lw_expr_type_special && evaluate_special)
|
|
363 {
|
|
364 lw_expr_t te;
|
|
365
|
|
366 te = evaluate_special(E -> value, E -> value2);
|
|
367 if (te)
|
|
368 {
|
|
369 for (o = E -> operands; o; o = o -> next)
|
|
370 lw_expr_destroy(o -> p);
|
|
371 if (E -> type == lw_expr_type_var)
|
|
372 lw_free(E -> value2);
|
|
373 *E = *te;
|
|
374 E -> operands = NULL;
|
|
375
|
|
376 if (te -> type == lw_expr_type_var)
|
|
377 E -> value2 = lw_strdup(te -> value2);
|
|
378 for (o = te -> operands; o; o = o -> next)
|
|
379 {
|
|
380 lw_expr_add_operand(E, lw_expr_copy(o -> p));
|
|
381 }
|
|
382 lw_expr_destroy(te);
|
|
383 goto again;
|
|
384 }
|
|
385 return;
|
|
386 }
|
|
387
|
|
388 if (E -> type == lw_expr_type_var && evaluate_var)
|
|
389 {
|
|
390 lw_expr_t te;
|
|
391
|
|
392 te = evaluate_var(E -> value2);
|
|
393 if (te)
|
|
394 {
|
|
395 for (o = E -> operands; o; o = o -> next)
|
|
396 lw_expr_destroy(o -> p);
|
|
397 if (E -> type == lw_expr_type_var)
|
|
398 lw_free(E -> value2);
|
|
399 *E = *te;
|
|
400 E -> operands = NULL;
|
|
401
|
|
402 if (te -> type == lw_expr_type_var)
|
|
403 E -> value2 = lw_strdup(te -> value2);
|
|
404 for (o = te -> operands; o; o = o -> next)
|
|
405 {
|
|
406 lw_expr_add_operand(E, lw_expr_copy(o -> p));
|
|
407 }
|
|
408 lw_expr_destroy(te);
|
|
409 goto again;
|
|
410 }
|
|
411 return;
|
|
412 }
|
|
413
|
|
414 // non-operators have no simplification to do!
|
|
415 if (E -> type != lw_expr_type_oper)
|
|
416 return;
|
|
417
|
335
|
418 // sort "constants" to the start of each operand list for + and *
|
|
419 lw_expr_simplify_sortconstfirst(E);
|
|
420
|
337
|
421 // simplify operands
|
|
422 for (o = E -> operands; o; o = o -> next)
|
|
423 lw_expr_simplify(o -> p);
|
|
424
|
|
425 for (o = E -> operands; o; o = o -> next)
|
|
426 {
|
|
427 if (o -> p -> type != lw_expr_type_int)
|
|
428 break;
|
|
429 }
|
|
430
|
|
431 if (!o)
|
|
432 {
|
|
433 // we can do the operation here!
|
|
434 int tr = -42424242;
|
|
435
|
|
436 switch (E -> value)
|
|
437 {
|
|
438 case lw_expr_oper_neg:
|
|
439 tr = -(E -> operands -> p -> value);
|
|
440 break;
|
|
441
|
|
442 case lw_expr_oper_com:
|
|
443 tr = ~(E -> operands -> p -> value);
|
|
444 break;
|
|
445
|
|
446 case lw_expr_oper_plus:
|
|
447 tr = E -> operands -> p -> value;
|
|
448 for (o = E -> operands -> next; o; o = o -> next)
|
|
449 tr += o -> p -> value;
|
|
450 break;
|
|
451
|
|
452 case lw_expr_oper_minus:
|
|
453 tr = E -> operands -> p -> value;
|
|
454 for (o = E -> operands -> next; o; o = o -> next)
|
|
455 tr -= o -> p -> value;
|
|
456 break;
|
|
457
|
|
458 case lw_expr_oper_times:
|
|
459 tr = E -> operands -> p -> value;
|
|
460 for (o = E -> operands -> next; o; o = o -> next)
|
|
461 tr *= o -> p -> value;
|
|
462 break;
|
|
463
|
|
464 case lw_expr_oper_divide:
|
|
465 tr = E -> operands -> p -> value / E -> operands -> next -> p -> value;
|
|
466 break;
|
|
467
|
|
468 case lw_expr_oper_mod:
|
|
469 tr = E -> operands -> p -> value % E -> operands -> next -> p -> value;
|
|
470 break;
|
|
471
|
|
472 case lw_expr_oper_intdiv:
|
|
473 tr = E -> operands -> p -> value / E -> operands -> next -> p -> value;
|
|
474 break;
|
|
475
|
|
476 case lw_expr_oper_bwand:
|
|
477 tr = E -> operands -> p -> value & E -> operands -> next -> p -> value;
|
|
478 break;
|
|
479
|
|
480 case lw_expr_oper_bwor:
|
|
481 tr = E -> operands -> p -> value | E -> operands -> next -> p -> value;
|
|
482 break;
|
|
483
|
|
484 case lw_expr_oper_bwxor:
|
|
485 tr = E -> operands -> p -> value ^ E -> operands -> next -> p -> value;
|
|
486 break;
|
|
487
|
|
488 case lw_expr_oper_and:
|
|
489 tr = E -> operands -> p -> value && E -> operands -> next -> p -> value;
|
|
490 break;
|
|
491
|
|
492 case lw_expr_oper_or:
|
|
493 tr = E -> operands -> p -> value || E -> operands -> next -> p -> value;
|
|
494 break;
|
|
495
|
|
496 }
|
|
497
|
|
498 while (E -> operands)
|
|
499 {
|
|
500 o = E -> operands;
|
|
501 E -> operands = o -> next;
|
|
502 lw_expr_destroy(o -> p);
|
|
503 lw_free(o);
|
|
504 }
|
|
505 E -> type = lw_expr_type_int;
|
|
506 E -> value = tr;
|
335
|
507 return;
|
337
|
508 }
|
335
|
509
|
|
510 if (E -> value == lw_expr_oper_times)
|
|
511 {
|
|
512 for (o = E -> operands; o; o = o -> next)
|
|
513 {
|
|
514 if (o -> p -> type == lw_expr_type_int && o -> p -> value == 0)
|
|
515 {
|
|
516 // one operand of times is 0, replace operation with 0
|
|
517 while (E -> operands)
|
|
518 {
|
|
519 o = E -> operands;
|
|
520 E -> operands = o -> next;
|
|
521 lw_expr_destroy(o -> p);
|
|
522 lw_free(o);
|
|
523 }
|
|
524 E -> type = lw_expr_type_int;
|
|
525 E -> value = 0;
|
|
526 return;
|
|
527 }
|
|
528 }
|
|
529 }
|
|
530
|
|
531 // look for like terms and collect them together
|
|
532 if (E -> value == lw_expr_oper_plus)
|
|
533 {
|
|
534 for (o = E -> operands; o; o = o -> next)
|
|
535 {
|
|
536 if (o -> p -> type == lw_expr_type_oper && o -> p -> value == lw_expr_oper_times)
|
|
537 {
|
|
538 // we have a "times" here
|
|
539 // find first non-const operand
|
|
540 struct lw_expr_opers *op1, *op2, *o2;
|
|
541 for (op1 = o -> p -> operands; op1; op1 = op1 -> next)
|
|
542 if (op1 -> p -> type != lw_expr_type_int)
|
|
543 break;
|
|
544
|
|
545 for (o2 = o -> next; o2; o2 = o2 -> next)
|
|
546 {
|
|
547 if (o2 -> p -> type == lw_expr_type_oper && o2 -> p -> value == lw_expr_oper_times)
|
|
548 {
|
|
549 // another "times"
|
|
550 for (op2 = o2 -> p -> operands; op2; op2 = op2 -> next)
|
|
551 if (op2 -> p -> type != lw_expr_type_int)
|
|
552 break;
|
|
553
|
336
|
554 if (lw_expr_simplify_compareoperandlist(&op1, &op2))
|
335
|
555 {
|
|
556 // we have like terms here
|
|
557 // do something about it
|
|
558 }
|
|
559 }
|
|
560 }
|
|
561 }
|
|
562 }
|
|
563 }
|
|
564
|
|
565
|
|
566 if (E -> value == lw_expr_oper_plus)
|
|
567 {
|
|
568 int c = 0, t = 0;
|
|
569 for (o = E -> operands; o; o = o -> next)
|
|
570 {
|
|
571 t++;
|
|
572 if (!(o -> p -> type == lw_expr_type_int && o -> p -> value == 0))
|
|
573 {
|
|
574 c++;
|
|
575 }
|
|
576 }
|
|
577 if (c == 1)
|
|
578 {
|
|
579 lw_expr_t r;
|
|
580 // find the value and "move it up"
|
|
581 while (E -> operands)
|
|
582 {
|
|
583 o = E -> operands;
|
|
584 if (o -> p -> type != lw_expr_type_int || o -> p -> value != 0)
|
|
585 {
|
337
|
586 r = lw_expr_copy(o -> p);
|
335
|
587 }
|
|
588 E -> operands = o -> next;
|
|
589 lw_expr_destroy(o -> p);
|
|
590 lw_free(o);
|
|
591 }
|
|
592 *E = *r;
|
|
593 return;
|
|
594 }
|
|
595 else if (c == 0)
|
|
596 {
|
|
597 // replace with 0
|
|
598 while (E -> operands)
|
|
599 {
|
|
600 o = E -> operands;
|
|
601 E -> operands = o -> next;
|
|
602 lw_expr_destroy(o -> p);
|
|
603 lw_free(o);
|
|
604 }
|
|
605 E -> type = lw_expr_type_int;
|
|
606 E -> value = 0;
|
|
607 return;
|
|
608 }
|
|
609 else if (c != t)
|
|
610 {
|
|
611 // collapse out zero terms
|
|
612 struct lw_expr_opers *o2;
|
|
613
|
|
614 for (o = E -> operands; o; o = o -> next)
|
|
615 {
|
|
616 if (o -> p -> type == lw_expr_type_int && o -> p -> value == 0)
|
|
617 {
|
|
618 if (o == E -> operands)
|
|
619 {
|
|
620 E -> operands = o -> next;
|
|
621 lw_expr_destroy(o -> p);
|
|
622 lw_free(o);
|
|
623 o = E -> operands;
|
|
624 }
|
|
625 else
|
|
626 {
|
|
627 for (o2 = E -> operands; o2 -> next == o; o2 = o2 -> next)
|
|
628 /* do nothing */ ;
|
|
629 o2 -> next = o -> next;
|
|
630 lw_expr_destroy(o -> p);
|
|
631 lw_free(o);
|
|
632 o = o2;
|
|
633 }
|
|
634 }
|
|
635 }
|
|
636 }
|
|
637 return;
|
|
638 }
|
|
639 }
|