annotate eval.c @ 64:f50b4ea6cbfe

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