-
Notifications
You must be signed in to change notification settings - Fork 0
/
tokenize.c
170 lines (147 loc) · 3 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
#include "9cc.h"
#include "util.h"
#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef enum {
NOT_IN_COMMENT = 0,
IN_COMMENT,
} COMMENT_STATUS;
static int mygetc();
static int myungetc(int);
static FILE *yyin;
Vector *tokenize(char *path) {
Vector *tokens;
Token *token;
char buf[100];
COMMENT_STATUS status = NOT_IN_COMMENT;
int c;
tokens = new_vector();
yyin = fopen(path, "r");
if (yyin == NULL)
error("file cannot open");
while (true) {
token = malloc(sizeof(Token));
c = mygetc();
// comment 終端処理
if (status == IN_COMMENT) {
if (c == '*') {
c = mygetc();
if (c == '/') {
status = NOT_IN_COMMENT;
continue;
}
myungetc(c);
}
continue;
}
// commnent 開始処理
if (c == '/') {
c = mygetc();
if (c == '*') {
status = IN_COMMENT;
continue;
}
// comment の開始記号ではない単なる '/'
myungetc(c);
token->ty = '/';
vec_push(tokens, token);
continue;
}
// ignore space
if (isspace(c)) {
continue;
}
// 演算子, ";"
switch (c) {
case '+':
case '-':
case '*':
case '&':
case '(':
case ')':
case '{':
case '}':
case ',':
case ';':
token->ty = c;
vec_push(tokens, token);
continue;
}
// "=", "=="
if (c == '=') {
c = mygetc();
if (c == '=') {
token->ty = TK_EQ;
vec_push(tokens, token);
} else {
myungetc(c);
token->ty = '=';
vec_push(tokens, token);
}
continue;
}
// "!="
if (c == '!') {
c = mygetc();
if (c == '=') {
token->ty = TK_NE;
vec_push(tokens, token);
} else {
token->ty = c;
vec_push(tokens, token);
myungetc(c);
}
continue;
}
// NUMBER
if (isdigit(c)) {
token->val = c - '0';
c = mygetc();
while (isdigit(c)) {
token->val = token->val * 10 + c - '0';
c = mygetc();
}
myungetc(c);
token->ty = TK_NUM;
vec_push(tokens, token);
continue;
}
// identifier or reserved word
if (isalpha(c) || c == '_') {
char *p = buf;
*p++ = c;
c = mygetc();
while (isalnum(c) || c == '_') {
*p++ = c;
c = mygetc();
}
myungetc(c);
*p = '\0';
token->name = malloc((strlen(buf) + 1) * sizeof(char));
strcpy(token->name, buf);
SYM_REC *rec;
token->ty = TK_IDENT;
if ((rec = query_reserved(token->name)) != NULL) {
token->ty = rec->token;
}
vec_push(tokens, token);
continue;
}
if (c == EOF) {
token->ty = TK_EOF;
vec_push(tokens, token);
break;
}
error("トークナイズできません: (%c)\n", c);
}
return tokens;
}
int mygetc() {
return fgetc(yyin);
}
int myungetc(int c) {
return ungetc(c, yyin);
}