annotate eval.c @ 21:e3fab8f1b52c

strip comments.
author David A. Holland
date Mon, 20 Dec 2010 04:15:02 -0500
parents 9dda765ee85c
children 8a955e3dda2c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
16
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
1 #include <stdlib.h>
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
2 #include <string.h>
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
3 #include <limits.h>
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
4 #include <errno.h>
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
5
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
6 #include "utils.h"
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
7 #include "array.h"
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
8 #include "mode.h"
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
9 #include "place.h"
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
10 #include "eval.h"
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
11
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
12 /*
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
13 * e ::=
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
14 * e1 ? e2 : e3
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
15 * e1 || e2
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
16 * e1 && e2
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
17 * e1 | e2
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
18 * e1 ^ e2
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
19 * e1 & e2
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
20 * e1 == e2 | e1 != e2
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
21 * e1 < e2 | e1 <= e2 | e1 > e2 | e1 >= e2
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
22 * e1 << e2 | e1 >> e2
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
23 * e1 + e2 | e1 - e2
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
24 * e1 * e2 | e1 / e2 | e1 % e2
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
25 * !e | ~e | -e | +e
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
26 * ( e ) | ident
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
27 */
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
28
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
29 enum tokens {
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
30 T_EOF, /* end of input */
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
31 T_VAL, /* value */
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
32 T_LPAREN, /* parens */
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
33 T_RPAREN,
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
34 T_PIPEPIPE, /* operators */
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
35 T_AMPAMP,
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
36 T_EQEQ,
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
37 T_BANGEQ,
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
38 T_LTEQ,
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
39 T_GTEQ,
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
40 T_LTLT,
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
41 T_GTGT,
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
42 T_QUES,
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
43 T_COLON,
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
44 T_PIPE,
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
45 T_CARET,
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
46 T_AMP,
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
47 T_LT,
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
48 T_GT,
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
49 T_PLUS,
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
50 T_MINUS,
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
51 T_STAR,
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
52 T_SLASH,
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
53 T_PCT,
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
54 T_BANG,
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
55 T_TILDE,
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
56 };
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
57
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
58 static const struct {
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
59 char c1, c2;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
60 enum tokens tok;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
61 } tokens_2[] = {
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
62 { '|', '|', T_PIPEPIPE },
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
63 { '&', '&', T_AMPAMP },
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
64 { '=', '=', T_EQEQ },
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
65 { '!', '=', T_BANGEQ },
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
66 { '<', '=', T_LTEQ },
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
67 { '>', '=', T_GTEQ },
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
68 { '<', '<', T_LTLT },
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
69 { '>', '>', T_GTGT },
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
70 };
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
71 static const unsigned num_tokens_2 = HOWMANY(tokens_2);
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
72
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
73 static const struct {
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
74 char c1;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
75 enum tokens tok;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
76 } tokens_1[] = {
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
77 { '?', T_QUES },
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
78 { ':', T_COLON },
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
79 { '|', T_PIPE },
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
80 { '^', T_CARET },
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
81 { '&', T_AMP },
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
82 { '<', T_LT },
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
83 { '>', T_GT },
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
84 { '+', T_PLUS },
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
85 { '-', T_MINUS },
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
86 { '*', T_STAR },
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
87 { '/', T_SLASH },
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
88 { '%', T_PCT },
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
89 { '!', T_BANG },
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
90 { '~', T_TILDE },
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
91 { '(', T_LPAREN },
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
92 { ')', T_RPAREN },
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
93 };
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
94 static const unsigned num_tokens_1 = HOWMANY(tokens_1);
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
95
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
96 struct token {
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
97 struct place place;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
98 enum tokens tok;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
99 int val;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
100 };
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
101 DECLARRAY(token);
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
102 DEFARRAY(token, );
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
103
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
104 static struct tokenarray tokens;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
105
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
106 static
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
107 struct token *
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
108 token_create(const struct place *p, enum tokens tok, int val)
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
109 {
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
110 struct token *t;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
111
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
112 t = domalloc(sizeof(*t));
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
113 t->place = *p;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
114 t->tok = tok;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
115 t->val = val;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
116 return t;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
117 }
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
118
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
119 static
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
120 void
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
121 token_destroy(struct token *t)
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
122 {
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
123 free(t);
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
124 }
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
125
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
126 DESTROYALL_ARRAY(token, );
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
127
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
128 static
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
129 bool
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
130 isuop(enum tokens tok)
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
131 {
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
132 switch (tok) {
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
133 case T_BANG:
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
134 case T_TILDE:
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
135 case T_MINUS:
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
136 case T_PLUS:
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
137 return true;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
138 default:
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
139 break;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
140 }
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
141 return false;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
142 }
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
143
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
144 static
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
145 bool
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
146 isbop(enum tokens tok)
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
147 {
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
148 switch (tok) {
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
149 case T_EOF:
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
150 case T_VAL:
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
151 case T_LPAREN:
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
152 case T_RPAREN:
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
153 case T_COLON:
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
154 case T_QUES:
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
155 case T_BANG:
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
156 case T_TILDE:
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
157 return false;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
158 default:
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
159 break;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
160 }
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
161 return true;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
162 }
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
163
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
164 static
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
165 bool
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
166 isop(enum tokens tok)
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
167 {
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
168 switch (tok) {
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
169 case T_EOF:
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
170 case T_VAL:
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
171 case T_LPAREN:
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
172 case T_RPAREN:
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
173 return false;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
174 default:
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
175 break;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
176 }
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
177 return true;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
178 }
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
179
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
180 static
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
181 int
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
182 getprec(enum tokens tok)
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
183 {
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
184 switch (tok) {
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
185 case T_STAR: case T_SLASH: case T_PCT: return 0;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
186 case T_PLUS: case T_MINUS: return 1;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
187 case T_LTLT: case T_GTGT: return 2;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
188 case T_LT: case T_LTEQ: case T_GT: case T_GTEQ: return 3;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
189 case T_EQEQ: case T_BANGEQ: return 4;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
190 case T_AMP: return 5;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
191 case T_CARET: return 6;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
192 case T_PIPE: return 7;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
193 case T_AMPAMP: return 8;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
194 case T_PIPEPIPE: return 9;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
195 default: break;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
196 }
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
197 return 10;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
198 }
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
199
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
200 static
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
201 bool
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
202 looser(enum tokens t1, enum tokens t2)
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
203 {
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
204 return getprec(t1) > getprec(t2);
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
205 }
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
206
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
207 static
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
208 int
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
209 eval_uop(enum tokens op, int val)
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
210 {
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
211 switch (op) {
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
212 case T_BANG: val = !val; break;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
213 case T_TILDE: val = (int)~(unsigned)val; break;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
214 case T_MINUS: val = -val; break;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
215 case T_PLUS: break;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
216 default: assert(0); break;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
217 }
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
218 return val;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
219 }
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
220
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
221 static
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
222 int
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
223 eval_bop(struct place *p, enum tokens op, int lv, int rv)
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
224 {
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
225 unsigned mask;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
226
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
227 switch (op) {
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
228 case T_PIPEPIPE: return lv || rv;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
229 case T_AMPAMP: return lv && rv;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
230 case T_PIPE: return (int)((unsigned)lv | (unsigned)rv);
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
231 case T_CARET: return (int)((unsigned)lv ^ (unsigned)rv);
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
232 case T_AMP: return (int)((unsigned)lv & (unsigned)rv);
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
233 case T_EQEQ: return lv == rv;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
234 case T_BANGEQ: return lv != rv;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
235 case T_LT: return lv < rv;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
236 case T_GT: return lv > rv;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
237 case T_LTEQ: return lv <= rv;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
238 case T_GTEQ: return lv >= rv;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
239
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
240 case T_LTLT:
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
241 case T_GTGT:
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
242 if (rv < 0) {
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
243 complain(p, "Negative bit-shift");
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
244 complain_fail();
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
245 rv = 0;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
246 }
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
247 if ((unsigned)rv >= CHAR_BIT * sizeof(unsigned)) {
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
248 complain(p, "Bit-shift farther than type width");
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
249 complain_fail();
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
250 rv = 0;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
251 }
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
252 if (op == T_LTLT) {
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
253 return (int)((unsigned)lv << (unsigned)rv);
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
254 }
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
255 mask = ((unsigned)-1) << (CHAR_BIT * sizeof(unsigned) - rv);
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
256 lv = (int)(((unsigned)lv >> (unsigned)rv) | mask);
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
257 return lv;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
258
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
259 case T_MINUS:
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
260 if (rv == INT_MIN) {
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
261 if (lv == INT_MIN) {
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
262 return 0;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
263 }
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
264 lv--;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
265 rv++;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
266 }
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
267 rv = -rv;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
268 /* FALLTHROUGH */
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
269 case T_PLUS:
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
270 if (rv > 0 && lv > (INT_MAX - rv)) {
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
271 complain(p, "Integer overflow");
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
272 complain_fail();
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
273 return INT_MAX;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
274 }
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
275 if (rv < 0 && lv < (INT_MIN - rv)) {
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
276 complain(p, "Integer underflow");
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
277 complain_fail();
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
278 return INT_MIN;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
279 }
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
280 return lv + rv;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
281
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
282 case T_STAR:
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
283 if (rv == 0) {
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
284 return 0;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
285 }
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
286 if (rv == 1) {
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
287 return lv;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
288 }
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
289 if (rv == -1 && lv == INT_MIN) {
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
290 lv++;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
291 lv = -lv;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
292 if (lv == INT_MAX) {
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
293 complain(p, "Integer overflow");
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
294 complain_fail();
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
295 return INT_MAX;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
296 }
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
297 lv++;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
298 return lv;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
299 }
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
300 if (lv == INT_MIN && rv < 0) {
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
301 complain(p, "Integer overflow");
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
302 complain_fail();
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
303 return INT_MAX;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
304 }
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
305 if (lv == INT_MIN && rv > 0) {
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
306 complain(p, "Integer underflow");
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
307 complain_fail();
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
308 return INT_MIN;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
309 }
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
310 if (rv < 0) {
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
311 rv = -rv;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
312 lv = -lv;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
313 }
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
314 if (lv > 0 && lv > INT_MAX / rv) {
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
315 complain(p, "Integer overflow");
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
316 complain_fail();
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
317 return INT_MAX;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
318 }
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
319 if (lv < 0 && lv < INT_MIN / rv) {
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
320 complain(p, "Integer underflow");
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
321 complain_fail();
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
322 return INT_MIN;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
323 }
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
324 return lv * rv;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
325
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
326 case T_SLASH:
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
327 if (rv == 0) {
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
328 complain(p, "Division by zero");
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
329 complain_fail();
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
330 return 0;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
331 }
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
332 return lv / rv;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
333
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
334 case T_PCT:
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
335 if (rv == 0) {
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
336 complain(p, "Modulus by zero");
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
337 complain_fail();
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
338 return 0;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
339 }
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
340 return lv % rv;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
341
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
342 default: assert(0); break;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
343 }
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
344 return 0;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
345 }
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
346
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
347 static
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
348 void
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
349 tryreduce(void)
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
350 {
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
351 unsigned num;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
352 struct token *t1, *t2, *t3, *t4, *t5, *t6;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
353
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
354 while (1) {
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
355 num = tokenarray_num(&tokens);
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
356 t1 = (num >= 1) ? tokenarray_get(&tokens, num-1) : NULL;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
357 t2 = (num >= 2) ? tokenarray_get(&tokens, num-2) : NULL;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
358 t3 = (num >= 3) ? tokenarray_get(&tokens, num-3) : NULL;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
359
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
360 if (num >= 3 &&
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
361 t3->tok == T_LPAREN &&
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
362 t2->tok == T_VAL &&
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
363 t1->tok == T_RPAREN) {
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
364 /* (x) -> x */
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
365 t2->place = t3->place;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
366 token_destroy(t1);
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
367 token_destroy(t3);
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
368 tokenarray_remove(&tokens, num-1);
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
369 tokenarray_remove(&tokens, num-3);
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
370 continue;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
371 }
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
372
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
373 if (num >= 2 &&
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
374 (num == 2 || isop(t3->tok) || t3->tok == T_LPAREN) &&
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
375 isuop(t2->tok) &&
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
376 t1->tok == T_VAL) {
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
377 /* unary operator */
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
378 t1->val = eval_uop(t2->tok, t1->val);
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
379 t1->place = t2->place;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
380 token_destroy(t2);
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
381 tokenarray_remove(&tokens, num-2);
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
382 continue;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
383 }
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
384 if (num >= 2 &&
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
385 (num == 2 || isop(t3->tok) || t3->tok == T_RPAREN) &&
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
386 t2->tok != T_LPAREN && t2->tok != T_VAL &&
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
387 t1->tok == T_VAL) {
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
388 complain(&t2->place, "Invalid unary operator");
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
389 complain_fail();
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
390 token_destroy(t2);
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
391 tokenarray_remove(&tokens, num-2);
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
392 continue;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
393 }
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
394
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
395
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
396 t4 = (num >= 4) ? tokenarray_get(&tokens, num-4) : NULL;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
397
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
398 if (num >= 4 &&
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
399 t4->tok == T_VAL &&
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
400 isbop(t3->tok) &&
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
401 t2->tok == T_VAL &&
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
402 (isbop(t1->tok) || !isop(t1->tok))) {
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
403 /* binary operator */
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
404 if (looser(t1->tok, t3->tok)) {
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
405 t4->val = eval_bop(&t3->place,
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
406 t4->val, t3->tok, t2->val);
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
407 token_destroy(t2);
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
408 token_destroy(t3);
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
409 tokenarray_remove(&tokens, num-2);
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
410 tokenarray_remove(&tokens, num-3);
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
411 continue;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
412 }
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
413 break;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
414 }
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
415
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
416 t5 = (num >= 5) ? tokenarray_get(&tokens, num-5) : NULL;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
417 t6 = (num >= 6) ? tokenarray_get(&tokens, num-6) : NULL;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
418
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
419 if (num >=6 &&
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
420 t6->tok == T_VAL &&
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
421 t5->tok == T_QUES &&
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
422 t4->tok == T_VAL &&
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
423 t3->tok == T_COLON &&
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
424 t2->tok == T_VAL &&
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
425 !isop(t1->tok)) {
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
426 /* conditional expression */
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
427 t6->val = t6->val ? t4->val : t2->val;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
428 token_destroy(t2);
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
429 token_destroy(t3);
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
430 token_destroy(t4);
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
431 token_destroy(t5);
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
432 tokenarray_remove(&tokens, num-2);
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
433 tokenarray_remove(&tokens, num-3);
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
434 tokenarray_remove(&tokens, num-4);
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
435 tokenarray_remove(&tokens, num-5);
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
436 continue;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
437 }
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
438
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
439 if (num >= 2 &&
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
440 t2->tok == T_LPAREN &&
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
441 t1->tok == T_RPAREN) {
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
442 complain(&t1->place, "Value expected within ()");
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
443 complain_fail();
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
444 t1->tok = T_VAL;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
445 t1->val = 0;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
446 token_destroy(t1);
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
447 tokenarray_remove(&tokens, num-1);
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
448 continue;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
449 }
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
450
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
451 if (num >= 2 &&
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
452 t2->tok == T_VAL &&
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
453 t1->tok == T_VAL) {
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
454 complain(&t1->place, "Operator expected");
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
455 complain_fail();
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
456 token_destroy(t1);
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
457 tokenarray_remove(&tokens, num-1);
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
458 continue;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
459 }
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
460
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
461 if (num >= 2 &&
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
462 isop(t2->tok) &&
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
463 t1->tok == T_EOF) {
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
464 complain(&t1->place, "Value expected after operator");
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
465 complain_fail();
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
466 token_destroy(t2);
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
467 tokenarray_remove(&tokens, num-2);
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
468 continue;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
469 }
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
470
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
471 if (num == 2 &&
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
472 t2->tok == T_VAL &&
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
473 t1->tok == T_RPAREN) {
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
474 complain(&t1->place, "Excess right parenthesis");
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
475 complain_fail();
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
476 token_destroy(t1);
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
477 tokenarray_remove(&tokens, num-1);
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
478 continue;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
479 }
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
480
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
481 if (num == 3 &&
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
482 t3->tok == T_LPAREN &&
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
483 t2->tok == T_VAL &&
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
484 t1->tok == T_EOF) {
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
485 complain(&t1->place, "Unclosed left parenthesis");
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
486 complain_fail();
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
487 token_destroy(t3);
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
488 tokenarray_remove(&tokens, num-3);
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
489 continue;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
490 }
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
491
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
492 if (num == 2 &&
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
493 t2->tok == T_VAL &&
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
494 t1->tok == T_EOF) {
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
495 /* accepting state */
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
496 break;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
497 }
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
498
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
499 if (num >= 1 &&
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
500 t1->tok == T_EOF) {
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
501 /* any other configuration at eof is an error */
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
502 complain(&t1->place, "Parse error");
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
503 complain_fail();
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
504 break;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
505 }
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
506
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
507 /* otherwise, wait for more input */
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
508 break;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
509 }
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
510 }
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
511
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
512 static
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
513 void
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
514 token(struct place *p, enum tokens tok, int val)
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
515 {
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
516 struct token *t;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
517
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
518 t = token_create(p, tok, val);
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
519
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
520 tokenarray_add(&tokens, t, NULL);
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
521 tryreduce();
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
522 }
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
523
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
524 static
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
525 int
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
526 wordval(struct place *p, char *word)
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
527 {
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
528 unsigned long val;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
529 char *t;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
530
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
531 if (word[0] >= '0' && word[0] <= '9') {
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
532 errno = 0;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
533 val = strtoul(word, &t, 0);
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
534 if (errno || *t != '\0') {
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
535 complain(p, "Invalid integer constant");
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
536 complain_fail();
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
537 return 0;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
538 }
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
539 if (val > INT_MAX) {
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
540 complain(p, "Integer constant too large");
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
541 complain_fail();
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
542 return INT_MAX;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
543 }
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
544 return val;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
545 }
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
546
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
547 /* if it's a symbol, warn and substitute 0. */
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
548 if (warns.undef) {
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
549 complain(p, "Warning: value of undefined symbol %s is 0",
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
550 word);
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
551 if (mode.werror) {
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
552 complain_fail();
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
553 }
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
554 }
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
555 return 0;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
556 }
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
557
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
558 static
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
559 bool
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
560 check_word(struct place *p, char *expr, size_t pos, size_t *len_ret)
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
561 {
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
562 size_t len;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
563 int val;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
564 char tmp;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
565
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
566 if (!strchr(alnum, expr[pos])) {
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
567 return false;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
568 }
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
569 len = strspn(expr + pos, alnum);
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
570 tmp = expr[pos + len];
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
571 expr[pos + len] = '\0';
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
572 val = wordval(p, expr + pos);
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
573 expr[pos + len] = tmp;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
574 token(p, T_VAL, val);
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
575 *len_ret = len;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
576 return true;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
577 }
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
578
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
579 static
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
580 bool
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
581 check_tokens_2(struct place *p, char *expr, size_t pos)
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
582 {
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
583 unsigned i;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
584
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
585 for (i=0; i<num_tokens_2; i++) {
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
586 if (expr[pos] == tokens_2[i].c1 &&
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
587 expr[pos+1] == tokens_2[i].c2) {
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
588 token(p, tokens_2[i].tok, 0);
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
589 return true;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
590 }
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
591 }
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
592 return false;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
593 }
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
594
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
595 static
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
596 bool
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
597 check_tokens_1(struct place *p, char *expr, size_t pos)
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
598 {
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
599 unsigned i;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
600
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
601 for (i=0; i<num_tokens_1; i++) {
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
602 if (expr[pos] == tokens_1[i].c1) {
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
603 token(p, tokens_1[i].tok, 0);
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
604 return true;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
605 }
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
606 }
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
607 return false;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
608 }
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
609
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
610 static
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
611 void
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
612 tokenize(struct place *p, char *expr)
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
613 {
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
614 size_t pos, len;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
615
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
616 pos = 0;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
617 while (expr[pos] != '\0') {
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
618 len = strspn(expr+pos, ws);
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
619 pos += len;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
620 p->column += len;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
621 if (check_word(p, expr, pos, &len)) {
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
622 pos += len;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
623 p->column += len;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
624 continue;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
625 }
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
626 if (check_tokens_2(p, expr, pos)) {
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
627 pos += 2;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
628 p->column += 2;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
629 continue;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
630 }
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
631 if (check_tokens_1(p, expr, pos)) {
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
632 pos++;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
633 p->column++;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
634 continue;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
635 }
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
636 complain(p, "Invalid character %u in #if-expression",
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
637 (unsigned char)expr[pos]);
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
638 complain_fail();
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
639 pos++;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
640 p->column++;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
641 }
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
642 token(p, T_EOF, 0);
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
643 }
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
644
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
645 bool
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
646 eval(struct place *p, char *expr)
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
647 {
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
648 struct token *t1, *t2;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
649 unsigned num;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
650 bool result;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
651
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
652 tokenarray_init(&tokens);
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
653 tokenize(p, expr);
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
654
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
655 result = false;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
656 num = tokenarray_num(&tokens);
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
657 if (num == 2) {
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
658 t1 = tokenarray_get(&tokens, num-1);
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
659 t2 = tokenarray_get(&tokens, num-2);
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
660 if (t1->tok == T_VAL &&
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
661 t2->tok == T_EOF) {
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
662 result = t1->val != 0;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
663 }
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
664 }
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
665
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
666 tokenarray_destroyall(&tokens);
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
667 tokenarray_cleanup(&tokens);
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
668 return result;
9dda765ee85c expression evaluator
David A. Holland
parents:
diff changeset
669 }