diff options
| author | Michael Williamson <mike@zwobble.org> | 2023-08-27 21:47:31 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2023-08-27 21:47:31 +0100 |
| commit | 03a34c054113843846a4eaf4fbd24f9dd99c826e (patch) | |
| tree | efde7f1dab58e78e6fc9d534947c461204dc36c5 /src | |
| parent | 0110c68debe431369dcd4f107daf142d478b8496 (diff) | |
Add Patio loop instructions
Diffstat (limited to 'src')
7 files changed, 100 insertions, 1 deletions
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 <code>continue</code> instruction can jump back to the + * start of the body. The <code>break</code> instruction can jump to the end of + * the body. + */ +public record PatioLoop( + List<PatioInstruction> 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 <code>loop</code> 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 <code>loop</code> 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 @@ -364,6 +364,43 @@ public class PatioSerializerTests { } @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( Patio.localRef("target"), 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<PatioInstruction> 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, |
