aboutsummaryrefslogtreecommitdiff
path: root/source/state/variable.d
blob: 978a2fdb6528f59860eb8ba64fd2993a0c481d3e (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
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),
		(DList!int  ) => false
	);
}

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]);
	}
}

}