aboutsummaryrefslogtreecommitdiff
path: root/src/ls_expr.c
blob: 95a6e381db4d61600d12beb8477116de2cbbc8f6 (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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
// 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_expr.h"

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

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

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

/// Number of operators that can be stacked at once.
#define OPSTACK_N 16

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

/// Operator definition
typedef struct {
	uint8_t precedence : 6;
	bool unary : 1;
	bool right : 1; // associativity
	/// Function for two ints, or for one int (ignore lhs, for unary)
	ls_int_t (*fun_int_int)(ls_int_t lhs, ls_int_t rhs);
} ls_operdef_t;

typedef struct __packed{
	ls_t * self;
	uint8_t opstack[OPSTACK_N];
	uint8_t opstack_i;
	ls_value_t * outstack;

	uint8_t nest;
	bool allow_unary;
} ls_shuntingyard_t;

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

/// Return the top operator on the stack without popping. Returns NO_OPER if
/// empty.
static ls_oper_t _top(ls_shuntingyard_t * yard);
/// Pop the top operator off the stack and return it. Throws LS_INTERNAL_ERROR
/// if empty.
static ls_oper_t _pop(ls_shuntingyard_t * yard);
/// Push an operator to the stack, throwing LS_OUT_OF_MEMORY if it's full.
static void _push(ls_shuntingyard_t * yard, ls_oper_t oper);

/// Handle a LS_TOK_WORD. If a word is found, it is transformed into a NUMBER
/// and should be passed along to _handle_number(). (If it is not found, an
/// error is thrown)
static void _handle_word(ls_shuntingyard_t * yard, ls_token_t * tok);

/// Handle a LS_TOK_NUMBER
static void _handle_number(ls_shuntingyard_t * yard, ls_token_t const * tok);

/// Handle a LS_TOK_KEYWORD. Some keywords need to be transformed into
/// operators, others halt parsing.
///
/// @return true if parsing should halt, or false if parsing should proceed
/// to _handle_operator().
static bool _handle_keyword(ls_shuntingyard_t * yard, ls_token_t * tok);
/// Handle a LS_TOK_OPERATOR
/// @return true if parsing should halt
static bool _handle_operator(ls_shuntingyard_t * yard, ls_token_t const * tok);
/// Handle all other tokens
///
/// @return true if parsing should halt
static bool _handle_other(ls_shuntingyard_t * yard, ls_token_t const * tok);

/// Pop one operator off the operator stack and apply it to the output stack.
static void _pop_oper_and_apply(ls_shuntingyard_t * yard);

static ls_int_t _add_int_int(ls_int_t lhs, ls_int_t rhs);
static ls_int_t _sub_int_int(ls_int_t lhs, ls_int_t rhs);
static ls_int_t _mul_int_int(ls_int_t lhs, ls_int_t rhs);
static ls_int_t _div_int_int(ls_int_t lhs, ls_int_t rhs);
static ls_int_t _mod_int_int(ls_int_t lhs, ls_int_t rhs);
static ls_int_t _abs_int(ls_int_t lhs, ls_int_t rhs);
static ls_int_t _neg_int(ls_int_t lhs, ls_int_t rhs);

// Logic
static ls_int_t _not_int(ls_int_t lhs, ls_int_t rhs);
static ls_int_t _and_int_int(ls_int_t lhs, ls_int_t rhs);
static ls_int_t _or_int_int(ls_int_t lhs, ls_int_t rhs);
static ls_int_t _xor_int_int(ls_int_t lhs, ls_int_t rhs);
static ls_int_t _eqv_int_int(ls_int_t lhs, ls_int_t rhs);
static ls_int_t _imp_int_int(ls_int_t lhs, ls_int_t rhs);

// Relational
static ls_int_t _eq_int_int(ls_int_t lhs, ls_int_t rhs);
static ls_int_t _neq_int_int(ls_int_t lhs, ls_int_t rhs);
static ls_int_t _lt_int_int(ls_int_t lhs, ls_int_t rhs);
static ls_int_t _gt_int_int(ls_int_t lhs, ls_int_t rhs);
static ls_int_t _leq_int_int(ls_int_t lhs, ls_int_t rhs);
static ls_int_t _geq_int_int(ls_int_t lhs, ls_int_t rhs);

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

static const ls_operdef_t _opers[] = {
	[OPER_ABS] = { 13, true, true, &_abs_int },
	[OPER_NOT] = { 13, true, true, &_not_int },
	[OPER_NEG] = { 13, true, true, &_neg_int },

	[OPER_MUL] = { 12, false, false, &_mul_int_int },
	[OPER_DIV] = { 12, false, false, &_div_int_int },
	[OPER_MOD] = { 12, false, false, &_mod_int_int },

	[OPER_ADD] = { 11, false, false, &_add_int_int },
	[OPER_SUB] = { 11, false, false, &_sub_int_int },

	[OPER_LT] = { 9, false, false, &_lt_int_int },
	[OPER_GT] = { 9, false, false, &_gt_int_int },
	[OPER_LEQ] = { 9, false, false, &_leq_int_int },
	[OPER_GEQ] = { 9, false, false, &_geq_int_int },

	[OPER_EQ] = { 8, false, false, &_eq_int_int },
	[OPER_NEQ] = { 8, false, false, &_neq_int_int },

	[OPER_AND] = { 7, false, false, &_and_int_int },
	[OPER_XOR] = { 7, false, false, &_xor_int_int },
	[OPER_OR] = { 7, false, false, &_or_int_int },
	[OPER_EQV] = { 7, false, false, &_eqv_int_int },
	[OPER_IMP] = { 7, false, false, &_imp_int_int },
};

// --- PUBLIC FUNCTIONS --------------------------------------------------------

void ls_eval_expr(ls_t * self, ls_value_t * val, ls_token_t * firsttok)
{
	ls_shuntingyard_t yard = {
		.self = self,
		.opstack_i = 0,
		.outstack = NULL,
		.nest = 0,
		.allow_unary = true,
	};
	bool done = false;

	ls_token_t tok;

	if (firsttok)
	{
		tok = *firsttok;
		goto have_token;
	}

	while (!done)
	{
		ls_addr_t pc_save = self->pc;

		ls_lex(self, &tok);
have_token:
		switch (tok.ty)
		{
		case LS_TOK_WORD:
			_handle_word(&yard, &tok);
			// fall through

		case LS_TOK_NUMBER:
			_handle_number(&yard, &tok);
			break;

		case LS_TOK_KEYWORD:
			if (_handle_keyword(&yard, &tok))
			{
				done = true;
				break;
			}

			// fall through
		case LS_TOK_OPERATOR:
			done = _handle_operator(&yard, &tok);
			break;

		default:
			done = _handle_other(&yard, &tok);
		}

		if (done) {
			// Don't eat the token that ended the line
			self->pc = pc_save;
		}
	}

	while (yard.opstack_i)
		_pop_oper_and_apply(&yard);

	if (!yard.outstack)
		ls_throw_err(self, LS_SYNTAX_ERROR);

	*val = *yard.outstack;

	if (yard.outstack->next)
		ls_throw_err(self, LS_SYNTAX_ERROR);

	ls_free(self, yard.outstack);
}

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

static ls_oper_t _top(ls_shuntingyard_t * yard)
{
	uint8_t i = yard->opstack_i;
	return i ? yard->opstack[i - 1] : NO_OPER;
}

static ls_oper_t _pop(ls_shuntingyard_t * yard)
{
	if (yard->opstack_i)
		return yard->opstack[--yard->opstack_i];
	else
		ls_throw_err(yard->self, LS_INTERNAL_ERROR);
	// unreachable
	return NO_OPER;
}

static void _push(ls_shuntingyard_t * yard, ls_oper_t oper)
{
	if (yard->opstack_i >= OPSTACK_N - 1)
		ls_throw_err(yard->self, LS_OUT_OF_MEMORY);
	else
		yard->opstack[yard->opstack_i++] = oper;
}

static void _pop_oper_and_apply(ls_shuntingyard_t * yard)
{
	ls_oper_t const op = _pop(yard);
	ls_operdef_t const * p_op = &_opers[op];

	if (op == OPER_LPAREN)
		return;

	ls_value_t * rhs = yard->outstack;
	if (!rhs)
		ls_throw_err(yard->self, LS_SYNTAX_ERROR);

	ls_value_t * lhs = rhs->next;
	if (!p_op->unary)
	{
		if (!lhs)
			ls_throw_err(yard->self, LS_SYNTAX_ERROR);
		yard->outstack = rhs;
		rhs->next = lhs->next;
	}

	// TODO: types
	rhs->body.integer.value = p_op->fun_int_int(
		lhs ? lhs->body.integer.value : 0,
		rhs->body.integer.value
	);

	if (!p_op->unary)
		ls_free(yard->self, lhs);
}

static void _handle_number(ls_shuntingyard_t * yard, ls_token_t const * tok)
{
	ls_value_t * v = ls_alloc(yard->self);
	v->ty = LS_TY_INT;
	v->body.integer.value = tok->body.number_val;
	v->prev = NULL;
	v->next = yard->outstack;
	yard->outstack = v;
	yard->allow_unary = false;
}

static void _handle_word(ls_shuntingyard_t * yard, ls_token_t * tok)
{
	ls_value_t * var = ls_find_var(yard->self, tok->body.word_val);

	if (!var)
		ls_throw_err(yard->self, LS_UNDEFINED_VARIABLE);

	// TODO: types
	tok->ty = LS_TOK_NUMBER;
	tok->body.number_val = ls_read_int_var(yard->self, var);
}

static bool _handle_keyword(ls_shuntingyard_t * yard, ls_token_t * tok)
{
	(void) yard;
	tok->ty = LS_TOK_OPERATOR;

	switch (tok->body.keyword_val)
	{
	case LS_KW_ABS:	tok->body.oper_val = OPER_ABS; break;
	case LS_KW_NOT:	tok->body.oper_val = OPER_NOT; break;
	case LS_KW_AND:	tok->body.oper_val = OPER_AND; break;
	case LS_KW_XOR:	tok->body.oper_val = OPER_XOR; break;
	case LS_KW_OR:	tok->body.oper_val = OPER_OR; break;
	case LS_KW_EQV:	tok->body.oper_val = OPER_EQV; break;
	case LS_KW_IMP:	tok->body.oper_val = OPER_IMP; break;
	default:
		return true;
	}

	return false;
}

static bool _handle_operator(ls_shuntingyard_t * yard, ls_token_t const * tok)
{
	ls_oper_t op = tok->body.oper_val;

	if (yard->allow_unary)
	{
		if (op == OPER_SUB)
			op = OPER_NEG;
	}

	if (op == OPER_RPAREN)
	{
		// This is likely an enclosing paren for something else
		// (e.g. function call syntax in GOSUB).
		if (yard->nest == 0)
			return true;

		while (_top(yard) != OPER_LPAREN)
			_pop_oper_and_apply(yard);
		_pop(yard);
		--yard->nest;
		yard->allow_unary = false;
	}
	else if (op == OPER_LPAREN)
	{
		_push(yard, op);
		++yard->nest;
		yard->allow_unary = true;
	}
	else
	{
		ls_operdef_t const * p_op = &_opers[op];
		uint8_t const prec = p_op->precedence;

		while (_top(yard) != NO_OPER)
		{
			ls_operdef_t const *p_top = &_opers[_top(yard)];
			uint8_t const tprec = p_top->precedence;

			bool apply = p_op->right
				? prec <  tprec
				: prec <= tprec;

			if (apply)
				_pop_oper_and_apply(yard);
			else
				break;
		}

		_push(yard, op);
		yard->allow_unary = true;
	}

	return false;
}

static bool _handle_other(ls_shuntingyard_t * yard, ls_token_t const * tok)
{
	switch (tok->ty)
	{
	case LS_TOK_COMMA:
		yard->allow_unary = true;
		if (!yard->nest)
			return true;
		break;

	case LS_TOK_STATEMENT_SEP:
	case LS_TOK_NONE:
		if (yard->nest)
			ls_throw_err(yard->self, LS_SYNTAX_ERROR);
		else
			return true;
		break;

	default:
		ls_throw_err(yard->self, LS_SYNTAX_ERROR);
	}

	return false;
}

static ls_int_t _add_int_int(ls_int_t lhs, ls_int_t rhs)
{
	return lhs + rhs;
}

static ls_int_t _sub_int_int(ls_int_t lhs, ls_int_t rhs)
{
	return lhs - rhs;
}

static ls_int_t _mul_int_int(ls_int_t lhs, ls_int_t rhs)
{
	return lhs * rhs;
}

static ls_int_t _div_int_int(ls_int_t lhs, ls_int_t rhs)
{
	return lhs / rhs;
}

static ls_int_t _mod_int_int(ls_int_t lhs, ls_int_t rhs)
{
	return lhs % rhs;
}

static ls_int_t _abs_int(ls_int_t lhs, ls_int_t rhs)
{
	(void) lhs;
	return rhs < 0 ? -rhs : rhs;
}

static ls_int_t _neg_int(ls_int_t lhs, ls_int_t rhs)
{
	(void) lhs;
	return -rhs;
}

static ls_int_t _not_int(ls_int_t lhs, ls_int_t rhs)
{
	(void) lhs;
	return ~rhs;
}

static ls_int_t _and_int_int(ls_int_t lhs, ls_int_t rhs)
{
	return lhs & rhs;
}

static ls_int_t _or_int_int(ls_int_t lhs, ls_int_t rhs)
{
	return lhs | rhs;
}

static ls_int_t _xor_int_int(ls_int_t lhs, ls_int_t rhs)
{
	return lhs ^ rhs;
}

static ls_int_t _eqv_int_int(ls_int_t lhs, ls_int_t rhs)
{
	return ~(lhs ^ rhs);
}

static ls_int_t _imp_int_int(ls_int_t lhs, ls_int_t rhs)
{
	return ~(lhs & ~rhs);
}

static ls_int_t _eq_int_int(ls_int_t lhs, ls_int_t rhs)
{
	return lhs == rhs;
}

static ls_int_t _neq_int_int(ls_int_t lhs, ls_int_t rhs)
{
	return lhs != rhs;
}

static ls_int_t _lt_int_int(ls_int_t lhs, ls_int_t rhs)
{
	return lhs < rhs;
}

static ls_int_t _gt_int_int(ls_int_t lhs, ls_int_t rhs)
{
	return lhs > rhs;
}

static ls_int_t _leq_int_int(ls_int_t lhs, ls_int_t rhs)
{
	return lhs <= rhs;
}

static ls_int_t _geq_int_int(ls_int_t lhs, ls_int_t rhs)
{
	return lhs >= rhs;
}