-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path9cc.c
233 lines (194 loc) · 3.64 KB
/
9cc.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
#include<ctype.h>
#include<stdarg.h>
#include<stdbool.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef enum{
TK_RESERVED,
TK_NUM,
TK_EOF,
} TokenKind;
typedef struct Token Token;
struct Token{
TokenKind kind;
Token *next;
int val;
char *str;
};
char *user_input;
Token *token;
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, "");
fprintf(stderr, "^ ");
vfprintf(stderr, fmt, ap);
fprintf(stderr, "\n");
exit(1);
}
bool consume(char op){
if(token->kind != TK_RESERVED || token->str[0] != op)
return false;
token = token->next;
return true;
}
void expect(char op){
if(token->kind != TK_RESERVED || token->str[0] != op)
error_at(token->str, "'%c'ではありません", op);
token = token->next;
}
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;
}
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;
}
Token *tokenize(){
char *p = user_input;
Token head;
head.next = NULL;
Token *cur = &head;
while(*p){
if(isspace(*p)){
p++;
continue;
}
if(strchr("+-*/()", *p)){
cur = new_token(TK_RESERVED, cur, p++);
continue;
}
if(isdigit(*p)){
cur = new_token(TK_NUM, cur, p);
cur->val = strtol(p, &p, 10);
continue;
}
error_at(p, "トークナイズできません");
}
new_token(TK_EOF, cur, p);
return head.next;
}
typedef enum {
ND_ADD,
ND_SUB,
ND_MUL,
ND_DIV,
ND_NUM,
} NodeKind;
typedef struct Node Node;
struct Node {
NodeKind kind;
Node *lhs;
Node *rhs;
int val;
};
Node *new_node(NodeKind kind){
Node *node = calloc(1, sizeof(Node));
node->kind = kind;
return node;
}
Node *new_binary(NodeKind kind, Node *lhs, Node *rhs){
Node *node = new_node(kind);
node->lhs = lhs;
node->rhs = rhs;
return node;
}
Node *new_num(int val){
Node *node = new_node(ND_NUM);
node->val = val;
return node;
}
Node *expr();
Node *mul();
Node *primary();
Node *expr(){
Node *node = mul();
for(;;) {
if (consume('+'))
node = new_binary(ND_ADD, node, mul());
else if (consume('-'))
node = new_binary(ND_SUB, node, mul());
else
return node;
}
}
Node *mul(){
Node *node = primary();
for(;;){
if(consume('*'))
node = new_binary(ND_MUL, node, primary());
else if(consume('/'))
node = new_binary(ND_DIV, node, primary());
else
return node;
}
}
Node *primary(){
if(consume('(')){
Node *node = expr();
expect(')');
return node;
}
return new_num(expect_number());
}
void gen(Node *node){
if(node->kind == ND_NUM){
printf(" push %d\n", node->val);
return;
}
gen(node->lhs);
gen(node->rhs);
printf(" pop rdi\n");
printf(" pop rax\n");
switch(node->kind){
case ND_ADD:
printf(" add rax, rdi\n");
break;
case ND_SUB:
printf(" sub rax, rdi\n");
break;
case ND_MUL:
printf(" imul rax, rdi\n");
break;
case ND_DIV:
printf(" cqo\n");
printf(" idiv rdi\n");
break;
}
printf(" push rax\n");
}
int main(int argc, char **argv){
if(argc !=2){
error("%s 引数の個数が正しくありません", argv[0]);
}
user_input = argv[1];
token = tokenize();
Node *node = expr();
printf(".intel_syntax noprefix\n");
printf(".globl main\n");
printf("main:\n");
gen(node);
printf(" pop rax\n");
printf(" ret\n");
return 0;
}