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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
|
#include "ccc.h"
#include "lexer.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdckdint.h>
#define LEXER_PANIC(format, ...) {\
fprintf(\
stderr,\
"ccc: lexer error: line %lu, column %lu: " format "\n",\
LINE,\
COL __VA_OPT__(,)\
__VA_ARGS__);\
exit(1);\
}
static FILE* file = NULL;
static int lookahead;
static unsigned long LINE, COL;
void lexer_load(const char* path) {
if (file != NULL) {
fclose(file);
}
file = fopen(path, "r");
if (file == NULL) CCC_PANIC;
lookahead = fgetc(file);
LINE = 1;
COL = 1;
}
void lexer_close() {
if (file == NULL) return;
fclose(file);
file = NULL;
}
bool lexer_peek(struct token* p_token) {
if (file == NULL) return false;
long orig_offset = ftell(file);
int orig_lookahead = lookahead;
bool rv = lexer_pop(p_token);
lookahead = orig_lookahead;
fseek(file, orig_offset, SEEK_SET);
return rv;
}
#define is_whitespace(c) (c == ' ' || c == '\t' || c == '\n')
#define is_lower_alpha(c) ('a' <= c && c <= 'z')
#define is_upper_alpha(c) ('A' <= c && c <= 'Z')
#define is_alpha(c) (is_lower_alpha(c) || is_upper_alpha(c))
#define is_numeric(c) ('0' <= c && c <= '9')
#define is_alphanumeric(c) (is_alpha(c) || is_numeric(c))
#define is_ident_legal(c) (is_alphanumeric(c) || c == '_' || c == '$')
static int consume_char() {
int rv = lookahead;
lookahead = fgetc(file);
COL++;
return rv;
}
static void lex_ident(struct token* p_token, char ic) {
char buf[1024] = {ic};
unsigned int len = 1;
while (is_ident_legal(lookahead)) {
int c = consume_char();
if (len >= sizeof(buf) - 1)
LEXER_PANIC(
"identifier exceeds maximum size (%ld)", sizeof(buf) - 1);
buf[len++] = c;
}
buf[len] = 0;
*p_token = (struct token) {
.type = IDENTIFIER,
.data.identifier = strndup(buf, sizeof(buf) - 1),
};
}
static void lex_float_lit(
struct token* p_token,
unsigned char base,
double iv
) {
LEXER_PANIC("floating point literals are not implemented");
}
static void lex_int_lit(struct token* p_token, int_lit_t iv) {
unsigned char base = 10;
/* TODO: exponentiation, 2e10 f.e. */
if (iv == 0) {
if (lookahead == 'x' || lookahead == 'X'
|| lookahead == 'b' || lookahead == 'B') {
base = (lookahead == 'x' || lookahead == 'X') ? 16 : 2;
int suffix = consume_char();
if (!is_alphanumeric(lookahead))
LEXER_PANIC("invalid suffix on integer constant: %c", suffix);
} else base = 8;
}
while (is_alphanumeric(lookahead)) {
int c = consume_char();
int_lit_t c_val;
if (is_numeric(c)) c_val = c - '0';
else if (is_lower_alpha(c)) c_val = c - 'a' + 10;
else c_val = c - 'A' + 10;
if (c_val >= base)
LEXER_PANIC("invalid digit in base %hhu: %c", base, c);
if (ckd_mul(&iv, iv, base))
LEXER_PANIC("integer literal will overflow");
if (ckd_add(&iv, iv, c_val))
LEXER_PANIC("integer literal will overflow");
}
if (lookahead == '.') {
consume_char();
lex_float_lit(p_token, base, iv);
return;
}
*p_token = (struct token) {
.type = INT_LIT,
.data.int_lit = iv,
};
}
static char replace_escape_sequence(char c) {
if (c == '\'') return '\'';
else if (c == '\"') return '\"';
else if (c == '\\') return '\\';
else if (c == 'r') return '\r';
else if (c == 'n') return '\n';
else if (c == 't') return '\t';
else LEXER_PANIC("escape sequence not implemented");
}
static void lex_char_lit(struct token* p_token) {
int c = consume_char();
if (c == EOF) LEXER_PANIC("unexpected EOF in char literal");
if (c == '\\') {
c = consume_char();
if (c == EOF) LEXER_PANIC("unexpected EOF in char literal");
c = replace_escape_sequence(c);
}
int close_quote = consume_char();
if (close_quote == EOF) LEXER_PANIC("unexpected EOF in char literal");
if (close_quote != '\'')
LEXER_PANIC(
"expected end of char literal, not \"%c\"", close_quote);
*p_token = (struct token) {
.type = CHAR_LIT,
.data.char_lit = c,
};
}
static void lex_str_lit(struct token* p_token) {
if (lookahead == '"') {
consume_char();
*p_token = (struct token) {
.type = STR_LIT,
.data.str_lit = strdup(""),
};
return;
}
char buf[65536];
unsigned int len = 0;
int c;
for (;;) {
c = consume_char();
if (c == '"') break;
if (c == EOF || c == '\n') LEXER_PANIC("unterminated string literal");
if (c == '\\') {
c = consume_char();
if (c == EOF) LEXER_PANIC("unterminated string literal");
c = replace_escape_sequence(c);
}
if (len >= sizeof(buf) - 1)
LEXER_PANIC(
"string literal exceeds maximum length (%ld)",
sizeof(buf) - 1);
buf[len++] = c;
}
buf[len] = 0;
*p_token = (struct token) {
.type = STR_LIT,
.data.str_lit = strndup(buf, sizeof(buf) - 1),
};
}
static enum token_type two_char_operator_type(char c) {
if (c == '!' && lookahead == '=') return NEQ;
if (c == '^' && lookahead == '=') return XEQ;
if (c == '&' && lookahead == '=') return AND_EQ;
if (c == '&' && lookahead == '&') return LOG_AND;
if (c == '*' && lookahead == '=') return MUL_EQ;
if (c == '-' && lookahead == '=') return NEG_EQ;
if (c == '-' && lookahead == '>') return ARROW;
if (c == '=' && lookahead == '=') return TEST_EQ;
if (c == '+' && lookahead == '=') return PLUS_EQ;
if (c == '|' && lookahead == '|') return LOG_PIPE;
if (c == '|' && lookahead == '=') return PIPE_EQ;
if (c == '/' && lookahead == '=') return DIV_EQ;
if (c == '%' && lookahead == '=') return MOD_EQ;
if (c == '<' && lookahead == '=') return LEQ;
if (c == '>' && lookahead == '=') return GEQ;
if (c == '<' && lookahead == '<') return SHL;
if (c == '>' && lookahead == '>') return SHR;
return NOT_FOUND;
}
static bool lex_complex_operator(struct token* p_token, char c) {
enum token_type type = two_char_operator_type(c);
if (type == NOT_FOUND) return false;
consume_char();
if (type == SHL && lookahead == '=') {
consume_char();
type = SHL_EQ;
}
if (type == SHR && lookahead == '=') {
consume_char();
type = SHR_EQ;
}
*p_token = (struct token) {.type = type};
return type;
}
static enum token_type lex_simple_operator(char c) {
switch (c) {
case '#': return HASHTAG;
case '(': return LPAREN;
case ')': return RPAREN;
case '{': return LCURLY;
case '}': return RCURLY;
case '[': return LSQUARE;
case ']': return RSQUARE;
case ':': return COLON;
case ';': return SEMI;
case ',': return COMMA;
case '.': return DOT;
case '?': return QMARK;
case '!': return NOT;
case '^': return XOR;
case '&': return AMP;
case '*': return STAR;
case '-': return NEG;
case '=': return ASSIGN;
case '+': return PLUS;
case '\\': return BSLASH;
case '|': return PIPE;
case '/': return DIV;
case '%': return MOD;
case '<': return LT;
case '>': return GT;
}
LEXER_PANIC("unexpected token %c", c);
}
bool lexer_pop(struct token* p_token) {
if (file == NULL) return false;
// consume all whitespace and comments preceding the next token
int c;
for (;;) {
c = consume_char();
if (c == EOF) return false;
else if (c == '/' && lookahead == '/') { // one of these
while (lookahead != EOF && lookahead != '\n') consume_char();
}
else if (c == '/' && lookahead == '*') {
consume_char(); /* consume the * */
int c = consume_char();
while (c != EOF && (c != '*' || lookahead != '/'))
c = consume_char();
if (c == EOF) LEXER_PANIC("unterminated /* comment");
consume_char(); /* consume the final slash */
}
else if (c == '\n') {
LINE++;
COL = 1;
}
else if (!is_whitespace(c)) break;
}
if (is_numeric(c))
lex_int_lit(p_token, c - '0');
else if (c == '.' && is_numeric(lookahead))
lex_float_lit(p_token, 10, 0);
else if (is_ident_legal(c))
lex_ident(p_token, c);
else if (c == '\'')
lex_char_lit(p_token);
else if (c == '"')
lex_str_lit(p_token);
else if (!lex_complex_operator(p_token, c))
p_token->type = lex_simple_operator(c);
return true;
}
|