diff options
| author | Michael Williamson <mike@zwobble.org> | 2023-10-01 21:50:32 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2023-10-01 21:50:32 +0100 |
| commit | 11dbdf3ebcac7e254f1c818178f64aa79406f47c (patch) | |
| tree | 4ea78fef28b7d6b14eaa88d5da42dba00949ad9c /src/main/java | |
| parent | a2752db879a24f88d4aae96969a2d048d77927cd (diff) | |
Add switch instruction to stack IR
Diffstat (limited to 'src/main/java')
4 files changed, 36 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 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<PatioOperand>(); 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<Case> cases, + Source source +) implements Instruction { + public record Case(StructType caseType, List<Instruction> body, Source source) { + } +} |
