-
Notifications
You must be signed in to change notification settings - Fork 6
/
parse.c
430 lines (394 loc) · 8.93 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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
#include <stdlib.h>
#include <string.h>
#include "kcc.h"
static Vector *tokens;
static int pos;
static Type int_ty = {INT};
static Type char_ty = {CHAR};
static Node *new_node() {
return calloc(1, sizeof(Node));
}
static Node *new_binop(int op, Node *lhs, Node *rhs) {
Node *node = new_node();
node->op = op;
node->lhs = lhs;
node->rhs = rhs;
return node;
}
static Token *next() {
return tokens->data[pos++];
}
static Token *peek() {
return tokens->data[pos];
}
// returns true (1) or false (0)
static int consume(int c) {
Token *t = tokens->data[pos];
if (t->ty != c) {
return 0;
}
pos++;
return 1;
}
static void expect(int ty) {
Token *t = next();
if (t->ty != ty)
error("expected %c (%d), but got %c (%d): %s", ty, ty, t->ty, t->ty, t->input);
}
static int is_typename() {
Token *t = peek();
return t->ty == TK_INT;
}
static Type *type() {
Token *t = next();
switch (t->ty) {
case TK_INT:
return new_type(INT);
case TK_CHAR:
return new_type(CHAR);
default:
error("undefined type: %s", t->input);
return NULL;
}
}
static Node *expr();
static Node *compound_stmt();
static Node *term() {
Token *t = next();
Node *node;
if (t->ty == '(') {
node = expr();
expect(')');
return node;
}
node = new_node();
if (t->ty == TK_NUM) {
node->op = ND_NUM;
node->val = t->val;
node->ty = &int_ty;
return node;
}
if (t->ty == TK_STR) {
node->op = ND_STR;
node->str = t->data;
node->ty = ary_of(&char_ty, strlen(node->str)+1);
return node;
}
if (t->ty == TK_IDENT) {
node->name = t->name;
if (!consume('(')) {
// variable access
node->op = ND_IDENT;
return node;
}
// function call
node->op = ND_CALL;
node->args = new_vector();
if (consume(')'))
return node;
for (int i = 0; i < 6; i++) {
vec_push(node->args, expr());
if (consume(')'))
return node;
expect(',');
}
error("too many argument: %s", t->input);
}
error("expect number or (, defined variable: %s", t->input);
return node;
}
static Node *postfix() {
Node *lhs = term();
while (consume('[')) {
Node *node = new_node();
node->op = ND_DEREF;
node->expr = new_binop('+', lhs, expr());
lhs = node;
expect(']');
}
return lhs;
}
static Node *unary() {
Node *node;
if (consume('*')) {
node = new_node();
node->op = ND_DEREF;
node->expr = unary();
return node;
}
if (consume('&')) {
node = new_node();
node->op = ND_ADDR;
node->expr = unary();
return node;
}
if (consume(TK_SIZEOF)) {
node = new_node();
node->op = ND_SIZEOF;
node->expr = expr();
return node;
}
return postfix();
}
static Node *mul() {
Node *lhs = unary();
for (;;) {
Token *t = peek();
if (t->ty != '*' && t->ty != '/')
break;
pos++;
lhs = new_binop(t->ty, lhs, unary());
}
return lhs;
}
static Node *add() {
Node *lhs = mul();
for (;;) {
Token *t = peek();
if (t->ty != '+' && t->ty != '-')
break;
pos++;
lhs = new_binop(t->ty, lhs, mul());
}
return lhs;
}
static Node *equality() {
Node *lhs = add();
for (;;) {
if (consume(TK_EQ)) {
lhs = new_binop(ND_EQ, lhs, add());
continue;
}
if (consume(TK_NE)) {
lhs = new_binop(ND_NE, lhs, add());
continue;
}
break;
}
return lhs;
}
static Node *logand() {
Node *lhs = equality();
for (;;) {
if (consume(TK_LOGAND)) {
lhs = new_binop(ND_LOGAND, lhs, equality());
continue;
}
break;
}
return lhs;
}
static Node *logor() {
Node *lhs = logand();
for (;;) {
if (consume(TK_LOGOR)) {
lhs = new_binop(ND_LOGOR, lhs, logand());
continue;
}
break;
}
return lhs;
}
static Node *assign() {
Node *lhs = logor();
if (consume('=')) {
return new_binop('=', lhs, expr());
}
return lhs;
}
static Node *stmt();
// alias
static Node *expr() {
if (consume('{')) {
Node *node = new_node();
node->op = ND_STMT_EXPR;
node->expr = compound_stmt();
return node;
}
return assign();
}
static Node *expr_stmt() {
Node *node = new_node();
node->op = ND_EXPR_STMT;
node->expr = expr();
expect(';');
return node;
}
static Type *array_param(Type *ty) {
Vector *lens = new_vector();
while (consume('[')) {
Token *t = next();
// TODO: calc on preprocess
if (t->ty != TK_NUM)
error("expect define array number: %s", t->input);
expect(']');
vec_pushi(lens, t->val);
}
// reverse Type tree
for (int i = lens->len-1; i >= 0; i--) {
ty = ary_of(ty, vec_geti(lens, i));
}
return ty;
}
static Node *param() {
Type *ty = type();
while (consume('*')) {
ty = ptr_of(ty);
}
Token *t = next();
if (t->ty != TK_IDENT)
error("not define variable: %s", t->input);
ty = array_param(ty);
Node *node = new_node();
node->op = ND_VARDEF;
node->name = t->name;
node->ty = ty;
return node;
}
static Node *decl() {
Node *node = param();
if (consume('='))
node->expr = expr();
else
node->expr = NULL;
expect(';');
return node;
}
static Node *stmt() {
Token *t = peek();
Node *node;
switch (t->ty) {
case TK_IF:
pos++;
node = new_node();
node->op = ND_IF;
expect('(');
node->cond = expr();
expect(')');
node->then = stmt();
if (consume(TK_ELSE)) {
node->els = stmt();
}
return node;
case TK_RETURN:
pos++;
node = new_node();
node->op = ND_RETURN;
node->expr = expr();
expect(';');
return node;
case TK_CHAR:
case TK_INT:
return decl();
case TK_FOR:
pos++;
node = new_node();
node->op = ND_FOR;
expect('(');
if (is_typename())
node->init = decl();
else
node->init = expr_stmt();
node->cond = expr();
expect(';');
node->incr = expr();
expect(')');
node->body = stmt();
return node;
case TK_DO:
pos++;
node = new_node();
node->op = ND_DO_WHILE;
node->then = stmt();
expect(TK_WHILE);
expect('(');
node->cond = expr();
expect(')');
expect(';');
return node;
case '{':
pos++;
node = compound_stmt();
return node;
default:
return expr_stmt();
}
}
static Node *compound_stmt() {
Node *node = new_node();
node->op = ND_COMP_STMT;
node->stmts = new_vector();
while (!consume('}')) {
vec_push(node->stmts, stmt());
}
return node;
}
static Vector *argsdef() {
Vector *args = new_vector();
if (consume(')'))
return args;
vec_push(args, param());
for (int i = 0; i < 6; i++) {
if (consume(')'))
return args;
expect(',');
vec_push(args, param());
}
Token *t = tokens->data[pos];
error("too many argument: %s", t->input);
return NULL;
}
static Node *literal() {
Token *t = next();
Node *node = new_node();
if (t->ty == TK_NUM) {
node->op = ND_NUM;
node->val = t->val;
node->ty = &int_ty;
return node;
}
if (t->ty == TK_STR) {
node->op = ND_STR;
node->str = t->data;
node->ty = ary_of(&char_ty, strlen(node->str)+1);
return node;
}
error("must be literal: %s", t->input);
return NULL;
}
static Node *toplevel() {
int is_extern = consume(TK_EXTERN);
Node *node = param();
if (consume('(')) {
// Function definition
if (node->ty->ty == ARY)
error("function can not return array: %s", node->name);
node->args = argsdef();
if (consume(';')) {
node->op = ND_FUNCDEF;
return node;
}
node->op = ND_FUNC;
expect('{');
node->body = compound_stmt();
return node;
}
// Global Variable
node->is_extern = is_extern;
if (!is_extern && consume('=')) {
node->expr = literal();
} else {
node->expr = NULL;
}
expect(';');
return node;
}
Vector *parse(Vector *_tokens) {
tokens = _tokens;
Vector *nodes = new_vector();
pos = 0;
for (Token *t = tokens->data[pos]; t->ty != TK_EOF; t = tokens->data[pos]) {
vec_push(nodes, toplevel());
}
return nodes;
}