From 11dbdf3ebcac7e254f1c818178f64aa79406f47c Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 1 Oct 2023 21:50:32 +0100 Subject: Add switch instruction to stack IR --- .../org/shedlang/compiler/patio/PatioCodeGen.java | 3 +++ .../compiler/stackir/InstructionSerializer.java | 13 +++++++++++++ .../compiler/stackir/instructions/Instruction.java | 2 +- .../stackir/instructions/SwitchInstruction.java | 19 +++++++++++++++++++ 4 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/shedlang/compiler/stackir/instructions/SwitchInstruction.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 9382eab7..b68bebdf 100644 --- a/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java +++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java @@ -512,6 +512,9 @@ public class PatioCodeGen { context ); + case SwitchInstruction switch_ -> + 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 6577e2f4..fb2139f5 100644 --- a/src/main/java/org/shedlang/compiler/stackir/InstructionSerializer.java +++ b/src/main/java/org/shedlang/compiler/stackir/InstructionSerializer.java @@ -200,6 +200,19 @@ public class InstructionSerializer { case StringAddInstruction stringAddInstruction -> treeList(treeIdentifier("string-add")); + case SwitchInstruction switchInstruction -> + treeListIndented( + treeIdentifier("switch"), + treeListIndented( + switchInstruction.cases().stream() + .map(switchCase -> treeListIndented( + structTypeToTree(switchCase.caseType()), + instructionsToTree(switchCase.body()) + )) + .toList() + ) + ); + 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 e43acf56..2966e8a7 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, DefineStructInstruction, DiscardInstruction, DuplicateInstruction, FieldLoadInstruction, IfInstruction, IntAddInstruction, IntEqualsInstruction, IntNotEqualInstruction, IntSubInstruction, LocalLoadInstruction, LocalStoreInstruction, LoopBreakInstruction, LoopContinueInstruction, LoopInstruction, NamespaceFieldStoreInstruction, NamespaceImportInstruction, PushBoolInstruction, PushIntInstruction, PushStringInstruction, PushUnitInstruction, ReturnInstruction, StringAddInstruction, TupleCreateInstruction, TupleGetInstruction, VarLoadInstruction, VarStoreInstruction { + permits CallInstruction, 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 { Source source(); } diff --git a/src/main/java/org/shedlang/compiler/stackir/instructions/SwitchInstruction.java b/src/main/java/org/shedlang/compiler/stackir/instructions/SwitchInstruction.java new file mode 100644 index 00000000..85f292e8 --- /dev/null +++ b/src/main/java/org/shedlang/compiler/stackir/instructions/SwitchInstruction.java @@ -0,0 +1,19 @@ +package org.shedlang.compiler.stackir.instructions; + +import org.shedlang.compiler.sources.Source; +import org.shedlang.compiler.types.StructType; + +import java.util.List; + +/** + * Pop a value from the stack, find the case corresponding to that value's type, + * and execute the instructions in the case's body. If there is no such case, + * the behaviour of this instruction is not defined. + */ +public record SwitchInstruction( + List cases, + Source source +) implements Instruction { + public record Case(StructType caseType, List body, Source source) { + } +} -- cgit v1.2.3