aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2024-12-22 20:44:49 +0000
committerMichael Williamson <mike@zwobble.org>2024-12-22 20:44:49 +0000
commit8f0f5c7f448b49652d4a3d6be70b0e867859a6d0 (patch)
treeb07d21c4fa042b6ba5835fbfb1f802622beb01b4 /src
parent6dd0bb83ea290fcf30f554185d18928e93219180 (diff)
Support any result type for Patio try
Diffstat (limited to 'src')
-rw-r--r--src/main/java/org/shedlang/compiler/backends/wasm/PatioToWasm.java10
-rw-r--r--src/test/java/org/shedlang/compiler/patio/PatioExampleTests.java43
2 files changed, 49 insertions, 4 deletions
diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/PatioToWasm.java b/src/main/java/org/shedlang/compiler/backends/wasm/PatioToWasm.java
index c4e73446..707d9a87 100644
--- a/src/main/java/org/shedlang/compiler/backends/wasm/PatioToWasm.java
+++ b/src/main/java/org/shedlang/compiler/backends/wasm/PatioToWasm.java
@@ -361,6 +361,7 @@ public class PatioToWasm {
case PatioSwitch patioSwitch -> {
// TODO: use br_table?
+
var wasmBlockTypeResult = generateBlockType(patioSwitch.destinations(), context);
var wasmBlockType = wasmBlockTypeResult.value();
context = wasmBlockTypeResult.context();
@@ -399,10 +400,11 @@ public class PatioToWasm {
}
case PatioTry try_ -> {
- // TODO: don't assume we always have a Patio int
- context = context.enterTryInstruction(
- WasmBlockType.of(target.patioIntType())
- );
+ var wasmBlockTypeResult = generateBlockType(try_.destinations(), context);
+ var wasmBlockType = wasmBlockTypeResult.value();
+ context = wasmBlockTypeResult.context();
+
+ context = context.enterTryInstruction(wasmBlockType);
context = generateBlockBody(try_.body(), context);
diff --git a/src/test/java/org/shedlang/compiler/patio/PatioExampleTests.java b/src/test/java/org/shedlang/compiler/patio/PatioExampleTests.java
index e1566c99..f7dcbcb5 100644
--- a/src/test/java/org/shedlang/compiler/patio/PatioExampleTests.java
+++ b/src/test/java/org/shedlang/compiler/patio/PatioExampleTests.java
@@ -791,6 +791,49 @@ public abstract class PatioExampleTests {
}
@Test
+ public void tryCanHaveMultipleResults() throws IOException, InterruptedException {
+ var patioFunction = PatioFunctionDefinition.builder()
+ .withName(FUNCTION_NAME)
+ .withReturnType(PatioTypes.VOID)
+ .addLocal(Patio.val("r1", PatioTypes.INT))
+ .addLocal(Patio.val("r2", PatioTypes.INT))
+ .addLocal(Patio.val("e1", PatioTypes.INT))
+ .addLocal(Patio.val("e2", PatioTypes.INT))
+ .addBodyInstruction(Patio.try_(
+ List.of(Patio.localRef("r1"), Patio.localRef("r2")),
+ List.of(
+ Patio.catchClause(
+ "Error",
+ Patio.localRef("e1"),
+ Patio.block(
+ List.of(),
+ List.of(Patio.intConst(11), Patio.intConst(12))
+ )
+ )
+ ),
+ Patio.block(
+ List.of(),
+ List.of(Patio.intConst(1), Patio.intConst(2))
+ )
+ ))
+ .addBodyInstruction(emitInt(Patio.localRef("r1")))
+ .addBodyInstruction(emitInt(Patio.localRef("r2")))
+ .addBodyInstruction(Patio.returnVoid())
+ .build();
+ var patio = Patio.compilationUnit(List.of(
+ Patio.exceptionTypeDefinition("Error", PatioTypes.INT),
+ patioFunction
+ ));
+
+ var result = run(patio);
+
+ assertThat(result, isSequence(
+ isInt(1),
+ isInt(2)
+ ));
+ }
+
+ @Test
public void vars() throws IOException, InterruptedException {
var patioFunction = PatioFunctionDefinition.builder()
.withName(FUNCTION_NAME)