From a8e9e00313a07442d05523ea4c189ac61d77a34f Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Wed, 22 Nov 2023 22:58:23 +0000 Subject: Add throw and try instructions to Patio --- .../compiler/patio/PatioSerializerTests.java | 92 ++++++++++++++++++++++ .../compiler/patio/instructions/Patio.java | 25 ++++++ 2 files changed, 117 insertions(+) (limited to 'src/test/java') 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 @@ -556,6 +556,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 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 targets, + List catchClauses, + PatioBlock body + ) { + return new PatioTry(targets, catchClauses, body, Sources.NULL); + } + public static PatioInstruction unreachable() { return new PatioUnreachable(Sources.NULL); } -- cgit v1.2.3