From 0fa0028daf69bc80a7e3b3ff9c6491fd12b9cc0b Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Mon, 3 Mar 2025 19:07:30 +0000 Subject: Generate Shid for native type method call --- .../compiler/shid/ShidCodeGenCallTests.java | 82 +++++++++++++++++++++- .../compiler/shid/instructions/ShidMatchers.java | 3 +- 2 files changed, 83 insertions(+), 2 deletions(-) (limited to 'src/test/java') diff --git a/src/test/java/org/shedlang/compiler/shid/ShidCodeGenCallTests.java b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenCallTests.java index 6a63be8f..931ba25a 100644 --- a/src/test/java/org/shedlang/compiler/shid/ShidCodeGenCallTests.java +++ b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenCallTests.java @@ -4,6 +4,7 @@ import org.junit.jupiter.api.Test; import org.shedlang.compiler.ast.typed.Typed; import org.shedlang.compiler.types.FunctionType; import org.shedlang.compiler.types.NamespaceName; +import org.shedlang.compiler.types.TypeParam; import org.shedlang.compiler.types.Types; import java.util.List; @@ -43,7 +44,42 @@ public class ShidCodeGenCallTests { } @Test - public void methodCallIsCompiledToDirectCallWithoutMethodReference() { + public void nativeTypeMethodCallIsCompiledToDirectCallWithoutMethodReference() { + var namespaceName = NamespaceName.of("Example", "Project"); + var nativeType = Types.nativeType(namespaceName, "Array"); + var node = Typed.call( + Typed.methodAccess( + Typed.reference("x", nativeType), + "f", + FunctionType.builder().build() + ), + List.of( + Typed.stringLiteral("p1"), + Typed.stringLiteral("p2") + ), + List.of( + Typed.namedArg("x2", Typed.stringLiteral("n2")), + Typed.namedArg("x1", Typed.stringLiteral("n1")) + ) + ); + var context = ShidCodeGen.Context.EMPTY + .addShedVal("x"); + + var result = ShidCodeGen.compileExpression(node, context); + + assertThat(result.instructions(), isSequence( + isShidMethodLoad(nativeType, "f"), + isShidLocalLoad("x__0"), + isShidPushString("p1"), + isShidPushString("p2"), + isShidPushString("n2"), + isShidPushString("n1"), + isShidCall(3, List.of("x2", "x1")) + )); + } + + @Test + public void structMethodCallIsCompiledToDirectCallWithoutMethodReference() { var namespaceName = NamespaceName.of("Example", "Project"); var structType = Types.struct(namespaceName, "User"); var node = Typed.call( @@ -76,4 +112,48 @@ public class ShidCodeGenCallTests { isShidCall(3, List.of("x2", "x1")) )); } + + @Test + public void constructedTypeMethodCallIsCompiledToDirectCallWithoutMethodReference() { + var namespaceName = NamespaceName.of("Example", "Project"); + var simpleStructType = Types.struct(namespaceName, "User"); + var structType = Types.constructedStructType( + Types.typeConstructor( + List.of( + TypeParam.forStructType(simpleStructType, "T") + ), + simpleStructType + ), + List.of(Types.INT) + ); + var node = Typed.call( + Typed.methodAccess( + Typed.reference("x", structType), + "f", + FunctionType.builder().build() + ), + List.of( + Typed.stringLiteral("p1"), + Typed.stringLiteral("p2") + ), + List.of( + Typed.namedArg("x2", Typed.stringLiteral("n2")), + Typed.namedArg("x1", Typed.stringLiteral("n1")) + ) + ); + var context = ShidCodeGen.Context.EMPTY + .addShedVal("x"); + + var result = ShidCodeGen.compileExpression(node, context); + + assertThat(result.instructions(), isSequence( + isShidMethodLoad(simpleStructType, "f"), + isShidLocalLoad("x__0"), + isShidPushString("p1"), + isShidPushString("p2"), + isShidPushString("n2"), + isShidPushString("n1"), + isShidCall(3, List.of("x2", "x1")) + )); + } } 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 715b0b42..96306fd2 100644 --- a/src/test/java/org/shedlang/compiler/shid/instructions/ShidMatchers.java +++ b/src/test/java/org/shedlang/compiler/shid/instructions/ShidMatchers.java @@ -1,6 +1,7 @@ package org.shedlang.compiler.shid.instructions; import org.shedlang.compiler.types.NamespaceName; +import org.shedlang.compiler.types.SimpleConcreteType; import org.shedlang.compiler.types.SimpleStructType; import org.shedlang.compiler.types.UserDefinedEffect; import org.zwobble.precisely.Matcher; @@ -155,7 +156,7 @@ public class ShidMatchers { } public static Matcher isShidMethodLoad( - SimpleStructType type, + SimpleConcreteType type, String methodName ) { return instanceOf( -- cgit v1.2.3