-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.c
305 lines (263 loc) · 7.24 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
#include "9cc.h"
// エラーを報告するための関数
// printfと同じ引数を取る
void error(char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
fprintf(stderr, "\n");
exit(1);
}
void error_at(char *loc, char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
int pos = loc - user_input;
fprintf(stderr, "%s\n", user_input);
fprintf(stderr, "%*s", pos, " "); // pos個の空白を出力
fprintf(stderr, "^ ");
vfprintf(stderr, fmt, ap);
fprintf(stderr, "\n");
exit(1);
}
bool is_alnum(char c) {
return ('a' <= c && c <= 'z') ||
('A' <= c && c <= 'Z') ||
('0' <= c && c <= '9') ||
(c == '_');
}
// 次のトークンが期待している記号の時には、トークンを1つ読み進めて
// 真を返す。それ以外の場合には偽を返す。
bool consume(char *op) {
if (token->kind != TK_RESERVED ||
strlen(op) != token->len ||
memcmp(token->str, op, token->len))
return false;
token = token->next;
return true;
}
Token *consume_ident() {
if (token->kind != TK_IDENT)
return NULL;
Token *ret_tok = token;
token = token->next;
return ret_tok;
}
bool consume_return() {
if (token->kind != TK_RETURN)
return false;
token = token->next;
return true;
}
// 次のトークンが期待している記号の時には、トークンを1つ読み進める。
// それ以外の場合にはエラーを報告する。
void expect(char *op) {
if (token->kind != TK_RESERVED ||
strlen(op) != token->len ||
memcmp(token->str, op, token->len))
error_at(token->str, "'%s'ではありません", op);
token = token->next;
}
// 次のトークンが数値の場合、トークンを1つ読み進めてその数値を返す。
// それ以外の場合にはエラーを報告する。
int expect_number() {
if (token->kind != TK_NUM)
error_at(token->str, "数ではありません");
int val = token->val;
token = token->next;
return val;
}
bool at_eof() {
return token->kind == TK_EOF;
}
// 新しいトークンを作成してcurにつなげる。
Token *new_token(TokenKind kind, Token *cur, char *str) {
Token *tok = calloc(1, sizeof(Token));
tok->kind = kind;;
tok->str = str;
cur->next = tok;
return tok;
}
// 入力文字列pをトークナイズしてそれを返す。
Token *tokenize(char *p) {
Token head;
head.next = NULL;
Token *cur = &head;
while (*p) {
// 空白文字をスキップ
if (isspace(*p)) {
p++;
continue;
}
if (!strncmp(p, "return", 6) && !is_alnum(p[6])) {
cur = new_token(TK_RETURN, cur, p);
cur->len = 6;
cur->str = p;
p += 6;
continue;
}
if (!strncmp(p, "==", 2) | !strncmp(p, "!=", 2) | !strncmp(p, "<=", 2) | !strncmp(p, ">=", 2)) {
cur = new_token(TK_RESERVED, cur, p);
cur->len = 2;
p += 2;
continue;
}
if (*p == '+' || *p == '-' || *p == '*' || *p == '/'
|| *p == '(' || *p == ')' || *p == '<' || *p == '>'
|| *p == ';' || *p == '=') {
cur = new_token(TK_RESERVED, cur, p++);
cur->len = 1;
continue;
}
if (isdigit(*p)) {
cur = new_token(TK_NUM, cur, p);
cur->val = strtol(p, &p, 10);
continue;
}
if (is_alnum(*p)) {
cur = new_token(TK_IDENT, cur, p);
cur->len = 1;
cur->str = p;
while (is_alnum(*++p)) {
cur->len++;
};
continue;
}
error_at(p, "トークナイズできません");
}
new_token(TK_EOF, cur, p);
return head.next;
}
// 変数を名前で検索する。見つからなかった場合は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;
}
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;
}
Node *primary() {
// 次のトークンが"("なら、"(" expr ")"のはず
if (consume("(")) {
Node *node = expr();
expect(")");
return node;
}
Token *tok = consume_ident();
if (tok) {
Node *node = calloc(1, sizeof(Node));
node->kind = ND_LVAR;
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());
}
Node *unary() {
if (consume("+"))
return primary();
if (consume("-"))
return new_node(ND_SUB, new_node_num(0), primary());
return primary();
}
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 *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 *relational() {
Node *node = add();
for (;;) {
if (consume("<"))
node = new_node(ND_LESS, node, add());
if (consume("<="))
node = new_node(ND_LESS_EQUAL, node, add());
if (consume(">"))
node = new_node(ND_LESS, add(), node);
if (consume(">="))
node = new_node(ND_LESS_EQUAL, add(), node);
else
return node;
}
}
Node *equality() {
Node *node = relational();
for (;;) {
if (consume("=="))
node = new_node(ND_EQUAL, node, relational());
else if (consume("!="))
node = new_node(ND_NOTEQ, node, relational());
else
return node;
}
}
Node *assign() {
Node *node = equality();
if (consume("="))
node = new_node(ND_ASSIGN, node, assign());
else
return node;
}
Node *expr() {
return assign();
}
Node *stmt() {
Node *node;
if (consume_return()) {
node = calloc(1, sizeof(Node));
node->kind = ND_RETURN;
node->lhs = expr();
} else {
node = expr();
}
if (!consume(";"))
error_at(token->str, "';'ではないトークンです");
return node;
}
Node *program() {
int i = 0;
locals = calloc(1, sizeof(LVar));
while(!at_eof()){
code[i++] = stmt();
}
code[i] = NULL;
}