From a654500cd3084cff93e4cf866a15e7977ff0cc94 Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Sat, 15 Apr 2017 18:56:35 +0200 Subject: Clearly separate handling of core, conditional and definition primitives --- source/primitives/core.d | 24 +++++++++++++++++++++++- source/primitives/eval.d | 26 ++------------------------ 2 files changed, 25 insertions(+), 25 deletions(-) (limited to 'source/primitives') diff --git a/source/primitives/core.d b/source/primitives/core.d index 07ed0c7..f2766d2 100644 --- a/source/primitives/core.d +++ b/source/primitives/core.d @@ -6,6 +6,28 @@ import base.stack; Token[string] variables; +bool handle(string word) { + switch ( word ) { + case "$" : binary_op_variable_bind; break; + case "@" : unary_op_variable_resolve; break; + case "+" : binary_op_add; break; + case "*" : binary_op_multiply; break; + case "/" : binary_op_divide; break; + case "%" : binary_op_modulo; break; + case "." : unary_op_io_print; break; + case "pop" : unary_op_stack_pop; break; + case "dup" : unary_op_stack_dup; break; + case "swp" : binary_op_stack_swp; break; + case "true" : nullary_op_value_bool(true); break; + case "false" : nullary_op_value_bool(false); break; + case "<" : binary_cond_lt; break; + case "=" : binary_cond_eq; break; + default : return false; + } + + return true; +} + void binary_op_variable_bind() { string name = stack.pop.get!string; Token value = stack.pop; @@ -91,6 +113,6 @@ void binary_cond_eq() { stack.push(a == b); } -void integral_value_bool(bool value) { +void nullary_op_value_bool(bool value) { stack.push(Token(value)); } diff --git a/source/primitives/eval.d b/source/primitives/eval.d index 2ae5373..fc5938e 100644 --- a/source/primitives/eval.d +++ b/source/primitives/eval.d @@ -3,33 +3,11 @@ module primitives.eval; import std.variant; import base.stack; -import primitives.core; +import core = primitives.core; import definition = base.definition; import conditional = primitives.conditional; -bool evaluate_primitive(string word) { - switch ( word ) { - case "$" : binary_op_variable_bind; break; - case "@" : unary_op_variable_resolve; break; - case "+" : binary_op_add; break; - case "*" : binary_op_multiply; break; - case "/" : binary_op_divide; break; - case "%" : binary_op_modulo; break; - case "." : unary_op_io_print; break; - case "pop" : unary_op_stack_pop; break; - case "dup" : unary_op_stack_dup; break; - case "swp" : binary_op_stack_swp; break; - case "true" : integral_value_bool(true); break; - case "false" : integral_value_bool(false); break; - case "<" : binary_cond_lt; break; - case "=" : binary_cond_eq; break; - default : return false; - } - - return true; -} - bool evaluate(Token token) { if ( definition.handle(token) ) { return true; @@ -42,7 +20,7 @@ bool evaluate(Token token) { return token.visit!( (int value) => false, (bool value) => false, - (string word ) => evaluate_primitive(word) + (string word ) => core.handle(word) ); } -- cgit v1.2.3