-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.c
1125 lines (989 loc) · 34.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
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "9cc.h"
struct LVar *locals;
struct GVar *globals;
struct Str *strings;
struct Struct *struct_defs;
struct Struct *struct_decls;
struct Function *functions;
struct Function *prototypes;
struct Enum *enums;
static size_t strnlen(const char *s, size_t n) {
const char *p = memchr(s, 0, n);
return p ? p-s : n;
}
static char *strndup(const char *s, size_t n) {
size_t l = strnlen(s, n);
char *d = malloc(l+1);
if (!d) return NULL;
memcpy(d, s, l);
d[l] = 0;
return d;
}
// static struct Struct *reverse_struct(struct Struct* head) {
// struct Struct *prev = NULL;
// struct Struct *current = head;
// struct Struct *next = NULL;
// while (current != NULL) {
// next = current->next; // 次のノードを保存
// current->next = prev; // currentの次をprevに変更
// prev = current; // prevをcurrentに移動
// current = next; // currentを次のノードに移動
// }
// head = prev;
// return head;
// }
// 構造体のリストから指定したタグ名に合う構造体を見つける
static struct Type *find_struct(char *ident) {
for (struct Struct *_struct = struct_defs; _struct; _struct = _struct->next) {
if (!strncmp(ident, _struct->decl->tag_name, _struct->decl->tag_len))
return _struct->decl;
}
for (struct Struct *_struct = struct_decls; _struct; _struct = _struct->next) {
if (!strncmp(ident, _struct->decl->tag_name, _struct->decl->tag_len))
return _struct->decl;
}
error_at(token->str, "構造体宣言が見つかりません");
return NULL;
}
// 構造体のメンバ群から指定したメンバを探す
static struct MStruct *find_member(struct Type *struct_ty, char *ident) {
if (struct_ty->kind != TY_STRUCT)
error_at(token->str, "演算子を適用できません");
for (struct MStruct *mem = find_struct(struct_ty->tag_name)->member; mem; mem = mem->next) { // 最新の型情報に対して検索
if (!strncmp(ident, mem->name, mem->len))
return mem;
}
error_at(token->str, "メンバが見つかりません");
return NULL;
}
// enumによって定義された識別子について値を返す
static struct Enum *find_enum(struct Token *tok) {
for (struct Enum *_enum = enums; _enum; _enum = _enum->next)
if (!memcmp(tok->str, _enum->name, tok->len))
return _enum;
return NULL;
}
// ローカル変数を名前で検索する。見つからなかった場合はNULLを返す。
static struct LVar *find_lvar(struct Token *tok) {
for (struct LVar *var = locals; var; var = var->next)
if (var->len == tok->len && !memcmp(tok->str, var->name, var->len))
return var;
return NULL;
}
// グローバル変数を名前で検索する。見つからなかった場合はNULLを返す。
static struct GVar *find_gvar(struct Token *tok) {
for (struct GVar *var = globals; var; var = var->next)
if (var->len == tok->len && !memcmp(tok->str, var->name, var->len))
return var;
return NULL;
}
static struct Node *new_node(long /*NodeKind*/ kind, struct Node *lhs, struct Node *rhs) {
struct Node *node = calloc(1, sizeof(struct Node));
node->kind = kind;
node->lhs = lhs;
node->rhs = rhs;
node->tok = token;
return node;
}
static struct Node *new_node_num(long val) {
struct Node *node = calloc(1, sizeof(struct Node));
node->kind = ND_NUM;
node->val = val;
node->tok = token;
return node;
}
static struct Node *new_node_lvar(struct LVar *lvar, struct Token *tok) {
struct Node *node = calloc(1, sizeof(struct Node));
node->kind = ND_LVAR;
node->lvar = lvar;
node->tok = tok;
return node;
}
static struct Node *new_node_gvar(struct GVar *gvar, struct Token *tok) {
struct Node *node = calloc(1, sizeof(struct Node));
node->kind = ND_GVAR;
node->gvar = gvar;
node->tok = tok;
return node;
}
static struct Node *new_node_str(struct Str *str, struct Token *tok) {
struct Node *node = calloc(1, sizeof(struct Node));
node->kind = ND_STR;
node->string = str;
node->tok = tok;
return node;
}
static struct Type *arr_to_ptr(struct Type *ty) {
struct Type *new_type = calloc(1, sizeof(struct Type));
memcpy(new_type, ty, sizeof(struct Type));
new_type->kind = TY_PTR;
new_type->size = 8;
new_type->base = new_type->elem;
new_type->decayed = 1; // 降格
return new_type;
}
static void decay_arr(struct Node *node) {
// nodeに対してまだ型が与えられていない or nodeの型が配列である配列型をポインタに降格させる
// 配列のローカル変数に対する降格
if ((!node->ty || node->ty->kind == TY_ARRAY) && node->kind == ND_LVAR && node->lvar->ty->kind == TY_ARRAY) // このオペランドの型が配列だったらポインタにする
node->ty = arr_to_ptr(node->lvar->ty); // ポインタに変換して付け替え
// 配列のグローバル変数に対する降格
if ((!node->ty || node->ty->kind == TY_ARRAY) && node->kind == ND_GVAR && node->gvar->ty->kind == TY_ARRAY)
node->ty = arr_to_ptr(node->gvar->ty);
// 文字列リテラルに対する降格
if ((!node->ty || node->ty->kind == TY_ARRAY) && node->kind == ND_STR && node->string->ty->kind == TY_ARRAY)
node->ty = arr_to_ptr(node->string->ty);
}
// static void integer_promotion(struct Node *lhs, struct Node *rhs) {
// // オペランドの型をlongに揃える
// if(is_integer(lhs->ty))
// lhs->ty->kind = TY_INT;
// if(is_integer(rhs->ty))
// rhs->ty->kind = TY_INT;
// }
static struct LVar *new_lvar(char *name, struct Type *ty) {
struct LVar *lvar = calloc(1, sizeof(struct LVar));
lvar->name = name;
lvar->ty = ty;
lvar->len = strlen(name);
lvar->next = locals;
locals = lvar;
return lvar;
}
static struct GVar *new_gvar(char *name, struct Type *ty) {
struct GVar *gvar = calloc(1, sizeof(struct GVar));
gvar->name = name;
gvar->ty = ty;
gvar->len = strlen(name);
gvar->next = globals;
globals = gvar;
return gvar;
}
static struct MStruct *new_mstruct(char *name, struct Type *ty) {
struct MStruct *smem = calloc(1, sizeof(struct MStruct));
smem->name = name;
smem->ty = ty;
smem->len = strlen(name);
return smem;
}
static struct Struct *new_struct(char *name) {
struct Struct *_struct = calloc(1, sizeof(struct Struct));
_struct->decl = calloc(1, sizeof(struct Type));
_struct->decl->kind = TY_STRUCT;
_struct->decl->tag_name = name;
_struct->decl->tag_len = strlen(name);
return _struct;
}
static struct Enum *new_enum(char *name) {
struct Enum *_enum = calloc(1, sizeof(struct Enum));
_enum->name = name;
return _enum;
}
static struct Str *new_str(struct Token *tok) {
struct Type *ty = array_of(new_type(TY_CHAR), tok->len-1);
ty->tok = tok;
struct Str *str = calloc(1, sizeof(struct Str));
str->literal = strndup(tok->str, tok->len);
str->ty = ty;
str->next = strings;
strings = str;
return str;
}
static char *get_ident() {
if (token->kind != TK_IDENT)
error_at(token->str, "識別子が必要");
struct Token *tok = token;
token = token->next;
return strndup(tok->str, tok->len);
}
// decl_basictype = ("int" | "char" | "long" | "void" | "struct" ident)
// 変数宣言のことも考慮して別の関数に振り分けた
static struct Type *decl_basictype() {
if (consume_keyword("struct"))
return find_struct(get_ident());
if (consume_keyword("int"))
return new_type(TY_INT);
if (consume_keyword("char"))
return new_type(TY_CHAR);
if (consume_keyword("long"))
return new_type(TY_INT);
if (!consume_keyword("void"))
error_at(token->str, "型名が必要");
return new_type(TY_VOID);
}
// equal_basictype = ("int" | "char" | "long" | "void" | "struct")
static long equal_basictype() {
return equal_keyword("int") || equal_keyword("char") || equal_keyword("long") || equal_keyword("void") || equal_keyword("struct");
}
static struct Type *declarator(struct Type *ty);
struct Program *parse();
static struct Function *function_definition();
static struct Function *prototype_declaration();
static struct Node *stmt();
static struct Node *expr();
static struct Node *assign();
static struct Node *logor();
static struct Node *logand();
static struct Node *equality();
static struct Node *relational();
static struct Node *new_add(struct Node *, struct Node *);
static struct Node *new_add_asgn(struct Node *, struct Node *);
static struct Node *new_sub_asgn(struct Node *, struct Node *);
static struct Node *add();
static struct Node *mul();
static struct Node *unary();
static struct Node *postfix();
static struct Node *primary();
// type-suffix = ("(" func-params? ")")
// func-params = param ("," param)*
// param = declarator
static struct Type *type_suffix(struct Type *ty) {
expect("(");
struct Type head = {};
struct Type *cur = &head;
while (!consume(")")) {
struct Type *basety = decl_basictype();
struct Type *ty = declarator(basety);
cur = cur->next = ty;
consume(",");
}
ty = func_type(ty);
ty->params = head.next;
return ty;
}
// declarator = "*" * ident ( "[" number "]" )?
static struct Type *declarator(struct Type *ty) { // 宣言子
while (consume("*"))
ty = pointer_to(ty);
struct Token *tok_lval = token;
char *name = get_ident();
if (consume("(")) // 宣言子の後ろに括弧が来たら関数定義、宣言として読み込み失敗
return NULL;
if (consume("[")) {
ty = array_of(ty, expect_number());
expect("]");
}
ty->name = name;
ty->tok = tok_lval;
return ty;
}
// declarator_except_ident = "*" * ( "[" number "]" )?
static struct Type *declarator_except_ident(struct Type *ty) { // 識別子を覗いた宣言子
while (consume("*"))
ty = pointer_to(ty);
struct Token *tok_lval = token;
if (consume("(")) // 宣言子の後ろに括弧が来たら関数定義、宣言として読み込み失敗
return NULL;
if (consume("[")) {
ty = array_of(ty, expect_number());
expect("]");
}
ty->tok = tok_lval;
return ty;
}
// function_declarator = "*" * ident type-suffix
static struct Type *function_declarator(struct Type *ty) { // 関数宣言子
while (consume("*"))
ty = pointer_to(ty);
struct Token *tok_lval = token;
char *name = get_ident();
ty = type_suffix(ty); // 関数の宣言時の引数読み込み等
ty->name = name;
ty->tok = tok_lval;
return ty;
}
// declaration = decl_basictype (declarator ("=" (assign | ("{" assign ("," assign)* "}")))? ("," declarator ("=" (assign | ("{" assign ("," assign)* "}")))?)*)? ";"
static struct Node *declaration() {
struct Type *basety = decl_basictype();
struct Node head = {};
struct Node *cur = &head;
while (!consume(";")) {
struct Type *ty = declarator(basety);
if (!ty) // 宣言として読み込み失敗
return NULL;
struct LVar *lvar = new_lvar(ty->name, ty);
if (consume("=")) {
if (lvar->ty->kind == TY_ARRAY) { // 配列に対する初期化
if (consume("{")) { // 初期化子リスト
long param_cnt = 0;
struct Node *lhs = new_node_lvar(lvar, ty->tok);
while (!consume("}")) {
struct Node *rhs = assign();
decay_arr(rhs); // 右辺が配列ならポインタに降格
if (param_cnt<lvar->ty->array_size) // 配列の要素数を超えた
cur = cur->next = new_node(ND_ASSIGN, new_node(ND_DEREF, new_add(lhs, new_node_num(param_cnt)), NULL), rhs);
++param_cnt;
consume(",");
}
while (param_cnt<lvar->ty->array_size) { // 残りの要素は0で初期化
cur = cur->next = new_node(ND_ASSIGN, new_node(ND_DEREF, new_add(lhs, new_node_num(param_cnt)), NULL), new_node_num(0));
++param_cnt;
}
} else { // 文字列リテラル
struct Token *tok = consume_str();
if (tok) {
long param_cnt = 0;
struct Node *lhs = new_node_lvar(lvar, ty->tok);
for(char *ptr = tok->str+1;param_cnt<tok->len-2 && param_cnt<lvar->ty->array_size;++ptr) {
cur = cur->next = new_node(ND_ASSIGN, new_node(ND_DEREF, new_add(lhs, new_node_num(param_cnt)), NULL), new_node_num(*ptr));
++param_cnt;
}
while (param_cnt<lvar->ty->array_size) { // 残りの要素は0で初期化
cur = cur->next = new_node(ND_ASSIGN, new_node(ND_DEREF, new_add(lhs, new_node_num(param_cnt)), NULL), new_node_num(0));
++param_cnt;
}
} else {
error_at(token->str, "不明な初期化式");
}
}
} else { // 配列以外
struct Node *lhs = new_node_lvar(lvar, ty->tok);
struct Node *rhs = assign();
decay_arr(rhs); // 右辺が配列ならポインタに降格
cur = cur->next = new_node(ND_ASSIGN, lhs, rhs);
}
}
consume(",");
}
struct Node *node = new_node(ND_BLOCK, NULL, NULL);
node->body = head.next;
return node;
}
// gvar_declaration = decl_basictype (declarator ("=" (num | string_literal | ("{" num ("," num)* "}")))? ("," declarator ("=" (num | string_literal | ("{" num ("," num)* "}")))?)*)? ";"
static long gvar_declaration() {
struct Type *basety = decl_basictype();
while (!consume(";")) {
struct Type *ty = declarator(basety);
if (!ty) // 宣言として読み込み失敗
return false;
struct GVar *gvar = new_gvar(ty->name, ty);
if (consume("=")) {
if (gvar->ty->kind == TY_ARRAY) { // 配列に対する初期化
if (consume("{")) { // 初期化子リスト
struct Node head = {};
struct Node *cur = &head;
long param_cnt = 0;
while (!consume("}")) {
if (param_cnt<gvar->ty->array_size) // 配列の要素数を超えた
cur = cur->next = new_node_num(expect_number());
++param_cnt;
consume(",");
}
while (param_cnt<gvar->ty->array_size) { // 残りの要素は0で初期化
cur = cur->next = new_node_num(0);
++param_cnt;
}
struct Node *node = new_node(ND_BLOCK, NULL, NULL);
node->body = head.next;
add_type(node);
gvar->init = node;
} else { // 文字列リテラル
struct Token *tok = consume_str();
if (tok) {
struct Node head = {};
struct Node *cur = &head;
struct Str *string = new_str(tok);
cur = cur->next = new_node_str(string, tok);
long param_cnt = string->ty->array_size;
while (param_cnt<gvar->ty->array_size) { // 残りの要素は0で初期化
cur = cur->next = new_node_num(0);
++param_cnt;
}
struct Node *node = new_node(ND_BLOCK, NULL, NULL);
node->body = head.next;
add_type(node);
gvar->init = node;
} else {
error_at(token->str, "不明な初期化式");
}
}
} else { // 配列以外
struct Token *tok = consume_str();
if (tok) {
struct Str *string = new_str(tok);
struct Node *node = new_node_str(string, tok);
decay_arr(node); // 右辺が配列ならポインタに降格
add_type(node);
gvar->init = node;
} else {
gvar->init = new_node_num(expect_number());
}
}
}
consume(",");
}
return true;
}
// 構造体にオフセット割り当て
static void assign_struct_offsets(struct Struct *_struct) {
long offset = 0;
long struct_align = 0;
for(struct MStruct *member = _struct->decl->member; member; member = member->next) {
if (!member->ty->align)
error_at(member->ty->tok->str, "alignment is not defigned");
member->offset = offset = align_to(offset, member->ty->align);
if (struct_align < member->ty->align)
struct_align = member->ty->align;
offset += member->ty->size;
}
offset = align_to(offset, struct_align);
_struct->decl->align = struct_align;
_struct->decl->size = offset;
}
// struct_declaration = "struct" ident "{" (decl_basictype declarator ";")* "}" ";"
static long struct_declaration() {
if (!consume_keyword("struct")) {
return false;
}
char *name = get_ident();
struct Struct *_struct = new_struct(name);
if(consume("{")) {
struct MStruct head = {};
struct MStruct *cur = &head;
while (!consume("}")) {
struct Type *basety = decl_basictype();
struct Type *ty = declarator(basety);
expect(";");
cur = cur->next = new_mstruct(ty->name, ty);
}
_struct->decl->member = head.next;
_struct->next = struct_defs;
struct_defs = _struct;
assign_struct_offsets(_struct);
expect(";");
return true;
} else {
if(!consume(";"))
return false;
_struct->next = struct_decls;
struct_decls = _struct;
return true;
}
}
// enum_declaration = "enum" "{" (ident ",")* "}" ";"
static long enum_declaration() {
if (!consume_keyword("enum")) {
return false;
}
if(!consume("{")) {
return false;
}
long enum_cnt = 0;
while (!consume("}")) {
char *name = get_ident();
struct Enum *_enum = new_enum(name);
expect(",");
_enum->node = new_node_num(enum_cnt++);
_enum->next = enums;
enums = _enum;
}
expect(";");
return true;
}
static void create_param_lvars(struct Type *param) {
if (param) {
create_param_lvars(param->next);
new_lvar(param->name, param);
}
}
// program = (function_definition | prototype_declaration | gvar_declaration | struct_declaration | enum_declaration)*
struct Program *parse() {
struct Program *prog = calloc(1, sizeof(struct Program));
struct Function func_head = {};
struct Function *func_cur = &func_head;
while (!at_eof()) {
struct Token *origin = token; // 解析失敗時はトークンを元に戻す
if (struct_declaration()) { // 構造体宣言
continue;
}
token = origin;
if (enum_declaration()) { // 列挙体宣言
continue;
}
token = origin;
if (gvar_declaration()) { // グローバル変数宣言として読むことができるか
continue;
}
token = origin;
struct Function *func = function_definition();
if (func) {
func_cur = func_cur->next = func;
functions = func_head.next;
continue;
}
token = origin;
func = prototype_declaration();
func->next = prototypes;
prototypes = func;
}
//error("正しくパースできませんでした。");
prog->funcs = func_head.next;
return prog;
}
// function_definition = decl_basictype function_declarator stmt
static struct Function *function_definition() {
struct Type *ty = decl_basictype();
ty = function_declarator(ty);
locals = NULL; // NULLのときが終端とするため
struct Function *fn = calloc(1, sizeof(struct Function));
fn->name = ty->name;
create_param_lvars(ty->params);
fn->params = locals;
if (!at_block())
return NULL;
fn->body = stmt();
fn->locals = locals;
fn->ret_ty = ty->return_ty;
return fn;
}
// prototype_declaration = decl_basictype function_declarator ";"
static struct Function *prototype_declaration() {
struct Type *ty = decl_basictype();
ty = function_declarator(ty);
locals = NULL; // NULLのときが終端とするため
struct Function *fn = calloc(1, sizeof(struct Function));
fn->name = ty->name;
create_param_lvars(ty->params);
fn->params = locals;
expect(";");
fn->locals = locals;
fn->ret_ty = ty->return_ty;
return fn;
}
// stmt = expr? ";"
// | "{" (declaration | stmt)* "}"
// | "if" "(" expr ")" stmt ("else" stmt)?
// | "for" "(" (declaration | stmt) expr? ";" expr? ")" stmt
// | "while" "(" expr ")" stmt
// | "return" expr ";"
static struct Node *stmt() {
struct Node *node;
if (consume("{")) {
struct Node head;
head.next = NULL;
struct Node *cur = &head;
while (!consume("}")) {
if (equal_basictype())
cur = cur->next = declaration();
else
cur = cur->next = stmt();
add_type(cur);
}
node = new_node(ND_BLOCK, NULL, NULL);
node->body = head.next;
return node;
} else if (consume_keyword("if")) {
node = new_node(ND_IF, NULL, NULL);
expect("(");
node->cond = expr();
expect(")");
node->then = stmt();
if (consume_keyword("else"))
node->els = stmt();
return node;
} else if (consume_keyword("for")) {
node = new_node(ND_LOOP, NULL, NULL);
expect("(");
if (equal_basictype())
node->init = declaration(); // 初期化式で変数定義
else
node->init = stmt(); // ";"の部分も含めてstmt
if (!consume(";")) {
node->cond = expr();
expect(";");
}
if (!consume(")")) {
node->inc = expr();
expect(")");
}
node->then = stmt();
return node;
} else if (consume_keyword("while")) {
node = new_node(ND_LOOP, NULL, NULL);
expect("(");
node->cond = expr();
expect(")");
node->then = stmt();
return node;
} else if (consume_keyword("return")) {
node = expr();
add_type(node);
decay_arr(node);
node = new_node(ND_RETURN, node, NULL);
} else if (consume(";")) { // ";"だけの文
return new_node(ND_BLOCK, NULL, NULL); // 空のブロック
} else {
node = expr();
}
expect(";");
return node;
}
// expr = assign
static struct Node *expr() {
return assign();
}
// assign = logor (("="|"+="|"-="|"*="|"/="|"%=") assign)?
static struct Node *assign() {
struct Node *node = logor();
if (consume("=")) {
struct Node *rhs = assign();
decay_arr(rhs); // 右辺が配列ならポインタに降格
node = new_node(ND_ASSIGN, node, rhs);
} else if (consume("+=")) {
struct Node *rhs = assign();
decay_arr(rhs);
node = new_add_asgn(node, rhs);
} else if (consume("-=")) {
struct Node *rhs = assign();
decay_arr(rhs);
node = new_sub_asgn(node, rhs);
} else if (consume("*=")) {
struct Node *rhs = assign();
decay_arr(rhs);
node = new_node(ND_MULASGN, node, rhs);
} else if (consume("/=")) {
struct Node *rhs = assign();
decay_arr(rhs);
node = new_node(ND_DIVASGN, node, rhs);
} else if (consume("%=")) {
struct Node *rhs = assign();
decay_arr(rhs);
node = new_node(ND_REMASGN, node, rhs);
}
return node;
}
// logor = logand ("||" logand)*
static struct Node *logor() {
struct Node *node = logand();
for (;;) {
if (consume("||"))
node = new_node(ND_LOGOR, node, logand());
else
return node;
}
}
// logand = equality ("&&" equality)*
static struct Node *logand() {
struct Node *node = equality();
for (;;) {
if (consume("&&"))
node = new_node(ND_LOGAND, node, equality());
else
return node;
}
}
// equality = relational ("==" relational | "!=" relational)*
static struct Node *equality() {
struct Node *node = relational();
for (;;) {
if (consume("=="))
node = new_node(ND_EQ, node, relational());
else if (consume("!="))
node = new_node(ND_NE, node, relational());
else
return node;
}
}
// relational = add ("<" add | "<=" add | ">" add | ">=" add)*
static struct Node *relational() {
struct Node *node = add();
for (;;) {
if (consume("<"))
node = new_node(ND_LT, node, add());
else if (consume("<="))
node = new_node(ND_LE, node, add());
else if (consume(">"))
node = new_node(ND_LT, add(), node);
else if (consume(">="))
node = new_node(ND_LE, add(), node);
else
return node;
}
}
// ポインタ演算の場合、pをポインタ、nを整数とすると
// p + nはpにnをそのまま加算するのではなく、
// sizeof(*p)*nをpの値に加算する。
static struct Node *new_add(struct Node *lhs, struct Node *rhs) {
add_type(lhs);
add_type(rhs);
// num + num
if (is_integer(lhs->ty) && is_integer(rhs->ty)) {
// lhsの型がadd計算後の型になる
// char + int を int + char にひっくり返す
if (lhs->ty->kind == TY_CHAR && rhs->ty->kind == TY_INT) {
struct Node *tmp = lhs;
lhs = rhs;
rhs = tmp;
}
return new_node(ND_ADD, lhs, rhs);
}
// 配列に対するaddは先頭要素のアドレスに降格
decay_arr(lhs); // 配列ならポインタに降格
decay_arr(rhs);
// ptr + ptr は計算できない
if (lhs->ty->base && rhs->ty->base)
error_at(token->str, "無効な演算(pointer+pointer)");
// num + ptr を ptr + num にひっくり返す.
if (!lhs->ty->base && rhs->ty->base) {
struct Node *tmp = lhs;
lhs = rhs;
rhs = tmp;
}
// ptr + num
rhs = new_node(ND_MUL, rhs, new_node_num(lhs->ty->base->size)); // シフト演算子を定義したら変更する
return new_node(ND_ADD, lhs, rhs);
}
// -演算子にもポインタと整数に対しての演算がある
static struct Node *new_sub(struct Node *lhs, struct Node *rhs) {
add_type(lhs);
add_type(rhs);
// num - num
if (is_integer(lhs->ty) && is_integer(rhs->ty)) {
// char - int を -int + char にする.
if (lhs->ty->kind == TY_CHAR && rhs->ty->kind == TY_INT)
return new_add(new_node(ND_SUB, new_node_num(0), rhs), lhs);
return new_node(ND_SUB, lhs, rhs);
}
// 配列に対するsubは先頭要素のアドレスに降格
decay_arr(lhs); // 配列ならポインタに降格
decay_arr(rhs);
// ptr - num
if (lhs->ty->base && is_integer(rhs->ty)) {
rhs = new_node(ND_MUL, rhs, new_node_num(lhs->ty->base->size)); // シフト演算子を定義したら変更する
add_type(rhs);
struct Node *node = new_node(ND_SUB, lhs, rhs);
node->ty = lhs->ty;
return node;
}
// ptr - ptr
// 2つのポインタ間の要素数を返す
if (lhs->ty->base && rhs->ty->base) {
struct Node *node = new_node(ND_SUB, lhs, rhs);
node->ty = new_type(TY_INT);
return new_node(ND_DIV, node, new_node_num(lhs->ty->base->size)); // シフト演算子を定義したら変更する
}
error_at(token->str, "無効な演算");
return NULL;
}
static struct Node *new_add_asgn(struct Node *lhs, struct Node *rhs) {
// if (!is_value(lhs))
// error_at(token->str, "非左辺値への代入");
add_type(lhs);
add_type(rhs);
// num += num
if (is_integer(lhs->ty) && is_integer(rhs->ty)) {
// lhsの型がadd_assign計算後の型になる
return new_node(ND_ADDASGN, lhs, rhs);
}
// array += ? は計算できない
if (lhs->ty->kind == TY_ARRAY)
error_at(token->str, "無効な演算(array+=num)");
// 配列に対するaddは先頭要素のアドレスに降格
decay_arr(rhs);
// ptr += ptr は計算できない
if (lhs->ty->base && rhs->ty->base)
error_at(token->str, "無効な演算(pointer+pointer)");
// num += ptr
if (!lhs->ty->base && rhs->ty->base) {
lhs = new_node(ND_MUL, lhs, new_node_num(rhs->ty->base->size));
return new_node(ND_ADDASGN, lhs, rhs);
}
if (lhs->ty->kind != TY_PTR)
error_at(token->str, "左辺値はポインタではありません");
if (!is_integer(rhs->ty))
error_at(token->str, "右辺値は整数型ではありません");
// ptr += num
rhs = new_node(ND_MUL, rhs, new_node_num(lhs->ty->base->size)); // シフト演算子を定義したら変更する
return new_node(ND_ADDASGN, lhs, rhs);
}
static struct Node *new_sub_asgn(struct Node *lhs, struct Node *rhs) {
add_type(lhs);
add_type(rhs);
// num -= num
if (is_integer(lhs->ty) && is_integer(rhs->ty)) {
// lhsの型がsub_assign計算後の型になる
return new_node(ND_SUBASGN, lhs, rhs);
}
// array -= ? は計算できない
if (lhs->ty->kind == TY_ARRAY)
error_at(token->str, "無効な演算(array-=num)");
// 配列に対するsubは先頭要素のアドレスに降格
decay_arr(rhs);
// ptr -= num
if (lhs->ty->base && is_integer(rhs->ty)) {
rhs = new_node(ND_MUL, rhs, new_node_num(lhs->ty->base->size)); // シフト演算子を定義したら変更する
add_type(rhs);
struct Node *node = new_node(ND_SUBASGN, lhs, rhs);
node->ty = lhs->ty;
return node;
}
// ptr -= ptr
// ptr - ptr は本来intのはずなので対応しない
// if (lhs->ty->base && rhs->ty->base) {
// lhs = new_node(ND_DIVASGN, lhs, new_node_num(lhs->ty->base->size));
// rhs = new_node(ND_DIV, rhs, new_node_num(rhs->ty->base->size));
// return new_node(ND_SUBASGN, lhs, rhs); // lhsが2回評価される
// }
error_at(token->str, "無効な演算");
return NULL;
}
// add = mul ("+" mul | "-" mul)*
static struct Node *add() {
struct Node *node = mul();
for (;;) {
if (consume("+"))
node = new_add(node, mul());
// ポインタ演算との場合分けなどの関数
else if (consume("-"))
node = new_sub(node, mul());
// ポインタ演算との場合分けなどの関数
else
return node;
}
}
// mul = unary ("*" unary | "/" unary | "%" unary)*
static struct Node *mul() {
struct Node *node = unary();
for (;;) {
if (consume("*"))
node = new_node(ND_MUL, node, unary());
else if (consume("/"))
node = new_node(ND_DIV, node, unary());
else if (consume("%"))
node = new_node(ND_REM, node, unary());
else
return node;
}
}
// unary = ("++"|"--")? unary
// | ("+" | "-")? postfix
// | "*" unary
// | "&" postfix
// | "!" unary
static struct Node *unary() {
if (consume("++")) // 前置インクリメント
return new_add_asgn(unary(), new_node_num(1));
if (consume("--")) // 前置デクリメント
return new_sub_asgn(unary(), new_node_num(1));
if (consume("+"))
return new_node(ND_ADD, new_node_num(0), postfix());
if (consume("-"))
return new_node(ND_SUB, new_node_num(0), postfix());
if (consume("*")) {
struct Node *node = unary();
decay_arr(node); // 配列ならポインタに降格
return new_node(ND_DEREF, node, NULL);
}
if (consume("&"))
return new_node(ND_ADDR, postfix(), NULL);
if (consume("!"))
return new_node(ND_EQ, new_node_num(0), unary()); // 0 == E
return postfix();
}