This repository has been archived by the owner on Jun 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 211
/
lex.c
690 lines (647 loc) · 17.1 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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
const char *typedef_keyword;
const char *enum_keyword;
const char *struct_keyword;
const char *union_keyword;
const char *var_keyword;
const char *const_keyword;
const char *func_keyword;
const char *sizeof_keyword;
const char *cast_keyword;
const char *break_keyword;
const char *continue_keyword;
const char *return_keyword;
const char *if_keyword;
const char *else_keyword;
const char *while_keyword;
const char *do_keyword;
const char *for_keyword;
const char *switch_keyword;
const char *case_keyword;
const char *default_keyword;
const char *first_keyword;
const char *last_keyword;
const char **keywords;
#define KEYWORD(name) name##_keyword = str_intern(#name); buf_push(keywords, name##_keyword)
void init_keywords(void) {
static bool inited;
if (inited) {
return;
}
KEYWORD(typedef);
char *arena_end = intern_arena.end;
KEYWORD(enum);
KEYWORD(struct);
KEYWORD(union);
KEYWORD(const);
KEYWORD(var);
KEYWORD(func);
KEYWORD(sizeof);
KEYWORD(cast);
KEYWORD(break);
KEYWORD(continue);
KEYWORD(return);
KEYWORD(if);
KEYWORD(else);
KEYWORD(while);
KEYWORD(do);
KEYWORD(for);
KEYWORD(switch);
KEYWORD(case);
KEYWORD(default);
assert(intern_arena.end == arena_end);
first_keyword = typedef_keyword;
last_keyword = default_keyword;
inited = true;
}
#undef KEYWORD
bool is_keyword_name(const char *name) {
return first_keyword <= name && name <= last_keyword;
}
typedef enum TokenKind {
TOKEN_EOF,
TOKEN_COLON,
TOKEN_LPAREN,
TOKEN_RPAREN,
TOKEN_LBRACE,
TOKEN_RBRACE,
TOKEN_LBRACKET,
TOKEN_RBRACKET,
TOKEN_COMMA,
TOKEN_DOT,
TOKEN_QUESTION,
TOKEN_SEMICOLON,
TOKEN_KEYWORD,
TOKEN_INT,
TOKEN_FLOAT,
TOKEN_STR,
TOKEN_NAME,
TOKEN_NEG,
TOKEN_NOT,
// Multiplicative precedence
TOKEN_FIRST_MUL,
TOKEN_MUL = TOKEN_FIRST_MUL,
TOKEN_DIV,
TOKEN_MOD,
TOKEN_AND,
TOKEN_LSHIFT,
TOKEN_RSHIFT,
TOKEN_LAST_MUL = TOKEN_RSHIFT,
// Additive precedence
TOKEN_FIRST_ADD,
TOKEN_ADD = TOKEN_FIRST_ADD,
TOKEN_SUB,
TOKEN_XOR,
TOKEN_OR,
TOKEN_LAST_ADD = TOKEN_OR,
// Comparative precedence
TOKEN_FIRST_CMP,
TOKEN_EQ = TOKEN_FIRST_CMP,
TOKEN_NOTEQ,
TOKEN_LT,
TOKEN_GT,
TOKEN_LTEQ,
TOKEN_GTEQ,
TOKEN_LAST_CMP = TOKEN_GTEQ,
TOKEN_AND_AND,
TOKEN_OR_OR,
// Assignment operators
TOKEN_FIRST_ASSIGN,
TOKEN_ASSIGN = TOKEN_FIRST_ASSIGN,
TOKEN_ADD_ASSIGN,
TOKEN_SUB_ASSIGN,
TOKEN_OR_ASSIGN,
TOKEN_AND_ASSIGN,
TOKEN_XOR_ASSIGN,
TOKEN_LSHIFT_ASSIGN,
TOKEN_RSHIFT_ASSIGN,
TOKEN_MUL_ASSIGN,
TOKEN_DIV_ASSIGN,
TOKEN_MOD_ASSIGN,
TOKEN_LAST_ASSIGN = TOKEN_MOD_ASSIGN,
TOKEN_INC,
TOKEN_DEC,
TOKEN_COLON_ASSIGN,
} TokenKind;
typedef enum TokenMod {
TOKENMOD_NONE,
TOKENMOD_HEX,
TOKENMOD_BIN,
TOKENMOD_OCT,
TOKENMOD_CHAR,
} TokenMod;
const char *token_kind_names[] = {
[TOKEN_EOF] = "EOF",
[TOKEN_COLON] = ":",
[TOKEN_LPAREN] = "(",
[TOKEN_RPAREN] = ")",
[TOKEN_LBRACE] = "{",
[TOKEN_RBRACE] = "}",
[TOKEN_LBRACKET] = "[",
[TOKEN_RBRACKET] = "]",
[TOKEN_COMMA] = ",",
[TOKEN_DOT] = ".",
[TOKEN_QUESTION] = "?",
[TOKEN_SEMICOLON] = ";",
[TOKEN_KEYWORD] = "keyword",
[TOKEN_INT] = "int",
[TOKEN_FLOAT] = "float",
[TOKEN_STR] = "string",
[TOKEN_NAME] = "name",
[TOKEN_MUL] = "*",
[TOKEN_DIV] = "/",
[TOKEN_MOD] = "%",
[TOKEN_AND] = "&",
[TOKEN_LSHIFT] = "<<",
[TOKEN_RSHIFT] = ">>",
[TOKEN_ADD] = "+",
[TOKEN_SUB] = "-",
[TOKEN_OR] = "|",
[TOKEN_XOR] = "^",
[TOKEN_EQ] = "==",
[TOKEN_NOTEQ] = "!=",
[TOKEN_LT] = "<",
[TOKEN_GT] = ">",
[TOKEN_LTEQ] = "<=",
[TOKEN_GTEQ] = ">=",
[TOKEN_AND_AND] = "&&",
[TOKEN_OR_OR] = "||",
[TOKEN_ASSIGN] = "=",
[TOKEN_ADD_ASSIGN] = "+=",
[TOKEN_SUB_ASSIGN] = "-=",
[TOKEN_OR_ASSIGN] = "|=",
[TOKEN_AND_ASSIGN] = "&=",
[TOKEN_XOR_ASSIGN] = "^=",
[TOKEN_MUL_ASSIGN] = "*=",
[TOKEN_DIV_ASSIGN] = "/=",
[TOKEN_MOD_ASSIGN] = "%=",
[TOKEN_LSHIFT_ASSIGN] = "<<=",
[TOKEN_RSHIFT_ASSIGN] = ">>=",
[TOKEN_INC] = "++",
[TOKEN_DEC] = "--",
[TOKEN_COLON_ASSIGN] = ":=",
};
const char *token_kind_name(TokenKind kind) {
if (kind < sizeof(token_kind_names)/sizeof(*token_kind_names)) {
return token_kind_names[kind];
} else {
return "<unknown>";
}
}
typedef struct Token {
TokenKind kind;
TokenMod mod;
const char *start;
const char *end;
union {
int64_t int_val;
double float_val;
const char *str_val;
const char *name;
};
} Token;
Token token;
const char *stream;
const char *line_start;
int src_line;
const char *src_name;
const char *token_info(void) {
if (token.kind == TOKEN_NAME || token.kind == TOKEN_KEYWORD) {
return token.name;
} else {
return token_kind_name(token.kind);
}
}
uint8_t char_to_digit[256] = {
['0'] = 0,
['1'] = 1,
['2'] = 2,
['3'] = 3,
['4'] = 4,
['5'] = 5,
['6'] = 6,
['7'] = 7,
['8'] = 8,
['9'] = 9,
['a'] = 10, ['A'] = 10,
['b'] = 11, ['B'] = 11,
['c'] = 12, ['C'] = 12,
['d'] = 13, ['D'] = 13,
['e'] = 14, ['E'] = 14,
['f'] = 15, ['F'] = 15,
};
void scan_int(void) {
uint64_t base = 10;
if (*stream == '0') {
stream++;
if (tolower(*stream) == 'x') {
stream++;
token.mod = TOKENMOD_HEX;
base = 16;
} else if (tolower(*stream) == 'b') {
stream++;
token.mod = TOKENMOD_BIN;
base = 2;
} else if (isdigit(*stream)) {
token.mod = TOKENMOD_OCT;
base = 8;
}
}
uint64_t val = 0;
for (;;) {
uint64_t digit = char_to_digit[*(unsigned char *)stream];
if (digit == 0 && *stream != '0') {
break;
}
if (digit >= base) {
syntax_error("Digit '%c' out of range for base %" PRIu64, *stream, base);
digit = 0;
}
if (val > (UINT64_MAX - digit)/base) {
syntax_error("Integer literal overflow");
while (isdigit(*stream)) {
stream++;
}
val = 0;
break;
}
val = val*base + digit;
stream++;
}
token.kind = TOKEN_INT;
token.int_val = val;
}
void scan_float(void) {
const char *start = stream;
while (isdigit(*stream)) {
stream++;
}
if (*stream == '.') {
stream++;
}
while (isdigit(*stream)) {
stream++;
}
if (tolower(*stream) == 'e') {
stream++;
if (*stream == '+' || *stream == '-') {
stream++;
}
if (!isdigit(*stream)) {
syntax_error("Expected digit after float literal exponent, found '%c'.", *stream);
}
while (isdigit(*stream)) {
stream++;
}
}
double val = strtod(start, NULL);
if (val == HUGE_VAL) {
syntax_error("Float literal overflow");
}
token.kind = TOKEN_FLOAT;
token.float_val = val;
}
char escape_to_char[256] = {
['n'] = '\n',
['r'] = '\r',
['t'] = '\t',
['v'] = '\v',
['b'] = '\b',
['a'] = '\a',
['0'] = 0,
};
void scan_char(void) {
assert(*stream == '\'');
stream++;
char val = 0;
if (*stream == '\'') {
syntax_error("Char literal cannot be empty");
stream++;
} else if (*stream == '\n') {
syntax_error("Char literal cannot contain newline");
} else if (*stream == '\\') {
stream++;
val = escape_to_char[*(unsigned char *)stream];
if (val == 0 && *stream != '0') {
syntax_error("Invalid char literal escape '\\%c'", *stream);
}
stream++;
} else {
val = *stream;
stream++;
}
if (*stream != '\'') {
syntax_error("Expected closing char quote, got '%c'", *stream);
} else {
stream++;
}
token.kind = TOKEN_INT;
token.int_val = val;
token.mod = TOKENMOD_CHAR;
}
void scan_str(void) {
assert(*stream == '"');
stream++;
char *str = NULL;
while (*stream && *stream != '"') {
char val = *stream;
if (val == '\n') {
syntax_error("String literal cannot contain newline");
break;
} else if (val == '\\') {
stream++;
val = escape_to_char[*(unsigned char *)stream];
if (val == 0 && *stream != '0') {
syntax_error("Invalid string literal escape '\\%c'", *stream);
}
}
buf_push(str, val);
stream++;
}
if (*stream) {
assert(*stream == '"');
stream++;
} else {
syntax_error("Unexpected end of file within string literal");
}
buf_push(str, 0);
token.kind = TOKEN_STR;
token.str_val = str;
}
#define CASE1(c1, k1) \
case c1: \
token.kind = k1; \
stream++; \
break;
#define CASE2(c1, k1, c2, k2) \
case c1: \
token.kind = k1; \
stream++; \
if (*stream == c2) { \
token.kind = k2; \
stream++; \
} \
break;
#define CASE3(c1, k1, c2, k2, c3, k3) \
case c1: \
token.kind = k1; \
stream++; \
if (*stream == c2) { \
token.kind = k2; \
stream++; \
} else if (*stream == c3) { \
token.kind = k3; \
stream++; \
} \
break;
void next_token(void) {
repeat:
token.start = stream;
token.mod = 0;
switch (*stream) {
case ' ': case '\n': case '\r': case '\t': case '\v':
while (isspace(*stream)) {
if (*stream++ == '\n') {
line_start = stream;
src_line++;
}
}
goto repeat;
case '\'':
scan_char();
break;
case '"':
scan_str();
break;
case '.':
if (isdigit(stream[1])) {
scan_float();
} else {
token.kind = TOKEN_DOT;
stream++;
}
break;
case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': {
while (isdigit(*stream)) {
stream++;
}
char c = *stream;
stream = token.start;
if (c == '.' || tolower(c) == 'e') {
scan_float();
} else {
scan_int();
}
break;
}
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 '_':
while (isalnum(*stream) || *stream == '_') {
stream++;
}
token.name = str_intern_range(token.start, stream);
token.kind = is_keyword_name(token.name) ? TOKEN_KEYWORD : TOKEN_NAME;
break;
case '<':
token.kind = TOKEN_LT;
stream++;
if (*stream == '<') {
token.kind = TOKEN_LSHIFT;
stream++;
if (*stream == '=') {
token.kind = TOKEN_LSHIFT_ASSIGN;
stream++;
}
} else if (*stream == '=') {
token.kind = TOKEN_LTEQ;
stream++;
}
break;
case '>':
token.kind = TOKEN_GT;
stream++;
if (*stream == '>') {
token.kind = TOKEN_RSHIFT;
stream++;
if (*stream == '=') {
token.kind = TOKEN_RSHIFT_ASSIGN;
stream++;
}
} else if (*stream == '=') {
token.kind = TOKEN_GTEQ;
int x = 0;
x |= 1;
stream++;
}
break;
case '/':
token.kind = TOKEN_DIV;
stream++;
if (*stream == '=') {
token.kind = TOKEN_DIV_ASSIGN;
stream++;
} else if (*stream == '/') {
stream++;
while (*stream && *stream != '\n') {
stream++;
}
goto repeat;
}
break;
CASE1('\0', TOKEN_EOF)
CASE1('(', TOKEN_LPAREN)
CASE1(')', TOKEN_RPAREN)
CASE1('{', TOKEN_LBRACE)
CASE1('}', TOKEN_RBRACE)
CASE1('[', TOKEN_LBRACKET)
CASE1(']', TOKEN_RBRACKET)
CASE1(',', TOKEN_COMMA)
CASE1('?', TOKEN_QUESTION)
CASE1(';', TOKEN_SEMICOLON)
CASE1('~', TOKEN_NEG)
CASE1('!', TOKEN_NOT)
CASE2(':', TOKEN_COLON, '=', TOKEN_COLON_ASSIGN)
CASE2('=', TOKEN_ASSIGN, '=', TOKEN_EQ)
CASE2('^', TOKEN_XOR, '=', TOKEN_XOR_ASSIGN)
CASE2('*', TOKEN_MUL, '=', TOKEN_MUL_ASSIGN)
CASE2('%', TOKEN_MOD, '=', TOKEN_MOD_ASSIGN)
CASE3('+', TOKEN_ADD, '=', TOKEN_ADD_ASSIGN, '+', TOKEN_INC)
CASE3('-', TOKEN_SUB, '=', TOKEN_SUB_ASSIGN, '-', TOKEN_DEC)
CASE3('&', TOKEN_AND, '=', TOKEN_AND_ASSIGN, '&', TOKEN_AND_AND)
CASE3('|', TOKEN_OR, '=', TOKEN_OR_ASSIGN, '|', TOKEN_OR_OR)
default:
syntax_error("Invalid '%c' token, skipping", *stream);
stream++;
goto repeat;
}
token.end = stream;
}
#undef CASE1
#undef CASE2
#undef CASE3
void init_stream(const char *name, const char *buf) {
stream = buf;
src_name = name ? name : "<anonymous>";
src_line = 1;
line_start = stream;
next_token();
}
bool is_token(TokenKind kind) {
return token.kind == kind;
}
bool is_token_eof(void) {
return token.kind == TOKEN_EOF;
}
bool is_token_name(const char *name) {
return token.kind == TOKEN_NAME && token.name == name;
}
bool is_keyword(const char *name) {
return is_token(TOKEN_KEYWORD) && token.name == name;
}
bool match_keyword(const char *name) {
if (is_keyword(name)) {
next_token();
return true;
} else {
return false;
}
}
bool match_token(TokenKind kind) {
if (is_token(kind)) {
next_token();
return true;
} else {
return false;
}
}
bool expect_token(TokenKind kind) {
if (is_token(kind)) {
next_token();
return true;
} else {
fatal("expected token %s, got %s", token_kind_name(kind), token_info());
return false;
}
}
void keyword_test(void) {
init_keywords();
assert(is_keyword_name(first_keyword));
assert(is_keyword_name(last_keyword));
for (const char **it = keywords; it != buf_end(keywords); it++) {
assert(is_keyword_name(*it));
}
assert(!is_keyword_name(str_intern("foo")));
}
#define assert_token(x) assert(match_token(x))
#define assert_token_name(x) assert(token.name == str_intern(x) && match_token(TOKEN_NAME))
#define assert_token_int(x) assert(token.int_val == (x) && match_token(TOKEN_INT))
#define assert_token_float(x) assert(token.float_val == (x) && match_token(TOKEN_FLOAT))
#define assert_token_str(x) assert(strcmp(token.str_val, (x)) == 0 && match_token(TOKEN_STR))
#define assert_token_eof() assert(is_token(0))
void lex_test(void) {
keyword_test();
assert(str_intern("func") == func_keyword);
// Integer literal tests
init_stream(NULL, "0 18446744073709551615 0xffffffffffffffff 042 0b1111");
assert_token_int(0);
assert_token_int(18446744073709551615ull);
assert(token.mod == TOKENMOD_HEX);
assert_token_int(0xffffffffffffffffull);
assert(token.mod == TOKENMOD_OCT);
assert_token_int(042);
assert(token.mod == TOKENMOD_BIN);
assert_token_int(0xF);
assert_token_eof();
// Float literal tests
init_stream(NULL, "3.14 .123 42. 3e10");
assert_token_float(3.14);
assert_token_float(.123);
assert_token_float(42.);
assert_token_float(3e10);
assert_token_eof();
// Char literal tests
init_stream(NULL, "'a' '\\n'");
assert_token_int('a');
assert_token_int('\n');
assert_token_eof();
// String literal tests
init_stream(NULL, "\"foo\" \"a\\nb\"");
assert_token_str("foo");
assert_token_str("a\nb");
assert_token_eof();
// Operator tests
init_stream(NULL, ": := + += ++ < <= << <<=");
assert_token(TOKEN_COLON);
assert_token(TOKEN_COLON_ASSIGN);
assert_token(TOKEN_ADD);
assert_token(TOKEN_ADD_ASSIGN);
assert_token(TOKEN_INC);
assert_token(TOKEN_LT);
assert_token(TOKEN_LTEQ);
assert_token(TOKEN_LSHIFT);
assert_token(TOKEN_LSHIFT_ASSIGN);
assert_token_eof();
// Misc tests
init_stream(NULL, "XY+(XY)_HELLO1,234+994");
assert_token_name("XY");
assert_token(TOKEN_ADD);
assert_token(TOKEN_LPAREN);
assert_token_name("XY");
assert_token(TOKEN_RPAREN);
assert_token_name("_HELLO1");
assert_token(TOKEN_COMMA);
assert_token_int(234);
assert_token(TOKEN_ADD);
assert_token_int(994);
assert_token_eof();
}
#undef assert_token
#undef assert_token_name
#undef assert_token_int
#undef assert_token_float
#undef assert_token_str
#undef assert_token_eof