From d5ea77e49e30ad751678f90123f891344642a36c Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Wed, 12 Apr 2017 21:55:26 +0200 Subject: Introduce native boolean type --- src/primitives/eval.d | 8 ++++++++ src/primitives/impl.d | 27 ++++++++++++--------------- 2 files changed, 20 insertions(+), 15 deletions(-) (limited to 'src/primitives') diff --git a/src/primitives/eval.d b/src/primitives/eval.d index 533f491..d62fdb8 100644 --- a/src/primitives/eval.d +++ b/src/primitives/eval.d @@ -6,6 +6,10 @@ bool evaluate(int value) { return drop_mode; } +bool evaluate(bool value) { + return drop_mode; +} + bool evaluate(string word) { if ( drop_mode ) { switch ( word ) { @@ -47,6 +51,10 @@ bool evaluate(string word) { return unary_op_stack_dup; case "swp": return binary_op_stack_swp; + case "true": + return integral_value_bool(true); + case "false": + return integral_value_bool(false); case "<": return binary_cond_lt; case "=": diff --git a/src/primitives/impl.d b/src/primitives/impl.d index cac3e64..dd257cc 100644 --- a/src/primitives/impl.d +++ b/src/primitives/impl.d @@ -5,8 +5,8 @@ import std.stdio; import src.stack; import src.definition; -int[string] variables; -bool drop_mode; +Token[string] variables; +bool drop_mode; bool definition_start() { src.definition.start; @@ -15,7 +15,7 @@ bool definition_start() { bool binary_op_variable_bind() { string name = stack.pop.get!string; - int value = stack.pop.get!int; + Token value = stack.pop; variables[name] = value; return true; @@ -32,16 +32,8 @@ bool unary_op_variable_resolve() { } bool conditional_if() { - switch ( stack.pop.get!int ) { - case 0: - drop_mode = true; - return true; - case 1: - drop_mode = false; - return true; - default: - throw new Exception("invalid logic value"); - } + drop_mode = !stack.pop.get!bool; + return true; } bool conditional_then() { @@ -132,9 +124,14 @@ bool binary_cond_lt() { } bool binary_cond_eq() { - int b = stack.pop.get!int; - int a = stack.pop.get!int; + auto b = stack.pop; + auto a = stack.pop; stack.push(a == b); return true; } + +bool integral_value_bool(bool value) { + stack.push(Token(value)); + return true; +} -- cgit v1.2.3