-
Notifications
You must be signed in to change notification settings - Fork 6
/
token.c
134 lines (117 loc) · 3.15 KB
/
token.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
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "kcc.h"
static Vector *tokens;
static Token *add_token(int ty, char *intput) {
Token *token = calloc(1, sizeof(Token));
token->ty = ty;
token->input = intput;
vec_push(tokens, token);
return token;
}
static struct {
char *name;
int ty;
} symbols[] = {
{"==", TK_EQ}, {"!=", TK_NE}, {"&&", TK_LOGAND}, {"||", TK_LOGOR},
};
static Map *keywords;
static Map *keyword_map() {
Map *map = new_map();
map_puti(map, "if", TK_IF);
map_puti(map, "else", TK_ELSE);
map_puti(map, "for", TK_FOR);
map_puti(map, "do", TK_DO);
map_puti(map, "while", TK_WHILE);
map_puti(map, "return", TK_RETURN);
map_puti(map, "sizeof", TK_SIZEOF);
map_puti(map, "extern", TK_EXTERN);
map_puti(map, "int", TK_INT);
map_puti(map, "char", TK_CHAR);
return map;
}
static char *string_literal(char *p) {
assert(*p == '"');
Token *t = add_token(TK_STR, p++);
StringBuilder *sb = new_sb();
for (;*p != '"';p++) {
if (!*p)
error("string literal is broken");
if (*p == '\\') {
p++;
// escape sequence
static char escaped_table[256] = {
['a'] = '\a', ['b'] = '\b', ['f'] = '\f', ['n'] = '\n',
['r'] = '\r', ['t'] = '\t', ['v'] = '\v',
};
if (escaped_table[(int)*p]) {
sb_add(sb, escaped_table[(int)*p]);
continue;
}
}
// TODO: escape sequence
sb_add(sb, *p);
}
p++;
t->data = sb_string(sb);
return p;
}
// parse p to tokens
Vector *tokenize(char *p) {
keywords = keyword_map();
tokens = new_vector();
loop:
while (*p) {
// skip whitespace
if (isspace(*p)) {
p++;
continue;
}
for (int i = 0; symbols[i].name; i++) {
char *name = symbols[i].name;
int len = strlen(name);
if (strncmp(p, name, len) == 0) {
add_token(symbols[i].ty, p);
p += len;
goto loop;
}
}
if (isalpha(*p) || *p == '_') {
int len = 1;
while (isalpha(p[len]) || p[len] == '_' || isdigit(p[len])) {
len++;
}
char *name = strndup(p, len);
if (map_exists(keywords, name)) {
int ty = map_geti(keywords, name);
add_token(ty, p);
p+=len;
continue;
}
Token *t =add_token(TK_IDENT, p);
t->name = name;
p+=len;
continue;
}
if (strchr("+-*/()=;,{}&[]", *p)) {
add_token(*p, p);
p++;
continue;
}
if (isdigit(*p)) {
Token *token = add_token(TK_NUM, p);
token->val = strtol(p, &p, 10);
continue;
}
// string literal
if (*p == '"') {
p = string_literal(p);
continue;
}
error("can not tokenize: %s\n", p);
}
add_token(TK_EOF, p);
return tokens;
}