aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-06-10 18:40:52 +0100
committerMichael Williamson <mike@zwobble.org>2023-06-10 18:40:52 +0100
commit062175737e6f7f3971075a7446aeae76946b5c9e (patch)
tree842624b72556678731493cd2123be79442d76363 /src
parent33cec419ce442aaaf8559abdd5920cd61699104e (diff)
Compile namespace to stack instructions
Diffstat (limited to 'src')
-rw-r--r--src/main/java/org/shedlang/compiler/stackir/StackCompiler.java18
-rw-r--r--src/main/java/org/shedlang/compiler/stackir/instructions/Instruction.java2
-rw-r--r--src/main/java/org/shedlang/compiler/stackir/instructions/ModuleExportInstruction.java9
-rw-r--r--src/test/java/org/shedlang/compiler/ast/typed/Typed.java4
-rw-r--r--src/test/java/org/shedlang/compiler/stackir/StackCompilerNamespaceTests.java39
-rw-r--r--src/test/java/org/shedlang/compiler/stackir/instructions/InstructionMatchers.java7
6 files changed, 78 insertions, 1 deletions
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<Instruction> 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<Instruction> 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<TypedNamespaceStatementNode> 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<Instruction> isModuleExportInstruction(String name) {
+ return instanceOf(
+ ModuleExportInstruction.class,
+ has("name", x -> x.name(), equalTo(name))
+ );
+ }
+
public static Matcher<Instruction> isPushBoolInstruction(boolean value) {
return instanceOf(
PushBoolInstruction.class,