From 03a34c054113843846a4eaf4fbd24f9dd99c826e Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 27 Aug 2023 21:47:31 +0100 Subject: Add Patio loop instructions --- .../compiler/patio/ast/PatioSerializer.java | 16 ++++++++++ .../patio/ast/instructions/PatioInstruction.java | 2 +- .../compiler/patio/ast/instructions/PatioLoop.java | 16 ++++++++++ .../patio/ast/instructions/PatioLoopBreak.java | 9 ++++++ .../patio/ast/instructions/PatioLoopContinue.java | 9 ++++++ .../compiler/patio/PatioSerializerTests.java | 37 ++++++++++++++++++++++ .../compiler/patio/instructions/Patio.java | 12 +++++++ 7 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioLoop.java create mode 100644 src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioLoopBreak.java create mode 100644 src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioLoopContinue.java (limited to 'src') diff --git a/src/main/java/org/shedlang/compiler/patio/ast/PatioSerializer.java b/src/main/java/org/shedlang/compiler/patio/ast/PatioSerializer.java index 276d4cbf..5868ce3e 100644 --- a/src/main/java/org/shedlang/compiler/patio/ast/PatioSerializer.java +++ b/src/main/java/org/shedlang/compiler/patio/ast/PatioSerializer.java @@ -252,6 +252,22 @@ public class PatioSerializer { operandToTree(localStore.value()) ); + case PatioLoop loop -> + treeListIndented( + treeIdentifier("loop"), + instructionsToTree(loop.body()) + ); + + case PatioLoopBreak loopBreak -> + treeList( + treeIdentifier("break") + ); + + case PatioLoopContinue loopContinue -> + treeList( + treeIdentifier("continue") + ); + case PatioMemoryLoad memoryLoad -> treeListIndented( treeIdentifier("memory-load"), diff --git a/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioInstruction.java b/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioInstruction.java index 26c92715..0c8d5721 100644 --- a/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioInstruction.java +++ b/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioInstruction.java @@ -2,6 +2,6 @@ package org.shedlang.compiler.patio.ast.instructions; import org.shedlang.compiler.sources.Source; -public sealed interface PatioInstruction permits PatioAllocHeap, PatioAllocStack, PatioBoolToInt, PatioCall, PatioCallPointer, PatioDataPtrToInt, PatioIf, PatioIntAdd, PatioIntEquals, PatioIntNotEqual, PatioIntSub, PatioIntToBool, PatioIntToDataPtr, PatioLocalStore, PatioMemoryLoad, PatioMemoryStore, PatioReturn, PatioVarLoad, PatioVarStore { +public sealed interface PatioInstruction permits PatioAllocHeap, PatioAllocStack, PatioBoolToInt, PatioCall, PatioCallPointer, PatioDataPtrToInt, PatioIf, PatioIntAdd, PatioIntEquals, PatioIntNotEqual, PatioIntSub, PatioIntToBool, PatioIntToDataPtr, PatioLocalStore, PatioLoop, PatioLoopBreak, PatioLoopContinue, PatioMemoryLoad, PatioMemoryStore, PatioReturn, PatioVarLoad, PatioVarStore { Source source(); } diff --git a/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioLoop.java b/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioLoop.java new file mode 100644 index 00000000..94d33a24 --- /dev/null +++ b/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioLoop.java @@ -0,0 +1,16 @@ +package org.shedlang.compiler.patio.ast.instructions; + +import org.shedlang.compiler.sources.Source; + +import java.util.List; + +/** + * Execute the body. The continue instruction can jump back to the + * start of the body. The break instruction can jump to the end of + * the body. + */ +public record PatioLoop( + List body, + Source source +) implements PatioInstruction { +} diff --git a/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioLoopBreak.java b/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioLoopBreak.java new file mode 100644 index 00000000..a276a5ad --- /dev/null +++ b/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioLoopBreak.java @@ -0,0 +1,9 @@ +package org.shedlang.compiler.patio.ast.instructions; + +import org.shedlang.compiler.sources.Source; + +/** + * Jump to the end of the enclosing loop instruction's body. + */ +public record PatioLoopBreak(Source source) implements PatioInstruction { +} diff --git a/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioLoopContinue.java b/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioLoopContinue.java new file mode 100644 index 00000000..f8afd614 --- /dev/null +++ b/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioLoopContinue.java @@ -0,0 +1,9 @@ +package org.shedlang.compiler.patio.ast.instructions; + +import org.shedlang.compiler.sources.Source; + +/** + * Jump to the start of the enclosing loop instruction's body. + */ +public record PatioLoopContinue(Source source) implements PatioInstruction { +} diff --git a/src/test/java/org/shedlang/compiler/patio/PatioSerializerTests.java b/src/test/java/org/shedlang/compiler/patio/PatioSerializerTests.java index 8f1c4762..0a214a26 100644 --- a/src/test/java/org/shedlang/compiler/patio/PatioSerializerTests.java +++ b/src/test/java/org/shedlang/compiler/patio/PatioSerializerTests.java @@ -363,6 +363,43 @@ public class PatioSerializerTests { assertThat(string, equalTo("(local-store (local \"target\") (local \"x\"))")); } + @Test + public void canSerializeLoop() { + var instruction = Patio.loop( + List.of( + Patio.localStore(Patio.localRef("x"), Patio.int_(1)) + ) + ); + + var string = PatioTesting.serializeInstruction(instruction); + + assertThat(string, equalTo(""" + ( + loop + ( + (local-store (local "x") (int 1)) + ) + )""")); + } + + @Test + public void canSerializeLoopBreak() { + var instruction = Patio.loopBreak(); + + var string = PatioTesting.serializeInstruction(instruction); + + assertThat(string, equalTo("(break)")); + } + + @Test + public void canSerializeLoopContinue() { + var instruction = Patio.loopContinue(); + + var string = PatioTesting.serializeInstruction(instruction); + + assertThat(string, equalTo("(continue)")); + } + @Test public void canSerializeMemoryLoad() { var instruction = Patio.memoryLoad( 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 90249c1d..3978bf39 100644 --- a/src/test/java/org/shedlang/compiler/patio/instructions/Patio.java +++ b/src/test/java/org/shedlang/compiler/patio/instructions/Patio.java @@ -179,6 +179,18 @@ public class Patio { return new PatioLocalRef(name); } + public static PatioInstruction loop(List body) { + return new PatioLoop(body, Sources.NULL); + } + + public static PatioInstruction loopBreak() { + return new PatioLoopBreak(Sources.NULL); + } + + public static PatioInstruction loopContinue() { + return new PatioLoopContinue(Sources.NULL); + } + public static PatioInstruction memoryLoad( PatioLocalRef target, PatioOperand pointer, -- cgit v1.2.3