annotate lwlib/lw_expr.c @ 371:9c24d9d485b9

Much bugfixing
author lost@starbug
date Wed, 21 Apr 2010 23:29:18 -0600
parents 6b33faa21a0a
children 90de73ba0cac
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
347
1649bc7bda5a Some data oriented pseudo ops added
lost@starbug
parents: 346
diff changeset
38 int lw_expr_istype(lw_expr_t e, int t)
1649bc7bda5a Some data oriented pseudo ops added
lost@starbug
parents: 346
diff changeset
39 {
1649bc7bda5a Some data oriented pseudo ops added
lost@starbug
parents: 346
diff changeset
40 if (e -> type == t)
1649bc7bda5a Some data oriented pseudo ops added
lost@starbug
parents: 346
diff changeset
41 return 1;
1649bc7bda5a Some data oriented pseudo ops added
lost@starbug
parents: 346
diff changeset
42 return 0;
1649bc7bda5a Some data oriented pseudo ops added
lost@starbug
parents: 346
diff changeset
43 }
1649bc7bda5a Some data oriented pseudo ops added
lost@starbug
parents: 346
diff changeset
44
1649bc7bda5a Some data oriented pseudo ops added
lost@starbug
parents: 346
diff changeset
45 int lw_expr_intval(lw_expr_t e)
1649bc7bda5a Some data oriented pseudo ops added
lost@starbug
parents: 346
diff changeset
46 {
1649bc7bda5a Some data oriented pseudo ops added
lost@starbug
parents: 346
diff changeset
47 if (e -> type == lw_expr_type_int)
1649bc7bda5a Some data oriented pseudo ops added
lost@starbug
parents: 346
diff changeset
48 return e -> value;
1649bc7bda5a Some data oriented pseudo ops added
lost@starbug
parents: 346
diff changeset
49 return -1;
1649bc7bda5a Some data oriented pseudo ops added
lost@starbug
parents: 346
diff changeset
50 }
1649bc7bda5a Some data oriented pseudo ops added
lost@starbug
parents: 346
diff changeset
51
367
34dfc9747f23 Reduction passes complete
lost@starbug
parents: 347
diff changeset
52 int lw_expr_specint(lw_expr_t e)
34dfc9747f23 Reduction passes complete
lost@starbug
parents: 347
diff changeset
53 {
34dfc9747f23 Reduction passes complete
lost@starbug
parents: 347
diff changeset
54 if (e -> type == lw_expr_type_special)
34dfc9747f23 Reduction passes complete
lost@starbug
parents: 347
diff changeset
55 return e -> value;
34dfc9747f23 Reduction passes complete
lost@starbug
parents: 347
diff changeset
56 return -1;
34dfc9747f23 Reduction passes complete
lost@starbug
parents: 347
diff changeset
57 }
34dfc9747f23 Reduction passes complete
lost@starbug
parents: 347
diff changeset
58
346
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
59 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
60 {
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
61 parse_term = fn;
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
62 }
337
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
63
342
7b4123dce741 Added basic symbol registration
lost@starbug
parents: 337
diff changeset
64 void lw_expr_set_special_handler(lw_expr_fn_t *fn)
337
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
65 {
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
66 evaluate_special = fn;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
67 }
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
68
342
7b4123dce741 Added basic symbol registration
lost@starbug
parents: 337
diff changeset
69 void lw_expr_set_var_handler(lw_expr_fn2_t *fn)
337
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
70 {
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
71 evaluate_var = fn;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
72 }
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
73
334
f2173d18c73f Checkpoint
lost
parents:
diff changeset
74 lw_expr_t lw_expr_create(void)
f2173d18c73f Checkpoint
lost
parents:
diff changeset
75 {
f2173d18c73f Checkpoint
lost
parents:
diff changeset
76 lw_expr_t r;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
77
f2173d18c73f Checkpoint
lost
parents:
diff changeset
78 r = lw_alloc(sizeof(struct lw_expr_priv));
f2173d18c73f Checkpoint
lost
parents:
diff changeset
79 r -> operands = NULL;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
80
f2173d18c73f Checkpoint
lost
parents:
diff changeset
81 return r;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
82 }
f2173d18c73f Checkpoint
lost
parents:
diff changeset
83
f2173d18c73f Checkpoint
lost
parents:
diff changeset
84 void lw_expr_destroy(lw_expr_t E)
f2173d18c73f Checkpoint
lost
parents:
diff changeset
85 {
337
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
86 struct lw_expr_opers *o;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
87 for (o = E -> operands; o; o = o -> next)
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
88 lw_expr_destroy(o -> p);
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
89 if (E -> type == lw_expr_type_var)
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
90 lw_free(E -> value2);
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
91 lw_free(E);
334
f2173d18c73f Checkpoint
lost
parents:
diff changeset
92 }
f2173d18c73f Checkpoint
lost
parents:
diff changeset
93
337
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
94 /* actually duplicates the entire expression */
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
95 void lw_expr_add_operand(lw_expr_t E, lw_expr_t O);
334
f2173d18c73f Checkpoint
lost
parents:
diff changeset
96 lw_expr_t lw_expr_copy(lw_expr_t E)
f2173d18c73f Checkpoint
lost
parents:
diff changeset
97 {
337
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
98 lw_expr_t r, t;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
99 struct lw_expr_opers *o;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
100
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
101 r = lw_alloc(sizeof(struct lw_expr_priv));
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
102 *r = *E;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
103 r -> operands = NULL;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
104
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
105 if (E -> type == lw_expr_type_var)
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
106 r -> value2 = lw_strdup(E -> value2);
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
107 for (o = E -> operands; o; o = o -> next)
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
108 {
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
109 lw_expr_add_operand(r, lw_expr_copy(o -> p));
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
110 }
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
111
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
112 return r;
334
f2173d18c73f Checkpoint
lost
parents:
diff changeset
113 }
f2173d18c73f Checkpoint
lost
parents:
diff changeset
114
f2173d18c73f Checkpoint
lost
parents:
diff changeset
115 void lw_expr_add_operand(lw_expr_t E, lw_expr_t O)
f2173d18c73f Checkpoint
lost
parents:
diff changeset
116 {
f2173d18c73f Checkpoint
lost
parents:
diff changeset
117 struct lw_expr_opers *o, *t;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
118
f2173d18c73f Checkpoint
lost
parents:
diff changeset
119 o = lw_alloc(sizeof(struct lw_expr_opers));
f2173d18c73f Checkpoint
lost
parents:
diff changeset
120 o -> p = lw_expr_copy(O);
f2173d18c73f Checkpoint
lost
parents:
diff changeset
121 o -> next = NULL;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
122 for (t = E -> operands; t && t -> next; t = t -> next)
f2173d18c73f Checkpoint
lost
parents:
diff changeset
123 /* do nothing */ ;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
124
f2173d18c73f Checkpoint
lost
parents:
diff changeset
125 if (t)
f2173d18c73f Checkpoint
lost
parents:
diff changeset
126 t -> next = o;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
127 else
f2173d18c73f Checkpoint
lost
parents:
diff changeset
128 E -> operands = o;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
129 }
f2173d18c73f Checkpoint
lost
parents:
diff changeset
130
335
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
131 lw_expr_t lw_expr_build_aux(int exprtype, va_list args)
334
f2173d18c73f Checkpoint
lost
parents:
diff changeset
132 {
f2173d18c73f Checkpoint
lost
parents:
diff changeset
133 lw_expr_t r;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
134 int t;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
135 void *p;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
136
f2173d18c73f Checkpoint
lost
parents:
diff changeset
137 lw_expr_t te1, te2;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
138
f2173d18c73f Checkpoint
lost
parents:
diff changeset
139 r = lw_expr_create();
f2173d18c73f Checkpoint
lost
parents:
diff changeset
140
f2173d18c73f Checkpoint
lost
parents:
diff changeset
141 switch (exprtype)
f2173d18c73f Checkpoint
lost
parents:
diff changeset
142 {
f2173d18c73f Checkpoint
lost
parents:
diff changeset
143 case lw_expr_type_int:
f2173d18c73f Checkpoint
lost
parents:
diff changeset
144 t = va_arg(args, int);
f2173d18c73f Checkpoint
lost
parents:
diff changeset
145 r -> type = lw_expr_type_int;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
146 r -> value = t;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
147 break;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
148
f2173d18c73f Checkpoint
lost
parents:
diff changeset
149 case lw_expr_type_var:
f2173d18c73f Checkpoint
lost
parents:
diff changeset
150 p = va_arg(args, char *);
f2173d18c73f Checkpoint
lost
parents:
diff changeset
151 r -> type = lw_expr_type_var;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
152 r -> value2 = lw_strdup(p);
f2173d18c73f Checkpoint
lost
parents:
diff changeset
153 break;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
154
f2173d18c73f Checkpoint
lost
parents:
diff changeset
155 case lw_expr_type_special:
f2173d18c73f Checkpoint
lost
parents:
diff changeset
156 t = va_arg(args, int);
f2173d18c73f Checkpoint
lost
parents:
diff changeset
157 p = va_arg(args, char *);
f2173d18c73f Checkpoint
lost
parents:
diff changeset
158 r -> type = lw_expr_type_special;
337
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
159 r -> value = t;
334
f2173d18c73f Checkpoint
lost
parents:
diff changeset
160 r -> value2 = p;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
161 break;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
162
f2173d18c73f Checkpoint
lost
parents:
diff changeset
163 case lw_expr_type_oper:
f2173d18c73f Checkpoint
lost
parents:
diff changeset
164 t = va_arg(args, int);
f2173d18c73f Checkpoint
lost
parents:
diff changeset
165 te1 = va_arg(args, lw_expr_t);
f2173d18c73f Checkpoint
lost
parents:
diff changeset
166 if (t != lw_expr_oper_com && t != lw_expr_oper_neg)
f2173d18c73f Checkpoint
lost
parents:
diff changeset
167 te2 = va_arg(args, lw_expr_t);
f2173d18c73f Checkpoint
lost
parents:
diff changeset
168 else
f2173d18c73f Checkpoint
lost
parents:
diff changeset
169 te2 = NULL;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
170
f2173d18c73f Checkpoint
lost
parents:
diff changeset
171 r -> type = lw_expr_type_oper;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
172 r -> value = t;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
173 lw_expr_add_operand(r, te1);
371
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
174 if (te2)
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
175 lw_expr_add_operand(r, te2);
334
f2173d18c73f Checkpoint
lost
parents:
diff changeset
176 break;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
177
f2173d18c73f Checkpoint
lost
parents:
diff changeset
178 default:
f2173d18c73f Checkpoint
lost
parents:
diff changeset
179 lw_error("Invalid expression type specified to lw_expr_build");
f2173d18c73f Checkpoint
lost
parents:
diff changeset
180 }
f2173d18c73f Checkpoint
lost
parents:
diff changeset
181
335
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
182 return r;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
183 }
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
184
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
185 lw_expr_t lw_expr_build(int exprtype, ...)
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
186 {
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
187 va_list args;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
188 lw_expr_t r;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
189
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
190 va_start(args, exprtype);
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
191 r = lw_expr_build_aux(exprtype, args);
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
192 va_end(args);
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
193 return r;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
194 }
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
195
370
6b33faa21a0a Debugging output and bugfixing pass 0
lost@starbug
parents: 367
diff changeset
196 void lw_expr_print(lw_expr_t E, FILE *fp)
334
f2173d18c73f Checkpoint
lost
parents:
diff changeset
197 {
f2173d18c73f Checkpoint
lost
parents:
diff changeset
198 struct lw_expr_opers *o;
335
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
199 int c = 0;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
200
334
f2173d18c73f Checkpoint
lost
parents:
diff changeset
201 for (o = E -> operands; o; o = o -> next)
f2173d18c73f Checkpoint
lost
parents:
diff changeset
202 {
335
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
203 c++;
370
6b33faa21a0a Debugging output and bugfixing pass 0
lost@starbug
parents: 367
diff changeset
204 lw_expr_print(o -> p, fp);
334
f2173d18c73f Checkpoint
lost
parents:
diff changeset
205 }
f2173d18c73f Checkpoint
lost
parents:
diff changeset
206
f2173d18c73f Checkpoint
lost
parents:
diff changeset
207 switch (E -> type)
f2173d18c73f Checkpoint
lost
parents:
diff changeset
208 {
f2173d18c73f Checkpoint
lost
parents:
diff changeset
209 case lw_expr_type_int:
371
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
210 if (E -> value < 0)
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
211 fprintf(fp, "-%#x ", -(E -> value));
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
212 else
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
213 fprintf(fp, "%#x ", E -> value);
334
f2173d18c73f Checkpoint
lost
parents:
diff changeset
214 break;
335
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
215
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
216 case lw_expr_type_var:
370
6b33faa21a0a Debugging output and bugfixing pass 0
lost@starbug
parents: 367
diff changeset
217 fprintf(fp, "V(%s) ", (char *)(E -> value2));
335
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
218 break;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
219
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
220 case lw_expr_type_special:
370
6b33faa21a0a Debugging output and bugfixing pass 0
lost@starbug
parents: 367
diff changeset
221 fprintf(fp, "S(%d,%p) ", E -> value, E -> value2);
335
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
222 break;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
223
334
f2173d18c73f Checkpoint
lost
parents:
diff changeset
224 case lw_expr_type_oper:
370
6b33faa21a0a Debugging output and bugfixing pass 0
lost@starbug
parents: 367
diff changeset
225 fprintf(fp, "[%d]", c);
334
f2173d18c73f Checkpoint
lost
parents:
diff changeset
226 switch (E -> value)
f2173d18c73f Checkpoint
lost
parents:
diff changeset
227 {
f2173d18c73f Checkpoint
lost
parents:
diff changeset
228 case lw_expr_oper_plus:
370
6b33faa21a0a Debugging output and bugfixing pass 0
lost@starbug
parents: 367
diff changeset
229 fprintf(fp, "+ ");
334
f2173d18c73f Checkpoint
lost
parents:
diff changeset
230 break;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
231
f2173d18c73f Checkpoint
lost
parents:
diff changeset
232 case lw_expr_oper_minus:
370
6b33faa21a0a Debugging output and bugfixing pass 0
lost@starbug
parents: 367
diff changeset
233 fprintf(fp, "- ");
334
f2173d18c73f Checkpoint
lost
parents:
diff changeset
234 break;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
235
f2173d18c73f Checkpoint
lost
parents:
diff changeset
236 case lw_expr_oper_times:
370
6b33faa21a0a Debugging output and bugfixing pass 0
lost@starbug
parents: 367
diff changeset
237 fprintf(fp, "* ");
334
f2173d18c73f Checkpoint
lost
parents:
diff changeset
238 break;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
239
f2173d18c73f Checkpoint
lost
parents:
diff changeset
240 case lw_expr_oper_divide:
370
6b33faa21a0a Debugging output and bugfixing pass 0
lost@starbug
parents: 367
diff changeset
241 fprintf(fp, "/ ");
334
f2173d18c73f Checkpoint
lost
parents:
diff changeset
242 break;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
243
f2173d18c73f Checkpoint
lost
parents:
diff changeset
244 case lw_expr_oper_mod:
370
6b33faa21a0a Debugging output and bugfixing pass 0
lost@starbug
parents: 367
diff changeset
245 fprintf(fp, "%% ");
334
f2173d18c73f Checkpoint
lost
parents:
diff changeset
246 break;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
247
f2173d18c73f Checkpoint
lost
parents:
diff changeset
248 case lw_expr_oper_intdiv:
370
6b33faa21a0a Debugging output and bugfixing pass 0
lost@starbug
parents: 367
diff changeset
249 fprintf(fp, "\\ ");
334
f2173d18c73f Checkpoint
lost
parents:
diff changeset
250 break;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
251
f2173d18c73f Checkpoint
lost
parents:
diff changeset
252 case lw_expr_oper_bwand:
370
6b33faa21a0a Debugging output and bugfixing pass 0
lost@starbug
parents: 367
diff changeset
253 fprintf(fp, "BWAND ");
334
f2173d18c73f Checkpoint
lost
parents:
diff changeset
254 break;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
255
f2173d18c73f Checkpoint
lost
parents:
diff changeset
256 case lw_expr_oper_bwor:
370
6b33faa21a0a Debugging output and bugfixing pass 0
lost@starbug
parents: 367
diff changeset
257 fprintf(fp, "BWOR ");
334
f2173d18c73f Checkpoint
lost
parents:
diff changeset
258 break;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
259
f2173d18c73f Checkpoint
lost
parents:
diff changeset
260 case lw_expr_oper_bwxor:
370
6b33faa21a0a Debugging output and bugfixing pass 0
lost@starbug
parents: 367
diff changeset
261 fprintf(fp, "BWXOR ");
334
f2173d18c73f Checkpoint
lost
parents:
diff changeset
262 break;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
263
f2173d18c73f Checkpoint
lost
parents:
diff changeset
264 case lw_expr_oper_and:
370
6b33faa21a0a Debugging output and bugfixing pass 0
lost@starbug
parents: 367
diff changeset
265 fprintf(fp, "AND ");
334
f2173d18c73f Checkpoint
lost
parents:
diff changeset
266 break;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
267
f2173d18c73f Checkpoint
lost
parents:
diff changeset
268 case lw_expr_oper_or:
370
6b33faa21a0a Debugging output and bugfixing pass 0
lost@starbug
parents: 367
diff changeset
269 fprintf(fp, "OR ");
334
f2173d18c73f Checkpoint
lost
parents:
diff changeset
270 break;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
271
f2173d18c73f Checkpoint
lost
parents:
diff changeset
272 case lw_expr_oper_neg:
370
6b33faa21a0a Debugging output and bugfixing pass 0
lost@starbug
parents: 367
diff changeset
273 fprintf(fp, "NEG ");
334
f2173d18c73f Checkpoint
lost
parents:
diff changeset
274 break;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
275
f2173d18c73f Checkpoint
lost
parents:
diff changeset
276 case lw_expr_oper_com:
370
6b33faa21a0a Debugging output and bugfixing pass 0
lost@starbug
parents: 367
diff changeset
277 fprintf(fp, "COM ");
334
f2173d18c73f Checkpoint
lost
parents:
diff changeset
278 break;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
279
f2173d18c73f Checkpoint
lost
parents:
diff changeset
280 default:
370
6b33faa21a0a Debugging output and bugfixing pass 0
lost@starbug
parents: 367
diff changeset
281 fprintf(fp, "OPER ");
334
f2173d18c73f Checkpoint
lost
parents:
diff changeset
282 break;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
283 }
f2173d18c73f Checkpoint
lost
parents:
diff changeset
284 break;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
285 default:
370
6b33faa21a0a Debugging output and bugfixing pass 0
lost@starbug
parents: 367
diff changeset
286 fprintf(fp, "ERR ");
334
f2173d18c73f Checkpoint
lost
parents:
diff changeset
287 break;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
288 }
f2173d18c73f Checkpoint
lost
parents:
diff changeset
289 }
f2173d18c73f Checkpoint
lost
parents:
diff changeset
290
f2173d18c73f Checkpoint
lost
parents:
diff changeset
291 /*
f2173d18c73f Checkpoint
lost
parents:
diff changeset
292 Return:
f2173d18c73f Checkpoint
lost
parents:
diff changeset
293 nonzero if expressions are the same (identical pointers or matching values)
f2173d18c73f Checkpoint
lost
parents:
diff changeset
294 zero if expressions are not the same
f2173d18c73f Checkpoint
lost
parents:
diff changeset
295
f2173d18c73f Checkpoint
lost
parents:
diff changeset
296 */
f2173d18c73f Checkpoint
lost
parents:
diff changeset
297 int lw_expr_compare(lw_expr_t E1, lw_expr_t E2)
f2173d18c73f Checkpoint
lost
parents:
diff changeset
298 {
f2173d18c73f Checkpoint
lost
parents:
diff changeset
299 struct lw_expr_opers *o1, *o2;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
300
f2173d18c73f Checkpoint
lost
parents:
diff changeset
301 if (E1 == E2)
f2173d18c73f Checkpoint
lost
parents:
diff changeset
302 return 1;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
303
f2173d18c73f Checkpoint
lost
parents:
diff changeset
304 if (!(E1 -> type == E2 -> type && E1 -> value == E2 -> value))
f2173d18c73f Checkpoint
lost
parents:
diff changeset
305 return 0;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
306
f2173d18c73f Checkpoint
lost
parents:
diff changeset
307 if (E1 -> type == lw_expr_type_var)
f2173d18c73f Checkpoint
lost
parents:
diff changeset
308 {
f2173d18c73f Checkpoint
lost
parents:
diff changeset
309 if (!strcmp(E1 -> value2, E2 -> value2))
f2173d18c73f Checkpoint
lost
parents:
diff changeset
310 return 1;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
311 else
f2173d18c73f Checkpoint
lost
parents:
diff changeset
312 return 0;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
313 }
f2173d18c73f Checkpoint
lost
parents:
diff changeset
314
f2173d18c73f Checkpoint
lost
parents:
diff changeset
315 if (E1 -> type == lw_expr_type_special)
f2173d18c73f Checkpoint
lost
parents:
diff changeset
316 {
f2173d18c73f Checkpoint
lost
parents:
diff changeset
317 if (E1 -> value2 == E2 -> value2)
f2173d18c73f Checkpoint
lost
parents:
diff changeset
318 return 1;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
319 else
f2173d18c73f Checkpoint
lost
parents:
diff changeset
320 return 0;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
321 }
f2173d18c73f Checkpoint
lost
parents:
diff changeset
322
f2173d18c73f Checkpoint
lost
parents:
diff changeset
323 for (o1 = E1 -> operands, o2 = E2 -> operands; o1 && o2; o1 = o1 -> next, o2 = o2 -> next)
f2173d18c73f Checkpoint
lost
parents:
diff changeset
324 if (lw_expr_compare(o1 -> p, o2 -> p) == 0)
f2173d18c73f Checkpoint
lost
parents:
diff changeset
325 return 0;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
326 if (o1 || o2)
f2173d18c73f Checkpoint
lost
parents:
diff changeset
327 return 0;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
328
f2173d18c73f Checkpoint
lost
parents:
diff changeset
329 return 1;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
330 }
f2173d18c73f Checkpoint
lost
parents:
diff changeset
331
f2173d18c73f Checkpoint
lost
parents:
diff changeset
332 /* return true if E is an operator of type oper */
f2173d18c73f Checkpoint
lost
parents:
diff changeset
333 int lw_expr_isoper(lw_expr_t E, int oper)
f2173d18c73f Checkpoint
lost
parents:
diff changeset
334 {
f2173d18c73f Checkpoint
lost
parents:
diff changeset
335 if (E -> type == lw_expr_type_oper && E -> value == oper)
f2173d18c73f Checkpoint
lost
parents:
diff changeset
336 return 1;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
337 return 0;
f2173d18c73f Checkpoint
lost
parents:
diff changeset
338 }
335
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
339
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
340
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
341 void lw_expr_simplify_sortconstfirst(lw_expr_t E)
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
342 {
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
343 struct lw_expr_opers *o;
371
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
344
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
345 if (E -> type != lw_expr_type_oper)
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
346 return;
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
347 if (E -> value != lw_expr_oper_times && E -> value != lw_expr_oper_plus)
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
348 return;
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
349
335
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
350 for (o = E -> operands; o; o = o -> next)
371
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
351 {
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
352 if (o -> p -> type == lw_expr_type_oper && (o -> p -> value == lw_expr_oper_times || o -> p -> value == lw_expr_oper_plus))
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
353 lw_expr_simplify_sortconstfirst(o -> p);
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
354 }
335
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
355
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
356 for (o = E -> operands; o; o = o -> next)
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
357 {
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
358 if (o -> p -> type == lw_expr_type_int && o != E -> operands)
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
359 {
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
360 struct lw_expr_opers *o2;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
361 for (o2 = E -> operands; o2 -> next != o; o2 = o2 -> next)
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
362 /* do nothing */ ;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
363 o2 -> next = o -> next;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
364 o -> next = E -> operands;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
365 E -> operands = o;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
366 o = o2;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
367 }
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
368 }
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
369 }
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
370
336
401587ab6a09 checkpoint
lost
parents: 335
diff changeset
371 void lw_expr_sortoperandlist(struct lw_expr_opers **o)
401587ab6a09 checkpoint
lost
parents: 335
diff changeset
372 {
401587ab6a09 checkpoint
lost
parents: 335
diff changeset
373 fprintf(stderr, "lw_expr_sortoperandlist() not yet implemented\n");
401587ab6a09 checkpoint
lost
parents: 335
diff changeset
374 }
401587ab6a09 checkpoint
lost
parents: 335
diff changeset
375
335
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
376 // return 1 if the operand lists match, 0 if not
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
377 // may re-order the argument lists
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
378 int lw_expr_simplify_compareoperandlist(struct lw_expr_opers **ol1, struct lw_expr_opers **ol2)
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
379 {
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
380 struct lw_expr_opers *o1, *o2;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
381
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
382 lw_expr_sortoperandlist(ol1);
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
383 lw_expr_sortoperandlist(ol2);
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
384
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
385 for (o1 = *ol1, o2 = *ol2; o1 && o2; o1 = o1 -> next, o2 = o2 -> next)
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
386 {
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
387 if (!lw_expr_compare(o1 -> p, o2 -> p))
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
388 return 0;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
389 }
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
390 if (o1 || o2)
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
391 return 0;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
392 return 1;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
393 }
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
394
371
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
395 int lw_expr_simplify_isliketerm(lw_expr_t e1, lw_expr_t e2)
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
396 {
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
397 fprintf(stderr, "isliketerm in: ");
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
398 lw_expr_print(e1, stderr);
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
399 fprintf(stderr, "; ");
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
400 lw_expr_print(e2, stderr);
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
401 fprintf(stderr, "\n");
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
402
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
403 // first term is a "times"
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
404 if (e1 -> type == lw_expr_type_oper && e1 -> value == lw_expr_oper_times)
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
405 {
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
406 // second term is a "times"
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
407 if (e2 -> type == lw_expr_type_oper && e2 -> value == lw_expr_oper_times)
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
408 {
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
409 // both times - easy check
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
410 struct lw_expr_opers *o1, *o2;
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
411 for (o1 = e1 -> operands; o1; o1 = o1 -> next)
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
412 if (o1 -> p -> type != lw_expr_type_int)
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
413 break;
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
414
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
415 for (o2 = e2 -> operands; o2; o2 = o2 -> next)
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
416 if (o2 -> p -> type != lw_expr_type_int)
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
417 break;
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
418
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
419 if (lw_expr_simplify_compareoperandlist(&o1, &o2))
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
420 return 1;
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
421 return 0;
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
422 }
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
423
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
424 // not a times - have to assume it's the operand list
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
425 // with a "1 *" in front if it
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
426 if (e1 -> operands -> next -> next)
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
427 return 0;
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
428 if (!lw_expr_compare(e1 -> operands -> next -> p, e2))
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
429 return 0;
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
430 return 1;
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
431 }
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
432
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
433 // e1 is not a times
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
434 if (e2 -> type == lw_expr_type_oper && e2 -> value == lw_expr_oper_times)
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
435 {
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
436 // e2 is a times
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
437 if (e2 -> operands -> next -> next)
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
438 return 0;
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
439 if (!lw_expr_compare(e1, e2 -> operands -> next -> p))
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
440 return 0;
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
441 return 1;
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
442 }
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
443
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
444 // neither are times
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
445 if (!lw_expr_compare(e1, e2))
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
446 return 0;
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
447 return 1;
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
448 }
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
449
346
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
450 void lw_expr_simplify(lw_expr_t E, void *priv)
335
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
451 {
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
452 struct lw_expr_opers *o;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
453
371
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
454 // replace subtraction with O1 + -1(O2)...
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
455 // needed for like term collection
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
456 if (E -> type == lw_expr_type_oper && E -> value == lw_expr_oper_minus)
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
457 {
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
458 for (o = E -> operands -> next; o; o = o -> next)
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
459 {
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
460 lw_expr_t e1, e2;
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
461
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
462 e2 = lw_expr_build(lw_expr_type_int, -1);
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
463 e1 = lw_expr_build(lw_expr_type_oper, lw_expr_oper_times, e2, o -> p);
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
464 lw_expr_destroy(o -> p);
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
465 lw_expr_destroy(e2);
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
466 o -> p = e1;
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
467 }
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
468 E -> value = lw_expr_oper_plus;
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
469 }
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
470
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
471 // turn "NEG" into -1(O) - needed for like term collection
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
472 if (E -> type == lw_expr_type_oper && E -> value == lw_expr_oper_neg)
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
473 {
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
474 lw_expr_t e1;
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
475
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
476 E -> value = lw_expr_oper_times;
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
477 e1 = lw_expr_build(lw_expr_type_int, -1);
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
478 lw_expr_add_operand(E, e1);
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
479 lw_expr_destroy(e1);
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
480 }
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
481
337
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
482 again:
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
483 // try to resolve non-constant terms to constants here
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
484 if (E -> type == lw_expr_type_special && evaluate_special)
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
485 {
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
486 lw_expr_t te;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
487
346
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
488 te = evaluate_special(E -> value, E -> value2, priv);
337
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
489 if (te)
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
490 {
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
491 for (o = E -> operands; o; o = o -> next)
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
492 lw_expr_destroy(o -> p);
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
493 if (E -> type == lw_expr_type_var)
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
494 lw_free(E -> value2);
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
495 *E = *te;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
496 E -> operands = NULL;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
497
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
498 if (te -> type == lw_expr_type_var)
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
499 E -> value2 = lw_strdup(te -> value2);
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
500 for (o = te -> operands; o; o = o -> next)
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
501 {
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
502 lw_expr_add_operand(E, lw_expr_copy(o -> p));
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
503 }
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
504 lw_expr_destroy(te);
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
505 goto again;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
506 }
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
507 return;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
508 }
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
509
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
510 if (E -> type == lw_expr_type_var && evaluate_var)
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
511 {
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
512 lw_expr_t te;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
513
346
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
514 te = evaluate_var(E -> value2, priv);
337
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
515 if (te)
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
516 {
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
517 for (o = E -> operands; o; o = o -> next)
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
518 lw_expr_destroy(o -> p);
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
519 if (E -> type == lw_expr_type_var)
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
520 lw_free(E -> value2);
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
521 *E = *te;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
522 E -> operands = NULL;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
523
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
524 if (te -> type == lw_expr_type_var)
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
525 E -> value2 = lw_strdup(te -> value2);
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
526 for (o = te -> operands; o; o = o -> next)
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
527 {
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
528 lw_expr_add_operand(E, lw_expr_copy(o -> p));
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
529 }
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
530 lw_expr_destroy(te);
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
531 goto again;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
532 }
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
533 return;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
534 }
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
535
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
536 // non-operators have no simplification to do!
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
537 if (E -> type != lw_expr_type_oper)
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
538 return;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
539
371
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
540 // merge plus operations
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
541 if (E -> value == lw_expr_oper_plus)
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
542 {
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
543 lw_expr_t e2;
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
544
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
545 tryagainplus:
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
546 for (o = E -> operands; o; o = o -> next)
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
547 {
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
548 if (o -> p -> type == lw_expr_type_oper && o -> p -> value == lw_expr_oper_plus)
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
549 {
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
550 struct lw_expr_opers *o2, *o3;
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
551 // we have a + operation - bring operands up
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
552
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
553 for (o2 = E -> operands; o2 && o2 -> next != o; o2 = o2 -> next)
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
554 /* do nothing */ ;
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
555 if (o2)
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
556 o2 -> next = o -> p -> operands;
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
557 else
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
558 E -> operands = o -> p -> operands;
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
559 for (o2 = o -> p -> operands; o2 -> next; o2 = o2 -> next)
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
560 /* do nothing */ ;
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
561 o2 -> next = o -> next;
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
562 o -> p -> operands = NULL;
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
563 lw_expr_destroy(o -> p);
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
564 lw_free(o);
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
565 goto tryagainplus;
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
566 }
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
567 }
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
568 }
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
569
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
570 // merge times operations
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
571 if (E -> value == lw_expr_oper_times)
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
572 {
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
573 lw_expr_t e2;
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
574
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
575 tryagaintimes:
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
576 for (o = E -> operands; o; o = o -> next)
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
577 {
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
578 if (o -> p -> type == lw_expr_type_oper && o -> p -> value == lw_expr_oper_times)
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
579 {
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
580 struct lw_expr_opers *o2, *o3;
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
581 // we have a + operation - bring operands up
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
582
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
583 for (o2 = E -> operands; o2 && o2 -> next != o; o2 = o2 -> next)
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
584 /* do nothing */ ;
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
585 if (o2)
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
586 o2 -> next = o -> p -> operands;
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
587 else
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
588 E -> operands = o -> p -> operands;
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
589 for (o2 = o -> p -> operands; o2 -> next; o2 = o2 -> next)
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
590 /* do nothing */ ;
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
591 o2 -> next = o -> next;
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
592 o -> p -> operands = NULL;
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
593 lw_expr_destroy(o -> p);
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
594 lw_free(o);
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
595 goto tryagaintimes;
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
596 }
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
597 }
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
598 }
335
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
599
337
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
600 // simplify operands
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
601 for (o = E -> operands; o; o = o -> next)
346
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
602 lw_expr_simplify(o -> p, priv);
337
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
603
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
604 for (o = E -> operands; o; o = o -> next)
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
605 {
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
606 if (o -> p -> type != lw_expr_type_int)
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
607 break;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
608 }
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
609
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
610 if (!o)
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
611 {
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
612 // we can do the operation here!
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
613 int tr = -42424242;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
614
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
615 switch (E -> value)
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
616 {
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
617 case lw_expr_oper_neg:
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
618 tr = -(E -> operands -> p -> value);
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
619 break;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
620
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
621 case lw_expr_oper_com:
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
622 tr = ~(E -> operands -> p -> value);
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
623 break;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
624
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
625 case lw_expr_oper_plus:
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
626 tr = E -> operands -> p -> value;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
627 for (o = E -> operands -> next; o; o = o -> next)
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
628 tr += o -> p -> value;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
629 break;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
630
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
631 case lw_expr_oper_minus:
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
632 tr = E -> operands -> p -> value;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
633 for (o = E -> operands -> next; o; o = o -> next)
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
634 tr -= o -> p -> value;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
635 break;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
636
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
637 case lw_expr_oper_times:
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
638 tr = E -> operands -> p -> value;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
639 for (o = E -> operands -> next; o; o = o -> next)
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
640 tr *= o -> p -> value;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
641 break;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
642
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
643 case lw_expr_oper_divide:
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
644 tr = E -> operands -> p -> value / E -> operands -> next -> p -> value;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
645 break;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
646
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
647 case lw_expr_oper_mod:
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
648 tr = E -> operands -> p -> value % E -> operands -> next -> p -> value;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
649 break;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
650
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
651 case lw_expr_oper_intdiv:
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
652 tr = E -> operands -> p -> value / E -> operands -> next -> p -> value;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
653 break;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
654
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
655 case lw_expr_oper_bwand:
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
656 tr = E -> operands -> p -> value & E -> operands -> next -> p -> value;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
657 break;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
658
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
659 case lw_expr_oper_bwor:
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
660 tr = E -> operands -> p -> value | E -> operands -> next -> p -> value;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
661 break;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
662
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
663 case lw_expr_oper_bwxor:
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
664 tr = E -> operands -> p -> value ^ E -> operands -> next -> p -> value;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
665 break;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
666
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
667 case lw_expr_oper_and:
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
668 tr = E -> operands -> p -> value && E -> operands -> next -> p -> value;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
669 break;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
670
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
671 case lw_expr_oper_or:
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
672 tr = E -> operands -> p -> value || E -> operands -> next -> p -> value;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
673 break;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
674
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
675 }
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
676
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
677 while (E -> operands)
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
678 {
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
679 o = E -> operands;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
680 E -> operands = o -> next;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
681 lw_expr_destroy(o -> p);
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
682 lw_free(o);
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
683 }
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
684 E -> type = lw_expr_type_int;
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
685 E -> value = tr;
335
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
686 return;
337
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
687 }
335
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
688
371
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
689 if (E -> value == lw_expr_oper_plus)
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
690 {
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
691 lw_expr_t e1;
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
692 int cval = 0;
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
693
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
694 e1 = lw_expr_create();
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
695 e1 -> operands = E -> operands;
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
696 E -> operands = 0;
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
697
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
698 for (o = e1 -> operands; o; o = o -> next)
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
699 {
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
700 if (o -> p -> type == lw_expr_type_int)
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
701 cval += o -> p -> value;
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
702 else
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
703 lw_expr_add_operand(E, o -> p);
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
704 }
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
705 lw_expr_destroy(e1);
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
706 if (cval)
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
707 {
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
708 e1 = lw_expr_build(lw_expr_type_int, cval);
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
709 lw_expr_add_operand(E, e1);
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
710 lw_expr_destroy(e1);
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
711 }
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
712 }
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
713
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
714 if (E -> value == lw_expr_oper_times)
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
715 {
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
716 lw_expr_t e1;
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
717 int cval = 1;
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
718
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
719 e1 = lw_expr_create();
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
720 e1 -> operands = E -> operands;
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
721 E -> operands = 0;
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
722
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
723 for (o = e1 -> operands; o; o = o -> next)
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
724 {
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
725 if (o -> p -> type == lw_expr_type_int)
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
726 cval *= o -> p -> value;
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
727 else
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
728 lw_expr_add_operand(E, o -> p);
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
729 }
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
730 lw_expr_destroy(e1);
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
731 if (cval != 1)
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
732 {
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
733 e1 = lw_expr_build(lw_expr_type_int, cval);
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
734 lw_expr_add_operand(E, e1);
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
735 lw_expr_destroy(e1);
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
736 }
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
737 }
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
738
335
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
739 if (E -> value == lw_expr_oper_times)
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
740 {
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
741 for (o = E -> operands; o; o = o -> next)
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
742 {
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
743 if (o -> p -> type == lw_expr_type_int && o -> p -> value == 0)
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
744 {
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
745 // one operand of times is 0, replace operation with 0
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
746 while (E -> operands)
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
747 {
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
748 o = E -> operands;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
749 E -> operands = o -> next;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
750 lw_expr_destroy(o -> p);
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
751 lw_free(o);
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
752 }
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
753 E -> type = lw_expr_type_int;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
754 E -> value = 0;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
755 return;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
756 }
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
757 }
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
758 }
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
759
371
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
760 // sort "constants" to the start of each operand list for + and *
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
761 if (E -> value == lw_expr_oper_plus || E -> value == lw_expr_oper_times)
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
762 lw_expr_simplify_sortconstfirst(E);
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
763
335
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
764 // look for like terms and collect them together
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
765 if (E -> value == lw_expr_oper_plus)
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
766 {
371
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
767 struct lw_expr_opers *o2;
335
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
768 for (o = E -> operands; o; o = o -> next)
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
769 {
371
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
770 // skip constants
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
771 if (o -> p -> type == lw_expr_type_int)
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
772 continue;
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
773
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
774 // we have a term to match
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
775 // (o -> p) is first term
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
776 for (o2 = o -> next; o2; o2 = o2 -> next)
335
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
777 {
371
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
778 lw_expr_t e1, e2;
335
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
779
371
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
780 if (o2 -> p -> type == lw_expr_type_int)
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
781 continue;
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
782
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
783 if (lw_expr_simplify_isliketerm(o -> p, o2 -> p))
335
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
784 {
371
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
785 int coef, coef2;
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
786
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
787 // we have a like term here
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
788 // do something about it
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
789 if (o -> p -> type == lw_expr_type_oper && o -> p -> value == lw_expr_oper_times)
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
790 {
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
791 if (o -> p -> operands -> p -> type == lw_expr_type_int)
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
792 coef = o -> p -> operands -> p -> value;
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
793 else
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
794 coef = 1;
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
795 }
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
796 else
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
797 coef = 1;
335
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
798 if (o2 -> p -> type == lw_expr_type_oper && o2 -> p -> value == lw_expr_oper_times)
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
799 {
371
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
800 if (o2 -> p -> operands -> p -> type == lw_expr_type_int)
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
801 coef2 = o2 -> p -> operands -> p -> value;
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
802 else
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
803 coef2 = 1;
335
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
804 }
371
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
805 else
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
806 coef2 = 1;
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
807 coef += coef2;
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
808 e1 = lw_expr_create();
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
809 e1 -> type = lw_expr_type_oper;
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
810 e1 -> value = lw_expr_oper_times;
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
811 if (coef != 1)
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
812 {
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
813 e2 = lw_expr_build(lw_expr_type_int, coef);
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
814 lw_expr_add_operand(e1, e2);
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
815 lw_expr_destroy(e2);
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
816 }
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
817 lw_expr_destroy(o -> p);
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
818 o -> p = e1;
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
819 for (o = o2 -> p -> operands; o; o = o -> next)
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
820 {
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
821 if (o -> p -> type == lw_expr_type_int)
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
822 continue;
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
823 lw_expr_add_operand(e1, o -> p);
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
824 }
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
825 lw_expr_destroy(o2 -> p);
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
826 o2 -> p = lw_expr_build(lw_expr_type_int, 0);
9c24d9d485b9 Much bugfixing
lost@starbug
parents: 370
diff changeset
827 goto again;
335
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
828 }
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
829 }
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
830 }
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
831 }
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
832
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
833
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
834 if (E -> value == lw_expr_oper_plus)
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
835 {
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
836 int c = 0, t = 0;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
837 for (o = E -> operands; o; o = o -> next)
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
838 {
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
839 t++;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
840 if (!(o -> p -> type == lw_expr_type_int && o -> p -> value == 0))
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
841 {
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
842 c++;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
843 }
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
844 }
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
845 if (c == 1)
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
846 {
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
847 lw_expr_t r;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
848 // find the value and "move it up"
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
849 while (E -> operands)
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
850 {
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
851 o = E -> operands;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
852 if (o -> p -> type != lw_expr_type_int || o -> p -> value != 0)
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
853 {
337
04c80c51b16a Checkpoint development
lost
parents: 336
diff changeset
854 r = lw_expr_copy(o -> p);
335
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
855 }
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
856 E -> operands = o -> next;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
857 lw_expr_destroy(o -> p);
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
858 lw_free(o);
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
859 }
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
860 *E = *r;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
861 return;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
862 }
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
863 else if (c == 0)
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
864 {
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
865 // replace with 0
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
866 while (E -> operands)
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
867 {
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
868 o = E -> operands;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
869 E -> operands = o -> next;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
870 lw_expr_destroy(o -> p);
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
871 lw_free(o);
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
872 }
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
873 E -> type = lw_expr_type_int;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
874 E -> value = 0;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
875 return;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
876 }
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
877 else if (c != t)
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
878 {
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
879 // collapse out zero terms
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
880 struct lw_expr_opers *o2;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
881
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
882 for (o = E -> operands; o; o = o -> next)
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
883 {
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
884 if (o -> p -> type == lw_expr_type_int && o -> p -> value == 0)
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
885 {
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
886 if (o == E -> operands)
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
887 {
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
888 E -> operands = o -> next;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
889 lw_expr_destroy(o -> p);
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
890 lw_free(o);
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
891 o = E -> operands;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
892 }
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
893 else
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
894 {
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
895 for (o2 = E -> operands; o2 -> next == o; o2 = o2 -> next)
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
896 /* do nothing */ ;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
897 o2 -> next = o -> next;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
898 lw_expr_destroy(o -> p);
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
899 lw_free(o);
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
900 o = o2;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
901 }
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
902 }
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
903 }
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
904 }
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
905 return;
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
906 }
9f58e3bca6e3 checkpoint
lost
parents: 334
diff changeset
907 }
346
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
908
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
909 /*
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
910
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
911 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
912 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
913 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
914 determine what the term really is. It also handles parentheses.
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
915
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
916 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
917 respects the order of operations.
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
918
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
919 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
920 following conditions:
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
921
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
922 1. a NUL character
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
923 2. a whitespace character
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
924 3. a )
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
925 4. a ,
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
926 5. any character that is not recognized as a term
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
927
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
928 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
929
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
930 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
931 error.
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
932
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
933 */
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
934
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
935 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
936
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
937 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
938 {
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
939 lw_expr_t term, term2;
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
940
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
941 eval_next:
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
942 if (!**p || isspace(**p) || **p == ')' || **p == ']')
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
943 return NULL;
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
944
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
945 // parentheses
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
946 if (**p == '(')
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
947 {
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
948 (*p)++;
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
949 term = lw_expr_parse_expr(p, priv, 0);
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
950 if (**p != ')')
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
951 {
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
952 lw_expr_destroy(term);
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
953 return NULL;
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
954 }
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
955 (*p)++;
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
956 return term;
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
957 }
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
958
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
959 // unary +
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
960 if (**p == '+')
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
961 {
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
962 (*p)++;
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
963 goto eval_next;
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
964 }
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
965
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
966 // unary - (prec 200)
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
967 if (**p == '-')
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
968 {
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
969 (*p)++;
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
970 term = lw_expr_parse_expr(p, priv, 200);
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
971 if (!term)
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
972 return NULL;
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
973
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
974 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
975 lw_expr_destroy(term);
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
976 return term2;
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
977 }
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
978
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
979 // unary ^ or ~ (complement, prec 200)
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
980 if (**p == '^' || **p == '~')
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
981 {
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
982 (*p)++;
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
983 term = lw_expr_parse_expr(p, priv, 200);
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
984 if (!term)
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
985 return NULL;
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
986
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
987 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
988 lw_expr_destroy(term);
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
989 return term2;
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
990 }
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
991
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
992 // non-operator - pass to caller
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
993 return parse_term(p, priv);
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
994 }
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
995
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
996 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
997 {
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
998 static const struct operinfo
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
999 {
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1000 int opernum;
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1001 char *operstr;
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1002 int operprec;
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1003 } operators[] =
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1004 {
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1005 { lw_expr_oper_plus, "+", 100 },
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1006 { lw_expr_oper_minus, "-", 100 },
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1007 { lw_expr_oper_times, "*", 100 },
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1008 { lw_expr_oper_divide, "/", 150 },
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1009 { lw_expr_oper_mod, "%", 150 },
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1010 { lw_expr_oper_intdiv, "\\", 150 },
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1011
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1012 { lw_expr_oper_and, "&&", 25 },
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1013 { lw_expr_oper_or, "||", 25 },
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1014
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1015 { lw_expr_oper_bwand, "&", 50 },
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1016 { lw_expr_oper_bwor, "|", 50 },
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1017 { lw_expr_oper_bwxor, "^", 50 },
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1018
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1019 { lw_expr_oper_none, "", 0 }
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1020 };
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1021
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1022 int opern, i;
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1023 lw_expr_t term1, term2, term3;
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1024
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1025 if (!**p || isspace(**p) || **p == ')' || **p == ',' || **p == ']')
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1026 return NULL;
370
6b33faa21a0a Debugging output and bugfixing pass 0
lost@starbug
parents: 367
diff changeset
1027
346
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1028 term1 = lw_expr_parse_term(p, priv);
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1029 if (!term1)
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1030 return NULL;
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1031
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1032 eval_next:
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1033 if (!**p || isspace(**p) || **p == ')' || **p == ',' || **p == ']')
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1034 return term1;
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1035
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1036 // expecting an operator here
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1037 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
1038 {
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1039 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
1040 /* do nothing */;
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1041 if (operators[opern].operstr[i] == '\0')
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1042 break;
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1043 }
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1044
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1045 if (operators[opern].opernum == lw_expr_oper_none)
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1046 {
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1047 // unrecognized operator
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1048 lw_expr_destroy(term1);
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1049 return NULL;
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1050 }
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1051
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1052 // operator number is in opern, length of oper in i
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1053
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1054 // logic:
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1055 // 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
1056 // we simply return without advancing the input pointer; the operator
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1057 // will be evaluated again in the enclosing function call
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1058 if (operators[opern].operprec <= prec)
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1059 return term1;
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1060
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1061 // logic:
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1062 // 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
1063 // 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
1064 // 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
1065 // stack and then go on with the expression evaluation
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1066 (*p) += i;
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1067
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1068 // evaluate next expression(s) of higher precedence
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1069 term2 = lw_expr_parse_expr(p, priv, operators[opern].operprec);
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1070 if (!term2)
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1071 {
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1072 lw_expr_destroy(term1);
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1073 return NULL;
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1074 }
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1075
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1076 // now create operator
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1077 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
1078 lw_expr_destroy(term1);
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1079 lw_expr_destroy(term2);
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1080
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1081 // the new "expression" is the next "left operand"
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1082 term1 = term3;
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1083
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1084 // continue evaluating
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1085 goto eval_next;
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1086 }
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1087
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1088 lw_expr_t lw_expr_parse(char **p, void *priv)
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1089 {
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1090 return lw_expr_parse_expr(p, priv, 0);
a82c55070624 Added expression parsing infrastructure and misc fixes
lost@starbug
parents: 342
diff changeset
1091 }
367
34dfc9747f23 Reduction passes complete
lost@starbug
parents: 347
diff changeset
1092
34dfc9747f23 Reduction passes complete
lost@starbug
parents: 347
diff changeset
1093 int lw_expr_testterms(lw_expr_t e, lw_expr_testfn_t *fn, void *priv)
34dfc9747f23 Reduction passes complete
lost@starbug
parents: 347
diff changeset
1094 {
34dfc9747f23 Reduction passes complete
lost@starbug
parents: 347
diff changeset
1095 struct lw_expr_opers *o;
34dfc9747f23 Reduction passes complete
lost@starbug
parents: 347
diff changeset
1096 int r;
34dfc9747f23 Reduction passes complete
lost@starbug
parents: 347
diff changeset
1097
34dfc9747f23 Reduction passes complete
lost@starbug
parents: 347
diff changeset
1098 for (o = e -> operands; o; o = o -> next)
34dfc9747f23 Reduction passes complete
lost@starbug
parents: 347
diff changeset
1099 {
34dfc9747f23 Reduction passes complete
lost@starbug
parents: 347
diff changeset
1100 r = lw_expr_testterms(o -> p, fn, priv);
34dfc9747f23 Reduction passes complete
lost@starbug
parents: 347
diff changeset
1101 if (r)
34dfc9747f23 Reduction passes complete
lost@starbug
parents: 347
diff changeset
1102 return r;
34dfc9747f23 Reduction passes complete
lost@starbug
parents: 347
diff changeset
1103 }
34dfc9747f23 Reduction passes complete
lost@starbug
parents: 347
diff changeset
1104 return (fn)(e, priv);
34dfc9747f23 Reduction passes complete
lost@starbug
parents: 347
diff changeset
1105 }