diff options
| author | Michael Williamson <mike@zwobble.org> | 2024-12-20 14:21:22 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2024-12-20 14:21:22 +0000 |
| commit | f570b54bd3e0fe6ebc134101e3c54be5bc49253d (patch) | |
| tree | 289b1e99b9e0347dec1c3cb4c42f257696a75df7 /src/test/java | |
| parent | 1527c9dcaf66d500b1ccf4c3b26cd70b37089269 (diff) | |
Add Shid code gen for methods on structs
Diffstat (limited to 'src/test/java')
| -rw-r--r-- | src/test/java/org/shedlang/compiler/shid/ShidCodeGenStructDefinitionTests.java | 55 | ||||
| -rw-r--r-- | src/test/java/org/shedlang/compiler/shid/instructions/Shid.java | 1 |
2 files changed, 55 insertions, 1 deletions
diff --git a/src/test/java/org/shedlang/compiler/shid/ShidCodeGenStructDefinitionTests.java b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenStructDefinitionTests.java index c3a932df..7db25429 100644 --- a/src/test/java/org/shedlang/compiler/shid/ShidCodeGenStructDefinitionTests.java +++ b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenStructDefinitionTests.java @@ -2,12 +2,16 @@ package org.shedlang.compiler.shid; import org.junit.jupiter.api.Test; import org.shedlang.compiler.ast.typed.Typed; +import org.shedlang.compiler.ast.typed.TypedFunctionDefinitionNode; import org.shedlang.compiler.ast.typed.TypedStructDefinitionNode; +import org.shedlang.compiler.shid.instructions.ShidDefineFunction; import org.shedlang.compiler.shid.instructions.ShidDefineStruct; import org.shedlang.compiler.types.NamespaceName; import org.shedlang.compiler.types.Types; -import static org.shedlang.compiler.shid.instructions.ShidMatchers.isShidLocalStore; +import java.util.Optional; + +import static org.shedlang.compiler.shid.instructions.ShidMatchers.*; import static org.zwobble.precisely.AssertThat.assertThat; import static org.zwobble.precisely.Matchers.*; @@ -37,4 +41,53 @@ public class ShidCodeGenStructDefinitionTests { isShidLocalStore("User__0") )); } + + @Test + public void structMethodsAreCompiledToFreeFunctions() { + var structType = Types.struct(NamespaceName.of("Example", "Project"), "User"); + var node = TypedStructDefinitionNode.builder() + .withType(structType) + .addMethod( + TypedFunctionDefinitionNode.builder() + .withName("name") + .addPositionalParam("this", Optional.empty()) + .addPositionalParam("a", Typed.typeLevelReference("Bool", Types.BOOL)) + .addNamedParam("b", Typed.typeLevelReference("Int", Types.INT)) + .withReturnType(Typed.typeLevelReference("String", Types.STRING)) + .addStatement(Typed.blockReturn(Typed.stringLiteral("hello"))) + .build() + ) + .build(); + var context = ShidCodeGen.Context.EMPTY + .addShedVal("User"); + + var result = ShidCodeGen.compileNamespaceStatement(node, context); + + assertThat(result.instructions(), isSequence( + instanceOf( + ShidDefineFunction.class, + has("name", x -> x.name(), equalTo("User__name__0")), + has("positionalParams", x -> x.positionalParams(), isSequence( + isShidDefineFunctionParam("this__0"), + isShidDefineFunctionParam("a__0") + )), + has("namedParams", x -> x.namedParams(), isSequence( + isShidDefineFunctionParam("b__0") + )), + has("freeVariables", x -> x.freeVariables(), containsExactly()), + has("body", x -> x.body(), isShidBlock(isSequence( + isShidPushString("hello"), + 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") + )); + } } 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 ee78a3e3..1a547eea 100644 --- a/src/test/java/org/shedlang/compiler/shid/instructions/Shid.java +++ b/src/test/java/org/shedlang/compiler/shid/instructions/Shid.java @@ -35,6 +35,7 @@ public class Shid { return new ShidDefineStruct( type, fields, + List.of(), Sources.NULL ); } |
