From daf09ab1ba05768625fa7beb370f73e5021d53a1 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 29 Nov 2026 11:03:05 +0000 Subject: Type check body of function expression --- hobgoblin/src/ast/typed.hob | 16 ++++++ .../compiler/ast/typed/TypedExpressionNode.java | 2 +- .../ast/typed/TypedFunctionExpressionNode.java | 66 ++++++++++++++++++++++ .../compiler/ast/typed/TypedParamNode.java | 44 +++++++++++++++ .../compiler/typechecker/TypeChecker.java | 20 ++++++- .../typed/TypedFunctionExpressionNodeMatcher.java | 59 +++++++++++++++++++ .../compiler/ast/typed/TypedParamNodeMatcher.java | 53 +++++++++++++++++ .../TypeCheckerFunctionExpressionTests.java | 39 +++++++++++++ 8 files changed, 296 insertions(+), 3 deletions(-) create mode 100644 src/main/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedFunctionExpressionNode.java create mode 100644 src/main/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedParamNode.java create mode 100644 src/test/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedFunctionExpressionNodeMatcher.java create mode 100644 src/test/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedParamNodeMatcher.java create mode 100644 src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerFunctionExpressionTests.java diff --git a/hobgoblin/src/ast/typed.hob b/hobgoblin/src/ast/typed.hob index dba76ea..6ec62ec 100644 --- a/hobgoblin/src/ast/typed.hob +++ b/hobgoblin/src/ast/typed.hob @@ -105,10 +105,19 @@ struct TypedTableDefinitionNode { field source: Source; } +// == Params == + +struct TypedParamNode { + field name: String; + field type: Type; + field source: Source; +} + // == Expressions == sum TypedExpressionNode { variant TypedBinaryOperationNode; + variant TypedFunctionExpressionNode; variant TypedMemberAccessNode; variant TypedNumberLiteralNode; variant TypedReferenceNode; @@ -128,6 +137,13 @@ enum TypedBinaryOperator { variant equals; } +struct TypedFunctionExpressionNode { + field params: List[TypedParamNode]; + field body: TypedExpressionNode; + field type: Type; + field source: Source; +} + struct TypedMemberAccessNode { field receiver: TypedExpressionNode; field memberName: String; diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedExpressionNode.java b/src/main/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedExpressionNode.java index 887b2b4..1b5eee1 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedExpressionNode.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedExpressionNode.java @@ -5,7 +5,7 @@ package org.zwobble.hobgoblin.compiler.ast.typed; // Custom area start: org.zwobble.hobgoblin.compiler.ast.typed.TypedExpressionNode imports // Custom area end: org.zwobble.hobgoblin.compiler.ast.typed.TypedExpressionNode imports -public sealed interface TypedExpressionNode permits org.zwobble.hobgoblin.compiler.ast.typed.TypedBinaryOperationNode, org.zwobble.hobgoblin.compiler.ast.typed.TypedMemberAccessNode, org.zwobble.hobgoblin.compiler.ast.typed.TypedNumberLiteralNode, org.zwobble.hobgoblin.compiler.ast.typed.TypedReferenceNode { +public sealed interface TypedExpressionNode permits org.zwobble.hobgoblin.compiler.ast.typed.TypedBinaryOperationNode, org.zwobble.hobgoblin.compiler.ast.typed.TypedFunctionExpressionNode, org.zwobble.hobgoblin.compiler.ast.typed.TypedMemberAccessNode, org.zwobble.hobgoblin.compiler.ast.typed.TypedNumberLiteralNode, org.zwobble.hobgoblin.compiler.ast.typed.TypedReferenceNode { public interface Builder { public org.zwobble.hobgoblin.compiler.ast.typed.TypedExpressionNode build(); diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedFunctionExpressionNode.java b/src/main/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedFunctionExpressionNode.java new file mode 100644 index 0000000..0c6652d --- /dev/null +++ b/src/main/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedFunctionExpressionNode.java @@ -0,0 +1,66 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.ast.typed; + +// Custom area start: org.zwobble.hobgoblin.compiler.ast.typed.TypedFunctionExpressionNode imports +// Custom area end: org.zwobble.hobgoblin.compiler.ast.typed.TypedFunctionExpressionNode imports + +public record TypedFunctionExpressionNode( + java.util.List params, + org.zwobble.hobgoblin.compiler.ast.typed.TypedExpressionNode body, + org.zwobble.hobgoblin.compiler.types.Type type, + org.zwobble.hobgoblin.compiler.sources.Source source +) implements org.zwobble.hobgoblin.compiler.ast.typed.TypedExpressionNode { + public static org.zwobble.hobgoblin.compiler.ast.typed.TypedFunctionExpressionNode.Builder arbitrary() { + return new org.zwobble.hobgoblin.compiler.ast.typed.TypedFunctionExpressionNode.Builder(java.util.List.of(), org.zwobble.hobgoblin.compiler.ast.typed.TypedNumberLiteralNode.arbitrary().build(), org.zwobble.hobgoblin.compiler.types.HobgoblinNativeTypes.arbitraryType(), org.zwobble.hobgoblin.compiler.sources.HobgoblinNativeSources.arbitrarySource()); + } + + public record Builder( + java.util.List params, + org.zwobble.hobgoblin.compiler.ast.typed.TypedExpressionNode body, + org.zwobble.hobgoblin.compiler.types.Type type, + org.zwobble.hobgoblin.compiler.sources.Source source + ) implements org.zwobble.hobgoblin.compiler.ast.typed.TypedExpressionNode.Builder { + public org.zwobble.hobgoblin.compiler.ast.typed.TypedFunctionExpressionNode build() { + return new org.zwobble.hobgoblin.compiler.ast.typed.TypedFunctionExpressionNode(params, body, type, source); + } + + public org.zwobble.hobgoblin.compiler.ast.typed.TypedFunctionExpressionNode.Builder withParams(java.util.List params) { + return new org.zwobble.hobgoblin.compiler.ast.typed.TypedFunctionExpressionNode.Builder(params, body, type, source); + } + + public org.zwobble.hobgoblin.compiler.ast.typed.TypedFunctionExpressionNode.Builder addParam(org.zwobble.hobgoblin.compiler.ast.typed.TypedParamNode param) { + var params = new java.util.ArrayList(this.params); + params.add(param); + return new org.zwobble.hobgoblin.compiler.ast.typed.TypedFunctionExpressionNode.Builder(params, body, type, source); + } + + public org.zwobble.hobgoblin.compiler.ast.typed.TypedFunctionExpressionNode.Builder addParam(org.zwobble.hobgoblin.compiler.ast.typed.TypedParamNode.Builder param) { + var params = new java.util.ArrayList(this.params); + params.add(param.build()); + return new org.zwobble.hobgoblin.compiler.ast.typed.TypedFunctionExpressionNode.Builder(params, body, type, source); + } + + public org.zwobble.hobgoblin.compiler.ast.typed.TypedFunctionExpressionNode.Builder withBody(org.zwobble.hobgoblin.compiler.ast.typed.TypedExpressionNode body) { + return new org.zwobble.hobgoblin.compiler.ast.typed.TypedFunctionExpressionNode.Builder(params, body, type, source); + } + + public org.zwobble.hobgoblin.compiler.ast.typed.TypedFunctionExpressionNode.Builder withBody(org.zwobble.hobgoblin.compiler.ast.typed.TypedExpressionNode.Builder body) { + return new org.zwobble.hobgoblin.compiler.ast.typed.TypedFunctionExpressionNode.Builder(params, body.build(), type, source); + } + + public org.zwobble.hobgoblin.compiler.ast.typed.TypedFunctionExpressionNode.Builder withType(org.zwobble.hobgoblin.compiler.types.Type type) { + return new org.zwobble.hobgoblin.compiler.ast.typed.TypedFunctionExpressionNode.Builder(params, body, type, source); + } + + public org.zwobble.hobgoblin.compiler.ast.typed.TypedFunctionExpressionNode.Builder withSource(org.zwobble.hobgoblin.compiler.sources.Source source) { + return new org.zwobble.hobgoblin.compiler.ast.typed.TypedFunctionExpressionNode.Builder(params, body, type, source); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.ast.typed.TypedFunctionExpressionNode.Builder body + // Custom area end: org.zwobble.hobgoblin.compiler.ast.typed.TypedFunctionExpressionNode.Builder body + } + + // Custom area start: org.zwobble.hobgoblin.compiler.ast.typed.TypedFunctionExpressionNode body + // Custom area end: org.zwobble.hobgoblin.compiler.ast.typed.TypedFunctionExpressionNode body +} diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedParamNode.java b/src/main/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedParamNode.java new file mode 100644 index 0000000..cf4f6aa --- /dev/null +++ b/src/main/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedParamNode.java @@ -0,0 +1,44 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.ast.typed; + +// Custom area start: org.zwobble.hobgoblin.compiler.ast.typed.TypedParamNode imports +// Custom area end: org.zwobble.hobgoblin.compiler.ast.typed.TypedParamNode imports + +public record TypedParamNode( + String name, + org.zwobble.hobgoblin.compiler.types.Type type, + org.zwobble.hobgoblin.compiler.sources.Source source +) { + public static org.zwobble.hobgoblin.compiler.ast.typed.TypedParamNode.Builder arbitrary() { + return new org.zwobble.hobgoblin.compiler.ast.typed.TypedParamNode.Builder("", org.zwobble.hobgoblin.compiler.types.HobgoblinNativeTypes.arbitraryType(), org.zwobble.hobgoblin.compiler.sources.HobgoblinNativeSources.arbitrarySource()); + } + + public record Builder( + String name, + org.zwobble.hobgoblin.compiler.types.Type type, + org.zwobble.hobgoblin.compiler.sources.Source source + ) { + public org.zwobble.hobgoblin.compiler.ast.typed.TypedParamNode build() { + return new org.zwobble.hobgoblin.compiler.ast.typed.TypedParamNode(name, type, source); + } + + public org.zwobble.hobgoblin.compiler.ast.typed.TypedParamNode.Builder withName(String name) { + return new org.zwobble.hobgoblin.compiler.ast.typed.TypedParamNode.Builder(name, type, source); + } + + public org.zwobble.hobgoblin.compiler.ast.typed.TypedParamNode.Builder withType(org.zwobble.hobgoblin.compiler.types.Type type) { + return new org.zwobble.hobgoblin.compiler.ast.typed.TypedParamNode.Builder(name, type, source); + } + + public org.zwobble.hobgoblin.compiler.ast.typed.TypedParamNode.Builder withSource(org.zwobble.hobgoblin.compiler.sources.Source source) { + return new org.zwobble.hobgoblin.compiler.ast.typed.TypedParamNode.Builder(name, type, source); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.ast.typed.TypedParamNode.Builder body + // Custom area end: org.zwobble.hobgoblin.compiler.ast.typed.TypedParamNode.Builder body + } + + // Custom area start: org.zwobble.hobgoblin.compiler.ast.typed.TypedParamNode body + // Custom area end: org.zwobble.hobgoblin.compiler.ast.typed.TypedParamNode body +} diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeChecker.java b/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeChecker.java index 741b8d4..d4b9c1c 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeChecker.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeChecker.java @@ -714,8 +714,8 @@ public class TypeChecker { throw new UnsupportedOperationException("TODO"); } - case UntypedFunctionExpressionNode _ -> { - throw new UnsupportedOperationException("TODO"); + case UntypedFunctionExpressionNode functionExpression -> { + yield typeCheckFunctionExpression(functionExpression, typeHint, context); } case UntypedMemberAccessNode memberAccess -> { @@ -778,6 +778,22 @@ public class TypeChecker { return value; } + private static TypedExpressionNode typeCheckFunctionExpression( + UntypedFunctionExpressionNode untyped, + Optional typeHint, + TypeCheckerNamespaceContext context + ) { + var body = typeCheckExpression(untyped.body(), Optional.empty(), context); + + return new TypedFunctionExpressionNode( + List.of(), + body, + // TODO: proper type + NativeTypes.INT_32, + untyped.source() + ); + } + private static TypedExpressionNode typeCheckMemberAccess( UntypedMemberAccessNode untyped, TypeCheckerNamespaceContext context diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedFunctionExpressionNodeMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedFunctionExpressionNodeMatcher.java new file mode 100644 index 0000000..175ab05 --- /dev/null +++ b/src/test/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedFunctionExpressionNodeMatcher.java @@ -0,0 +1,59 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.ast.typed; + +// Custom area start: org.zwobble.hobgoblin.compiler.ast.typed.TypedFunctionExpressionNodeMatcher imports +// Custom area end: org.zwobble.hobgoblin.compiler.ast.typed.TypedFunctionExpressionNodeMatcher imports + +public final class TypedFunctionExpressionNodeMatcher implements org.zwobble.precisely.Matcher { + public static org.zwobble.hobgoblin.compiler.ast.typed.TypedFunctionExpressionNodeMatcher isTypedFunctionExpressionNode() { + return new org.zwobble.hobgoblin.compiler.ast.typed.TypedFunctionExpressionNodeMatcher(java.util.List.of()); + } + + private final java.util.List> submatchers; + + private TypedFunctionExpressionNodeMatcher( + java.util.List> submatchers + ) { + this.submatchers = submatchers; + } + + public org.zwobble.precisely.MatchResult match(java.lang.Object actual) { + return this.toMatcher().match(actual); + } + + public org.zwobble.precisely.TextTree describe() { + return this.toMatcher().describe(); + } + + private org.zwobble.precisely.Matcher toMatcher() { + return org.zwobble.precisely.Matchers.instanceOf(org.zwobble.hobgoblin.compiler.ast.typed.TypedFunctionExpressionNode.class, this.submatchers); + } + + public org.zwobble.hobgoblin.compiler.ast.typed.TypedFunctionExpressionNodeMatcher withParams(org.zwobble.precisely.Matcher> params) { + var submatchers = new java.util.ArrayList>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("params", org.zwobble.hobgoblin.compiler.ast.typed.TypedFunctionExpressionNode::params, (org.zwobble.precisely.Matcher) params)); + return new org.zwobble.hobgoblin.compiler.ast.typed.TypedFunctionExpressionNodeMatcher(submatchers); + } + + public org.zwobble.hobgoblin.compiler.ast.typed.TypedFunctionExpressionNodeMatcher withBody(org.zwobble.precisely.Matcher body) { + var submatchers = new java.util.ArrayList>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("body", org.zwobble.hobgoblin.compiler.ast.typed.TypedFunctionExpressionNode::body, (org.zwobble.precisely.Matcher) body)); + return new org.zwobble.hobgoblin.compiler.ast.typed.TypedFunctionExpressionNodeMatcher(submatchers); + } + + public org.zwobble.hobgoblin.compiler.ast.typed.TypedFunctionExpressionNodeMatcher withType(org.zwobble.precisely.Matcher type) { + var submatchers = new java.util.ArrayList>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("type", org.zwobble.hobgoblin.compiler.ast.typed.TypedFunctionExpressionNode::type, (org.zwobble.precisely.Matcher) type)); + return new org.zwobble.hobgoblin.compiler.ast.typed.TypedFunctionExpressionNodeMatcher(submatchers); + } + + public org.zwobble.hobgoblin.compiler.ast.typed.TypedFunctionExpressionNodeMatcher withSource(org.zwobble.precisely.Matcher source) { + var submatchers = new java.util.ArrayList>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("source", org.zwobble.hobgoblin.compiler.ast.typed.TypedFunctionExpressionNode::source, (org.zwobble.precisely.Matcher) source)); + return new org.zwobble.hobgoblin.compiler.ast.typed.TypedFunctionExpressionNodeMatcher(submatchers); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.ast.typed.TypedFunctionExpressionNodeMatcher body + // Custom area end: org.zwobble.hobgoblin.compiler.ast.typed.TypedFunctionExpressionNodeMatcher body +} diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedParamNodeMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedParamNodeMatcher.java new file mode 100644 index 0000000..5efa568 --- /dev/null +++ b/src/test/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedParamNodeMatcher.java @@ -0,0 +1,53 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.ast.typed; + +// Custom area start: org.zwobble.hobgoblin.compiler.ast.typed.TypedParamNodeMatcher imports +// Custom area end: org.zwobble.hobgoblin.compiler.ast.typed.TypedParamNodeMatcher imports + +public final class TypedParamNodeMatcher implements org.zwobble.precisely.Matcher { + public static org.zwobble.hobgoblin.compiler.ast.typed.TypedParamNodeMatcher isTypedParamNode() { + return new org.zwobble.hobgoblin.compiler.ast.typed.TypedParamNodeMatcher(java.util.List.of()); + } + + private final java.util.List> submatchers; + + private TypedParamNodeMatcher( + java.util.List> submatchers + ) { + this.submatchers = submatchers; + } + + public org.zwobble.precisely.MatchResult match(java.lang.Object actual) { + return this.toMatcher().match(actual); + } + + public org.zwobble.precisely.TextTree describe() { + return this.toMatcher().describe(); + } + + private org.zwobble.precisely.Matcher toMatcher() { + return org.zwobble.precisely.Matchers.instanceOf(org.zwobble.hobgoblin.compiler.ast.typed.TypedParamNode.class, this.submatchers); + } + + public org.zwobble.hobgoblin.compiler.ast.typed.TypedParamNodeMatcher withName(org.zwobble.precisely.Matcher name) { + var submatchers = new java.util.ArrayList>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("name", org.zwobble.hobgoblin.compiler.ast.typed.TypedParamNode::name, (org.zwobble.precisely.Matcher) name)); + return new org.zwobble.hobgoblin.compiler.ast.typed.TypedParamNodeMatcher(submatchers); + } + + public org.zwobble.hobgoblin.compiler.ast.typed.TypedParamNodeMatcher withType(org.zwobble.precisely.Matcher type) { + var submatchers = new java.util.ArrayList>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("type", org.zwobble.hobgoblin.compiler.ast.typed.TypedParamNode::type, (org.zwobble.precisely.Matcher) type)); + return new org.zwobble.hobgoblin.compiler.ast.typed.TypedParamNodeMatcher(submatchers); + } + + public org.zwobble.hobgoblin.compiler.ast.typed.TypedParamNodeMatcher withSource(org.zwobble.precisely.Matcher source) { + var submatchers = new java.util.ArrayList>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("source", org.zwobble.hobgoblin.compiler.ast.typed.TypedParamNode::source, (org.zwobble.precisely.Matcher) source)); + return new org.zwobble.hobgoblin.compiler.ast.typed.TypedParamNodeMatcher(submatchers); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.ast.typed.TypedParamNodeMatcher body + // Custom area end: org.zwobble.hobgoblin.compiler.ast.typed.TypedParamNodeMatcher body +} diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerFunctionExpressionTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerFunctionExpressionTests.java new file mode 100644 index 0000000..f927b95 --- /dev/null +++ b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerFunctionExpressionTests.java @@ -0,0 +1,39 @@ +package org.zwobble.hobgoblin.compiler.typechecker; + +import org.junit.jupiter.api.Test; +import org.zwobble.hobgoblin.compiler.ast.untyped.UntypedFunctionExpressionNode; +import org.zwobble.hobgoblin.compiler.ast.untyped.UntypedReferenceNode; +import org.zwobble.hobgoblin.compiler.builtins.NativeTypes; +import org.zwobble.hobgoblin.compiler.sources.NullSource; +import org.zwobble.hobgoblin.compiler.types.NamespaceName; + +import java.util.List; +import java.util.Optional; + +import static org.zwobble.hobgoblin.compiler.ast.typed.TypedFunctionExpressionNodeMatcher.isTypedFunctionExpressionNode; +import static org.zwobble.hobgoblin.compiler.ast.typed.TypedReferenceNodeMatcher.isTypedReferenceNode; +import static org.zwobble.precisely.AssertThat.assertThat; +import static org.zwobble.precisely.Matchers.equalTo; +import static org.zwobble.precisely.Matchers.isSequence; + +public class TypeCheckerFunctionExpressionTests { + @Test + public void bodyOfFunctionExpressionIsTypeChecked() { + var untyped = UntypedFunctionExpressionNode.arbitrary() + .withParams(List.of()) + .withBody(UntypedReferenceNode.arbitrary().withName("a")) + .build(); + var globalContext = TypeCheckerContextArb.globalContext(); + var namespaceName = NamespaceName.of("App"); + var namespaceContext = globalContext.enterNamespace(namespaceName); + namespaceContext.declare("a", NativeTypes.INT_32, NullSource.INSTANCE); + + var typed = TypeChecker.typeCheckExpression(untyped, Optional.empty(), namespaceContext); + + // TODO: assert type + assertThat(typed, isTypedFunctionExpressionNode() + .withParams(isSequence()) + .withBody(isTypedReferenceNode().withName(equalTo("a")).withType(equalTo(NativeTypes.INT_32))) + ); + } +} -- cgit v1.2.3