annotate lwlib/lw_expr.c @ 342:7b4123dce741

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