annotate src/expr.c @ 305:10cc87f4b16c

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