From 900c223ed40e36ffedce95e9a987d9a3c0309271 Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Wed, 12 Apr 2017 16:47:45 +0200 Subject: Implementent conditional primitive --- repl.d | 9 ++++++++- src/primitives.d | 27 +++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/repl.d b/repl.d index 89c3104..4938a11 100644 --- a/repl.d +++ b/repl.d @@ -12,7 +12,14 @@ static import src.definition; static import src.primitives; void process(int x) { - stack.push(x); + try { + if ( !src.primitives.evaluate(x) ) { + stack.push(x); + } + } + catch (Exception ex) { + writeln("Error: ", ex.msg); + } } void process(string word) { diff --git a/src/primitives.d b/src/primitives.d index 0be6836..f577713 100644 --- a/src/primitives.d +++ b/src/primitives.d @@ -6,8 +6,21 @@ import src.stack; import src.definition; int[string] variables; +bool drop_mode; + +bool evaluate(int value) { + return drop_mode; +} bool evaluate(string word) { + if ( drop_mode ) { + if ( word == "then" ) { + drop_mode = false; + } + + return true; + } + switch ( word ) { case "ยง": src.definition.start; @@ -25,6 +38,20 @@ bool evaluate(string word) { stack.push(variables[name]); } return true; + case "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"); + } + case "then": + drop_mode = false; + return true; case "+": int b = stack.pop.get!int; int a = stack.pop.get!int; -- cgit v1.2.3