annotate lwlink/expr.c @ 426:652eee8f0c82

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