diff options
author | Adrian Kummerlaender | 2017-04-12 18:15:38 +0200 |
---|---|---|
committer | Adrian Kummerlaender | 2017-04-12 18:15:38 +0200 |
commit | 061db1f3810efac768dc7a83a8fbfaecfc512577 (patch) | |
tree | 3f09b618a2c52b6137f6e0d6735749aa61681216 | |
parent | d0c6994a0c3706a3eb09d32c8bf33513b1dbd3be (diff) | |
download | slang-061db1f3810efac768dc7a83a8fbfaecfc512577.tar slang-061db1f3810efac768dc7a83a8fbfaecfc512577.tar.gz slang-061db1f3810efac768dc7a83a8fbfaecfc512577.tar.bz2 slang-061db1f3810efac768dc7a83a8fbfaecfc512577.tar.lz slang-061db1f3810efac768dc7a83a8fbfaecfc512577.tar.xz slang-061db1f3810efac768dc7a83a8fbfaecfc512577.tar.zst slang-061db1f3810efac768dc7a83a8fbfaecfc512577.zip |
Expand conditional primitive to choose between `then` and `else` branch
i.e. `1 if true then false else` evaluates to `true`, `0 if true then
false else` evaluates to `false`.
-rw-r--r-- | src/primitives/eval.d | 17 | ||||
-rw-r--r-- | src/primitives/impl.d | 9 |
2 files changed, 18 insertions, 8 deletions
diff --git a/src/primitives/eval.d b/src/primitives/eval.d index 3f52e6d..533f491 100644 --- a/src/primitives/eval.d +++ b/src/primitives/eval.d @@ -8,10 +8,13 @@ bool evaluate(int value) { bool evaluate(string word) { if ( drop_mode ) { - if ( word == "then" ) { - return conditional_end; - } else { - return true; + switch ( word ) { + case "then": + return conditional_then; + case "else": + return conditional_else; + default: + return true; } } @@ -23,9 +26,11 @@ bool evaluate(string word) { case "@": return unary_op_variable_resolve; case "if": - return conditional_start; + return conditional_if; case "then": - return conditional_end; + return conditional_then; + case "else": + return conditional_else; case "+": return binary_op_add; case "*": diff --git a/src/primitives/impl.d b/src/primitives/impl.d index 56ea4ce..cac3e64 100644 --- a/src/primitives/impl.d +++ b/src/primitives/impl.d @@ -31,7 +31,7 @@ bool unary_op_variable_resolve() { return true; } -bool conditional_start() { +bool conditional_if() { switch ( stack.pop.get!int ) { case 0: drop_mode = true; @@ -44,7 +44,12 @@ bool conditional_start() { } } -bool conditional_end() { +bool conditional_then() { + drop_mode = !drop_mode; + return true; +} + +bool conditional_else() { drop_mode = false; return true; } |