From 74163c2028e8d09b66cc1bb8d8002aba1a14ffed Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 8 Oct 2023 11:30:37 +0100 Subject: Add Patio switch instruction --- .../compiler/patio/PatioSerializerTests.java | 92 ++++++++++++++++++++++ .../compiler/patio/instructions/Patio.java | 13 +++ 2 files changed, 105 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 d2dd9205..a4ede83c 100644 --- a/src/test/java/org/shedlang/compiler/patio/PatioSerializerTests.java +++ b/src/test/java/org/shedlang/compiler/patio/PatioSerializerTests.java @@ -441,6 +441,98 @@ public class PatioSerializerTests { assertThat(string, equalTo("(return (local \"value\"))")); } + @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 targets, + PatioOperand value, + List 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); } -- cgit v1.2.3