From 47611c4938363346be414b1cf00a63b438e043c4 Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Sat, 15 Apr 2017 14:02:50 +0200 Subject: Perform custom word definition at the same level as conditional primitives --- source/app.d | 21 +++++++++------------ source/base/stack.d | 6 +----- source/primitives/conditional.d | 2 +- source/primitives/eval.d | 20 +++++++++++++------- 4 files changed, 24 insertions(+), 25 deletions(-) diff --git a/source/app.d b/source/app.d index 7d25f7f..0f17afc 100644 --- a/source/app.d +++ b/source/app.d @@ -9,7 +9,7 @@ import base.stack; import definition = base.definition; import primitives = primitives.eval; -Stack!Token resolve(Token token) { +Stack!Token evaluate(Token token) { try { if ( primitives.evaluate(token) ) { return primitives.result; @@ -31,17 +31,14 @@ void process(string value) { auto buffer = make!(Stack!Token)(toToken(value)); do { - Token current = buffer.pop; - - if ( !definition.handle(current) ) { - Stack!Token resolved = resolve(current); - - if ( !resolved.empty ) { - if ( resolved.front == current ) { - stack.push(current); - } else { - buffer.insertFront(resolved[]); - } + Token current = buffer.pop; + Stack!Token result = evaluate(current); + + if ( !result.empty ) { + if ( result.front == current ) { + stack.push(current); + } else { + buffer.insertFront(result[]); } } } diff --git a/source/base/stack.d b/source/base/stack.d index 253bac0..e42ff12 100644 --- a/source/base/stack.d +++ b/source/base/stack.d @@ -5,8 +5,6 @@ import std.string; import std.variant; import std.container : SList; -import definition = base.definition; - alias Token = Algebraic!(int, bool, string); alias Stack = SList; @@ -35,9 +33,7 @@ Token pop(ref Stack!Token stack) { } void push(ref Stack!Token stack, Token token) { - if ( !definition.handle(token) ) { - stack.insertFront(token); - } + stack.insertFront(token); } template push(T) diff --git a/source/primitives/conditional.d b/source/primitives/conditional.d index 08c0f52..e5ba591 100644 --- a/source/primitives/conditional.d +++ b/source/primitives/conditional.d @@ -16,7 +16,7 @@ void capture(Token token) { } } -bool drop(Token token) { +bool handle(Token token) { if ( concluded && buffer.isNull ) { return false; } diff --git a/source/primitives/eval.d b/source/primitives/eval.d index 0ddd65a..0cb45b7 100644 --- a/source/primitives/eval.d +++ b/source/primitives/eval.d @@ -4,6 +4,8 @@ import std.variant; import base.stack; import primitives.core; + +import definition = base.definition; import conditional = primitives.conditional; bool evaluate_primitive(string word) { @@ -33,15 +35,19 @@ bool evaluate_primitive(string word) { } bool evaluate(Token token) { - if ( conditional.drop(token) ) { + if ( definition.handle(token) ) { + return true; + } + + if ( conditional.handle(token) ) { return true; - } else { - return token.visit!( - (int value) => false, - (bool value) => false, - (string word ) => evaluate_primitive(word) - ); } + + return token.visit!( + (int value) => false, + (bool value) => false, + (string word ) => evaluate_primitive(word) + ); } Stack!Token result() { -- cgit v1.2.3