aboutsummaryrefslogtreecommitdiff
path: root/src
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
parent0b8c4b6a27838e89ff49e1ab66bf2ed4378ec7da (diff)
Handle ifs with multiple conditional branches
Diffstat (limited to 'src')
-rw-r--r--src/main/java/org/shedlang/compiler/stackir/StackCompiler.java22
-rw-r--r--src/test/java/org/shedlang/compiler/stackir/StackCompilerIfTests.java47
2 files changed, 61 insertions, 8 deletions
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<Instruction> 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<Instruction> 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()
+ )
+ )
+ )
+ )
+ ));
+ }
}