From 5a5e3cad6edd5af97522c99f7b0ffe145d200db0 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Wed, 25 Oct 2023 21:44:51 +0100 Subject: Introduce UntypedFunctionSignatureNode --- .../ast/untyped/UntypedFunctionDefinitionNode.java | 55 ++----- .../ast/untyped/UntypedFunctionExpressionNode.java | 45 +---- .../ast/untyped/UntypedFunctionSignatureNode.java | 92 +++++++++++ .../java/org/shedlang/compiler/parser/Parser.java | 51 +++--- .../shedlang/compiler/typechecker/TypeChecker.java | 18 +- .../UntypedFunctionDefinitionNodeMatcher.java | 20 +-- .../UntypedFunctionExpressionNodeMatcher.java | 20 +-- .../UntypedFunctionSignatureNodeMatcher.java | 50 ++++++ .../compiler/ast/untyped/UntypedNodeMatchers.java | 4 + .../parser/ParserFunctionDefinitionTests.java | 165 +------------------ .../parser/ParserFunctionExpressionTests.java | 163 +----------------- .../parser/ParserFunctionSignatureTests.java | 183 +++++++++++++++++++++ .../compiler/parser/ParserFunctionTypeTests.java | 12 +- 13 files changed, 411 insertions(+), 467 deletions(-) create mode 100644 src/main/java/org/shedlang/compiler/ast/untyped/UntypedFunctionSignatureNode.java create mode 100644 src/test/java/org/shedlang/compiler/ast/untyped/UntypedFunctionSignatureNodeMatcher.java create mode 100644 src/test/java/org/shedlang/compiler/parser/ParserFunctionSignatureTests.java (limited to 'src') diff --git a/src/main/java/org/shedlang/compiler/ast/untyped/UntypedFunctionDefinitionNode.java b/src/main/java/org/shedlang/compiler/ast/untyped/UntypedFunctionDefinitionNode.java index c81a4409..d9abdcdb 100644 --- a/src/main/java/org/shedlang/compiler/ast/untyped/UntypedFunctionDefinitionNode.java +++ b/src/main/java/org/shedlang/compiler/ast/untyped/UntypedFunctionDefinitionNode.java @@ -6,14 +6,10 @@ import org.shedlang.compiler.sources.Source; import org.shedlang.compiler.sources.Sources; import org.shedlang.compiler.types.Visibility; -import java.util.Optional; - public record UntypedFunctionDefinitionNode( String name, Visibility visibility, - UntypedParamsNode params, - Optional effect, - UntypedTypeLevelExpressionNode returnType, + UntypedFunctionSignatureNode signature, UntypedBlockNode body, Source source ) implements UntypedNamespaceStatementNode, UntypedVariableDeclarationNode { @@ -21,10 +17,7 @@ public record UntypedFunctionDefinitionNode( return new Builder( "f", Visibility.PRIVATE, - Empty.vector(), - Empty.vector(), - Optional.empty(), - new UntypedTypeLevelReferenceNode("Unit", Sources.NULL), + UntypedFunctionSignatureNode.builder(), Empty.vector() ); } @@ -32,19 +25,14 @@ public record UntypedFunctionDefinitionNode( public record Builder( String name, Visibility visibility, - PVector positionalParams, - PVector namedParams, - Optional effect, - UntypedTypeLevelExpressionNode returnType, + UntypedFunctionSignatureNode.Builder signature, PVector body ) { public UntypedFunctionDefinitionNode build() { return new UntypedFunctionDefinitionNode( name, visibility, - new UntypedParamsNode(positionalParams, namedParams, Sources.NULL), - effect, - returnType, + signature.build(), new UntypedBlockNode(body, Sources.NULL), Sources.NULL ); @@ -54,10 +42,7 @@ public record UntypedFunctionDefinitionNode( return new Builder( name, this.visibility, - this.positionalParams, - this.namedParams, - this.effect, - this.returnType, + this.signature, this.body ); } @@ -66,10 +51,7 @@ public record UntypedFunctionDefinitionNode( return new Builder( this.name, this.visibility, - this.positionalParams.plus(new UntypedParamNode(name, type, Sources.NULL)), - this.namedParams, - this.effect, - this.returnType, + this.signature.addPositionalParam(name, type), this.body ); } @@ -78,10 +60,7 @@ public record UntypedFunctionDefinitionNode( return new Builder( this.name, this.visibility, - this.positionalParams, - this.namedParams.plus(new UntypedParamNode(name, type, Sources.NULL)), - this.effect, - this.returnType, + this.signature.addNamedParam(name, type), this.body ); } @@ -90,10 +69,7 @@ public record UntypedFunctionDefinitionNode( return new Builder( this.name, this.visibility, - this.positionalParams, - this.namedParams, - Optional.of(effect), - this.returnType, + this.signature.withEffect(effect), this.body ); } @@ -102,10 +78,7 @@ public record UntypedFunctionDefinitionNode( return new Builder( this.name, this.visibility, - this.positionalParams, - this.namedParams, - this.effect, - returnType, + this.signature.withReturnType(returnType), this.body ); } @@ -114,10 +87,7 @@ public record UntypedFunctionDefinitionNode( return new Builder( this.name, this.visibility, - this.positionalParams, - this.namedParams, - this.effect, - this.returnType, + this.signature.addStatement(statement), this.body.plus(statement) ); } @@ -126,10 +96,7 @@ public record UntypedFunctionDefinitionNode( return new Builder( this.name, visibility, - this.positionalParams, - this.namedParams, - this.effect, - this.returnType, + this.signature, this.body ); } diff --git a/src/main/java/org/shedlang/compiler/ast/untyped/UntypedFunctionExpressionNode.java b/src/main/java/org/shedlang/compiler/ast/untyped/UntypedFunctionExpressionNode.java index bc931a5a..1ffddf69 100644 --- a/src/main/java/org/shedlang/compiler/ast/untyped/UntypedFunctionExpressionNode.java +++ b/src/main/java/org/shedlang/compiler/ast/untyped/UntypedFunctionExpressionNode.java @@ -5,37 +5,25 @@ import org.pcollections.PVector; import org.shedlang.compiler.sources.Source; import org.shedlang.compiler.sources.Sources; -import java.util.Optional; - public record UntypedFunctionExpressionNode( - UntypedParamsNode params, - Optional effect, - UntypedTypeLevelExpressionNode returnType, + UntypedFunctionSignatureNode signature, UntypedBlockNode body, Source source ) implements UntypedExpressionNode { public static Builder builder() { return new Builder( - Empty.vector(), - Empty.vector(), - Optional.empty(), - new UntypedTypeLevelReferenceNode("Unit", Sources.NULL), + UntypedFunctionSignatureNode.builder(), Empty.vector() ); } public record Builder( - PVector positionalParams, - PVector namedParams, - Optional effect, - UntypedTypeLevelExpressionNode returnType, + UntypedFunctionSignatureNode.Builder signature, PVector body ) { public UntypedFunctionExpressionNode build() { return new UntypedFunctionExpressionNode( - new UntypedParamsNode(positionalParams, namedParams, Sources.NULL), - effect, - returnType, + signature.build(), new UntypedBlockNode(body, Sources.NULL), Sources.NULL ); @@ -43,50 +31,35 @@ public record UntypedFunctionExpressionNode( public Builder addPositionalParam(String name, UntypedTypeLevelExpressionNode type) { return new Builder( - this.positionalParams.plus(new UntypedParamNode(name, type, Sources.NULL)), - this.namedParams, - this.effect, - this.returnType, + this.signature.addPositionalParam(name, type), this.body ); } public Builder addNamedParam(String name, UntypedTypeLevelExpressionNode type) { return new Builder( - this.positionalParams, - this.namedParams.plus(new UntypedParamNode(name, type, Sources.NULL)), - this.effect, - this.returnType, + this.signature.addNamedParam(name, type), this.body ); } public Builder withEffect(UntypedTypeLevelExpressionNode effect) { return new Builder( - this.positionalParams, - this.namedParams, - Optional.of(effect), - this.returnType, + this.signature.withEffect(effect), this.body ); } public Builder withReturnType(UntypedTypeLevelExpressionNode returnType) { return new Builder( - this.positionalParams, - this.namedParams, - this.effect, - returnType, + this.signature.withReturnType(returnType), this.body ); } public Builder addStatement(UntypedFunctionStatementNode statement) { return new Builder( - this.positionalParams, - this.namedParams, - this.effect, - this.returnType, + this.signature, this.body.plus(statement) ); } diff --git a/src/main/java/org/shedlang/compiler/ast/untyped/UntypedFunctionSignatureNode.java b/src/main/java/org/shedlang/compiler/ast/untyped/UntypedFunctionSignatureNode.java new file mode 100644 index 00000000..179b4f1e --- /dev/null +++ b/src/main/java/org/shedlang/compiler/ast/untyped/UntypedFunctionSignatureNode.java @@ -0,0 +1,92 @@ +package org.shedlang.compiler.ast.untyped; + +import org.pcollections.Empty; +import org.pcollections.PVector; +import org.shedlang.compiler.sources.Source; +import org.shedlang.compiler.sources.Sources; + +import java.util.Optional; + +public record UntypedFunctionSignatureNode( + UntypedParamsNode params, + Optional effect, + UntypedTypeLevelExpressionNode returnType, + Source source +) implements UntypedNode { + public static Builder builder() { + return new Builder( + Empty.vector(), + Empty.vector(), + Optional.empty(), + new UntypedTypeLevelReferenceNode("Unit", Sources.NULL), + Empty.vector() + ); + } + + public record Builder( + PVector positionalParams, + PVector namedParams, + Optional effect, + UntypedTypeLevelExpressionNode returnType, + PVector body + ) { + public UntypedFunctionSignatureNode build() { + return new UntypedFunctionSignatureNode( + new UntypedParamsNode(positionalParams, namedParams, Sources.NULL), + effect, + returnType, + Sources.NULL + ); + } + + public Builder addPositionalParam(String name, UntypedTypeLevelExpressionNode type) { + return new Builder( + this.positionalParams.plus(new UntypedParamNode(name, type, Sources.NULL)), + this.namedParams, + this.effect, + this.returnType, + this.body + ); + } + + public Builder addNamedParam(String name, UntypedTypeLevelExpressionNode type) { + return new Builder( + this.positionalParams, + this.namedParams.plus(new UntypedParamNode(name, type, Sources.NULL)), + this.effect, + this.returnType, + this.body + ); + } + + public Builder withEffect(UntypedTypeLevelExpressionNode effect) { + return new Builder( + this.positionalParams, + this.namedParams, + Optional.of(effect), + this.returnType, + this.body + ); + } + + public Builder withReturnType(UntypedTypeLevelExpressionNode returnType) { + return new Builder( + this.positionalParams, + this.namedParams, + this.effect, + returnType, + this.body + ); + } + + public Builder addStatement(UntypedFunctionStatementNode statement) { + return new Builder( + this.positionalParams, + this.namedParams, + this.effect, + this.returnType, + this.body.plus(statement) + ); + } + } +} diff --git a/src/main/java/org/shedlang/compiler/parser/Parser.java b/src/main/java/org/shedlang/compiler/parser/Parser.java index fefd6d62..ac7f5bb1 100644 --- a/src/main/java/org/shedlang/compiler/parser/Parser.java +++ b/src/main/java/org/shedlang/compiler/parser/Parser.java @@ -289,14 +289,7 @@ public class Parser { tokens.skip(TokenType.KEYWORD_FUN); var name = tokens.nextValue(TokenType.IDENTIFIER); - tokens.skip(TokenType.SYMBOL_PAREN_OPEN); - var params = parseParams(tokens); - tokens.skip(TokenType.SYMBOL_PAREN_CLOSE); - - var effect = parseFunctionTypeEffect(tokens); - - tokens.skip(TokenType.SYMBOL_THIN_ARROW); - var returnType = parseTopLevelTypeLevelExpression(tokens); + var signature = parseFunctionSignature(tokens); var body = parseBlock(tokens); @@ -306,9 +299,7 @@ public class Parser { return new UntypedFunctionDefinitionNode( name, visibility, - params, - effect, - returnType, + signature, body, source ); @@ -321,14 +312,7 @@ public class Parser { tokens.skip(TokenType.KEYWORD_FN); - tokens.skip(TokenType.SYMBOL_PAREN_OPEN); - var params = parseParams(tokens); - tokens.skip(TokenType.SYMBOL_PAREN_CLOSE); - - var effect = parseFunctionTypeEffect(tokens); - - tokens.skip(TokenType.SYMBOL_THIN_ARROW); - var returnType = parseTopLevelTypeLevelExpression(tokens); + var signature = parseFunctionSignature(tokens); var body = parseBlock(tokens); @@ -336,9 +320,7 @@ public class Parser { var source = start.to(end); return new UntypedFunctionExpressionNode( - params, - effect, - returnType, + signature, body, source ); @@ -359,6 +341,31 @@ public class Parser { return new UntypedFunctionReturnNode(expression, source); } + static UntypedFunctionSignatureNode parseFunctionSignature( + TokenIterator tokens + ) { + var start = tokens.position(); + + tokens.skip(TokenType.SYMBOL_PAREN_OPEN); + var params = parseParams(tokens); + tokens.skip(TokenType.SYMBOL_PAREN_CLOSE); + + var effect = parseFunctionTypeEffect(tokens); + + tokens.skip(TokenType.SYMBOL_THIN_ARROW); + var returnType = parseTopLevelTypeLevelExpression(tokens); + + var end = tokens.position(); + var source = start.to(end); + + return new UntypedFunctionSignatureNode( + params, + effect, + returnType, + source + ); + } + static UntypedFunctionStatementNode parseFunctionStatement(TokenIterator tokens) { return switch (tokens.peek().tokenType()) { case KEYWORD_ASSIGN, KEYWORD_VAL, KEYWORD_VAR, SYMBOL_UNDERSCORE -> diff --git a/src/main/java/org/shedlang/compiler/typechecker/TypeChecker.java b/src/main/java/org/shedlang/compiler/typechecker/TypeChecker.java index 0c3fa89d..9cd0d474 100644 --- a/src/main/java/org/shedlang/compiler/typechecker/TypeChecker.java +++ b/src/main/java/org/shedlang/compiler/typechecker/TypeChecker.java @@ -484,11 +484,7 @@ public class TypeChecker { UntypedFunctionDefinitionNode untyped, TypeCheckerContext context ) { - var typedSignature = typeCheckFunctionSignature(new UntypedFunctionSignature( - untyped.params(), - untyped.effect(), - untyped.returnType() - ), context); + var typedSignature = typeCheckFunctionSignature(untyped.signature(), context); return context .define(untyped.name(), typedSignature.type(), untyped.source()) @@ -594,14 +590,6 @@ public class TypeChecker { return context.toResult(typed); } - private record UntypedFunctionSignature( - UntypedParamsNode params, - Optional effect, - UntypedTypeLevelExpressionNode returnType - ) { - - } - private record TypedFunctionSignature( TypedParamsNode params, Optional> effect, @@ -622,8 +610,8 @@ public class TypeChecker { } } - private static TypedFunctionSignature typeCheckFunctionSignature( - UntypedFunctionSignature untyped, + static TypedFunctionSignature typeCheckFunctionSignature( + UntypedFunctionSignatureNode untyped, TypeCheckerContext context ) { var typedParams = typeCheckParams(untyped.params(), context); diff --git a/src/test/java/org/shedlang/compiler/ast/untyped/UntypedFunctionDefinitionNodeMatcher.java b/src/test/java/org/shedlang/compiler/ast/untyped/UntypedFunctionDefinitionNodeMatcher.java index f50056f0..d3d1e962 100644 --- a/src/test/java/org/shedlang/compiler/ast/untyped/UntypedFunctionDefinitionNodeMatcher.java +++ b/src/test/java/org/shedlang/compiler/ast/untyped/UntypedFunctionDefinitionNodeMatcher.java @@ -6,8 +6,6 @@ import org.shedlang.compiler.testing.MatcherBuilder; import org.shedlang.compiler.types.Visibility; import org.zwobble.precisely.Matcher; -import java.util.Optional; - import static org.zwobble.precisely.Matchers.equalTo; import static org.zwobble.precisely.Matchers.has; @@ -30,26 +28,20 @@ public class UntypedFunctionDefinitionNodeMatcher extends MatcherBuilder body) { - return with(has("body", x -> x.body(), body)); + public UntypedFunctionDefinitionNodeMatcher withSignature( + Matcher signature + ) { + return with(has("signature", x -> x.signature(), signature)); } - public UntypedFunctionDefinitionNodeMatcher withEffect(Matcher> effect) { - return with(has("effect", x -> x.effect(), effect)); + public UntypedFunctionDefinitionNodeMatcher withBody(Matcher body) { + return with(has("body", x -> x.body(), body)); } public UntypedFunctionDefinitionNodeMatcher withName(String name) { return with(has("name", x -> x.name(), equalTo(name))); } - public UntypedFunctionDefinitionNodeMatcher withParams(Matcher params) { - return with(has("params", x -> x.params(), params)); - } - - public UntypedFunctionDefinitionNodeMatcher withReturnType(Matcher returnType) { - return with(has("returnType", x -> x.returnType(), returnType)); - } - public UntypedFunctionDefinitionNodeMatcher withVisibility(Visibility visibility) { return with(has("visibility", x -> x.visibility(), equalTo(visibility))); } diff --git a/src/test/java/org/shedlang/compiler/ast/untyped/UntypedFunctionExpressionNodeMatcher.java b/src/test/java/org/shedlang/compiler/ast/untyped/UntypedFunctionExpressionNodeMatcher.java index f5c8d161..434d79e6 100644 --- a/src/test/java/org/shedlang/compiler/ast/untyped/UntypedFunctionExpressionNodeMatcher.java +++ b/src/test/java/org/shedlang/compiler/ast/untyped/UntypedFunctionExpressionNodeMatcher.java @@ -5,8 +5,6 @@ import org.pcollections.PVector; import org.shedlang.compiler.testing.MatcherBuilder; import org.zwobble.precisely.Matcher; -import java.util.Optional; - import static org.zwobble.precisely.Matchers.has; public class UntypedFunctionExpressionNodeMatcher extends MatcherBuilder< @@ -36,19 +34,13 @@ public class UntypedFunctionExpressionNodeMatcher extends MatcherBuilder< return new UntypedFunctionExpressionNodeMatcher(submatchers); } - public UntypedFunctionExpressionNodeMatcher withBody(Matcher body) { - return with(has("body", x -> x.body(), body)); - } - - public UntypedFunctionExpressionNodeMatcher withEffect(Matcher> effect) { - return with(has("effect", x -> x.effect(), effect)); - } - - public UntypedFunctionExpressionNodeMatcher withParams(Matcher params) { - return with(has("params", x -> x.params(), params)); + public UntypedFunctionExpressionNodeMatcher withSignature( + Matcher signature + ) { + return with(has("signature", x -> x.signature(), signature)); } - public UntypedFunctionExpressionNodeMatcher withReturnType(Matcher returnType) { - return with(has("returnType", x -> x.returnType(), returnType)); + public UntypedFunctionExpressionNodeMatcher withBody(Matcher body) { + return with(has("body", x -> x.body(), body)); } } diff --git a/src/test/java/org/shedlang/compiler/ast/untyped/UntypedFunctionSignatureNodeMatcher.java b/src/test/java/org/shedlang/compiler/ast/untyped/UntypedFunctionSignatureNodeMatcher.java new file mode 100644 index 00000000..d799245b --- /dev/null +++ b/src/test/java/org/shedlang/compiler/ast/untyped/UntypedFunctionSignatureNodeMatcher.java @@ -0,0 +1,50 @@ +package org.shedlang.compiler.ast.untyped; + +import org.pcollections.Empty; +import org.pcollections.PVector; +import org.shedlang.compiler.testing.MatcherBuilder; +import org.zwobble.precisely.Matcher; + +import java.util.Optional; + +import static org.zwobble.precisely.Matchers.has; + +public class UntypedFunctionSignatureNodeMatcher extends MatcherBuilder< + UntypedFunctionSignatureNode, + UntypedFunctionSignatureNode, + UntypedFunctionSignatureNodeMatcher +> { + public static UntypedFunctionSignatureNodeMatcher empty() { + return new UntypedFunctionSignatureNodeMatcher(Empty.vector()); + } + + public UntypedFunctionSignatureNodeMatcher( + PVector> submatchers + ) { + super(submatchers); + } + + @Override + protected Class expectedType() { + return UntypedFunctionSignatureNode.class; + } + + @Override + protected UntypedFunctionSignatureNodeMatcher create( + PVector> submatchers + ) { + return new UntypedFunctionSignatureNodeMatcher(submatchers); + } + + public UntypedFunctionSignatureNodeMatcher withEffect(Matcher> effect) { + return with(has("effect", x -> x.effect(), effect)); + } + + public UntypedFunctionSignatureNodeMatcher withParams(Matcher params) { + return with(has("params", x -> x.params(), params)); + } + + public UntypedFunctionSignatureNodeMatcher withReturnType(Matcher returnType) { + return with(has("returnType", x -> x.returnType(), returnType)); + } +} diff --git a/src/test/java/org/shedlang/compiler/ast/untyped/UntypedNodeMatchers.java b/src/test/java/org/shedlang/compiler/ast/untyped/UntypedNodeMatchers.java index 0d4940e8..1a7f0611 100644 --- a/src/test/java/org/shedlang/compiler/ast/untyped/UntypedNodeMatchers.java +++ b/src/test/java/org/shedlang/compiler/ast/untyped/UntypedNodeMatchers.java @@ -127,6 +127,10 @@ public class UntypedNodeMatchers { ); } + public static UntypedFunctionSignatureNodeMatcher isUntypedFunctionSignatureNode() { + return UntypedFunctionSignatureNodeMatcher.empty(); + } + public static UntypedFunctionTypeNodeMatcher isUntypedFunctionTypeNode() { return UntypedFunctionTypeNodeMatcher.empty(); } diff --git a/src/test/java/org/shedlang/compiler/parser/ParserFunctionDefinitionTests.java b/src/test/java/org/shedlang/compiler/parser/ParserFunctionDefinitionTests.java index 431ef6d0..9c3148a3 100644 --- a/src/test/java/org/shedlang/compiler/parser/ParserFunctionDefinitionTests.java +++ b/src/test/java/org/shedlang/compiler/parser/ParserFunctionDefinitionTests.java @@ -1,7 +1,6 @@ package org.shedlang.compiler.parser; import org.junit.jupiter.api.Test; -import org.shedlang.compiler.parser.errors.PositionalParamAfterNamedParamError; import org.shedlang.compiler.tokeniser.UnexpectedTokenException; import org.shedlang.compiler.types.Visibility; @@ -24,9 +23,12 @@ public class ParserFunctionDefinitionTests { node, isUntypedFunctionDefinitionNode() .withName("f") - .withParams(isUntypedParamsNode(isSequence(), isSequence())) - .withEffect(isOptionalEmpty()) - .withReturnType(isUntypedTypeLevelReferenceNode("Unit")) + .withSignature( + isUntypedFunctionSignatureNode() + .withParams(isUntypedParamsNode(isSequence(), isSequence())) + .withEffect(isOptionalEmpty()) + .withReturnType(isUntypedTypeLevelReferenceNode("Unit")) + ) .withBody(isUntypedBlockNode(isSequence())) ); } @@ -46,7 +48,7 @@ public class ParserFunctionDefinitionTests { } @Test - public void whenFunIsPrefixWithPubThenVisibilityIsPublic() { + public void whenFunIsPrefixedWithPubThenVisibilityIsPublic() { var source = """ pub fun f() -> Unit { }"""; @@ -59,159 +61,6 @@ public class ParserFunctionDefinitionTests { ); } - @Test - public void canParseFunctionWithSinglePositionalParam() { - var source = """ - fun f(a: Int) -> Unit { - }"""; - - var node = parseString(source, Parser::parseNamespaceStatement); - - assertThat( - node, - isUntypedFunctionDefinitionNode() - .withParams(isUntypedParamsNode( - isSequence( - isUntypedParamNode("a", isUntypedTypeLevelReferenceNode("Int")) - ), - isSequence() - )) - ); - } - - @Test - public void canParseFunctionWithManyPositionalParams() { - var source = """ - fun f(a: Int, b: String, c: Bool) -> Unit { - }"""; - - var node = parseString(source, Parser::parseNamespaceStatement); - - assertThat( - node, - isUntypedFunctionDefinitionNode() - .withParams(isUntypedParamsNode( - isSequence( - isUntypedParamNode("a", isUntypedTypeLevelReferenceNode("Int")), - isUntypedParamNode("b", isUntypedTypeLevelReferenceNode("String")), - isUntypedParamNode("c", isUntypedTypeLevelReferenceNode("Bool")) - ), - isSequence() - )) - ); - } - - @Test - public void canParseFunctionWithSingleNamedParam() { - var source = """ - fun f(.a: Int) -> Unit { - }"""; - - var node = parseString(source, Parser::parseNamespaceStatement); - - assertThat( - node, - isUntypedFunctionDefinitionNode() - .withParams(isUntypedParamsNode( - isSequence(), - isSequence( - isUntypedParamNode("a", isUntypedTypeLevelReferenceNode("Int")) - ) - )) - ); - } - - @Test - public void canParseFunctionWithManyNamedParams() { - var source = """ - fun f(.a: Int, .b: String, .c: Bool) -> Unit { - }"""; - - var node = parseString(source, Parser::parseNamespaceStatement); - - assertThat( - node, - isUntypedFunctionDefinitionNode() - .withParams(isUntypedParamsNode( - isSequence(), - isSequence( - isUntypedParamNode("a", isUntypedTypeLevelReferenceNode("Int")), - isUntypedParamNode("b", isUntypedTypeLevelReferenceNode("String")), - isUntypedParamNode("c", isUntypedTypeLevelReferenceNode("Bool")) - ) - )) - ); - } - - @Test - public void canParseNamedParamAfterPositionalParam() { - var source = """ - fun f(a: Int, .b: String) -> Unit { - }"""; - - var node = parseString(source, Parser::parseNamespaceStatement); - - assertThat( - node, - isUntypedFunctionDefinitionNode() - .withParams(isUntypedParamsNode( - isSequence( - isUntypedParamNode("a", isUntypedTypeLevelReferenceNode("Int")) - ), - isSequence( - isUntypedParamNode("b", isUntypedTypeLevelReferenceNode("String")) - ) - )) - ); - } - - @Test - public void cannotParsePositionalParamAfterNamedParam() { - var source = """ - fun f(.a: Int, b: String) -> Unit { - }"""; - - assertThrows( - PositionalParamAfterNamedParamError.class, - () -> parseString(source, Parser::parseNamespaceStatement) - ); - } - - @Test - public void paramsCanHaveTrailingComma() { - var source = """ - fun f(a: Int, ) -> Unit { - }"""; - - var node = parseString(source, Parser::parseNamespaceStatement); - - assertThat( - node, - isUntypedFunctionDefinitionNode() - .withParams(isUntypedParamsNode( - isSequence( - isUntypedParamNode("a", isUntypedTypeLevelReferenceNode("Int")) - ), - isSequence() - )) - ); - } - - @Test - public void canParseFunctionWithEffect() { - var source = """ - fun f() ! Io -> Unit { - }"""; - - var node = parseString(source, Parser::parseNamespaceStatement); - - assertThat( - node, - isUntypedFunctionDefinitionNode() - .withEffect(isOptionalOf(isUntypedTypeLevelReferenceNode("Io"))) - ); - } - @Test public void canParseFunctionWithBodyWithoutReturn() { var source = """ diff --git a/src/test/java/org/shedlang/compiler/parser/ParserFunctionExpressionTests.java b/src/test/java/org/shedlang/compiler/parser/ParserFunctionExpressionTests.java index d86776be..8b4c72a6 100644 --- a/src/test/java/org/shedlang/compiler/parser/ParserFunctionExpressionTests.java +++ b/src/test/java/org/shedlang/compiler/parser/ParserFunctionExpressionTests.java @@ -1,7 +1,6 @@ package org.shedlang.compiler.parser; import org.junit.jupiter.api.Test; -import org.shedlang.compiler.parser.errors.PositionalParamAfterNamedParamError; import org.shedlang.compiler.tokeniser.UnexpectedTokenException; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -22,166 +21,16 @@ public class ParserFunctionExpressionTests { assertThat( node, isUntypedFunctionExpressionNode() - .withParams(isUntypedParamsNode(isSequence(), isSequence())) - .withEffect(isOptionalEmpty()) - .withReturnType(isUntypedTypeLevelReferenceNode("Unit")) + .withSignature( + isUntypedFunctionSignatureNode() + .withParams(isUntypedParamsNode(isSequence(), isSequence())) + .withEffect(isOptionalEmpty()) + .withReturnType(isUntypedTypeLevelReferenceNode("Unit")) + ) .withBody(isUntypedBlockNode(isSequence())) ); } - @Test - public void canParseFunctionWithSinglePositionalParam() { - var source = """ - fn (a: Int) -> Unit { - }"""; - - var node = parseString(source, Parser::parseTopLevelExpression); - - assertThat( - node, - isUntypedFunctionExpressionNode() - .withParams(isUntypedParamsNode( - isSequence( - isUntypedParamNode("a", isUntypedTypeLevelReferenceNode("Int")) - ), - isSequence() - )) - ); - } - - @Test - public void canParseFunctionWithManyPositionalParams() { - var source = """ - fn (a: Int, b: String, c: Bool) -> Unit { - }"""; - - var node = parseString(source, Parser::parseTopLevelExpression); - - assertThat( - node, - isUntypedFunctionExpressionNode() - .withParams(isUntypedParamsNode( - isSequence( - isUntypedParamNode("a", isUntypedTypeLevelReferenceNode("Int")), - isUntypedParamNode("b", isUntypedTypeLevelReferenceNode("String")), - isUntypedParamNode("c", isUntypedTypeLevelReferenceNode("Bool")) - ), - isSequence() - )) - ); - } - - @Test - public void canParseFunctionWithSingleNamedParam() { - var source = """ - fn (.a: Int) -> Unit { - }"""; - - var node = parseString(source, Parser::parseTopLevelExpression); - - assertThat( - node, - isUntypedFunctionExpressionNode() - .withParams(isUntypedParamsNode( - isSequence(), - isSequence( - isUntypedParamNode("a", isUntypedTypeLevelReferenceNode("Int")) - ) - )) - ); - } - - @Test - public void canParseFunctionWithManyNamedParams() { - var source = """ - fn (.a: Int, .b: String, .c: Bool) -> Unit { - }"""; - - var node = parseString(source, Parser::parseTopLevelExpression); - - assertThat( - node, - isUntypedFunctionExpressionNode() - .withParams(isUntypedParamsNode( - isSequence(), - isSequence( - isUntypedParamNode("a", isUntypedTypeLevelReferenceNode("Int")), - isUntypedParamNode("b", isUntypedTypeLevelReferenceNode("String")), - isUntypedParamNode("c", isUntypedTypeLevelReferenceNode("Bool")) - ) - )) - ); - } - - @Test - public void canParseNamedParamAfterPositionalParam() { - var source = """ - fn (a: Int, .b: String) -> Unit { - }"""; - - var node = parseString(source, Parser::parseTopLevelExpression); - - assertThat( - node, - isUntypedFunctionExpressionNode() - .withParams(isUntypedParamsNode( - isSequence( - isUntypedParamNode("a", isUntypedTypeLevelReferenceNode("Int")) - ), - isSequence( - isUntypedParamNode("b", isUntypedTypeLevelReferenceNode("String")) - ) - )) - ); - } - - @Test - public void cannotParsePositionalParamAfterNamedParam() { - var source = """ - fn (.a: Int, b: String) -> Unit { - }"""; - - assertThrows( - PositionalParamAfterNamedParamError.class, - () -> parseString(source, Parser::parseTopLevelExpression) - ); - } - - @Test - public void paramsCanHaveTrailingComma() { - var source = """ - fn (a: Int, ) -> Unit { - }"""; - - var node = parseString(source, Parser::parseTopLevelExpression); - - assertThat( - node, - isUntypedFunctionExpressionNode() - .withParams(isUntypedParamsNode( - isSequence( - isUntypedParamNode("a", isUntypedTypeLevelReferenceNode("Int")) - ), - isSequence() - )) - ); - } - - @Test - public void canParseFunctionWithEffect() { - var source = """ - fn () ! Io -> Unit { - }"""; - - var node = parseString(source, Parser::parseTopLevelExpression); - - assertThat( - node, - isUntypedFunctionExpressionNode() - .withEffect(isOptionalOf(isUntypedTypeLevelReferenceNode("Io"))) - ); - } - @Test public void canParseFunctionWithBodyWithoutReturn() { var source = """ diff --git a/src/test/java/org/shedlang/compiler/parser/ParserFunctionSignatureTests.java b/src/test/java/org/shedlang/compiler/parser/ParserFunctionSignatureTests.java new file mode 100644 index 00000000..cfb610af --- /dev/null +++ b/src/test/java/org/shedlang/compiler/parser/ParserFunctionSignatureTests.java @@ -0,0 +1,183 @@ +package org.shedlang.compiler.parser; + +import org.junit.jupiter.api.Test; +import org.shedlang.compiler.parser.errors.PositionalParamAfterNamedParamError; + +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.shedlang.compiler.ast.untyped.UntypedNodeMatchers.*; +import static org.shedlang.compiler.ast.untyped.UntypedNodeMatchers.isUntypedTypeLevelReferenceNode; +import static org.shedlang.compiler.parser.ParserTesting.parseString; +import static org.zwobble.precisely.AssertThat.assertThat; +import static org.zwobble.precisely.Matchers.*; + +public class ParserFunctionSignatureTests { + @Test + public void canParseEmptyParams() { + var source = "() -> Unit"; + + var node = parseString(source, Parser::parseFunctionSignature); + + assertThat( + node, + isUntypedFunctionSignatureNode() + .withParams(isUntypedParamsNode(isSequence(), isSequence())) + ); + } + + @Test + public void canParseSinglePositionalParam() { + var source = "(a: Int) -> Unit"; + + var node = parseString(source, Parser::parseFunctionSignature); + + assertThat( + node, + isUntypedFunctionSignatureNode().withParams(isUntypedParamsNode( + isSequence( + isUntypedParamNode("a", isUntypedTypeLevelReferenceNode("Int")) + ), + isSequence() + )) + ); + } + + @Test + public void canParseManyPositionalParams() { + var source = "(a: Int, b: String, c: Bool) -> Unit"; + + var node = parseString(source, Parser::parseFunctionSignature); + + assertThat( + node, + isUntypedFunctionSignatureNode().withParams(isUntypedParamsNode( + isSequence( + isUntypedParamNode("a", isUntypedTypeLevelReferenceNode("Int")), + isUntypedParamNode("b", isUntypedTypeLevelReferenceNode("String")), + isUntypedParamNode("c", isUntypedTypeLevelReferenceNode("Bool")) + ), + isSequence() + )) + ); + } + + @Test + public void canParseSingleNamedParam() { + var source = "(.a: Int) -> Unit"; + + var node = parseString(source, Parser::parseFunctionSignature); + + assertThat( + node, + isUntypedFunctionSignatureNode().withParams(isUntypedParamsNode( + isSequence(), + isSequence( + isUntypedParamNode("a", isUntypedTypeLevelReferenceNode("Int")) + ) + )) + ); + } + + @Test + public void canParseManyNamedParams() { + var source = "(.a: Int, .b: String, .c: Bool) -> Unit"; + + var node = parseString(source, Parser::parseFunctionSignature); + + assertThat( + node, + isUntypedFunctionSignatureNode().withParams(isUntypedParamsNode( + isSequence(), + isSequence( + isUntypedParamNode("a", isUntypedTypeLevelReferenceNode("Int")), + isUntypedParamNode("b", isUntypedTypeLevelReferenceNode("String")), + isUntypedParamNode("c", isUntypedTypeLevelReferenceNode("Bool")) + ) + )) + ); + } + + @Test + public void canParseNamedParamAfterPositionalParam() { + var source = "(a: Int, .b: String) -> Unit"; + + var node = parseString(source, Parser::parseFunctionSignature); + + assertThat( + node, + isUntypedFunctionSignatureNode().withParams(isUntypedParamsNode( + isSequence( + isUntypedParamNode("a", isUntypedTypeLevelReferenceNode("Int")) + ), + isSequence( + isUntypedParamNode("b", isUntypedTypeLevelReferenceNode("String")) + ) + )) + ); + } + + @Test + public void cannotParsePositionalParamAfterNamedParam() { + var source = "(.a: Int, b: String) -> Unit"; + + assertThrows( + PositionalParamAfterNamedParamError.class, + () -> parseString(source, Parser::parseFunctionSignature) + ); + } + + @Test + public void paramsCanHaveTrailingComma() { + var source = "(a: Int, ) -> Unit"; + + var node = parseString(source, Parser::parseFunctionSignature); + + assertThat( + node, + isUntypedFunctionSignatureNode().withParams(isUntypedParamsNode( + isSequence( + isUntypedParamNode("a", isUntypedTypeLevelReferenceNode("Int")) + ), + isSequence() + )) + ); + } + + @Test + public void canParseSignatureWithoutEffect() { + var source = "() -> Unit"; + + var node = parseString(source, Parser::parseFunctionSignature); + + assertThat( + node, + isUntypedFunctionSignatureNode() + .withEffect(isOptionalEmpty()) + ); + } + + @Test + public void canParseSignatureWithEffect() { + var source = "() ! Io -> Unit"; + + var node = parseString(source, Parser::parseFunctionSignature); + + assertThat( + node, + isUntypedFunctionSignatureNode() + .withEffect(isOptionalOf(isUntypedTypeLevelReferenceNode("Io"))) + ); + } + + @Test + public void canParseReturnType() { + var source = "() -> Unit"; + + var node = parseString(source, Parser::parseFunctionSignature); + + assertThat( + node, + isUntypedFunctionSignatureNode() + .withReturnType(isUntypedTypeLevelReferenceNode("Unit")) + ); + } +} diff --git a/src/test/java/org/shedlang/compiler/parser/ParserFunctionTypeTests.java b/src/test/java/org/shedlang/compiler/parser/ParserFunctionTypeTests.java index 038baef4..76311a97 100644 --- a/src/test/java/org/shedlang/compiler/parser/ParserFunctionTypeTests.java +++ b/src/test/java/org/shedlang/compiler/parser/ParserFunctionTypeTests.java @@ -136,18 +136,16 @@ public class ParserFunctionTypeTests { @Test public void paramsCanHaveTrailingComma() { - var source = """ - fun f(a: Int, ) -> Unit { - }"""; + var source = "fn (Int, ) -> Unit"; - var node = parseString(source, Parser::parseNamespaceStatement); + var node = parseString(source, Parser::parseTopLevelTypeLevelExpression); assertThat( node, - isUntypedFunctionDefinitionNode() - .withParams(isUntypedParamsNode( + isUntypedFunctionTypeNode() + .withParams(isUntypedParamTypesNode( isSequence( - isUntypedParamNode("a", isUntypedTypeLevelReferenceNode("Int")) + isUntypedTypeLevelReferenceNode("Int") ), isSequence() )) -- cgit v1.2.3