From 0dccc0cdf7813c3c5571a79186a73bfd239a66d6 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Tue, 24 Oct 2023 21:02:07 +0100 Subject: Parse function expressions --- .../ast/untyped/UntypedExpressionNode.java | 2 +- .../ast/untyped/UntypedFunctionExpressionNode.java | 94 ++++++++ .../java/org/shedlang/compiler/parser/Parser.java | 32 +++ .../UntypedFunctionExpressionNodeMatcher.java | 54 +++++ .../compiler/ast/untyped/UntypedNodeMatchers.java | 4 + .../parser/ParserFunctionExpressionTests.java | 246 +++++++++++++++++++++ 6 files changed, 431 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/shedlang/compiler/ast/untyped/UntypedFunctionExpressionNode.java create mode 100644 src/test/java/org/shedlang/compiler/ast/untyped/UntypedFunctionExpressionNodeMatcher.java create mode 100644 src/test/java/org/shedlang/compiler/parser/ParserFunctionExpressionTests.java (limited to 'src') diff --git a/src/main/java/org/shedlang/compiler/ast/untyped/UntypedExpressionNode.java b/src/main/java/org/shedlang/compiler/ast/untyped/UntypedExpressionNode.java index 107bc20b..e8f3842d 100644 --- a/src/main/java/org/shedlang/compiler/ast/untyped/UntypedExpressionNode.java +++ b/src/main/java/org/shedlang/compiler/ast/untyped/UntypedExpressionNode.java @@ -1,5 +1,5 @@ package org.shedlang.compiler.ast.untyped; public sealed interface UntypedExpressionNode extends UntypedNode - permits UntypedBinaryOperationNode, UntypedBoolLiteralNode, UntypedCallNode, UntypedFieldAccessNode, UntypedIfNode, UntypedIntLiteralNode, UntypedReferenceNode, UntypedStringLiteralNode, UntypedTupleNode, UntypedWhenNode { + permits UntypedBinaryOperationNode, UntypedBoolLiteralNode, UntypedCallNode, UntypedFieldAccessNode, UntypedFunctionExpressionNode, UntypedIfNode, UntypedIntLiteralNode, UntypedReferenceNode, UntypedStringLiteralNode, UntypedTupleNode, UntypedWhenNode { } diff --git a/src/main/java/org/shedlang/compiler/ast/untyped/UntypedFunctionExpressionNode.java b/src/main/java/org/shedlang/compiler/ast/untyped/UntypedFunctionExpressionNode.java new file mode 100644 index 00000000..bc931a5a --- /dev/null +++ b/src/main/java/org/shedlang/compiler/ast/untyped/UntypedFunctionExpressionNode.java @@ -0,0 +1,94 @@ +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 UntypedFunctionExpressionNode( + UntypedParamsNode params, + Optional effect, + UntypedTypeLevelExpressionNode returnType, + UntypedBlockNode body, + Source source +) implements UntypedExpressionNode { + 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 UntypedFunctionExpressionNode build() { + return new UntypedFunctionExpressionNode( + new UntypedParamsNode(positionalParams, namedParams, Sources.NULL), + effect, + returnType, + new UntypedBlockNode(body, Sources.NULL), + 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 13b62323..fefd6d62 100644 --- a/src/main/java/org/shedlang/compiler/parser/Parser.java +++ b/src/main/java/org/shedlang/compiler/parser/Parser.java @@ -314,6 +314,36 @@ public class Parser { ); } + private static UntypedExpressionNode parseFunctionExpression( + TokenIterator tokens + ) { + var start = tokens.position(); + + 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 body = parseBlock(tokens); + + var end = tokens.position(); + var source = start.to(end); + + return new UntypedFunctionExpressionNode( + params, + effect, + returnType, + body, + source + ); + } + private static UntypedFunctionStatementNode parseFunctionReturn(TokenIterator tokens) { var start = tokens.position(); @@ -670,6 +700,8 @@ public class Parser { case KEYWORD_FALSE, KEYWORD_TRUE -> parseBoolLiteral(tokens); + case KEYWORD_FN -> parseFunctionExpression(tokens); + case KEYWORD_WHEN -> parseWhen(tokens); case STRING -> parseStringLiteral(tokens); diff --git a/src/test/java/org/shedlang/compiler/ast/untyped/UntypedFunctionExpressionNodeMatcher.java b/src/test/java/org/shedlang/compiler/ast/untyped/UntypedFunctionExpressionNodeMatcher.java new file mode 100644 index 00000000..f5c8d161 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/ast/untyped/UntypedFunctionExpressionNodeMatcher.java @@ -0,0 +1,54 @@ +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 UntypedFunctionExpressionNodeMatcher extends MatcherBuilder< + UntypedExpressionNode, + UntypedFunctionExpressionNode, + UntypedFunctionExpressionNodeMatcher +> { + public static UntypedFunctionExpressionNodeMatcher empty() { + return new UntypedFunctionExpressionNodeMatcher(Empty.vector()); + } + + public UntypedFunctionExpressionNodeMatcher( + PVector> submatchers + ) { + super(submatchers); + } + + @Override + protected Class expectedType() { + return UntypedFunctionExpressionNode.class; + } + + @Override + protected UntypedFunctionExpressionNodeMatcher create( + PVector> submatchers + ) { + 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 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 f45adba9..0d4940e8 100644 --- a/src/test/java/org/shedlang/compiler/ast/untyped/UntypedNodeMatchers.java +++ b/src/test/java/org/shedlang/compiler/ast/untyped/UntypedNodeMatchers.java @@ -114,6 +114,10 @@ public class UntypedNodeMatchers { return UntypedFunctionDefinitionNodeMatcher.empty(); } + public static UntypedFunctionExpressionNodeMatcher isUntypedFunctionExpressionNode() { + return UntypedFunctionExpressionNodeMatcher.empty(); + } + public static Matcher isUntypedFunctionReturnNode( Matcher expression ) { diff --git a/src/test/java/org/shedlang/compiler/parser/ParserFunctionExpressionTests.java b/src/test/java/org/shedlang/compiler/parser/ParserFunctionExpressionTests.java new file mode 100644 index 00000000..d86776be --- /dev/null +++ b/src/test/java/org/shedlang/compiler/parser/ParserFunctionExpressionTests.java @@ -0,0 +1,246 @@ +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; +import static org.shedlang.compiler.ast.untyped.UntypedNodeMatchers.*; +import static org.shedlang.compiler.parser.ParserTesting.parseString; +import static org.zwobble.precisely.AssertThat.assertThat; +import static org.zwobble.precisely.Matchers.*; + +public class ParserFunctionExpressionTests { + @Test + public void canParseMinimalFunction() { + var source = """ + fn () -> Unit { + }"""; + + var node = parseString(source, Parser::parseTopLevelExpression); + + assertThat( + node, + isUntypedFunctionExpressionNode() + .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 = """ + fn () -> Unit { + a; + b; + c; + }"""; + + var node = parseString(source, Parser::parseTopLevelExpression); + + assertThat( + node, + isUntypedFunctionExpressionNode() + .withBody(isUntypedBlockNode(isSequence( + isUntypedExpressionStatementNode(isUntypedReferenceNode("a")), + isUntypedExpressionStatementNode(isUntypedReferenceNode("b")), + isUntypedExpressionStatementNode(isUntypedReferenceNode("c")) + ))) + ); + } + + @Test + public void canParseFunctionWithBodyWithBlockReturn() { + var source = """ + fn () -> Unit { + a; + b; + c + }"""; + + var node = parseString(source, Parser::parseTopLevelExpression); + + assertThat( + node, + isUntypedFunctionExpressionNode() + .withBody(isUntypedBlockNode(isSequence( + isUntypedExpressionStatementNode(isUntypedReferenceNode("a")), + isUntypedExpressionStatementNode(isUntypedReferenceNode("b")), + isUntypedBlockReturnNode(isUntypedReferenceNode("c")) + ))) + ); + } + + @Test + public void blockReturnEndsFunction() { + var source = """ + fn () -> Unit { + a; + b + c + }"""; + + var error = assertThrows( + UnexpectedTokenException.class, + () -> parseString(source, Parser::parseTopLevelExpression) + ); + + assertThat(error.getActual().value(), equalTo("c")); + assertThat(error.getExpected(), equalTo(TokenType.SYMBOL_BRACE_CLOSE.toString())); + } +} -- cgit v1.2.3