aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-10-24 21:02:07 +0100
committerMichael Williamson <mike@zwobble.org>2023-10-24 21:02:07 +0100
commit0dccc0cdf7813c3c5571a79186a73bfd239a66d6 (patch)
treebc313d4336b83fe774ea1d8f14064bef2d09a97b /src
parent192cb27a8d8bc78211897c9f9325eb898390dbc4 (diff)
Parse function expressions
Diffstat (limited to 'src')
-rw-r--r--src/main/java/org/shedlang/compiler/ast/untyped/UntypedExpressionNode.java2
-rw-r--r--src/main/java/org/shedlang/compiler/ast/untyped/UntypedFunctionExpressionNode.java94
-rw-r--r--src/main/java/org/shedlang/compiler/parser/Parser.java32
-rw-r--r--src/test/java/org/shedlang/compiler/ast/untyped/UntypedFunctionExpressionNodeMatcher.java54
-rw-r--r--src/test/java/org/shedlang/compiler/ast/untyped/UntypedNodeMatchers.java4
-rw-r--r--src/test/java/org/shedlang/compiler/parser/ParserFunctionExpressionTests.java246
6 files changed, 431 insertions, 1 deletions
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<UntypedTypeLevelExpressionNode> 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<UntypedParamNode> positionalParams,
+ PVector<UntypedParamNode> namedParams,
+ Optional<UntypedTypeLevelExpressionNode> effect,
+ UntypedTypeLevelExpressionNode returnType,
+ PVector<UntypedFunctionStatementNode> 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<TokenType> 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<TokenType> 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<Matcher<? super UntypedFunctionExpressionNode>> submatchers
+ ) {
+ super(submatchers);
+ }
+
+ @Override
+ protected Class<UntypedFunctionExpressionNode> expectedType() {
+ return UntypedFunctionExpressionNode.class;
+ }
+
+ @Override
+ protected UntypedFunctionExpressionNodeMatcher create(
+ PVector<Matcher<? super UntypedFunctionExpressionNode>> submatchers
+ ) {
+ return new UntypedFunctionExpressionNodeMatcher(submatchers);
+ }
+
+ public UntypedFunctionExpressionNodeMatcher withBody(Matcher<UntypedBlockNode> body) {
+ return with(has("body", x -> x.body(), body));
+ }
+
+ public UntypedFunctionExpressionNodeMatcher withEffect(Matcher<Optional<? extends UntypedTypeLevelExpressionNode>> effect) {
+ return with(has("effect", x -> x.effect(), effect));
+ }
+
+ public UntypedFunctionExpressionNodeMatcher withParams(Matcher<UntypedParamsNode> params) {
+ return with(has("params", x -> x.params(), params));
+ }
+
+ public UntypedFunctionExpressionNodeMatcher withReturnType(Matcher<UntypedTypeLevelExpressionNode> 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<UntypedFunctionStatementNode> isUntypedFunctionReturnNode(
Matcher<UntypedExpressionNode> 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()));
+ }
+}