annotate lwlink/trunk/src/expr.c @ 127:44dd2f2e42a7

Fixed reading object file incomplete references table
author lost
date Fri, 23 Jan 2009 05:49:16 +0000
parents d450943305a7
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
116
817ab0e124fa reading files implemented
lost
parents:
diff changeset
1 /*
817ab0e124fa reading files implemented
lost
parents:
diff changeset
2 expr.c
118
d450943305a7 Fixed copyright year
lost
parents: 116
diff changeset
3 Copyright © 2009 William Astle
116
817ab0e124fa reading files implemented
lost
parents:
diff changeset
4
817ab0e124fa reading files implemented
lost
parents:
diff changeset
5 This file is part of LWLINK.
817ab0e124fa reading files implemented
lost
parents:
diff changeset
6
817ab0e124fa reading files implemented
lost
parents:
diff changeset
7 LWLINK is free software: you can redistribute it and/or modify it under the
817ab0e124fa reading files implemented
lost
parents:
diff changeset
8 terms of the GNU General Public License as published by the Free Software
817ab0e124fa reading files implemented
lost
parents:
diff changeset
9 Foundation, either version 3 of the License, or (at your option) any later
817ab0e124fa reading files implemented
lost
parents:
diff changeset
10 version.
817ab0e124fa reading files implemented
lost
parents:
diff changeset
11
817ab0e124fa reading files implemented
lost
parents:
diff changeset
12 This program is distributed in the hope that it will be useful, but WITHOUT
817ab0e124fa reading files implemented
lost
parents:
diff changeset
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
817ab0e124fa reading files implemented
lost
parents:
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
817ab0e124fa reading files implemented
lost
parents:
diff changeset
15 more details.
817ab0e124fa reading files implemented
lost
parents:
diff changeset
16
817ab0e124fa reading files implemented
lost
parents:
diff changeset
17 You should have received a copy of the GNU General Public License along with
817ab0e124fa reading files implemented
lost
parents:
diff changeset
18 this program. If not, see <http://www.gnu.org/licenses/>.
817ab0e124fa reading files implemented
lost
parents:
diff changeset
19 */
817ab0e124fa reading files implemented
lost
parents:
diff changeset
20
817ab0e124fa reading files implemented
lost
parents:
diff changeset
21 /*
817ab0e124fa reading files implemented
lost
parents:
diff changeset
22 This file contains the actual expression evaluator
817ab0e124fa reading files implemented
lost
parents:
diff changeset
23 */
817ab0e124fa reading files implemented
lost
parents:
diff changeset
24
817ab0e124fa reading files implemented
lost
parents:
diff changeset
25 #define __expr_c_seen__
817ab0e124fa reading files implemented
lost
parents:
diff changeset
26
817ab0e124fa reading files implemented
lost
parents:
diff changeset
27 #include <ctype.h>
817ab0e124fa reading files implemented
lost
parents:
diff changeset
28 #include <stdlib.h>
817ab0e124fa reading files implemented
lost
parents:
diff changeset
29 #include <string.h>
817ab0e124fa reading files implemented
lost
parents:
diff changeset
30
817ab0e124fa reading files implemented
lost
parents:
diff changeset
31 #include "expr.h"
817ab0e124fa reading files implemented
lost
parents:
diff changeset
32 #include "util.h"
817ab0e124fa reading files implemented
lost
parents:
diff changeset
33
817ab0e124fa reading files implemented
lost
parents:
diff changeset
34 lw_expr_stack_t *lw_expr_stack_create(void)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
35 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
36 lw_expr_stack_t *s;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
37
817ab0e124fa reading files implemented
lost
parents:
diff changeset
38 s = lw_malloc(sizeof(lw_expr_stack_t));
817ab0e124fa reading files implemented
lost
parents:
diff changeset
39 s -> head = NULL;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
40 s -> tail = NULL;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
41 return s;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
42 }
817ab0e124fa reading files implemented
lost
parents:
diff changeset
43
817ab0e124fa reading files implemented
lost
parents:
diff changeset
44 void lw_expr_stack_free(lw_expr_stack_t *s)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
45 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
46 while (s -> head)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
47 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
48 s -> tail = s -> head;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
49 s -> head = s -> head -> next;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
50 lw_expr_term_free(s -> tail -> term);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
51 lw_free(s -> tail);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
52 }
817ab0e124fa reading files implemented
lost
parents:
diff changeset
53 lw_free(s);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
54 }
817ab0e124fa reading files implemented
lost
parents:
diff changeset
55
817ab0e124fa reading files implemented
lost
parents:
diff changeset
56 void lw_expr_term_free(lw_expr_term_t *t)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
57 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
58 if (t)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
59 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
60 if (t -> term_type == LW_TERM_SYM)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
61 lw_free(t -> symbol);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
62 lw_free(t);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
63 }
817ab0e124fa reading files implemented
lost
parents:
diff changeset
64 }
817ab0e124fa reading files implemented
lost
parents:
diff changeset
65
817ab0e124fa reading files implemented
lost
parents:
diff changeset
66 lw_expr_term_t *lw_expr_term_create_oper(int oper)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
67 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
68 lw_expr_term_t *t;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
69
817ab0e124fa reading files implemented
lost
parents:
diff changeset
70 t = lw_malloc(sizeof(lw_expr_term_t));
817ab0e124fa reading files implemented
lost
parents:
diff changeset
71 t -> term_type = LW_TERM_OPER;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
72 t -> value = oper;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
73 return t;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
74 }
817ab0e124fa reading files implemented
lost
parents:
diff changeset
75
817ab0e124fa reading files implemented
lost
parents:
diff changeset
76 lw_expr_term_t *lw_expr_term_create_int(int val)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
77 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
78 lw_expr_term_t *t;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
79
817ab0e124fa reading files implemented
lost
parents:
diff changeset
80 t = lw_malloc(sizeof(lw_expr_term_t));
817ab0e124fa reading files implemented
lost
parents:
diff changeset
81 t -> term_type = LW_TERM_INT;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
82 t -> value = val;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
83 return t;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
84 }
817ab0e124fa reading files implemented
lost
parents:
diff changeset
85
817ab0e124fa reading files implemented
lost
parents:
diff changeset
86 lw_expr_term_t *lw_expr_term_create_sym(char *sym, int symtype)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
87 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
88 lw_expr_term_t *t;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
89
817ab0e124fa reading files implemented
lost
parents:
diff changeset
90 t = lw_malloc(sizeof(lw_expr_term_t));
817ab0e124fa reading files implemented
lost
parents:
diff changeset
91 t -> term_type = LW_TERM_SYM;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
92 t -> symbol = lw_strdup(sym);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
93 t -> value = symtype;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
94 return t;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
95 }
817ab0e124fa reading files implemented
lost
parents:
diff changeset
96
817ab0e124fa reading files implemented
lost
parents:
diff changeset
97 lw_expr_term_t *lw_expr_term_dup(lw_expr_term_t *t)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
98 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
99 switch (t -> term_type)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
100 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
101 case LW_TERM_INT:
817ab0e124fa reading files implemented
lost
parents:
diff changeset
102 return lw_expr_term_create_int(t -> value);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
103
817ab0e124fa reading files implemented
lost
parents:
diff changeset
104 case LW_TERM_OPER:
817ab0e124fa reading files implemented
lost
parents:
diff changeset
105 return lw_expr_term_create_oper(t -> value);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
106
817ab0e124fa reading files implemented
lost
parents:
diff changeset
107 case LW_TERM_SYM:
817ab0e124fa reading files implemented
lost
parents:
diff changeset
108 return lw_expr_term_create_sym(t -> symbol, t -> value);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
109
817ab0e124fa reading files implemented
lost
parents:
diff changeset
110 default:
817ab0e124fa reading files implemented
lost
parents:
diff changeset
111 exit(1);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
112 }
817ab0e124fa reading files implemented
lost
parents:
diff changeset
113 // can't get here
817ab0e124fa reading files implemented
lost
parents:
diff changeset
114 }
817ab0e124fa reading files implemented
lost
parents:
diff changeset
115
817ab0e124fa reading files implemented
lost
parents:
diff changeset
116 void lw_expr_stack_push(lw_expr_stack_t *s, lw_expr_term_t *t)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
117 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
118 lw_expr_stack_node_t *n;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
119
817ab0e124fa reading files implemented
lost
parents:
diff changeset
120 if (!s)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
121 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
122 exit(1);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
123 }
817ab0e124fa reading files implemented
lost
parents:
diff changeset
124
817ab0e124fa reading files implemented
lost
parents:
diff changeset
125 n = lw_malloc(sizeof(lw_expr_stack_node_t));
817ab0e124fa reading files implemented
lost
parents:
diff changeset
126 n -> next = NULL;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
127 n -> prev = s -> tail;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
128 n -> term = lw_expr_term_dup(t);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
129
817ab0e124fa reading files implemented
lost
parents:
diff changeset
130 if (s -> head)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
131 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
132 s -> tail -> next = n;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
133 s -> tail = n;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
134 }
817ab0e124fa reading files implemented
lost
parents:
diff changeset
135 else
817ab0e124fa reading files implemented
lost
parents:
diff changeset
136 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
137 s -> head = n;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
138 s -> tail = n;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
139 }
817ab0e124fa reading files implemented
lost
parents:
diff changeset
140 }
817ab0e124fa reading files implemented
lost
parents:
diff changeset
141
817ab0e124fa reading files implemented
lost
parents:
diff changeset
142 lw_expr_term_t *lw_expr_stack_pop(lw_expr_stack_t *s)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
143 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
144 lw_expr_term_t *t;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
145 lw_expr_stack_node_t *n;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
146
817ab0e124fa reading files implemented
lost
parents:
diff changeset
147 if (!(s -> tail))
817ab0e124fa reading files implemented
lost
parents:
diff changeset
148 return NULL;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
149
817ab0e124fa reading files implemented
lost
parents:
diff changeset
150 n = s -> tail;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
151 s -> tail = n -> prev;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
152 if (!(n -> prev))
817ab0e124fa reading files implemented
lost
parents:
diff changeset
153 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
154 s -> head = NULL;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
155 }
817ab0e124fa reading files implemented
lost
parents:
diff changeset
156
817ab0e124fa reading files implemented
lost
parents:
diff changeset
157 t = n -> term;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
158 n -> term = NULL;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
159
817ab0e124fa reading files implemented
lost
parents:
diff changeset
160 lw_free(n);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
161
817ab0e124fa reading files implemented
lost
parents:
diff changeset
162 return t;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
163 }
817ab0e124fa reading files implemented
lost
parents:
diff changeset
164
817ab0e124fa reading files implemented
lost
parents:
diff changeset
165 /*
817ab0e124fa reading files implemented
lost
parents:
diff changeset
166 take an expression stack s and scan for operations that can be completed
817ab0e124fa reading files implemented
lost
parents:
diff changeset
167
817ab0e124fa reading files implemented
lost
parents:
diff changeset
168 return -1 on error, 0 on no error
817ab0e124fa reading files implemented
lost
parents:
diff changeset
169
817ab0e124fa reading files implemented
lost
parents:
diff changeset
170 possible errors are: division by zero or unknown operator
817ab0e124fa reading files implemented
lost
parents:
diff changeset
171
817ab0e124fa reading files implemented
lost
parents:
diff changeset
172 theory of operation:
817ab0e124fa reading files implemented
lost
parents:
diff changeset
173
817ab0e124fa reading files implemented
lost
parents:
diff changeset
174 scan the stack for an operator which has two constants preceding it (binary)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
175 or 1 constant preceding it (unary) and if found, perform the calculation
817ab0e124fa reading files implemented
lost
parents:
diff changeset
176 and replace the operator and its operands with the result
817ab0e124fa reading files implemented
lost
parents:
diff changeset
177
817ab0e124fa reading files implemented
lost
parents:
diff changeset
178 repeat the scan until no futher simplications are found or if there are no
817ab0e124fa reading files implemented
lost
parents:
diff changeset
179 further operators or only a single term remains
817ab0e124fa reading files implemented
lost
parents:
diff changeset
180
817ab0e124fa reading files implemented
lost
parents:
diff changeset
181 */
817ab0e124fa reading files implemented
lost
parents:
diff changeset
182 int lw_expr_reval(lw_expr_stack_t *s, lw_expr_stack_t *(*sfunc)(char *sym, int stype, void *state), void *state)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
183 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
184 lw_expr_stack_node_t *n, *n2;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
185 lw_expr_stack_t *ss;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
186 int c;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
187
817ab0e124fa reading files implemented
lost
parents:
diff changeset
188 next_iter_sym:
817ab0e124fa reading files implemented
lost
parents:
diff changeset
189 // resolve symbols
817ab0e124fa reading files implemented
lost
parents:
diff changeset
190 // symbols that do not resolve to an expression are left alone
817ab0e124fa reading files implemented
lost
parents:
diff changeset
191 for (c = 0, n = s -> head; n; n = n -> next)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
192 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
193 if (n -> term -> term_type == LW_TERM_SYM)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
194 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
195 ss = sfunc(n -> term -> symbol, n -> term -> value, state);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
196 if (ss)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
197 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
198 c++;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
199 // splice in the result stack
817ab0e124fa reading files implemented
lost
parents:
diff changeset
200 if (n -> prev)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
201 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
202 n -> prev -> next = ss -> head;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
203 }
817ab0e124fa reading files implemented
lost
parents:
diff changeset
204 else
817ab0e124fa reading files implemented
lost
parents:
diff changeset
205 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
206 s -> head = ss -> head;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
207 }
817ab0e124fa reading files implemented
lost
parents:
diff changeset
208 ss -> head -> prev = n -> prev;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
209 ss -> tail -> next = n -> next;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
210 if (n -> next)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
211 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
212 n -> next -> prev = ss -> tail;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
213 }
817ab0e124fa reading files implemented
lost
parents:
diff changeset
214 else
817ab0e124fa reading files implemented
lost
parents:
diff changeset
215 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
216 s -> tail = ss -> tail;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
217 }
817ab0e124fa reading files implemented
lost
parents:
diff changeset
218 lw_expr_term_free(n -> term);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
219 lw_free(n);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
220 n = ss -> tail;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
221
817ab0e124fa reading files implemented
lost
parents:
diff changeset
222 ss -> head = NULL;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
223 ss -> tail = NULL;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
224 lw_expr_stack_free(ss);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
225 }
817ab0e124fa reading files implemented
lost
parents:
diff changeset
226 }
817ab0e124fa reading files implemented
lost
parents:
diff changeset
227 }
817ab0e124fa reading files implemented
lost
parents:
diff changeset
228 if (c)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
229 goto next_iter_sym;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
230
817ab0e124fa reading files implemented
lost
parents:
diff changeset
231 next_iter:
817ab0e124fa reading files implemented
lost
parents:
diff changeset
232 // a single term
817ab0e124fa reading files implemented
lost
parents:
diff changeset
233 if (s -> head == s -> tail)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
234 return 0;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
235
817ab0e124fa reading files implemented
lost
parents:
diff changeset
236 // search for an operator
817ab0e124fa reading files implemented
lost
parents:
diff changeset
237 for (n = s -> head; n; n = n -> next)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
238 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
239 if (n -> term -> term_type == LW_TERM_OPER)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
240 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
241 if (n -> term -> value == LW_OPER_NEG
817ab0e124fa reading files implemented
lost
parents:
diff changeset
242 || n -> term -> value == LW_OPER_COM
817ab0e124fa reading files implemented
lost
parents:
diff changeset
243 )
817ab0e124fa reading files implemented
lost
parents:
diff changeset
244 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
245 // unary operator
817ab0e124fa reading files implemented
lost
parents:
diff changeset
246 if (n -> prev && n -> prev -> term -> term_type == LW_TERM_INT)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
247 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
248 // a unary operator we can resolve
817ab0e124fa reading files implemented
lost
parents:
diff changeset
249 // we do the op then remove the term "n" is pointing at
817ab0e124fa reading files implemented
lost
parents:
diff changeset
250 if (n -> term -> value == LW_OPER_NEG)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
251 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
252 n -> prev -> term -> value = -(n -> prev -> term -> value);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
253 }
817ab0e124fa reading files implemented
lost
parents:
diff changeset
254 else if (n -> term -> value == LW_OPER_COM)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
255 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
256 n -> prev -> term -> value = ~(n -> prev -> term -> value);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
257 }
817ab0e124fa reading files implemented
lost
parents:
diff changeset
258 n -> prev -> next = n -> next;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
259 if (n -> next)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
260 n -> next -> prev = n -> prev;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
261 else
817ab0e124fa reading files implemented
lost
parents:
diff changeset
262 s -> tail = n -> prev;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
263
817ab0e124fa reading files implemented
lost
parents:
diff changeset
264 lw_expr_term_free(n -> term);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
265 lw_free(n);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
266 break;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
267 }
817ab0e124fa reading files implemented
lost
parents:
diff changeset
268 }
817ab0e124fa reading files implemented
lost
parents:
diff changeset
269 else
817ab0e124fa reading files implemented
lost
parents:
diff changeset
270 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
271 // binary operator
817ab0e124fa reading files implemented
lost
parents:
diff changeset
272 if (n -> prev && n -> prev -> prev && n -> prev -> term -> term_type == LW_TERM_INT && n -> prev -> prev -> term -> term_type == LW_TERM_INT)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
273 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
274 // a binary operator we can resolve
817ab0e124fa reading files implemented
lost
parents:
diff changeset
275 switch (n -> term -> value)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
276 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
277 case LW_OPER_PLUS:
817ab0e124fa reading files implemented
lost
parents:
diff changeset
278 n -> prev -> prev -> term -> value += n -> prev -> term -> value;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
279 break;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
280
817ab0e124fa reading files implemented
lost
parents:
diff changeset
281 case LW_OPER_MINUS:
817ab0e124fa reading files implemented
lost
parents:
diff changeset
282 n -> prev -> prev -> term -> value -= n -> prev -> term -> value;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
283 break;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
284
817ab0e124fa reading files implemented
lost
parents:
diff changeset
285 case LW_OPER_TIMES:
817ab0e124fa reading files implemented
lost
parents:
diff changeset
286 n -> prev -> prev -> term -> value *= n -> prev -> term -> value;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
287 break;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
288
817ab0e124fa reading files implemented
lost
parents:
diff changeset
289 case LW_OPER_DIVIDE:
817ab0e124fa reading files implemented
lost
parents:
diff changeset
290 if (n -> prev -> term -> value == 0)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
291 return -1;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
292 n -> prev -> prev -> term -> value /= n -> prev -> term -> value;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
293 break;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
294
817ab0e124fa reading files implemented
lost
parents:
diff changeset
295 case LW_OPER_MOD:
817ab0e124fa reading files implemented
lost
parents:
diff changeset
296 if (n -> prev -> term -> value == 0)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
297 return -1;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
298 n -> prev -> prev -> term -> value %= n -> prev -> term -> value;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
299 break;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
300
817ab0e124fa reading files implemented
lost
parents:
diff changeset
301 case LW_OPER_INTDIV:
817ab0e124fa reading files implemented
lost
parents:
diff changeset
302 if (n -> prev -> term -> value == 0)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
303 return -1;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
304 n -> prev -> prev -> term -> value /= n -> prev -> term -> value;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
305 break;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
306
817ab0e124fa reading files implemented
lost
parents:
diff changeset
307 case LW_OPER_BWAND:
817ab0e124fa reading files implemented
lost
parents:
diff changeset
308 n -> prev -> prev -> term -> value &= n -> prev -> term -> value;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
309 break;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
310
817ab0e124fa reading files implemented
lost
parents:
diff changeset
311 case LW_OPER_BWOR:
817ab0e124fa reading files implemented
lost
parents:
diff changeset
312 n -> prev -> prev -> term -> value |= n -> prev -> term -> value;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
313 break;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
314
817ab0e124fa reading files implemented
lost
parents:
diff changeset
315 case LW_OPER_BWXOR:
817ab0e124fa reading files implemented
lost
parents:
diff changeset
316 n -> prev -> prev -> term -> value ^= n -> prev -> term -> value;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
317 break;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
318
817ab0e124fa reading files implemented
lost
parents:
diff changeset
319 case LW_OPER_AND:
817ab0e124fa reading files implemented
lost
parents:
diff changeset
320 n -> prev -> prev -> term -> value = (n -> prev -> term -> value && n -> prev -> prev -> term -> value) ? 1 : 0;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
321 break;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
322
817ab0e124fa reading files implemented
lost
parents:
diff changeset
323 case LW_OPER_OR:
817ab0e124fa reading files implemented
lost
parents:
diff changeset
324 n -> prev -> prev -> term -> value = (n -> prev -> term -> value || n -> prev -> prev -> term -> value) ? 1 : 0;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
325 break;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
326
817ab0e124fa reading files implemented
lost
parents:
diff changeset
327 default:
817ab0e124fa reading files implemented
lost
parents:
diff changeset
328 // return error if unknown operator!
817ab0e124fa reading files implemented
lost
parents:
diff changeset
329 return -1;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
330 }
817ab0e124fa reading files implemented
lost
parents:
diff changeset
331
817ab0e124fa reading files implemented
lost
parents:
diff changeset
332 // now remove the two unneeded entries from the stack
817ab0e124fa reading files implemented
lost
parents:
diff changeset
333 n -> prev -> prev -> next = n -> next;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
334 if (n -> next)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
335 n -> next -> prev = n -> prev -> prev;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
336 else
817ab0e124fa reading files implemented
lost
parents:
diff changeset
337 s -> tail = n -> prev -> prev;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
338
817ab0e124fa reading files implemented
lost
parents:
diff changeset
339 lw_expr_term_free(n -> term);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
340 lw_expr_term_free(n -> prev -> term);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
341 lw_free(n -> prev);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
342 lw_free(n);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
343 break;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
344 }
817ab0e124fa reading files implemented
lost
parents:
diff changeset
345 }
817ab0e124fa reading files implemented
lost
parents:
diff changeset
346 }
817ab0e124fa reading files implemented
lost
parents:
diff changeset
347 }
817ab0e124fa reading files implemented
lost
parents:
diff changeset
348 // note for the terminally confused about dynamic memory and pointers:
817ab0e124fa reading files implemented
lost
parents:
diff changeset
349 // n will not be NULL even after the lw_free calls above so
817ab0e124fa reading files implemented
lost
parents:
diff changeset
350 // this test will still work (n will be a dangling pointer)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
351 // (n will only be NULL if we didn't find any operators to simplify)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
352 if (n)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
353 goto next_iter;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
354
817ab0e124fa reading files implemented
lost
parents:
diff changeset
355 return 0;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
356 }