-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.c
executable file
·577 lines (505 loc) · 15.9 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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
#include "y3c.h"
// All local variable instances created during parsing are accumulated to this
// list.
Var *locals;
static void print_all_locals() {
printf("LOCALS: [");
for (Var *var = locals; var; var = var->next) {
printf("%s ", var->name);
}
printf("] \n");
}
static Type *typespec(Token **rest, Token *tok);
static Type *declarator(Token **rest, Token *tok, Type *ty);
static Node *declaration(Token **rest, Token *tok);
static Node *multi_statement(Token **rest, Token *tok);
static Node *statement(Token **rest, Token *tok);
static Node *expr_statement(Token **rest, Token *tok);
static Node *expr(Token **rest, Token *tok);
static Node *assign(Token **rest, Token *tok);
static Node *equality(Token **rest, Token *tok);
static Node *relational(Token **rest, Token *tok);
static Node *add(Token **rest, Token *tok);
static Node *mul(Token **rest, Token *tok);
static Node *unary(Token **rest, Token *tok);
static Node *postfix(Token **rest, Token *tok);
static Node *primary(Token **rest, Token *tok);
char *mystrndup(const char *s, size_t n) {
char *new = (char *)malloc(n + 1);
if (new == NULL) {
return NULL;
}
new[n] = '\0';
return (char *)memcpy(new, s, n);
}
static Var *find_var(Token *tok) {
for (Var *var = locals; var; var = var->next) {
if (strlen(var->name) == tok->token_length
&& !strncmp(tok->token_string, var->name, tok->token_length)) {
return var;
}
}
return NULL;
}
static Node *create_new_node(NodeKind kind, Token *tok) {
Node *node = calloc(1, sizeof(Node));
node->kind = kind;
node->tok = tok;
return node;
}
static Node *create_new_binary_node(
NodeKind kind, Node *lhs, Node *rhs, Token *tok) {
Node *node = create_new_node(kind, tok);
node->lhs = lhs;
node->rhs = rhs;
return node;
}
static Node *create_new_unary_node(NodeKind kind, Node *lhs, Token *tok) {
Node *node = create_new_node(kind, tok);
node->lhs = lhs;
return node;
}
static Node *create_new_num_node(int val, Token *tok) {
Node *node = create_new_node(NODE_NUM, tok);
node->val = val;
return node;
}
static Node *create_new_var_node(Var *var, Token *tok) {
Node *node = create_new_node(NODE_VAR, tok);
node->var = var;
return node;
}
static Var *create_new_local_var(char *name, Type *ty) {
Var *var = calloc(1, sizeof(Var));
var->name = name;
var->ty = ty;
var->next = locals;
locals = var;
// offset will be set after all tokens are parsed.
return var;
}
static char *get_identifier(Token *tok) {
if (tok->kind != TOKEN_IDENTIFIER) {
error_tok(tok, "expected an identifier.");
}
return mystrndup(tok->token_string, tok->token_length);
}
// Ensures that the current token is |TOKEN_NUM|.
static int take_number(Token *tok) {
if (tok->kind != TOKEN_NUM)
error_tok(tok, "expected a number.");
return tok->val;
}
// funcdef = typespec declarator multi-statement
static Function *funcdef(Token **rest, Token *tok) {
locals = NULL;
Type *ty = typespec(&tok, tok);
ty = declarator(&tok, tok, ty);
Function *fn = calloc(1, sizeof(Function));
fn->name = get_identifier(ty->name);
for (Type *t = ty->params; t; t = t->next) {
create_new_local_var(get_identifier(t->name), t);
}
fn->params = locals;
tok = skip(tok, "{");
fn->node = multi_statement(&tok, tok)->body;
fn->locals = locals;
tok = skip(tok, "}");
*rest = tok;
return fn;
}
// typespec = "int"
static Type *typespec(Token **rest, Token *tok) {
*rest = skip(tok, "int");
return ty_int;
}
// func-params = (param ( "," param) * ) ? ")"
// param = typespec declarator
static Type *func_params(Token **rest, Token *tok, Type *ty) {
Type head;
head.next = NULL;
Type *tail = &head;
while (!equal(tok, ")")) {
if (tail != &head) {
tok = skip(tok, ",");
}
Type *basety = typespec(&tok, tok);
Type *ty = declarator(&tok, tok, basety);
tail = tail->next = copy_type(ty);
}
ty = func_type(ty);
ty->params = head.next;
tok = skip(tok, ")");
*rest = tok;
return ty;
}
// type-suffix = "(" func-params
// | "[" num "]" type-suffix
// | ε
static Type *type_suffix(Token **rest, Token *tok, Type *ty) {
if (equal(tok, "(")) {
return func_params(rest, tok->next, ty);
}
if (equal(tok, "[")) {
int sz = take_number(tok->next);
tok = skip(tok->next->next, "]");
ty = type_suffix(rest, tok, ty);
return array_of(ty, sz);
}
*rest = tok;
return ty;
}
// declarator = "*" * identifier type-suffix
static Type *declarator(Token **rest, Token *tok, Type *ty) {
while (consume(&tok, tok, "*")) {
ty = pointer_to(ty);
}
if (tok->kind != TOKEN_IDENTIFIER) {
error_tok(tok, "expected a variable name.");
}
ty = type_suffix(rest, tok->next, ty);
ty->name = tok;
return ty;
}
// declaration = typespec (declarator ("=" expr)?
// ("," declarator ("=" expr)? )* )? ";"
static Node *declaration(Token **rest, Token *tok) {
Type *basety = typespec(&tok, tok);
Node head;
head.next = NULL;
Node *tail = &head;
int cnt = 0;
while (!equal(tok, ";")) {
if (cnt++ > 0) {
tok = skip(tok, ",");
}
Type *ty = declarator(&tok, tok, basety);
Var *var = create_new_local_var(get_identifier(ty->name), ty);
if (!equal(tok, "=")) {
continue;
}
Node *lhs = create_new_var_node(var, ty->name);
tok = skip(tok, "=");
Node *rhs = assign(&tok, tok);
Node *node = create_new_binary_node(NODE_ASSIGN, lhs, rhs, tok);
tail = tail->next = create_new_unary_node(NODE_EXPR_STATEMENT, node, tok);
}
Node *node = create_new_node(NODE_BLOCK, tok);
node->body = head.next;
tok = skip(tok, ";");
*rest = tok;
return node;
}
// statement = "return" expr ";"
// | "if" "(" expr ")" statement ("else" statement)?
// | "for" "(" expr? ";" expr? ";" expr? ")" statement
// | "while" "(" expr ")" statement
// | "{" multi_statement "}"
// | expr ";"
static Node *statement(Token **rest, Token *tok) {
if (equal(tok, "return")) {
Node *node = create_new_node(NODE_RETURN, tok);
node->lhs = expr(&tok, tok->next);
*rest = skip(tok, ";");
return node;
}
if (equal(tok, "if")) {
Node *node = create_new_node(NODE_IF, tok);
tok = skip(tok->next, "(");
node->cond = expr(&tok, tok);
tok = skip(tok, ")");
node->then = statement(&tok, tok);
if (equal(tok, "else")) {
node->els = statement(&tok, tok->next);
}
*rest = tok;
return node;
}
if (equal(tok, "for")) {
Node *node = create_new_node(NODE_FOR, tok);
tok = skip(tok->next, "(");
if (!equal(tok, ";")) {
node->init = expr_statement(&tok, tok);
}
tok = skip(tok, ";");
if (!equal(tok, ";")) {
node->cond = expr(&tok, tok);
}
tok = skip(tok, ";");
if (!equal(tok, ")")) {
node->inc = expr_statement(&tok, tok);
}
tok = skip(tok, ")");
node->then = statement(&tok, tok);
*rest = tok;
return node;
}
if (equal(tok, "while")) {
Node *node = create_new_node(NODE_FOR, tok);
tok = skip(tok->next, "(");
node->cond = expr(&tok, tok);
tok = skip(tok, ")");
node->then = statement(rest, tok);
return node;
}
if (equal(tok, "{")) {
Node *node = multi_statement(&tok, tok->next);
tok = skip(tok, "}");
*rest = tok;
return node;
}
Node *node = expr_statement(&tok, tok);
*rest = skip(tok, ";");
return node;
}
// multi_statement = (declaration | statement)*
static Node *multi_statement(Token **rest, Token *tok) {
Node *node = create_new_node(NODE_BLOCK, tok);
Node head;
head.next = NULL;
Node *tail = &head;
while (!equal(tok, "}")) {
if (equal(tok, "int")) {
tail = tail->next = declaration(&tok, tok);
}
else {
tail = tail->next = statement(&tok, tok);
}
add_type(tail);
}
node->body = head.next;
*rest = tok;
return node;
}
// expr_statement = expr
static Node *expr_statement(Token **rest, Token *tok) {
Node *node = create_new_node(NODE_EXPR_STATEMENT, tok);
node->lhs = expr(rest, tok);
return node;
}
// expr = assign
static Node *expr(Token **rest, Token *tok) {
return assign(rest, tok);
}
// assign = equality ("=" assign)?
static Node *assign(Token **rest, Token *tok) {
Node *node = equality(&tok, tok);
if (equal(tok, "=")) {
return create_new_binary_node(
NODE_ASSIGN, node, assign(rest, tok->next), tok);
}
*rest = tok;
return node;
}
// equality = relational ("==" relational | "!=" relational)*
static Node *equality(Token **rest, Token *tok) {
Node *node = relational(&tok, tok);
for (;;) {
if (equal(tok, "==")) {
node = create_new_binary_node(NODE_EQ, node, NULL, tok);
node->rhs = relational(&tok, tok->next);
}
else if (equal(tok, "!=")) {
node = create_new_binary_node(NODE_NE, node, NULL, tok);
node->rhs = relational(&tok, tok->next);
}
else {
*rest = tok;
return node;
}
}
}
// relational = add ("<" add | "<=" add | ">" add | ">=" add)*
static Node *relational(Token **rest, Token *tok) {
Node *node = add(&tok, tok);
for (;;) {
if (equal(tok, "<")) {
node = create_new_binary_node(NODE_LT, node, NULL, tok);
node->rhs = add(&tok, tok->next);
}
else if (equal(tok, "<=")) {
node = create_new_binary_node(NODE_LE, node, NULL, tok);
node->rhs = add(&tok, tok->next);
}
else if (equal(tok, ">")) {
node = create_new_binary_node(NODE_GT, node, NULL, tok);
node->rhs = add(&tok, tok->next);
}
else if (equal(tok, ">=")) {
node = create_new_binary_node(NODE_GE, node, NULL, tok);
node->rhs = add(&tok, tok->next);
}
else {
*rest = tok;
return node;
}
}
}
// In C, '+' operator is overloaded to perform the pointer arithmetic.
// If p is a pointer, p + n adds not n but sizeof (*p) * n to the value of p,
// so that p + n points to the location n elements (not bytes) ahead of p.
// In other words, we neet to scale an integer value before adding to a pointer
// value. This function takes care of the scaling.
static Node *create_new_add_node(Node *lhs, Node *rhs, Token *tok) {
add_type(lhs);
add_type(rhs);
// number + number
if (is_integer(lhs->ty) && is_integer(rhs->ty)) {
return create_new_binary_node(NODE_ADD, lhs, rhs, tok);
}
// pointer + pointer
if (lhs->ty->base && rhs->ty->base) {
error_tok(tok, " pointer + pointer is invalid operands.");
}
// Canonicalize 'number + pointer' to 'pointer + number'.
if (!lhs->ty->base && rhs->ty->base) {
Node *tmp = lhs;
lhs = rhs;
rhs = tmp;
}
rhs = create_new_binary_node(
NODE_MUL, rhs, create_new_num_node(lhs->ty->base->size, tok), tok);
return create_new_binary_node(NODE_ADD, lhs, rhs, tok);
}
// Like '+', '-' is overloaded for the pointer type.
static Node *create_new_sub_node(Node *lhs, Node *rhs, Token *tok) {
add_type(lhs);
add_type(rhs);
// number - number
if (is_integer(lhs->ty) && is_integer(rhs->ty)) {
return create_new_binary_node(NODE_SUB, lhs, rhs, tok);
}
// pointer - number
if (lhs->ty->base && is_integer(rhs->ty)) {
rhs = create_new_binary_node(
NODE_MUL, rhs, create_new_num_node(lhs->ty->base->size, tok), tok);
return create_new_binary_node(NODE_SUB, lhs, rhs, tok);
}
// pointer - pointer
if (lhs->ty->base && rhs->ty->base) {
Node *node = create_new_binary_node(NODE_SUB, lhs, rhs, tok);
return create_new_binary_node(
NODE_DIV, node, create_new_num_node(lhs->ty->base->size, tok), tok);
}
// number - pointer
error_tok(tok, "number - pointer is invalid operands.");
return create_new_num_node(0, tok);
}
// add = mul ("+" mul | "-" mul)*
static Node *add(Token **rest, Token *tok) {
Node *node = mul(&tok, tok);
for (;;) {
Token *start = tok;
if (equal(tok, "+")) {
node = create_new_add_node(node, mul(&tok, tok->next), start);
}
else if (equal(tok, "-")) {
node = create_new_sub_node(node, mul(&tok, tok->next), start);
}
else {
*rest = tok;
return node;
}
}
}
// mul = unary ("*" unary | "/" unary)*
static Node *mul(Token **rest, Token *tok) {
Node *node = unary(&tok, tok);
for (;;) {
if (equal(tok, "*")) {
node = create_new_binary_node(NODE_MUL, node, NULL, tok);
node->rhs = unary(&tok, tok->next);
}
else if (equal(tok, "/")) {
node = create_new_binary_node(NODE_DIV, node, NULL, tok);
node->rhs = unary(&tok, tok->next);
}
else {
*rest = tok;
return node;
}
}
}
// unary = ("+" | "-" | "*" | "&") unary
// | postfix
static Node *unary(Token **rest, Token *tok) {
if (equal(tok, "+")) {
return unary(rest, tok->next);
}
else if (equal(tok, "-")) {
return create_new_binary_node(
NODE_SUB, create_new_num_node(0, tok), unary(rest, tok->next), tok);
}
else if (equal(tok, "&")) {
return create_new_unary_node(NODE_ADDRESS, unary(rest, tok->next), tok);
}
else if (equal(tok, "*")) {
return create_new_unary_node(
NODE_DEREFERENCE, unary(rest, tok->next), tok);
}
return postfix(rest, tok);
}
// func-args = "(" (assign ("," assign)* )? ")"
static Node *func_args(Token **rest, Token *tok) {
Node head;
head.next = NULL;
Node *tail = &head;
while (!equal(tok, ")")) {
if (tail != &head) {
tok = skip(tok, ",");
}
tail = tail->next = assign(&tok, tok);
}
*rest = skip(tok, ")");
return head.next;
}
// postfix = primary ("[" expr "]")*
static Node *postfix(Token **rest, Token *tok) {
Node *node = primary(&tok, tok);
while (equal(tok, "[")) {
Token *start = tok;
Node *idx = expr(&tok, tok->next);
tok = skip(tok, "]");
node = create_new_unary_node(
NODE_DEREFERENCE, create_new_add_node(node, idx, start), start);
}
*rest = tok;
return node;
}
// primary = "(" expr ")" | num | idnetifier func-args?
static Node *primary(Token **rest, Token *tok) {
if (equal(tok, "(")) {
Node *node = expr(&tok, tok->next);
*rest = skip(tok, ")");
return node;
}
if (tok->kind == TOKEN_IDENTIFIER) {
// Function call
if (equal(tok->next, "(")) {
Node *node = create_new_node(NODE_FUNCTION_CALL, tok);
node->funcname = mystrndup(tok->token_string, tok->token_length);
node->args = func_args(rest, tok->next->next);
return node;
}
// Variable
Var *var = find_var(tok);
if (!var) {
error_tok(tok, "undefined variable.");
}
*rest = tok->next;
return create_new_var_node(var, tok);
}
Node *node = create_new_num_node(take_number(tok), tok);
*rest = tok->next;
return node;
}
// program = funcdef*
Function *parse(Token *tok) {
Function head;
head.next = NULL;
Function *tail = &head;
while (tok->kind != TOKEN_EOF) {
tail = tail->next = funcdef(&tok, tok);
}
return head.next;
}