diff options
author | Adrian Kummerlaender | 2017-04-14 23:21:51 +0200 |
---|---|---|
committer | Adrian Kummerlaender | 2017-04-14 23:21:51 +0200 |
commit | c6d2b259a4f253403929f6f1104395a6a71b1be6 (patch) | |
tree | 6d4cdfa4ab50fbe0fa982db601d772c67e4dda20 /source/primitives/conditional.d | |
parent | 963ace2e5ba0337130e5f343d7ab97a30b4547ae (diff) | |
download | slang-c6d2b259a4f253403929f6f1104395a6a71b1be6.tar slang-c6d2b259a4f253403929f6f1104395a6a71b1be6.tar.gz slang-c6d2b259a4f253403929f6f1104395a6a71b1be6.tar.bz2 slang-c6d2b259a4f253403929f6f1104395a6a71b1be6.tar.lz slang-c6d2b259a4f253403929f6f1104395a6a71b1be6.tar.xz slang-c6d2b259a4f253403929f6f1104395a6a71b1be6.tar.zst slang-c6d2b259a4f253403929f6f1104395a6a71b1be6.zip |
Convert structure to _dub_ build system
Diffstat (limited to 'source/primitives/conditional.d')
-rw-r--r-- | source/primitives/conditional.d | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/source/primitives/conditional.d b/source/primitives/conditional.d new file mode 100644 index 0000000..678f516 --- /dev/null +++ b/source/primitives/conditional.d @@ -0,0 +1,77 @@ +module primitives.conditional; + +import std.variant; +import std.typecons; +import std.container : DList; + +import base.stack; + +Nullable!(DList!Token) buffer; +bool concluded = true; +bool drop_mode = false; + +void capture(Token token) { + if ( !drop_mode ) { + buffer.insertBack(token); + } +} + +bool drop(Token token) { + if ( concluded && buffer.isNull ) { + return false; + } + + if ( token.type == typeid(string) ) { + switch ( *token.peek!string ) { + case "if" : eval_if; break; + case "then" : eval_then; break; + case "else" : eval_else; break; + default : capture(token); break; + } + } else { + capture(token); + } + + return true; +} + +void eval_if() { + if ( concluded ) { + buffer = DList!Token(); + drop_mode = !stack.pop.get!bool; + concluded = false; + } else { + throw new Exception("conditionals may not be nested directly"); + } +} + +void eval_then() { + if ( concluded ) { + throw new Exception("`then` without preceding `if`"); + } else { + drop_mode = !drop_mode; + } +} + +void eval_else() { + if ( concluded ) { + throw new Exception("`else` without preceding `if`"); + } else { + drop_mode = false; + concluded = true; + } +} + +bool dischargeable() { + return concluded && !buffer.isNull; +} + +Stack!Token discharge() { + if ( concluded ) { + Stack!Token result = buffer[]; + buffer.nullify; + return result; + } else { + throw new Exception("unconcluded conditional may not be discharged"); + } +} |