-
Notifications
You must be signed in to change notification settings - Fork 0
/
tokenize.c
92 lines (78 loc) · 1.76 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
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include "9cc.h"
Vector* tokens;
Token *new_token(int ty, char *input) {
Token* tok = malloc(sizeof(Token));
tok->ty = ty;
tok->input = input;
return tok;
}
Token* new_token_ident(char* input, String *name) {
Token* tok = malloc(sizeof(Token));
tok->ty = TK_IDENT;
tok->input = input;
tok->name = name;
return tok;
}
Token *new_token_val(int ty, char *input, int val) {
Token* tok = malloc(sizeof(Token));
tok->ty = ty;
tok->input = input;
tok->val = val;
return tok;
}
bool is_identifier_char(char c) {
return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || '_' == c;
}
void tokenize(char *p) {
tokens = new_vector();
while (*p) {
if (isspace(*p) || *p == '\n') {
p++;
continue;
}
if (*p == '+'
|| *p == '-'
|| *p == '/'
|| *p == '*'
|| *p == '('
|| *p == ')'
|| *p == '{'
|| *p == '}'
|| *p == '<'
|| *p == '>'
|| *p == '&'
|| *p == '|'
|| *p == '='
|| *p == ';'
|| *p == '!'
|| *p == ',') {
vec_push(tokens, new_token(*p, p));
p++;
continue;
}
if (is_identifier_char(*p)) {
String *ident = new_string();
do {
str_push(ident, *p);
p++;
} while (is_identifier_char(*p));
str_push(ident, '\0');
Token* tok = new_token_ident(p, ident);
vec_push(tokens, tok);
continue;
}
if (isdigit(*p)) {
int int_val = strtol(p, &p, 10);
vec_push(tokens, new_token_val(TK_NUM, p, int_val));
continue;
}
fprintf(stderr, "トークナイズできません: %s\n", p);
exit(1);
}
vec_push(tokens, new_token(TK_EOF, p));
}