From 44f419264898f84fe5536f3ea159c18b381e6083 Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Sun, 16 Apr 2017 13:17:22 +0200 Subject: Extract variable management, move into `state` package --- source/state/variable.d | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 source/state/variable.d (limited to 'source/state/variable.d') diff --git a/source/state/variable.d b/source/state/variable.d new file mode 100644 index 0000000..f77e207 --- /dev/null +++ b/source/state/variable.d @@ -0,0 +1,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]); + } +} + +} -- cgit v1.2.3