-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparse.c
332 lines (281 loc) · 8.82 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
// This file contains a recursive descent parser for C.
//
// Most functions in this file are named after the symbols they are
// supposed to read from an input token list. For example, stmt() is
// responsible for reading a statement from a token list. The function
// then construct an AST node representing a statement.
//
// Each function conceptually returns two values, an AST node and
// remaining part of the input tokens. Since C doesn't support
// multiple return values, the remaining tokens are returned to the
// caller via a pointer argument.
//
// Input tokens are represented by a linked list. Unlike many recursive
// descent parsers, we don't have the notion of the "input token stream".
// Most parsing functions don't change the global state of the parser.
// So it is very easy to lookahead arbitrary number of tokens in this
// parser.
#include "jcc.h"
/* All local variable instances created during parsing are
* accumulated to this list
*/
Obj *locals;
int debug_count = 0;
static Node *compund_stmt(Token **rest, Token *tok);
static Node *stmt(Token **rest, Token *tok);
static Node *expr_stmt(Token **rest, Token *tok);
static Node *expr(Token **rest, Token *tok);
static Node *assign(Token **rest, Token *tok);
static Node *equality(Token **rest, Token *tok);
static Node *relational(Token **rest, Token *tok);
static Node *add(Token **rest, Token *tok);
static Node *mul(Token **rest, Token *tok);
static Node *unary(Token **rest, Token *tok);
static Node *primary(Token **rest, Token *tok);
// Find a local varible by name
static Obj *find_var(Token *tok) {
for(Obj *var = locals; var; var = var->next) {
if (strlen(var->name) == tok->len && !strncmp(var->name, tok->loc, tok->len))
return var;
}
return NULL;
}
static Node *new_node(NodeKind kind, Token *tok) {
Node *node = calloc(1, sizeof(Node));
node->kind = kind;
node->tok = tok;
return node;
}
static Node *new_binary(NodeKind kind, Node *lhs, Node *rhs, Token *tok) {
Node *node = new_node(kind, tok);
node->lhs = lhs;
node->rhs = rhs;
return node;
}
static Node *new_unary(NodeKind kind, Node *expr, Token *tok) {
Node *node = new_node(kind, tok);
node->lhs = expr;
return node;
}
static Node *new_num(int val, Token *tok) {
Node *node = new_node(ND_NUM, tok);
node->val = val;
return node;
}
static Node *new_var_node(Obj *var, Token *tok) {
Node *node = new_node(ND_VAR, tok);
node->var = var;
return node;
}
static Obj *new_lvar(char *name) {
Obj *var = calloc(1, sizeof(Obj));
var->name = name;
var->next = locals;
locals = var;
return var;
}
//compund_stmt = stmt* "}"
static Node *compund_stmt(Token **rest, Token *tok) {
Node head = {};
Node *cur = &head;
while(!equal(tok, "}")) {
cur = cur->next=stmt(&tok, tok);
debug_count += 1;
printf("debug_count = %d\n", debug_count);
}
Node *node = new_node(ND_BLOCK, tok);
node->body = head.next;
*rest = tok->next;
return node;
}
// stmt = 'return' expr ";"
// | "if" "(" expr ")" stmt ("else" stmt) ?
// | "for" "(" expr-stmt expr? ";" expr? ")" stmt
// | "while" "(" expr ")" stmt
// | compund_stmt
// | expr-stmt
static Node *stmt(Token **rest, Token *tok) {
if (equal(tok, "return")) {
Node *node = new_unary(ND_RETURN, expr(&tok, tok->next), tok);
*rest = skip(tok, ";");
return node;
}
if (equal(tok, "if")) {
Node *node = new_node(ND_IF, tok);
tok = skip(tok->next, "(");
node->cond = expr(&tok, tok);
tok = skip(tok, ")");
node->then = stmt(&tok, tok);
if (equal(tok, "else"))
node->els = stmt(&tok, tok->next);
*rest = tok;
return node;
}
if (equal(tok, "for")) {
Node *node = new_node(ND_FOR, tok);
tok = skip(tok->next, "(");
node->init = expr_stmt(&tok, tok);
if(!equal(tok, ";"))
node->cond = expr(&tok, tok);
tok = skip(tok, ";");
if(!equal(tok, ")"))
node->inc = expr(&tok, tok);
tok = skip(tok, ")");
node->then = stmt(rest, tok);
return node;
}
if (equal(tok, "while")) {
Node *node = new_node(ND_FOR, tok);
tok = skip(tok->next, "(");
node->cond = expr(&tok, tok);
tok = skip(tok, ")");
node->then = stmt(rest, tok);
return node;
}
if (equal(tok, "{"))
return compund_stmt(rest, tok->next);
return expr_stmt(rest, tok);
}
// expr-stmt = expr ? ";"
static Node *expr_stmt(Token **rest, Token *tok) {
if (equal(tok, ";")) {
*rest = tok->next;
return new_node(ND_BLOCK, tok);
}
Node *node = new_node(ND_EXPR_STMT, tok);
node->lhs = expr(&tok, tok);
*rest = skip(tok, ";");
return node;
}
// expr = assign
static Node *expr(Token **rest, Token *tok) {
return assign(rest, tok);
}
// assign = equality ("=" assign)?
static Node *assign(Token **rest, Token *tok){
Node *node = equality(&tok, tok);
if (equal(tok, "="))
node = new_binary(ND_ASSIGN, node, assign(&tok, tok->next), tok);
*rest = tok;
return node;
}
// equality = relational ("==" relational | "!=" relational)*
static Node *equality(Token **rest, Token *tok){
Node *node = relational(&tok, tok);
for(;;) {
Token *start = tok;
if (equal(tok, "==")) {
node = new_binary(ND_EQ, node, relational(&tok, tok->next), start);
continue;
}
if (equal(tok, "!=")) {
node = new_binary(ND_NE, node, relational(&tok, tok->next), start);
continue;
}
*rest = tok;
return node;
}
}
// relational = add ("<" add | "<=" add | ">" add | ">=" add)*
static Node *relational(Token **rest, Token *tok){
Node *node = add(&tok, tok);
for(;;) {
Token *start = tok;
if (equal(tok, "<")) {
node = new_binary(ND_LT, node, add(&tok, tok->next), start);
continue;
}
if (equal(tok, "<=")) {
node = new_binary(ND_LE, node, add(&tok, tok->next), start);
continue;
}
if (equal(tok, ">")) {
node = new_binary(ND_LT, add(&tok, tok->next), node, start);
continue;
}
if (equal(tok, ">=")) {
node = new_binary(ND_LE, add(&tok, tok->next), node, start);
continue;
}
*rest = tok;
return node;
}
}
// expr = mul ("+" mul | "-" mul)*
static Node *add(Token **rest, Token *tok) {
Node *node = mul(&tok, tok);
for (;;) {
Token *start = tok;
if (equal(tok, "+")) {
node = new_binary(ND_ADD, node, mul(&tok, tok->next), start);
continue;
}
if (equal(tok, "-")) {
node = new_binary(ND_SUB, node, mul(&tok, tok->next), start);
continue;
}
*rest = tok;
return node;
}
}
// mul = unary ("*" unary | "/" unary)*
static Node *mul(Token **rest, Token *tok) {
Node *node = unary(&tok, tok);
for (;;) {
if (equal(tok, "*")) {
node = new_binary(ND_MUL, node, unary(&tok, tok->next), tok);
continue;
}
if (equal(tok, "/")) {
node = new_binary(ND_DIV, node, unary(&tok, tok->next), tok);
continue;
}
*rest = tok;
return node;
}
}
// unary = ("+" | "-") unary
// | primary
static Node *unary(Token **rest, Token *tok) {
// move next after "+"
if (equal(tok, "+"))
return unary(rest, tok->next);
if (equal(tok, "-"))
return new_unary(ND_NEG, unary(rest, tok->next), tok);
if (equal(tok, "&"))
return new_unary(ND_ADDR, unary(rest, tok->next), tok);
if (equal(tok, "*"))
return new_unary(ND_DEREF, unary(rest, tok->next), tok);
return primary(rest, tok);
}
// primary = "(" expr ")" | num | ident
static Node *primary(Token **rest, Token *tok) {
if (equal(tok, "(")) {
Node *node = expr(&tok, tok->next);
*rest = skip(tok, ")");
return node;
}
if (tok->kind == TK_NUM) {
Node *node = new_num(tok->val, tok);
*rest = tok->next;
return node;
}
if (tok->kind == TK_IDENT) {
// if the first define a variable var is NULL
// if not the first then use the last info included :'name、offset'
Obj *var = find_var(tok);
if (!var)
var = new_lvar(strndup(tok->loc, tok->len));
*rest = tok->next;
return new_var_node(var, tok);
}
error_tok(tok, "expected an expression");
}
// program = compound_stmt *
Fuction *parse(Token *tok) {
tok = skip(tok, "{");
Fuction *prog = calloc(1, sizeof(Fuction));
prog->body = compund_stmt(&tok, tok);
prog->locals = locals;
return prog;
}