-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.c
More file actions
462 lines (398 loc) · 11.5 KB
/
parse.c
File metadata and controls
462 lines (398 loc) · 11.5 KB
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
#include "Temple_cc.h"
LVar *locals;
// 変数を名前で検索する。見つからなかった場合はNULLを返す。
static 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;
}
static Node *new_node(NodeKind kind, Node *lhs, Node *rhs) {
Node *node = calloc(1, sizeof(Node));
node->kind = kind;
node->lhs = lhs;
node->rhs = rhs;
node->tok = token;
return node;
}
static Node *new_node_num(int val) {
Node *node = calloc(1, sizeof(Node));
node->kind = ND_NUM;
node->val = val;
node->tok = token;
return node;
}
static Node *new_node_lvar(LVar *lvar, Token *tok) {
Node *node = calloc(1, sizeof(Node));
node->kind = ND_LVAR;
node->lvar = lvar;
node->tok = tok;
return node;
}
static int lvar_size(Type *ty) {
if (ty->kind != TY_ARRAY)
return 2;
return ty->array_size * lvar_size(ty->base);
}
static LVar *new_lvar(char *name, Type *ty) {
LVar *lvar = calloc(1, sizeof(LVar));
lvar->name = name;
lvar->ty = ty;
lvar->len = strlen(name);
lvar->lvar_size = lvar_size(ty);
lvar->next = locals;
locals = lvar;
return lvar;
}
static char *get_ident() {
if (token->kind != TK_IDENT)
error_at(token->str, "関数名が必要");
Token *tok = token;
token = token->next;
return strndup(tok->str, tok->len);
}
// decl_basictype = "int"
// 変数宣言のことも考慮して別の関数に振り分けた
static Type *decl_basictype() {
if (!consume_keyword("int"))
error_at(token->str, "型名が必要");
return new_type(TY_INT);
}
static Type *declarator(Type *ty);
Function *parse();
static Node *stmt();
static Node *expr();
static Node *assign();
static Node *equality();
static Node *relational();
static Node *add();
static Node *mul();
static Node *unary();
static Node *primary();
// type-suffix = ("(" func-params? ")")?
// func-params = param ("," param)*
// param = declarator
static Type *type_suffix(Type *ty) {
if (consume("(")) {
Type head = {};
Type *cur = &head;
while (!consume(")")) {
Type *basety = decl_basictype();
Type *ty = declarator(basety);
cur = cur->next = ty;
consume(",");
}
ty = func_type(ty);
ty->params = head.next;
}
return ty;
}
// declarator = decl_basictype "*" * ident type-suffix
static Type *declarator(Type *ty) { // 宣言
while (consume("*"))
ty = pointer_to(ty);
Token *tok_lval = token;
char* name = get_ident();
while (consume("[")) {
int array_size = expect_number();
ty = array_of(ty, array_size);
expect("]");
}
ty = type_suffix(ty); // 関数の宣言だった時の引数読み込み等
ty->name = name;
ty->tok = tok_lval;
return ty;
}
// declaration = decl_basictype (declarator ("=" expr)? ("," declarator ("=" expr)?)*)? ";"
static Node *declaration() {
Type *basety = decl_basictype();
Node head = {};
Node *cur = &head;
while (!consume(";")) {
Type *ty = declarator(basety);
LVar *lvar = new_lvar(ty->name, ty);
if (consume("=")) {
Node *lhs = new_node_lvar(lvar, ty->tok);
cur = cur->next = new_node(ND_ASSIGN, lhs, assign());
}
consume(",");
}
Node *node = new_node(ND_BLOCK, NULL, NULL);
node->body = head.next;
return node;
}
static void create_param_lvars(Type *param) {
if (param) {
create_param_lvars(param->next);
new_lvar(param->name, param);
}
}
static Function *function() {
Type *ty = decl_basictype();
ty = declarator(ty);
locals = NULL; // NULLのときが終端とするため
Function *fn = calloc(1, sizeof(Function));
fn->name = ty->name;
create_param_lvars(ty->params);
fn->params = locals;
if (!at_block())
error("中括弧で覆われていません。");
fn->body = stmt();
fn->locals = locals;
return fn;
}
// program = stmt*
Function *parse() {
Function head = {};
Function *cur = &head;
while (!at_eof())
cur = cur->next = function();
//error("正しくパースできませんでした。");
return head.next;
}
// stmt = expr? ";"
// | "{" stmt* "}"
// | "if" "(" expr ")" stmt ("else" stmt)?
// | "for" "(" expr? ";" expr? ";" expr? ")" stmt
// | "while" "(" expr ")" stmt
// | "return" expr ";"
static Node *stmt() {
Node *node;
if (consume("{")) {
Node head;
head.next = NULL;
Node *cur = &head;
while (!consume("}")) {
if (equal_keyword("int"))
cur = cur->next = declaration();
else
cur = cur->next = stmt();
add_type(cur);
}
node = new_node(ND_BLOCK, NULL, NULL);
node->body = head.next;
return node;
} else if (consume_keyword("if")) {
node = new_node(ND_IF, NULL, NULL);
expect("(");
node->cond = expr();
expect(")");
node->then = stmt();
if (consume_keyword("else"))
node->els = stmt();
return node;
} else if (consume_keyword("for")) {
node = new_node(ND_LOOP, NULL, NULL);
expect("(");
node->init = stmt(); // ";"の部分も含めてstmt
if (!consume(";")) {
node->cond = expr();
expect(";");
}
if (!consume(")")) {
node->inc = expr();
expect(")");
}
node->then = stmt();
return node;
} else if (consume_keyword("while")) {
node = new_node(ND_LOOP, NULL, NULL);
expect("(");
node->cond = expr();
expect(")");
node->then = stmt();
return node;
} else if (consume_keyword("return")) {
node = new_node(ND_RETURN, expr(), NULL);
} else if (consume(";")) { // ";"だけの文
return new_node(ND_BLOCK, NULL, NULL); // 空のブロック
} else {
node = expr();
}
expect(";");
return node;
}
// expr = assign
static Node *expr() {
return assign();
}
// assign = equality ("=" assign)?
static Node *assign() {
Node *node = equality();
if (consume("="))
node = new_node(ND_ASSIGN, node, assign());
return node;
}
// equality = relational ("==" relational | "!=" relational)*
static 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;
}
}
// relational = add ("<" add | "<=" add | ">" add | ">=" add)*
static 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;
}
}
// ポインタ演算の場合、pをポインタ、nを整数とすると
// p + nはpにnをそのまま加算するのではなく、
// sizeof(*p)*nをpの値に加算する。
static Node *new_add(Node *lhs, Node *rhs) {
add_type(lhs);
add_type(rhs);
// num + num
if (is_integer(lhs->ty) && is_integer(rhs->ty))
return new_node(ND_ADD, lhs, rhs);
// ptr + ptr は計算できない
if (lhs->ty->base && rhs->ty->base)
error_at(token->str, "無効な演算");
// num + ptr を ptr + num にひっくり返す.
if (!lhs->ty->base && rhs->ty->base) {
Node *tmp = lhs;
lhs = rhs;
rhs = tmp;
}
// ptr + num
use_func_MUL = true; // 現状シフト演算子を使わず掛け算として処理するため
rhs = new_node(ND_MUL, rhs, new_node_num(2)); // シフト演算子を定義したら変更する
return new_node(ND_ADD, lhs, rhs);
}
// -演算子にもポインタと整数に対しての演算がある
static Node *new_sub(Node *lhs, Node *rhs) {
add_type(lhs);
add_type(rhs);
// num - num
if (is_integer(lhs->ty) && is_integer(rhs->ty))
return new_node(ND_SUB, lhs, rhs);
// ptr - num
if (lhs->ty->base && is_integer(rhs->ty)) {
use_func_MUL = true; // 現状シフト演算子を使わず掛け算として処理するため
rhs = new_node(ND_MUL, rhs, new_node_num(2)); // シフト演算子を定義したら変更する
add_type(rhs);
Node *node = new_node(ND_SUB, lhs, rhs);
node->ty = lhs->ty;
return node;
}
// ptr - ptr
// 2つのポインタ間の要素数を返す
if (lhs->ty->base && rhs->ty->base) {
Node *node = new_node(ND_SUB, lhs, rhs);
node->ty = new_type(TY_INT);
use_func_DIV = true; // 現状シフト演算子を使わず割り算として処理するため
return new_node(ND_DIV, node, new_node_num(2)); // シフト演算子を定義したら変更する
}
error_at(token->str, "無効な演算");
return NULL;
}
// add = mul ("+" mul | "-" mul)*
static Node *add() {
Node *node = mul();
for (;;) {
if (consume("+"))
node = new_add(node, mul());
// ポインタ演算との場合分けなどの関数
else if (consume("-"))
node = new_sub(node, mul());
// ポインタ演算との場合分けなどの関数
else
return node;
}
}
// mul = unary ("*" unary | "/" unary)*
static Node *mul() {
Node *node = unary();
for (;;) {
if (consume("*")) {
use_func_MUL = true;
node = new_node(ND_MUL, node, unary());
} else if (consume("/")) {
use_func_DIV = true;
node = new_node(ND_DIV, node, unary());
} else
return node;
}
}
// unary = ("+" | "-")? primary
// | "*" unary
// | "&" unary
static Node *unary() {
if (consume("+"))
return primary();
if (consume("-"))
return new_node(ND_SUB, new_node_num(0), primary());
if (consume("*"))
return new_node(ND_DEREF,unary(), NULL);
if (consume("&"))
return new_node(ND_ADDR, unary(), NULL);
return primary();
}
// funcall = "(" (assign ("," assign)*)? ")"
static Node *funcall(Token *tok) {
Node head = {};
Node *cur = &head;
while (!consume(")")) {
cur = cur->next = assign();
consume(",");
}
Node *node = new_node(ND_FUNCALL, NULL, NULL);
node->funcname = strndup(tok->str, tok->len);
node->args = head.next;
return node;
}
// primary = num | ident | "(" expr ")" | "sizeof" unary
static Node *primary() {
// 次のトークンが"("なら、"(" expr ")"のはず
if (consume("(")) {
Node *node = expr();
expect(")");
return node;
}
if (consume_keyword("sizeof")) {
Node *node = unary();
add_type(node);
return new_node_num(node->kind == ND_LVAR ? node->lvar->lvar_size : node->ty->size);
}
Token *tok = consume_ident();
if (tok) {
// 関数呼び出し
if (consume("(")) {
return funcall(tok);
}
LVar *lvar = find_lvar(tok);
if (!lvar)
error_at(token->str, "未定義の変数");
if (consume("[")) { // 添え字演算子
Node *node = expr();
expect("]");
// x[y]を*(x+y)に変換する
return new_node(ND_DEREF, new_add(new_node_lvar(lvar, tok), node), NULL);
}
return new_node_lvar(lvar, tok);
}
// そうでなければ数値のはず
Node *node_num = new_node_num(expect_number());
if (consume("[")) { // 添え字演算子
Node *node = expr();
expect("]");
// x[y]を*(x+y)に変換する
return new_node(ND_DEREF, new_add(node_num, node), NULL);
}
return node_num;
}