aboutsummaryrefslogtreecommitdiff
path: root/source/state/variable.d
blob: f77e207538b35b4e1b7125175d0f3c37d8244b87 (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
module state.variable;

import std.variant;

import state.stack;

bool handle(string word) {
	switch ( word ) {
		case "$" : binary_op_variable_bind;   return true;
		case "@" : unary_op_variable_resolve; return true;
		default  :                            return false;
	}
}

bool handle(Token token) {
	return token.visit!(
		(int        ) => false,
		(bool       ) => false,
		(string word) => handle(word)
	);
}

private {

Token[string] variables;

void binary_op_variable_bind() {
	string name     = stack.pop.get!string;
	Token  value    = stack.pop;
	variables[name] = value;
}

void unary_op_variable_resolve() {
	string name = stack.pop.get!string;

	if ( name in variables ) {
		stack.push(variables[name]);
	}
}

}