aboutsummaryrefslogtreecommitdiff
path: root/lib/ls_lex.c
blob: a5908dd6a816ec13963941324bd4fadb45ad4b36 (plain)
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
// This software disclaims copyright. Do what you want with it. Be gay, do
// crime. Originally written by Alexis Lockwood in 2021. Ⓐ

// --- DEPENDENCIES ------------------------------------------------------------

// This module
#include "ls_lex.h"

// Supporting modules
#include "ls_internal.h"
#include "ls_label_cache.h"

// Standard headers
#include <stdbool.h>
#include <stddef.h>
#include <inttypes.h>
#include <string.h>
#include <ctype.h>
#include <limits.h>

// --- PRIVATE MACROS ----------------------------------------------------------
// --- PRIVATE DATATYPES -------------------------------------------------------

typedef enum {
	CH_KIND_OPER,
	CH_KIND_DIGIT,
	CH_KIND_LETTER,
	CH_KIND_SPACE,
	CH_KIND_STR,
	CH_KIND_SIGIL,
	CH_KIND_REM,
	CH_KIND_COMMA,
	CH_KIND_LINESEP,
	CH_KIND_SEP,
	CH_KIND_LABEL,
	CH_KIND_KW,
	CH_KIND_END,
	CH_KIND_INVALID // must be last and no higher than 15
} ch_kind_t;

// --- PRIVATE CONSTANTS -------------------------------------------------------

// The LUT encodes ASCII values 0x20 to 0x7E. Anything below 0x20 is treated as
// whitespace (with \n specifically as a linesep), anything above 0x7F is an
// abbreviated keyword, and 0x7F (delete char) is also treated as whitespace.
//
// The LUT encoding is rather packed but the code to unpack it is quite a bit
// smaller than the actual excess LUT space would be without packing, on small
// architectures.
//
// First packing:
// Values 0x20 to 0x5F are found at position (value - 0x20)/2, with the
// even-numbered entry in the low nibble and the odd-numbered in the high
// nibble.
//
// Second packing:
// Because 0x40...0x5F and 0x60...0x7F all have the same character kind except
// 0x7F, the second range is folded onto the first. Values 0x60 to 0x7F are
// found at position (value - 0x40)/2, overlapping value-0x20.
//
// Yeah I kinda did that for fun. Sorry it's dumb

#define K(a, b) ((uint8_t)(a) | ((uint8_t)(b) << 4))
static const uint8_t _ch_kind_lut[32] = {
	K(CH_KIND_SPACE,  CH_KIND_OPER),	// space !
	K(CH_KIND_STR,    CH_KIND_SIGIL),	// "     #
	K(CH_KIND_SIGIL,  CH_KIND_SIGIL),	// $     %
	K(CH_KIND_DIGIT,  CH_KIND_REM),		// &     '
	K(CH_KIND_OPER,   CH_KIND_OPER),	// (     )
	K(CH_KIND_OPER,   CH_KIND_OPER),	// *     +
	K(CH_KIND_COMMA,  CH_KIND_OPER),	// ,     -
	K(CH_KIND_DIGIT,  CH_KIND_OPER),	// .     /
	K(CH_KIND_DIGIT,  CH_KIND_DIGIT),	// 0     1
	K(CH_KIND_DIGIT,  CH_KIND_DIGIT),	// 2     3
	K(CH_KIND_DIGIT,  CH_KIND_DIGIT),	// 4     5
	K(CH_KIND_DIGIT,  CH_KIND_DIGIT),	// 6     7
	K(CH_KIND_DIGIT,  CH_KIND_DIGIT),	// 8     9
	K(CH_KIND_LABEL,  CH_KIND_SEP),		// :     ;
	K(CH_KIND_OPER,   CH_KIND_OPER),	// <     =
	K(CH_KIND_OPER,   CH_KIND_OPER),	// >     ?

	K(CH_KIND_OPER,   CH_KIND_LETTER),	// @`    Aa
	K(CH_KIND_LETTER, CH_KIND_LETTER),	// Bb    Cc
	K(CH_KIND_LETTER, CH_KIND_LETTER),	// Dd    Ee
	K(CH_KIND_LETTER, CH_KIND_LETTER),	// Ff    Gg
	K(CH_KIND_LETTER, CH_KIND_LETTER),	// Hh    Ii
	K(CH_KIND_LETTER, CH_KIND_LETTER),	// Jj    Kk
	K(CH_KIND_LETTER, CH_KIND_LETTER),	// Ll    Mm
	K(CH_KIND_LETTER, CH_KIND_LETTER),	// Nn    Oo
	K(CH_KIND_LETTER, CH_KIND_LETTER),	// Pp    Qq
	K(CH_KIND_LETTER, CH_KIND_LETTER),	// Rr    Ss
	K(CH_KIND_LETTER, CH_KIND_LETTER),	// Tt    Uu
	K(CH_KIND_LETTER, CH_KIND_LETTER),	// Vv    Ww
	K(CH_KIND_LETTER, CH_KIND_LETTER),	// Xx    Yy
	K(CH_KIND_LETTER, CH_KIND_OPER),	// Zz    [{
	K(CH_KIND_OPER,   CH_KIND_OPER),	// \|    ]}
	K(CH_KIND_OPER ,  CH_KIND_LETTER),	// ^~    _del
};
#undef K

static const ls_uchar _ops[][2] = {
	[LS_OP_LEQ] = "<=",
	[LS_OP_GEQ] = ">=",
	[LS_OP_NEQ] = "<>",
	[LS_OP_LPAREN] = "(",
	[LS_OP_RPAREN] = ")",
	[LS_OP_MOD] = "%",
	[LS_OP_MUL] = "*",
	[LS_OP_ADD] = "+",
	[LS_OP_SUB] = "-",
	[LS_OP_DIV] = "/",
	[LS_OP_POW] = "^",
	[LS_OP_LT] = "<",
	[LS_OP_EQ] = "=",
	[LS_OP_GT] = ">",
};


// --- PRIVATE FUNCTION PROTOTYPES ---------------------------------------------

/// Identify what kind of character ch is. Handles all values of ls_uchar.
static ch_kind_t _ident_ch_kind(ls_uchar ch);

/// Advance through whitespace and comments to the beginning of the next
/// interesting token. Returns the character it stopped on.
static ls_uchar _advance_to_token(ls_t * self);

static ls_token_t _lex_oper(ls_t * self, ls_uchar ch[2]);
static ls_token_t _lex_num(ls_t * self, ls_uchar ch[2]);
static ls_token_t _lex_word(ls_t * self, ls_uchar ch[2]);
static ls_token_t _lex_str(ls_t * self, ls_uchar ch[2]);
static ls_token_t _lex_kw(ls_t * self, ls_uchar ch[2]);

// --- PUBLIC VARIABLES --------------------------------------------------------
// --- PRIVATE VARIABLES -------------------------------------------------------
// --- PUBLIC FUNCTIONS --------------------------------------------------------

ls_token_t
ls_lex(ls_t * self)
{
	ls_uchar ch[2];
	ch[0] = _advance_to_token(self);
	ch[1] = ls_fetch(self);

	switch (_ident_ch_kind(ch[0]))
	{
	case CH_KIND_OPER:
		return _lex_oper(self, ch);

	case CH_KIND_DIGIT:
		return _lex_num(self, ch);

	case CH_KIND_LETTER:
		return _lex_word(self, ch);

	case CH_KIND_STR:
		return _lex_str(self, ch);

	case CH_KIND_SIGIL:
		ls_throw_err(self, LS_SYNTAX_ERROR);

	case CH_KIND_COMMA:
		return LS_TOK_COMMA;

	case CH_KIND_LINESEP:
		// TODO this is a bad place for this, but if we're going to
		// clear this in ls_exec_line() where it belongs then we
		// need to make sure nothing eats the newlines before it sees
		// them.
		ls_get_callctx(self)->body.sctx_call.executing_if = false;
		return LS_TOK_NEWLINE;

	case CH_KIND_SEP:
		return LS_TOK_SEMICOLON;

	case CH_KIND_LABEL:
		// These are actually parsed inside digit/word parsers
		ls_throw_err(self, LS_SYNTAX_ERROR);

	case CH_KIND_KW:
		return _lex_kw(self, ch);

	case CH_KIND_END:
		return LS_TOK_NONE;

	case CH_KIND_INVALID:
	default:
		ls_throw_err(self, LS_SYNTAX_ERROR);
	}
}

void
ls_consume_to_eol(ls_t * self)
{
	for (;;) {
		ls_uchar uch = ls_fetch(self);

		if (uch == 0 || uch == '\n')
		{
			ls_get_callctx(self)->body.sctx_call.executing_if
				= false;
			return;
		}

		self->_pc++;
	}
}

void
ls_consume_to_eos(ls_t * self)
{
	for (;;) {
		ls_token_t tok = ls_lex(self);
		if (LS_TOK_EOS(tok))
			return;
	}
}

// --- PRIVATE FUNCTION DEFINITIONS --------------------------------------------

static ch_kind_t
_ident_ch_kind(ls_uchar ch)
{
	if (ch >= 0x80)
		return CH_KIND_KW;
	else if (ch == 0x7F)
		return CH_KIND_SPACE;
	else if (ch >= 0x20)
	{
		if (ch >= 0x60)
			ch -= 0x20;
		ch -= 0x20;

		uint8_t k = _ch_kind_lut[ch / 2];
		if (ch & 1)
			// this is one instruction on avr
			k = k << 4 | k >> 4;
		return k & 0xF;
	}
	else if (ch == '\n')
		return CH_KIND_LINESEP;
	else if (ch == 0)
		return CH_KIND_END;
	else
		return CH_KIND_SPACE;
}

static ls_uchar
_advance_to_token(ls_t * self)
{
	ls_uchar ch;
	ch_kind_t ch_kind;
	bool skip_rem = false;

	do {
		ch = ls_fetch(self);
		ch_kind = _ident_ch_kind(ch);
		self->_pc++;

		if (ch_kind == CH_KIND_REM)
			skip_rem = true;
		else if (ch_kind == CH_KIND_LINESEP || ch_kind == CH_KIND_END)
			skip_rem = false;
	}
	while (ch_kind == CH_KIND_SPACE || skip_rem);

	return ch;
}

static ls_token_t
_lex_oper(ls_t * self, ls_uchar ch[2])
{
	for (uint8_t i = 0; i < sizeof(_ops)/sizeof(_ops[0]); i++)
	{
		if (_ops[i][1])
		{
			if (ch[0] == _ops[i][0] && ch[1] == _ops[i][1])
			{
				self->_pc++; // consume second char
				return (ls_token_t) i;
			}
		}
		else
		{
			if (ch[0] == _ops[i][0])
				return (ls_token_t) i;
		}
	}

	return LS_TOK_INVALID;
}

static ls_token_t
_lex_num(ls_t * self, ls_uchar ch[2])
{
	int8_t radix = 10;
	ls_token_t tok = LS_TOK_NUMBER;
	ls_int_t val = 0;

	if (ch[0] == '&')
	{
		radix = 8;
		self->_pc++;
		ch[1] = (ls_uchar) toupper(ch[1]);
		if (ch[1] == 'H')
			radix = 16;
		else if (ch[1] == 'O')
			radix = 8;
		else if (ch[1] == 'B')
			radix = 2;
		else
			self->_pc--; // bare & without a letter

		ch[0] = ls_fetch(self);
		self->_pc++;
	}

	for (;;)
	{
		int8_t digit = 0;
		ch[0] = (ls_uchar) toupper(ch[0]);

		if (ch[0] >= '0' && ch[0] <= '9')
			digit = (int8_t)(ch[0] - '0');
		else if (ch[0] >= 'A' && ch[0] <= 'F')
			digit = (int8_t)(ch[0] - 'A' + 10);
		else if (ch[0] == ':')
		{
			if (val > LS_ADDR_MAX || val < 0)
				ls_throw_err(self, LS_LABEL_RANGE);

			ls_record_num_label(self, (ls_addr_t) val);
			tok = LS_TOK_NUM_LABEL;
			break;
		}
		else
		{
			self->_pc--;
			break;
		}

		ch[0] = ls_fetch(self);

		if (digit >= radix)
			ls_throw_err(self, LS_NUMBER_FORMAT);
		val = val * radix + digit;
		self->_pc++;
	}

	self->_token.number = val;
	return tok;
}

static ls_token_t
_lex_word(ls_t * self, ls_uchar ch[2])
{
	ls_token_t tok = LS_TOK_WORD;

	memset(self->_token.word, 0, sizeof(self->_token.word));
	for (size_t i = 0;; i++)
	{
		if (i < sizeof(self->_token.word) - 1 && ch[0] <= CHAR_MAX)
			self->_token.word[i] = (char) ch[0];

		ch[0] = ls_fetch(self);
		ch_kind_t ch_kind = _ident_ch_kind(ch[0]);
		self->_pc++;

		if (ch_kind == CH_KIND_LABEL)
		{
			tok = LS_TOK_STR_LABEL;
			break;
		}
		else if (ch_kind != CH_KIND_LETTER
				&& ch_kind != CH_KIND_DIGIT)
		{
			self->_pc--;
			break;
		}
	}

	ls_kw_t kw = ls_convert_kw(self->_token.word);

	if (kw != LS_NOT_A_KW)
	{
		if (tok == LS_TOK_STR_LABEL)
			ls_throw_err(self, LS_LABEL_RANGE);

		return (ls_token_t) kw;
	}
	else if (tok == LS_TOK_STR_LABEL)
		ls_record_str_label(self, self->_token.word);

	return tok;
}

static ls_token_t
_lex_str(ls_t * self, ls_uchar ch[2])
{
	// TODO: handle escapes (how?)
	self->_token.string[0] = self->_pc;
	for (;;)
	{
		ch[0] = ls_fetch(self);
		if (ch[0] == '"')
		{
			self->_token.string[1] = (ls_addr_t)(self->_pc - 1);
			break;
		}
		if (ch[0] == '\n' || ch[0] == 0)
			ls_throw_err(self, LS_MISSING_QUOTE);
		self->_pc++;
	}
	self->_pc++;

	return LS_TOK_STRING;
}

static ls_token_t
_lex_kw(ls_t * self, ls_uchar ch[2])
{
	if (ch[0] >= LS_KW_OFFSET && ch[0] < LS_MAX_KWS)
		return (ls_token_t) ch[0];
	else
		ls_throw_err(self, LS_SYNTAX_ERROR);
}