aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2026-04-06 15:06:54 +0100
committerMichael Williamson <mike@zwobble.org>2026-04-06 15:06:54 +0100
commitcb8e58ca67297861298d00153a69fe354d5be548 (patch)
tree552ddf9cc6a39923d06b21dafff16e8ed7d18985
parent0afa2b177ed80ad94704f5c1f536c1177033bd84 (diff)
Add test for Patio loop break
-rw-r--r--src/test/java/org/shedlang/compiler/patio/PatioExampleTests.java36
1 files changed, 36 insertions, 0 deletions
diff --git a/src/test/java/org/shedlang/compiler/patio/PatioExampleTests.java b/src/test/java/org/shedlang/compiler/patio/PatioExampleTests.java
index 0fa702ec..8267a13a 100644
--- a/src/test/java/org/shedlang/compiler/patio/PatioExampleTests.java
+++ b/src/test/java/org/shedlang/compiler/patio/PatioExampleTests.java
@@ -425,6 +425,42 @@ public abstract class PatioExampleTests {
}
@Test
+ public void loopCanExitEarlyWithBreak() throws IOException, InterruptedException {
+ var patioFunction = PatioFunctionDefinition.builder()
+ .withName(FUNCTION_NAME)
+ .withReturnType(PatioTypes.VOID)
+ .addLocal(Patio.val("c", PatioTypes.BOOL))
+ .addLocal(Patio.var("x_var", PatioTypes.INT))
+ .addLocal(Patio.val("x_current", PatioTypes.INT))
+ .addLocal(Patio.val("x_next", PatioTypes.INT))
+ .addBodyInstruction(Patio.varStore(Patio.localRef("x_var"), Patio.intConst(3)))
+ .addBodyInstruction(Patio.loop(List.of(
+ Patio.varLoad(Patio.localRef("x_current"), Patio.localRef("x_var")),
+ Patio.intEquals(Patio.localRef("c"), Patio.localRef("x_current"), Patio.intConst(1)),
+ Patio.if_(
+ List.of(),
+ Patio.localRef("c"),
+ Patio.block(List.of(Patio.loopBreak()), List.of()),
+ Patio.block(List.of(), List.of())
+ ),
+ emitInt(Patio.localRef("x_current")),
+ Patio.intAdd(Patio.localRef("x_next"), Patio.localRef("x_current"), Patio.intConst(-1)),
+ Patio.varStore(Patio.localRef("x_var"), Patio.localRef("x_next")),
+ Patio.loopContinue()
+ )))
+ .addBodyInstruction(Patio.returnVoid())
+ .build();
+ var patio = Patio.compilationUnit(List.of(patioFunction));
+
+ var result = run(patio);
+
+ assertThat(result, isSequence(
+ isInt(3),
+ isInt(2)
+ ));
+ }
+
+ @Test
public void boolToIntToBoolIsIdentityForFalse() throws IOException, InterruptedException {
var patioFunction = PatioFunctionDefinition.builder()
.withName(FUNCTION_NAME)