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 --- .../org/shedlang/compiler/ast/typed/Typed.java | 4 ++ .../compiler/stackir/StackCompilerAssignTests.java | 55 ++++++++++++++++++++-- .../stackir/instructions/InstructionMatchers.java | 11 +++++ 3 files changed, 67 insertions(+), 3 deletions(-) (limited to 'src/test/java') 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