From 568e457d5f879633adc8a33d8c2979d563556868 Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Sat, 15 Apr 2017 18:16:39 +0200 Subject: Handle definition, conditional primitive words in respective modules --- source/primitives/conditional.d | 60 ++++++++++++++++++++++------------------- 1 file changed, 32 insertions(+), 28 deletions(-) (limited to 'source/primitives/conditional.d') 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; } -- cgit v1.2.3