aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2025-01-17 22:42:19 +0000
committerMichael Williamson <mike@zwobble.org>2025-01-17 22:42:19 +0000
commit7c5489a16401738969f87bd1a5d89d0e1e736f99 (patch)
treeff325db6c820f1730623489a821c1b7a0a81baa1 /src/main/java
parent519f9c3b940f4dbdde6ecf7861db9102ed43b8eb (diff)
Include namespace name in ShidDeclareNativeFunction
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/org/shedlang/compiler/shid/ShidCodeGen.java48
-rw-r--r--src/main/java/org/shedlang/compiler/shid/instructions/ShidDeclareNativeFunction.java15
2 files changed, 60 insertions, 3 deletions
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> namespaceName;
private final PVector<ShidInstruction> instructions;
private final PMap<String, ShidVariable> shedVariablesByName;
private final PVector<ShidVariable> boundVariables;
private final Source source;
public BlockContext(
+ Optional<NamespaceName> namespaceName,
PVector<ShidInstruction> instructions,
PMap<String, ShidVariable> shedVariablesByName,
PVector<ShidVariable> 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<BoundVariable> shedVariables, Source source) {
+ public Context enterBlock(
+ List<BoundVariable> shedVariables,
+ Source source
+ ) {
+ return enterBlock(Optional.empty(), shedVariables, source);
+ }
+
+ public Context enterBlock(
+ NamespaceName namespaceName,
+ List<BoundVariable> shedVariables,
+ Source source
+ ) {
+ return enterBlock(
+ Optional.of(namespaceName),
+ shedVariables,
+ source
+ );
+ }
+
+ public Context enterBlock(
+ Optional<NamespaceName> namespaceName,
+ List<BoundVariable> 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>(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
);
}