aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-08-27 22:07:14 +0100
committerMichael Williamson <mike@zwobble.org>2023-08-27 22:07:14 +0100
commitf914488506c83ae98ee699d88501d7df756dbae5 (patch)
treedff208a90456c77fece89e653072bc870459ce75 /src
parentd408f4f80670ed7f73d09f8ccfefdcb8a7835406 (diff)
Fix generation of if instruction for while loop
Diffstat (limited to 'src')
-rw-r--r--src/main/java/org/shedlang/compiler/stackir/StackCompiler.java11
-rw-r--r--src/test/java/org/shedlang/compiler/stackir/StackCompilerWhileTests.java16
2 files changed, 21 insertions, 6 deletions
diff --git a/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java b/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java
index f4580e69..570ce62a 100644
--- a/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java
+++ b/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java
@@ -418,11 +418,18 @@ public class StackCompiler {
context = context.enterBlock(List.of());
context = compileExpression(node.condition(), context);
+ // TODO: support no yielding from if instruction?
context = context.addInstruction(new IfInstruction(
- List.of(),
- List.of(new LoopBreakInstruction(node.source())),
+ List.of(
+ new PushUnitInstruction(node.source())
+ ),
+ List.of(
+ new LoopBreakInstruction(node.source()),
+ new PushUnitInstruction(node.source())
+ ),
node.source()
));
+ context = context.addInstruction(new DiscardInstruction(node.source()));
context = context.addInstructions(ifTrueInstructions);
diff --git a/src/test/java/org/shedlang/compiler/stackir/StackCompilerWhileTests.java b/src/test/java/org/shedlang/compiler/stackir/StackCompilerWhileTests.java
index c8fd1cdc..398a9827 100644
--- a/src/test/java/org/shedlang/compiler/stackir/StackCompilerWhileTests.java
+++ b/src/test/java/org/shedlang/compiler/stackir/StackCompilerWhileTests.java
@@ -28,11 +28,15 @@ public class StackCompilerWhileTests {
isSequence(
isLocalLoadInstruction("c__0"),
isIfInstruction(
- isSequence(),
isSequence(
- isLoopBreakInstruction()
+ isPushUnitInstruction()
+ ),
+ isSequence(
+ isLoopBreakInstruction(),
+ isPushUnitInstruction()
)
),
+ isDiscardInstruction(),
isPushIntInstruction(42),
isDiscardInstruction(),
isLoopContinueInstruction()
@@ -61,11 +65,15 @@ public class StackCompilerWhileTests {
isSequence(
isLocalLoadInstruction("c__0"),
isIfInstruction(
- isSequence(),
isSequence(
- isLoopBreakInstruction()
+ isPushUnitInstruction()
+ ),
+ isSequence(
+ isLoopBreakInstruction(),
+ isPushUnitInstruction()
)
),
+ isDiscardInstruction(),
isPushIntInstruction(42),
isDiscardInstruction(),
isLoopContinueInstruction()