From f914488506c83ae98ee699d88501d7df756dbae5 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 27 Aug 2023 22:07:14 +0100 Subject: Fix generation of if instruction for while loop --- .../org/shedlang/compiler/stackir/StackCompiler.java | 11 +++++++++-- .../compiler/stackir/StackCompilerWhileTests.java | 16 ++++++++++++---- 2 files changed, 21 insertions(+), 6 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 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() -- cgit v1.2.3