aboutsummaryrefslogtreecommitdiff
path: root/src/test/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-11-22 22:58:23 +0000
committerMichael Williamson <mike@zwobble.org>2023-11-22 22:58:23 +0000
commita8e9e00313a07442d05523ea4c189ac61d77a34f (patch)
treeda154647e80c335ee8b8f40e6b623fa49d517bcf /src/test/java
parent5bd9f3a11c73ee9c4196985a05f68f4d69ff9432 (diff)
Add throw and try instructions to Patio
Diffstat (limited to 'src/test/java')
-rw-r--r--src/test/java/org/shedlang/compiler/patio/PatioSerializerTests.java92
-rw-r--r--src/test/java/org/shedlang/compiler/patio/instructions/Patio.java25
2 files changed, 117 insertions, 0 deletions
diff --git a/src/test/java/org/shedlang/compiler/patio/PatioSerializerTests.java b/src/test/java/org/shedlang/compiler/patio/PatioSerializerTests.java
index fced74d5..8ff0ff96 100644
--- a/src/test/java/org/shedlang/compiler/patio/PatioSerializerTests.java
+++ b/src/test/java/org/shedlang/compiler/patio/PatioSerializerTests.java
@@ -557,6 +557,98 @@ public class PatioSerializerTests {
}
@Test
+ public void canSerializeThrow() {
+ var instruction = Patio.throw_("Error", Patio.localRef("value"));
+
+ var string = PatioTesting.serializeInstruction(instruction);
+
+ assertThat(string, equalTo("""
+ (throw "Error" (local "value"))"""));
+ }
+
+ @Test
+ public void canSerializeTry() {
+ var instruction = Patio.try_(
+ List.of(Patio.localRef("x"), Patio.localRef("y")),
+ List.of(
+ Patio.catchClause(
+ "Error1",
+ Patio.localRef("a1"),
+ Patio.block(
+ List.of(
+ Patio.localStore(Patio.localRef("xCatch1"), Patio.int_(11)),
+ Patio.localStore(Patio.localRef("yCatch1"), Patio.int_(12))
+ ),
+ List.of(Patio.localRef("xCatch1"), Patio.localRef("yCatch1"))
+ )
+ ),
+ Patio.catchClause(
+ "Error2",
+ Patio.localRef("a2"),
+ Patio.block(
+ List.of(
+ Patio.localStore(Patio.localRef("xCatch2"), Patio.int_(21)),
+ Patio.localStore(Patio.localRef("yCatch2"), Patio.int_(22))
+ ),
+ List.of(Patio.localRef("xCatch2"), Patio.localRef("yCatch2"))
+ )
+ )
+ ),
+ Patio.block(
+ List.of(
+ Patio.localStore(Patio.localRef("xTry"), Patio.int_(31)),
+ Patio.localStore(Patio.localRef("yTry"), Patio.int_(32))
+ ),
+ List.of(Patio.localRef("xTry"), Patio.localRef("yTry"))
+ )
+ );
+
+ var string = PatioTesting.serializeInstruction(instruction);
+
+ assertThat(string, equalTo("""
+ (
+ try
+ ((local "x") (local "y"))
+ (
+ (
+ catch
+ "Error1"
+ (local "a1")
+ (
+ block
+ (
+ (local-store (local "xCatch1") (int 11))
+ (local-store (local "yCatch1") (int 12))
+ )
+ results=((local "xCatch1") (local "yCatch1"))
+ )
+ )
+ (
+ catch
+ "Error2"
+ (local "a2")
+ (
+ block
+ (
+ (local-store (local "xCatch2") (int 21))
+ (local-store (local "yCatch2") (int 22))
+ )
+ results=((local "xCatch2") (local "yCatch2"))
+ )
+ )
+ )
+ (
+ block
+ (
+ (local-store (local "xTry") (int 31))
+ (local-store (local "yTry") (int 32))
+ )
+ results=((local "xTry") (local "yTry"))
+ )
+ )"""));
+ }
+
+ @Test
public void canSerializeUnreachable() {
var instruction = Patio.unreachable();
diff --git a/src/test/java/org/shedlang/compiler/patio/instructions/Patio.java b/src/test/java/org/shedlang/compiler/patio/instructions/Patio.java
index 44ef8f25..bc44dd2f 100644
--- a/src/test/java/org/shedlang/compiler/patio/instructions/Patio.java
+++ b/src/test/java/org/shedlang/compiler/patio/instructions/Patio.java
@@ -63,6 +63,19 @@ public class Patio {
);
}
+ public static PatioCatchClause catchClause(
+ String exceptionTypeName,
+ PatioLocalRef target,
+ PatioBlock body
+ ) {
+ return new PatioCatchClause(
+ exceptionTypeName,
+ target,
+ body,
+ Sources.NULL
+ );
+ }
+
public static PatioCompilationUnit compilationUnit(
List<PatioCompilationUnitElement> elements
) {
@@ -246,6 +259,18 @@ public class Patio {
return new PatioSwitch.Case(value, body, Sources.NULL);
}
+ public static PatioInstruction throw_(String exceptionTypeName, PatioOperand value) {
+ return new PatioThrow(exceptionTypeName, value, Sources.NULL);
+ }
+
+ public static PatioTry try_(
+ List<PatioLocalRef> targets,
+ List<PatioCatchClause> catchClauses,
+ PatioBlock body
+ ) {
+ return new PatioTry(targets, catchClauses, body, Sources.NULL);
+ }
+
public static PatioInstruction unreachable() {
return new PatioUnreachable(Sources.NULL);
}