-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.c
280 lines (247 loc) · 5.43 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
#include "chibicc.h"
Var *locals;
// Find a local variable by name.
Var *find_var(Token *tok) {
for (Var *var = locals; var; var = var->next)
if (strlen(var->name) == tok->len &&
!strncmp(tok->str, var->name, tok->len))
return var;
return NULL;
}
Node *new_node(NodeKind kind) {
Node *node = calloc(1, sizeof(Node));
node->kind = kind;
return node;
}
Node *new_unary(NodeKind kind, Node *expr) {
Node *node = new_node(kind);
node->lhs = expr;
return node;
}
Node *new_binary(NodeKind kind, Node *lhs, Node *rhs) {
Node *node = new_node(kind);
node->lhs = lhs;
node->rhs = rhs;
return node;
}
Node *new_num(int val) {
Node *node = new_node(ND_NUM);
node->val = val;
return node;
}
Node *new_var(Var *var) {
Node *node = new_node(ND_VAR);
node->var = var;
return node;
}
Var *push_var(char *name) {
Var *var = calloc(1, sizeof(Var));
var->next = locals;
var->name = name;
locals = var;
return var;
}
Function *function();
Node *stmt();
Node *expr();
Node *assign();
Node *eq();
Node *cmp();
Node *add();
Node *mul();
Node *unary();
Node *primary();
Node *func_args();
// program = function*
Function *program() {
Function head;
head.next = NULL;
Function *cur = &head;
while (!at_eof()) {
cur->next = function();
cur = cur->next;
}
return head.next;
}
// function = id "(" ")" "{" stmt* "}"
Function *function() {
locals = NULL;
char *name = expect_id();
expect("(");
expect(")");
expect("{");
Node head;
head.next = NULL;
Node *cur = &head;
while (!consume("}")) {
cur->next = stmt();
cur = cur->next;
}
Function *fn = calloc(1, sizeof(Function));
fn->name = name;
fn->node = head.next;
fn->locals = locals;
return fn;
}
Node *read_expr_stmt() { return new_unary(ND_STMT, expr()); }
// stmt = "if" "(" expr ")" stmt ("else" stmt)?
// | "while" "(" expr ")" stmt
// | "for" "(" expr? ";" expr? ";" expr? ")" stmt
// | "{" stmt* "}"
// | "return" expr ";"
// | expr ";"
Node *stmt() {
Node *node = NULL;
if (consume("if")) {
node = new_node(ND_IF);
} else if (consume("while")) {
node = new_node(ND_WHILE);
} else if (consume("for")) {
node = new_node(ND_FOR);
}
if (node) {
expect("(");
if (node->kind == ND_FOR) {
if (!consume(";")) {
node->init = read_expr_stmt();
expect(";");
}
if (!consume(";")) {
node->cond = expr();
expect(";");
}
if (!consume(")")) {
node->inc = read_expr_stmt();
expect(")");
}
} else {
node->cond = expr();
expect(")");
}
node->then = stmt();
if (node->kind == ND_IF && consume("else"))
node->els = stmt();
} else if (consume("{")) {
node = new_node(ND_BLOCK);
Node head;
head.next = NULL;
Node *cur = &head;
while (!consume("}")) {
cur->next = stmt();
cur = cur->next;
}
node->body = head.next;
} else {
if (consume("return"))
node = new_unary(ND_RT, expr());
else
node = read_expr_stmt();
expect(";");
}
return node;
}
// expr = assign
Node *expr() { return assign(); }
// assign = eq ("=" assign)?
Node *assign() {
Node *node = eq();
if (consume("="))
node = new_binary(ND_ASSIGN, node, assign());
return node;
}
// eq = cmp ("==" cmp | "!=" cmp)*
Node *eq() {
Node *node = cmp();
while (true) {
if (consume("=="))
node = new_binary(ND_EQ, node, cmp());
else if (consume("!="))
node = new_binary(ND_NE, node, cmp());
else
return node;
}
}
// cmp = add ("<" add | "<=" add | ">" add | ">=" add)*
Node *cmp() {
Node *node = add();
while (true) {
if (consume("<"))
node = new_binary(ND_LT, node, add());
else if (consume("<="))
node = new_binary(ND_LE, node, add());
if (consume(">"))
node = new_binary(ND_LT, add(), node);
else if (consume(">="))
node = new_binary(ND_LE, add(), node);
else
return node;
}
}
// add = mul ("+" mul | "-" mul)*
Node *add() {
Node *node = mul();
while (true) {
if (consume("+"))
node = new_binary(ND_ADD, node, mul());
else if (consume("-"))
node = new_binary(ND_SUB, node, mul());
else
return node;
}
}
// mul = unary ("*" unary | "/" unary)*
Node *mul() {
Node *node = unary();
while (true) {
if (consume("*"))
node = new_binary(ND_MUL, node, unary());
else if (consume("/"))
node = new_binary(ND_DIV, node, unary());
else
return node;
}
}
// unary = ("+" | "-")? unary
// | primary
Node *unary() {
if (consume("+"))
return unary();
if (consume("-"))
return new_binary(ND_SUB, new_num(0), unary());
return primary();
}
// primary = "(" expr ")" | id func-args? | num
Node *primary() {
if (consume("(")) {
Node *node = expr();
expect(")");
return node;
}
Token *tok = consume_id();
if (tok) {
if (consume("(")) {
Node *node = new_node(ND_CALL);
node->funcname = strndup(tok->str, tok->len);
node->args = func_args();
return node;
}
Var *var = find_var(tok);
if (!var)
var = push_var(strndup(tok->str, tok->len));
return new_var(var);
}
return new_num(expect_number());
}
// func-args = "(" (assign ("," assign)*)? ")"
Node *func_args() {
if (consume(")"))
return NULL;
Node *head = assign();
Node *cur = head;
while (consume(",")) {
cur->next = assign();
cur = cur->next;
}
expect(")");
return head;
}