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 --- .../shedlang/compiler/stackir/StackCompiler.java | 18 ++++++++++ .../compiler/stackir/instructions/Instruction.java | 2 +- .../instructions/ModuleExportInstruction.java | 9 +++++ .../org/shedlang/compiler/ast/typed/Typed.java | 4 +++ .../stackir/StackCompilerNamespaceTests.java | 39 ++++++++++++++++++++++ .../stackir/instructions/InstructionMatchers.java | 7 ++++ 6 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/shedlang/compiler/stackir/instructions/ModuleExportInstruction.java create mode 100644 src/test/java/org/shedlang/compiler/stackir/StackCompilerNamespaceTests.java (limited to 'src') diff --git a/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java b/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java index 406da12c..55cd628f 100644 --- a/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java +++ b/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java @@ -91,6 +91,24 @@ public class StackCompiler { return P.vector(new PushIntInstruction(node.value(), node.source())); } + public static PVector compileNamespace(TypedNamespaceNode node) { + var instructions = node.body().stream() + .map(statement -> compileNamespaceStatement(statement)) + .reduce(P.vector(), PVector::plusAll); + + // TODO: handle exports explicitly + for (var statement : node.body()) { + switch (statement) { + case TypedFunctionDefinitionNode functionDefinition -> + instructions = instructions + .plus(new LocalLoadInstruction(functionDefinition.name(), functionDefinition.source())) + .plus(new ModuleExportInstruction(functionDefinition.name(), functionDefinition.source())); + } + } + + return instructions; + } + public static PVector compileNamespaceStatement(TypedNamespaceStatementNode node) { return switch (node) { case TypedFunctionDefinitionNode functionDefinition -> diff --git a/src/main/java/org/shedlang/compiler/stackir/instructions/Instruction.java b/src/main/java/org/shedlang/compiler/stackir/instructions/Instruction.java index 2ff0eebc..0cd97c83 100644 --- a/src/main/java/org/shedlang/compiler/stackir/instructions/Instruction.java +++ b/src/main/java/org/shedlang/compiler/stackir/instructions/Instruction.java @@ -3,7 +3,7 @@ package org.shedlang.compiler.stackir.instructions; import org.shedlang.compiler.sources.Source; public sealed interface Instruction - permits CallInstruction, DefineFunctionInstruction, DiscardInstruction, LocalLoadInstruction, LocalStoreInstruction, PushBoolInstruction, PushIntInstruction, PushStringInstruction { + permits CallInstruction, DefineFunctionInstruction, DiscardInstruction, LocalLoadInstruction, LocalStoreInstruction, ModuleExportInstruction, PushBoolInstruction, PushIntInstruction, PushStringInstruction { Source source(); } diff --git a/src/main/java/org/shedlang/compiler/stackir/instructions/ModuleExportInstruction.java b/src/main/java/org/shedlang/compiler/stackir/instructions/ModuleExportInstruction.java new file mode 100644 index 00000000..0f41ba04 --- /dev/null +++ b/src/main/java/org/shedlang/compiler/stackir/instructions/ModuleExportInstruction.java @@ -0,0 +1,9 @@ +package org.shedlang.compiler.stackir.instructions; + +import org.shedlang.compiler.sources.Source; + +public record ModuleExportInstruction( + String name, + Source source +) implements Instruction { +} 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