diff options
Diffstat (limited to 'source/primitives')
-rw-r--r-- | source/primitives/conditional.d | 60 | ||||
-rw-r--r-- | source/primitives/core.d | 5 | ||||
-rw-r--r-- | source/primitives/eval.d | 4 |
3 files changed, 32 insertions, 37 deletions
diff --git a/source/primitives/conditional.d b/source/primitives/conditional.d index e5ba591..38f0050 100644 --- a/source/primitives/conditional.d +++ b/source/primitives/conditional.d @@ -10,32 +10,7 @@ Nullable!(DList!Token) buffer; bool concluded = true; bool drop_mode = false; -void capture(Token token) { - if ( !drop_mode ) { - buffer.insertBack(token); - } -} - -bool handle(Token token) { - if ( concluded && buffer.isNull ) { - return false; - } - - if ( token.type == typeid(string) ) { - switch ( *token.peek!string ) { - case "if" : eval_if; break; - case "then" : eval_then; break; - case "else" : eval_else; break; - default : capture(token); break; - } - } else { - capture(token); - } - - return true; -} - -void eval_if() { +void unary_op_if() { if ( concluded ) { buffer = DList!Token(); drop_mode = !stack.pop.get!bool; @@ -45,7 +20,7 @@ void eval_if() { } } -void eval_then() { +void n_ary_op_then() { if ( concluded ) { throw new Exception("`then` without preceding `if`"); } else { @@ -53,7 +28,7 @@ void eval_then() { } } -void eval_else() { +void n_ary_op_else() { if ( concluded ) { throw new Exception("`else` without preceding `if`"); } else { @@ -62,6 +37,35 @@ void eval_else() { } } +bool capture(Token token) { + if ( concluded && buffer.isNull ) { + return false; + } else { + if ( !drop_mode ) { + buffer.insertBack(token); + } + + return true; + } +} + +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; } diff --git a/source/primitives/core.d b/source/primitives/core.d index 45bbeac..07ed0c7 100644 --- a/source/primitives/core.d +++ b/source/primitives/core.d @@ -3,14 +3,9 @@ module primitives.core; import std.stdio; import base.stack; -import definition = base.definition; Token[string] variables; -void definition_start() { - definition.start; -} - void binary_op_variable_bind() { string name = stack.pop.get!string; Token value = stack.pop; diff --git a/source/primitives/eval.d b/source/primitives/eval.d index 0cb45b7..2ae5373 100644 --- a/source/primitives/eval.d +++ b/source/primitives/eval.d @@ -10,12 +10,8 @@ import conditional = primitives.conditional; bool evaluate_primitive(string word) { switch ( word ) { - case "ยง" : definition_start; break; case "$" : binary_op_variable_bind; break; case "@" : unary_op_variable_resolve; break; - case "if" : conditional.eval_if; break; - case "then" : conditional.eval_then; break; - case "else" : conditional.eval_else; break; case "+" : binary_op_add; break; case "*" : binary_op_multiply; break; case "/" : binary_op_divide; break; |