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
|
#include "lex.h"
#include <string.h>
#include <stdio.h>
#include <math.h>
#include "dyn_arr.h"
#define advance(lex) (++lex->head, ++lex->col)
#define current_char(lex) (*lex->head)
// For printing purposes
static
char *token_names[] = {
"TOKEN_EOF",
#define TOKEN_NAME(name) "TOKEN_" #name,
XTOKENS(TOKEN_NAME)
#undef TOKEN_NAME
};
static
bool is_digit(char c)
{
return c >= '0' && c <= '9';
}
static
bool is_upper(char c)
{
return c >= 'A' && c <= 'Z';
}
static
bool is_lower(char c)
{
return c >= 'a' && c <= 'z';
}
static
bool is_ident(char c)
{
return is_lower(c) ||
is_upper(c) ||
c == '_';
}
static
bool match_kw(const struct lexer *lex, const char *b)
{
size_t alen = (size_t)(lex->head - lex->base);
size_t blen = strlen(b);
return alen == blen && memcmp(lex->base, b, alen) == 0;
}
// Advances if the given character is a match.
static
bool match_char(struct lexer *lex, char c)
{
if (current_char(lex) == c) {
advance(lex);
return true;
}
return false;
}
// Looks for a non-whitespace character and sets the start of the token to
// that. If it sees a newline, it will also update all relevant data. And
// finally, if it sees a #, it will interpret it as a comment.
static
void goto_token_start(struct lexer *lex)
{
while (true) {
switch (current_char(lex)) {
case '#':
while (
current_char(lex) != '\n' &&
current_char(lex) != '\0'
)
advance(lex);
break;
case '\n':
advance(lex);
lex->line++;
lex->col = 0;
break;
case ' ':
case '\t':
case '\r':
advance(lex);
break;
default:
lex->base = lex->head;
return;
}
}
}
static
struct token create_token(struct lexer *lex, u16 token_kind)
{
struct token tok;
tok.kind = token_kind;
tok.start = lex->base;
tok.len = (int)(lex->head - lex->base);
tok.line = lex->line;
tok.col = lex->col - tok.len;
tok.val = create_nada();
return tok;
}
static
struct token err_token(struct lexer *lex, const char *msg)
{
struct token tok;
tok.kind = TOKEN_ERR;
tok.start = msg;
tok.len = (int)strlen(msg);
tok.line = lex->line;
tok.col = lex->col;
tok.val = create_nada();
return tok;
}
static
struct token num_token(struct lexer *lex)
{
while (is_digit(current_char(lex))) {
advance(lex);
}
if (current_char(lex) == '.') {
advance(lex);
while (is_digit(current_char(lex))) {
advance(lex);
}
}
return create_token(lex, TOKEN_NUM);
}
// And ident(ifier) token is either a keyword, or an actual identifier
static
struct token ident_token(struct lexer *lex)
{
while (true) {
char c = current_char(lex);
if (c == ':') {
// If a : appears in the middle of an identifier, allow
// it
if (!is_ident(lex->head[1]))
break;
} else if (!is_ident(c) && !is_digit(c)) {
break;
}
advance(lex);
}
// : is just to be used as a namespacer. So obviously it should be
// disallowed at the beginning and end of identifiers.
if (lex->head[-1] == ':')
return err_token(lex, "cannot end an identifier in ':'");
u16 kind = TOKEN_IDENT;
if (match_kw(lex, "and"))
kind = TOKEN_AND;
else if (match_kw(lex, "break"))
kind = TOKEN_BREAK;
else if (match_kw(lex, "do"))
kind = TOKEN_DO;
else if (match_kw(lex, "else"))
kind = TOKEN_ELSE;
else if (match_kw(lex, "elseif"))
kind = TOKEN_ELSEIF;
else if (match_kw(lex, "end"))
kind = TOKEN_END;
else if (match_kw(lex, "false"))
kind = TOKEN_FALSE;
else if (match_kw(lex, "fun"))
kind = TOKEN_FUN;
else if (match_kw(lex, "global"))
kind = TOKEN_GLOBAL;
else if (match_kw(lex, "if"))
kind = TOKEN_IF;
else if (match_kw(lex, "in"))
kind = TOKEN_IN;
else if (match_kw(lex, "let"))
kind = TOKEN_LET;
else if (match_kw(lex, "loop"))
kind = TOKEN_LOOP;
else if (match_kw(lex, "nada"))
kind = TOKEN_NADA;
else if (match_kw(lex, "next"))
kind = TOKEN_NEXT;
else if (match_kw(lex, "not"))
kind = TOKEN_NOT;
else if (match_kw(lex, "or"))
kind = TOKEN_OR;
else if (match_kw(lex, "ret"))
kind = TOKEN_RET;
else if (match_kw(lex, "true"))
kind = TOKEN_TRUE;
return create_token(lex, kind);
}
static
struct token str_token(struct lexer *lex, char term)
{
// TODO: escape sequences
char *chars = da_create(char, 0);
while (current_char(lex) != term && current_char(lex) != '\0') {
if (current_char(lex) != '\\') {
da_append(char, &chars, current_char(lex));
advance(lex);
continue;
}
advance(lex);
switch (current_char(lex)) {
case '\\': da_append(char, &chars, '\\'); break;
case '\'': da_append(char, &chars, '\''); break;
case '"': da_append(char, &chars, '"'); break;
case 'n': da_append(char, &chars, '\n'); break;
case 't': da_append(char, &chars, '\t'); break;
case 'r': da_append(char, &chars, '\r'); break;
default:
da_free(chars);
return err_token(lex, "invalid escape sequence");
}
advance(lex); // eat escape
}
da_append(char, &chars, 0);
if (current_char(lex) == '\0')
return err_token(lex, "string never terminates");
advance(lex); // eat terminator
struct token str = create_token(lex, TOKEN_STR);
str.val = wrap_str(copy_str(chars, da_len(chars) - 1));
da_free(chars);
return str;
}
// A symbol token is any token that contains no alphanumeric characters.
static
struct token symbol_token(struct lexer *lex, char c)
{
switch (c) {
case '(':
case ')':
case '{':
case '}':
case '[':
case ']':
case ',':
case ';':
case ':':
return create_token(lex, c);
case '.':
return create_token(
lex,
match_char(lex, '.') ? TOKEN_DOT_DOT : c
);
case '+':
return create_token(
lex,
match_char(lex, '=') ? TOKEN_PLUS_EQL : c
);
case '-':
return create_token(
lex,
match_char(lex, '=') ? TOKEN_MINUS_EQL : c
);
case '*':
return create_token(
lex,
match_char(lex, '=') ? TOKEN_MULT_EQL : c
);
case '/':
return create_token(
lex,
match_char(lex, '=') ? TOKEN_DIV_EQL : c
);
case '%':
return create_token(
lex,
match_char(lex, '=') ? TOKEN_MOD_EQL : c
);
case '<':
return create_token(
lex,
match_char(lex, '=') ? TOKEN_LTEQL : c
);
case '>':
return create_token(
lex,
match_char(lex, '=') ? TOKEN_GTEQL : c
);
case '=':
return create_token(
lex,
match_char(lex, '=') ? TOKEN_EQL : c
);
case '!':
if (match_char(lex, '='))
return create_token(lex, TOKEN_NEQL);
break;
}
// TODO: log unknown character
return err_token(lex, "unknown character");
}
void lex_init(struct lexer *lex, const char *src)
{
lex->src = src;
lex->base = src;
lex->head = src;
lex->line = 1;
lex->col = 0;
}
struct token lex_next_token(struct lexer *lex)
{
goto_token_start(lex);
char c = current_char(lex);
if (c == '\0')
return create_token(lex, TOKEN_EOF);
advance(lex);
if (c == '"' || c == '\'')
return str_token(lex, c);
if (is_digit(c))
return num_token(lex);
if (is_ident(c))
return ident_token(lex);
return symbol_token(lex, c);
}
void token_kind_name(char *dst, size_t len, u16 kind)
{
char char_name[2] = {0, 0};
const char *name = char_name;
if (kind < TOKEN_EOF)
char_name[0] = kind;
else
name = token_names[kind - TOKEN_EOF];
len = fmin(len, strlen(name));
memcpy(dst, name, len);
dst[len] = '\0';
}
void print_token(struct token tok)
{
char kind_name[128];
token_kind_name(kind_name, 128, tok.kind);
printf(
"%-15s | %3d:%-2d | %-24.*s\n",
kind_name,
tok.line,
tok.col,
tok.len,
tok.start
);
}
|