-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.c
344 lines (308 loc) · 7.8 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
#include "shucc.h"
/* global variable used inside parse.c */
Func* fn; // function now being parsed
/*
* Create a non-leaf node.
* @param kind node kind
* @param lhs lhs of the node
* @param rhs rhs of the node
*/
Node* new_node(NodeKind kind, Node* lhs, Node* rhs) {
Node* node = calloc(1, sizeof(Node));
node->kind = kind;
node->lhs = lhs;
node->rhs = rhs;
return node;
}
/*
* Create a leaf node.
* @param val an integer
*/
Node* new_node_num(int val) {
Node* node = calloc(1, sizeof(Node));
node->kind = ND_NUM;
node->val = val;
node->type = new_ty_int();
return node;
}
/*
* Check if there is a local variable whose name is tok.
* @param tok token to be checked
*/
Lvar* find_lvar(Token* tok) { return map_at(fn->lvars, tok->str); }
/**
* Read type.
* @return read type
*/
Type* read_type() {
Type* type;
if (consume("int")) {
type = new_ty_int();
} else {
error("No such type");
}
while (consume("*")) {
type = new_ty_ptr(type);
}
return type;
}
/**
* Create a new Type instance
* @param kind type kind of the new instance
* @param size type size of the new instance (e.g. 4 if int)
* @return created instance
*/
Type* new_ty(TypeKind kind, int size) {
Type* type = calloc(1, sizeof(Type)); // calloc of Type is done only in this function
type->kind = kind;
type->type_size = size;
return type;
}
/**
* Create a new int type instance
* @return created int instance
*/
Type* new_ty_int() {
return new_ty(TY_INT, 8); // TODO: 4
}
/**
* Create a new pointer type instance
* @param dest destination of the pointer (e.g. int if int*)
* @return created pointer instance
*/
Type* new_ty_ptr(Type* dest) {
Type* type = new_ty(TY_PTR, 8);
type->ptr_to = dest;
return type;
}
/* grammatical functions */
Node* declaration() {
Type* type = read_type();
Lvar* lvar = calloc(1, sizeof(Lvar));
Token* tok = consume_ident();
lvar->name = tok->str;
lvar->len = tok->len;
lvar->type = type;
Node* node = calloc(1, sizeof(Node));
node->kind = ND_LVAR;
node->lvar = lvar;
node->type = type;
map_insert(fn->lvars, tok->str, lvar);
return node;
}
Map* program() {
// fprintf(stderr, "hello from program\n");
code = map_create();
while (token->kind != TK_EOF) {
func(); // inside func tokens are updated
}
return code;
}
void func() {
// fprintf(stderr, "hello from func\n");
Type* type = read_type();
Token* tok = consume_ident();
if (!tok) {
error("Unexpected token");
}
fn = calloc(1, sizeof(Func));
fn->name = tok->str;
fn->return_type = type;
fn->lvars = map_create();
expect("(");
// read arguments
fn->args = vec_create();
while (!consume(")")) {
if (0 < fn->args->size) {
expect(",");
}
vec_push(fn->args, declaration());
}
map_insert(code, fn->name, fn);
expect("{");
// read body
Node* node = calloc(1, sizeof(Node));
node->kind = ND_BLOCK;
node->stmts = vec_create();
while (!consume("}")) {
vec_push(node->stmts, (void*)stmt());
}
fn->body = node;
}
Node* stmt() {
Node* node;
if (peek("int")) {
node = declaration();
expect(";");
} else if (consume("{")) {
node = calloc(1, sizeof(Node));
node->kind = ND_BLOCK;
node->stmts = vec_create(); // initialize stmts
while (!consume("}")) {
vec_push(node->stmts, (void*)stmt()); // push stmt to stmts
}
} else if (consume("return")) {
node = calloc(1, sizeof(Node));
node->kind = ND_RETURN;
node->lhs = expr();
if (!consume(";")) {
error("';' is expected");
}
} else if (consume("if")) {
node = calloc(1, sizeof(Node));
node->kind = ND_IF;
expect("(");
node->cond = expr();
expect(")");
node->then = stmt();
if (consume("else")) {
node->els = stmt();
}
} else if (consume("while")) {
node = calloc(1, sizeof(Node));
node->kind = ND_WHILE;
expect("(");
node->cond = expr();
expect(")");
node->then = stmt();
} else if (consume("for")) {
node = calloc(1, sizeof(Node));
node->kind = ND_FOR;
expect("(");
node->init = expr();
expect(";");
node->cond = expr();
expect(";");
node->loop = expr();
expect(")");
node->then = stmt();
} else {
node = expr();
if (!consume(";")) {
error("';' is expected");
}
}
return node;
}
Node* expr() {
Node* node = assign();
return node;
}
Node* assign() {
Node* node = equality();
if (consume("=")) {
node = new_node(ND_ASSIGN, node, assign());
}
return node;
}
Node* equality() {
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;
}
}
}
Node* relational() {
Node* node = add();
for (;;) {
if (consume("<=")) {
node = new_node(ND_LE, node, add());
} else if (consume(">=")) {
node = new_node(ND_LE, add(), node); // ND_GE
} else if (consume("<")) {
node = new_node(ND_LT, node, add());
} else if (consume(">")) {
node = new_node(ND_LT, add(), node); // ND_GT
} else {
return node;
}
}
}
Node* add() {
Node* node = mul();
for (;;) {
if (consume("+")) {
node = new_node(ND_ADD, node, mul());
} else if (consume("-")) {
node = new_node(ND_SUB, node, mul());
} else {
return node;
}
}
}
Node* mul() {
Node* node = unary();
for (;;) {
if (consume("*")) {
node = new_node(ND_MUL, node, unary());
} else if (consume("/")) {
node = new_node(ND_DIV, node, unary());
} else {
return node;
}
}
}
Node* unary() {
if (consume("+")) {
return primary();
}
if (consume("-")) {
return new_node(ND_SUB, new_node_num(0), primary());
}
if (consume("&")) {
return new_node(ND_ADDR, unary(), NULL);
}
if (consume("*")) {
return new_node(ND_DEREF, unary(), NULL);
}
if (consume("sizeof")) {
return new_node(ND_SIZEOF, unary(), NULL);
}
return primary();
}
Node* primary() {
Node* node;
// case primary = "(" expr ")"
if (consume("(")) {
node = expr();
expect(")");
return node;
}
// case primary = ident
Token* tok = consume_ident();
if (tok) {
node = calloc(1, sizeof(Node));
if (consume("(")) { // function
Func* fn = map_at(code, tok->str);
if (!fn) {
error("function '%s' is not defined", tok->str);
}
node->kind = ND_FUNC_CALL;
node->func = fn;
node->func_name = tok->str;
node->args = vec_create();
while (!consume(")")) {
if (0 < node->args->size) {
expect(",");
}
Node* arg = expr();
vec_push(node->args, arg);
}
} else { // local variable
node->kind = ND_LVAR;
Lvar* lvar = find_lvar(tok);
if (!lvar) { // if this is the first time for the lvar to appear
error("lvar not defined");
}
node->lvar = lvar;
}
return node;
}
// case primary = num
return new_node_num(expect_number());
}