aboutsummaryrefslogtreecommitdiff
path: root/src/test/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-10-08 11:30:37 +0100
committerMichael Williamson <mike@zwobble.org>2023-10-08 11:30:37 +0100
commit74163c2028e8d09b66cc1bb8d8002aba1a14ffed (patch)
treedf22c99b3d1222fc4fdb5d50593ed7aebd18e218 /src/test/java
parentcf0e7fa6e20b49b7a2ccd755aead11f2e78d18a9 (diff)
Add Patio switch instruction
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.java13
2 files changed, 105 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 d2dd9205..a4ede83c 100644
--- a/src/test/java/org/shedlang/compiler/patio/PatioSerializerTests.java
+++ b/src/test/java/org/shedlang/compiler/patio/PatioSerializerTests.java
@@ -442,6 +442,98 @@ public class PatioSerializerTests {
}
@Test
+ public void canSerializeSwitch() {
+ var instruction = Patio.switch_(
+ List.of(
+ Patio.localRef("x"),
+ Patio.localRef("y")
+ ),
+ Patio.localRef("value"),
+ List.of(
+ Patio.switchCase(
+ 1,
+ Patio.block(
+ List.of(
+ Patio.localStore(Patio.localRef("x1"), Patio.int_(11)),
+ Patio.localStore(Patio.localRef("y1"), Patio.int_(12))
+ ),
+ List.of(
+ Patio.localRef("x1"),
+ Patio.localRef("y1")
+ )
+ )
+ ),
+ Patio.switchCase(
+ 2,
+ Patio.block(
+ List.of(
+ Patio.localStore(Patio.localRef("x2"), Patio.int_(21)),
+ Patio.localStore(Patio.localRef("y2"), Patio.int_(22))
+ ),
+ List.of(
+ Patio.localRef("x2"),
+ Patio.localRef("y2")
+ )
+ )
+ )
+ ),
+ Patio.block(
+ List.of(
+ Patio.localStore(Patio.localRef("x3"), Patio.int_(31)),
+ Patio.localStore(Patio.localRef("y3"), Patio.int_(32))
+ ),
+ List.of(
+ Patio.localRef("x3"),
+ Patio.localRef("y3")
+ )
+ )
+ );
+
+ var string = PatioTesting.serializeInstruction(instruction);
+
+ assertThat(string, equalTo("""
+ (
+ switch
+ ((local "x") (local "y"))
+ (local "value")
+ (
+ (
+ case
+ 1
+ (
+ block
+ (
+ (local-store (local "x1") (int 11))
+ (local-store (local "y1") (int 12))
+ )
+ results=((local "x1") (local "y1"))
+ )
+ )
+ (
+ case
+ 2
+ (
+ block
+ (
+ (local-store (local "x2") (int 21))
+ (local-store (local "y2") (int 22))
+ )
+ results=((local "x2") (local "y2"))
+ )
+ )
+ )
+ (
+ block
+ (
+ (local-store (local "x3") (int 31))
+ (local-store (local "y3") (int 32))
+ )
+ results=((local "x3") (local "y3"))
+ )
+ )"""));
+ }
+
+ @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 075a9aca..451628da 100644
--- a/src/test/java/org/shedlang/compiler/patio/instructions/Patio.java
+++ b/src/test/java/org/shedlang/compiler/patio/instructions/Patio.java
@@ -225,6 +225,19 @@ public class Patio {
return new PatioReturn(value, Sources.NULL);
}
+ public static PatioInstruction switch_(
+ List<PatioLocalRef> targets,
+ PatioOperand value,
+ List<PatioSwitch.Case> cases,
+ PatioBlock defaultBody
+ ) {
+ return new PatioSwitch(targets, value, cases, defaultBody, Sources.NULL);
+ }
+
+ public static PatioSwitch.Case switchCase(int value, PatioBlock body) {
+ return new PatioSwitch.Case(value, body, Sources.NULL);
+ }
+
public static PatioInstruction unreachable() {
return new PatioUnreachable(Sources.NULL);
}