From c2b45daf5ddab7b518aed9a51026a599fe4bccef Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 17 Sep 2023 18:10:00 +0100 Subject: Add tuple-get instruction --- src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java | 4 ++++ .../shedlang/compiler/stackir/InstructionSerializer.java | 6 ++++++ .../shedlang/compiler/stackir/instructions/Instruction.java | 2 +- .../stackir/instructions/TupleCreateInstruction.java | 5 +++-- .../compiler/stackir/instructions/TupleGetInstruction.java | 13 +++++++++++++ .../compiler/stackir/InstructionSerializerTests.java | 9 +++++++++ .../compiler/stackir/instructions/Instructions.java | 4 ++++ 7 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 src/main/java/org/shedlang/compiler/stackir/instructions/TupleGetInstruction.java (limited to 'src') diff --git a/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java b/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java index 3332a117..e6e3ba0a 100644 --- a/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java +++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java @@ -471,6 +471,10 @@ public class PatioCodeGen { throw new UnsupportedOperationException("TODO"); } + case TupleGetInstruction tupleGet -> { + throw new UnsupportedOperationException("TODO"); + } + case VarLoadInstruction varLoad -> { var varValueResult = context.allocateVal( PatioCodeGenLocalSpace.VAR_VALUE, diff --git a/src/main/java/org/shedlang/compiler/stackir/InstructionSerializer.java b/src/main/java/org/shedlang/compiler/stackir/InstructionSerializer.java index d2dfc37b..875dbcec 100644 --- a/src/main/java/org/shedlang/compiler/stackir/InstructionSerializer.java +++ b/src/main/java/org/shedlang/compiler/stackir/InstructionSerializer.java @@ -177,6 +177,12 @@ public class InstructionSerializer { treeInt(tupleCreateInstruction.elementCount()) ); + case TupleGetInstruction tupleGetInstruction -> + treeList( + treeIdentifier("tuple-get"), + treeInt(tupleGetInstruction.elementIndex()) + ); + case VarLoadInstruction varLoadInstruction -> treeList( treeIdentifier("var-load"), 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 25b14e93..29703869 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, DiscardInstruction, FieldLoadInstruction, IfInstruction, IntAddInstruction, IntEqualsInstruction, IntNotEqualInstruction, IntSubInstruction, LocalLoadInstruction, LocalStoreInstruction, LoopBreakInstruction, LoopContinueInstruction, LoopInstruction, NamespaceFieldStoreInstruction, NamespaceImportInstruction, PushBoolInstruction, PushIntInstruction, PushStringInstruction, PushUnitInstruction, ReturnInstruction, StringAddInstruction, TupleCreateInstruction, VarLoadInstruction, VarStoreInstruction { + permits CallInstruction, DefineFunctionInstruction, DiscardInstruction, FieldLoadInstruction, IfInstruction, IntAddInstruction, IntEqualsInstruction, IntNotEqualInstruction, IntSubInstruction, LocalLoadInstruction, LocalStoreInstruction, LoopBreakInstruction, LoopContinueInstruction, LoopInstruction, NamespaceFieldStoreInstruction, NamespaceImportInstruction, PushBoolInstruction, PushIntInstruction, PushStringInstruction, PushUnitInstruction, ReturnInstruction, StringAddInstruction, TupleCreateInstruction, TupleGetInstruction, VarLoadInstruction, VarStoreInstruction { Source source(); } diff --git a/src/main/java/org/shedlang/compiler/stackir/instructions/TupleCreateInstruction.java b/src/main/java/org/shedlang/compiler/stackir/instructions/TupleCreateInstruction.java index 0888d947..d1dec767 100644 --- a/src/main/java/org/shedlang/compiler/stackir/instructions/TupleCreateInstruction.java +++ b/src/main/java/org/shedlang/compiler/stackir/instructions/TupleCreateInstruction.java @@ -3,8 +3,9 @@ package org.shedlang.compiler.stackir.instructions; import org.shedlang.compiler.sources.Source; /** - * Pop {@code elementCount} values from the stack and create a tuple from those - * elements. Elements should have been pushed onto the stack in index order. + * Pop {@code elementCount} values from the stack, create a tuple from those + * elements, and push the tuple onto the stack. Elements should have been pushed + * onto the stack in index order. */ public record TupleCreateInstruction( int elementCount, diff --git a/src/main/java/org/shedlang/compiler/stackir/instructions/TupleGetInstruction.java b/src/main/java/org/shedlang/compiler/stackir/instructions/TupleGetInstruction.java new file mode 100644 index 00000000..b621a336 --- /dev/null +++ b/src/main/java/org/shedlang/compiler/stackir/instructions/TupleGetInstruction.java @@ -0,0 +1,13 @@ +package org.shedlang.compiler.stackir.instructions; + +import org.shedlang.compiler.sources.Source; + +/** + * Pop a tuple from the stack, get the element at the given index, and push it + * onto the stack. + */ +public record TupleGetInstruction( + int elementIndex, + Source source +) implements Instruction { +} diff --git a/src/test/java/org/shedlang/compiler/stackir/InstructionSerializerTests.java b/src/test/java/org/shedlang/compiler/stackir/InstructionSerializerTests.java index 84df6b84..bfbec2a6 100644 --- a/src/test/java/org/shedlang/compiler/stackir/InstructionSerializerTests.java +++ b/src/test/java/org/shedlang/compiler/stackir/InstructionSerializerTests.java @@ -294,6 +294,15 @@ public class InstructionSerializerTests { assertThat(string, equalTo("(tuple-create 2)")); } + @Test + public void canSerializeTupleGet() { + var instruction = Instructions.tupleGet(2); + + var string = serialize(instruction); + + assertThat(string, equalTo("(tuple-get 2)")); + } + @Test public void canSerializeVarLoad() { var instruction = Instructions.varLoad("x"); 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 90c95941..dbef836e 100644 --- a/src/test/java/org/shedlang/compiler/stackir/instructions/Instructions.java +++ b/src/test/java/org/shedlang/compiler/stackir/instructions/Instructions.java @@ -106,6 +106,10 @@ public class Instructions { return new TupleCreateInstruction(elementCount, Sources.NULL); } + public static Instruction tupleGet(int elementIndex) { + return new TupleGetInstruction(elementIndex, Sources.NULL); + } + public static Instruction varLoad(String name) { return new VarLoadInstruction(name, Sources.NULL); } -- cgit v1.2.3