aboutsummaryrefslogtreecommitdiff
path: root/src/test/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-07-14 21:43:06 +0100
committerMichael Williamson <mike@zwobble.org>2023-07-14 21:43:06 +0100
commit1a7c4eba61bd9c055c2e3e19fffbae10b5317197 (patch)
tree648a5c250fb594210baa821f438896b800a8e0ac /src/test/java
parent0b8c4b6a27838e89ff49e1ab66bf2ed4378ec7da (diff)
Handle ifs with multiple conditional branches
Diffstat (limited to 'src/test/java')
-rw-r--r--src/test/java/org/shedlang/compiler/stackir/StackCompilerIfTests.java47
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()
+ )
+ )
+ )
+ )
+ ));
+ }
}