From 78c3d52bc16fefbe35dbceb778f52618078b0854 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Wed, 14 Jun 2023 18:29:10 +0100 Subject: Use field names on namespace node in StackCompiler --- .../org/shedlang/compiler/stackir/StackCompiler.java | 20 ++++++++------------ .../java/org/shedlang/compiler/ast/typed/Typed.java | 10 +++++++++- .../stackir/StackCompilerNamespaceTests.java | 3 ++- 3 files changed, 19 insertions(+), 14 deletions(-) (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 8e66db30..6b8452c1 100644 --- a/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java +++ b/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java @@ -107,18 +107,14 @@ public class StackCompiler { .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 NamespaceFieldStoreInstruction( - node.namespaceName(), - functionDefinition.name(), - functionDefinition.source() - )); - } + for (var fieldName : node.fieldNames()) { + instructions = instructions + .plus(new LocalLoadInstruction(fieldName, node.source())) + .plus(new NamespaceFieldStoreInstruction( + node.namespaceName(), + fieldName, + node.source() + )); } return instructions; 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 ae709dd4..ff87c8e8 100644 --- a/src/test/java/org/shedlang/compiler/ast/typed/Typed.java +++ b/src/test/java/org/shedlang/compiler/ast/typed/Typed.java @@ -51,7 +51,15 @@ public class Typed { NamespaceName namespaceName, List body ) { - return new TypedNamespaceNode(namespaceName, body, List.of(), Sources.NULL); + return namespace(namespaceName, body, List.of()); + } + + public static TypedNamespaceNode namespace( + NamespaceName namespaceName, + List body, + List fieldNames + ) { + return new TypedNamespaceNode(namespaceName, body, fieldNames, Sources.NULL); } public static TypedStringLiteralNode stringLiteral(String value) { diff --git a/src/test/java/org/shedlang/compiler/stackir/StackCompilerNamespaceTests.java b/src/test/java/org/shedlang/compiler/stackir/StackCompilerNamespaceTests.java index 32ff72ba..1ed17236 100644 --- a/src/test/java/org/shedlang/compiler/stackir/StackCompilerNamespaceTests.java +++ b/src/test/java/org/shedlang/compiler/stackir/StackCompilerNamespaceTests.java @@ -27,7 +27,8 @@ public class StackCompilerNamespaceTests { .addNamedParam("y", Typed.typeLevelReference("Int", Types.INT)) .addStatement(Typed.expressionStatement(Typed.stringLiteral("hello"))) .build() - ) + ), + List.of("f") ); var instructions = StackCompiler.compileNamespace(node); -- cgit v1.2.3