From 1232307b381528fd21411a602e7a125fdee1fffb Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 17 Sep 2023 18:14:17 +0100 Subject: Compile tuple expression into stack IR --- .../org/shedlang/compiler/ast/typed/Typed.java | 15 ++++++++--- .../compiler/stackir/StackCompilerTupleTests.java | 31 ++++++++++++++++++++++ .../stackir/instructions/InstructionMatchers.java | 7 +++++ 3 files changed, 49 insertions(+), 4 deletions(-) create mode 100644 src/test/java/org/shedlang/compiler/stackir/StackCompilerTupleTests.java (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 ca4e251a..3f0230c0 100644 --- a/src/test/java/org/shedlang/compiler/ast/typed/Typed.java +++ b/src/test/java/org/shedlang/compiler/ast/typed/Typed.java @@ -1,10 +1,7 @@ package org.shedlang.compiler.ast.typed; import org.shedlang.compiler.sources.Sources; -import org.shedlang.compiler.types.NamespaceName; -import org.shedlang.compiler.types.Type; -import org.shedlang.compiler.types.TypeLevelValue; -import org.shedlang.compiler.types.Types; +import org.shedlang.compiler.types.*; import java.math.BigInteger; import java.util.List; @@ -197,6 +194,16 @@ public class Typed { return new TypedTargetNode.Var(name, Sources.NULL); } + public static TypedTupleNode tuple(List elements) { + return new TypedTupleNode( + elements, + new TupleType(elements.stream() + .map(element -> element.type()) + .toList()), + Sources.NULL + ); + } + public static TypedTypeLevelExpressionNode typeLevelReference( String name, T value diff --git a/src/test/java/org/shedlang/compiler/stackir/StackCompilerTupleTests.java b/src/test/java/org/shedlang/compiler/stackir/StackCompilerTupleTests.java new file mode 100644 index 00000000..f05e9257 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/stackir/StackCompilerTupleTests.java @@ -0,0 +1,31 @@ +package org.shedlang.compiler.stackir; + +import org.junit.jupiter.api.Test; +import org.shedlang.compiler.ast.typed.Typed; + +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 StackCompilerTupleTests { + @Test + public void stringAddIsCompiledToStringAddInstruction() { + var node = Typed.tuple(List.of( + Typed.stringLiteral("a"), + Typed.boolLiteral(true), + Typed.intLiteral(4) + )); + var context = StackCompiler.Context.EMPTY; + + var result = StackCompiler.compileExpression(node, context); + + assertThat(result.instructions(), isSequence( + isPushStringInstruction("a"), + isPushBoolInstruction(true), + isPushIntInstruction(4), + isTupleCreateInstruction(3) + )); + } +} 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 b70ff8dd..a256c321 100644 --- a/src/test/java/org/shedlang/compiler/stackir/instructions/InstructionMatchers.java +++ b/src/test/java/org/shedlang/compiler/stackir/instructions/InstructionMatchers.java @@ -136,6 +136,13 @@ public class InstructionMatchers { return instanceOf(StringAddInstruction.class); } + public static Matcher isTupleCreateInstruction(int elementCount) { + return instanceOf( + TupleCreateInstruction.class, + has("elementCount", x -> x.elementCount(), equalTo(elementCount)) + ); + } + public static Matcher isVarLoadInstruction(String name) { return instanceOf( VarLoadInstruction.class, -- cgit v1.2.3