From 3fcd30f12e7ef31017603f4f954540e6696d77b1 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 8 Mar 2025 11:39:35 +0000 Subject: Compile native type methods from Shed to Shid --- .../ShidCodeGenNativeTypeDeclarationTests.java | 45 +++++++++++++++++++++- .../compiler/shid/instructions/ShidMatchers.java | 2 +- 2 files changed, 45 insertions(+), 2 deletions(-) (limited to 'src/test/java') 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 isShidMethodStore( - SimpleStructType type, + SimpleConcreteType type, String methodName ) { return instanceOf( -- cgit v1.2.3