forked from jdah/jdh-8
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlex.c
415 lines (364 loc) · 12.4 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
#include "lex.h"
#include "asm.h"
const char TOKEN_VALUES[TK_COUNT] = {
[TK_EOL] = 0,
[TK_SYMBOL] = 0,
[TK_NUMBER] = 0,
[TK_STRING] = 0,
[TK_LPAREN] = '(',
[TK_RPAREN] = ')',
[TK_AT] = '@',
[TK_LBRACKET] = '[',
[TK_RBRACKET] = ']',
[TK_COLON] = ':',
[TK_PERCENT] = '%',
[TK_PLUS] = '+',
[TK_MINUS] = '-',
[TK_AMPERSAND] = '&',
[TK_PIPE] = '|',
[TK_NOT] = '~',
[TK_DOLLAR] = '$',
[TK_STAR] = '*',
[TK_CARET] = '^',
[TK_SLASH] = '/',
[TK_DOT] = '.',
[TK_LESS] = '<',
[TK_GREATER] = '>',
};
// retrieves data from token into buf of size n. if buf is NULL, a buffer of
// adequate size is asmalloc'd and returned. returns NULL on failure.
char *token_data(const struct Token *token, char *buf, usize n) {
assert(token->data);
if (buf == NULL) {
buf = asmalloc(token->len + 1);
} else if (n < token->len + 1) {
return NULL;
}
memcpy(buf, token->data, token->len);
buf[token->len] = '\0';
return buf;
}
// retrieves the entire line of a token into buf of size n. if buf is NULL, a
// buffer of adequate size is asmalloc'd and returned. returns NULL on failure.
char *token_line(const struct Token *token, char *buf, usize n) {
const char *end = strchr(token->line, '\n');
const usize llen =
end == NULL ?
strlen(token->line) :
((usize) (end - token->line));
if (buf == NULL) {
buf = asmalloc(llen + 1);
} else if (llen + 1 > n) {
return NULL;
}
memcpy(buf, token->line, llen);
buf[llen] = '\0';
return buf;
}
// retrieves the string data between start and end (inclusive) into buf of size
// n. if buf is NULL, a buffer of adequate size is asmalloc'd and returned.
// returns NULL on failure.
char *token_between(
const struct Token *start,
const struct Token *end,
char *buf,
usize n
) {
// count total number of tokens, inclusive
const struct Token *t = start;
usize num_tokens = 0;
while (t != end->next) {
num_tokens++;
t = t->next;
}
// count characters in each token, including whitespace/ignored chars after
char *p;
usize i = 0, j = 0, len = 0, lens[num_tokens];
t = start;
while (t != end->next) {
j = 0;
while ((p = strchr(" ,\t", t->data[t->len + j])) && *p != '\0') {
j++;
}
lens[i] = t->len + j;
len += lens[i];
i++;
t = t->next;
}
if (buf == NULL) {
buf = asmalloc(len + 1);
} else if (len + 1 > n) {
return NULL;
}
// copy into buffer
i = 0;
len = 0;
t = start;
while (t != end->next) {
memcpy(&buf[len], t->data, lens[i]);
len += lens[i];
i++;
t = t->next;
}
buf[len] = '\0';
return buf;
}
// does some preliminary parsing on symbol tokens to identify them
static void kind_symbol(struct Token *token) {
char data[256];
assert(token->kind == TK_SYMBOL);
assert(token_data(token, data, sizeof(data)));
i64 l;
if (!strtoi64(data, 0, &l)) {
token->kind = TK_NUMBER;
token->value = l;
}
}
// expands a potentially @define'd symbol
static bool expand_define(struct Context *ctx, struct Token *token) {
char data[256];
assert(token->kind == TK_SYMBOL);
assert(token_data(token, data, sizeof(data)));
struct Define *d = ctx->defines;
while (d != NULL && strcasecmp(data, d->name)) {
d = d->next;
}
if (d == NULL) {
return false;
}
asmdbg(
ctx, token,
"Expanding %s to %s in %s:%lu\n",
data, d->value, token->input->name, token->line_no
);
if (d->value) {
assert(!push_input_buffer(ctx, "(define)", d->value));
return true;
}
return false;
}
// pushes a token to the token stream, returning the latest in the stream
static struct Token *lex_push(
struct Context *ctx,
struct Token *current,
struct Token token
) {
current->next = asmalloc(sizeof(struct Token));
token.prev = current;
token.next = NULL;
token.input = ctx->input;
token.line = ctx->input->line;
token.line_no = ctx->input->line_no;
token.flags =
(ctx->ifs.size == 0 || (BUFPEEK(ctx->ifs) == IFF_SUCCESS))
? 0 : (1 << TF_IGNORE);
*current->next = token;
if (token.flags & TF_IGNORE) {
asmdbg(ctx, current->next, "Pushed ignore token");
}
return current->next;
}
// pushes a data token to the token stream, returning the latest in the stream
static struct Token *lex_data(
struct Context *ctx,
struct Token *current,
enum TokenKind kind,
const char *data,
usize len
) {
return lex_push(ctx, current, (struct Token) {
.kind = kind,
.data = data,
.len = len,
.line_index = data - ctx->input->line
});
}
// pushes a value token to the token stream, returning the latest in the stream
static struct Token *lex_value(
struct Context *ctx,
struct Token *current,
enum TokenKind value
) {
return lex_push(ctx, current, (struct Token) {
.kind = value,
.data = ctx->current,
.len = 1,
.line_index = ctx->current - ctx->input->line
});
}
// lexes a potentially escaped charater, returning its escaped value if
// applicable. otherwise just returns the character.
static char lex_potentially_escaped(struct Context *ctx, struct Token *token) {
bool escaped = *ctx->current == '\\';
asmchk(
!escaped || isprint(*(ctx->current + 1)), ctx, token,
"Expected character to escape"
);
char c = escaped ? *(ctx->current + 1) : *ctx->current;
if (escaped) {
c = escchar(c);
}
ctx->current += escaped ? 2 : 1;
return c;
}
// tokenizes whatever input is currently available in ctx, calling back into
// linecb() with the first token of every line read.
// returns a pointer to the start of the token stream, NULL if there are no
// tokens in the stream.
struct Token *lex(
struct Context *ctx,
void (*linecb)(struct Context*, struct Token*)
) {
// first/current token and start of current line
struct Token *first = asmalloc(sizeof(struct Token)),
*token = first;
// first token is NOP, should be skipped on return
*first = (struct Token) { .kind = TK_NOP };
// pushes start to the token stream if it exists
// TODO: this is kind of a garbage macro (complicated, modifies control flow,
// etc.) but I can't think of a better solution at the moment.
#define lex_symbol() \
if (start != NULL) { \
token = lex_data( \
ctx, \
token, \
TK_SYMBOL, \
start, \
ctx->current - start \
); \
start = NULL; \
if (expand_define(ctx, token)) { \
token = token->prev; \
goto next_it; \
} else { \
kind_symbol(token); \
} \
}
// start of current symbol being read
const char *start = NULL;
bool in_str = false, escaped = false;
while (ctx->current != NULL) {
// current character is escaped if prefixed with non-escaped backslash
escaped =
ctx->current != ctx->input->data &&
ctx->current[-1] == '\\' &&
(ctx->current == (ctx->input->data + 1) ||
ctx->current[-2] != '\\');
// read string literal if in_str
if (in_str) {
asmchk(*ctx->current != '\0', ctx, token, "Unfinished string");
if (!escaped && *ctx->current == '\"') {
in_str = false;
token = lex_data(
ctx, token,
TK_STRING, start + 1, ctx->current - start - 1
);
start = NULL;
}
ctx->current++;
continue;
}
switch (*ctx->current) {
case '\0':
case EOF:
lex_symbol();
pop_input(ctx);
continue;
case '\t':
case ' ':
case ',':
lex_symbol();
break;
case ';':
// EOL when comment (;) is encountered
if (escaped) {
break;
}
case '\r':
case '\n':
lex_symbol();
token = lex_value(ctx, token, TK_EOL);
// seek to character after next newline
ctx->current = strpbrk(ctx->current, "\n");
ctx->current = ctx->current ?
(ctx->current + 1) :
(ctx->current + strlen(ctx->current) + 1);
ctx->input->line = ctx->current;
// trace backwards to find line start
struct Token *line_start = token;
while (line_start->prev->kind != TK_EOL &&
line_start->prev != first) {
line_start = line_start->prev;
}
if (linecb != NULL) {
linecb(ctx, line_start);
}
line_start = NULL;
ctx->input->line_no++;
continue;
case '\"':
if (!escaped) {
lex_symbol();
start = ctx->current;
in_str = true;
break;
}
case '\'':
if (!escaped) {
lex_symbol();
const char *char_start = ctx->current;
ctx->current++;
asmchk(
isprint(*ctx->current), ctx, token,
"Expected continuation of character"
);
char v = lex_potentially_escaped(ctx, token);
asmchk(
*ctx->current == '\'', ctx, token,
"Expected end of character"
);
token = lex_push(ctx, token, (struct Token) {
.kind = TK_NUMBER,
.data = ctx->current,
.len = ctx->current - char_start + 1,
.line_index = char_start - ctx->input->line,
.value = v
});
break;
}
default:
// try to read single character tokens
if (!escaped) {
for (usize i = 0; i < TK_COUNT; i++) {
if (*ctx->current != TOKEN_VALUES[i]) {
continue;
}
lex_symbol();
token = lex_value(ctx, token, i);
start = NULL;
goto next_char;
}
}
// starting a new symbol
if (start == NULL) {
start = ctx->current;
}
// check that this character is legal in a symbol
asmchk(
isalpha(*ctx->current) ||
isdigit(*ctx->current) ||
*ctx->current == '_',
ctx,
token,
"Illegal character '%c' in symbol",
*ctx->current
);
}
next_char:
ctx->current++;
next_it:
continue;
}
// first->next to skip over initial NOP token
return first->next;
}