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 --- .../shedlang/compiler/stackir/StackCompiler.java | 22 ++++++---- .../compiler/stackir/StackCompilerIfTests.java | 47 ++++++++++++++++++++++ 2 files changed, 61 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java b/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java index e062ab78..85d3cc5a 100644 --- a/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java +++ b/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java @@ -4,6 +4,7 @@ import org.pcollections.PVector; import org.shedlang.compiler.ShedBuiltins; import org.shedlang.compiler.ast.typed.*; import org.shedlang.compiler.stackir.instructions.*; +import org.shedlang.compiler.util.Lists; import org.shedlang.compiler.util.P; import org.shedlang.compiler.util.PCollectors; @@ -164,16 +165,21 @@ public class StackCompiler { } private static PVector compileIf(TypedIfNode node) { - var conditionalBranch = node.conditionalBranches().get(0); - var conditionInstructions = compileExpression(conditionalBranch.condition()); + var instructions = compileBlock(node.elseBody()); - var ifInstruction = new IfInstruction( - compileBlock(conditionalBranch.body()), - compileBlock(node.elseBody()), - node.source() - ); + for (var conditionalBranch : Lists.reversed(node.conditionalBranches())) { + var conditionInstructions = compileExpression(conditionalBranch.condition()); + + var ifInstruction = new IfInstruction( + compileBlock(conditionalBranch.body()), + instructions, + node.source() + ); - return conditionInstructions.plus(ifInstruction); + instructions = conditionInstructions.plus(ifInstruction); + } + + return instructions; } private static PVector compileIntLiteral(TypedIntLiteralNode node) { 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