diff options
-rw-r--r-- | source/base/definition.d | 35 | ||||
-rw-r--r-- | source/primitives/conditional.d | 62 | ||||
-rw-r--r-- | source/primitives/core.d | 8 |
3 files changed, 59 insertions, 46 deletions
diff --git a/source/base/definition.d b/source/base/definition.d index 4c0f50c..d37ad20 100644 --- a/source/base/definition.d +++ b/source/base/definition.d @@ -10,8 +10,27 @@ 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; -Words words; void register(DList!Token definition) { string wordToBeDefined; @@ -62,18 +81,4 @@ bool handle(string word) { } } -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)); - } } diff --git a/source/primitives/conditional.d b/source/primitives/conditional.d index 38f0050..5dc0fa5 100644 --- a/source/primitives/conditional.d +++ b/source/primitives/conditional.d @@ -6,6 +6,39 @@ import std.container : DList; import base.stack; +bool handle(string word) { + switch ( word ) { + case "if" : unary_op_if; return true; + case "then" : n_ary_op_then; return true; + case "else" : n_ary_op_else; return true; + default : return capture(Token(word)); + } +} + +bool handle(Token token) { + return token.visit!( + (int ) => capture(token), + (bool ) => capture(token), + (string word) => handle(word) + ); +} + +bool dischargeable() { + return concluded && !buffer.isNull; +} + +Stack!Token discharge() { + if ( concluded ) { + Stack!Token result = buffer[]; + buffer.nullify; + return result; + } else { + throw new Exception("unconcluded conditional may not be discharged"); + } +} + +private { + Nullable!(DList!Token) buffer; bool concluded = true; bool drop_mode = false; @@ -49,33 +82,4 @@ bool capture(Token token) { } } -bool handle(string word) { - switch ( word ) { - case "if" : unary_op_if; return true; - case "then" : n_ary_op_then; return true; - case "else" : n_ary_op_else; return true; - default : return capture(Token(word)); - } -} - -bool handle(Token token) { - return token.visit!( - (int ) => capture(token), - (bool ) => capture(token), - (string word) => handle(word) - ); -} - -bool dischargeable() { - return concluded && !buffer.isNull; -} - -Stack!Token discharge() { - if ( concluded ) { - Stack!Token result = buffer[]; - buffer.nullify; - return result; - } else { - throw new Exception("unconcluded conditional may not be discharged"); - } } diff --git a/source/primitives/core.d b/source/primitives/core.d index f2766d2..cae577b 100644 --- a/source/primitives/core.d +++ b/source/primitives/core.d @@ -4,8 +4,6 @@ import std.stdio; import base.stack; -Token[string] variables; - bool handle(string word) { switch ( word ) { case "$" : binary_op_variable_bind; break; @@ -28,6 +26,10 @@ bool handle(string word) { return true; } +private { + +Token[string] variables; + void binary_op_variable_bind() { string name = stack.pop.get!string; Token value = stack.pop; @@ -116,3 +118,5 @@ void binary_cond_eq() { void nullary_op_value_bool(bool value) { stack.push(Token(value)); } + +} |