aboutsummaryrefslogtreecommitdiff
path: root/src/test/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-10-21 13:44:25 +0100
committerMichael Williamson <mike@zwobble.org>2023-10-21 13:44:25 +0100
commit317f6fa18dfb5dd0020a086d2815c538fcb558ff (patch)
tree8b6907bfccf8866a3b2f7987aa05b72ce56ffda8 /src/test/java
parent2759cad35ee775aa655f06598172603797571a9e (diff)
Generate stack IR for imports
Diffstat (limited to 'src/test/java')
-rw-r--r--src/test/java/org/shedlang/compiler/ast/typed/Typed.java4
-rw-r--r--src/test/java/org/shedlang/compiler/stackir/StackCompilerNamespaceTests.java28
-rw-r--r--src/test/java/org/shedlang/compiler/stackir/instructions/InstructionMatchers.java9
3 files changed, 38 insertions, 3 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 057e6062..09e7f080 100644
--- a/src/test/java/org/shedlang/compiler/ast/typed/Typed.java
+++ b/src/test/java/org/shedlang/compiler/ast/typed/Typed.java
@@ -90,6 +90,10 @@ public class Typed {
);
}
+ public static TypedImportNode import_(NamespaceName namespaceName) {
+ return new TypedImportNode(namespaceName, Sources.NULL);
+ }
+
public static TypedBinaryOperationNode intAdd(
TypedExpressionNode left,
TypedExpressionNode right
diff --git a/src/test/java/org/shedlang/compiler/stackir/StackCompilerNamespaceTests.java b/src/test/java/org/shedlang/compiler/stackir/StackCompilerNamespaceTests.java
index 4ec958cb..e6206b9a 100644
--- a/src/test/java/org/shedlang/compiler/stackir/StackCompilerNamespaceTests.java
+++ b/src/test/java/org/shedlang/compiler/stackir/StackCompilerNamespaceTests.java
@@ -1,9 +1,7 @@
package org.shedlang.compiler.stackir;
import org.junit.jupiter.api.Test;
-import org.shedlang.compiler.ast.typed.Typed;
-import org.shedlang.compiler.ast.typed.TypedFunctionDefinitionNode;
-import org.shedlang.compiler.ast.typed.TypedStructDefinitionNode;
+import org.shedlang.compiler.ast.typed.*;
import org.shedlang.compiler.stackir.instructions.*;
import org.shedlang.compiler.types.NamespaceName;
import org.shedlang.compiler.types.Types;
@@ -52,6 +50,30 @@ public class StackCompilerNamespaceTests {
}
@Test
+ public void importsAreLoaded() {
+ var node = TypedNamespaceNode.builder()
+ .addImport(Typed.import_(NamespaceName.of("A")))
+ .addBoundVariableName("A")
+ .addImport(Typed.import_(NamespaceName.of("B")))
+ .addBoundVariableName("B")
+ .build();
+
+ var stackIrNamespace = StackCompiler.compileNamespace(node);
+
+ assertThat(stackIrNamespace.instructions(), isSequence(
+ // TODO: test the builtin loading properly
+ instanceOf(NamespaceImportInstruction.class),
+ instanceOf(FieldLoadInstruction.class),
+ instanceOf(LocalStoreInstruction.class),
+
+ isNamespaceImportInstruction(NamespaceName.of("A")),
+ isLocalStoreInstruction("A__0"),
+ isNamespaceImportInstruction(NamespaceName.of("B")),
+ isLocalStoreInstruction("B__0")
+ ));
+ }
+
+ @Test
public void structsAreHoisted() {
var namespaceName = new NamespaceName(List.of("Example"));
var structType = Types.struct(namespaceName, "None");
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 81b35cfe..a33f4f6c 100644
--- a/src/test/java/org/shedlang/compiler/stackir/instructions/InstructionMatchers.java
+++ b/src/test/java/org/shedlang/compiler/stackir/instructions/InstructionMatchers.java
@@ -128,6 +128,15 @@ public class InstructionMatchers {
);
}
+ public static Matcher<Instruction> isNamespaceImportInstruction(
+ NamespaceName namespaceName
+ ) {
+ return instanceOf(
+ NamespaceImportInstruction.class,
+ has("namespaceName", x -> x.namespaceName(), equalTo(namespaceName))
+ );
+ }
+
public static Matcher<Instruction> isPushBoolInstruction(boolean value) {
return instanceOf(
PushBoolInstruction.class,