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 | |
| parent | a2752db879a24f88d4aae96969a2d048d77927cd (diff) | |
Add switch instruction to stack IR
Diffstat (limited to 'src')
6 files changed, 87 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) { + } +} diff --git a/src/test/java/org/shedlang/compiler/stackir/InstructionSerializerTests.java b/src/test/java/org/shedlang/compiler/stackir/InstructionSerializerTests.java index b0143a9b..e2294588 100644 --- a/src/test/java/org/shedlang/compiler/stackir/InstructionSerializerTests.java +++ b/src/test/java/org/shedlang/compiler/stackir/InstructionSerializerTests.java @@ -328,6 +328,46 @@ public class InstructionSerializerTests { } @Test + public void canSerializeSwitch() { + var instruction = Instructions.switch_(List.of( + Instructions.switchCase( + Types.struct(NamespaceName.of("Example", "Project"), "A"), + List.of( + Instructions.return_() + ) + ), + Instructions.switchCase( + Types.struct(NamespaceName.of("Example", "Project"), "B"), + List.of( + Instructions.discard() + ) + ) + )); + + var string = serialize(instruction); + + assertThat(string, equalTo(""" + ( + switch + ( + ( + "Example/Project.A" + ( + (return) + ) + ) + ( + "Example/Project.B" + ( + (discard) + ) + ) + ) + )""" + )); + } + + @Test public void canSerializeTupleCreate() { var instruction = Instructions.tupleCreate(2); diff --git a/src/test/java/org/shedlang/compiler/stackir/instructions/Instructions.java b/src/test/java/org/shedlang/compiler/stackir/instructions/Instructions.java index 3ac0ac62..4971575c 100644 --- a/src/test/java/org/shedlang/compiler/stackir/instructions/Instructions.java +++ b/src/test/java/org/shedlang/compiler/stackir/instructions/Instructions.java @@ -134,6 +134,17 @@ public class Instructions { return new StringAddInstruction(Sources.NULL); } + public static Instruction switch_(List<SwitchInstruction.Case> cases) { + return new SwitchInstruction(cases, Sources.NULL); + } + + public static SwitchInstruction.Case switchCase( + StructType type, + List<Instruction> body + ) { + return new SwitchInstruction.Case(type, body, Sources.NULL); + } + public static Instruction tupleCreate(int elementCount) { return new TupleCreateInstruction(elementCount, Sources.NULL); } |
