From c93c58c1bf84fac0903dbc7788eed9634123a0e2 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 29 Nov 2026 11:21:01 +0000 Subject: Type check function expression params --- .../compiler/typechecker/TypeChecker.java | 37 ++++++++++++++++++++-- .../TypeCheckerFunctionExpressionTests.java | 34 ++++++++++++++++++++ 2 files changed, 68 insertions(+), 3 deletions(-) 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 333d955..a462929 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeChecker.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeChecker.java @@ -783,12 +783,43 @@ public class TypeChecker { Optional typeHint, TypeCheckerNamespaceContext context ) { - var body = typeCheckExpression(untyped.body(), Optional.empty(), context); + var functionTypeHint = typeHint.flatMap( + hint -> hint instanceof FunctionType functionType + ? Optional.of(functionType) + : Optional.empty() + ); + + var typedParams = new ArrayList(); + + for (var paramIndex = 0; paramIndex < untyped.params().size(); paramIndex++) { + var untypedParam = untyped.params().get(paramIndex); + + // TODO: param count mismatch + if (functionTypeHint.isEmpty()) { + throw new UnsupportedOperationException("TODO"); + } + var paramTypeHint = functionTypeHint.get().params().get(paramIndex); + + var typedParam = new TypedParamNode( + untypedParam.name(), + paramTypeHint, + untypedParam.source() + ); + + typedParams.add(typedParam); + } + + var paramTypes = typedParams.stream() + .map(param -> param.type()) + .toList(); + + var bodyTypeHint = functionTypeHint.map(hint -> hint.returns()); + var body = typeCheckExpression(untyped.body(), bodyTypeHint, context); - var type = new FunctionType(List.of(), body.type()); + var type = new FunctionType(paramTypes, body.type()); return new TypedFunctionExpressionNode( - List.of(), + typedParams, body, type, untyped.source() diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerFunctionExpressionTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerFunctionExpressionTests.java index 9a261c6..0dde897 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerFunctionExpressionTests.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerFunctionExpressionTests.java @@ -2,16 +2,21 @@ 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.UntypedNumberLiteralNode; +import org.zwobble.hobgoblin.compiler.ast.untyped.UntypedParamNode; 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.FunctionType; import org.zwobble.hobgoblin.compiler.types.NamespaceName; +import org.zwobble.hobgoblin.compiler.types.Types; +import java.math.BigDecimal; 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.TypedParamNodeMatcher.isTypedParamNode; import static org.zwobble.hobgoblin.compiler.ast.typed.TypedReferenceNodeMatcher.isTypedReferenceNode; import static org.zwobble.precisely.AssertThat.assertThat; import static org.zwobble.precisely.Matchers.equalTo; @@ -40,4 +45,33 @@ public class TypeCheckerFunctionExpressionTests { ))) ); } + + @Test + public void paramTypesAreInferredFromHint() { + var untyped = UntypedFunctionExpressionNode.arbitrary() + .addParam(UntypedParamNode.arbitrary().withName("a")) + .addParam(UntypedParamNode.arbitrary().withName("b")) + .withBody(UntypedNumberLiteralNode.arbitrary().withValue(BigDecimal.ZERO)) + .build(); + var globalContext = TypeCheckerContextArb.globalContext(); + var namespaceName = NamespaceName.of("App"); + var namespaceContext = globalContext.enterNamespace(namespaceName); + + var typeHint = new FunctionType( + List.of(NativeTypes.INT_64, NativeTypes.STRING), + NativeTypes.INT_32 + ); + var typed = TypeChecker.typeCheckExpression(untyped, Optional.of(typeHint), namespaceContext); + + assertThat(typed, isTypedFunctionExpressionNode() + .withParams(isSequence( + isTypedParamNode().withName(equalTo("a")).withType(equalTo(NativeTypes.INT_64)), + isTypedParamNode().withName(equalTo("b")).withType(equalTo(NativeTypes.STRING)) + )) + .withType(equalTo(new FunctionType( + List.of(NativeTypes.INT_64, NativeTypes.STRING), + Types.constantNumber(BigDecimal.ZERO) + ))) + ); + } } -- cgit v1.2.3