diff options
| author | mryouse | 2023-06-07 22:52:09 -0400 |
|---|---|---|
| committer | mryouse | 2023-06-07 22:52:09 -0400 |
| commit | fbc75bca57a3f66006468fb1a8f3988cdc8227d2 (patch) | |
| tree | 48c37fe529dcb55d2bd8516841c516d799ef05a9 /compiler.d | |
| parent | 730a6d777530a33fbb8df86df8b16d842b12c283 (diff) | |
initial commit of branch
Diffstat (limited to 'compiler.d')
| -rw-r--r-- | compiler.d | 38 |
1 files changed, 38 insertions, 0 deletions
@@ -83,6 +83,41 @@ class Compiler { } } + void compileBranch(Form form) { + + Branch branch = cast(Branch)form; + + Cons clause; + int[] endJumps; + int currentJump; + foreach (Form cl ; branch.clauses) { + // resolve the condition + clause = cast(Cons)cl; + this.resolve(clause.head); + this.func.chunk.writeOp(OpCode.OP_TYPE_CHECK_BOOLEAN, clause.line); + + // write a jump spot and pop + currentJump = this.jump(OpCode.OP_JUMP_IF_FALSE); + this.func.chunk.writeOp(OpCode.OP_POP, clause.line); + + // resolve the result of the condition (only executes if true) + foreach (Form f ; clause.tail) { + this.resolve(f); + } + + // jump to the end (if we got here) + endJumps ~= this.jump(OpCode.OP_JUMP); + + // this is where we jump to if the condition was false + this.patchJump(currentJump); + } + + // patch all the end jumps + foreach (int jmp; endJumps) { + this.patchJump(jmp); + } + } + void compileIf(Form form) { If if_ = cast(If)form; @@ -908,6 +943,9 @@ class Compiler { case FormType.IF: this.compileIf(form); break; + case FormType.BRANCH: + this.compileBranch(form); + break; case FormType.AND: this.compileAnd(form); break; |
