-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.c
358 lines (326 loc) · 8.03 KB
/
parse.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/*
* this is the a brute force recursive descent parser
*/
#include "ccc.h"
/*
* parse a statement - this is really the heart of the compiler frontend
* it recursively calls itself
* there is some hair here having to do with scope
*/
struct stmt*
statement(struct stmt *parent) {
#ifdef notdef
struct stmt *st, **pst = 0;
int block = 1;
struct scope *sc;
while (block) {
switch (cur.type) {
case END: // end a block
block = 0;
break;
case BEGIN: // begin a block
gettoken();
push_scope(blockname());
st = makestmt(BEGIN, 0);
st->parent = parent;
statement(parent);
pop_scope();
need(END, END, ER_S_CC);
break;
case IF: // if <condition> <statement>
gettoken();
need(LPAR, LPAR, ER_S_NP);
st = makestmt(IF, expr(PRI_PAREN, parent));
need(RPAR, RPAR, ER_S_NP);
st->chain = statement(st);
if (cur.type == ELSE) { // else <statement>
gettoken();
st->otherwise = statement(st);
}
break;
case BREAK:
gettoken();
need(SEMI, SEMI, ER_S_SN);
st = makestmt(BREAK, 0);
break;
case DEFAULT:
gettoken();
need(SEMI, SEMI, ER_S_SN);
break;
case RETURN:
gettoken();
st = makestmt(RETURN, 0);
if (cur.type != SEMI) {
st->left = expr(PRI_ALL, parent);
}
need(SEMI, SEMI, ER_S_SN);
break;
case SYM:
if (next.type == COLON) {
st = makestmt(LABEL, 0);
st->label = strdup(symbuf);
st->flags |= S_LABEL;
gettoken();
gettoken();
break;
}
/* fall through */
case LPAR:
case STAR:
case INCR:
case DECR:
st = makestmt(EXPR, expr(PRI_ALL, parent));
need(SEMI, SEMI, ER_S_SN);
break;
case FOR: // for (<expr>; <expr>; <expr>) <statement> ;
gettoken();
need(LPAR, LPAR, ER_S_NP);
st = makestmt(FOR, expr(PRI_ALL, parent));
need(SEMI, SEMI, ER_S_SN);
st->middle = expr(PRI_ALL, parent);
need(SEMI, SEMI, ER_S_SN);
st->right = expr(PRI_ALL, parent);
need(RPAR, RPAR, ER_S_NP);
st->chain = statement(st);
break;
case WHILE: // while <condition> <statement> ;
gettoken();
need(LPAR, LPAR, ER_S_NP);
st = makestmt(WHILE, expr(PRI_ALL, parent));
need(RPAR, RPAR, ER_S_NP);
st->chain = statement(st);
break;
case 'E':
recover(SEMI, ER_S_OE);
break;
case SWITCH: // switch (<expr>) <block> ;
gettoken();
need(LPAR, LPAR, ER_S_NP);
st = makestmt(SWITCH, expr(PRI_ALL, parent));
need(RPAR, RPAR, ER_S_NP);
need(BEGIN, BEGIN, ER_S_SB);
st->chain = statement(st);
need(END, END, ER_S_CC);
break;
case CASE:
gettoken();
st = makestmt(CASE, expr(PRI_ALL, parent));
if (!(st->left->flags & E_CONST) || (st->left->type->size != 1)) {
err(ER_S_NC);
}
need(COLON, COLON, ER_S_NL);
break;
case GOTO:
gettoken();
st = makestmt(GOTO, 0);
if (cur.type != SYM) {
recover(ER_S_GL, SEMI);
break;
}
st->label = strdup(symbuf);
gettoken();
need(SEMI, SEMI, ER_S_SN);
break;
case DEFAULT:
gettoken();
need(COLON, COLON, ER_S_NL);
st = makestmt(DEFAULT, 0);
break;
case ';':
gettoken();
st = makestmt(';', 0);
break;
case DO: // do <statement> while <condition> ;
gettoken();
need(BEGIN, SEMI, ER_S_CC);
st = makestmt(DO, 0);
st->chain = statement(st);
if ((cur.type != END || next.type != WHILE) {
err(ER_S_DO);
break;
}
if (cur.type != LPAR) {
err(ER_S_NP);
}
st->left = expr(PRI_ALL, parent);
need(RPAR, SEMI, ER_S_NP);
need(SEMI, SEMI, ER_S_SN);
break;
case ASM:
st->chain = asmblock();
break;
default:
lose();
}
if (!pst) {
if (!st) {
continue;
}
st->flags |= S_PARENT;
}
pst = &st->next;
st->function = v;
st->parent = parent;
} // while
#else
return (struct stmt*) 0;
#endif
}
struct stmt*
makestmt(char op, struct expr *left) {
struct stmt *st;
st = malloc(sizeof(*st));
st->op = op;
st->left = left;
return st;
}
void
parsefunc(struct name *f) {
// v->body = statement(0);
// v->body->flags = S_FUNC;
}
/*
* storage class clauses - many combinations are illogical
*/
#define SC_EXTERN 0x01
#define SC_REGISTER 0x02
#define SC_STATIC 0x04
#define SC_CONST 0x88
#define SC_VOLATILE 0x10
#define SC_AUTO 0x20
char *sclass_bitdefs[] = { "EXTERN", "REGISTER", "STATIC", "CONST", "VOLATILE",
"AUTO"
};
/*
* parse the storage class on a declaration. this is a muddy concept, since
* we've got visibility and storage lumped together, and context also contributes into where our
* thing actually will reside. we're just interested in the parse part.
* so, let's just eat extern, auto, register, volatile, static and const
* in other compilers, bizarre stuff like fortran, far and pascal show up here.
* gripe about bogus combinations.
*
* how this actually resolves into a storage space is a code generator issue
*/
unsigned char
parse_sclass() {
int ret = 0;
int bit;
while (1) {
switch (cur.type) {
case EXTERN:
bit = SC_EXTERN;
break;
case REGISTER:
bit = SC_REGISTER;
break;
case STATIC:
bit = SC_STATIC;
break;
case CONST:
bit = SC_CONST;
break;
case VOLATILE:
bit = SC_VOLATILE;
break;
case AUTO:
bit = SC_AUTO;
break;
default:
bit = 0;
break;
}
if (bit) {
if (ret & bit) {
err(ER_P_SC);
}
ret |= bit;
gettoken();
} else {
break;
}
}
// bogosity checks
if ((ret & SC_EXTERN)
& (ret & (SC_CONST | SC_STATIC | SC_AUTO | SC_REGISTER))) {
err(ER_P_SC);
}
if ((ret & SC_REGISTER) & (ret & (SC_CONST | SC_STATIC))) {
err(ER_P_SC);
}
if ((ret & SC_STATIC) & (ret & (SC_CONST | SC_AUTO))) {
err(ER_P_SC);
}
if ((ret & SC_CONST) & (ret & (SC_VOLATILE))) {
err(ER_P_SC);
}
return ret;
}
/*
* read a declaration
*/
void
declaration() {
struct type *base;
struct name *n;
struct initial *i;
char sclass;
struct type *basetype;
struct name *v;
while (1) {
sclass = parse_sclass();
basetype = 0;
v = declare(&basetype);
#ifdef notdef
if (v->type & T_FUNC) {
if (cur.type == BEGIN) {
parsefunc(v);
if (sclass == STATIC) {
v->flags |= V_STATIC;
}
v->flags |= V_GLOBAL;
v->next = global;
global = v;
break;
}
}
if (cur.type == ASSIGN) {
do_initializer();
}
#endif
if (cur.type == COMMA) {
gettoken();
continue;
}
if (cur.type == SEMI) {
break;
}
}
}
char bnbuf[20];
char*
blockname() {
static int blockid = 0;
sprintf(bnbuf, "block %d", blockid++);
return strdup(bnbuf);
}
void
block() {
}
/*
* global level parse
*/
void
parse() {
push_scope("global");
initbasictype();
while (cur.type != E_O_F) {
while (cur.type == NONE) {
gettoken();
}
declaration();
}
pop_scope();
}
/*
* vim: tabstop=4 shiftwidth=4 expandtab:
*/