-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.c
1053 lines (879 loc) · 27.3 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
/* parse.c */
#include "zcc.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
/* =---- Parser ----= */
/* Assumption: all tokens before lex_token are parsed. lex_token is the next to be parsed. */
int _prs_depth = -1;
//#define PRT_LEAVING
#define PRS_FUNC_BG _prs_depth++;
#ifdef PRT_LEAVING
#define PRS_FUNC_ED \
PT_PRT_IND\
fprintf(stderr, "%s() leaving\n", __FUNCTION__); \
_prs_depth--;
#else
#define PRS_FUNC_ED _prs_depth--;
#endif
#define PT_PRT_IND \
{ int _i = _prs_depth; \
while(_i--) fprintf(stderr, "- "); }
#define PT_FUNC \
PT_PRT_IND \
fprintf(stderr, "In %s()\n", __FUNCTION__);
int prs_prec[256];
int prs_asso[256];
/***
* Initialize operator precedence & association
*/
void prs_init() {
#define zz(u, v)
#define xx yy
#define yy(tk_class, u, asso_, prec_) \
prs_prec[tk_class] = prec_; \
prs_asso[tk_class] = asso_;
#include "operators.h"
#undef xx
#undef yy
#undef zz
}
#define prs_expect_char(c) _prs_expect_char (c, __FUNCTION__, __LINE__)
#define prs_expect_class(c) _prs_expect_class(c, #c, __FUNCTION__, __LINE__)
int _prs_expect_char(char c, const char *fname, int linen) {
if(lex_token.tk_str[0] != c) {
fprintf(stderr, "\n=== In %s() line %d ===\n", fname, linen);
fprintf(stderr, "Expect '%c' but encounter '%c'\n", c, lex_token.tk_str[0]);
}
assert(lex_token.tk_str[0] == c);
return 0;
}
int _prs_expect_class(tk_class_en tk_class, const char *cname, const char *fname, int linen) {
if(lex_token.tk_class != tk_class) {
fprintf(stderr, "\n=== In %s() line %d ===\n", fname, linen);
fprintf(stderr, "Expect '%s' but encounter '%s'\n", cname, lex_token.tk_str);
}
assert(lex_token.tk_class == tk_class);
return 0;
}
/* =-- Expressions --= */
/***
expression:
assignment-expression { , assignment-expression }
*/
var_st* prs_expr() {
PRS_FUNC_BG
PT_FUNC
var_st *r;
//prs_binary(prs_prec[TK_OP_ASSIGN]);
r = prs_assign();
while(lex_valid() && lex_token.tk_str[0] == ',') {
PT_PRT_IND
fprintf(stderr, "operator[,]\n");
lex_next();
r = prs_assign();
}
PRS_FUNC_ED
return r;
}
/***
assignment-expression:
conditional-expression
unary-expression assign-operator assignment-expression
assign-operator:
one of = += -= *= /= %= <<= >>= &= ^= |=
Note:
It's hard to choose from one of the productions.
So we merge them into
=> conditional-expression assign-operator assignment-expression
This leads to incorrect expresstion like a + b = c gets accepted
But we could leave error detection later to semantic stage
i.e. reject input once we find a+b is not lvalue.
*/
var_st* prs_assign() {
PRS_FUNC_BG
PT_FUNC
var_st *r, *t;
r = prs_cond();
if(lex_valid() && lex_isassignop(&lex_token)) {
PT_PRT_IND
fprintf(stderr, "operator[%s]\n", lex_token.tk_str);
/* TODO: support assignment other than = */
lex_next();
t = prs_assign();
assert(r->lvalue == 1);
gen_emit(IR_ASSIGN, r, t);
sym_dispose_temp_var(t);
}
PRS_FUNC_ED
return r;
}
/***
conditional-expression:
binary-expression [ ? expression : conditional-expression ]
*/
var_st* prs_cond() {
PRS_FUNC_BG
PT_FUNC
var_st *r;
r = prs_binary(prs_prec[TK_OP_ASSIGN]+1);
if(lex_valid() && lex_token.tk_str[0] == '?') {
PT_PRT_IND
fprintf(stderr, "operator[?:]\n");
/* TODO: support trinity operator, i.e. cond ? A : B; */
lex_next();
prs_expr();
prs_expect_char(':'); lex_next();
prs_cond();
}
PRS_FUNC_ED
return r;
}
/***
binary-expression:
unary-expression { binary-operator unary-expression }
Note:
prs_binary(k): parse binary expression of precedence k or higher
precedence defined in operators.h
trick avoiding deep recursion descripted in LCC Chap.8.6
*/
var_st* prs_binary(int k) {
PRS_FUNC_BG
PT_PRT_IND
fprintf(stderr, "In prs_binary(k=%d)\n", k);
var_st *r, *u = NULL, *x, *y, *t = sym_make_temp_var(type_int);
ir_op_en ir_op;
tk_class_en tk_op;
int i;
r = prs_unary();
for(i = prs_prec[lex_token.tk_class]; lex_valid() && i >= k; i--) {
/* parse from high precedence to low precedence */
/* TODO: 1. manage && || execution flow
2. add support to other operators
3. support for pointer add integer, pointers subtract
4. support other operators
5. variable lifting (a.k.a varible promition)
*/
while(lex_valid() && prs_prec[lex_token.tk_class] == i) {
PT_PRT_IND
fprintf(stderr, "operator[%s]\n", lex_token.tk_str);
tk_op = lex_token.tk_class;
if(tk_op == TK_OP_ADD) ir_op = IR_ADD;
else if(tk_op == TK_OP_LT) ir_op = IR_LT;
else if(tk_op == TK_OP_LE) ir_op = IR_LE;
else if(tk_op == TK_OP_EQ) ir_op = IR_EQ;
else if(tk_op == TK_OP_GT) ir_op = IR_GT;
else if(tk_op == TK_OP_GE) ir_op = IR_GE;
else if(tk_op == TK_OP_NEQ) ir_op = IR_NEQ;
else if(tk_op == TK_OP_SUB) ir_op = IR_SUB;
else if(tk_op == TK_OP_MUL) ir_op = IR_MUL;
else if(tk_op == TK_OP_DIV) ir_op = IR_DIV;
else fexit("Not supported operator %s", lex_token.tk_str);
lex_next();
y = prs_binary(i+1);
if(u == NULL) {
/* TODO: variable lifting */
x = r;
u = sym_make_temp_var(binop_res_type(ir_op, x->type, y->type));
}
else {
x = u;
u->type = binop_res_type(ir_op, x->type, y->type);
}
if(sym_is_pointer(x) || sym_is_pointer(y)) {
if(x->type == type_int) {
gen_emit_offset(t, x, y->type->ref->bytes);
x = y; y = t;
}
if(y->type == type_int) {
gen_emit_offset(t, y, x->type->ref->bytes);
y = t;
}
}
gen_emit(ir_op, u, x, y);
}
}
sym_dispose_temp_var(t);
PRS_FUNC_ED
return u ? u : r;
}
/***
unary-expression:
postfix-expression
unary-operator unary-expression
'(' type-name ')' unary-expression
sizeof unary-expression
sizeof '(' type-name ')'
*/
var_st* prs_unary() {
PRS_FUNC_BG
PT_FUNC
var_st *r = NULL;
if(lex_token.tk_class == TK_OP_SIZEOF) {
/* TODO */
lex_next();
if(lex_token.tk_str[0] == '(') {
/* => sizeof '(' type-name ')' */
lex_next();
if(!sym_hastype(&lex_token))
fexit("expect `type-name`.");
else {
lex_next();
prs_expect_char(')'); lex_next();
}
}
else
/* => sizeof unary-expression */
prs_unary();
}
else if(lex_token.tk_str[0] == '(') {
/* TODO */
lex_next();
if(sym_hastype(&lex_token)) {
/* => '(' type-name ')' unary-expression */
lex_next();
prs_expect_char(')'); lex_next();
prs_unary(); /* TODO */
}
else {
/* TRICK: parsed primary-expression for postfix-expression
* => postfix-expression
* => primary-expression { postfix-operator }
* => '(' expression ')' { postfix-operator }
*/
var_st *var = prs_expr();
prs_expect_char(')'); lex_next();
r = prs_pst(var);
}
}
else if(lex_isunaryop(&lex_token)) {
/* TODO */
/* => unary-operator unary-expression */
tk_class_en tk_class = lex_token.tk_class;
PT_PRT_IND
fprintf(stderr, "operator[%s]\n", lex_token.tk_str);
lex_next();
if(tk_class == TK_OP_MUL) {
r = prs_unary();
assert(sym_is_pointer(r));
r = sym_deref(r);
}
else {
/* TODO */
fexit("unary operator other than * not implemented");
}
}
else {
r = prs_pst(0);
}
PRS_FUNC_ED
return r;
}
/***
postfix-expression:
primary-expression { postfix-operator }
postfix-operator:
'[' expression ']' // array index. e.g. a[0]
. identifier
-> identifier
++
--
Note: assume primary-expression parsed by caller and passed(?) in.
*/
var_st* prs_pst(var_st* passed_primary/* placeholder for primary-expression */) {
PRS_FUNC_BG
PT_FUNC
var_st *r = passed_primary, *t, *ind;
if(!passed_primary)
r = prs_primary();
while(1) {
/* TODO: parse postfix-operator */
if(!lex_valid()) break;
if(lex_token.tk_str[0] == '[') {
/* => '[' expression ']' */
lex_next();
ind = prs_expr();
assert(sym_is_pointer(r));
t = sym_make_temp_lvar(r->type->ref, 1); /* make lvalue */
gen_emit(IR_IND, t, r, ind);
sym_dispose_temp_var(r);
r = t;
prs_expect_char(']'); lex_next();
}
else if(lex_token.tk_str[0] == '.') {
PT_PRT_IND
fprintf(stderr, "operator[.]\n");
lex_next();
sym_hasid(&lex_token);
lex_next();
}
else if(lex_token.tk_class == TK_OP_DEREF) {
PT_PRT_IND
fprintf(stderr, "operator[->]\n");
lex_next();
sym_hasid(&lex_token);
lex_next();
}
else if(lex_token.tk_class == TK_OP_INC) {
PT_PRT_IND
fprintf(stderr, "operator[T++]\n");
lex_next();
t = sym_make_temp_var(r->type);
gen_emit(IR_ASSIGN, t, r);
gen_emit(IR_INC, r);
r = t;
}
else if(lex_token.tk_class == TK_OP_DEC) {
PT_PRT_IND
fprintf(stderr, "operator[T--]\n");
lex_next();
t = sym_make_temp_var(r->type);
gen_emit(IR_ASSIGN, t, r);
gen_emit(IR_DEC, r);
r = t;
}
else break;
}
PRS_FUNC_ED
return r;
}
/***
primary-expression:
identifer
constant
string-literal
'(' expression ')'
function-call
function-call:
identifier '(' argument-list ')'
*/
var_st* prs_primary() {
PRS_FUNC_BG
char vname[56];
var_st* r = NULL;
if(lex_token.tk_class == TK_IDENTIFIER) {
PT_PRT_IND
printf("prs_primary[IDENTIFIER=%s]\n", lex_token.tk_str);
strcpy(vname, lex_token.tk_str);
lex_next();
if(lex_token.tk_str[0] != '(') {
/* => identifer */
fprintf(stderr, "finding var[%s]\n", vname);
r = sym_find_var(vname);
//assert(r);
if(!r) {
fprintf(stderr, "var[name=%s] not found\n", vname);
fexit("");
}
}
else {
/* => function-call */
lex_next();
PT_PRT_IND
printf("prs_primary[call=%s()]\n", vname);
list_st *pars = prs_args();
func_st *func = sym_find_func(vname);
if(!func) {
fprintf(stderr, "Warning: function call of var[name=%s] not declared\n", vname);
/* TODO:
* for now, regard undeclared function return int.
*/
func = sym_make_temp_func(vname, type_int);
}
r = sym_make_temp_var(func->rtype);
gen_emit_call(IR_CALL, func, r, pars);
prs_expect_char(')'); lex_next();
}
}
else if(lex_token.tk_class == TK_CONST_INT) {
PT_PRT_IND
printf("prs_primary[CONST=%s]\n", lex_token.tk_str);
r = sym_make_imm(&lex_token);
//fprintf(stderr, "const[addr=%x, value=%d]", r, r->value->i);
lex_next();
//fprintf(stderr, "!--sym_make_imm(&lex_token)\n");
}
else if(lex_token.tk_class == TK_CONST_STRING) {
PT_PRT_IND
r = sym_make_imm(&lex_token);
lex_next();
}
else if(lex_token.tk_str[0] == '(') {
lex_next();
r = prs_expr();
lex_next();
prs_expect_char(')');
lex_next();
}
else {
PT_PRT_IND
fprintf(stderr, "In primary(): token[%s]\n", lex_token.tk_str);
PT_PRT_IND
fexit("In primary(): Unexpected token");
}
PRS_FUNC_ED
return r;
}
/***
argument-list:
assignment-expression { , assignment-expression }
*/
list_st* prs_args() {
PRS_FUNC_BG
PT_FUNC
list_st *args = make_list();
if(lex_token.tk_str[0] == ')') return args;
int i = 0;
while(1) {
i++;
list_append(args, prs_assign());
if(lex_token.tk_str[0] == ',') lex_next();
else if(lex_token.tk_str[0] == ')') break;
else fexit("Unexpected token");
}
PRS_FUNC_ED
return args;
}
/* =-- Statements --= */
/***
statement:
ID : statement
case constant-expression : statement
default : statement
[ expression ] ;
if '(' expression ')' statement
if '(' expression ')' statement else statement
switch '(' expression ') ' statement
while '(' expression ') ' statement
do statement while '(' expression ')' ;
for '(' [ expression ] ; [ expression ] ; [ expression ] ')' statement
break ;
continue ;
goto ID ;
return [ expression ] ;
compound-statement
compound-statement:
'{' { declaration } { statement } '}'
Note:
Seems like declaration is only allowed at the beginning of the block.
This is subject to change but leave it for now.
*/
int prs_stmt_label() {
/* => ID : statement */
PT_PRT_IND
fprintf(stderr, "[label=%s]\n", lex_token.tk_str);
lex_next();
prs_expect_char(':'); lex_next();
prs_stmt();
return 0;
}
int prs_stmt_case() {
/* => case constant-expression : statement */
PT_PRT_IND
fprintf(stderr, "[keyword=%s]\n", lex_token.tk_str);
lex_next();
prs_expr();
prs_expect_char(':'); lex_next();
prs_stmt();
return 0;
}
int prs_stmt_default() {
PT_PRT_IND
fprintf(stderr, "[keyword=%s]\n", lex_token.tk_str);
lex_next();
prs_expect_char(':');
prs_stmt();
return 0;
}
int prs_stmt_if() {
PT_PRT_IND
fprintf(stderr, "[keyword=%s]\n", lex_token.tk_str);
var_st *cond;
label_st *ifT = sym_make_label(), *ifF = sym_make_label(), *ifE = sym_make_label();
lex_next();
prs_expect_char('('); lex_next();
cond = prs_expr();
prs_expect_char(')'); lex_next();
if(cond->value) {
if(cond->value->i) gen_emit(IR_JMP, ifT);
else gen_emit(IR_JMP, ifF);
}
else {
gen_emit(IR_CJMP, cond, ifT);
gen_emit(IR_JMP, ifF);
}
sym_dispose_temp_var(cond);
gen_emit(IR_LABEL, ifT);
prs_stmt();
gen_emit(IR_JMP, ifE);
if(lex_valid() && lex_token.tk_class == TK_ELSE) {
PT_PRT_IND
fprintf(stderr, "[keyword=%s]\n", lex_token.tk_str);
lex_next();
gen_emit(IR_LABEL, ifF);
prs_stmt();
}
else ifF->chain = ifE;
gen_emit(IR_LABEL, ifE);
return 0;
}
int prs_stmt_switch() {
PT_PRT_IND
fprintf(stderr, "[keyword=%s]\n", lex_token.tk_str);
lex_next();
prs_expect_char('('); lex_next();
prs_expr();
prs_expect_char(')'); lex_next();
prs_stmt();
return 0;
}
int prs_stmt_while() {
PT_PRT_IND
fprintf(stderr, "[keyword=%s]\n", lex_token.tk_str);
sym_mnp_scope();
var_st *cond;
label_st *forC = sym_make_label(), *forB = sym_make_label(), *forE = sym_make_label();
context.scope->forB = forB;
context.scope->forE = forE;
context.scope->forC = forC;
gen_emit(IR_LABEL, forC);
lex_next();
prs_expect_char('('); lex_next();
cond = prs_expr();
prs_expect_char(')'); lex_next();
if(cond->value) {
/* deterministic branch */
if(cond->value->i) gen_emit(IR_JMP, forB);
else gen_emit(IR_JMP, forE);
}
else {
gen_emit(IR_CJMP, cond, forB);
gen_emit(IR_JMP, forE);
}
sym_dispose_temp_var(cond);
gen_emit(IR_LABEL, forB);
prs_stmt();
gen_emit(IR_LABEL, forE);
sym_pop_scope();
return 0;
}
int prs_stmt_dowhile() {
PT_PRT_IND
fprintf(stderr, "[keyword=%s]\n", lex_token.tk_str);
sym_mnp_scope();
var_st *cond;
label_st *forB = sym_make_label(), *forE = sym_make_label();
context.scope->forB = forB;
context.scope->forE = forE;
gen_emit(IR_LABEL, forB);
lex_next();
prs_stmt();
prs_expect_class(TK_WHILE); lex_next();
prs_expect_char('('); lex_next();
cond = prs_expr();
gen_emit(IR_CJMP, cond, forB);
sym_dispose_temp_var(cond);
prs_expect_char(')'); lex_next();
prs_expect_char(';'); lex_next();
gen_emit(IR_LABEL, forE);
sym_pop_scope();
return 0;
}
int prs_stmt_for() {
PT_PRT_IND
fprintf(stderr, "[keyword=%s]\n", lex_token.tk_str);
sym_mnp_scope();
var_st *cond;
label_st *forB = sym_make_label(), *forC = sym_make_label(), *forE = sym_make_label();
label_st *forI = sym_make_label();
context.scope->forB = forB;
context.scope->forE = forE;
context.scope->forC = forC;
lex_next();
prs_expect_char('('); lex_next();
if(lex_token.tk_str[0] != ';') {
prs_expr();
}
prs_expect_char(';'); lex_next();
if(lex_token.tk_str[0] != ';') {
gen_emit(IR_LABEL, forC);
cond = prs_expr();
gen_emit(IR_CJMP, cond, forB);
gen_emit(IR_JMP, forE);
}
prs_expect_char(';'); lex_next();
if(lex_token.tk_str[0] != ')') {
gen_emit(IR_LABEL, forI);
prs_expr();
gen_emit(IR_JMP, forC);
}
prs_expect_char(')'); lex_next();
sym_dispose_temp_var(cond);
gen_emit(IR_LABEL, forB);
prs_stmt();
gen_emit(IR_JMP, forI);
gen_emit(IR_LABEL, forE);
sym_pop_scope();
return 0;
}
int prs_stmt_break() {
PT_PRT_IND
fprintf(stderr, "[keyword=%s]\n", lex_token.tk_str);
lex_next();
prs_expect_char(';'); lex_next();
assert(context.scope->forE);
gen_emit(IR_JMP, context.scope->forE);
return 0;
}
int prs_stmt_continue() {
PT_PRT_IND
fprintf(stderr, "[keyword=%s]\n", lex_token.tk_str);
lex_next();
prs_expect_char(';'); lex_next();
assert(context.scope->forC);
gen_emit(IR_JMP, context.scope->forC);
return 0;
}
int prs_stmt_goto() {
PT_PRT_IND
fprintf(stderr, "[keyword=%s]\n", lex_token.tk_str);
lex_next();
prs_expect_class(TK_IDENTIFIER); lex_next();
prs_expect_char(';'); lex_next();
return 0;
}
int prs_stmt_return() {
PT_PRT_IND
fprintf(stderr, "[keyword=%s]\n", lex_token.tk_str);
var_st *v = NULL;
lex_next();
if(lex_token.tk_str[0] != ';') {
v = prs_expr();
}
prs_expect_char(';'); lex_next();
if(context.func->ret && v)
gen_emit(IR_ASSIGN, context.func->ret, v);
gen_emit(IR_RETURN);
return 0;
}
int prs_stmt() {
PRS_FUNC_BG
PT_FUNC
if(sym_islabel(&lex_token)) {
/* => ID : statement */
prs_stmt_label();
}
else if(lex_token.tk_class == TK_CASE) {
/* => case constant-expression : statement */
prs_stmt_case();
}
else if(lex_token.tk_class == TK_DEFAULT) {
/* => default : statement */
prs_stmt_default();
}
else if(lex_token.tk_class == TK_IF) {
/* => if '(' expression ')' statement
* => if '(' expression ')' statement else statement
*/
prs_stmt_if();
}
else if(lex_token.tk_class == TK_SWITCH) {
/* => switch '(' expression ') ' statement */
prs_stmt_switch();
}
else if(lex_token.tk_class == TK_WHILE) {
/* => while '(' expression ') ' statement */
prs_stmt_while();
}
else if(lex_token.tk_class == TK_DO) {
/* => do statement while '(' expression ')'; */
prs_stmt_dowhile();
}
else if(lex_token.tk_class == TK_FOR) {
/* => for '(' [ expression ] ; [ expression ] ; [ expression ] ')' statement */
prs_stmt_for();
}
else if(lex_token.tk_class == TK_BREAK) {
/* => break ; */
prs_stmt_break();
}
else if(lex_token.tk_class == TK_CONTINUE) {
/* => continue ; */
prs_stmt_continue();
}
else if(lex_token.tk_class == TK_GOTO) {
/* => break ; */
prs_stmt_goto();
}
else if(lex_token.tk_class == TK_RETURN) {
/* => return [ expression ] ; */
prs_stmt_return();
}
else if(lex_token.tk_str[0] == '{') {
/* => '{' { declaration } { statement } '}' */
/* TODO: need declaration */
PT_PRT_IND
fprintf(stderr, "stmt[{}]\n");
lex_next();
sym_mnp_scope();
prs_decls();
while(lex_token.tk_str[0] != '}') {
prs_stmt();
}
sym_pop_scope();
prs_expect_char('}'); lex_next();
PT_PRT_IND
fprintf(stderr, "prs_stmt[leave], next lex_token=%s\n", lex_token.tk_str);
}
else if(lex_token.tk_str[0] != '}') {
/* => [expression] ;
* (single expression)
*/
prs_expr();
prs_expect_char(';'); lex_next();
}
PRS_FUNC_ED;
return 0;
}
/* =-- Statements --= */
/***
* { declaration }
*/
int prs_decls() {
PRS_FUNC_BG
PT_FUNC
while(sym_hastype(&lex_token)) {
prs_decl();
fprintf(stderr, "\n");
}
PRS_FUNC_ED
return 0;
}
/***
declaration:
declaration-specifiers init-declarator { , init-declarator } ;
declaration-specifiers func-declarator compound-statement
init-declarator:
identifier
identifier = assignment-expression
func-declarator:
identifer '(' parameter-list ')'
parameter-list:
parameter { , parameter } [ , ... ]
parameter:
declaration-specifiers identifier
*/
int prs_decl() {
PRS_FUNC_BG
PT_FUNC
type_st *type_base = prs_decl_spec();
type_st *type = NULL;
var_st *var = NULL;
char vname[255];
/* (possibly) Variable declaration
* => init-declarator { , init-declarator } ;
* => identifier { ... }
* identifier = assignment-expression { ... }
*/
while(1) {
var = NULL;
type = type_base;
while(lex_token.tk_str[0] == '*') {
PT_PRT_IND
fprintf(stderr, "operator[*]\n");
lex_next();
type = pointer_of(type);
}
prs_expect_class(TK_IDENTIFIER);
strcpy(vname, lex_token.tk_str);
lex_next();
PT_PRT_IND
fprintf(stderr, "%s[name='%s']\n", lex_token.tk_str[0] == '(' ? "func" : "var", vname);
if(lex_token.tk_str[0] != '(') {
/* Variable declaration */
var = sym_make_var(vname, type);
}
if(lex_token.tk_class == TK_OP_ASSIGN) {
/* TODO */
PT_PRT_IND
fprintf(stderr, "operator[=]\n");
lex_next();
var_st *result = prs_assign();
gen_emit(IR_ASSIGN, var, result);
}
if(lex_token.tk_str[0] == ',') lex_next();
else break;
}
/* function declaration */
if(lex_token.tk_str[0] == '(') { lex_next();
/* => declaration-specifiers func-declarator compound-statement
* => declaration-specifiers identifer '(' parameter-list ')'
* where specifier & identifier are already parsed
*
* parameter-list: parameter { , parameter } [ , ... ]
* parameter: declaration-specifiers identifier
*/
sym_mnp_scope();
func_st *nfunc = sym_make_func(vname, type);
if(lex_token.tk_str[0] != ')') {
/* parse parameters
* TODO: wrap in a indepedent parse function
*/
while(1) {
char *par_name;
type_st *par_type = prs_decl_spec();
if(lex_token.tk_str[0] == '*') {
PT_PRT_IND
fprintf(stderr, "operator[*]\n");
lex_next();
par_type = pointer_of(par_type);
}
/* parameter name is not obligatory */
par_name = lex_token.tk_class == TK_IDENTIFIER ? dup_str(lex_token.tk_str) : NULL;
PT_PRT_IND
fprintf(stderr, "para[name='%s']\n", par_name);
if(par_name) lex_next();