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 --- .../org/shedlang/compiler/patio/ast/PatioSerializer.java | 16 ++++++++++++++++ .../patio/ast/instructions/PatioInstruction.java | 2 +- .../compiler/patio/ast/instructions/PatioLoop.java | 16 ++++++++++++++++ .../compiler/patio/ast/instructions/PatioLoopBreak.java | 9 +++++++++ .../patio/ast/instructions/PatioLoopContinue.java | 9 +++++++++ 5 files changed, 51 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/main/java') 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 { +} -- cgit v1.2.3