Skip to content

Commit 29de6b0

Browse files
committed
complete debug: renew the tokenize function
1 parent be89379 commit 29de6b0

File tree

5 files changed

+53
-9
lines changed

5 files changed

+53
-9
lines changed

.vscode/tasks.json

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"tasks": [
3+
{
4+
"type": "cppbuild",
5+
"label": "C/C++: gcc.exe アクティブなファイルのビルド",
6+
"command": "C:\\MinGW\\bin\\gcc.exe",
7+
"args": [
8+
"-fdiagnostics-color=always",
9+
"-g",
10+
"${file}",
11+
"-o",
12+
"${fileDirname}\\${fileBasenameNoExtension}.exe"
13+
],
14+
"options": {
15+
"cwd": "${fileDirname}"
16+
},
17+
"problemMatcher": [
18+
"$gcc"
19+
],
20+
"group": {
21+
"kind": "build",
22+
"isDefault": true
23+
},
24+
"detail": "デバッガーによって生成されたタスク。"
25+
}
26+
],
27+
"version": "2.0.0"
28+
}

9cc

-8 Bytes
Binary file not shown.

9cc.c

+8-9
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ bool at_eof() {
9090
return token->kind == TK_EOF;
9191
}
9292

93-
// 新しいトークンを作成してcurに繋げる
9493
Token *new_token(TokenKind kind, Token *cur, char *str) {
9594
Token *tok = calloc(1, sizeof(Token));
9695
tok->kind = kind;
@@ -99,20 +98,20 @@ Token *new_token(TokenKind kind, Token *cur, char *str) {
9998
return tok;
10099
}
101100

102-
// 入力文字列pをトークナイズしてそれを返す
103-
Token *tokenize(char *p) {
101+
// 新しいトークンを作成してcurに繋げる
102+
Token *tokenize() {
103+
char *p = user_input;
104104
Token head;
105105
head.next = NULL;
106106
Token *cur = &head;
107107

108108
while (*p) {
109-
// 空白文字をスキップ
110109
if (isspace(*p)) {
111110
p++;
112111
continue;
113112
}
114113

115-
if (*p == '+' || *p == '-') {
114+
if (strchr("+-*/()", *p)) {
116115
cur = new_token(TK_RESERVED, cur, p++);
117116
continue;
118117
}
@@ -123,7 +122,7 @@ Token *tokenize(char *p) {
123122
continue;
124123
}
125124

126-
error_at(token->str, "トークナイズできません");
125+
error_at(p, "invalid token");
127126
}
128127

129128
new_token(TK_EOF, cur, p);
@@ -226,10 +225,9 @@ int main(int argc, char **argv) {
226225
error_at(argv[1], "引数の個数が正しくありません");
227226
return 1;
228227
}
229-
230228
// トークナイズしてパースする
231229
user_input = argv[1];
232-
token = tokenize(user_input);
230+
token = tokenize();
233231
Node *node = expr();
234232

235233
// アセンブリの前半部分を出力
@@ -245,4 +243,5 @@ int main(int argc, char **argv) {
245243
printf(" pop rax\n");
246244
printf(" ret\n");
247245
return 0;
248-
}
246+
}
247+

9cc.exe

52.1 KB
Binary file not shown.

tmp.s

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
.intel_syntax noprefix
2+
.globl main
3+
main:
4+
push 3
5+
push 5
6+
pop rdi
7+
pop rax
8+
add rax, rdi
9+
push rax
10+
push 2
11+
pop rdi
12+
pop rax
13+
cqo
14+
idiv rdi
15+
push rax
16+
pop rax
17+
ret

0 commit comments

Comments
 (0)