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/base/definition.d | 84 ----------------------------------------- source/base/stack.d | 48 ----------------------- source/machine.d | 4 +- source/primitives/conditional.d | 2 +- source/primitives/core.d | 20 +--------- source/primitives/eval.d | 10 ++++- source/state/definition.d | 84 +++++++++++++++++++++++++++++++++++++++++ source/state/stack.d | 48 +++++++++++++++++++++++ source/state/variable.d | 41 ++++++++++++++++++++ 9 files changed, 185 insertions(+), 156 deletions(-) delete mode 100644 source/base/definition.d delete mode 100644 source/base/stack.d create mode 100644 source/state/definition.d create mode 100644 source/state/stack.d create mode 100644 source/state/variable.d (limited to 'source') diff --git a/source/base/definition.d b/source/base/definition.d deleted file mode 100644 index d37ad20..0000000 --- a/source/base/definition.d +++ /dev/null @@ -1,84 +0,0 @@ -module base.definition; - -import std.string; -import std.variant; -import std.typecons; - -import std.container : DList; - -import base.stack; - -alias Words = Stack!Token[string]; - -Words words; - -bool handle(Token token) { - return token.visit!( - (int value) => handle(value), - (bool value) => handle(value), - (string word ) => handle(word) - ); -} - -Stack!Token get(string word) { - if ( word in words ) { - return words[word].dup; - } else { - return Stack!Token(Token(word)); - } -} - -private { - -Nullable!(DList!Token) definition; - -void register(DList!Token definition) { - string wordToBeDefined; - - definition.front.visit!( - (int value) => wordToBeDefined = "", - (bool value) => wordToBeDefined = "", - (string name ) => wordToBeDefined = name - ); - - if ( wordToBeDefined == "" ) { - throw new Exception("words may not be numeric or boolean"); - } - - definition.removeFront; - words[wordToBeDefined] = Stack!Token(definition[]); -} - -template handle(T) -if ( is(T == int) || is(T == bool) ) { - bool handle(T value) { - if ( definition.isNull ) { - return false; - } else { - definition.insertBack(Token(value)); - return true; - } - } -} - -bool handle(string word) { - if ( definition.isNull ) { - if ( word == "§" ) { - definition = DList!Token(); - return true; - } else { - return false; - } - } else { - if ( word == ";" ) { - register(definition); - definition.nullify; - } else { - definition.insertBack(Token(word)); - } - - return true; - } -} - -} diff --git a/source/base/stack.d b/source/base/stack.d deleted file mode 100644 index c6e1a59..0000000 --- a/source/base/stack.d +++ /dev/null @@ -1,48 +0,0 @@ -module base.stack; - -import std.conv; -import std.string; -import std.variant; -import std.container : SList; - -alias Token = Algebraic!(int, bool, string); -alias Stack = SList; - -Stack!Token stack; - -Token toToken(string value) { - if ( value.isNumeric ) { - return Token(parse!int(value)); - } else { - return Token(value); - } -} - -Token top(ref Stack!Token stack) { - if ( stack.empty ) { - throw new Exception("stack is empty"); - } else { - return stack.front; - } -} - -Token pop(ref Stack!Token stack) { - Token token = stack.top; - stack.removeFront; - return token; -} - -template push(T) -if ( is(T == int) || is(T == bool) || is (T == string) ) { - void push(ref Stack!Token stack, T value) { - stack.push(Token(value)); - } -} - -void push(ref Stack!Token stack, Token token) { - stack.insertFront(token); -} - -void push(ref Stack!Token stack, Stack!Token prefix) { - stack.insertFront(prefix[]); -} 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/state/definition.d b/source/state/definition.d new file mode 100644 index 0000000..ab100d5 --- /dev/null +++ b/source/state/definition.d @@ -0,0 +1,84 @@ +module state.definition; + +import std.string; +import std.variant; +import std.typecons; + +import std.container : DList; + +import state.stack; + +alias Words = Stack!Token[string]; + +Words words; + +bool handle(Token token) { + return token.visit!( + (int value) => handle(value), + (bool value) => handle(value), + (string word ) => handle(word) + ); +} + +Stack!Token get(string word) { + if ( word in words ) { + return words[word].dup; + } else { + return Stack!Token(Token(word)); + } +} + +private { + +Nullable!(DList!Token) definition; + +void register(DList!Token definition) { + string wordToBeDefined; + + definition.front.visit!( + (int value) => wordToBeDefined = "", + (bool value) => wordToBeDefined = "", + (string name ) => wordToBeDefined = name + ); + + if ( wordToBeDefined == "" ) { + throw new Exception("words may not be numeric or boolean"); + } + + definition.removeFront; + words[wordToBeDefined] = Stack!Token(definition[]); +} + +template handle(T) +if ( is(T == int) || is(T == bool) ) { + bool handle(T value) { + if ( definition.isNull ) { + return false; + } else { + definition.insertBack(Token(value)); + return true; + } + } +} + +bool handle(string word) { + if ( definition.isNull ) { + if ( word == "§" ) { + definition = DList!Token(); + return true; + } else { + return false; + } + } else { + if ( word == ";" ) { + register(definition); + definition.nullify; + } else { + definition.insertBack(Token(word)); + } + + return true; + } +} + +} diff --git a/source/state/stack.d b/source/state/stack.d new file mode 100644 index 0000000..3c6230f --- /dev/null +++ b/source/state/stack.d @@ -0,0 +1,48 @@ +module state.stack; + +import std.conv; +import std.string; +import std.variant; +import std.container : SList; + +alias Token = Algebraic!(int, bool, string); +alias Stack = SList; + +Stack!Token stack; + +Token toToken(string value) { + if ( value.isNumeric ) { + return Token(parse!int(value)); + } else { + return Token(value); + } +} + +Token top(ref Stack!Token stack) { + if ( stack.empty ) { + throw new Exception("stack is empty"); + } else { + return stack.front; + } +} + +Token pop(ref Stack!Token stack) { + Token token = stack.top; + stack.removeFront; + return token; +} + +template push(T) +if ( is(T == int) || is(T == bool) || is (T == string) ) { + void push(ref Stack!Token stack, T value) { + stack.push(Token(value)); + } +} + +void push(ref Stack!Token stack, Token token) { + stack.insertFront(token); +} + +void push(ref Stack!Token stack, Stack!Token prefix) { + stack.insertFront(prefix[]); +} 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