From eb145887ab57b75299a55c85c753d3b4707105be Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 9 Aug 2026 22:16:47 +0100 Subject: Introduce variables for type params --- .../zwobble/hobgoblin/compiler/typechecker/TypeChecker.java | 11 ++++++++++- .../compiler/typechecker/TypeCheckerNamespaceContext.java | 13 +++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) (limited to 'src/main') 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 97008b6..63fc357 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeChecker.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeChecker.java @@ -239,8 +239,17 @@ public class TypeChecker { throw new UnsupportedOperationException("TODO"); }; + var typeParams = switch (typeOrConstructor) { + case TypeOrConstructor.Type _ -> + List.of(); + + case TypeOrConstructor.Constructor constructor -> + constructor.value().params(); + }; + var bodyContext = context.enter(typeParams); + var typeCheckedFieldDefinitions = untyped.fields().isPresent() - ? Optional.of(typeCheckFieldDefinitions(untyped.fields().get(), context)) + ? Optional.of(typeCheckFieldDefinitions(untyped.fields().get(), bodyContext)) : Optional.empty(); var typedFields = typeCheckedFieldDefinitions.map(TypeCheckFieldDefinitionsResult::fields); var typedFieldNodes = typeCheckedFieldDefinitions.map(TypeCheckFieldDefinitionsResult::typedFieldNodes); diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerNamespaceContext.java b/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerNamespaceContext.java index 2c8ad67..ce7153f 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerNamespaceContext.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerNamespaceContext.java @@ -89,4 +89,17 @@ public class TypeCheckerNamespaceContext { public List fieldsOf(SumType type) { return this.typesInfo.fieldsOf(type); } + + public TypeCheckerNamespaceContext enter(List typeParams) { + var newVariables = new HashMap<>(this.variables); + for (var typeParam : typeParams) { + newVariables.put(typeParam.name(), Types.metaType(typeParam)); + } + + return new TypeCheckerNamespaceContext( + this.namespaceName, + newVariables, + this.typesInfo + ); + } } -- cgit v1.2.3