diff options
| author | Michael Williamson <mike@zwobble.org> | 2023-07-14 21:43:06 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2023-07-14 21:43:06 +0100 |
| commit | 1a7c4eba61bd9c055c2e3e19fffbae10b5317197 (patch) | |
| tree | 648a5c250fb594210baa821f438896b800a8e0ac /src/test/java | |
| parent | 0b8c4b6a27838e89ff49e1ab66bf2ed4378ec7da (diff) | |
Handle ifs with multiple conditional branches
Diffstat (limited to 'src/test/java')
| -rw-r--r-- | src/test/java/org/shedlang/compiler/stackir/StackCompilerIfTests.java | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/src/test/java/org/shedlang/compiler/stackir/StackCompilerIfTests.java b/src/test/java/org/shedlang/compiler/stackir/StackCompilerIfTests.java index 49c47208..a07e9aa6 100644 --- a/src/test/java/org/shedlang/compiler/stackir/StackCompilerIfTests.java +++ b/src/test/java/org/shedlang/compiler/stackir/StackCompilerIfTests.java @@ -43,4 +43,51 @@ public class StackCompilerIfTests { ) )); } + + @Test + public void ifWithMultipleConditionalBranchesIsCompiledToNestedIfInstructions() { + var node = Typed.if_( + List.of( + Typed.conditionalBranch( + Typed.reference("c"), + Typed.block(List.of( + Typed.blockReturn(Typed.intLiteral(42)) + )) + ), + Typed.conditionalBranch( + Typed.reference("d"), + Typed.block(List.of( + Typed.blockReturn(Typed.intLiteral(1)) + )) + ) + ), + Typed.block(List.of( + Typed.functionReturn(Typed.stringLiteral("x")) + )), + Types.INT + ); + + var instructions = StackCompiler.compileExpression(node); + + assertThat(instructions, isSequence( + isLocalLoadInstruction("c"), + isIfInstruction( + isSequence( + isPushIntInstruction(42) + ), + isSequence( + isLocalLoadInstruction("d"), + isIfInstruction( + isSequence( + isPushIntInstruction(1) + ), + isSequence( + isPushStringInstruction("x"), + isReturnInstruction() + ) + ) + ) + ) + )); + } } |
