-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.c
963 lines (833 loc) · 21.2 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
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
#include "kmcc.h"
// ローカル変数,グローバル変数,typedefのためのスコープ
typedef struct VarScope VarScope;
struct VarScope {
VarScope *next;
char *name;
Var *var;
Type *type_def;
};
// struct tagsのためのスコープ
typedef struct TagScope TagScope;
struct TagScope {
TagScope *next;
char *name;
Type *ty;
};
typedef struct {
VarScope *var_scope;
TagScope *tag_scope;
} Scope;
// すべてのローカル変数
static VarList *locals;
// グローバル変数のリスト
static VarList *globals;
// Cには2つのブロックスコープがある.
// 変数/typedefと,構造体のタグ
// 以下2つの変数は,現在のスコープ情報を保持
static VarScope *var_scope;
static TagScope *tag_scope;
// sc は,それまでのスコープ情報を一時保存
// leave時に,一時保存していたscをもとに戻す.
static Scope *enter_scope() {
Scope *sc = calloc(1, sizeof(Scope));
sc->var_scope = var_scope;
sc->tag_scope = tag_scope;
return sc;
}
static void leave_scope(Scope *sc) {
var_scope = sc->var_scope;
tag_scope = sc->tag_scope;
}
// 変数を名前で検索する.見つからなかった場合は,NULLを返す.
static VarScope *find_var(Token *tok) {
for (VarScope *sc = var_scope; sc; sc = sc->next) {
if (strlen(sc->name) == tok->len
&& !strncmp(tok->str, sc->name, tok->len)) {
return sc;
}
}
return NULL;
}
static TagScope *find_tag(Token *tok) {
for (TagScope *sc = tag_scope; sc; sc = sc->next) {
if (strlen(sc->name) == tok->len
&& !strncmp(tok->str, sc->name, tok->len)) {
return sc;
}
}
return NULL;
}
static Node *new_node(NodeKind kind, Token *tok) {
Node *node = calloc(1, sizeof(Node));
node->kind = kind;
node->tok = tok;
return node;
}
static Node *new_binary(NodeKind kind,
Node *lhs,
Node *rhs,
Token *tok) {
Node *node = new_node(kind, tok);
node->lhs = lhs;
node->rhs = rhs;
return node;
}
static Node *new_unary(NodeKind kind,
Node *expr,
Token *tok) {
Node *node = new_node(kind, tok);
node->lhs = expr;
return node;
}
static Node *new_num(int val, Token *tok) {
Node *node = new_node(ND_NUM, tok);
node->val = val;
return node;
}
static Node *new_var_node(Var *var, Token *tok) {
Node *node = new_node(ND_VAR, tok);
node->var = var;
return node;
}
static VarScope *push_scope(char *name) {
VarScope *sc = calloc(1, sizeof(VarScope));
sc->name = name;
sc->next = var_scope;
var_scope = sc;
return sc;
}
static Var *new_var(char *name, Type *ty, bool is_local) {
Var *var = calloc(1, sizeof(Var));
var->name = name;
var->ty = ty;
var->is_local = is_local;
return var;
}
static Var *new_lvar(char *name, Type *ty) {
Var *var = new_var(name, ty, true);
push_scope(name)->var = var;
VarList *vl = calloc(1, sizeof(VarList));
vl->var = var;
vl->next = locals;
locals = vl;
return var;
}
static Var *new_gvar(char *name, Type *ty, bool emit) {
Var *var = new_var(name, ty, false);
push_scope(name)->var = var;
// 変数かどうかをemitで判断?
// 関数のときには,emit = false
if (emit) {
VarList *vl = calloc(1, sizeof(VarList));
vl->var = var;
vl->next = globals;
globals = vl;
}
return var;
}
static Type *find_typedef(Token *tok) {
if (tok->kind == TK_IDENT) {
VarScope *sc = find_var(tok);
if (sc) {
return sc->type_def;
}
}
return NULL;
}
static char *new_label() {
static int cnt = 0;
char buf[20];
sprintf(buf, ".L.data.%d", cnt++);
return duplicate(buf, 20);
}
static Function *function();
static Type *basetype(bool *is_typedef);
static Type *declarator(Type *ty, char **name);
static Type *type_suffix(Type *ty);
static Type *struct_decl();
static Member *struct_member();
static void global_var();
static Node *declaration();
static bool is_typename();
static Node *stmt();
static Node *stmt2();
static Node *expr();
static Node *assign();
static Node *equality();
static Node *relational();
static Node *add();
static Node *mul();
static Node *unary();
static Node *postfix();
static Node *primary();
// 次のトップレベルに置かれているものが関数か
// グローバル変数化をトークンを先読みして判断する.
static bool is_function() {
Token *tok = token;
bool is_typedef;
Type *ty = basetype(&is_typedef);
char *name = NULL;
declarator(ty, &name);
// 識別子+( と続いていたら,関数
bool isfunc = name && consume("(");
token = tok;
return isfunc;
}
// program = (global-var | function)*
Program *program() {
Function head = {};
Function *cur = &head;
globals = NULL;
while (!at_eof()) {
if (is_function()) {
Function *fn = function();
if (!fn) {
continue;
}
cur->next = fn;
cur = cur->next;
continue;
}
global_var();
}
Program *prog = calloc(1, sizeof(Program));
prog->globals = globals;
prog->fns = head.next;
return prog;
}
// basetype = builtin-type | struct-decl | typedef-name
// builtin-type = "void" | "_Bool" | "char"
// | "short" | "int" | "long" | "long" "long"
static Type *basetype(bool *is_typedef) {
if (!is_typename()) {
error_tok(token, "typename expected");
}
enum {
VOID = 1 << 0,
BOOL = 1 << 2,
CHAR = 1 << 4,
SHORT = 1 << 6,
INT = 1 << 8,
LONG = 1 << 10,
OTHER = 1 << 12,
};
Type *ty = int_type;
int counter = 0;
if (is_typedef) {
*is_typedef = false;
}
while (is_typename()) {
Token *tok = token;
if (consume("typedef")) {
if (!is_typedef) {
error_tok(tok, "invalid storage class specifier");
}
*is_typedef = true;
continue;
}
// user defined type
if (!peek("void") && !peek("_Bool") && !peek("char")
&& !peek("short") && !peek("int")
&& !peek("long")) {
if (counter) {
break;
}
if (peek("struct")) {
ty = struct_decl();
} else {
ty = find_typedef(token);
assert(ty);
token = token->next;
}
counter |= OTHER;
continue;
}
// handle built-in types;
if (consume("void")) {
counter += VOID;
} else if (consume("_Bool")) {
counter += BOOL;
} else if (consume("char")) {
counter += CHAR;
} else if (consume("short")) {
counter += SHORT;
} else if (consume("int")) {
counter += INT;
} else if (consume("long")) {
counter += LONG;
}
switch (counter) {
case VOID:
ty = void_type;
break;
case BOOL:
ty = bool_type;
break;
case CHAR:
ty = char_type;
break;
case SHORT:
case SHORT + INT:
ty = short_type;
break;
case INT:
ty = int_type;
break;
case LONG:
case LONG + INT:
case LONG + LONG:
case LONG + LONG + INT:
ty = long_type;
break;
default:
error_tok(tok, "invalid type");
}
}
return ty;
}
// declarator = "*"* ("(" declarator ")" | ident)
// type-suffix
static Type *declarator(Type *ty, char **name) {
while (consume("*")) {
ty = pointer_to(ty);
}
if (consume("(")) {
Type *placeholder = calloc(1, sizeof(Type));
Type *new_ty = declarator(placeholder, name);
expect(")");
memcpy(placeholder, type_suffix(ty), sizeof(Type));
return new_ty;
}
*name = expect_ident();
return type_suffix(ty);
}
// type-suffix = ("[" num "]" type-suffix)?
static Type *type_suffix(Type *ty) {
if (!consume("[")) {
return ty;
}
int array_size = expect_number();
expect("]");
ty = type_suffix(ty);
return array_of(ty, array_size);
}
static void push_tag_scope(Token *tok, Type *ty) {
TagScope *sc = calloc(1, sizeof(TagScope));
sc->next = tag_scope;
sc->name = duplicate(tok->str, tok->len);
sc->ty = ty;
tag_scope = sc;
}
// struct-decl = "struct" idnet
// | "struct" ident? "{" struct-member "}"
static Type *struct_decl() {
// Read struct tag
expect("struct");
Token *tag = consume_ident();
if (tag && !peek("{")) {
TagScope *sc = find_tag(tag);
if (!sc) {
error_tok(tag, "unknown struct type");
}
return sc->ty;
}
expect("{");
// Read struct members.
Member head = {};
Member *cur = &head;
while (!consume("}")) {
cur->next = struct_member();
cur = cur->next;
}
Type *ty = calloc(1, sizeof(Type));
ty->kind = TY_STRUCT;
ty->members = head.next;
// structのメンバのoffsetを設定する.
int offset = 0;
for (Member *mem = ty->members; mem; mem = mem->next) {
offset = align_to(offset, mem->ty->align);
mem->offset = offset;
offset += mem->ty->size;
if (ty->align < mem->ty->align) {
ty->align = mem->ty->align;
}
}
ty->size = align_to(offset, ty->align);
// nameが与えられていた場合は,struct typeを登録する.
if (tag) {
push_tag_scope(tag, ty);
}
return ty;
}
// struct-member = basetype ident ("[" num "]")* ";"
static Member *struct_member() {
Type *ty = basetype(NULL);
char *name = NULL;
ty = declarator(ty, &name);
ty = type_suffix(ty);
expect(";");
Member *mem = calloc(1, sizeof(Member));
mem->name = name;
mem->ty = ty;
return mem;
}
static VarList *read_func_param() {
Type *ty = basetype(NULL);
char *name = NULL;
ty = declarator(ty, &name);
ty = type_suffix(ty);
VarList *vl = calloc(1, sizeof(VarList));
vl->var = new_lvar(name, ty);
return vl;
}
static VarList *read_func_params() {
if (consume(")"))
return NULL;
VarList *head = read_func_param();
VarList *cur = head;
while (!consume(")")) {
expect(",");
cur->next = read_func_param();
cur = cur->next;
}
return head;
}
// function = basetype declarator "(" params? ")"
// ("{" stmt* "}" | ";")
// params = param ("," param)*
// param = basetype declarator type-suffix
Function *function() {
locals = NULL;
Type *ty = basetype(NULL);
char *name = NULL;
ty = declarator(ty, &name);
// add a function type to the scope
new_gvar(name, func_type(ty), false);
// construct a function object
Function *fn = calloc(1, sizeof(Function));
fn->name = name;
expect("(");
// 現状のscopeを一時保存
Scope *sc = enter_scope();
fn->params = read_func_params();
if (consume(";")) {
leave_scope(sc);
return NULL;
}
// function の中身を読み取る
Node head = {};
Node *cur = &head;
expect("{");
while (!consume("}")) {
cur->next = stmt();
cur = cur->next;
}
// 一時保存していたscopeを戻して,
// 一時保存からここまでで作られた変数の情報を開放
leave_scope(sc);
fn->node = head.next;
fn->locals = locals;
return fn;
}
// global_var = basetype declarator type-suffix ";"
static void global_var() {
bool is_typedef;
Type *ty = basetype(&is_typedef);
char *name = NULL;
ty = declarator(ty, &name);
ty = type_suffix(ty);
expect(";");
if (is_typedef) {
push_scope(name)->type_def = ty;
} else {
new_gvar(name, ty, true);
}
}
// declaration = basetype declarator type-suffix
// ("=" expr)? ";"
// | basetype ";"
static Node *declaration() {
Token *tok = token;
bool is_typedef;
Type *ty = basetype(&is_typedef);
if (consume(";")) {
return new_node(ND_NULL, tok);
}
char *name = NULL;
ty = declarator(ty, &name);
ty = type_suffix(ty);
if (is_typedef) {
expect(";");
push_scope(name)->type_def = ty;
return new_node(ND_NULL, tok);
}
if (ty->kind == TY_VOID) {
error_tok(tok, "variable dclared void");
}
Var *var = new_lvar(name, ty);
if (consume(";")) {
return new_node(ND_NULL, tok);
}
expect("=");
Node *lhs = new_var_node(var, tok);
Node *rhs = expr();
expect(";");
Node *node = new_binary(ND_ASSIGN, lhs, rhs, tok);
return new_unary(ND_EXPR_STMT, node, tok);
}
static Node *read_expr_stmt(void) {
Token *tok = token;
return new_unary(ND_EXPR_STMT, expr(), tok);
}
// 次のトークンが型を表していればtrue
static bool is_typename() {
return peek("void") || peek("_Bool") || peek("char")
|| peek("short") || peek("int") || peek("long")
|| peek("struct") || peek("typedef")
|| find_typedef(token);
}
static Node *stmt() {
Node *node = stmt2();
add_type(node);
return node;
}
// stmt2 = "return" expr ";"
// | "if" "(" expr ")" stmt ("else" stmt)?
// | "while" "(" expr ")" stmt
// | "for" "(" expr? ";" expr? ";" expr? ")" stmt
// | "{" stmt* "}"
// | declaration
// | expr ";"
Node *stmt2() {
Token *tok;
if ((tok = consume("return"))) {
Node *node = new_unary(ND_RETURN, expr(), tok);
expect(";");
return node;
}
if ((tok = consume("if"))) {
Node *node = new_node(ND_IF, tok);
expect("(");
node->cond = expr();
expect(")");
node->then = stmt();
if (consume("else")) {
node->els = stmt();
}
return node;
}
if ((tok = consume("while"))) {
Node *node = new_node(ND_WHILE, tok);
expect("(");
node->cond = expr();
expect(")");
node->then = stmt();
return node;
}
if ((tok = consume("for"))) {
Node *node = new_node(ND_FOR, tok);
expect("(");
if (!consume(";")) {
node->init = read_expr_stmt();
expect(";");
}
if (!consume(";")) {
node->cond = expr();
expect(";");
}
if (!consume(")")) {
node->inc = read_expr_stmt();
expect(")");
}
node->then = stmt();
return node;
}
if ((tok = consume("{"))) {
Node head = {};
Node *cur = &head;
// ブロックには入る前までの変数の宣言状態を一時保存
Scope *sc = enter_scope();
while (!consume("}")) {
cur->next = stmt();
cur = cur->next;
}
// ブロックから出たので,一時保存していた変数の宣言状態を復旧
leave_scope(sc);
Node *node = new_node(ND_BLOCK, tok);
node->body = head.next;
return node;
}
if (is_typename()) {
return declaration();
}
Node *node = read_expr_stmt();
expect(";");
return node;
}
// expr = equality
Node *expr() {
return assign();
}
Node *assign() {
Node *node = equality();
Token *tok;
if ((tok = consume("=")))
node = new_binary(ND_ASSIGN, node, assign(), tok);
return node;
}
// equality = relational ("==" relational | "!="
// relational)*
Node *equality() {
Node *node = relational();
Token *tok;
for (;;) {
if ((tok = consume("==")))
node = new_binary(ND_EQ, node, relational(), tok);
else if ((tok = consume("!=")))
node = new_binary(ND_NE, node, relational(), tok);
else
return node;
}
}
// relational = add("<" add | "<=" add | "<=" add)*
Node *relational() {
Node *node = add();
Token *tok;
for (;;) {
if ((tok = consume("<")))
node = new_binary(ND_LT, node, add(), tok);
else if ((tok = consume("<=")))
node = new_binary(ND_LE, node, add(), tok);
else if ((tok = consume(">")))
node = new_binary(ND_LT, add(), node, tok);
else if ((tok = consume(">=")))
node = new_binary(ND_LE, add(), node, tok);
else
return node;
}
}
static Node *new_add(Node *lhs, Node *rhs, Token *tok) {
add_type(lhs);
add_type(rhs);
if (is_integer(lhs->ty) && is_integer(rhs->ty)) {
return new_binary(ND_ADD, lhs, rhs, tok);
}
if (lhs->ty->base && is_integer(rhs->ty)) {
return new_binary(ND_PTR_ADD, lhs, rhs, tok);
}
if (is_integer(lhs->ty) && rhs->ty->base) {
return new_binary(ND_PTR_ADD, rhs, lhs, tok);
}
error_tok(tok, "invalid operands");
return NULL;
}
static Node *new_sub(Node *lhs, Node *rhs, Token *tok) {
add_type(lhs);
add_type(rhs);
if (is_integer(lhs->ty) && is_integer(rhs->ty)) {
return new_binary(ND_SUB, lhs, rhs, tok);
}
if (lhs->ty->base && is_integer(rhs->ty)) {
return new_binary(ND_PTR_SUB, lhs, rhs, tok);
}
if (lhs->ty->base && rhs->ty->base) {
return new_binary(ND_PTR_DIFF, lhs, rhs, tok);
}
error_tok(tok, "invalid operands");
return NULL;
}
// add = mul ("+" mul | "-" mul)*
Node *add() {
Node *node = mul();
Token *tok;
for (;;) {
if ((tok = consume("+")))
node = new_add(node, mul(), tok);
else if ((tok = consume("-")))
node = new_sub(node, mul(), tok);
else
return node;
}
}
Node *mul() {
Node *node = unary();
Token *tok;
for (;;) {
if ((tok = consume("*"))) {
node = new_binary(ND_MUL, node, unary(), tok);
} else if ((tok = consume("/"))) {
node = new_binary(ND_DIV, node, unary(), tok);
} else {
return node;
}
}
}
// unary = ("+" | "-" | "*" | "&")? unary
// | postfix
Node *unary() {
Token *tok;
if (consume("+"))
return unary();
if ((tok = consume("-")))
return new_binary(
ND_SUB, new_num(0, tok), unary(), tok);
if ((tok = consume("&")))
return new_unary(ND_ADDR, unary(), tok);
if ((tok = consume("*")))
return new_unary(ND_DEREF, unary(), tok);
return postfix();
}
static Member *find_member(Type *ty, char *name) {
for (Member *mem = ty->members; mem; mem = mem->next) {
if (!strcmp(mem->name, name)) {
return mem;
}
}
return NULL;
}
static Node *struct_ref(Node *lhs) {
add_type(lhs);
if (lhs->ty->kind != TY_STRUCT) {
error_tok(lhs->tok, "not a struct");
}
Token *tok = token;
Member *mem = find_member(lhs->ty, expect_ident());
if (!mem) {
error_tok(tok, "no such member");
}
Node *node = new_unary(ND_MEMBER, lhs, tok);
node->member = mem;
return node;
}
// postfix = primary ("[" expr "]" | "." ident
// |"->" ident)*
static Node *postfix() {
Node *node = primary();
Token *tok;
for (;;) {
if ((tok = consume("["))) {
// x[y] は *(x+y)の省略
Node *exp = new_add(node, expr(), tok);
expect("]");
node = new_unary(ND_DEREF, exp, tok);
continue;
}
if ((tok = consume("."))) {
node = struct_ref(node);
continue;
}
if ((tok = consume("->"))) {
// x->y is short for (*x).y
node = new_unary(ND_DEREF, node, tok);
node = struct_ref(node);
continue;
}
return node;
}
}
// stmt-expr = "(" "{" stmt stmt* "}" ")"
//
// Statement expression は, GNUCの拡張です.
static Node *stmt_expr(Token *tok) {
// stmt_exprに入る前までの変数の状態を一時保存
Scope *sc = enter_scope();
Node *node = new_node(ND_STMT_EXPR, tok);
node->body = stmt();
Node *cur = node->body;
while (!consume("}")) {
cur->next = stmt();
cur = cur->next;
}
expect(")");
// スコープから外れる際に,一時保存していた変数の状態を復旧
leave_scope(sc);
if (cur->kind != ND_EXPR_STMT) {
error_tok(
cur->tok,
"stmt expr はvoidを返すことをサポートしてません.");
}
memcpy(cur, cur->lhs, sizeof(Node));
return node;
}
char *duplicate(char *str, size_t len) {
char *buffer = calloc(len + 1, sizeof(char));
memcpy(buffer, str, len);
buffer[len] = '\0';
return buffer;
}
// func-args = "(" (assign ("," assign)*)? ")"
static Node *func_args(void) {
if (consume(")"))
return NULL;
Node *head = assign();
Node *cur = head;
while (consume(",")) {
cur->next = assign();
cur = cur->next;
}
expect(")");
return head;
}
// primary = "(" "{" stmt-expr-tail
// | "(" expr ")"
// | "sizeof" unary
// | ident func-args?
// | str
// | num
Node *primary() {
Token *tok;
// 次のトークンが"("なら,"(" expr ")"のはず
if ((tok = consume("("))) {
if (consume("{")) {
return stmt_expr(tok);
}
Node *node = expr();
expect(")");
return node;
}
if ((tok = consume("sizeof"))) {
Node *node = unary();
add_type(node);
return new_num(node->ty->size, tok);
}
if ((tok = consume_ident())) {
// Function call
if (consume("(")) {
Node *node = new_node(ND_FUNC_CALL, tok);
node->func_name = duplicate(tok->str, tok->len);
node->args = func_args();
add_type(node);
VarScope *sc = find_var(tok);
if (sc) {
if (!sc->var || sc->var->ty->kind != TY_FUNC) {
error_tok(tok, "not a function");
}
node->ty = sc->var->ty->return_ty;
} else {
warn_tok(node->tok,
"implicit declaration of function");
node->ty = int_type;
}
return node;
}
// 変数
VarScope *sc = find_var(tok);
if (sc && sc->var) {
return new_var_node(sc->var, tok);
}
error_tok(tok, "undefined variable");
}
tok = token;
if (tok->kind == TK_STR) {
token = token->next;
Type *ty = array_of(char_type, tok->cont_len);
Var *var = new_gvar(new_label(), ty, true);
var->contents = tok->contents;
var->cont_len = tok->cont_len;
return new_var_node(var, tok);
}
if (tok->kind != TK_NUM)
error_tok(tok, "expected expression");
return new_num(expect_number(), tok);
}