aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2025-03-08 11:39:35 +0000
committerMichael Williamson <mike@zwobble.org>2025-03-08 11:39:35 +0000
commit3fcd30f12e7ef31017603f4f954540e6696d77b1 (patch)
tree9724a46a51d646cfb91abfe0141d7614d1ef09b3 /src
parent2d03ff53b4f1c73bfdb02045d5b632a80f8caeeb (diff)
Compile native type methods from Shed to Shid
Diffstat (limited to 'src')
-rw-r--r--src/main/java/org/shedlang/compiler/shid/ShidCodeGen.java63
-rw-r--r--src/test/java/org/shedlang/compiler/shid/ShidCodeGenNativeTypeDeclarationTests.java45
-rw-r--r--src/test/java/org/shedlang/compiler/shid/instructions/ShidMatchers.java2
3 files changed, 101 insertions, 9 deletions
diff --git a/src/main/java/org/shedlang/compiler/shid/ShidCodeGen.java b/src/main/java/org/shedlang/compiler/shid/ShidCodeGen.java
index 9987944a..71f43610 100644
--- a/src/main/java/org/shedlang/compiler/shid/ShidCodeGen.java
+++ b/src/main/java/org/shedlang/compiler/shid/ShidCodeGen.java
@@ -613,6 +613,8 @@ public class ShidCodeGen {
if (statement instanceof TypedStructDefinitionNode structDefinitionNode) {
// TODO: test method hoisting
context = compileStructDefinitionMethods(structDefinitionNode, context);
+ } else if (statement instanceof TypedNativeTypeDeclarationNode nativeTypeDeclarationNode) {
+ context = compileNativeTypeDeclarationMethods(nativeTypeDeclarationNode, context);
}
}
@@ -665,22 +667,38 @@ public class ShidCodeGen {
Context context
) {
var signature = node.signature();
- context = context.addInstruction(new ShidDeclareNativeFunction(
+ context = compileNativeFunction(
context.namespaceName(),
node.name(),
+ signature,
+ node.source(),
+ context
+ );
+
+ var localName = context.shedVariableToLocal(node.name());
+ var storeInstruction = new ShidLocalStore(localName, node.source());
+
+ return context.addInstruction(storeInstruction);
+ }
+
+ private static Context compileNativeFunction(
+ NamespaceName namespaceName,
+ String name,
+ TypedFunctionTypeNode signature,
+ Source source,
+ Context context
+ ) {
+ return context.addInstruction(new ShidDeclareNativeFunction(
+ namespaceName,
+ name,
IntStream.range(0, signature.value().positionalParams().size())
.mapToObj(paramIndex -> new ShidParam("arg" + paramIndex, signature.source()))
.toList(),
signature.value().namedParams().stream()
.map(param -> new ShidParam(param.name(), signature.source()))
.toList(),
- node.source()
+ source
));
-
- var localName = context.shedVariableToLocal(node.name());
- var storeInstruction = new ShidLocalStore(localName, node.source());
-
- return context.addInstruction(storeInstruction);
}
private static Context compileNativeTypeDeclaration(
@@ -700,6 +718,37 @@ public class ShidCodeGen {
return context.addInstruction(storeInstruction);
}
+ static Context compileNativeTypeDeclarationMethods(
+ TypedNativeTypeDeclarationNode node,
+ Context context
+ ) {
+ var type = node.typeOrConstructor().toType();
+
+ for (var methodNode : node.methods()) {
+ context = compileNativeFunction(
+ type.namespaceName(),
+ String.format(
+ "method__%s__%s",
+ type.name(),
+ methodNode.name()
+ ),
+ methodNode.signature(),
+ methodNode.source(),
+ context
+ );
+
+ context = context.addInstruction(
+ new ShidMethodStore(
+ type,
+ methodNode.name(),
+ methodNode.source()
+ )
+ );
+ }
+
+ return context;
+ }
+
private static Context compileReference(TypedReferenceNode node, Context context) {
var shidVariable = context.shedVariableToShidVariable(node.name());
diff --git a/src/test/java/org/shedlang/compiler/shid/ShidCodeGenNativeTypeDeclarationTests.java b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenNativeTypeDeclarationTests.java
index 6a4070c5..d49d9762 100644
--- a/src/test/java/org/shedlang/compiler/shid/ShidCodeGenNativeTypeDeclarationTests.java
+++ b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenNativeTypeDeclarationTests.java
@@ -1,12 +1,15 @@
package org.shedlang.compiler.shid;
import org.junit.jupiter.api.Test;
+import org.shedlang.compiler.ast.typed.TypedNativeFunctionDeclarationNode;
import org.shedlang.compiler.ast.typed.TypedNativeTypeDeclarationNode;
+import org.shedlang.compiler.shid.instructions.ShidDeclareNativeFunction;
import org.shedlang.compiler.shid.instructions.ShidDeclareNativeType;
+import org.shedlang.compiler.types.FunctionType;
import org.shedlang.compiler.types.NamespaceName;
import org.shedlang.compiler.types.Types;
-import static org.shedlang.compiler.shid.instructions.ShidMatchers.isShidLocalStore;
+import static org.shedlang.compiler.shid.instructions.ShidMatchers.*;
import static org.zwobble.precisely.AssertThat.assertThat;
import static org.zwobble.precisely.Matchers.*;
@@ -30,4 +33,44 @@ public class ShidCodeGenNativeTypeDeclarationTests {
isShidLocalStore("Array__0")
));
}
+
+ @Test
+ public void nativeTypeMethodsAreCompiledToFreeFunctionDeclarations() {
+ var nativeType = Types.nativeType(NamespaceName.of("Example", "Project"), "User");
+ var node = TypedNativeTypeDeclarationNode.builder()
+ .withType(nativeType)
+ .addMethod(
+ TypedNativeFunctionDeclarationNode.builder()
+ .withName("name")
+ .withSignature(
+ FunctionType.builder()
+ .addPositionalParam(nativeType)
+ .addPositionalParam(Types.BOOL)
+ .addNamedParam("b", Types.INT)
+ .withReturnType(Types.STRING)
+ .build()
+ )
+ .build()
+ )
+ .build();
+ var context = ShidCodeGen.Context.EMPTY
+ .addShedVal("User");
+
+ var result = ShidCodeGen.compileNativeTypeDeclarationMethods(node, context);
+
+ assertThat(result.instructions(), isSequence(
+ instanceOf(
+ ShidDeclareNativeFunction.class,
+ has("name", x -> x.name(), equalTo("method__User__name")),
+ has("positionalParams", x -> x.positionalParams(), isSequence(
+ isShidDefineFunctionParam("arg0"),
+ isShidDefineFunctionParam("arg1")
+ )),
+ has("namedParams", x -> x.namedParams(), isSequence(
+ isShidDefineFunctionParam("b")
+ ))
+ ),
+ isShidMethodStore(nativeType, "name")
+ ));
+ }
}
diff --git a/src/test/java/org/shedlang/compiler/shid/instructions/ShidMatchers.java b/src/test/java/org/shedlang/compiler/shid/instructions/ShidMatchers.java
index 96306fd2..c41858c6 100644
--- a/src/test/java/org/shedlang/compiler/shid/instructions/ShidMatchers.java
+++ b/src/test/java/org/shedlang/compiler/shid/instructions/ShidMatchers.java
@@ -167,7 +167,7 @@ public class ShidMatchers {
}
public static Matcher<ShidInstruction> isShidMethodStore(
- SimpleStructType type,
+ SimpleConcreteType type,
String methodName
) {
return instanceOf(