From 1a7c4eba61bd9c055c2e3e19fffbae10b5317197 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Fri, 14 Jul 2023 21:43:06 +0100 Subject: Handle ifs with multiple conditional branches --- .../compiler/stackir/StackCompilerIfTests.java | 47 ++++++++++++++++++++++ 1 file changed, 47 insertions(+) (limited to 'src/test/java') 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() + ) + ) + ) + ) + )); + } } -- cgit v1.2.3