aboutsummaryrefslogtreecommitdiff
path: root/source/state
diff options
context:
space:
mode:
authorAdrian Kummerlaender2017-04-16 13:17:22 +0200
committerAdrian Kummerlaender2017-04-16 13:17:22 +0200
commit44f419264898f84fe5536f3ea159c18b381e6083 (patch)
tree5fc32c362f3cb854c114b7d75260de87f654efaa /source/state
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
Diffstat (limited to 'source/state')
-rw-r--r--source/state/definition.d84
-rw-r--r--source/state/stack.d48
-rw-r--r--source/state/variable.d41
3 files changed, 173 insertions, 0 deletions
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]);
+ }
+}
+
+}