annotate lwlib/lw_expr.c @ 346:a82c55070624

Added expression parsing infrastructure and misc fixes
author lost@starbug
date Sat, 27 Mar 2010 19:04:03 -0600
parents 7b4123dce741
children 1649bc7bda5a
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;
346
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
36 static lw_expr_fn3_t *parse_term = NULL;
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
37
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
38 void lw_expr_set_term_parser(lw_expr_fn3_t *fn)
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
39 {
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
40 parse_term = fn;
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
41 }
337
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
42
342
7b4123dce741 Added basic symbol registration
lost@starbug
parents: 337
diff changeset
43 void lw_expr_set_special_handler(lw_expr_fn_t *fn)
337
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
44 {
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
45 evaluate_special = fn;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
46 }
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
47
342
7b4123dce741 Added basic symbol registration
lost@starbug
parents: 337
diff changeset
48 void lw_expr_set_var_handler(lw_expr_fn2_t *fn)
337
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
49 {
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
50 evaluate_var = fn;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
51 }
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
52
334
f2173d18c73f Checkpoint
lost
parents:
diff changeset
53 lw_expr_t lw_expr_create(void)
f2173d18c73f Checkpoint
lost
parents:
diff changeset
54 {
f2173d18c73f Checkpoint
lost
parents:
diff changeset
55 lw_expr_t r;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
56
f2173d18c73f Checkpoint
lost
parents:
diff changeset
57 r = lw_alloc(sizeof(struct lw_expr_priv));
f2173d18c73f Checkpoint
lost
parents:
diff changeset
58 r -> operands = NULL;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
59
f2173d18c73f Checkpoint
lost
parents:
diff changeset
60 return r;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
61 }
f2173d18c73f Checkpoint
lost
parents:
diff changeset
62
f2173d18c73f Checkpoint
lost
parents:
diff changeset
63 void lw_expr_destroy(lw_expr_t E)
f2173d18c73f Checkpoint
lost
parents:
diff changeset
64 {
337
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
65 struct lw_expr_opers *o;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
66 for (o = E -> operands; o; o = o -> next)
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
67 lw_expr_destroy(o -> p);
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
68 if (E -> type == lw_expr_type_var)
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
69 lw_free(E -> value2);
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
70 lw_free(E);
334
f2173d18c73f Checkpoint
lost
parents:
diff changeset
71 }
f2173d18c73f Checkpoint
lost
parents:
diff changeset
72
337
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
73 /* actually duplicates the entire expression */
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
74 void lw_expr_add_operand(lw_expr_t E, lw_expr_t O);
334
f2173d18c73f Checkpoint
lost
parents:
diff changeset
75 lw_expr_t lw_expr_copy(lw_expr_t E)
f2173d18c73f Checkpoint
lost
parents:
diff changeset
76 {
337
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
77 lw_expr_t r, t;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
78 struct lw_expr_opers *o;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
79
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
80 r = lw_alloc(sizeof(struct lw_expr_priv));
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
81 *r = *E;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
82 r -> operands = NULL;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
83
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
84 if (E -> type == lw_expr_type_var)
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
85 r -> value2 = lw_strdup(E -> value2);
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
86 for (o = E -> operands; o; o = o -> next)
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
87 {
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
88 lw_expr_add_operand(r, lw_expr_copy(o -> p));
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
89 }
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
90
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
91 return r;
334
f2173d18c73f Checkpoint
lost
parents:
diff changeset
92 }
f2173d18c73f Checkpoint
lost
parents:
diff changeset
93
f2173d18c73f Checkpoint
lost
parents:
diff changeset
94 void lw_expr_add_operand(lw_expr_t E, lw_expr_t O)
f2173d18c73f Checkpoint
lost
parents:
diff changeset
95 {
f2173d18c73f Checkpoint
lost
parents:
diff changeset
96 struct lw_expr_opers *o, *t;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
97
f2173d18c73f Checkpoint
lost
parents:
diff changeset
98 o = lw_alloc(sizeof(struct lw_expr_opers));
f2173d18c73f Checkpoint
lost
parents:
diff changeset
99 o -> p = lw_expr_copy(O);
f2173d18c73f Checkpoint
lost
parents:
diff changeset
100 o -> next = NULL;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
101 for (t = E -> operands; t && t -> next; t = t -> next)
f2173d18c73f Checkpoint
lost
parents:
diff changeset
102 /* do nothing */ ;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
103
f2173d18c73f Checkpoint
lost
parents:
diff changeset
104 if (t)
f2173d18c73f Checkpoint
lost
parents:
diff changeset
105 t -> next = o;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
106 else
f2173d18c73f Checkpoint
lost
parents:
diff changeset
107 E -> operands = o;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
108 }
f2173d18c73f Checkpoint
lost
parents:
diff changeset
109
335
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
110 lw_expr_t lw_expr_build_aux(int exprtype, va_list args)
334
f2173d18c73f Checkpoint
lost
parents:
diff changeset
111 {
f2173d18c73f Checkpoint
lost
parents:
diff changeset
112 lw_expr_t r;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
113 int t;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
114 void *p;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
115
f2173d18c73f Checkpoint
lost
parents:
diff changeset
116 lw_expr_t te1, te2;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
117
f2173d18c73f Checkpoint
lost
parents:
diff changeset
118 r = lw_expr_create();
f2173d18c73f Checkpoint
lost
parents:
diff changeset
119
f2173d18c73f Checkpoint
lost
parents:
diff changeset
120 switch (exprtype)
f2173d18c73f Checkpoint
lost
parents:
diff changeset
121 {
f2173d18c73f Checkpoint
lost
parents:
diff changeset
122 case lw_expr_type_int:
f2173d18c73f Checkpoint
lost
parents:
diff changeset
123 t = va_arg(args, int);
f2173d18c73f Checkpoint
lost
parents:
diff changeset
124 r -> type = lw_expr_type_int;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
125 r -> value = t;
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_var:
f2173d18c73f Checkpoint
lost
parents:
diff changeset
129 p = va_arg(args, char *);
f2173d18c73f Checkpoint
lost
parents:
diff changeset
130 r -> type = lw_expr_type_var;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
131 r -> value2 = lw_strdup(p);
f2173d18c73f Checkpoint
lost
parents:
diff changeset
132 break;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
133
f2173d18c73f Checkpoint
lost
parents:
diff changeset
134 case lw_expr_type_special:
f2173d18c73f Checkpoint
lost
parents:
diff changeset
135 t = va_arg(args, int);
f2173d18c73f Checkpoint
lost
parents:
diff changeset
136 p = va_arg(args, char *);
f2173d18c73f Checkpoint
lost
parents:
diff changeset
137 r -> type = lw_expr_type_special;
337
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
138 r -> value = t;
334
f2173d18c73f Checkpoint
lost
parents:
diff changeset
139 r -> value2 = p;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
140 break;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
141
f2173d18c73f Checkpoint
lost
parents:
diff changeset
142 case lw_expr_type_oper:
f2173d18c73f Checkpoint
lost
parents:
diff changeset
143 t = va_arg(args, int);
f2173d18c73f Checkpoint
lost
parents:
diff changeset
144 te1 = va_arg(args, lw_expr_t);
f2173d18c73f Checkpoint
lost
parents:
diff changeset
145 if (t != lw_expr_oper_com && t != lw_expr_oper_neg)
f2173d18c73f Checkpoint
lost
parents:
diff changeset
146 te2 = va_arg(args, lw_expr_t);
f2173d18c73f Checkpoint
lost
parents:
diff changeset
147 else
f2173d18c73f Checkpoint
lost
parents:
diff changeset
148 te2 = NULL;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
149
f2173d18c73f Checkpoint
lost
parents:
diff changeset
150 r -> type = lw_expr_type_oper;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
151 r -> value = t;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
152 lw_expr_add_operand(r, te1);
f2173d18c73f Checkpoint
lost
parents:
diff changeset
153 lw_expr_add_operand(r, te2);
f2173d18c73f Checkpoint
lost
parents:
diff changeset
154 break;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
155
f2173d18c73f Checkpoint
lost
parents:
diff changeset
156 default:
f2173d18c73f Checkpoint
lost
parents:
diff changeset
157 lw_error("Invalid expression type specified to lw_expr_build");
f2173d18c73f Checkpoint
lost
parents:
diff changeset
158 }
f2173d18c73f Checkpoint
lost
parents:
diff changeset
159
335
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
160 return r;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
161 }
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
162
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
163 lw_expr_t lw_expr_build(int exprtype, ...)
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
164 {
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
165 va_list args;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
166 lw_expr_t r;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
167
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
168 va_start(args, exprtype);
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
169 r = lw_expr_build_aux(exprtype, args);
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
170 va_end(args);
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
171 return r;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
172 }
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
173
334
f2173d18c73f Checkpoint
lost
parents:
diff changeset
174 void lw_expr_print(lw_expr_t E)
f2173d18c73f Checkpoint
lost
parents:
diff changeset
175 {
f2173d18c73f Checkpoint
lost
parents:
diff changeset
176 struct lw_expr_opers *o;
335
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
177 int c = 0;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
178
334
f2173d18c73f Checkpoint
lost
parents:
diff changeset
179 for (o = E -> operands; o; o = o -> next)
f2173d18c73f Checkpoint
lost
parents:
diff changeset
180 {
335
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
181 c++;
334
f2173d18c73f Checkpoint
lost
parents:
diff changeset
182 lw_expr_print(o -> p);
f2173d18c73f Checkpoint
lost
parents:
diff changeset
183 }
f2173d18c73f Checkpoint
lost
parents:
diff changeset
184
f2173d18c73f Checkpoint
lost
parents:
diff changeset
185 switch (E -> type)
f2173d18c73f Checkpoint
lost
parents:
diff changeset
186 {
f2173d18c73f Checkpoint
lost
parents:
diff changeset
187 case lw_expr_type_int:
f2173d18c73f Checkpoint
lost
parents:
diff changeset
188 printf("%d ", E -> value);
f2173d18c73f Checkpoint
lost
parents:
diff changeset
189 break;
335
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
190
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
191 case lw_expr_type_var:
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
192 printf("V(%s) ", (char *)(E -> value2));
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
193 break;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
194
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
195 case lw_expr_type_special:
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
196 printf("S(%d,%p) ", E -> value, E -> value2);
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
197 break;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
198
334
f2173d18c73f Checkpoint
lost
parents:
diff changeset
199 case lw_expr_type_oper:
335
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
200 printf("[%d]", c);
334
f2173d18c73f Checkpoint
lost
parents:
diff changeset
201 switch (E -> value)
f2173d18c73f Checkpoint
lost
parents:
diff changeset
202 {
f2173d18c73f Checkpoint
lost
parents:
diff changeset
203 case lw_expr_oper_plus:
f2173d18c73f Checkpoint
lost
parents:
diff changeset
204 printf("+ ");
f2173d18c73f Checkpoint
lost
parents:
diff changeset
205 break;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
206
f2173d18c73f Checkpoint
lost
parents:
diff changeset
207 case lw_expr_oper_minus:
f2173d18c73f Checkpoint
lost
parents:
diff changeset
208 printf("- ");
f2173d18c73f Checkpoint
lost
parents:
diff changeset
209 break;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
210
f2173d18c73f Checkpoint
lost
parents:
diff changeset
211 case lw_expr_oper_times:
f2173d18c73f Checkpoint
lost
parents:
diff changeset
212 printf("* ");
f2173d18c73f Checkpoint
lost
parents:
diff changeset
213 break;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
214
f2173d18c73f Checkpoint
lost
parents:
diff changeset
215 case lw_expr_oper_divide:
f2173d18c73f Checkpoint
lost
parents:
diff changeset
216 printf("/ ");
f2173d18c73f Checkpoint
lost
parents:
diff changeset
217 break;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
218
f2173d18c73f Checkpoint
lost
parents:
diff changeset
219 case lw_expr_oper_mod:
f2173d18c73f Checkpoint
lost
parents:
diff changeset
220 printf("%% ");
f2173d18c73f Checkpoint
lost
parents:
diff changeset
221 break;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
222
f2173d18c73f Checkpoint
lost
parents:
diff changeset
223 case lw_expr_oper_intdiv:
f2173d18c73f Checkpoint
lost
parents:
diff changeset
224 printf("\\ ");
f2173d18c73f Checkpoint
lost
parents:
diff changeset
225 break;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
226
f2173d18c73f Checkpoint
lost
parents:
diff changeset
227 case lw_expr_oper_bwand:
f2173d18c73f Checkpoint
lost
parents:
diff changeset
228 printf("BWAND ");
f2173d18c73f Checkpoint
lost
parents:
diff changeset
229 break;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
230
f2173d18c73f Checkpoint
lost
parents:
diff changeset
231 case lw_expr_oper_bwor:
f2173d18c73f Checkpoint
lost
parents:
diff changeset
232 printf("BWOR ");
f2173d18c73f Checkpoint
lost
parents:
diff changeset
233 break;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
234
f2173d18c73f Checkpoint
lost
parents:
diff changeset
235 case lw_expr_oper_bwxor:
f2173d18c73f Checkpoint
lost
parents:
diff changeset
236 printf("BWXOR ");
f2173d18c73f Checkpoint
lost
parents:
diff changeset
237 break;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
238
f2173d18c73f Checkpoint
lost
parents:
diff changeset
239 case lw_expr_oper_and:
f2173d18c73f Checkpoint
lost
parents:
diff changeset
240 printf("AND ");
f2173d18c73f Checkpoint
lost
parents:
diff changeset
241 break;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
242
f2173d18c73f Checkpoint
lost
parents:
diff changeset
243 case lw_expr_oper_or:
f2173d18c73f Checkpoint
lost
parents:
diff changeset
244 printf("OR ");
f2173d18c73f Checkpoint
lost
parents:
diff changeset
245 break;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
246
f2173d18c73f Checkpoint
lost
parents:
diff changeset
247 case lw_expr_oper_neg:
f2173d18c73f Checkpoint
lost
parents:
diff changeset
248 printf("NEG ");
f2173d18c73f Checkpoint
lost
parents:
diff changeset
249 break;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
250
f2173d18c73f Checkpoint
lost
parents:
diff changeset
251 case lw_expr_oper_com:
f2173d18c73f Checkpoint
lost
parents:
diff changeset
252 printf("COM ");
f2173d18c73f Checkpoint
lost
parents:
diff changeset
253 break;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
254
f2173d18c73f Checkpoint
lost
parents:
diff changeset
255 default:
f2173d18c73f Checkpoint
lost
parents:
diff changeset
256 printf("OPER ");
f2173d18c73f Checkpoint
lost
parents:
diff changeset
257 break;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
258 }
f2173d18c73f Checkpoint
lost
parents:
diff changeset
259 break;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
260 default:
f2173d18c73f Checkpoint
lost
parents:
diff changeset
261 printf("ERR ");
f2173d18c73f Checkpoint
lost
parents:
diff changeset
262 break;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
263 }
f2173d18c73f Checkpoint
lost
parents:
diff changeset
264 }
f2173d18c73f Checkpoint
lost
parents:
diff changeset
265
f2173d18c73f Checkpoint
lost
parents:
diff changeset
266 /*
f2173d18c73f Checkpoint
lost
parents:
diff changeset
267 Return:
f2173d18c73f Checkpoint
lost
parents:
diff changeset
268 nonzero if expressions are the same (identical pointers or matching values)
f2173d18c73f Checkpoint
lost
parents:
diff changeset
269 zero if expressions are not the same
f2173d18c73f Checkpoint
lost
parents:
diff changeset
270
f2173d18c73f Checkpoint
lost
parents:
diff changeset
271 */
f2173d18c73f Checkpoint
lost
parents:
diff changeset
272 int lw_expr_compare(lw_expr_t E1, lw_expr_t E2)
f2173d18c73f Checkpoint
lost
parents:
diff changeset
273 {
f2173d18c73f Checkpoint
lost
parents:
diff changeset
274 struct lw_expr_opers *o1, *o2;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
275
f2173d18c73f Checkpoint
lost
parents:
diff changeset
276 if (E1 == E2)
f2173d18c73f Checkpoint
lost
parents:
diff changeset
277 return 1;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
278
f2173d18c73f Checkpoint
lost
parents:
diff changeset
279 if (!(E1 -> type == E2 -> type && E1 -> value == E2 -> value))
f2173d18c73f Checkpoint
lost
parents:
diff changeset
280 return 0;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
281
f2173d18c73f Checkpoint
lost
parents:
diff changeset
282 if (E1 -> type == lw_expr_type_var)
f2173d18c73f Checkpoint
lost
parents:
diff changeset
283 {
f2173d18c73f Checkpoint
lost
parents:
diff changeset
284 if (!strcmp(E1 -> value2, E2 -> value2))
f2173d18c73f Checkpoint
lost
parents:
diff changeset
285 return 1;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
286 else
f2173d18c73f Checkpoint
lost
parents:
diff changeset
287 return 0;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
288 }
f2173d18c73f Checkpoint
lost
parents:
diff changeset
289
f2173d18c73f Checkpoint
lost
parents:
diff changeset
290 if (E1 -> type == lw_expr_type_special)
f2173d18c73f Checkpoint
lost
parents:
diff changeset
291 {
f2173d18c73f Checkpoint
lost
parents:
diff changeset
292 if (E1 -> value2 == E2 -> value2)
f2173d18c73f Checkpoint
lost
parents:
diff changeset
293 return 1;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
294 else
f2173d18c73f Checkpoint
lost
parents:
diff changeset
295 return 0;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
296 }
f2173d18c73f Checkpoint
lost
parents:
diff changeset
297
f2173d18c73f Checkpoint
lost
parents:
diff changeset
298 for (o1 = E1 -> operands, o2 = E2 -> operands; o1 && o2; o1 = o1 -> next, o2 = o2 -> next)
f2173d18c73f Checkpoint
lost
parents:
diff changeset
299 if (lw_expr_compare(o1 -> p, o2 -> p) == 0)
f2173d18c73f Checkpoint
lost
parents:
diff changeset
300 return 0;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
301 if (o1 || o2)
f2173d18c73f Checkpoint
lost
parents:
diff changeset
302 return 0;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
303
f2173d18c73f Checkpoint
lost
parents:
diff changeset
304 return 1;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
305 }
f2173d18c73f Checkpoint
lost
parents:
diff changeset
306
f2173d18c73f Checkpoint
lost
parents:
diff changeset
307 /* return true if E is an operator of type oper */
f2173d18c73f Checkpoint
lost
parents:
diff changeset
308 int lw_expr_isoper(lw_expr_t E, int oper)
f2173d18c73f Checkpoint
lost
parents:
diff changeset
309 {
f2173d18c73f Checkpoint
lost
parents:
diff changeset
310 if (E -> type == lw_expr_type_oper && E -> value == oper)
f2173d18c73f Checkpoint
lost
parents:
diff changeset
311 return 1;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
312 return 0;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
313 }
335
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
314
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
315
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
316 void lw_expr_simplify_sortconstfirst(lw_expr_t E)
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
317 {
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
318 struct lw_expr_opers *o;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
319
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
320 for (o = E -> operands; o; o = o -> next)
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
321 lw_expr_simplify_sortconstfirst(o -> p);
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
322
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
323 for (o = E -> operands; o; o = o -> next)
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
324 {
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
325 if (o -> p -> type == lw_expr_type_int && o != E -> operands)
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
326 {
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
327 struct lw_expr_opers *o2;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
328 for (o2 = E -> operands; o2 -> next != o; o2 = o2 -> next)
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
329 /* do nothing */ ;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
330 o2 -> next = o -> next;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
331 o -> next = E -> operands;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
332 E -> operands = o;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
333 o = o2;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
334 }
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
335 }
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
336 }
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
337
336
401587ab6a09 checkpoint
lost
parents: 335
diff changeset
338 void lw_expr_sortoperandlist(struct lw_expr_opers **o)
401587ab6a09 checkpoint
lost
parents: 335
diff changeset
339 {
401587ab6a09 checkpoint
lost
parents: 335
diff changeset
340 fprintf(stderr, "lw_expr_sortoperandlist() not yet implemented\n");
401587ab6a09 checkpoint
lost
parents: 335
diff changeset
341 }
401587ab6a09 checkpoint
lost
parents: 335
diff changeset
342
335
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
343 // return 1 if the operand lists match, 0 if not
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
344 // may re-order the argument lists
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
345 int lw_expr_simplify_compareoperandlist(struct lw_expr_opers **ol1, struct lw_expr_opers **ol2)
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
346 {
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
347 struct lw_expr_opers *o1, *o2;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
348
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
349 lw_expr_sortoperandlist(ol1);
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
350 lw_expr_sortoperandlist(ol2);
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
351
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
352 for (o1 = *ol1, o2 = *ol2; o1 && o2; o1 = o1 -> next, o2 = o2 -> next)
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
353 {
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
354 if (!lw_expr_compare(o1 -> p, o2 -> p))
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
355 return 0;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
356 }
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
357 if (o1 || o2)
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
358 return 0;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
359 return 1;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
360 }
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
361
346
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
362 void lw_expr_simplify(lw_expr_t E, void *priv)
335
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
363 {
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
364 struct lw_expr_opers *o;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
365
337
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
366 again:
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
367 // try to resolve non-constant terms to constants here
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
368 if (E -> type == lw_expr_type_special && evaluate_special)
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
369 {
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
370 lw_expr_t te;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
371
346
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
372 te = evaluate_special(E -> value, E -> value2, priv);
337
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
373 if (te)
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
374 {
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
375 for (o = E -> operands; o; o = o -> next)
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
376 lw_expr_destroy(o -> p);
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
377 if (E -> type == lw_expr_type_var)
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
378 lw_free(E -> value2);
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
379 *E = *te;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
380 E -> operands = NULL;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
381
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
382 if (te -> type == lw_expr_type_var)
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
383 E -> value2 = lw_strdup(te -> value2);
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
384 for (o = te -> operands; o; o = o -> next)
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
385 {
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
386 lw_expr_add_operand(E, lw_expr_copy(o -> p));
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
387 }
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
388 lw_expr_destroy(te);
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
389 goto again;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
390 }
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
391 return;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
392 }
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
393
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
394 if (E -> type == lw_expr_type_var && evaluate_var)
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
395 {
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
396 lw_expr_t te;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
397
346
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
398 te = evaluate_var(E -> value2, priv);
337
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
399 if (te)
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
400 {
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
401 for (o = E -> operands; o; o = o -> next)
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
402 lw_expr_destroy(o -> p);
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
403 if (E -> type == lw_expr_type_var)
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
404 lw_free(E -> value2);
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
405 *E = *te;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
406 E -> operands = NULL;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
407
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
408 if (te -> type == lw_expr_type_var)
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
409 E -> value2 = lw_strdup(te -> value2);
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
410 for (o = te -> operands; o; o = o -> next)
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
411 {
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
412 lw_expr_add_operand(E, lw_expr_copy(o -> p));
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
413 }
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
414 lw_expr_destroy(te);
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
415 goto again;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
416 }
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
417 return;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
418 }
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
419
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
420 // non-operators have no simplification to do!
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
421 if (E -> type != lw_expr_type_oper)
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
422 return;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
423
335
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
424 // sort "constants" to the start of each operand list for + and *
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
425 lw_expr_simplify_sortconstfirst(E);
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
426
337
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
427 // simplify operands
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
428 for (o = E -> operands; o; o = o -> next)
346
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
429 lw_expr_simplify(o -> p, priv);
337
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
430
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
431 for (o = E -> operands; o; o = o -> next)
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
432 {
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
433 if (o -> p -> type != lw_expr_type_int)
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
434 break;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
435 }
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
436
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
437 if (!o)
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
438 {
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
439 // we can do the operation here!
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
440 int tr = -42424242;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
441
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
442 switch (E -> value)
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
443 {
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
444 case lw_expr_oper_neg:
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
445 tr = -(E -> operands -> p -> value);
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
446 break;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
447
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
448 case lw_expr_oper_com:
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
449 tr = ~(E -> operands -> 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_plus:
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_minus:
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_times:
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
465 tr = E -> operands -> p -> value;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
466 for (o = E -> operands -> next; o; o = o -> next)
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
467 tr *= o -> p -> value;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
468 break;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
469
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
470 case lw_expr_oper_divide:
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
471 tr = E -> operands -> p -> value / E -> operands -> next -> p -> value;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
472 break;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
473
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
474 case lw_expr_oper_mod:
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
475 tr = E -> operands -> p -> value % E -> operands -> next -> p -> value;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
476 break;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
477
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
478 case lw_expr_oper_intdiv:
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
479 tr = E -> operands -> p -> value / E -> operands -> next -> p -> value;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
480 break;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
481
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
482 case lw_expr_oper_bwand:
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
483 tr = E -> operands -> p -> value & E -> operands -> next -> p -> value;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
484 break;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
485
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
486 case lw_expr_oper_bwor:
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
487 tr = E -> operands -> p -> value | E -> operands -> next -> p -> value;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
488 break;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
489
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
490 case lw_expr_oper_bwxor:
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
491 tr = E -> operands -> p -> value ^ E -> operands -> next -> p -> value;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
492 break;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
493
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
494 case lw_expr_oper_and:
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
495 tr = E -> operands -> p -> value && E -> operands -> next -> p -> value;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
496 break;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
497
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
498 case lw_expr_oper_or:
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
499 tr = E -> operands -> p -> value || E -> operands -> next -> p -> value;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
500 break;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
501
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
502 }
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
503
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
504 while (E -> operands)
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
505 {
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
506 o = E -> operands;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
507 E -> operands = o -> next;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
508 lw_expr_destroy(o -> p);
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
509 lw_free(o);
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
510 }
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
511 E -> type = lw_expr_type_int;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
512 E -> value = tr;
335
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
513 return;
337
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
514 }
335
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
515
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
516 if (E -> value == lw_expr_oper_times)
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
517 {
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
518 for (o = E -> operands; o; o = o -> next)
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
519 {
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
520 if (o -> p -> type == lw_expr_type_int && o -> p -> value == 0)
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
521 {
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
522 // one operand of times is 0, replace operation with 0
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
523 while (E -> operands)
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
524 {
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
525 o = E -> operands;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
526 E -> operands = o -> next;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
527 lw_expr_destroy(o -> p);
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
528 lw_free(o);
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
529 }
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
530 E -> type = lw_expr_type_int;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
531 E -> value = 0;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
532 return;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
533 }
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
534 }
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
535 }
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
536
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
537 // look for like terms and collect them together
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
538 if (E -> value == lw_expr_oper_plus)
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
539 {
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
540 for (o = E -> operands; o; o = o -> next)
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
541 {
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
542 if (o -> p -> type == lw_expr_type_oper && o -> p -> value == lw_expr_oper_times)
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
543 {
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
544 // we have a "times" here
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
545 // find first non-const operand
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
546 struct lw_expr_opers *op1, *op2, *o2;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
547 for (op1 = o -> p -> operands; op1; op1 = op1 -> next)
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
548 if (op1 -> p -> type != lw_expr_type_int)
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
549 break;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
550
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
551 for (o2 = o -> next; o2; o2 = o2 -> next)
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
552 {
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
553 if (o2 -> p -> type == lw_expr_type_oper && o2 -> p -> value == lw_expr_oper_times)
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
554 {
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
555 // another "times"
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
556 for (op2 = o2 -> p -> operands; op2; op2 = op2 -> next)
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
557 if (op2 -> p -> type != lw_expr_type_int)
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
558 break;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
559
336
401587ab6a09 checkpoint
lost
parents: 335
diff changeset
560 if (lw_expr_simplify_compareoperandlist(&op1, &op2))
335
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
561 {
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
562 // we have like terms here
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
563 // do something about it
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
564 }
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
565 }
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
566 }
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
567 }
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
568 }
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
569 }
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
570
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
571
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
572 if (E -> value == lw_expr_oper_plus)
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
573 {
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
574 int c = 0, t = 0;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
575 for (o = E -> operands; o; o = o -> next)
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
576 {
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
577 t++;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
578 if (!(o -> p -> type == lw_expr_type_int && o -> p -> value == 0))
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
579 {
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
580 c++;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
581 }
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
582 }
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
583 if (c == 1)
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
584 {
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
585 lw_expr_t r;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
586 // find the value and "move it up"
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
587 while (E -> operands)
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
588 {
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
589 o = E -> operands;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
590 if (o -> p -> type != lw_expr_type_int || o -> p -> value != 0)
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
591 {
337
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
592 r = lw_expr_copy(o -> p);
335
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
593 }
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
594 E -> operands = o -> next;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
595 lw_expr_destroy(o -> p);
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
596 lw_free(o);
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
597 }
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
598 *E = *r;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
599 return;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
600 }
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
601 else if (c == 0)
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
602 {
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
603 // replace with 0
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
604 while (E -> operands)
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
605 {
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
606 o = E -> operands;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
607 E -> operands = o -> next;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
608 lw_expr_destroy(o -> p);
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
609 lw_free(o);
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
610 }
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
611 E -> type = lw_expr_type_int;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
612 E -> value = 0;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
613 return;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
614 }
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
615 else if (c != t)
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
616 {
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
617 // collapse out zero terms
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
618 struct lw_expr_opers *o2;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
619
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
620 for (o = E -> operands; o; o = o -> next)
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
621 {
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
622 if (o -> p -> type == lw_expr_type_int && o -> p -> value == 0)
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
623 {
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
624 if (o == E -> operands)
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
625 {
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
626 E -> operands = o -> next;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
627 lw_expr_destroy(o -> p);
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
628 lw_free(o);
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
629 o = E -> operands;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
630 }
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
631 else
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
632 {
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
633 for (o2 = E -> operands; o2 -> next == o; o2 = o2 -> next)
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
634 /* do nothing */ ;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
635 o2 -> next = o -> next;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
636 lw_expr_destroy(o -> p);
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
637 lw_free(o);
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
638 o = o2;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
639 }
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
640 }
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
641 }
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
642 }
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
643 return;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
644 }
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
645 }
346
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
646
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
647 /*
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
648
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
649 The following two functions are co-routines which evaluate an infix
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
650 expression. lw_expr_parse_term checks for unary prefix operators then, if
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
651 none found, passes the string off the the defined helper function to
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
652 determine what the term really is. It also handles parentheses.
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
653
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
654 lw_expr_parse_expr evaluates actual expressions with infix operators. It
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
655 respects the order of operations.
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
656
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
657 The end of an expression is determined by the presence of any of the
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
658 following conditions:
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
659
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
660 1. a NUL character
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
661 2. a whitespace character
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
662 3. a )
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
663 4. a ,
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
664 5. any character that is not recognized as a term
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
665
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
666 lw_expr_parse_term returns NULL if there is no term (end of expr, etc.)
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
667
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
668 lw_expr_parse_expr returns NULL if there is no expression or on a syntax
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
669 error.
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
670
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
671 */
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
672
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
673 lw_expr_t lw_expr_parse_expr(char **p, void *priv, int prec);
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
674
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
675 lw_expr_t lw_expr_parse_term(char **p, void *priv)
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
676 {
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
677 lw_expr_t term, term2;
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
678
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
679 eval_next:
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
680 if (!**p || isspace(**p) || **p == ')' || **p == ']')
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
681 return NULL;
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
682
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
683 // parentheses
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
684 if (**p == '(')
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
685 {
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
686 (*p)++;
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
687 term = lw_expr_parse_expr(p, priv, 0);
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
688 if (**p != ')')
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
689 {
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
690 lw_expr_destroy(term);
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
691 return NULL;
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
692 }
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
693 (*p)++;
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
694 return term;
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
695 }
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
696
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
697 // unary +
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
698 if (**p == '+')
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
699 {
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
700 (*p)++;
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
701 goto eval_next;
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
702 }
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
703
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
704 // unary - (prec 200)
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
705 if (**p == '-')
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
706 {
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
707 (*p)++;
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
708 term = lw_expr_parse_expr(p, priv, 200);
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
709 if (!term)
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
710 return NULL;
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
711
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
712 term2 = lw_expr_build(lw_expr_type_oper, lw_expr_oper_neg, term);
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
713 lw_expr_destroy(term);
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
714 return term2;
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
715 }
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
716
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
717 // unary ^ or ~ (complement, prec 200)
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
718 if (**p == '^' || **p == '~')
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
719 {
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
720 (*p)++;
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
721 term = lw_expr_parse_expr(p, priv, 200);
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
722 if (!term)
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
723 return NULL;
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
724
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
725 term2 = lw_expr_build(lw_expr_type_oper, lw_expr_oper_com, term);
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
726 lw_expr_destroy(term);
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
727 return term2;
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
728 }
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
729
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
730 // non-operator - pass to caller
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
731 return parse_term(p, priv);
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
732 }
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
733
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
734 lw_expr_t lw_expr_parse_expr(char **p, void *priv, int prec)
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
735 {
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
736 static const struct operinfo
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
737 {
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
738 int opernum;
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
739 char *operstr;
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
740 int operprec;
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
741 } operators[] =
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
742 {
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
743 { lw_expr_oper_plus, "+", 100 },
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
744 { lw_expr_oper_minus, "-", 100 },
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
745 { lw_expr_oper_times, "*", 100 },
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
746 { lw_expr_oper_divide, "/", 150 },
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
747 { lw_expr_oper_mod, "%", 150 },
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
748 { lw_expr_oper_intdiv, "\\", 150 },
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
749
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
750 { lw_expr_oper_and, "&&", 25 },
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
751 { lw_expr_oper_or, "||", 25 },
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
752
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
753 { lw_expr_oper_bwand, "&", 50 },
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
754 { lw_expr_oper_bwor, "|", 50 },
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
755 { lw_expr_oper_bwxor, "^", 50 },
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
756
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
757 { lw_expr_oper_none, "", 0 }
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
758 };
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
759
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
760 int opern, i;
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
761 lw_expr_t term1, term2, term3;
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
762
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
763 if (!**p || isspace(**p) || **p == ')' || **p == ',' || **p == ']')
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
764 return NULL;
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
765
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
766 term1 = lw_expr_parse_term(p, priv);
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
767 if (!term1)
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
768 return NULL;
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
769
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
770 eval_next:
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
771 if (!**p || isspace(**p) || **p == ')' || **p == ',' || **p == ']')
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
772 return term1;
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
773
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
774 // expecting an operator here
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
775 for (opern = 0; operators[opern].opernum != lw_expr_oper_none; opern++)
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
776 {
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
777 for (i = 0; (*p)[i] && operators[opern].operstr[i] && ((*p)[i] == operators[opern].operstr[i]); i++)
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
778 /* do nothing */;
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
779 if (operators[opern].operstr[i] == '\0')
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
780 break;
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
781 }
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
782
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
783 if (operators[opern].opernum == lw_expr_oper_none)
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
784 {
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
785 // unrecognized operator
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
786 lw_expr_destroy(term1);
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
787 return NULL;
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
788 }
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
789
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
790 // operator number is in opern, length of oper in i
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
791
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
792 // logic:
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
793 // if the precedence of this operation is <= to the "prec" flag,
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
794 // we simply return without advancing the input pointer; the operator
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
795 // will be evaluated again in the enclosing function call
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
796 if (operators[opern].operprec <= prec)
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
797 return term1;
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
798
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
799 // logic:
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
800 // we have a higher precedence operator here so we will advance the
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
801 // input pointer to the next term and let the expression evaluator
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
802 // loose on it after which time we will push our operator onto the
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
803 // stack and then go on with the expression evaluation
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
804 (*p) += i;
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
805
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
806 // evaluate next expression(s) of higher precedence
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
807 term2 = lw_expr_parse_expr(p, priv, operators[opern].operprec);
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
808 if (!term2)
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
809 {
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
810 lw_expr_destroy(term1);
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
811 return NULL;
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
812 }
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
813
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
814 // now create operator
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
815 term3 = lw_expr_build(lw_expr_type_oper, operators[opern].opernum, term1, term2);
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
816 lw_expr_destroy(term1);
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
817 lw_expr_destroy(term2);
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
818
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
819 // the new "expression" is the next "left operand"
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
820 term1 = term3;
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
821
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
822 // continue evaluating
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
823 goto eval_next;
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
824 }
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
825
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
826 lw_expr_t lw_expr_parse(char **p, void *priv)
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
827 {
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
828 return lw_expr_parse_expr(p, priv, 0);
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
829 }