aboutsummaryrefslogtreecommitdiff
path: root/src/test/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-06-10 10:18:56 +0100
committerMichael Williamson <mike@zwobble.org>2023-06-10 10:18:56 +0100
commit8c346ff940f7336a9426bc61d7f321b0944f6bdc (patch)
tree7ff97dc7756a19d510249c1a3329d11109117961 /src/test/java
parent345b4b4c0b03e2331ffb0b5b33a80bee795e70e2 (diff)
Compile reference to stack instruction
Diffstat (limited to 'src/test/java')
-rw-r--r--src/test/java/org/shedlang/compiler/ast/typed/Typed.java10
-rw-r--r--src/test/java/org/shedlang/compiler/stackir/StackCompilerReferenceTests.java21
-rw-r--r--src/test/java/org/shedlang/compiler/stackir/instructions/InstructionMatchers.java7
3 files changed, 38 insertions, 0 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 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<Instruction> isLocalLoadInstruction(String name) {
+ return instanceOf(
+ LocalLoadInstruction.class,
+ has("name", x -> x.name(), equalTo(name))
+ );
+ }
+
public static Matcher<Instruction> isPushBoolInstruction(boolean value) {
return instanceOf(
PushBoolInstruction.class,