aboutsummaryrefslogtreecommitdiff
path: root/src/ls_internal.c
blob: 670b1dcdee2da740c801190a81d0aef10ee773c5 (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
// 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_internal.h"

// Supporting modules
#include "ls_kws.h"
#include "ls_expr.h"
#include "ls_lex.h"

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

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

/// Check a return code and throw if it is an error.
#define CHECK_THROW(ctx, v) do {ls_error_t _e = (v); _throw_err((ctx), _e);} \
	while (0)

#define IS_WORD_CHAR(ch) (isalpha((ch)) || (ch) == '_' || (ch) >= 0x80)
#define IS_TOK_CHAR(ch) ((ch) >= 0x80)
#define IS_NUMBER_CHAR(ch) (isdigit((ch)) || (ch) == '&')
#define IS_WHITESPACE_CHAR(ch) ((ch) == ' ' || (ch) == '\t' || (ch) == 0x7F)
#define IS_SEPARATOR_CHAR(ch) ((ch) == '\n' || (ch) == '\r' || (ch) == ';')

// --- PRIVATE DATATYPES -------------------------------------------------------
// --- PRIVATE CONSTANTS -------------------------------------------------------
// --- PRIVATE FUNCTION PROTOTYPES ---------------------------------------------
// --- PUBLIC VARIABLES --------------------------------------------------------
// --- PRIVATE VARIABLES -------------------------------------------------------
// --- PUBLIC FUNCTIONS --------------------------------------------------------

void ls_init_ctx(ls_context_t * ctx, ls_value_t * pool, size_t szpool)
{
	ctx->error = LS_OK;

	ctx->pool = pool;

	if (pool)
	{
		ctx->pool[szpool - 1].ty = LS_TY_PRISTINE;
		ctx->pool[szpool - 1].next = NULL;
		ctx->pool[szpool - 1].prev = NULL;

		for (size_t i = 0; i < szpool - 1; i++)
		{
			ctx->pool[i].ty = LS_TY_PRISTINE;
			ctx->pool[i].next = &ctx->pool[i + 1];
			ctx->pool[i].prev = NULL;
		}

		ctx->callstack = ctx->pool;
		ctx->pool = ctx->pool->next;
		*ctx->callstack = (ls_value_t) {
			.ty = LS_TY_SCTX_CALL,
			.prev = NULL,
			.next = NULL,
			.body.sctx_call = {
				.pc = LS_ADDR_NULL,
				.readptr = LS_ADDR_NULL,
			},
		};
	}

	ctx->funcs = NULL;
	ctx->labels = NULL;
	ctx->pc = 0;
	ctx->stop = false;

	for (size_t i = 0; i < LS_LABEL_CACHE_SIZE; i++)
		ctx->label_cache[i].pc = LS_ADDR_NULL;
	ctx->label_cache_i = 0;
}

ls_kw_t ls_convert_kw(char const * word)
{
	uint8_t hash = LS_KW_HASH_DEFAULT;
	for (size_t i = 0; word[i]; i += 1)
	{
		char ch = word[i];
		hash = LS_KW_HASH(hash, ch);
	}
	hash = LS_KW_HASH_FINALIZE(hash);

	uint8_t i = ls_kw_hashmap_indices[hash];

	if (i == (uint8_t) -1)
		return LS_NOT_A_KW;

	do {
		uint8_t n_kw = LS_HASHMAP_ENTRY_UNPACK_SENTINEL(
			ls_kw_hashmap[i]
		);
		LS_PROGMEM const ls_kwdef_t * kwdef = &ls_kwmap[n_kw];

		// TODO PROGMEM
		if (!strcasecmp(kwdef->name, word))
			return (ls_kw_t) n_kw + LS_KW_OFFSET;
	} while (!LS_HASHMAP_ENTRY_IS_SENTINEL(ls_kw_hashmap[i++]));

	return LS_NOT_A_KW;
}

void ls_throw_err(ls_context_t * ctx, ls_error_t e)
{
	if (e)
	{
		ctx->error = e;
		longjmp(ctx->error_jmp_buf, 1);
	}
}

ls_uchar ls_fetch_rel(ls_context_t * ctx, int offset)
{
	if (ctx->pc + offset > UINT16_MAX || ctx->pc + offset < 0)
		ls_throw_err(ctx, LS_INTERNAL_ERROR);
	return ls_fetch(ctx, (ls_addr_t)(ctx->pc + offset));
}

ls_uchar ls_fetch(ls_context_t * ctx, ls_addr_t pc)
{
	int rc = ctx->fetcher(ctx->fetcher_ctx, pc);
	if (rc == -LS_NO_MORE_PROGRAM)
		return 0;
	else if (rc > 0)
		return (ls_uchar) rc;
	else
		ls_throw_err(ctx, -rc);
	return 0;
}

ls_value_t * ls_alloc(ls_context_t * ctx)
{
	if (!ctx->pool)
		ls_throw_err(ctx, LS_OUT_OF_MEMORY);

	ls_value_t * v = ctx->pool;
	ctx->pool = v->next;
	return v;
}

void ls_free(ls_context_t * ctx, ls_value_t * v)
{
	v->ty = LS_TY_NOT_ALLOC;
	v->next = ctx->pool;
	ctx->pool = v;
}

void ls_free_val(ls_context_t * ctx, ls_value_t * v)
{
	ls_value_t * child;

	switch (v->ty)
	{
	case LS_TY_STR:
		child = v->body.str.chunk;
		break;
	case LS_TY_LIST:
		child = v->body.list.first;
		break;
	default:
		child = NULL;
		break;
	}

	ls_value_t * next;
	for (; child; child = next)
	{
		next = child->next;
		ls_free(ctx, child);
	}

	ls_free(ctx, v);
}

size_t ls_mem_avail(ls_context_t * ctx)
{
	size_t count = 0;
	ls_value_t * iter = ctx->pool;

	while (iter)
	{
		count++;
		iter = iter->next;
	}

	return count;
}

ls_value_t * ls_find_var(ls_context_t * ctx, char const * name)
{
	for (ls_value_t * scope  = ctx->callstack; scope; scope = scope->prev)
	{
		for (ls_value_t * var = scope->next; var; var = var->next)
		{
			char const * ident;
			if (var->ty == LS_TY_INT_VAR)
				ident = var->body.int_var.ident;
			else
				ident = var->body.var.ident;

			if (!strncmp(ident, name, LS_IDENT_LEN))
				return var;
		}
	}

	return NULL;
}

ls_value_t * ls_new_var(ls_context_t * ctx, char const * name)
{
	ls_value_t * var = ls_alloc(ctx);
	var->ty = LS_TY_INT_VAR;
	memset(var->body.int_var.ident, 0, LS_IDENT_LEN);
	strncpy(var->body.int_var.ident, name, LS_IDENT_LEN);
	var->body.int_var.value = 0;

	var->next = ctx->callstack->next;
	ctx->callstack->next = var;

	return var;
}

ls_int_t ls_read_int_var(ls_context_t * ctx, ls_value_t * var)
{
	if (var->ty == LS_TY_INT_VAR)
		return var->body.int_var.value;
	else
		ls_throw_err(ctx, LS_TYPE_MISMATCH);

	// Unreachable
	return 0;
}

void ls_write_int_var(ls_context_t * ctx, ls_value_t * var, ls_int_t val)
{
	if (var->ty == LS_TY_VAR)
	{
		if (var->body.var.value)
			ls_free(ctx, var->body.var.value);
		var->ty = LS_TY_INT_VAR;
	}

	if (var->ty != LS_TY_INT_VAR)
		ls_throw_err(ctx, LS_INTERNAL_ERROR);

	var->body.int_var.value = val;
}

void ls_write_var(ls_context_t * ctx, ls_value_t * var, ls_value_t * val)
{
	if (val->ty == LS_TY_INT)
	{
		ls_write_int_var(ctx, var, val->body.integer.value);
	}
	else
	{
		if (var->ty == LS_TY_INT_VAR)
		{
			var->ty = LS_TY_VAR;
			var->body.var.value = NULL;
		}

		if (var->body.var.value)
			ls_free_val(ctx, var->body.var.value);

		var->body.var.value = ls_alloc(ctx);
		memcpy(var->body.var.value, val, sizeof(ls_value_t));
	}
}

bool ls_exec_line(ls_context_t * ctx)
{
	ls_token_t tok = {.ty = LS_TOK_STATEMENT_SEP};

	while (tok.ty == LS_TOK_STATEMENT_SEP)
		ls_lex(ctx, &tok);

	if (ctx->line_trace_hook)
		ctx->line_trace_hook(ctx);

	if (tok.ty == LS_TOK_STR_LABEL)
	{
		// TODO: handle string labels
		ls_lex(ctx, &tok);
	}
	else if (tok.ty == LS_TOK_NUM_LABEL)
	{
		ls_lex(ctx, &tok);
	}

	// LET is handled here because of the slight complexity of implicit-LET
	if (tok.ty == LS_TOK_KEYWORD && tok.body.keyword_val == LS_KW_LET)
	{
		ls_lex(ctx, &tok);
		if (tok.ty != LS_TOK_WORD)
			ls_throw_err(ctx, LS_SYNTAX_ERROR);
	}

	// Now both implicit and explicit LET can be handled
	if (tok.ty == LS_TOK_WORD)
	{
		ls_value_t * var = ls_find_var(ctx, tok.body.word_val);

		ls_token_t tok_eq;
		ls_lex(ctx, &tok_eq);
		if (tok_eq.ty != LS_TOK_OPERATOR
				|| tok_eq.body.oper_val != OPER_EQ)
			ls_throw_err(ctx, LS_SYNTAX_ERROR);

		ls_value_t val;
		ls_eval_expr(ctx, &val, NULL);

		if (val.ty != LS_TY_INT)
			ls_throw_err(ctx, LS_TYPE_MISMATCH);

		if (!var)
			var = ls_new_var(ctx, tok.body.word_val);

		ls_write_int_var(ctx, var, val.body.integer.value);
	}
	else if (tok.ty == LS_TOK_KEYWORD)
	{
		ls_kwmap[tok.body.keyword_val - LS_KW_OFFSET].fun(ctx);
	}
	else if (tok.ty == LS_TOK_STATEMENT_SEP)
	{
		return true;
	}
	else if (tok.ty == LS_TOK_NONE)
	{
		return false;
	}
	else
	{
		ls_throw_err(ctx, LS_SYNTAX_ERROR);
	}

	return true;
}

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