From 2dba29dfa74c32a3102a9317455e16cd9a5a35a7 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 21 Dec 2024 10:40:28 +0000 Subject: Compile methods into free functions --- .../compiler/shid/ShidCodeGenNamespaceTests.java | 44 ++++++++++++++++++++++ .../shid/ShidCodeGenStructDefinitionTests.java | 18 ++++----- .../shedlang/compiler/shid/instructions/Shid.java | 1 - 3 files changed, 52 insertions(+), 11 deletions(-) (limited to 'src/test/java') diff --git a/src/test/java/org/shedlang/compiler/shid/ShidCodeGenNamespaceTests.java b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenNamespaceTests.java index 1e6a1000..e760bdae 100644 --- a/src/test/java/org/shedlang/compiler/shid/ShidCodeGenNamespaceTests.java +++ b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenNamespaceTests.java @@ -7,6 +7,7 @@ import org.shedlang.compiler.types.NamespaceName; import org.shedlang.compiler.types.Types; import java.util.List; +import java.util.Optional; import static org.shedlang.compiler.shid.instructions.ShidMatchers.*; import static org.zwobble.precisely.AssertThat.assertThat; @@ -115,4 +116,47 @@ public class ShidCodeGenNamespaceTests { isShidNamespaceFieldStore(namespaceName, "None") ))); } + + @Test + public void structMethodsAreExported() { + var namespaceName = new NamespaceName(List.of("Example", "Project")); + var structType = Types.struct(namespaceName, "User"); + var node = Typed.namespace( + namespaceName, + List.of( + TypedStructDefinitionNode.builder() + .withType(structType) + .addMethod( + TypedFunctionDefinitionNode.builder() + .withName("name") + .addPositionalParam("this", Optional.empty()) + .build() + ) + .build() + ), + List.of(BoundVariable.val("User")), + List.of() + ); + + var shidNamespace = ShidCodeGen.compileNamespace(node); + + assertThat(shidNamespace.body(), isShidBlock(isSequence( + instanceOf(ShidNamespaceImport.class), + instanceOf(ShidFieldLoad.class), + instanceOf(ShidLocalStore.class), + + instanceOf( + ShidDefineStruct.class, + has("type", x -> x.type(), equalTo(structType)) + ), + isShidLocalStore("User__0"), + instanceOf( + ShidDefineFunction.class, + has("name", x -> x.name(), equalTo("$methods__Example_Project__User__name__0")) + ), + isShidLocalStore("$methods__Example_Project__User__name__0"), + isShidLocalLoad("$methods__Example_Project__User__name__0"), + isShidNamespaceFieldStore(namespaceName, "$methods__Example_Project__User__name__0") + ))); + } } diff --git a/src/test/java/org/shedlang/compiler/shid/ShidCodeGenStructDefinitionTests.java b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenStructDefinitionTests.java index 7db25429..0ffea260 100644 --- a/src/test/java/org/shedlang/compiler/shid/ShidCodeGenStructDefinitionTests.java +++ b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenStructDefinitionTests.java @@ -61,12 +61,12 @@ public class ShidCodeGenStructDefinitionTests { var context = ShidCodeGen.Context.EMPTY .addShedVal("User"); - var result = ShidCodeGen.compileNamespaceStatement(node, context); + var result = ShidCodeGen.compileStructDefinitionMethods(node, context); assertThat(result.instructions(), isSequence( instanceOf( ShidDefineFunction.class, - has("name", x -> x.name(), equalTo("User__name__0")), + has("name", x -> x.name(), equalTo("$methods__Example_Project__User__name__0")), has("positionalParams", x -> x.positionalParams(), isSequence( isShidDefineFunctionParam("this__0"), isShidDefineFunctionParam("a__0") @@ -80,14 +80,12 @@ public class ShidCodeGenStructDefinitionTests { isShidReturn() ))) ), - instanceOf( - ShidDefineStruct.class, - has("type", x -> x.type(), equalTo(structType)), - has("methods", x -> x.methods(), isSequence( - equalTo(new ShidDefineStruct.Method("name", "User__name__0")) - )) - ), - isShidLocalStore("User__0") + isShidLocalStore("$methods__Example_Project__User__name__0"), + isShidLocalLoad("$methods__Example_Project__User__name__0"), + isShidNamespaceFieldStore( + NamespaceName.of("Example", "Project"), + "$methods__Example_Project__User__name__0" + ) )); } } diff --git a/src/test/java/org/shedlang/compiler/shid/instructions/Shid.java b/src/test/java/org/shedlang/compiler/shid/instructions/Shid.java index 1a547eea..ee78a3e3 100644 --- a/src/test/java/org/shedlang/compiler/shid/instructions/Shid.java +++ b/src/test/java/org/shedlang/compiler/shid/instructions/Shid.java @@ -35,7 +35,6 @@ public class Shid { return new ShidDefineStruct( type, fields, - List.of(), Sources.NULL ); } -- cgit v1.2.3