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
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
|
#include "../include/tokenizer.h"
#include "../include/std/mem.h"
#include "../include/std/log.h"
#include "../include/std/thread.h"
#include "../include/std/scoped_allocator.h"
#include <assert.h>
#include <limits.h>
#include <stdio.h>
#include <stdarg.h>
static int isAlpha(int c) {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
}
static int isDigit(int c) {
return c >= '0' && c <= '9';
}
static int isAlphaDigit(int c) {
return isAlpha(c) || isDigit(c);
}
static int tokenizer_get_start_of_line_from_index(Tokenizer *self, int index);
static int tokenizer_get_end_of_line_from_index(Tokenizer *self, int index);
/* Returns -1 if end of multiline comment was not found */
static int tokenizer_get_end_of_multiline_comment(Tokenizer *self, int index);
int tokenizer_init(Tokenizer *self, ScopedAllocator *allocator, BufferView code, BufferView code_name, const amal_compiler_options *compiler_options) {
assert(code.size <= INT_MAX);
assert(compiler_options);
self->code = code;
self->index = 0;
self->prev_index = 0;
self->token = TOK_NONE;
self->needs_update = bool_true;
self->code_name = code_name.data ? code_name : create_buffer_view("<buffer>", 8);
self->number_is_integer = bool_false;
self->allocator = allocator;
self->compiler_options = compiler_options;
return 0;
}
static int tokenizer_get_char(Tokenizer *self) {
assert(self->index >= 0 && self->index < (int)self->code.size);
return self->code.data[self->index];
}
static Token tokenizer_skip_whitespace(Tokenizer *self) {
int c;
for(;;) {
if(self->index >= (int)self->code.size)
return TOK_END_OF_FILE;
c = self->code.data[self->index];
switch(c) {
case '\n':
case ' ':
case '\t':
break;
default:
self->prev_index = self->index;
return TOK_NONE;
}
++self->index;
}
self->prev_index = self->index;
return TOK_NONE;
}
/* Returns -1 if end of string can't be found */
static CHECK_RESULT int find_end_of_string(BufferView buf, int index) {
int c;
bool escape_quote;
escape_quote = bool_false;
for(; index < (int)buf.size; ++index) {
c = buf.data[index];
if(c == '\\')
escape_quote = !escape_quote;
else if(!escape_quote && c == '"')
return index;
else
escape_quote = bool_false;
}
return -1;
}
/* TODO: Optimize string to integer and string to float */
#define I64_OVERFLOW_ERROR -1
static CHECK_RESULT int string_to_integer_unchecked(BufferView str, i64 *result) {
int i;
i64 value;
value = 0;
for(i = 0; i < (int)str.size; ++i) {
i64 value_before;
value_before = value;
value = value * 10 + (str.data[i] - '0');
/* overflow */
if(value < value_before)
return I64_OVERFLOW_ERROR;
}
*result = value;
return 0;
}
#define F64_OVERFLOW_ERROR -1
static CHECK_RESULT int string_to_float_unchecked(BufferView str, int dot_index, f64 *result) {
int i;
f64 value_before_dot;
f64 value_after_dot;
f64 div_factor;
value_before_dot = 0.0;
for(i = 0; i < dot_index; ++i) {
f64 value_before;
value_before = value_before_dot;
value_before_dot = value_before_dot * 10.0 + (str.data[i] - '0');
/* overflow */
if(value_before_dot < value_before)
return F64_OVERFLOW_ERROR;
}
value_after_dot = 0.0;
div_factor = 1.0;
for(i = dot_index + 1; i < (int)str.size; ++i) {
value_after_dot = value_after_dot * 10.0 + (str.data[i] - '0');
div_factor *= 10.0;
/* TODO: Check mantissa overflow */
}
*result = value_before_dot + (value_after_dot / div_factor);
return 0;
}
#define SET_BINOP(_binop_type) do { *token = TOK_BINOP; self->value.binop_type = (_binop_type); } while(0)
static CHECK_RESULT int tokenizer_next(Tokenizer *self, Token *token);
static CHECK_RESULT int __tokenizer_next(Tokenizer *self, Token *token) {
Token last_token;
int c;
int result;
last_token = tokenizer_skip_whitespace(self);
if(last_token == TOK_END_OF_FILE) {
*token = TOK_END_OF_FILE;
return TOKENIZER_OK;
}
c = tokenizer_get_char(self);
if(isAlpha(c) || c == '_') {
int identifier_start;
identifier_start = self->index;
++self->index;
while(self->index < (int)self->code.size) {
c = tokenizer_get_char(self);
if(isAlphaDigit(c) || c == '_')
++self->index;
else
break;
}
self->value.identifier = create_buffer_view(self->code.data + identifier_start, self->index - identifier_start);
switch(self->value.identifier.size) {
case 2: {
if(am_memeql(self->value.identifier.data, "fn", 2)) {
*token = TOK_FN;
return TOKENIZER_OK;
} else if(am_memeql(self->value.identifier.data, "if", 2)) {
*token = TOK_IF;
return TOKENIZER_OK;
}
break;
}
case 3: {
if(am_memeql(self->value.identifier.data, "var", 3)) {
*token = TOK_VAR;
return TOKENIZER_OK;
} else if(am_memeql(self->value.identifier.data, "pub", 3)) {
*token = TOK_PUB;
return TOKENIZER_OK;
}
break;
}
case 4: {
if(am_memeql(self->value.identifier.data, "else", 4)) {
*token = TOK_ELSE;
return TOKENIZER_OK;
}
break;
}
case 5: {
if(am_memeql(self->value.identifier.data, "const", 5)) {
*token = TOK_CONST;
return TOKENIZER_OK;
} else if(am_memeql(self->value.identifier.data, "while", 5)) {
*token = TOK_WHILE;
return TOKENIZER_OK;
}
break;
}
case 6: {
if(am_memeql(self->value.identifier.data, "struct", 6)) {
*token = TOK_STRUCT;
return TOKENIZER_OK;
} else if(am_memeql(self->value.identifier.data, "extern", 6)) {
*token = TOK_EXTERN;
return TOKENIZER_OK;
}
break;
}
}
*token = TOK_IDENTIFIER;
} else if(c == '"') {
int string_end;
++self->index;
string_end = find_end_of_string(self->code, self->index);
if(string_end == -1) {
tokenizer_print_error(self, self->prev_index, "String end not found. Did you forget '\"' or did you have a mismatch of number of '\"'?");
return TOKENIZER_ERR;
}
self->value.string.data = &self->code.data[self->index];
self->value.string.size = string_end - self->index;
self->index = string_end + 1;
*token = TOK_STRING;
} else if(isDigit(c)) {
int number_start;
int dot_index;
BufferView number_str;
number_start = self->index;
dot_index = -1;
++self->index;
while(self->index < (int)self->code.size) {
c = tokenizer_get_char(self);
if(isDigit(c))
++self->index;
else if(c == '.' && dot_index == -1) {
dot_index = self->index - number_start;
++self->index;
} else {
break;
}
}
number_str = create_buffer_view(self->code.data + number_start, self->index - number_start);
if(dot_index == -1) {
int result;
result = string_to_integer_unchecked(number_str, &self->value.integer);
if(result != 0) {
/* TODO */
tokenizer_print_error(self, self->prev_index, "Integer value %.*s is too large to fit in signed 64-bit. Support for large integers is not supported yet.", number_str.size, number_str.data);
return TOKENIZER_ERR;
}
self->number_is_integer = bool_true;
} else {
int result;
result = string_to_float_unchecked(number_str, dot_index, &self->value.floating);
if(result != 0) {
/* TODO */
tokenizer_print_error(self, self->prev_index, "Float value %.*s is too large to fit in 64-bit. Support for large floating numbers is not supported yet.", number_str.size, number_str.data);
return TOKENIZER_ERR;
}
self->number_is_integer = bool_false;
}
*token = TOK_NUMBER;
} else if(c == '.') {
++self->index;
SET_BINOP(BINOP_DOT);
} else if(c == '+') {
++self->index;
SET_BINOP(BINOP_ADD);
} else if(c == '-') {
++self->index;
SET_BINOP(BINOP_SUB);
} else if(c == '*') {
++self->index;
SET_BINOP(BINOP_MUL);
} else if(c == '/') {
++self->index;
if(self->index < (int)self->code.size) {
c = tokenizer_get_char(self);
/* Single line comment */
if(c == '/') {
++self->index;
self->index = tokenizer_get_end_of_line_from_index(self, self->index);
return __tokenizer_next(self, token);
} else if(c == '*') {
++self->index;
self->index = tokenizer_get_end_of_multiline_comment(self, self->index);
if(self->index == -1) {
tokenizer_print_error(self, self->prev_index, "End of multiline comment not found");
return TOKENIZER_ERR;
}
return __tokenizer_next(self, token);
}
}
SET_BINOP(BINOP_DIV);
} else if(c == '=') {
++self->index;
if(self->index < (int)self->code.size && tokenizer_get_char(self) == '=') {
++self->index;
SET_BINOP(BINOP_EQUALS);
} else {
*token = TOK_EQUALS;
}
} else if(c == '(') {
++self->index;
*token = TOK_OPEN_PAREN;
} else if(c == ')') {
++self->index;
*token = TOK_CLOSING_PAREN;
} else if(c == ',') {
++self->index;
*token = TOK_COMMA;
} else if(c == '{') {
++self->index;
*token = TOK_OPEN_BRACE;
} else if(c == '}') {
++self->index;
*token = TOK_CLOSING_BRACE;
} else if(c == ';') {
++self->index;
*token = TOK_SEMICOLON;
} else if(c == ':') {
++self->index;
*token = TOK_COLON;
} else if(c == '@') {
const char *err_msg;
++self->index;
if(self->index + 6 >= (int)self->code.size || !am_memeql(self->code.data + self->index, "import", 6)) {
err_msg = "Expected '@import(path)'";
goto import_error;
}
self->index += 6;
result = tokenizer_next(self, &last_token);
if(result != 0 || last_token != TOK_OPEN_PAREN) {
err_msg = "Expected '(' after @import";
goto import_error;
}
result = tokenizer_next(self, &last_token);
if(result != 0 || last_token != TOK_STRING) {
err_msg = "Expected string after @import(";
goto import_error;
}
if(self->value.string.size == 0) {
err_msg = "Path in @import can't be empty";
goto import_error;
}
result = tokenizer_next(self, &last_token);
if(result != 0 || last_token != TOK_CLOSING_PAREN) {
err_msg = "Expected ')' after @import(path";
goto import_error;
}
*token = TOK_IMPORT;
return TOKENIZER_OK;
import_error:
tokenizer_print_error(self, self->prev_index, err_msg);
return TOKENIZER_ERR;
} else {
tokenizer_print_error(self, self->prev_index, "Unexpected symbol '%c'", c);
return TOKENIZER_UNEXPECTED_TOKEN;
}
return TOKENIZER_OK;
}
/* Wrapper around __tokenizer_next to store last parsed token */
int tokenizer_next(Tokenizer *self, Token *token) {
int result;
result = __tokenizer_next(self, token);
self->token = *token;
return result;
}
static usize strlen(const char *str) {
usize len;
len = 0;
while(*str != '\0') {
++len;
++str;
}
return len;
}
/*
static const char* binop_to_string(BinopType binop_type) {
switch(binop_type) {
case BINOP_DOT: return ".";
case BINOP_ADD: return "+";
case BINOP_SUB: return "-";
case BINOP_MUL: return "*";
case BINOP_DIV: return "/";
}
assert(bool_false && "binop_to_string not implemented for binop_type");
}
*/
static BufferView tokenizer_expected_token_as_string(Token token) {
const char *str;
switch(token) {
case TOK_NONE:
str = "none";
break;
case TOK_END_OF_FILE:
str = "<eof>";
break;
case TOK_IDENTIFIER:
str = "identifier";
break;
case TOK_CONST:
str = "const";
break;
case TOK_VAR:
str = "var";
break;
case TOK_STRING:
str = "string";
break;
case TOK_FN:
str = "fn";
break;
case TOK_STRUCT:
str = "struct";
break;
case TOK_EQUALS:
str = "=";
break;
case TOK_OPEN_PAREN:
str = "(";
break;
case TOK_CLOSING_PAREN:
str = ")";
break;
case TOK_COMMA:
str = ",";
break;
case TOK_OPEN_BRACE:
str = "{";
break;
case TOK_CLOSING_BRACE:
str = "}";
break;
case TOK_IMPORT:
str = "import";
break;
case TOK_NUMBER:
str = "number";
break;
case TOK_BINOP:
/* TODO: binop_to_string */
str = "binop";
break;
case TOK_SEMICOLON:
str = ";";
break;
case TOK_COLON:
str = ":";
break;
case TOK_PUB:
str = "pub";
break;
case TOK_IF:
str = "if";
break;
case TOK_ELSE:
str = "else";
break;
case TOK_WHILE:
str = "while";
break;
case TOK_EXTERN:
str = "extern";
break;
}
return create_buffer_view(str, strlen(str));
}
static BufferView tokenizer_actual_token_as_string(Tokenizer *self) {
#if 0
const char *str;
switch(self->token) {
case TOK_NONE:
str = "none";
break;
case TOK_END_OF_FILE:
str = "<eof>";
break;
case TOK_IDENTIFIER:
/*return self->value.identifier;
*/
str = "identifier";
break;
case TOK_CONST:
str = "const";
break;
case TOK_VAR:
str = "var";
break;
case TOK_STRING:
/*return self->value.string;*/
str = "string";
break;
case TOK_EQUALS:
str = "=";
break;
case TOK_OPEN_PAREN:
str = "(";
break;
case TOK_CLOSING_PAREN:
str = ")";
break;
case TOK_OPEN_BRACE:
str = "{";
break;
case TOK_CLOSING_BRACE:
str = "}";
break;
case TOK_IMPORT:
str = "import";
break;
case TOK_COMMA:
str = ",";
break;
default:
str = "Unknown token";
break;
}
return create_buffer_view(str, strlen(str));
#endif
return tokenizer_expected_token_as_string(self->token);
}
int tokenizer_accept(Tokenizer *self, Token expected_token) {
Token actual_token;
BufferView actual_token_str;
BufferView expected_token_str;
if(!self->needs_update) {
bool match;
match = (self->token == expected_token);
self->needs_update = match;
if(match)
return TOKENIZER_OK;
} else {
return_if_error(tokenizer_next(self, &actual_token));
if(actual_token == expected_token) {
self->needs_update = bool_true;
return TOKENIZER_OK;
}
}
expected_token_str = tokenizer_expected_token_as_string(expected_token);
actual_token_str = tokenizer_actual_token_as_string(self);
tokenizer_print_error(self, self->prev_index, "Expected \"%.*s\", got \"%.*s\"", expected_token_str.size, expected_token_str.data, actual_token_str.size, actual_token_str.data);
return TOKENIZER_UNEXPECTED_TOKEN;
}
int tokenizer_consume_if(Tokenizer *self, Token expected_token, bool *result) {
Token actual_token;
if(!self->needs_update) {
*result = (self->token == expected_token);
self->needs_update = *result;
return TOKENIZER_OK;
}
return_if_error(tokenizer_next(self, &actual_token));
*result = (actual_token == expected_token);
self->needs_update = *result;
return TOKENIZER_OK;
}
int tokenizer_get_start_of_line_from_index(Tokenizer *self, int index) {
char c;
while(index >= 0) {
c = self->code.data[index];
if(c == '\n' || c == '\r') {
return index + 1;
}
--index;
}
return 0;
}
int tokenizer_get_end_of_line_from_index(Tokenizer *self, int index) {
char c;
while(index < (int)self->code.size) {
c = self->code.data[index];
if(c == '\n' || c == '\r')
break;
++index;
}
return index;
}
/*
static int find_non_whitespace(const char *str, usize size) {
usize i;
for(i = 0; i < size; ++i) {
char c;
c = str[i];
if(c != ' ' && c != '\t')
return i;
}
return -1;
}
*/
int tokenizer_get_end_of_multiline_comment(Tokenizer *self, int index) {
char c;
int comment_count;
comment_count = 1;
++index;
while(index < (int)self->code.size) {
c = self->code.data[index];
if(c == '*' && self->code.data[index - 1] == '/') {
++comment_count;
} else if(c == '/' && self->code.data[index - 1] == '*') {
--comment_count;
if(comment_count == 0)
return index + 1;
}
++index;
}
return -1;
}
/* TODO: Optimize */
static int tokenizer_get_line_by_index(Tokenizer *self, int index) {
int i;
int line;
if(index < 0 || index >= (int)self->code.size)
return -1;
line = 1;
for(i = 0; i < index; ++i) {
if(self->code.data[i] == '\n')
++line;
}
return line;
}
static int max(int a, int b) {
return a > b ? a : b;
}
void tokenizer_print_error_args(Tokenizer *self, int index, const char *fmt, va_list args) {
int line;
int line_start;
int line_end;
/*int code_start;*/
int prev_column;
int i;
line = tokenizer_get_line_by_index(self, index);
line_start = tokenizer_get_start_of_line_from_index(self, index);
line_end = tokenizer_get_end_of_line_from_index(self, index);
/*code_start = find_non_whitespace(&self->code.data[line_start], line_end - line_start);
if(code_start != -1)
line_start += code_start;*/
prev_column = index - line_start;
if(self->compiler_options->error_callback) {
char buffer[2048];
int bytes_copied;
bytes_copied = 0;
bytes_copied += max(0, snprintf(buffer + bytes_copied, sizeof(buffer) - bytes_copied, "%.*s:%d:%d: error: ", (int)self->code_name.size, self->code_name.data, line, 1 + prev_column));
if(sizeof(buffer) - bytes_copied > 0) {
bytes_copied += max(0, vsnprintf(buffer + bytes_copied, sizeof(buffer) - bytes_copied, fmt, args));
}
if(sizeof(buffer) - bytes_copied > 0)
bytes_copied += max(0, snprintf(buffer + bytes_copied, sizeof(buffer) - bytes_copied, "\n%.*s\n", line_end - line_start, self->code.data + line_start));
if(sizeof(buffer) - bytes_copied > 0) {
for(i = 0; i < prev_column; ++i)
bytes_copied += max(0, snprintf(buffer + bytes_copied, sizeof(buffer) - bytes_copied, " "));
}
if(sizeof(buffer) - bytes_copied > 0)
bytes_copied += max(0, snprintf(buffer + bytes_copied, sizeof(buffer) - bytes_copied, "^\n"));
self->compiler_options->error_callback(buffer, bytes_copied, self->compiler_options->error_callback_userdata);
} else {
amal_mutex *mutex;
mutex = amal_log_get_mutex();
ignore_result_int(amal_mutex_lock(mutex, "tokenizer_print_error"));
fprintf(stderr, "\x1b[1;37m%.*s:%d:%d:\x1b[0m \x1b[1;31merror:\x1b[0m ", (int)self->code_name.size, self->code_name.data, line, 1 + prev_column);
vfprintf(stderr, fmt, args);
fprintf(stderr, "\n%.*s\n", line_end - line_start, self->code.data + line_start);
for(i = 0; i < prev_column; ++i)
fprintf(stderr, " ");
fprintf(stderr, "\x1b[1;32m^\x1b[0m\n");
ignore_result_int(amal_mutex_unlock(mutex));
}
}
void tokenizer_print_error(Tokenizer *self, int index, const char *fmt, ...) {
va_list args;
va_start(args, fmt);
tokenizer_print_error_args(self, index, fmt, args);
va_end(args);
}
void tokenizer_print_error_object(Tokenizer *self, TokenizerError *error) {
tokenizer_print_error(self, error->index, "%s", error->str);
}
TokenizerError tokenizer_create_error(Tokenizer *self, int index, const char *fmt, ...) {
TokenizerError result;
va_list args;
char buffer[1024];
int bytes_copied;
va_start(args, fmt);
bytes_copied = max(0, vsnprintf(buffer, sizeof(buffer), fmt, args));
va_end(args);
result.index = index;
result.str = NULL;
ignore_result_int(scoped_allocator_alloc(self->allocator, bytes_copied + 1, (void**)&result.str));
if(result.str && bytes_copied > 0)
am_memcpy(result.str, buffer, bytes_copied + 1);
return result;
}
int tokenizer_get_error_index(Tokenizer *self) {
return self->prev_index;
}
int tokenizer_get_code_reference_index(Tokenizer *self, const char *ref) {
if(!ref)
return -1;
return ref - self->code.data;
}
|