-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathast.c
327 lines (285 loc) · 6.64 KB
/
ast.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
#include <ctype.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "9cc.h"
typedef struct LVar LVar;
// local variable.
struct LVar {
LVar *next;
char *name;
int len;
int offset;
};
// ローカル変数を連結リスト
LVar *locals = NULL;
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;
}
Node *new_node_num(int val) {
Node *node = calloc(1, sizeof(Node));
node->kind = ND_NUM;
node->val = val;
return node;
}
/*
int memcmp( const void *str1 , const void *str2, size_t len );
if str1 == str2 (len characters) -> 0
else if str1 > str2 -> positive integer
else if str1 < str2 -> postive integer
*/
// 変数を名前で検索する見つからなかった場合はNULLを返す。
LVar *find_lvar(Token *tok) {
for (LVar *var = locals; var; var = var->next) {
if (var->len == tok->len && !memcmp(tok->str, var->name, var->len))
return var;
}
return NULL;
}
Token* consume_ident() {
if (token->kind != TK_IDENT)
return NULL;
Token* tmp = token;
token = token->next;
return tmp;
}
void function() {
Token *tok;
//local初期化
LVar *l = calloc(1, sizeof(LVar));
locals = l;
int i = 0;
while (!at_eof()) {
tok = consume_ident();
if (!tok) {
error_at(token[pos].str,"関数ではありません");
}
Node *node = calloc(1, sizeof(Node));
node->kind = ND_FUNCTIONDECLARATION;
node->name = tok->str;
node->len = tok->len;
code[i++] = node;
//引数
expect("(");
int arg_len = 0;
while(!consume(")")) {
tok = consume_ident();
if (!tok) {
error_at(token[pos].str,"引数にふさわしくありません");
}
consume(",");
LVar *lvar = calloc(1, sizeof(LVar));
//ローカル変数の連結リストに接続
lvar->next = locals;
lvar->name = tok->str;
lvar->len = tok->len;
lvar->offset = locals->offset + 8;
node->arg_offset[arg_len] = lvar->offset;
locals = lvar;
arg_len+=1;
}
//引数の個数
node->arg_len = arg_len;
expect("{");
//ボディ
int j = 0;
while (!consume("}")) {
node->block[j++] = stmt();
}
node->block_len = j;
}
code[i] = NULL;
}
Node *stmt() {
Node *node;
if (consume_kind(TK_IF)) {
// if (A) B else C
node = calloc(1, sizeof(Node));
expect("(");
node->lhs = expr();
expect(")");
Node *child;
child= calloc(1, sizeof(Node));
child->lhs = stmt();
if (consume_kind(TK_ELSE)) {
node->kind = ND_IFELSE;
child->rhs = stmt();
} else {
node->kind = ND_IF;
}
node->rhs = child;
} else if (consume_kind(TK_WHILE)) {
/*
while (A) B
*/
node = calloc(1, sizeof(Node));
node->kind=ND_WHILE;
expect("(");
node->lhs = expr(); // A
expect(")");
node->rhs = stmt(); // B
} else if (consume_kind(TK_FOR)) {
/*
for (A; B; C)
D;
*/
Node *child, *grandchild;
node = calloc(1, sizeof(Node));
child= calloc(1, sizeof(Node));
grandchild =calloc (1, sizeof(Node));
//set kind
node->kind = ND_FOR;
// construct tree
node->rhs = child;
child->rhs = grandchild;
expect("(");
node->lhs = stmt(); //A
child->lhs = stmt();//B
grandchild->lhs = expr();//C
expect(")");
grandchild->rhs = stmt();//D
} else if (consume_kind(TK_RETURN)) {
node = calloc(1, sizeof(Node));
node->kind = ND_RETURN;
node->lhs = stmt();
} else if (consume("{")) {
node = calloc(1, sizeof(Node));
for (int i = 0; !consume("}") && i < 100; ++i) {
Node* tmp_node;
tmp_node = stmt();
node->block[i] = tmp_node;
node->block_len = i+1;
}
node->kind = ND_BLOCK;
} else {
node = expr();
if (!consume(";"))
error_at(token[pos].str,"';' ではないトークンです");
}
return node;
}
Node *expr() {
return assign();
}
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_LT, node, add());
else if (consume("<="))
node = new_node(ND_LE, node, add());
else if (consume(">"))
node = new_node(ND_LT, add(), node);
else if (consume(">="))
node = new_node(ND_LE, add(), node);
else
return node;
}
}
Node *add() {
Node *node = mul();
for (;;) {
if (consume("+"))
node = new_node(ND_ADD, node, mul());
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 term();
if (consume("-"))
return new_node(ND_SUB, new_node_num(0), term());
return term();
}
Node *term() {
Node *node;
if (consume("(")) {
node = expr();
expect(")");
return node;
}
Token *tok = consume_ident();
if (tok) {
if (consume("(")) {
//関数呼び出し
node = calloc(1, sizeof(Node));
node->kind = ND_FUNCTIONCALL;
node->name = tok->str;
node->len = tok->len;
node->arg_len = 0;
for (int i = 0; !consume(")"); i++) {
if (i > 5) {
error_at(token[pos].str,"引数は6個以下です。");
}
Node* tmp = calloc(1, sizeof(Node));
tmp = expr();
node->arg[i] = tmp;
node->arg_len = i+1;
consume(",");
}
return node;
} else {
//変数解決
node = calloc(1, sizeof(Node));
node->kind = ND_LVAR;
if (locals == NULL) {
LVar *l = calloc(1, sizeof(LVar));
locals = l;
}
LVar *lvar = find_lvar(tok);
if (lvar) {
//登録済みのローカル変数
node->offset = lvar->offset;
} else {
//未登録のローカル変数
lvar = calloc(1, sizeof(LVar));
//ローカル変数の連結リストに接続
lvar->next = locals;
lvar->name = tok->str;
lvar->len = tok->len;
lvar->offset = locals->offset + 8;
node->offset = lvar->offset;
locals = lvar;
}
}
return node;
}
return new_node_num(expect_number());
}