From 7c5489a16401738969f87bd1a5d89d0e1e736f99 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Fri, 17 Jan 2025 22:42:19 +0000 Subject: Include namespace name in ShidDeclareNativeFunction --- .../org/shedlang/compiler/shid/ShidCodeGen.java | 48 +++++++++++++++++++++- .../instructions/ShidDeclareNativeFunction.java | 15 ++++++- 2 files changed, 60 insertions(+), 3 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/org/shedlang/compiler/shid/ShidCodeGen.java b/src/main/java/org/shedlang/compiler/shid/ShidCodeGen.java index a7fcff19..f6830ae5 100644 --- a/src/main/java/org/shedlang/compiler/shid/ShidCodeGen.java +++ b/src/main/java/org/shedlang/compiler/shid/ShidCodeGen.java @@ -13,6 +13,7 @@ import org.shedlang.compiler.util.P; import java.text.MessageFormat; import java.util.ArrayList; import java.util.List; +import java.util.Optional; import java.util.stream.Stream; public class ShidCodeGen { @@ -521,7 +522,11 @@ public class ShidCodeGen { } public static ShidNamespace compileNamespace(TypedNamespaceNode node) { - var context = Context.EMPTY.enterBlock(node.boundVariables(), node.source()); + var context = Context.EMPTY.enterBlock( + node.namespaceName(), + node.boundVariables(), + node.source() + ); context = context.addShedVal(ShedBuiltins.PRINT_NAME); var printLocalName = context.shedVariableToLocal(ShedBuiltins.PRINT_NAME); @@ -624,6 +629,7 @@ public class ShidCodeGen { Context context ) { context = context.addInstruction(new ShidDeclareNativeFunction( + context.namespaceName(), node.name(), node.source() )); @@ -932,23 +938,27 @@ public class ShidCodeGen { private static class BlockContext { public static final BlockContext EMPTY = new BlockContext( + Optional.empty(), P.vector(), P.map(), P.vector(), Sources.NULL ); + private final Optional namespaceName; private final PVector instructions; private final PMap shedVariablesByName; private final PVector boundVariables; private final Source source; public BlockContext( + Optional namespaceName, PVector instructions, PMap shedVariablesByName, PVector boundVariables, Source source ) { + this.namespaceName = namespaceName; this.instructions = instructions; this.shedVariablesByName = shedVariablesByName; this.boundVariables = boundVariables; @@ -962,6 +972,7 @@ public class ShidCodeGen { public BlockContext addInstruction(ShidInstruction instruction) { return new BlockContext( + this.namespaceName, this.instructions.plus(instruction), this.shedVariablesByName, this.boundVariables, @@ -981,6 +992,7 @@ public class ShidCodeGen { public BlockContext addShedVariable(String shedName, ShidVariable shidVariable) { return new BlockContext( + this.namespaceName, this.instructions, this.shedVariablesByName.plus(shedName, shidVariable), this.boundVariables.plus(shidVariable), @@ -990,6 +1002,7 @@ public class ShidCodeGen { public BlockContext addShidVal(String localName) { return new BlockContext( + this.namespaceName, this.instructions, this.shedVariablesByName, this.boundVariables.plus(new ShidVariable(localName, ShidVariable.Variant.VAL)), @@ -1020,10 +1033,34 @@ public class ShidCodeGen { this.localNames = localNames; } - public Context enterBlock(List shedVariables, Source source) { + public Context enterBlock( + List shedVariables, + Source source + ) { + return enterBlock(Optional.empty(), shedVariables, source); + } + + public Context enterBlock( + NamespaceName namespaceName, + List shedVariables, + Source source + ) { + return enterBlock( + Optional.of(namespaceName), + shedVariables, + source + ); + } + + public Context enterBlock( + Optional namespaceName, + List shedVariables, + Source source + ) { var context = new Context( this.blockStack.plus(this.block), new BlockContext( + namespaceName, P.vector(), this.block.shedVariablesByName, P.vector(), @@ -1116,6 +1153,13 @@ public class ShidCodeGen { return new Result<>(value, this); } + public NamespaceName namespaceName() { + // TODO: better error + // TODO: walk up the block stack? Or make it clear that this will + // only get the namespace for the immediate block. + return this.block.namespaceName.orElseThrow(); + } + public record Result(T value, Context context) {} } diff --git a/src/main/java/org/shedlang/compiler/shid/instructions/ShidDeclareNativeFunction.java b/src/main/java/org/shedlang/compiler/shid/instructions/ShidDeclareNativeFunction.java index 47bc6e01..9f88ca5f 100644 --- a/src/main/java/org/shedlang/compiler/shid/instructions/ShidDeclareNativeFunction.java +++ b/src/main/java/org/shedlang/compiler/shid/instructions/ShidDeclareNativeFunction.java @@ -2,12 +2,14 @@ package org.shedlang.compiler.shid.instructions; import org.shedlang.compiler.sources.Source; import org.shedlang.compiler.sources.Sources; +import org.shedlang.compiler.types.NamespaceName; /** * Push a native function value onto the temporary value stack with the given * declaration. */ public record ShidDeclareNativeFunction( + NamespaceName namespaceName, String name, Source source ) implements ShidInstruction { @@ -18,22 +20,33 @@ public record ShidDeclareNativeFunction( public static Builder builder() { return new Builder( + NamespaceName.of(), "f" ); } public record Builder( + NamespaceName namespaceName, String name ) { public ShidDeclareNativeFunction build() { return new ShidDeclareNativeFunction( - name, + this.namespaceName, + this.name, Sources.NULL ); } + public Builder withNamespaceName(NamespaceName namespaceName) { + return new Builder( + namespaceName, + this.name + ); + } + public Builder withName(String name) { return new Builder( + this.namespaceName, name ); } -- cgit v1.2.3