aboutsummaryrefslogtreecommitdiff
path: root/src/test/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2025-02-07 21:21:17 +0000
committerMichael Williamson <mike@zwobble.org>2025-02-09 17:05:55 +0000
commite0b28ade710475893d4a2855371b3478c195fd3b (patch)
tree940d54508843ef97815424fb124797a1253a0bad /src/test/java
parent62fe16bbb26f80b6f8c49791fe8752c292d48cf7 (diff)
Use function type parsing for native function declarations
Diffstat (limited to 'src/test/java')
-rw-r--r--src/test/java/org/shedlang/compiler/ast/typed/TypedNativeFunctionDeclarationNodeMatcher.java21
-rw-r--r--src/test/java/org/shedlang/compiler/ast/untyped/UntypedNativeFunctionDeclarationNodeMatcher.java2
-rw-r--r--src/test/java/org/shedlang/compiler/parser/ParserNativeFunctionDeclarationTests.java7
-rw-r--r--src/test/java/org/shedlang/compiler/parser/ParserNativeTypeDeclarationTests.java8
-rw-r--r--src/test/java/org/shedlang/compiler/shid/ShidCodeGenNativeFunctionDeclarationTests.java19
-rw-r--r--src/test/java/org/shedlang/compiler/typechecker/TypeCheckerNativeFunctionDeclarationTests.java45
6 files changed, 32 insertions, 70 deletions
diff --git a/src/test/java/org/shedlang/compiler/ast/typed/TypedNativeFunctionDeclarationNodeMatcher.java b/src/test/java/org/shedlang/compiler/ast/typed/TypedNativeFunctionDeclarationNodeMatcher.java
index 94c8c65a..fa49b840 100644
--- a/src/test/java/org/shedlang/compiler/ast/typed/TypedNativeFunctionDeclarationNodeMatcher.java
+++ b/src/test/java/org/shedlang/compiler/ast/typed/TypedNativeFunctionDeclarationNodeMatcher.java
@@ -6,9 +6,8 @@ import org.shedlang.compiler.testing.MatcherBuilder;
import org.shedlang.compiler.types.FunctionType;
import org.zwobble.precisely.Matcher;
-import java.util.Optional;
-
-import static org.zwobble.precisely.Matchers.*;
+import static org.zwobble.precisely.Matchers.equalTo;
+import static org.zwobble.precisely.Matchers.has;
public class TypedNativeFunctionDeclarationNodeMatcher extends MatcherBuilder<
TypedNamespaceStatementNode,
@@ -37,23 +36,11 @@ public class TypedNativeFunctionDeclarationNodeMatcher extends MatcherBuilder<
return new TypedNativeFunctionDeclarationNodeMatcher(submatchers);
}
- public TypedNativeFunctionDeclarationNodeMatcher withEffect(Matcher<Optional<? extends TypedTypeLevelExpressionNode<?>>> effect) {
- return with(has("effect", x -> x.signature().effect(), effect));
- }
-
public TypedNativeFunctionDeclarationNodeMatcher withName(String name) {
return with(has("name", x -> x.name(), equalTo(name)));
}
- public TypedNativeFunctionDeclarationNodeMatcher withParams(Matcher<TypedParamsNode> params) {
- return with(has("params", x -> x.signature().params(), params));
- }
-
- public TypedNativeFunctionDeclarationNodeMatcher withReturnType(Matcher<TypedTypeLevelExpressionNode<?>> returnType) {
- return with(has("returnType", x -> x.signature().returnType(), isOptionalOf(returnType)));
- }
-
- public TypedNativeFunctionDeclarationNodeMatcher withType(FunctionType type) {
- return with(has("type", x -> x.type(), equalTo(type)));
+ public TypedNativeFunctionDeclarationNodeMatcher withSignature(FunctionType signature) {
+ return with(has("signature", x -> x.signature().value(), equalTo(signature)));
}
}
diff --git a/src/test/java/org/shedlang/compiler/ast/untyped/UntypedNativeFunctionDeclarationNodeMatcher.java b/src/test/java/org/shedlang/compiler/ast/untyped/UntypedNativeFunctionDeclarationNodeMatcher.java
index 9eb338fd..4afc03d1 100644
--- a/src/test/java/org/shedlang/compiler/ast/untyped/UntypedNativeFunctionDeclarationNodeMatcher.java
+++ b/src/test/java/org/shedlang/compiler/ast/untyped/UntypedNativeFunctionDeclarationNodeMatcher.java
@@ -37,7 +37,7 @@ public class UntypedNativeFunctionDeclarationNodeMatcher extends MatcherBuilder<
}
public UntypedNativeFunctionDeclarationNodeMatcher withSignature(
- Matcher<? super UntypedFunctionSignatureNode> signature
+ Matcher<? super UntypedFunctionTypeNode> signature
) {
return with(has("signature", x -> x.signature(), signature));
}
diff --git a/src/test/java/org/shedlang/compiler/parser/ParserNativeFunctionDeclarationTests.java b/src/test/java/org/shedlang/compiler/parser/ParserNativeFunctionDeclarationTests.java
index f11a673c..bf7621e5 100644
--- a/src/test/java/org/shedlang/compiler/parser/ParserNativeFunctionDeclarationTests.java
+++ b/src/test/java/org/shedlang/compiler/parser/ParserNativeFunctionDeclarationTests.java
@@ -22,8 +22,11 @@ public class ParserNativeFunctionDeclarationTests {
isUntypedNativeFunctionDeclarationNode()
.withName("f")
.withSignature(
- isUntypedFunctionSignatureNode()
- .withParams(isUntypedParamsNode(isSequence(), isSequence()))
+ isUntypedFunctionTypeNode()
+ .withParams(isUntypedFunctionTypeParamsNode(
+ isSequence(),
+ isSequence()
+ ))
.withEffect(isOptionalEmpty())
.withReturnType(isUntypedTypeLevelReferenceNode("Unit"))
)
diff --git a/src/test/java/org/shedlang/compiler/parser/ParserNativeTypeDeclarationTests.java b/src/test/java/org/shedlang/compiler/parser/ParserNativeTypeDeclarationTests.java
index 900ac1aa..8aeec6e2 100644
--- a/src/test/java/org/shedlang/compiler/parser/ParserNativeTypeDeclarationTests.java
+++ b/src/test/java/org/shedlang/compiler/parser/ParserNativeTypeDeclarationTests.java
@@ -137,7 +137,7 @@ public class ParserNativeTypeDeclarationTests {
public void canParseNativeTypeWithSingleMethod() {
var source = """
native type X {
- native fun a(this);
+ native fun a(this) -> Unit;
}
""";
@@ -156,11 +156,11 @@ public class ParserNativeTypeDeclarationTests {
public void canParseNativeTypeWithMultipleMethods() {
var source = """
native type X {
- native fun a(this);
+ native fun a(this) -> Unit;
- native fun b(this);
+ native fun b(this) -> Unit;
- native fun c(this);
+ native fun c(this) -> Unit;
}
""";
diff --git a/src/test/java/org/shedlang/compiler/shid/ShidCodeGenNativeFunctionDeclarationTests.java b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenNativeFunctionDeclarationTests.java
index 03948076..d0dadeff 100644
--- a/src/test/java/org/shedlang/compiler/shid/ShidCodeGenNativeFunctionDeclarationTests.java
+++ b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenNativeFunctionDeclarationTests.java
@@ -4,10 +4,11 @@ import org.junit.jupiter.api.Test;
import org.shedlang.compiler.ast.typed.TypedNativeFunctionDeclarationNode;
import org.shedlang.compiler.shid.instructions.ShidDeclareNativeFunction;
import org.shedlang.compiler.sources.Sources;
+import org.shedlang.compiler.types.FunctionType;
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.isShidDefineFunctionParam;
import static org.shedlang.compiler.shid.instructions.ShidMatchers.isShidLocalStore;
@@ -19,10 +20,14 @@ public class ShidCodeGenNativeFunctionDeclarationTests {
public void nativeFunctionIsCompiledToDeclareNativeFunctionInstruction() {
var node = TypedNativeFunctionDeclarationNode.builder()
.withName("f")
- .addPositionalParam("p1", Optional.empty())
- .addPositionalParam("p2", Optional.empty())
- .addNamedParam("n1", Optional.empty())
- .addNamedParam("n2", Optional.empty())
+ .withSignature(
+ FunctionType.builder()
+ .addPositionalParam(Types.INT)
+ .addPositionalParam(Types.INT)
+ .addNamedParam("n1", Types.INT)
+ .addNamedParam("n2", Types.INT)
+ .build()
+ )
.build();
var context = ShidCodeGen.Context.EMPTY
.enterBlock(
@@ -40,8 +45,8 @@ public class ShidCodeGenNativeFunctionDeclarationTests {
has("namespaceName", x -> x.namespaceName(), equalTo(NamespaceName.of("Example", "Project"))),
has("name", x -> x.name(), equalTo("f")),
has("positionalParams", x -> x.positionalParams(), isSequence(
- isShidDefineFunctionParam("p1"),
- isShidDefineFunctionParam("p2")
+ isShidDefineFunctionParam("arg0"),
+ isShidDefineFunctionParam("arg1")
)),
has("namedParams", x -> x.namedParams(), isSequence(
isShidDefineFunctionParam("n1"),
diff --git a/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerNativeFunctionDeclarationTests.java b/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerNativeFunctionDeclarationTests.java
index b6a5ce9d..7d807f92 100644
--- a/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerNativeFunctionDeclarationTests.java
+++ b/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerNativeFunctionDeclarationTests.java
@@ -3,21 +3,15 @@ package org.shedlang.compiler.typechecker;
import org.junit.jupiter.api.Test;
import org.shedlang.compiler.ast.untyped.Untyped;
import org.shedlang.compiler.ast.untyped.UntypedNativeFunctionDeclarationNode;
-import org.shedlang.compiler.typechecker.errors.FunctionSignatureMissingParamTypeError;
-import org.shedlang.compiler.typechecker.errors.FunctionSignatureMissingReturnTypeError;
import org.shedlang.compiler.types.FunctionType;
import org.shedlang.compiler.types.Types;
import org.shedlang.compiler.util.P;
import java.util.Map;
-import java.util.Optional;
-import static org.junit.jupiter.api.Assertions.assertThrows;
-import static org.shedlang.compiler.ast.typed.TypedNodeMatchers.*;
+import static org.shedlang.compiler.ast.typed.TypedNodeMatchers.isTypedNativeFunctionDeclarationNode;
import static org.shedlang.compiler.typechecker.TypeCheckerTesting.typeCheckNamespaceStatement;
import static org.zwobble.precisely.AssertThat.assertThat;
-import static org.zwobble.precisely.Matchers.isOptionalEmpty;
-import static org.zwobble.precisely.Matchers.isSequence;
public class TypeCheckerNativeFunctionDeclarationTests {
@Test
@@ -35,38 +29,11 @@ public class TypeCheckerNativeFunctionDeclarationTests {
typed.value(),
isTypedNativeFunctionDeclarationNode()
.withName("f")
- .withParams(isTypedParamsNode(isSequence(), isSequence()))
- .withEffect(isOptionalEmpty())
- .withReturnType(isTypedTypeLevelReferenceNode("Unit", Types.UNIT))
- .withType(FunctionType.builder().withReturnType(Types.UNIT).build())
- );
- }
-
- @Test
- public void whenSignatureIsMissingReturnTypeThenErrorIsThrown() {
- var untyped = UntypedNativeFunctionDeclarationNode.builder()
- .withoutReturnType()
- .build();
- var context = TypeCheckerContext.EMPTY;
-
- assertThrows(
- FunctionSignatureMissingReturnTypeError.class,
- () -> typeCheckNamespaceStatement(untyped, context)
- );
- }
-
- @Test
- public void whenSignatureIsMissingParamTypeThenErrorIsThrown() {
- var untyped = UntypedNativeFunctionDeclarationNode.builder()
- .addPositionalParam("x", Optional.empty())
- .withReturnType(Untyped.typeLevelReference("Unit"))
- .build();
- var context = TypeCheckerContext.EMPTY
- .enterGlobal(P.map(Map.entry("Unit", Types.metaType(Types.UNIT))));
-
- assertThrows(
- FunctionSignatureMissingParamTypeError.class,
- () -> typeCheckNamespaceStatement(untyped, context)
+ .withSignature(
+ FunctionType.builder()
+ .withReturnType(Types.UNIT)
+ .build()
+ )
);
}
}