Debian Codegraph @

cc/cpp/cpc.c

raw symbols SCIP
1 /* $Id: cpc.c,v 1.8 2016/08/08 16:38:29 ragge Exp $ */
2
3 /*
4 * Copyright (c) 2014 Anders Magnusson ([email protected]).
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 /*
29 * Small recursive parser for #if statements.
30 */
31
32 #include "cpp.h"
33
34 static int ctok;
35
36 typedef struct nd ND;
37 struct nd yynode;
38
39 void qloop(void (*fun)(ND *), ND *n1, int a0, int a1, int a2, int a3);
40 void shft(void);
41 int yyparse(void);
42 void expr(ND *n1);
43 void eqcol(ND *n1);
44 void eoror(ND *n1);
45 void eandand(ND *n1);
46 void exor(ND *n1);
47 void eand(ND *n1);
48 void eqne(ND *n1);
49 void eget(ND *n1);
50 void elrs(ND *n1);
51 void eplmin(ND *n1);
52 void emdv(ND *n1);
53 void eterm(ND *n1);
54 void eval(int op, ND *n1, ND *n2);
55
56
57 void
58 shft(void)
59 {
60 ctok = yylex();
61 }
62
63 int
64 yyparse(void)
65 {
66 ND n1;
67
68 shft();
69 expr(&n1);
70 if (n1.op == 0)
71 error("division by zero");
72 if (ctok != WARN)
73 error("junk after expression");
74 return (int)n1.nd_val;
75 }
76
77 /*
78 * evaluate a complete preprocessor #if expression.
79 */
80 void
81 expr(ND *n1)
82 {
83 for (;;) {
84 eqcol(n1);
85 if (ctok != ',')
86 return;
87 shft();
88 }
89 }
90
91 /*
92 * evaluate ?: conditionals.
93 */
94 void
95 eqcol(ND *n1)
96 {
97 ND n2, n3;
98
99 eoror(n1);
100 if (ctok != '?')
101 return;
102
103 shft();
104 expr(&n2);
105 if (ctok != ':')
106 error("no : found");
107 shft();
108 eqcol(&n3);
109 if (n1->nd_val)
110 n1->nd_val = n2.nd_val, n1->op = n2.op;
111 else
112 n1->nd_val = n3.nd_val, n1->op = n3.op;
113 }
114
115 void
116 eoror(ND *n1)
117 {
118 qloop(eandand, n1, OROR, 0, 0, 0);
119 }
120
121 void
122 eandand(ND *n1)
123 {
124 qloop(exor, n1, ANDAND, 0, 0, 0);
125 }
126
127 void
128 exor(ND *n1)
129 {
130 qloop(eand, n1, '|', '^', 0, 0);
131 }
132
133 void
134 eand(ND *n1)
135 {
136 qloop(eqne, n1, '&', 0, 0, 0);
137 }
138
139 void
140 eqne(ND *n1)
141 {
142 qloop(eget, n1, EQ, NE, 0, 0);
143 }
144
145 void
146 eget(ND *n1)
147 {
148 qloop(elrs, n1, '<', '>', LE, GE);
149 }
150
151 void
152 elrs(ND *n1)
153 {
154 qloop(eplmin, n1, LS, RS, 0, 0);
155 }
156
157 void
158 eplmin(ND *n1)
159 {
160 qloop(emdv, n1, '+', '-', 0, 0);
161 }
162
163 void
164 emdv(ND *n1)
165 {
166 qloop(eterm, n1, '*', '/', '%', 0);
167 }
168
169 /*
170 * Loop to evaluate all operators on the same precedence level.
171 */
172 void
173 qloop(void (*fun)(ND *), ND *n1, int a0, int a1, int a2, int a3)
174 {
175 ND n2;
176 int op;
177
178 fun(n1);
179 while (ctok == a0 || ctok == a1 || ctok == a2 || ctok == a3) {
180 op = ctok;
181 shft();
182 fun(&n2);
183 eval(op, n1, &n2);
184 }
185 }
186
187 static void
188 gnum(int o, ND *n1)
189 {
190 n1->op = yynode.op;
191 n1->nd_val = yynode.nd_val;
192 shft();
193 }
194
195 /*
196 * Highest precedence operators + numeric terminals.
197 */
198 void
199 eterm(ND *n1)
200 {
201 int o = ctok;
202
203 switch (o) {
204 case '~':
205 case '!':
206 case '+':
207 case '-':
208 shft();
209 eterm(n1);
210 eval(o, n1, 0);
211 break;
212
213 case NUMBER:
214 case UNUMBER:
215 gnum(o, n1);
216 break;
217
218 case '(':
219 shft();
220 expr(n1);
221 if (ctok == ')') {
222 shft();
223 break;
224 }
225 /* FALLTHROUGH */
226 default:
227 error("bad terminal (%d)", o);
228 break;
229 }
230 }
231
232 /*
233 * keep all numeric evaluation here.
234 * evaluated value returned in n1.
235 */
236 void
237 eval(int op, ND *n1, ND *n2)
238 {
239
240 if ((op == '/' || op == '%') && n2->nd_val == 0)
241 n1->op = 0;
242
243 if (n1->op == 0)
244 return; /* div by zero involved */
245
246 /* unary ops */
247 if (n2 == 0) {
248 switch (op) {
249 case '+': break;
250 case '-': n1->nd_val = -n1->nd_val; break;
251 case '~': n1->nd_val = ~n1->nd_val; break;
252 case '!': n1->nd_val = !n1->nd_val; n1->op = NUMBER; break;
253 }
254 return;
255 }
256
257 if (op == OROR && n1->nd_val) {
258 n1->nd_val = 1, n1->op = NUMBER;
259 return;
260 }
261 if (op == ANDAND && n1->nd_val == 0) {
262 n1->op = NUMBER;
263 return;
264 }
265
266 if (n2->op == 0) {
267 n1->op = 0;
268 return;
269 }
270
271 if (n2->op == UNUMBER)
272 n1->op = UNUMBER;
273
274 switch (op) {
275 case OROR:
276 if (n2->nd_val)
277 n1->nd_val = 1;
278 n1->op = NUMBER;
279 break;
280 case ANDAND:
281 n1->nd_val = n2->nd_val != 0;
282 n1->op = NUMBER;
283 break;
284 case '+': n1->nd_val += n2->nd_val; break;
285 case '-': n1->nd_val -= n2->nd_val; break;
286 case '|': n1->nd_val |= n2->nd_val; break;
287 case '^': n1->nd_val ^= n2->nd_val; break;
288 case '&': n1->nd_val &= n2->nd_val; break;
289 case LS: n1->nd_val <<= n2->nd_val; break;
290 case EQ: n1->nd_val = n1->nd_val == n2->nd_val; n1->op = NUMBER; break;
291 case NE: n1->nd_val = n1->nd_val != n2->nd_val; n1->op = NUMBER; break;
292 }
293
294 if (n1->op == NUMBER) {
295 switch (op) {
296 case '*': n1->nd_val *= n2->nd_val; break;
297 case '/': n1->nd_val /= n2->nd_val; break;
298 case '%': n1->nd_val %= n2->nd_val; break;
299 case '<': n1->nd_val = n1->nd_val < n2->nd_val; break;
300 case '>': n1->nd_val = n1->nd_val > n2->nd_val; break;
301 case LE: n1->nd_val = n1->nd_val <= n2->nd_val; break;
302 case GE: n1->nd_val = n1->nd_val >= n2->nd_val; break;
303 case RS: n1->nd_val >>= n2->nd_val; break;
304 }
305 } else /* op == UNUMBER */ {
306 switch (op) {
307 case '*': n1->nd_uval *= n2->nd_uval; break;
308 case '/': n1->nd_uval /= n2->nd_uval; break;
309 case '%': n1->nd_uval %= n2->nd_uval; break;
310 case '<': n1->nd_uval = n1->nd_uval < n2->nd_uval; break;
311 case '>': n1->nd_uval = n1->nd_uval > n2->nd_uval; break;
312 case LE: n1->nd_uval = n1->nd_uval <= n2->nd_uval; break;
313 case GE: n1->nd_uval = n1->nd_uval >= n2->nd_uval; break;
314 case RS: n1->nd_uval >>= n2->nd_uval; break;
315 }
316 }
317 }