aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdrian Kummerlaender2017-04-16 13:17:22 +0200
committerAdrian Kummerlaender2017-04-16 13:17:22 +0200
commit44f419264898f84fe5536f3ea159c18b381e6083 (patch)
tree5fc32c362f3cb854c114b7d75260de87f654efaa
parentc06a6ade2d144cd8042392e597ead385756bcbc7 (diff)
downloadslang-44f419264898f84fe5536f3ea159c18b381e6083.tar
slang-44f419264898f84fe5536f3ea159c18b381e6083.tar.gz
slang-44f419264898f84fe5536f3ea159c18b381e6083.tar.bz2
slang-44f419264898f84fe5536f3ea159c18b381e6083.tar.lz
slang-44f419264898f84fe5536f3ea159c18b381e6083.tar.xz
slang-44f419264898f84fe5536f3ea159c18b381e6083.tar.zst
slang-44f419264898f84fe5536f3ea159c18b381e6083.zip
Extract variable management, move into `state` package
-rw-r--r--source/machine.d4
-rw-r--r--source/primitives/conditional.d2
-rw-r--r--source/primitives/core.d20
-rw-r--r--source/primitives/eval.d10
-rw-r--r--source/state/definition.d (renamed from source/base/definition.d)4
-rw-r--r--source/state/stack.d (renamed from source/base/stack.d)2
-rw-r--r--source/state/variable.d41
7 files changed, 56 insertions, 27 deletions
diff --git a/source/machine.d b/source/machine.d
index 61d20cb..fe41701 100644
--- a/source/machine.d
+++ b/source/machine.d
@@ -6,10 +6,10 @@ import std.variant;
import std.stdio : writeln;
import std.container.util : make;
-import definition = base.definition;
import primitives = primitives.eval;
+import definition = state.definition;
-import base.stack;
+import state.stack;
void process(string value) {
auto buffer = make!(Stack!Token)(toToken(value));
diff --git a/source/primitives/conditional.d b/source/primitives/conditional.d
index 5dc0fa5..fd101bd 100644
--- a/source/primitives/conditional.d
+++ b/source/primitives/conditional.d
@@ -4,7 +4,7 @@ import std.variant;
import std.typecons;
import std.container : DList;
-import base.stack;
+import state.stack;
bool handle(string word) {
switch ( word ) {
diff --git a/source/primitives/core.d b/source/primitives/core.d
index cae577b..75783ed 100644
--- a/source/primitives/core.d
+++ b/source/primitives/core.d
@@ -2,12 +2,10 @@ module primitives.core;
import std.stdio;
-import base.stack;
+import state.stack;
bool handle(string word) {
switch ( word ) {
- case "$" : binary_op_variable_bind; break;
- case "@" : unary_op_variable_resolve; break;
case "+" : binary_op_add; break;
case "*" : binary_op_multiply; break;
case "/" : binary_op_divide; break;
@@ -28,22 +26,6 @@ bool handle(string 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]);
- }
-}
-
void binary_op_add() {
int b = stack.pop.get!int;
int a = stack.pop.get!int;
diff --git a/source/primitives/eval.d b/source/primitives/eval.d
index fc5938e..a18e897 100644
--- a/source/primitives/eval.d
+++ b/source/primitives/eval.d
@@ -2,10 +2,12 @@ module primitives.eval;
import std.variant;
-import base.stack;
+import state.stack;
+
+import definition = state.definition;
+import variable = state.variable;
import core = primitives.core;
-import definition = base.definition;
import conditional = primitives.conditional;
bool evaluate(Token token) {
@@ -17,6 +19,10 @@ bool evaluate(Token token) {
return true;
}
+ if ( variable.handle(token) ) {
+ return true;
+ }
+
return token.visit!(
(int value) => false,
(bool value) => false,
diff --git a/source/base/definition.d b/source/state/definition.d
index d37ad20..ab100d5 100644
--- a/source/base/definition.d
+++ b/source/state/definition.d
@@ -1,4 +1,4 @@
-module base.definition;
+module state.definition;
import std.string;
import std.variant;
@@ -6,7 +6,7 @@ import std.typecons;
import std.container : DList;
-import base.stack;
+import state.stack;
alias Words = Stack!Token[string];
diff --git a/source/base/stack.d b/source/state/stack.d
index c6e1a59..3c6230f 100644
--- a/source/base/stack.d
+++ b/source/state/stack.d
@@ -1,4 +1,4 @@
-module base.stack;
+module state.stack;
import std.conv;
import std.string;
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]);
+ }
+}
+
+}