-
Notifications
You must be signed in to change notification settings - Fork 1
/
parse_analyze_statement.c
500 lines (413 loc) · 14.2 KB
/
parse_analyze_statement.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
#include "analyzer.h"
#include "std.h"
#include "std_io.h"
static struct Expr integer_1(void)
{
struct Expr expr;
expr.details.type = INT_TYPE();
expr.int_value = 1;
expr.category = INT_VALUE;
return expr;
}
/*
* Adjusts the newest_offset and add a local variable to the scope.
* Returns the offset of the newly added variable.
*/
int add_local_var_to_scope(struct AnalyzerState *ptr_ps,
const struct Type *ref_vartype, const char *str)
{
ptr_ps->newest_offset -= size_of(ptr_ps, ref_vartype);
struct LocalVarInfo *ptr_varinfo = calloc(1, sizeof(struct LocalVarInfo));
ptr_varinfo->offset = ptr_ps->newest_offset;
ptr_varinfo->type = *ref_vartype;
insert(ptr_ps->scope_chain.var_table, str, ptr_varinfo);
return ptr_varinfo->offset;
}
struct Statement parse_labeled_statement(struct AnalyzerState *ptr_ps,
const struct Token **ptr_tokvec)
{
const struct Token *tokvec = *ptr_tokvec;
enum TokenKind kind = tokvec[0].kind;
struct SourceLabel l;
if (kind == RES_DEFAULT) {
++tokvec;
l.category = DEFAULT_LABEL;
} else if (kind == RES_CASE) {
++tokvec;
const struct UntypedExpr u = parse_constant_expression(&tokvec);
int case_int =
typecheck_constant_integral_expression(ptr_ps, &u, "case");
l.category = CASE_LABEL;
l.case_int = case_int;
} else {
l.ident_str = tokvec[0].ident_str;
++tokvec;
l.category = IDENT_LABEL;
}
expect_and_consume(&tokvec, COLON,
"colon of `default:`, `case VALUE:`, or `ident:`");
struct Statement s = parse_statement(ptr_ps, &tokvec);
struct SourceLabel *ptr_l = calloc(1, sizeof(struct SourceLabel));
*ptr_l = l;
push_vector(&s.labels, ptr_l);
*ptr_tokvec = tokvec;
return s;
}
static struct Expr *declare_var_and_return_initializer(
struct AnalyzerState *ptr_ps, const struct Type *ref_vartype,
const char *str, const struct UntypedExpr *ptr_uexpr,
struct Statement *ptr_statement);
struct Statement parse_statement(struct AnalyzerState *ptr_ps,
const struct Token **ptr_tokvec)
{
const struct Token *tokvec = *ptr_tokvec;
if (tokvec[0].kind == RES_CASE || tokvec[0].kind == RES_DEFAULT ||
(tokvec[0].kind == IDENT_OR_RESERVED && tokvec[1].kind == COLON)) {
return parse_labeled_statement(ptr_ps, ptr_tokvec);
}
switch (tokvec[0].kind) {
case LEFT_BRACE: {
return parse_compound_statement(ptr_ps, ptr_tokvec);
}
case RES_IF: {
++tokvec;
expect_and_consume(&tokvec, LEFT_PAREN,
"left parenthesis immediately after `if`");
const struct UntypedExpr u = parse_expression(&tokvec);
struct Expr expr = typecheck_expression(ptr_ps, &u);
expect_scalar(&expr.details.type, "`if` statement");
expect_and_consume(&tokvec, RIGHT_PAREN, "right parenthesis of `if`");
struct Statement *ptr_inner_s = calloc(1, sizeof(struct Statement));
*ptr_inner_s = parse_statement(ptr_ps, &tokvec);
struct Statement s;
s.labels = init_vector();
s.expr1 = expr;
if (tokvec[0].kind == RES_ELSE) { /* must bind to the most inner one */
++tokvec;
struct Statement *ptr_inner_s2 =
calloc(1, sizeof(struct Statement));
*ptr_inner_s2 = parse_statement(ptr_ps, &tokvec);
*ptr_tokvec = tokvec;
s.category = IF_ELSE_STATEMENT;
s.statement_vector = init_vector();
push_vector(&s.statement_vector, ptr_inner_s);
push_vector(&s.statement_vector, ptr_inner_s2);
return s;
}
*ptr_tokvec = tokvec;
s.category = IF_STATEMENT;
s.inner_statement = ptr_inner_s;
return s;
}
case RES_SWITCH: {
++tokvec;
expect_and_consume(&tokvec, LEFT_PAREN,
"left parenthesis immediately after `switch`");
const struct UntypedExpr u = parse_expression(&tokvec);
struct Expr expr = typecheck_expression(ptr_ps, &u);
expect_and_consume(&tokvec, RIGHT_PAREN,
"right parenthesis of `switch`");
expect_integral(
&expr.details.type,
"controlling expression of `switch` must be integer type");
struct Statement *ptr_inner_s = calloc(1, sizeof(struct Statement));
*ptr_inner_s = parse_statement(ptr_ps, &tokvec);
struct Statement s;
s.labels = init_vector();
s.expr1 = expr;
*ptr_tokvec = tokvec;
s.category = SWITCH_STATEMENT;
s.inner_statement = ptr_inner_s;
return s;
}
case RES_RETURN: {
++tokvec;
*ptr_tokvec = tokvec;
struct Statement s;
s.labels = init_vector();
s.category = RETURN_STATEMENT;
if (tokvec[0].kind == SEMICOLON) {
++tokvec;
s.expr1.category = VOID_EXPR;
s.expr1.details.type.type_category = VOID_;
s.expr1.details.true_type.type_category = VOID_;
} else {
const struct UntypedExpr u = parse_expression(&tokvec);
struct Expr expr = typecheck_expression(ptr_ps, &u);
if (ptr_ps->func_ret_type.type_category == PTR_) {
struct TypePair details;
details.type = ptr_ps->func_ret_type;
details.true_type = ptr_ps->func_ret_type;
cast_to_null_pointer_if_possible(&expr, &details);
if_function_cast_to_pointer(&expr);
}
expect_type(ptr_ps, &expr.details.type, &ptr_ps->func_ret_type,
"mismatched type in the return value");
expect_and_consume(&tokvec, SEMICOLON,
"semicolon (after `return expr`)");
s.expr1 = expr;
}
*ptr_tokvec = tokvec;
return s;
}
case RES_DO: {
++tokvec;
struct Statement inner_s = parse_statement(ptr_ps, &tokvec);
expect_and_consume(&tokvec, RES_WHILE, "`while` of do-while");
expect_and_consume(&tokvec, LEFT_PAREN, "left parenthesis of do-while");
const struct UntypedExpr u = parse_expression(&tokvec);
struct Expr expr = typecheck_expression(ptr_ps, &u);
expect_scalar(&expr.details.type, "`do-while` statement");
expect_and_consume(&tokvec, RIGHT_PAREN,
"right parenthesis of do-while");
expect_and_consume(&tokvec, SEMICOLON, "semicolon after do-while");
*ptr_tokvec = tokvec;
struct Statement s;
s.labels = init_vector();
s.category = DO_WHILE_STATEMENT;
s.expr1 = expr;
struct Statement *ptr_inner_s = calloc(1, sizeof(struct Statement));
*ptr_inner_s = inner_s;
s.inner_statement = ptr_inner_s;
return s;
}
case RES_WHILE: {
++tokvec;
expect_and_consume(&tokvec, LEFT_PAREN, "left parenthesis of while");
const struct UntypedExpr u = parse_expression(&tokvec);
struct Expr expr = typecheck_expression(ptr_ps, &u);
expect_scalar(&expr.details.type, "`while` statement");
expect_and_consume(&tokvec, RIGHT_PAREN, "left parenthesis of while");
struct Statement inner_s = parse_statement(ptr_ps, &tokvec);
*ptr_tokvec = tokvec;
struct Statement s;
s.labels = init_vector();
s.category = WHILE_STATEMENT;
s.expr1 = expr;
struct Statement *ptr_inner_s = calloc(1, sizeof(struct Statement));
*ptr_inner_s = inner_s;
s.inner_statement = ptr_inner_s;
return s;
}
case RES_BREAK: {
++tokvec;
expect_and_consume(&tokvec, SEMICOLON, "semicolon after `break`");
*ptr_tokvec = tokvec;
struct Statement s;
s.labels = init_vector();
s.category = BREAK_STATEMENT;
return s;
}
case RES_CONTINUE: {
++tokvec;
expect_and_consume(&tokvec, SEMICOLON, "semicolon after `continue`");
*ptr_tokvec = tokvec;
struct Statement s;
s.labels = init_vector();
s.category = CONTINUE_STATEMENT;
return s;
}
case RES_GOTO: {
++tokvec;
expect_and_consume(&tokvec, IDENT_OR_RESERVED,
"identifier after `goto`");
const char *ident = tokvec[-1].ident_str;
expect_and_consume(&tokvec, SEMICOLON,
"semicolon after `goto identifier`");
*ptr_tokvec = tokvec;
struct Statement s;
s.labels = init_vector();
s.category = GOTO_STATEMENT;
s.destination = ident;
return s;
}
case RES_FOR: {
++tokvec;
expect_and_consume(&tokvec, LEFT_PAREN, "left parenthesis of `for`");
/* make the whole thing into a block*/
struct Statement statement;
statement.category = COMPOUND_STATEMENT;
statement.labels = init_vector();
statement.statement_vector = init_vector();
/* block scope begins */
struct ScopeChain current_table = ptr_ps->scope_chain;
struct ScopeChain new_table;
new_table.var_table = init_map();
new_table.outer = ¤t_table;
ptr_ps->scope_chain = new_table;
struct Expr expr1;
struct Expr expr2;
struct Expr expr3;
if (tokvec[0].kind == SEMICOLON) { /* expression1 is missing */
expr1 = integer_1();
++tokvec;
} else if (can_start_a_type(tokvec)) {
const char *str;
struct UntypedExpr *ptr_uexpr;
const struct Type vartype =
parse_declaration(&tokvec, &str, &ptr_uexpr);
struct Expr *ptr_expr = declare_var_and_return_initializer(
ptr_ps, &vartype, str, ptr_uexpr, &statement);
expr1 = ptr_expr ? *ptr_expr : integer_1();
} else {
const struct UntypedExpr u = parse_expression(&tokvec);
expr1 = typecheck_expression(ptr_ps, &u);
expect_and_consume(&tokvec, SEMICOLON, "first semicolon of `for`");
}
if (tokvec[0].kind == SEMICOLON) { /* expression2 is missing */
expr2 = integer_1();
} else {
const struct UntypedExpr u = parse_expression(&tokvec);
expr2 = typecheck_expression(ptr_ps, &u);
}
expect_scalar(&expr2.details.type, "`for` statement");
expect_and_consume(&tokvec, SEMICOLON, "second semicolon of `for`");
if (tokvec[0].kind == RIGHT_PAREN) { /* expression3 is missing */
expr3 = integer_1();
} else {
const struct UntypedExpr u = parse_expression(&tokvec);
expr3 = typecheck_expression(ptr_ps, &u);
}
expect_and_consume(&tokvec, RIGHT_PAREN, "right parenthesis of `for`");
struct Statement s;
s.labels = init_vector();
s.category = FOR_STATEMENT;
s.expr1 = expr1;
s.expr2 = expr2;
s.expr3 = expr3;
struct Statement inner_s = parse_statement(ptr_ps, &tokvec);
*ptr_tokvec = tokvec;
struct Statement *ptr_inner_s = calloc(1, sizeof(struct Statement));
*ptr_inner_s = inner_s;
s.inner_statement = ptr_inner_s;
struct Statement *ptr_s = calloc(1, sizeof(struct Statement));
*ptr_s = s;
push_vector(&statement.statement_vector, ptr_s);
/* block scope ends */
ptr_ps->scope_chain = current_table;
return statement;
}
default: {
const struct UntypedExpr uexpr = parse_expression(&tokvec);
struct Expr expr = typecheck_expression(ptr_ps, &uexpr);
expect_and_consume(&tokvec, SEMICOLON, "semicolon after an expression");
*ptr_tokvec = tokvec;
struct Statement s;
s.labels = init_vector();
s.category = EXPRESSION_STATEMENT;
s.expr1 = expr;
return s;
}
}
}
static struct Expr *declare_var_and_return_initializer(
struct AnalyzerState *ptr_ps, const struct Type *ref_vartype,
const char *str, const struct UntypedExpr *ptr_uexpr,
struct Statement *ptr_statement)
{
const struct Type vartype = *ref_vartype;
add_local_var_to_scope(ptr_ps, ref_vartype, str);
struct Statement s;
s.category = DECLARATION_STATEMENT;
s.declaration.type = vartype;
s.declaration.ident_str = str;
s.labels = init_vector();
struct Statement *ptr_s = calloc(1, sizeof(struct Statement));
*ptr_s = s;
push_vector(&ptr_statement->statement_vector, ptr_s);
struct Expr *ptr_expr = 0;
if (ptr_uexpr) { /* initializer exists */
struct UntypedExpr left_uexpr;
left_uexpr.category = VAR;
left_uexpr.var_name = str;
struct UntypedExpr uexpr =
binary_op_untyped(&left_uexpr, ptr_uexpr, OP_EQ);
struct Expr expr = typecheck_expression(ptr_ps, &uexpr);
ptr_expr = calloc(1, sizeof(struct Expr));
*ptr_expr = expr;
}
return ptr_expr;
}
struct Statement parse_compound_statement(struct AnalyzerState *ptr_ps,
const struct Token **ptr_tokvec)
{
const struct Token *tokvec = *ptr_tokvec;
struct Statement statement;
statement.category = COMPOUND_STATEMENT;
statement.labels = init_vector();
statement.statement_vector = init_vector();
if (tokvec[0].kind == LEFT_BRACE) {
struct ScopeChain current_table = ptr_ps->scope_chain;
struct ScopeChain new_table;
new_table.var_table = init_map();
/* current_table disappears at the end of this function,
but there is no problem because new_table itself gets overwritten at
the end of this function.
*/
new_table.outer = ¤t_table;
ptr_ps->scope_chain = new_table;
++tokvec;
*ptr_tokvec = tokvec;
while (1) {
if (tokvec[0].kind == RIGHT_BRACE) {
++tokvec;
*ptr_tokvec = tokvec;
ptr_ps->scope_chain = current_table;
return statement;
} else if (can_start_a_type(tokvec)) {
struct Type *optional_ptr_type =
try_parse_type_specifier_and_semicolon(&tokvec);
if (optional_ptr_type) {
struct Statement s;
s.category = DECLARATION_STATEMENT;
s.declaration.type = *optional_ptr_type;
s.declaration.ident_str = 0;
s.labels = init_vector(); /* must initialize this, since it
is used in goto traversal */
struct Statement *ptr_s =
calloc(1, sizeof(struct Statement));
*ptr_s = s;
push_vector(&statement.statement_vector, ptr_s);
continue;
}
const char *str;
struct UntypedExpr *ptr_uexpr;
const struct Type vartype =
parse_declaration(&tokvec, &str, &ptr_uexpr);
/* while function prototypes are also allowed here in C, I will
* not implement it here */
if (vartype.type_category == FN) {
unsupported(
"declaring a function inside a compound statement\n");
exit(EXIT_FAILURE);
}
struct Expr *ptr_expr = declare_var_and_return_initializer(
ptr_ps, &vartype, str, ptr_uexpr, &statement);
if (ptr_expr) {
struct Statement assignment;
assignment.category = EXPRESSION_STATEMENT;
assignment.expr1 = *ptr_expr;
assignment.labels = init_vector();
struct Statement *ptr_s =
calloc(1, sizeof(struct Statement));
*ptr_s = assignment;
push_vector(&statement.statement_vector, ptr_s);
}
} else {
struct Statement s = parse_statement(ptr_ps, &tokvec);
struct Statement *ptr_s = calloc(1, sizeof(struct Statement));
*ptr_s = s;
push_vector(&statement.statement_vector, ptr_s);
}
}
} else {
fprintf(stderr,
"****************************\n"
"* INTERNAL COMPILER ERROR @ %s\n"
"* Unexpected value: `tokvec[0].kind` != `LEFT_BRACE` (which "
"is %d), and is instead %d\n\n"
"****************************\n",
__func__, LEFT_BRACE, tokvec[0].kind);
exit(EXIT_FAILURE);
}
}