diff options
| author | Michael Williamson <mike@zwobble.org> | 2023-09-17 18:14:17 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2023-09-17 18:14:17 +0100 |
| commit | 1232307b381528fd21411a602e7a125fdee1fffb (patch) | |
| tree | 1a19c2bdd65903e05748c6590e8d1eed1f3ffc4e /src/test/java | |
| parent | c2b45daf5ddab7b518aed9a51026a599fe4bccef (diff) | |
Compile tuple expression into stack IR
Diffstat (limited to 'src/test/java')
3 files changed, 49 insertions, 4 deletions
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<TypedExpressionNode> elements) { + return new TypedTupleNode( + elements, + new TupleType(elements.stream() + .map(element -> element.type()) + .toList()), + Sources.NULL + ); + } + public static <T extends TypeLevelValue> TypedTypeLevelExpressionNode<T> 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<Instruction> isTupleCreateInstruction(int elementCount) { + return instanceOf( + TupleCreateInstruction.class, + has("elementCount", x -> x.elementCount(), equalTo(elementCount)) + ); + } + public static Matcher<Instruction> isVarLoadInstruction(String name) { return instanceOf( VarLoadInstruction.class, |
