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 --- .../org/shedlang/compiler/ast/typed/Typed.java | 4 +++ .../stackir/StackCompilerNamespaceTests.java | 39 ++++++++++++++++++++++ .../stackir/instructions/InstructionMatchers.java | 7 ++++ 3 files changed, 50 insertions(+) create mode 100644 src/test/java/org/shedlang/compiler/stackir/StackCompilerNamespaceTests.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 9d6cc3c3..3b12bac6 100644 --- a/src/test/java/org/shedlang/compiler/ast/typed/Typed.java +++ b/src/test/java/org/shedlang/compiler/ast/typed/Typed.java @@ -42,6 +42,10 @@ public class Typed { return new TypedNamedArgNode(name, expression, Sources.NULL); } + public static TypedNamespaceNode namespace(List body) { + return new TypedNamespaceNode(body, Sources.NULL); + } + public static TypedStringLiteralNode stringLiteral(String value) { return new TypedStringLiteralNode(value, Sources.NULL); } 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