annotate lwlib/lw_expr.c @ 431:d7d7e4dca3e7

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