-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lex.c
291 lines (273 loc) · 5.5 KB
/
lex.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include "8ccc.h"
#define BUFLEN 256
static Token *ungotten = NULL;
static Token *make_ident(String *s)
{
Token *r = malloc(sizeof(Token));
r->type = TTYPE_IDENT;
r->sval = get_cstring(s);
return r;
}
static Token *make_strtok(String *s)
{
Token *r = malloc(sizeof(Token));
r->type = TTYPE_STRING;
r->sval = get_cstring(s);
return r;
}
static Token *make_punct(char punct)
{
Token *r = malloc(sizeof(Token));
r->type = TTYPE_PUNCT;
r->punct = punct;
return r;
}
static Token *make_int(int ival)
{
Token *r = malloc(sizeof(Token));
r->type = TTYPE_INT;
r->ival = ival;
return r;
}
static Token *make_char(char c)
{
Token *r = malloc(sizeof(Token));
r->type = TTYPE_CHAR;
r->c = c;
return r;
}
static int getc_nonspace(void)
{
int c;
while ((c = getc(stdin)) != EOF) {
if (isspace(c) || c == '\n' || c == '\r') {
continue;
}
return c;
}
return EOF;
}
static Token *read_number(char c)
{
{
int n = c - '0';
for (;;) {
int c = getc(stdin);
if (!isdigit(c)) {
ungetc(c, stdin);
return make_int(n);
}
if (n < 0) {
n = n * 10 - (c - '0');
} else {
n = n * 10 + (c - '0');
}
}
return make_int(n);
}
}
static Token *read_char(void)
{
char c = getc(stdin);
if (c == EOF) {
goto err;
}
if (c == '\\') {
c = getc(stdin);
if (c == EOF) {
goto err;
}
}
char c2 = getc(stdin);
if (c2 == EOF) {
goto err;
}
if (c2 != '\'') {
error("Malformed char constant");
}
return make_char(c);
err:
error("Unterminated char");
}
static Token *read_string(void)
{
String *s = make_string();
for (;;) {
int c = getc(stdin);
if (c == EOF) {
error("Unterminated string");
}
if (c == '"') {
break;
}
if (c == '\\') {
c = getc(stdin);
if (c == EOF) {
error("Unterminated \\");
}
}
string_append(s, c);
}
return make_strtok(s);
}
static Token *read_ident(char c)
{
String *s = make_string();
string_append(s, c);
for (;;) {
int c2 = getc(stdin);
if (isalnum(c2) || c == '_') {
string_append(s, c2);
} else {
ungetc(c2, stdin);
return make_ident(s);
}
}
return make_strtok(s);
}
static Token *read_token_int(void)
{
int c = getc_nonspace();
switch (c) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
return read_number(c);
case '"':
return read_string();
case '\'':
return read_char();
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
case 'g':
case 'h':
case 'i':
case 'j':
case 'k':
case 'l':
case 'm':
case 'n':
case 'o':
case 'p':
case 'q':
case 'r':
case 's':
case 't':
case 'u':
case 'v':
case 'w':
case 'x':
case 'y':
case 'z':
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
case 'G':
case 'H':
case 'I':
case 'J':
case 'K':
case 'L':
case 'M':
case 'N':
case 'O':
case 'P':
case 'Q':
case 'R':
case 'S':
case 'T':
case 'U':
case 'V':
case 'W':
case 'X':
case 'Y':
case 'Z':
case '_':
return read_ident(c);
case '/':
case '=':
case '*':
case '+':
case '-':
case '(':
case ')':
case ',':
case ';':
return make_punct(c);
case EOF:
return NULL;
default:
error("Unexpected character: '%c'", c);
}
}
char *token_to_string(Token *tok)
{
switch (tok->type) {
case TTYPE_IDENT:
return tok->sval;
case TTYPE_PUNCT:
case TTYPE_CHAR: {
String *s = make_string();
string_append(s, tok->c);
return get_cstring(s);
}
case TTYPE_INT: {
String *s = make_string();
string_appendf(s, "%d", tok->ival);
return get_cstring(s);
}
case TTYPE_STRING: {
String *s = make_string();
string_appendf(s, "\"%s\"", tok->sval);
return get_cstring(s);
}
default:
error("internal error: unknown token type: %d", tok->type);
}
}
bool is_punct(Token *tok, char c)
{
if (!tok) {
error("Token is null");
}
return tok->type == TTYPE_PUNCT && tok->punct == c;
}
void unget_token(Token *tok)
{
if (ungotten) {
error("Push back buffer is already full");
}
ungotten = tok;
}
Token *peek_token(void)
{
Token *tok = read_token();
unget_token(tok);
return tok;
}
Token *read_token(void)
{
if (ungotten) {
Token *tok = ungotten;
ungotten = NULL;
return tok;
}
return read_token_int();
}