aboutsummaryrefslogtreecommitdiff
path: root/src/test/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2025-03-03 19:07:30 +0000
committerMichael Williamson <mike@zwobble.org>2025-03-03 19:07:30 +0000
commit0fa0028daf69bc80a7e3b3ff9c6491fd12b9cc0b (patch)
tree29f5e1dd9291cc9bdcfcec4ab5d6f21e556337ea /src/test/java
parent5c18fa22c6d30337bd389dd94270426476f06430 (diff)
Generate Shid for native type method call
Diffstat (limited to 'src/test/java')
-rw-r--r--src/test/java/org/shedlang/compiler/shid/ShidCodeGenCallTests.java82
-rw-r--r--src/test/java/org/shedlang/compiler/shid/instructions/ShidMatchers.java3
2 files changed, 83 insertions, 2 deletions
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<ShidInstruction> isShidMethodLoad(
- SimpleStructType type,
+ SimpleConcreteType type,
String methodName
) {
return instanceOf(