/
tokenize.c
195 lines (165 loc) · 4.16 KB
/
tokenize.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
#include "chibicc.h"
char *user_input;
Token *token;
// Reports an error and exit.
void error(char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
fprintf(stderr, "\n");
exit(1);
}
// Reports an error location and exit.
void verror_at(char *loc, char *fmt, va_list ap) {
int pos = loc - user_input;
fprintf(stderr, "%s\n", user_input);
fprintf(stderr, "%*s", pos, ""); // print pos spaces.
fprintf(stderr, "^ ");
vfprintf(stderr, fmt, ap);
fprintf(stderr, "\n");
exit(1);
}
// Reports an error location and exit.
void error_at(char *loc, char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
verror_at(loc, fmt, ap);
}
// Reports an error location and exit.
void error_tok(Token *tok, char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
if (tok)
verror_at(tok->str, fmt, ap);
vfprintf(stderr, fmt, ap);
fprintf(stderr, "\n");
exit(1);
}
char *strndup(char *p, int len) {
char *buf = malloc(len + 1);
strncpy(buf, p, len);
buf[len] = '\0';
return buf;
}
// Consumes the current token if it matches `op`.
Token *consume(char *op) {
if (token->kind != TK_RESERVED || strlen(op) != token->len ||
memcmp(token->str, op, token->len))
return NULL;
Token *t = token;
token = token->next;
return t;
}
// Consumes the current token if it is an identifier.
Token *consume_ident() {
if (token->kind != TK_IDENT)
return NULL;
Token *t = token;
token = token->next;
return t;
}
// Ensure that the current token is `op`.
void expect(char *op) {
if (token->kind != TK_RESERVED || strlen(op) != token->len ||
memcmp(token->str, op, token->len))
error_tok(token, "expected \"%s\"", op);
token = token->next;
}
// Ensure that the current token is TK_NUM.
int expect_number() {
if (token->kind != TK_NUM)
error_tok(token, "expected a number");
int val = token->val;
token = token->next;
return val;
}
// Ensure that the current token is TK_IDENT.
char *expect_ident() {
if (token->kind != TK_IDENT)
error_tok(token, "expected an identifier");
char *s = strndup(token->str, token->len);
token = token->next;
return s;
}
bool at_eof() {
return token->kind == TK_EOF;
}
// Create a new token and add it as the next token of `cur`.
Token *new_token(TokenKind kind, Token *cur, char *str, int len) {
Token *tok = calloc(1, sizeof(Token));
tok->kind = kind;
tok->str = str;
tok->len = len;
cur->next = tok;
return tok;
}
bool startswith(char *p, char *q) {
return memcmp(p, q, strlen(q)) == 0;
}
bool is_alpha(char c) {
return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || c == '_';
}
bool is_alnum(char c) {
return is_alpha(c) || ('0' <= c && c <= '9');
}
char *starts_with_reserved(char *p) {
// Keyword
static char *kw[] = {"return", "if", "else", "while", "for"};
for (int i = 0; i < sizeof(kw) / sizeof(*kw); i++) {
int len = strlen(kw[i]);
if (startswith(p, kw[i]) && !is_alnum(p[len]))
return kw[i];
}
// Multi-letter punctuator
static char *ops[] = {"==", "!=", "<=", ">="};
for (int i = 0; i < sizeof(ops) / sizeof(*ops); i++)
if (startswith(p, ops[i]))
return ops[i];
return NULL;
}
// Tokenize `user_input` and returns new tokens.
Token *tokenize() {
char *p = user_input;
Token head;
head.next = NULL;
Token *cur = &head;
while (*p) {
// Skip whitespace characters.
if (isspace(*p)) {
p++;
continue;
}
// Keyword or multi-letter punctuator
char *kw = starts_with_reserved(p);
if (kw) {
int len = strlen(kw);
cur = new_token(TK_RESERVED, cur, p, len);
p += len;
continue;
}
// Single-letter punctuator
if (strchr("+-*/()<>;={},&", *p)) {
cur = new_token(TK_RESERVED, cur, p++, 1);
continue;
}
// Identifier
if (is_alpha(*p)) {
char *q = p++;
while (is_alnum(*p))
p++;
cur = new_token(TK_IDENT, cur, q, p - q);
continue;
}
// Integer literal
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;
}
error_at(p, "invalid token");
}
new_token(TK_EOF, cur, p, 0);
return head.next;
}