aboutsummaryrefslogtreecommitdiff
path: root/lib/ls_expr.c
blob: 3c5581b333aa0f9e8d52c7c335f9a284b8f45200 (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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
// 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_opdef_t;

typedef struct {
	ls_t       *self;
	ls_value_t *outstack;
	bool       allow_unary;

	uint8_t opstack[OPSTACK_N];
	uint8_t opstack_i;

	uint8_t nest;
} ls_shuntingyard_t;

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

/// Return the top operator on the stack without popping. Returns LS_NO_OP if
/// empty.
static ls_op_t _top(ls_shuntingyard_t * yard);

/// Pop the top operator off the stack and return it. Throws LS_INTERNAL_ERROR
/// if empty.
static ls_op_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_op_t op);

/// 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);

/// Handle a LS_TOK_NUMBER
static void _handle_number(ls_shuntingyard_t * yard);

/// Handle a LS_TOK_KEYWORD(). Some keywords need to be transformed into
/// operators, others halt parsing.
///
/// @return LS_TOK_NONE if parsing should halt, or an operator if parsing
/// should proceed to _handle_operator().
static ls_token_t _handle_keyword(ls_shuntingyard_t * yard, ls_token_t tok);

/// Handle a LS_TOK_OPER()
/// @return true if parsing should halt
static bool _handle_operator(ls_shuntingyard_t * yard, ls_token_t tok);

/// Handle all other tokens
///
/// @return true if parsing should halt
static bool _handle_other(ls_shuntingyard_t * yard, ls_token_t 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 _shl_int_int(ls_int_t lhs, ls_int_t rhs);
static ls_int_t _shr_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_opdef_t _ops[] = {
	[LS_OP_ABS] = { 13, true, true, &_abs_int },
	[LS_OP_NOT] = { 13, true, true, &_not_int },
	[LS_OP_NEG] = { 13, true, true, &_neg_int },

	[LS_OP_MUL] = { 12, false, false, &_mul_int_int },
	[LS_OP_DIV] = { 12, false, false, &_div_int_int },
	[LS_OP_MOD] = { 12, false, false, &_mod_int_int },

	[LS_OP_ADD] = { 11, false, false, &_add_int_int },
	[LS_OP_SUB] = { 11, false, false, &_sub_int_int },

	[LS_OP_SHL] = { 10, false, false, &_shl_int_int },
	[LS_OP_SHR] = { 10, false, false, &_shr_int_int },

	[LS_OP_LT] = { 9, false, false, &_lt_int_int },
	[LS_OP_GT] = { 9, false, false, &_gt_int_int },
	[LS_OP_LEQ] = { 9, false, false, &_leq_int_int },
	[LS_OP_GEQ] = { 9, false, false, &_geq_int_int },

	[LS_OP_EQ] = { 8, false, false, &_eq_int_int },
	[LS_OP_NEQ] = { 8, false, false, &_neq_int_int },

	[LS_OP_AND] = { 7, false, false, &_and_int_int },
	[LS_OP_XOR] = { 7, false, false, &_xor_int_int },
	[LS_OP_OR] = { 7, false, false, &_or_int_int },
	[LS_OP_EQV] = { 7, false, false, &_eqv_int_int },
	[LS_OP_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,
	};

	ls_addr_t pc_save = self->_pc;
	bool done = false;

	if (firsttok == LS_TOK_NONE)
		firsttok = ls_lex(self);

	for (ls_token_t tok = firsttok;; tok = ls_lex(self))
	{
		switch (tok)
		{
		case LS_TOK_WORD:
			_handle_word(&yard);
			// becomes number
			// fall through

		case LS_TOK_NUMBER:
			_handle_number(&yard);
			break;

		default:
			if (LS_TOK_KEYWORD(tok))
			{
				tok = _handle_keyword(&yard, tok);

				if (tok == LS_TOK_NONE)
				{
					done = true;
					break;
				}
				// becomes operator
			}

			if (LS_TOK_OPER(tok))
				done = _handle_operator(&yard, tok);
			else
				done = _handle_other(&yard, tok);
		}

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

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

	if (!yard.outstack)
	{
		val->ty = LS_TY_NULL;
		return;
	}

	*val = *yard.outstack;

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

	ls_free(self, yard.outstack);
}

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

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

static ls_op_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);
}

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

static void
_pop_oper_and_apply(ls_shuntingyard_t * yard)
{
	ls_op_t const op = _pop(yard);
	ls_opdef_t const * p_op = &_ops[op];

	if (op == LS_OP_LPAREN)
		return;

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

	ls_value_t * lhs = rhs->next;
	if (!p_op->unary)
	{
		if (!lhs)
			ls_throw_err(yard->self, LS_INVALID_EXPR);
		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_value_t * v = ls_alloc(yard->self);
	v->ty = LS_TY_INT;
	v->body.integer.value = yard->self->_token.number;
	v->prev = NULL;
	v->next = yard->outstack;
	yard->outstack = v;
	yard->allow_unary = false;
}

static void
_handle_word(ls_shuntingyard_t * yard)
{
	ls_value_t * var = ls_find_var(yard->self, yard->self->_token.word);

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

	// TODO: types
	yard->self->_token.number = ls_read_int_var(yard->self, var);
}

static ls_token_t
_handle_keyword(ls_shuntingyard_t * yard, ls_token_t tok)
{
	(void) yard;

	uint8_t flags = ls_kwmap[tok - LS_KW_OFFSET].flags;
	if (KWDEF_FLAG_HAS_OP(flags))
		return KWDEF_FLAG_GET_OP(flags);
	else
		return LS_TOK_NONE;
}

static bool
_handle_operator(ls_shuntingyard_t * yard, ls_token_t tok)
{
	if (yard->allow_unary)
	{
		if (tok == LS_OP_SUB)
			tok = LS_OP_NEG;
	}

	if (tok == LS_OP_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) != LS_OP_LPAREN)
			_pop_oper_and_apply(yard);
		_pop(yard);
		--yard->nest;
		yard->allow_unary = false;
	}
	else if (tok == LS_OP_LPAREN)
	{
		_push(yard, tok);
		++yard->nest;
		yard->allow_unary = true;
	}
	else
	{
		ls_opdef_t const * p_op = &_ops[tok];
		uint8_t const prec = p_op->precedence;

		while (_top(yard) != LS_NO_OP)
		{
			ls_opdef_t const *p_top = &_ops[_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, tok);
		yard->allow_unary = true;
	}

	return false;
}

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

	case LS_TOK_NEWLINE:
	case LS_TOK_SEMICOLON:
	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
_shl_int_int(ls_int_t lhs, ls_int_t rhs)
{
	if (rhs < 0)
		return _shr_int_int(lhs, -rhs);
	else if (rhs > 32)
		return 0;
	else
		return lhs << rhs;
}

static ls_int_t
_shr_int_int(ls_int_t lhs, ls_int_t rhs)
{
	if (rhs < 0)
		return _shl_int_int(lhs, -rhs);
	else if (rhs > 32)
		return 0;
	else
		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;
}