-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtokenize.c
More file actions
299 lines (261 loc) · 8.28 KB
/
tokenize.c
File metadata and controls
299 lines (261 loc) · 8.28 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
#include "9cc.h"
// 入力プログラム
char *user_input;
// 入力ファイル名
char *filename;
// 現在着目しているトークン
struct Token *token;
// エラーを報告するための関数
// printfと同じ引数を取る
void error(char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
fprintf(stderr, "\n");
exit(1);
}
// エラーの起きた場所を報告するための関数
// 下のようなフォーマットでエラーメッセージを表示する
//
// foo.c:10: x = y + + 5;
// ^ 式ではありません
void error_at(char *loc, char *msg, ...) {
va_list ap;
va_start(ap, msg);
// locが含まれている行の開始地点と終了地点を取得
char *line = loc;
while (user_input < line && line[-1] != '\n')
line--;
char *end = loc;
while (*end != '\n')
end++;
// 見つかった行が全体の何行目なのかを調べる
long line_num = 1;
for (char *p = user_input; p < line; p++)
if (*p == '\n')
line_num++;
// 見つかった行を、ファイル名と行番号と一緒に表示
long indent = fprintf(stderr, "%s:%ld: ", filename, line_num);
fprintf(stderr, "%.*s\n", (long)(end - line), line);
// エラー箇所を"^"で指し示して、エラーメッセージを表示
long pos = loc - line + indent;
fprintf(stderr, "%*s", pos, ""); // pos個の空白を出力
fprintf(stderr, "^ ");
vfprintf(stderr, msg, ap);
fprintf(stderr, "\n");
exit(1);
}
struct Token *get_token();
void set_token(struct Token *token);
// 次のトークンが期待している記号のときには、トークンを1つ読み進めて
// 真を返す。それ以外の場合には偽を返す。
long consume(char *op) {
struct Token *token = get_token();
if (token->kind != TK_RESERVED ||
strlen(op) != token->len ||
memcmp(token->str, op, token->len))
return false;
set_token(token->next);
return true;
}
long consume_keyword(char *op) {
struct Token *token = get_token();
if (token->kind != TK_KEYWORD ||
strlen(op) != token->len ||
memcmp(token->str, op, token->len))
return false;
set_token(token->next);
return true;
}
long equal_keyword(char *op) {
struct Token *token = get_token();
if (token->kind != TK_KEYWORD ||
strlen(op) != token->len ||
memcmp(token->str, op, token->len))
return false;
return true;
}
// 次のトークンが期待している記号のときには、トークンを1つ読み進める。
// それ以外の場合にはエラーを報告する。
void expect(char *op) {
struct Token *token = get_token();
if (token->kind != TK_RESERVED ||
strlen(op) != token->len ||
memcmp(token->str, op, token->len))
error_at(token->str, "\"%s\"ではありません", op);
set_token(token->next);
}
// 次のトークンが数値の場合、トークンを1つ読み進めてその数値を返す。
// それ以外の場合にはエラーを報告する。
long expect_number() {
struct Token *token = get_token();
if (token->kind != TK_NUM)
error_at(token->str, "数ではありません");
long val = token->val;
set_token(token->next);
return val;
}
long at_block() {
struct Token *token = get_token();
if (token->kind != TK_RESERVED ||
token->len != 1 ||
token->str[0] != "{"[0])
return false;
return true;
}
long at_eof() {
struct Token *token = get_token();
return token->kind == TK_EOF;
}
struct Token *consume_ident() {
struct Token *token = get_token();
if (token->kind != TK_IDENT)
return NULL;
struct Token *tok = token;
set_token(token->next);
return tok;
}
struct Token *consume_str() {
struct Token *token = get_token();
if (token->kind != TK_STR)
return NULL;
struct Token *tok = token;
set_token(token->next);
return tok;
}
// 新しいトークンを作成してcurに繋げる
static struct Token *new_token(long /*TokenKind*/ kind, struct Token *cur, char *str, long len) {
struct Token *tok = calloc(1, sizeof(struct Token));
tok->kind = kind;
tok->str = str;
tok->len = len;
cur->next = tok;
return tok;
}
static long startswith(char *p, char *q) {
return strncmp(p, q, strlen(q)) == 0;
}
static long isdoublequote(char c) {
return c == '"';
}
static long isescapedoublequote(char *p) {
return startswith(p, "\\\"");
}
// cが識別子の最初の文字として有効な場合、trueを返す
static long is_ident1(char c) {
return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || c == '_';
}
// cが識別子の2番目以降の文字として有効な場合、trueを返す
static long is_ident2(char c) {
return is_ident1(c) || ('0' <= c && c <= '9');
}
// pから区切子を読み取り、その長さを返す
static long read_punct(char *p) {
if (startswith(p, "+=") || startswith(p, "-=") ||
startswith(p, "*=") || startswith(p, "/=") ||
startswith(p, "%=") || startswith(p, "&=") ||
startswith(p, "^=") || startswith(p, "|=") ||
startswith(p, "<<=")|| startswith(p, ">>=")||
startswith(p, "||") || startswith(p, "&&") ||
startswith(p, "==") || startswith(p, "!=") ||
startswith(p, "<=") || startswith(p, ">=") ||
startswith(p, "++") || startswith(p, "--") ||
startswith(p, "->"))
return 2;
return ispunct(*p) ? 1 : 0;
}
static long is_alnum(char c) {
return ('a' <= c && c <= 'z') ||
('A' <= c && c <= 'Z') ||
('0' <= c && c <= '9') ||
(c == '_');
}
char const * const tokenize_gloval_keywords[] = {
"if", "else", "for", "while", "return", "int", "char", "long", "void", "sizeof", "struct", "enum", "_Alignof", "offsetof", "__builtin_offsetof"};
// キーワードかどうかを調べる。
static long is_keyword(const char * const p) {
for (long i = 0; i < sizeof(tokenize_gloval_keywords) / sizeof(*tokenize_gloval_keywords); i++)
if (strncmp(p, tokenize_gloval_keywords[i], strlen(tokenize_gloval_keywords[i])) == 0 && !is_alnum(p[strlen(tokenize_gloval_keywords[i])]))
return strlen(tokenize_gloval_keywords[i]);
return 0;
}
// 入力文字列user_inputをトークナイズしてそれを返す
void tokenize() {
char *p = user_input;
struct Token head;
head.next = NULL;
struct Token *cur = &head;
while (*p) {
// 空白文字をスキップ
if (isspace(*p)) {
p++;
continue;
}
// 行コメントをスキップ
if(strncmp(p, "//", 2) == 0) {
p += 2;
while(*p != '\n') {
p++;
}
continue;
}
// ブロックコメントをスキップ
if(strncmp(p, "/*", 2) == 0) {
char *q = strstr(p + 2, "*/");
if (!q)
error_at(p, "コメントが閉じられていません");
p = q + 2;
continue;
}
// 整数リテラル
if (isdigit(*p)) {
cur = new_token(TK_NUM, cur, p, 0);
char *q = p;
cur->val = strtol(p, &p, 10);
cur->len = p - q;
continue;
}
// 文字列リテラル
if (isdoublequote(*p)) {
char *start = p;
do {
p++;
} while (!(isdoublequote(*p)&&!isescapedoublequote(p-1))); // エスケープされた"じゃなければ文字列リテラル終了
++p;
cur = new_token(TK_STR, cur, start, p-start);
continue;
}
// キーワード
long key_len;
if ((key_len = is_keyword(p))) {
cur = new_token(TK_KEYWORD, cur, p, key_len);
p += key_len;
continue;
}
// staticを無視
if (strncmp(p, "static", strlen("static")) == 0 && !is_alnum(p[strlen("static")])) {
p += strlen("static");
continue;
}
// 識別子
if (is_ident1(*p)) {
char *start = p;
do {
p++;
} while (is_ident2(*p));
cur = new_token(TK_IDENT, cur, start, p-start);
continue;
}
// 区切子
long punct_len = read_punct(p);
if (punct_len) {
cur = new_token(TK_RESERVED, cur, p, punct_len);
p += cur->len;
continue;
}
error_at(p, "トークナイズできません");
}
new_token(TK_EOF, cur, p, 0);
token = head.next;
return;
}