From c260aad8915adb3958ab5fc35d51c20a5d025c2e Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 11 Nov 2023 18:27:09 +0000 Subject: Add try instruction --- .../org/shedlang/compiler/patio/PatioCodeGen.java | 4 ++++ .../compiler/stackir/InstructionSerializer.java | 10 +++++++++- .../compiler/stackir/instructions/Instruction.java | 2 +- .../stackir/instructions/TryInstruction.java | 22 ++++++++++++++++++++++ 4 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 src/main/java/org/shedlang/compiler/stackir/instructions/TryInstruction.java (limited to 'src/main/java') 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(); 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 instructions) { + private static Tree instructionsToTree(List 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. + *

+ * If the body runs to completion, it should leave a single value on the stack. + */ +public record TryInstruction( + UserDefinedEffect handledEffect, + List handlerMethods, + List body, + Source source +) implements Instruction { +} -- cgit v1.2.3