aboutsummaryrefslogtreecommitdiff
path: root/src/primitives/eval.d
blob: 08d8fb9879e6085cfcf97f616c82505e6851ea40 (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
module src.primitives.eval;

import std.variant;

import src.stack;
import src.primitives.core;
import conditional = src.primitives.conditional;

bool evaluate_primitive(string word) {
	switch ( word ) {
		case     "§"     : definition_start;           break;
		case     "$"     : binary_op_variable_bind;    break;
		case     "@"     : unary_op_variable_resolve;  break;
		case     "if"    : conditional.eval_if;        break;
		case     "then"  : conditional.eval_then;      break;
		case     "else"  : conditional.eval_else;      break;
		case     "+"     : binary_op_add;              break;
		case     "*"     : binary_op_multiply;         break;
		case     "/"     : binary_op_divide;           break;
		case     "%"     : binary_op_modulo;           break;
		case     "."     : unary_op_io_print;          break;
		case     "pop"   : unary_op_stack_pop;         break;
		case     "dup"   : unary_op_stack_dup;         break;
		case     "swp"   : binary_op_stack_swp;        break;
		case     "true"  : integral_value_bool(true);  break;
		case     "false" : integral_value_bool(false); break;
		case     "<"     : binary_cond_lt;             break;
		case     "="     : binary_cond_eq;             break;
		default          : return false;
	}

	return true;
}

bool evaluate(Token token) {
	if ( conditional.drop(token) ) {
		return true;
	} else {
		return token.visit!(
			(int    value) => false,
			(bool   value) => false,
			(string word ) => evaluate_primitive(word)
		);
	}
}

Stack!Token result() {
	if ( conditional.dischargeable ) {
		return conditional.discharge;
	} else {
		return Stack!Token();
	}
}