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

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

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

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

// --- PRIVATE MACROS ----------------------------------------------------------
// --- PRIVATE DATATYPES -------------------------------------------------------
// --- PRIVATE CONSTANTS -------------------------------------------------------
// --- PRIVATE FUNCTION PROTOTYPES ---------------------------------------------

/// Call for keywords without implementations.
static void _no_impl(ls_t * self);

static void _consume_to_eol(ls_t * self);

void ls_kw_fun_GOTO(ls_t * self);

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

void ls_kw_fun_ABS(ls_t * self) { _no_impl(self); }
void ls_kw_fun_AND(ls_t * self) { _no_impl(self); }
void ls_kw_fun_AS(ls_t * self) { _no_impl(self); }
void ls_kw_fun_ASC(ls_t * self) { _no_impl(self); }
void ls_kw_fun_AT(ls_t * self) { _no_impl(self); }
void ls_kw_fun_ATN(ls_t * self) { _no_impl(self); }
void ls_kw_fun_CALL(ls_t * self) { _no_impl(self); }
void ls_kw_fun_CAT(ls_t * self) { _no_impl(self); }
void ls_kw_fun_CHR(ls_t * self) { _no_impl(self); }
void ls_kw_fun_CLOSE(ls_t * self) { _no_impl(self); }
void ls_kw_fun_COS(ls_t * self) { _no_impl(self); }
void ls_kw_fun_COUNT(ls_t * self) { _no_impl(self); }
void ls_kw_fun_DATA(ls_t * self) { _no_impl(self); }
void ls_kw_fun_DEC(ls_t * self) { _no_impl(self); }
void ls_kw_fun_DEF(ls_t * self) { _no_impl(self); }

void ls_kw_fun_END(ls_t * self) { ls_throw_err(self, LS_STOPPED); }

void ls_kw_fun_EQV(ls_t * self) { _no_impl(self); }
void ls_kw_fun_ERASE(ls_t * self) { _no_impl(self); }
void ls_kw_fun_ERROR(ls_t * self) { _no_impl(self); }
void ls_kw_fun_FN(ls_t * self) { _no_impl(self); }

void ls_kw_fun_FOR(ls_t * self)
{
	// FOR ident = a TO b [STEP c]
	ls_value_t * ctx = ls_alloc(self);
	*ctx = (ls_value_t) {
		.ty = LS_TY_SCTX_FOR,
		.body.sctx_for = {
			.term = 0,
			.step = 1,
			.for_pc = LS_ADDR_NULL,
		},
		.prev = self->_callstack,
		.next = NULL,
	};
	self->_callstack = ctx;

	ls_value_t * iterator = ls_alloc(self);
	*iterator = (ls_value_t) {
		.ty = LS_TY_INT_VAR,
		.body.int_var = {{0}},
	};

	ctx->next = iterator;

	// FOR ident = a TO b [STEP c]
	if (ls_lex(self) != LS_TOK_WORD)
		ls_throw_err(self, LS_SYNTAX_ERROR);

	strncpy(iterator->body.int_var.ident, self->_token.word, LS_IDENT_LEN);

	if (ls_lex(self) != LS_OP_EQ)
		ls_throw_err(self, LS_SYNTAX_ERROR);

	ls_value_t val;
	ls_eval_expr(self, &val, LS_TOK_NONE);
	if (val.ty != LS_TY_INT)
		ls_throw_err(self, LS_SYNTAX_ERROR);
	iterator->body.int_var.value = val.body.integer.value;

	if (ls_lex(self) != LS_KW_TO)
		ls_throw_err(self, LS_SYNTAX_ERROR);

	ls_eval_expr(self, &val, LS_TOK_NONE);
	if (val.ty != LS_TY_INT)
		ls_throw_err(self, LS_SYNTAX_ERROR);
	ctx->body.sctx_for.term = val.body.integer.value;

	ls_token_t tok = ls_lex(self);

	if (tok == LS_KW_STEP)
	{
		ls_eval_expr(self, &val, LS_TOK_NONE);
		if (val.ty != LS_TY_INT)
			ls_throw_err(self, LS_SYNTAX_ERROR);

		if (val.body.integer.value > INT16_MAX ||
			val.body.integer.value < INT16_MIN)
			ls_throw_err(self, LS_FOR_STEP_TOO_LARGE);

		ctx->body.sctx_for.step = (int16_t) val.body.integer.value;
		tok = ls_lex(self);
	}

	if (tok == LS_TOK_STATEMENT_SEP)
		ctx->body.sctx_for.for_pc = self->_pc;
	else
		ls_throw_err(self, LS_SYNTAX_ERROR);
}

void ls_kw_fun_HEX(ls_t * self) { _no_impl(self); }

void ls_kw_fun_IF(ls_t * self)
{
	// TODO how to support ELSE - the current parsing method doesn't
	// really allow it. Should we bother?
	ls_value_t cond;
	ls_eval_expr(self, &cond, LS_TOK_NONE);

	if (cond.ty != LS_TY_INT)
		ls_throw_err(self, LS_SYNTAX_ERROR);

	if (cond.body.integer.value == 0)
	{
		_consume_to_eol(self);
		return;
	}
	else
	{
		ls_token_t tok = ls_lex(self);

		if (tok == LS_KW_GOTO)
			ls_kw_fun_GOTO(self);
		else if (tok == LS_KW_THEN)
			// TODO (THEN) - need to factor something out of
			// ls_exec_line
			_no_impl(self);
		else
			ls_throw_err(self, LS_SYNTAX_ERROR);
	}
}

void ls_kw_fun_IMP(ls_t * self) { _no_impl(self); }
void ls_kw_fun_INPUT(ls_t * self) { _no_impl(self); }
void ls_kw_fun_LEFT(ls_t * self) { _no_impl(self); }
void ls_kw_fun_LET(ls_t * self) { _no_impl(self); }
void ls_kw_fun_LOG(ls_t * self) { _no_impl(self); }
void ls_kw_fun_MID(ls_t * self) { _no_impl(self); }

void ls_kw_fun_NEXT(ls_t * self)
{
	if (self->_callstack->ty != LS_TY_SCTX_FOR)
		ls_throw_err(self, LS_FOR_NEXT_MISMATCH);

	ls_value_t * ctx = self->_callstack;
	ls_value_t * iterator = ctx->next;
	ls_int_t itval = iterator->body.int_var.value;
	ls_int_t term = ctx->body.sctx_for.term;
	ls_int_t step = (ls_int_t) ctx->body.sctx_for.step;

	bool done = false;

	if (step < 0)
	{
		if (itval <= term || (itval + step < term))
			done = true;
	}
	else
	{
		if (itval >= term || (itval + step > term))
			done = true;
	}

	if (done)
	{
		self->_callstack = ctx->prev;
		ls_free_val(self, ctx);
		_consume_to_eol(self);
	}
	else
	{
		self->_pc = ctx->body.sctx_for.for_pc;
		itval += step;
		iterator->body.int_var.value = itval;
	}
}

void ls_kw_fun_NOT(ls_t * self) { _no_impl(self); }
void ls_kw_fun_OCT(ls_t * self) { _no_impl(self); }
void ls_kw_fun_ON(ls_t * self) { _no_impl(self); }
void ls_kw_fun_OR(ls_t * self) { _no_impl(self); }
void ls_kw_fun_OPEN(ls_t * self) { _no_impl(self); }
void ls_kw_fun_PACK(ls_t * self) { _no_impl(self); }
void ls_kw_fun_RANDOMIZE(ls_t * self) { _no_impl(self); }
void ls_kw_fun_READ(ls_t * self) { _no_impl(self); }

void ls_kw_fun_REM(ls_t * self)
{
	_consume_to_eol(self);
}

void ls_kw_fun_RESTORE(ls_t * self) { _no_impl(self); }
void ls_kw_fun_RIGHT(ls_t * self) { _no_impl(self); }
void ls_kw_fun_RND(ls_t * self) { _no_impl(self); }
void ls_kw_fun_SIN(ls_t * self) { _no_impl(self); }
void ls_kw_fun_SQR(ls_t * self) { _no_impl(self); }
void ls_kw_fun_STEP(ls_t * self) { _no_impl(self); }
void ls_kw_fun_SWAP(ls_t * self) { _no_impl(self); }
void ls_kw_fun_TAN(ls_t * self) { _no_impl(self); }
void ls_kw_fun_THEN(ls_t * self) { _no_impl(self); }
void ls_kw_fun_TO(ls_t * self) { _no_impl(self); }
void ls_kw_fun_UNPACK(ls_t * self) { _no_impl(self); }
void ls_kw_fun_UNTIL(ls_t * self) { _no_impl(self); }
void ls_kw_fun_VAL(ls_t * self) { _no_impl(self); }

void ls_kw_fun_WEND(ls_t * self)
{
	if (self->_callstack->ty != LS_TY_SCTX_WHILE)
		ls_throw_err(self, LS_WHILE_WEND_MISMATCH);

	ls_addr_t pc_wend = self->_pc;
	self->_pc = self->_callstack->body.sctx_while.while_pc;

	ls_value_t cond;
	ls_eval_expr(self, &cond, LS_TOK_NONE);

	if (cond.ty != LS_TY_INT)
		ls_throw_err(self, LS_SYNTAX_ERROR);

	if (cond.body.integer.value == 0)
	{
		ls_value_t * ctx = self->_callstack;
		self->_callstack = ctx->prev;
		ls_free_val(self, ctx);
		self->_pc = pc_wend;
	}
}

void ls_kw_fun_WHILE(ls_t * self)
{
	ls_addr_t pc_cond = self->_pc;

	ls_value_t cond;
	ls_eval_expr(self, &cond, LS_TOK_NONE);

	if (cond.ty != LS_TY_INT)
		ls_throw_err(self, LS_SYNTAX_ERROR);

	if (cond.body.integer.value != 0)
	{
		ls_value_t * ctx = ls_alloc(self);
		*ctx = (ls_value_t) {
			.ty = LS_TY_SCTX_WHILE,
			.body.sctx_while = {
				.while_pc = pc_cond,
			},
			.prev = self->_callstack,
			.next = NULL,
		};
		self->_callstack = ctx;
	}
	else
	{
		bool keyword_allowed = false;
		ls_token_t tok = LS_TOK_NUMBER;
		int8_t nest = 0;

		while (tok != LS_TOK_NONE)
		{
			tok = ls_lex(self);

			if (keyword_allowed)
			{
				if (tok == LS_KW_WHILE)
					nest++;
				else if (tok == LS_KW_WEND)
					if (nest)
						nest--;
					else
						break;
				else if (tok == LS_KW_REM)
				{
					_consume_to_eol(self);
					continue; // keep keyword true
				}
			}

			keyword_allowed =
				tok == LS_TOK_STR_LABEL ||
				tok == LS_TOK_NUM_LABEL ||
				tok == LS_TOK_STATEMENT_SEP;
		}

		if (tok == LS_TOK_NONE)
		{
			self->_pc = pc_cond;
			ls_throw_err(self, LS_WHILE_WEND_MISMATCH);
		}
	}
}

void ls_kw_fun_WRITE(ls_t * self) { _no_impl(self); }
void ls_kw_fun_XOR(ls_t * self) { _no_impl(self); }

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

static void _no_impl(ls_t * self)
{
	ls_throw_err(self, LS_BAD_KEYWORD);
}

static void _consume_to_eol(ls_t * self)
{
	for (;;) {
		ls_uchar uch = ls_fetch_rel(self, 0);

		if (uch == 0 || uch == '\n')
			return;

		self->_pc++;
	}
}