annotate lwlink/expr.c @ 211:51511cf1c9f8

Prepare for gnulib integration
author lost
date Fri, 24 Apr 2009 06:27:00 +0000
parents 299c5d793aca
children bae1e3ecdce1
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
206
299c5d793aca Made lwlink smarter about not included unneeded (unreferenced) members of a library file
lost
parents: 139
diff changeset
56 lw_expr_stack_t *lw_expr_stack_dup(lw_expr_stack_t *s)
299c5d793aca Made lwlink smarter about not included unneeded (unreferenced) members of a library file
lost
parents: 139
diff changeset
57 {
299c5d793aca Made lwlink smarter about not included unneeded (unreferenced) members of a library file
lost
parents: 139
diff changeset
58 lw_expr_stack_node_t *t;
299c5d793aca Made lwlink smarter about not included unneeded (unreferenced) members of a library file
lost
parents: 139
diff changeset
59 lw_expr_stack_t *s2;
299c5d793aca Made lwlink smarter about not included unneeded (unreferenced) members of a library file
lost
parents: 139
diff changeset
60
299c5d793aca Made lwlink smarter about not included unneeded (unreferenced) members of a library file
lost
parents: 139
diff changeset
61 s2 = lw_expr_stack_create();
299c5d793aca Made lwlink smarter about not included unneeded (unreferenced) members of a library file
lost
parents: 139
diff changeset
62 for (t = s -> head; t; t = t -> next)
299c5d793aca Made lwlink smarter about not included unneeded (unreferenced) members of a library file
lost
parents: 139
diff changeset
63 {
299c5d793aca Made lwlink smarter about not included unneeded (unreferenced) members of a library file
lost
parents: 139
diff changeset
64 lw_expr_stack_push(s2, t -> term);
299c5d793aca Made lwlink smarter about not included unneeded (unreferenced) members of a library file
lost
parents: 139
diff changeset
65 }
299c5d793aca Made lwlink smarter about not included unneeded (unreferenced) members of a library file
lost
parents: 139
diff changeset
66 return s2;
299c5d793aca Made lwlink smarter about not included unneeded (unreferenced) members of a library file
lost
parents: 139
diff changeset
67 }
299c5d793aca Made lwlink smarter about not included unneeded (unreferenced) members of a library file
lost
parents: 139
diff changeset
68
116
817ab0e124fa reading files implemented
lost
parents:
diff changeset
69 void lw_expr_term_free(lw_expr_term_t *t)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
70 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
71 if (t)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
72 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
73 if (t -> term_type == LW_TERM_SYM)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
74 lw_free(t -> symbol);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
75 lw_free(t);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
76 }
817ab0e124fa reading files implemented
lost
parents:
diff changeset
77 }
817ab0e124fa reading files implemented
lost
parents:
diff changeset
78
817ab0e124fa reading files implemented
lost
parents:
diff changeset
79 lw_expr_term_t *lw_expr_term_create_oper(int oper)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
80 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
81 lw_expr_term_t *t;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
82
817ab0e124fa reading files implemented
lost
parents:
diff changeset
83 t = lw_malloc(sizeof(lw_expr_term_t));
817ab0e124fa reading files implemented
lost
parents:
diff changeset
84 t -> term_type = LW_TERM_OPER;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
85 t -> value = oper;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
86 return t;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
87 }
817ab0e124fa reading files implemented
lost
parents:
diff changeset
88
817ab0e124fa reading files implemented
lost
parents:
diff changeset
89 lw_expr_term_t *lw_expr_term_create_int(int val)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
90 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
91 lw_expr_term_t *t;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
92
817ab0e124fa reading files implemented
lost
parents:
diff changeset
93 t = lw_malloc(sizeof(lw_expr_term_t));
817ab0e124fa reading files implemented
lost
parents:
diff changeset
94 t -> term_type = LW_TERM_INT;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
95 t -> value = val;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
96 return t;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
97 }
817ab0e124fa reading files implemented
lost
parents:
diff changeset
98
817ab0e124fa reading files implemented
lost
parents:
diff changeset
99 lw_expr_term_t *lw_expr_term_create_sym(char *sym, int symtype)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
100 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
101 lw_expr_term_t *t;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
102
817ab0e124fa reading files implemented
lost
parents:
diff changeset
103 t = lw_malloc(sizeof(lw_expr_term_t));
817ab0e124fa reading files implemented
lost
parents:
diff changeset
104 t -> term_type = LW_TERM_SYM;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
105 t -> symbol = lw_strdup(sym);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
106 t -> value = symtype;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
107 return t;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
108 }
817ab0e124fa reading files implemented
lost
parents:
diff changeset
109
817ab0e124fa reading files implemented
lost
parents:
diff changeset
110 lw_expr_term_t *lw_expr_term_dup(lw_expr_term_t *t)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
111 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
112 switch (t -> term_type)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
113 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
114 case LW_TERM_INT:
817ab0e124fa reading files implemented
lost
parents:
diff changeset
115 return lw_expr_term_create_int(t -> value);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
116
817ab0e124fa reading files implemented
lost
parents:
diff changeset
117 case LW_TERM_OPER:
817ab0e124fa reading files implemented
lost
parents:
diff changeset
118 return lw_expr_term_create_oper(t -> value);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
119
817ab0e124fa reading files implemented
lost
parents:
diff changeset
120 case LW_TERM_SYM:
817ab0e124fa reading files implemented
lost
parents:
diff changeset
121 return lw_expr_term_create_sym(t -> symbol, t -> value);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
122
817ab0e124fa reading files implemented
lost
parents:
diff changeset
123 default:
817ab0e124fa reading files implemented
lost
parents:
diff changeset
124 exit(1);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
125 }
817ab0e124fa reading files implemented
lost
parents:
diff changeset
126 // can't get here
817ab0e124fa reading files implemented
lost
parents:
diff changeset
127 }
817ab0e124fa reading files implemented
lost
parents:
diff changeset
128
817ab0e124fa reading files implemented
lost
parents:
diff changeset
129 void lw_expr_stack_push(lw_expr_stack_t *s, lw_expr_term_t *t)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
130 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
131 lw_expr_stack_node_t *n;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
132
817ab0e124fa reading files implemented
lost
parents:
diff changeset
133 if (!s)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
134 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
135 exit(1);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
136 }
817ab0e124fa reading files implemented
lost
parents:
diff changeset
137
817ab0e124fa reading files implemented
lost
parents:
diff changeset
138 n = lw_malloc(sizeof(lw_expr_stack_node_t));
817ab0e124fa reading files implemented
lost
parents:
diff changeset
139 n -> next = NULL;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
140 n -> prev = s -> tail;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
141 n -> term = lw_expr_term_dup(t);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
142
817ab0e124fa reading files implemented
lost
parents:
diff changeset
143 if (s -> head)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
144 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
145 s -> tail -> next = n;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
146 s -> tail = n;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
147 }
817ab0e124fa reading files implemented
lost
parents:
diff changeset
148 else
817ab0e124fa reading files implemented
lost
parents:
diff changeset
149 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
150 s -> head = n;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
151 s -> tail = n;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
152 }
817ab0e124fa reading files implemented
lost
parents:
diff changeset
153 }
817ab0e124fa reading files implemented
lost
parents:
diff changeset
154
817ab0e124fa reading files implemented
lost
parents:
diff changeset
155 lw_expr_term_t *lw_expr_stack_pop(lw_expr_stack_t *s)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
156 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
157 lw_expr_term_t *t;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
158 lw_expr_stack_node_t *n;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
159
817ab0e124fa reading files implemented
lost
parents:
diff changeset
160 if (!(s -> tail))
817ab0e124fa reading files implemented
lost
parents:
diff changeset
161 return NULL;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
162
817ab0e124fa reading files implemented
lost
parents:
diff changeset
163 n = s -> tail;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
164 s -> tail = n -> prev;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
165 if (!(n -> prev))
817ab0e124fa reading files implemented
lost
parents:
diff changeset
166 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
167 s -> head = NULL;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
168 }
817ab0e124fa reading files implemented
lost
parents:
diff changeset
169
817ab0e124fa reading files implemented
lost
parents:
diff changeset
170 t = n -> term;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
171 n -> term = NULL;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
172
817ab0e124fa reading files implemented
lost
parents:
diff changeset
173 lw_free(n);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
174
817ab0e124fa reading files implemented
lost
parents:
diff changeset
175 return t;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
176 }
817ab0e124fa reading files implemented
lost
parents:
diff changeset
177
817ab0e124fa reading files implemented
lost
parents:
diff changeset
178 /*
817ab0e124fa reading files implemented
lost
parents:
diff changeset
179 take an expression stack s and scan for operations that can be completed
817ab0e124fa reading files implemented
lost
parents:
diff changeset
180
817ab0e124fa reading files implemented
lost
parents:
diff changeset
181 return -1 on error, 0 on no error
817ab0e124fa reading files implemented
lost
parents:
diff changeset
182
817ab0e124fa reading files implemented
lost
parents:
diff changeset
183 possible errors are: division by zero or unknown operator
817ab0e124fa reading files implemented
lost
parents:
diff changeset
184
817ab0e124fa reading files implemented
lost
parents:
diff changeset
185 theory of operation:
817ab0e124fa reading files implemented
lost
parents:
diff changeset
186
817ab0e124fa reading files implemented
lost
parents:
diff changeset
187 scan the stack for an operator which has two constants preceding it (binary)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
188 or 1 constant preceding it (unary) and if found, perform the calculation
817ab0e124fa reading files implemented
lost
parents:
diff changeset
189 and replace the operator and its operands with the result
817ab0e124fa reading files implemented
lost
parents:
diff changeset
190
817ab0e124fa reading files implemented
lost
parents:
diff changeset
191 repeat the scan until no futher simplications are found or if there are no
817ab0e124fa reading files implemented
lost
parents:
diff changeset
192 further operators or only a single term remains
817ab0e124fa reading files implemented
lost
parents:
diff changeset
193
817ab0e124fa reading files implemented
lost
parents:
diff changeset
194 */
817ab0e124fa reading files implemented
lost
parents:
diff changeset
195 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
196 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
197 lw_expr_stack_node_t *n, *n2;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
198 lw_expr_stack_t *ss;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
199 int c;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
200
817ab0e124fa reading files implemented
lost
parents:
diff changeset
201 next_iter_sym:
817ab0e124fa reading files implemented
lost
parents:
diff changeset
202 // resolve symbols
817ab0e124fa reading files implemented
lost
parents:
diff changeset
203 // symbols that do not resolve to an expression are left alone
817ab0e124fa reading files implemented
lost
parents:
diff changeset
204 for (c = 0, n = s -> head; n; n = n -> next)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
205 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
206 if (n -> term -> term_type == LW_TERM_SYM)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
207 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
208 ss = sfunc(n -> term -> symbol, n -> term -> value, state);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
209 if (ss)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
210 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
211 c++;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
212 // splice in the result stack
817ab0e124fa reading files implemented
lost
parents:
diff changeset
213 if (n -> prev)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
214 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
215 n -> prev -> next = ss -> head;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
216 }
817ab0e124fa reading files implemented
lost
parents:
diff changeset
217 else
817ab0e124fa reading files implemented
lost
parents:
diff changeset
218 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
219 s -> head = ss -> head;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
220 }
817ab0e124fa reading files implemented
lost
parents:
diff changeset
221 ss -> head -> prev = n -> prev;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
222 ss -> tail -> next = n -> next;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
223 if (n -> next)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
224 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
225 n -> next -> prev = ss -> tail;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
226 }
817ab0e124fa reading files implemented
lost
parents:
diff changeset
227 else
817ab0e124fa reading files implemented
lost
parents:
diff changeset
228 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
229 s -> tail = ss -> tail;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
230 }
817ab0e124fa reading files implemented
lost
parents:
diff changeset
231 lw_expr_term_free(n -> term);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
232 lw_free(n);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
233 n = ss -> tail;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
234
817ab0e124fa reading files implemented
lost
parents:
diff changeset
235 ss -> head = NULL;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
236 ss -> tail = NULL;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
237 lw_expr_stack_free(ss);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
238 }
817ab0e124fa reading files implemented
lost
parents:
diff changeset
239 }
817ab0e124fa reading files implemented
lost
parents:
diff changeset
240 }
817ab0e124fa reading files implemented
lost
parents:
diff changeset
241 if (c)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
242 goto next_iter_sym;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
243
817ab0e124fa reading files implemented
lost
parents:
diff changeset
244 next_iter:
817ab0e124fa reading files implemented
lost
parents:
diff changeset
245 // a single term
817ab0e124fa reading files implemented
lost
parents:
diff changeset
246 if (s -> head == s -> tail)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
247 return 0;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
248
817ab0e124fa reading files implemented
lost
parents:
diff changeset
249 // search for an operator
817ab0e124fa reading files implemented
lost
parents:
diff changeset
250 for (n = s -> head; n; n = n -> next)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
251 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
252 if (n -> term -> term_type == LW_TERM_OPER)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
253 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
254 if (n -> term -> value == LW_OPER_NEG
817ab0e124fa reading files implemented
lost
parents:
diff changeset
255 || n -> term -> value == LW_OPER_COM
817ab0e124fa reading files implemented
lost
parents:
diff changeset
256 )
817ab0e124fa reading files implemented
lost
parents:
diff changeset
257 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
258 // unary operator
817ab0e124fa reading files implemented
lost
parents:
diff changeset
259 if (n -> prev && n -> prev -> term -> term_type == LW_TERM_INT)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
260 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
261 // a unary operator we can resolve
817ab0e124fa reading files implemented
lost
parents:
diff changeset
262 // we do the op then remove the term "n" is pointing at
817ab0e124fa reading files implemented
lost
parents:
diff changeset
263 if (n -> term -> value == LW_OPER_NEG)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
264 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
265 n -> prev -> term -> value = -(n -> prev -> term -> value);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
266 }
817ab0e124fa reading files implemented
lost
parents:
diff changeset
267 else if (n -> term -> value == LW_OPER_COM)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
268 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
269 n -> prev -> term -> value = ~(n -> prev -> term -> value);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
270 }
817ab0e124fa reading files implemented
lost
parents:
diff changeset
271 n -> prev -> next = n -> next;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
272 if (n -> next)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
273 n -> next -> prev = n -> prev;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
274 else
817ab0e124fa reading files implemented
lost
parents:
diff changeset
275 s -> tail = n -> prev;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
276
817ab0e124fa reading files implemented
lost
parents:
diff changeset
277 lw_expr_term_free(n -> term);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
278 lw_free(n);
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 }
817ab0e124fa reading files implemented
lost
parents:
diff changeset
282 else
817ab0e124fa reading files implemented
lost
parents:
diff changeset
283 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
284 // binary operator
817ab0e124fa reading files implemented
lost
parents:
diff changeset
285 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
286 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
287 // a binary operator we can resolve
817ab0e124fa reading files implemented
lost
parents:
diff changeset
288 switch (n -> term -> value)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
289 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
290 case LW_OPER_PLUS:
817ab0e124fa reading files implemented
lost
parents:
diff changeset
291 n -> prev -> prev -> term -> value += n -> prev -> term -> value;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
292 break;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
293
817ab0e124fa reading files implemented
lost
parents:
diff changeset
294 case LW_OPER_MINUS:
817ab0e124fa reading files implemented
lost
parents:
diff changeset
295 n -> prev -> prev -> term -> value -= n -> prev -> term -> value;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
296 break;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
297
817ab0e124fa reading files implemented
lost
parents:
diff changeset
298 case LW_OPER_TIMES:
817ab0e124fa reading files implemented
lost
parents:
diff changeset
299 n -> prev -> prev -> term -> value *= n -> prev -> term -> value;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
300 break;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
301
817ab0e124fa reading files implemented
lost
parents:
diff changeset
302 case LW_OPER_DIVIDE:
817ab0e124fa reading files implemented
lost
parents:
diff changeset
303 if (n -> prev -> term -> value == 0)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
304 return -1;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
305 n -> prev -> prev -> term -> value /= n -> prev -> term -> value;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
306 break;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
307
817ab0e124fa reading files implemented
lost
parents:
diff changeset
308 case LW_OPER_MOD:
817ab0e124fa reading files implemented
lost
parents:
diff changeset
309 if (n -> prev -> term -> value == 0)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
310 return -1;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
311 n -> prev -> prev -> term -> value %= n -> prev -> term -> value;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
312 break;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
313
817ab0e124fa reading files implemented
lost
parents:
diff changeset
314 case LW_OPER_INTDIV:
817ab0e124fa reading files implemented
lost
parents:
diff changeset
315 if (n -> prev -> term -> value == 0)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
316 return -1;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
317 n -> prev -> prev -> term -> value /= n -> prev -> term -> value;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
318 break;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
319
817ab0e124fa reading files implemented
lost
parents:
diff changeset
320 case LW_OPER_BWAND:
817ab0e124fa reading files implemented
lost
parents:
diff changeset
321 n -> prev -> prev -> term -> value &= n -> prev -> term -> value;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
322 break;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
323
817ab0e124fa reading files implemented
lost
parents:
diff changeset
324 case LW_OPER_BWOR:
817ab0e124fa reading files implemented
lost
parents:
diff changeset
325 n -> prev -> prev -> term -> value |= n -> prev -> term -> value;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
326 break;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
327
817ab0e124fa reading files implemented
lost
parents:
diff changeset
328 case LW_OPER_BWXOR:
817ab0e124fa reading files implemented
lost
parents:
diff changeset
329 n -> prev -> prev -> term -> value ^= n -> prev -> term -> value;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
330 break;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
331
817ab0e124fa reading files implemented
lost
parents:
diff changeset
332 case LW_OPER_AND:
817ab0e124fa reading files implemented
lost
parents:
diff changeset
333 n -> prev -> prev -> term -> value = (n -> prev -> term -> value && n -> prev -> prev -> term -> value) ? 1 : 0;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
334 break;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
335
817ab0e124fa reading files implemented
lost
parents:
diff changeset
336 case LW_OPER_OR:
817ab0e124fa reading files implemented
lost
parents:
diff changeset
337 n -> prev -> prev -> term -> value = (n -> prev -> term -> value || n -> prev -> prev -> term -> value) ? 1 : 0;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
338 break;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
339
817ab0e124fa reading files implemented
lost
parents:
diff changeset
340 default:
817ab0e124fa reading files implemented
lost
parents:
diff changeset
341 // return error if unknown operator!
817ab0e124fa reading files implemented
lost
parents:
diff changeset
342 return -1;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
343 }
817ab0e124fa reading files implemented
lost
parents:
diff changeset
344
817ab0e124fa reading files implemented
lost
parents:
diff changeset
345 // now remove the two unneeded entries from the stack
817ab0e124fa reading files implemented
lost
parents:
diff changeset
346 n -> prev -> prev -> next = n -> next;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
347 if (n -> next)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
348 n -> next -> prev = n -> prev -> prev;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
349 else
817ab0e124fa reading files implemented
lost
parents:
diff changeset
350 s -> tail = n -> prev -> prev;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
351
817ab0e124fa reading files implemented
lost
parents:
diff changeset
352 lw_expr_term_free(n -> term);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
353 lw_expr_term_free(n -> prev -> term);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
354 lw_free(n -> prev);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
355 lw_free(n);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
356 break;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
357 }
817ab0e124fa reading files implemented
lost
parents:
diff changeset
358 }
817ab0e124fa reading files implemented
lost
parents:
diff changeset
359 }
817ab0e124fa reading files implemented
lost
parents:
diff changeset
360 }
817ab0e124fa reading files implemented
lost
parents:
diff changeset
361 // note for the terminally confused about dynamic memory and pointers:
817ab0e124fa reading files implemented
lost
parents:
diff changeset
362 // n will not be NULL even after the lw_free calls above so
817ab0e124fa reading files implemented
lost
parents:
diff changeset
363 // this test will still work (n will be a dangling pointer)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
364 // (n will only be NULL if we didn't find any operators to simplify)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
365 if (n)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
366 goto next_iter;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
367
817ab0e124fa reading files implemented
lost
parents:
diff changeset
368 return 0;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
369 }