From cb79027f3045b979b964d5dd4e6229a6d0212b0e Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 17 Sep 2023 19:23:35 +0100 Subject: Compile tuple target to stack IR --- .../shedlang/compiler/stackir/StackCompiler.java | 18 ++++++- .../org/shedlang/compiler/ast/typed/Typed.java | 4 ++ .../compiler/stackir/StackCompilerAssignTests.java | 55 ++++++++++++++++++++-- .../stackir/instructions/InstructionMatchers.java | 11 +++++ 4 files changed, 84 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java b/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java index 12a32125..8c5bea8c 100644 --- a/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java +++ b/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java @@ -384,7 +384,23 @@ public class StackCompiler { } case TypedTargetNode.Tuple tuple -> { - throw new UnsupportedOperationException("TODO"); + if (tuple.elements().size() == 0) { + context = context.addInstruction(new DiscardInstruction(tuple.source())); + } + + for (var elementIndex = 0; elementIndex < tuple.elements().size(); elementIndex++) { + if (elementIndex < tuple.elements().size() - 1) { + context = context + .addInstruction(new DuplicateInstruction(tuple.source())); + } + + context = context + .addInstruction(new TupleGetInstruction(elementIndex, tuple.source())); + + context = compileTarget(tuple.elements().get(elementIndex), context); + } + + yield context; } case TypedTargetNode.Val val -> { diff --git a/src/test/java/org/shedlang/compiler/ast/typed/Typed.java b/src/test/java/org/shedlang/compiler/ast/typed/Typed.java index 3f0230c0..0d48711c 100644 --- a/src/test/java/org/shedlang/compiler/ast/typed/Typed.java +++ b/src/test/java/org/shedlang/compiler/ast/typed/Typed.java @@ -186,6 +186,10 @@ public class Typed { return new TypedTargetNode.Reassign(name, Sources.NULL); } + public static TypedTargetNode targetTuple(List elements) { + return new TypedTargetNode.Tuple(elements, Sources.NULL); + } + public static TypedTargetNode targetVal(String name) { return new TypedTargetNode.Val(name, Sources.NULL); } diff --git a/src/test/java/org/shedlang/compiler/stackir/StackCompilerAssignTests.java b/src/test/java/org/shedlang/compiler/stackir/StackCompilerAssignTests.java index 20abbff3..b717648a 100644 --- a/src/test/java/org/shedlang/compiler/stackir/StackCompilerAssignTests.java +++ b/src/test/java/org/shedlang/compiler/stackir/StackCompilerAssignTests.java @@ -3,14 +3,15 @@ package org.shedlang.compiler.stackir; import org.junit.jupiter.api.Test; import org.shedlang.compiler.ast.typed.Typed; -import static org.shedlang.compiler.stackir.instructions.InstructionMatchers.isPushStringInstruction; -import static org.shedlang.compiler.stackir.instructions.InstructionMatchers.isVarStoreInstruction; +import java.util.List; + +import static org.shedlang.compiler.stackir.instructions.InstructionMatchers.*; import static org.zwobble.precisely.AssertThat.assertThat; import static org.zwobble.precisely.Matchers.isSequence; public class StackCompilerAssignTests { @Test - public void expressionIsEvaluatedAndStoredAsVar() { + public void whenTargetIsReassignThenExpressionIsEvaluatedAndStoredAsVar() { var node = Typed.assign( Typed.targetReassign("x"), Typed.stringLiteral("hello") @@ -25,4 +26,52 @@ public class StackCompilerAssignTests { isVarStoreInstruction("x__0") )); } + + @Test + public void whenTargetIsTupleThenExpressionIsDeconstructed() { + var node = Typed.assign( + Typed.targetTuple(List.of( + Typed.targetVal("a"), + Typed.targetVal("b"), + Typed.targetVal("c") + )), + Typed.reference("x") + ); + var context = StackCompiler.Context.EMPTY + .addShedVariable("x") + .addShedVariable("a") + .addShedVariable("b") + .addShedVariable("c"); + + var result = StackCompiler.compileFunctionStatement(node, context); + + assertThat(result.instructions(), isSequence( + isLocalLoadInstruction("x__0"), + isDuplicateInstruction(), + isTupleGetInstruction(0), + isLocalStoreInstruction("a__0"), + isDuplicateInstruction(), + isTupleGetInstruction(1), + isLocalStoreInstruction("b__0"), + isTupleGetInstruction(2), + isLocalStoreInstruction("c__0") + )); + } + + @Test + public void deconstructingEmptyTupleDiscardsExpression() { + var node = Typed.assign( + Typed.targetTuple(List.of()), + Typed.reference("x") + ); + var context = StackCompiler.Context.EMPTY + .addShedVariable("x"); + + var result = StackCompiler.compileFunctionStatement(node, context); + + assertThat(result.instructions(), isSequence( + isLocalLoadInstruction("x__0"), + isDiscardInstruction() + )); + } } diff --git a/src/test/java/org/shedlang/compiler/stackir/instructions/InstructionMatchers.java b/src/test/java/org/shedlang/compiler/stackir/instructions/InstructionMatchers.java index a256c321..b50e4903 100644 --- a/src/test/java/org/shedlang/compiler/stackir/instructions/InstructionMatchers.java +++ b/src/test/java/org/shedlang/compiler/stackir/instructions/InstructionMatchers.java @@ -33,6 +33,10 @@ public class InstructionMatchers { ); } + public static Matcher isDuplicateInstruction() { + return instanceOf(DuplicateInstruction.class); + } + public static Matcher isIfInstruction( Matcher> trueBody, Matcher> falseBody @@ -143,6 +147,13 @@ public class InstructionMatchers { ); } + public static Matcher isTupleGetInstruction(int elementIndex) { + return instanceOf( + TupleGetInstruction.class, + has("elementIndex", x -> x.elementIndex(), equalTo(elementIndex)) + ); + } + public static Matcher isVarLoadInstruction(String name) { return instanceOf( VarLoadInstruction.class, -- cgit v1.2.3