aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/shedlang/compiler/stackir
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/org/shedlang/compiler/stackir')
-rw-r--r--src/main/java/org/shedlang/compiler/stackir/InstructionSerializer.java10
-rw-r--r--src/main/java/org/shedlang/compiler/stackir/instructions/Instruction.java2
-rw-r--r--src/main/java/org/shedlang/compiler/stackir/instructions/TryInstruction.java22
3 files changed, 32 insertions, 2 deletions
diff --git a/src/main/java/org/shedlang/compiler/stackir/InstructionSerializer.java b/src/main/java/org/shedlang/compiler/stackir/InstructionSerializer.java
index ad180d60..5cd8478d 100644
--- a/src/main/java/org/shedlang/compiler/stackir/InstructionSerializer.java
+++ b/src/main/java/org/shedlang/compiler/stackir/InstructionSerializer.java
@@ -24,7 +24,7 @@ public class InstructionSerializer {
serializeTree(tree, builder, 0);
}
- private static Tree instructionsToTree(List<Instruction> instructions) {
+ private static Tree instructionsToTree(List<? extends Instruction> instructions) {
return treeListIndented(
instructions.stream()
.map(instruction -> instructionToTree(instruction))
@@ -220,6 +220,14 @@ public class InstructionSerializer {
)
);
+ case TryInstruction tryInstruction ->
+ treeListIndented(
+ treeIdentifier("try"),
+ userDefinedEffectToTree(tryInstruction.handledEffect()),
+ instructionsToTree(tryInstruction.body()),
+ instructionsToTree(tryInstruction.handlerMethods())
+ );
+
case TupleCreateInstruction tupleCreateInstruction ->
treeList(
treeIdentifier("tuple-create"),
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 fd47da6b..30216032 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, DefineEffectInstruction, DefineFunctionInstruction, DefineStructInstruction, DiscardInstruction, DuplicateInstruction, FieldLoadInstruction, IfInstruction, IntAddInstruction, IntEqualsInstruction, IntNotEqualInstruction, IntSubInstruction, LocalLoadInstruction, LocalStoreInstruction, LoopBreakInstruction, LoopContinueInstruction, LoopInstruction, NamespaceFieldStoreInstruction, NamespaceImportInstruction, PushBoolInstruction, PushIntInstruction, PushStringInstruction, PushUnitInstruction, ReturnInstruction, StringAddInstruction, SwitchInstruction, TupleCreateInstruction, TupleGetInstruction, VarLoadInstruction, VarStoreInstruction {
+ permits CallInstruction, DefineEffectInstruction, DefineFunctionInstruction, DefineStructInstruction, DiscardInstruction, DuplicateInstruction, FieldLoadInstruction, IfInstruction, IntAddInstruction, IntEqualsInstruction, IntNotEqualInstruction, IntSubInstruction, LocalLoadInstruction, LocalStoreInstruction, LoopBreakInstruction, LoopContinueInstruction, LoopInstruction, NamespaceFieldStoreInstruction, NamespaceImportInstruction, PushBoolInstruction, PushIntInstruction, PushStringInstruction, PushUnitInstruction, ReturnInstruction, StringAddInstruction, SwitchInstruction, TryInstruction, TupleCreateInstruction, TupleGetInstruction, VarLoadInstruction, VarStoreInstruction {
Source source();
}
diff --git a/src/main/java/org/shedlang/compiler/stackir/instructions/TryInstruction.java b/src/main/java/org/shedlang/compiler/stackir/instructions/TryInstruction.java
new file mode 100644
index 00000000..78a5276d
--- /dev/null
+++ b/src/main/java/org/shedlang/compiler/stackir/instructions/TryInstruction.java
@@ -0,0 +1,22 @@
+package org.shedlang.compiler.stackir.instructions;
+
+
+import org.shedlang.compiler.sources.Source;
+import org.shedlang.compiler.types.UserDefinedEffect;
+
+import java.util.List;
+
+/**
+ * Push an effect handler for the given effect with the given methods onto the
+ * effect handler stack, and execute the body. Once the body has exited, pop the
+ * effect handler from the effect handler stack.
+ * <p>
+ * If the body runs to completion, it should leave a single value on the stack.
+ */
+public record TryInstruction(
+ UserDefinedEffect handledEffect,
+ List<DefineFunctionInstruction> handlerMethods,
+ List<Instruction> body,
+ Source source
+) implements Instruction {
+}