From 8c346ff940f7336a9426bc61d7f321b0944f6bdc Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 10 Jun 2023 10:18:56 +0100 Subject: Compile reference to stack instruction --- .../java/org/shedlang/compiler/ast/typed/Typed.java | 10 ++++++++++ .../stackir/StackCompilerReferenceTests.java | 21 +++++++++++++++++++++ .../stackir/instructions/InstructionMatchers.java | 7 +++++++ 3 files changed, 38 insertions(+) create mode 100644 src/test/java/org/shedlang/compiler/stackir/StackCompilerReferenceTests.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 2468449a..75f54d36 100644 --- a/src/test/java/org/shedlang/compiler/ast/typed/Typed.java +++ b/src/test/java/org/shedlang/compiler/ast/typed/Typed.java @@ -1,6 +1,8 @@ package org.shedlang.compiler.ast.typed; import org.shedlang.compiler.sources.Sources; +import org.shedlang.compiler.types.Type; +import org.shedlang.compiler.types.Types; import java.math.BigInteger; @@ -19,4 +21,12 @@ public class Typed { public static TypedStringLiteralNode stringLiteral(String value) { return new TypedStringLiteralNode(value, Sources.NULL); } + + public static TypedReferenceNode reference(String name) { + return reference(name, Types.STRING); + } + + public static TypedReferenceNode reference(String name, Type type) { + return new TypedReferenceNode(name, type, Sources.NULL); + } } diff --git a/src/test/java/org/shedlang/compiler/stackir/StackCompilerReferenceTests.java b/src/test/java/org/shedlang/compiler/stackir/StackCompilerReferenceTests.java new file mode 100644 index 00000000..2b5115c5 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/stackir/StackCompilerReferenceTests.java @@ -0,0 +1,21 @@ +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.isLocalLoadInstruction; +import static org.zwobble.precisely.AssertThat.assertThat; +import static org.zwobble.precisely.Matchers.isSequence; + +public class StackCompilerReferenceTests { + @Test + public void referenceIsCompiledToLocalLoad() { + var node = Typed.reference("x"); + + var instructions = StackCompiler.compileExpression(node); + + assertThat(instructions, isSequence( + isLocalLoadInstruction("x") + )); + } +} 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 f2d153cc..2fc1aa21 100644 --- a/src/test/java/org/shedlang/compiler/stackir/instructions/InstructionMatchers.java +++ b/src/test/java/org/shedlang/compiler/stackir/instructions/InstructionMatchers.java @@ -10,6 +10,13 @@ public class InstructionMatchers { private InstructionMatchers() { } + public static Matcher isLocalLoadInstruction(String name) { + return instanceOf( + LocalLoadInstruction.class, + has("name", x -> x.name(), equalTo(name)) + ); + } + public static Matcher isPushBoolInstruction(boolean value) { return instanceOf( PushBoolInstruction.class, -- cgit v1.2.3