diff options
| author | Michael Williamson <mike@zwobble.org> | 2023-11-11 18:27:09 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2023-11-11 18:27:09 +0000 |
| commit | c260aad8915adb3958ab5fc35d51c20a5d025c2e (patch) | |
| tree | 1a953c2165df26c34e29d07b8d03c88d0900e410 /src/main/java | |
| parent | 8ddcc9410a370664c800525bfedc5a01a61e1489 (diff) | |
Add try instruction
Diffstat (limited to 'src/main/java')
4 files changed, 36 insertions, 2 deletions
diff --git a/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java b/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java index 19b479a5..d8244758 100644 --- a/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java +++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java @@ -578,6 +578,10 @@ public class PatioCodeGen { yield context; } + case TryInstruction try_ -> { + throw new UnsupportedOperationException("TODO"); + } + case TupleCreateInstruction tupleCreate -> { var elements = new ArrayList<PatioOperand>(); 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 { +} |
