aboutsummaryrefslogtreecommitdiff
path: root/source
diff options
context:
space:
mode:
authorAdrian Kummerlaender2017-04-16 13:29:23 +0200
committerAdrian Kummerlaender2017-04-16 13:29:23 +0200
commitfe08adffcf1446dce2a7d366f7f12c90fdce7e58 (patch)
tree373ce479b39fb734e879044bd3f0ef3b1f21b605 /source
parent44f419264898f84fe5536f3ea159c18b381e6083 (diff)
downloadslang-fe08adffcf1446dce2a7d366f7f12c90fdce7e58.tar
slang-fe08adffcf1446dce2a7d366f7f12c90fdce7e58.tar.gz
slang-fe08adffcf1446dce2a7d366f7f12c90fdce7e58.tar.bz2
slang-fe08adffcf1446dce2a7d366f7f12c90fdce7e58.tar.lz
slang-fe08adffcf1446dce2a7d366f7f12c90fdce7e58.tar.xz
slang-fe08adffcf1446dce2a7d366f7f12c90fdce7e58.tar.zst
slang-fe08adffcf1446dce2a7d366f7f12c90fdce7e58.zip
Clean up state, primitives, processing distinction
Diffstat (limited to 'source')
-rw-r--r--source/machine.d41
-rw-r--r--source/primitives/core.d13
-rw-r--r--source/primitives/eval.d39
3 files changed, 41 insertions, 52 deletions
diff --git a/source/machine.d b/source/machine.d
index fe41701..6dadfd6 100644
--- a/source/machine.d
+++ b/source/machine.d
@@ -6,11 +6,14 @@ import std.variant;
import std.stdio : writeln;
import std.container.util : make;
-import primitives = primitives.eval;
-import definition = state.definition;
-
import state.stack;
+import definition = state.definition;
+import variable = state.variable;
+
+import core = primitives.core;
+import conditional = primitives.conditional;
+
void process(string value) {
auto buffer = make!(Stack!Token)(toToken(value));
@@ -33,15 +36,31 @@ private {
Stack!Token evaluate(Token token) {
try {
- if ( primitives.evaluate(token) ) {
- return primitives.result;
- } else {
- return token.visit!(
- (int value) => Stack!Token(Token(value)),
- (bool value) => Stack!Token(Token(value)),
- (string word ) => definition.get(word)
- );
+ if ( definition.handle(token) ) {
+ return Stack!Token();
+ }
+
+ if ( conditional.handle(token) ) {
+ if ( conditional.dischargeable ) {
+ return conditional.discharge;
+ } else {
+ return Stack!Token();
+ }
+ }
+
+ if ( variable.handle(token) ) {
+ return Stack!Token();
}
+
+ if ( core.handle(token) ) {
+ return Stack!Token();
+ }
+
+ return token.visit!(
+ (int ) => Stack!Token(token),
+ (bool ) => Stack!Token(token),
+ (string word) => definition.get(word)
+ );
}
catch (Exception ex) {
writeln("Error: ", ex.msg);
diff --git a/source/primitives/core.d b/source/primitives/core.d
index 75783ed..ab0b85c 100644
--- a/source/primitives/core.d
+++ b/source/primitives/core.d
@@ -1,9 +1,20 @@
module primitives.core;
import std.stdio;
+import std.variant;
import state.stack;
+bool handle(Token token) {
+ return token.visit!(
+ (int ) => false,
+ (bool ) => false,
+ (string word) => handle(word)
+ );
+}
+
+private {
+
bool handle(string word) {
switch ( word ) {
case "+" : binary_op_add; break;
@@ -24,8 +35,6 @@ bool handle(string word) {
return true;
}
-private {
-
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
deleted file mode 100644
index a18e897..0000000
--- a/source/primitives/eval.d
+++ /dev/null
@@ -1,39 +0,0 @@
-module primitives.eval;
-
-import std.variant;
-
-import state.stack;
-
-import definition = state.definition;
-import variable = state.variable;
-
-import core = primitives.core;
-import conditional = primitives.conditional;
-
-bool evaluate(Token token) {
- if ( definition.handle(token) ) {
- return true;
- }
-
- if ( conditional.handle(token) ) {
- return true;
- }
-
- if ( variable.handle(token) ) {
- return true;
- }
-
- return token.visit!(
- (int value) => false,
- (bool value) => false,
- (string word ) => core.handle(word)
- );
-}
-
-Stack!Token result() {
- if ( conditional.dischargeable ) {
- return conditional.discharge;
- } else {
- return Stack!Token();
- }
-}