From 062175737e6f7f3971075a7446aeae76946b5c9e Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 10 Jun 2023 18:40:52 +0100 Subject: Compile namespace to stack instructions --- .../stackir/StackCompilerNamespaceTests.java | 39 ++++++++++++++++++++++ .../stackir/instructions/InstructionMatchers.java | 7 ++++ 2 files changed, 46 insertions(+) create mode 100644 src/test/java/org/shedlang/compiler/stackir/StackCompilerNamespaceTests.java (limited to 'src/test/java/org/shedlang/compiler/stackir') diff --git a/src/test/java/org/shedlang/compiler/stackir/StackCompilerNamespaceTests.java b/src/test/java/org/shedlang/compiler/stackir/StackCompilerNamespaceTests.java new file mode 100644 index 00000000..88a91157 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/stackir/StackCompilerNamespaceTests.java @@ -0,0 +1,39 @@ +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.stackir.instructions.DefineFunctionInstruction; +import org.shedlang.compiler.types.Types; + +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.*; + +public class StackCompilerNamespaceTests { + @Test + public void bodyOfNamespaceIsCompiledWithNamesBeingExported() { + var node = Typed.namespace(List.of( + TypedFunctionDefinitionNode.builder() + .withName("f") + .addPositionalParam("x", Typed.typeLevelReference("String", Types.STRING)) + .addNamedParam("y", Typed.typeLevelReference("Int", Types.INT)) + .addStatement(Typed.expressionStatement(Typed.stringLiteral("hello"))) + .build() + )); + + var instructions = StackCompiler.compileNamespace(node); + + assertThat(instructions, isSequence( + instanceOf( + DefineFunctionInstruction.class, + has("name", x -> x.name(), equalTo("f")) + ), + isLocalStoreInstruction("f"), + isLocalLoadInstruction("f"), + isModuleExportInstruction("f") + )); + } +} 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 4ae1e793..99fc7d5b 100644 --- a/src/test/java/org/shedlang/compiler/stackir/instructions/InstructionMatchers.java +++ b/src/test/java/org/shedlang/compiler/stackir/instructions/InstructionMatchers.java @@ -43,6 +43,13 @@ public class InstructionMatchers { ); } + public static Matcher isModuleExportInstruction(String name) { + return instanceOf( + ModuleExportInstruction.class, + has("name", x -> x.name(), equalTo(name)) + ); + } + public static Matcher isPushBoolInstruction(boolean value) { return instanceOf( PushBoolInstruction.class, -- cgit v1.2.3