aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-08-27 20:51:49 +0100
committerMichael Williamson <mike@zwobble.org>2023-08-27 20:51:49 +0100
commitc3784202feea6d9e16044007dbb992dab3628553 (patch)
treeec5b8346b6c00472a122ee21fb0c43ead7eef136 /src/main/java
parent623c8a2542cf071b59aebe4811d7d61c762db774 (diff)
Add loop instructions to Stack IR
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java9
-rw-r--r--src/main/java/org/shedlang/compiler/stackir/InstructionSerializer.java12
-rw-r--r--src/main/java/org/shedlang/compiler/stackir/instructions/Instruction.java2
-rw-r--r--src/main/java/org/shedlang/compiler/stackir/instructions/LoopBreakInstruction.java9
-rw-r--r--src/main/java/org/shedlang/compiler/stackir/instructions/LoopContinueInstruction.java9
-rw-r--r--src/main/java/org/shedlang/compiler/stackir/instructions/LoopInstruction.java17
6 files changed, 57 insertions, 1 deletions
diff --git a/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java b/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java
index 57a08d69..c16dbc4a 100644
--- a/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java
+++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java
@@ -366,6 +366,15 @@ public class PatioCodeGen {
(local, c) -> c.temporaryPopIntoLocal(local, localStore.source())
);
+ case LoopBreakInstruction loopBreakInstruction ->
+ throw new UnsupportedOperationException("TODO");
+
+ case LoopContinueInstruction loopContinueInstruction ->
+ throw new UnsupportedOperationException("TODO");
+
+ case LoopInstruction loopInstruction ->
+ throw new UnsupportedOperationException("TODO");
+
case NamespaceFieldStoreInstruction namespaceFieldStore -> {
var fieldValueResult = context.temporaryPop();
var fieldValue = fieldValueResult.value();
diff --git a/src/main/java/org/shedlang/compiler/stackir/InstructionSerializer.java b/src/main/java/org/shedlang/compiler/stackir/InstructionSerializer.java
index 807caa57..1ae8f04d 100644
--- a/src/main/java/org/shedlang/compiler/stackir/InstructionSerializer.java
+++ b/src/main/java/org/shedlang/compiler/stackir/InstructionSerializer.java
@@ -113,6 +113,18 @@ public class InstructionSerializer {
treeIdentifier("local-store"),
treeString(localStoreInstruction.name())
);
+
+ case LoopInstruction loop ->
+ treeListIndented(
+ treeIdentifier("loop"),
+ instructionsToTree(loop.body())
+ );
+
+ case LoopBreakInstruction loopBreak ->
+ treeList(treeIdentifier("break"));
+
+ case LoopContinueInstruction loopContinue ->
+ treeList(treeIdentifier("continue"));
case NamespaceFieldStoreInstruction namespaceFieldStore ->
treeList(
diff --git a/src/main/java/org/shedlang/compiler/stackir/instructions/Instruction.java b/src/main/java/org/shedlang/compiler/stackir/instructions/Instruction.java
index b4a59136..01af3588 100644
--- a/src/main/java/org/shedlang/compiler/stackir/instructions/Instruction.java
+++ b/src/main/java/org/shedlang/compiler/stackir/instructions/Instruction.java
@@ -3,7 +3,7 @@ package org.shedlang.compiler.stackir.instructions;
import org.shedlang.compiler.sources.Source;
public sealed interface Instruction
- permits CallInstruction, DefineFunctionInstruction, DiscardInstruction, FieldLoadInstruction, IfInstruction, IntAddInstruction, IntEqualsInstruction, IntNotEqualInstruction, IntSubInstruction, LocalLoadInstruction, LocalStoreInstruction, NamespaceFieldStoreInstruction, NamespaceImportInstruction, PushBoolInstruction, PushIntInstruction, PushStringInstruction, PushUnitInstruction, ReturnInstruction, StringAddInstruction, VarLoadInstruction, VarStoreInstruction {
+ permits CallInstruction, DefineFunctionInstruction, DiscardInstruction, FieldLoadInstruction, IfInstruction, IntAddInstruction, IntEqualsInstruction, IntNotEqualInstruction, IntSubInstruction, LocalLoadInstruction, LocalStoreInstruction, LoopBreakInstruction, LoopContinueInstruction, LoopInstruction, NamespaceFieldStoreInstruction, NamespaceImportInstruction, PushBoolInstruction, PushIntInstruction, PushStringInstruction, PushUnitInstruction, ReturnInstruction, StringAddInstruction, VarLoadInstruction, VarStoreInstruction {
Source source();
}
diff --git a/src/main/java/org/shedlang/compiler/stackir/instructions/LoopBreakInstruction.java b/src/main/java/org/shedlang/compiler/stackir/instructions/LoopBreakInstruction.java
new file mode 100644
index 00000000..60a1d056
--- /dev/null
+++ b/src/main/java/org/shedlang/compiler/stackir/instructions/LoopBreakInstruction.java
@@ -0,0 +1,9 @@
+package org.shedlang.compiler.stackir.instructions;
+
+import org.shedlang.compiler.sources.Source;
+
+/**
+ * Jump to the end of the enclosing <code>loop</code> instruction's body.
+ */
+public record LoopBreakInstruction(Source source) implements Instruction {
+}
diff --git a/src/main/java/org/shedlang/compiler/stackir/instructions/LoopContinueInstruction.java b/src/main/java/org/shedlang/compiler/stackir/instructions/LoopContinueInstruction.java
new file mode 100644
index 00000000..d7111ea9
--- /dev/null
+++ b/src/main/java/org/shedlang/compiler/stackir/instructions/LoopContinueInstruction.java
@@ -0,0 +1,9 @@
+package org.shedlang.compiler.stackir.instructions;
+
+import org.shedlang.compiler.sources.Source;
+
+/**
+ * Jump to the start of the enclosing <code>loop</code> instruction's body.
+ */
+public record LoopContinueInstruction(Source source) implements Instruction {
+}
diff --git a/src/main/java/org/shedlang/compiler/stackir/instructions/LoopInstruction.java b/src/main/java/org/shedlang/compiler/stackir/instructions/LoopInstruction.java
new file mode 100644
index 00000000..119d9a52
--- /dev/null
+++ b/src/main/java/org/shedlang/compiler/stackir/instructions/LoopInstruction.java
@@ -0,0 +1,17 @@
+package org.shedlang.compiler.stackir.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 LoopInstruction(
+ List<Instruction> body,
+ Source source
+) implements Instruction {
+}