-
Notifications
You must be signed in to change notification settings - Fork 0
/
tokenize.c
107 lines (89 loc) · 2.21 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
#include "50cc.h"
Token *token;
char *user_input;
bool at_eof() { return token->kind == TK_EOF; }
bool startswith(char *p, char *q) { return memcmp(p, q, strlen(q)) == 0; }
int is_alnum(char c) {
return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') ||
('0' <= c && c <= '9') || (c == '_');
}
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;
}
Token *tokenize() {
char *p = user_input;
Token head;
head.next = NULL;
Token *cur = &head;
while (*p) {
if (isspace(*p)) {
p++;
continue;
}
if (startswith(p, "==") || startswith(p, "!=") || startswith(p, "<=") ||
startswith(p, ">=")) {
cur = new_token(TK_RESERVED, cur, p, 2);
p += 2;
continue;
}
if (strchr("+-*/()<>=;{},&", *p)) {
cur = new_token(TK_RESERVED, cur, p++, 1);
continue;
}
if (strncmp(p, "return", 6) == 0 && !isalnum(p[6])) {
cur = new_token(TK_RETURN, cur, p, 6);
p += 6;
continue;
}
if (startswith(p, "if") && !isalnum(p[2])) {
cur = new_token(TK_IF, cur, p, 2);
p += 2;
continue;
}
if (startswith(p, "else") && !isalnum(p[4])) {
cur = new_token(TK_ELSE, cur, p, 4);
p += 4;
continue;
}
if (startswith(p, "while") && !isalnum(p[5])) {
cur = new_token(TK_WHILE, cur, p, 5);
p += 5;
continue;
}
if (startswith(p, "for") && !isalnum(p[3])) {
cur = new_token(TK_FOR, cur, p, 3);
p += 3;
continue;
}
if (startswith(p, "int") && !isalnum(p[3])) {
cur = new_token(TK_TYPE, cur, p, 3);
p += 3;
continue;
}
if ('a' <= *p && *p <= 'z') {
char *c = p;
while ('a' <= *c && *c <= 'z') {
c++;
};
int len = c - p;
cur = new_token(TK_IDENT, cur, p, len);
p = c;
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;
}
error_at(p, "expected a number");
}
new_token(TK_EOF, cur, p, 0);
return head.next;
}