From c0cbdea8e3ca03ae507ece22eb530977b3149793 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Fri, 26 Jun 2026 16:18:38 +0100 Subject: Use TypesInfoInMemory in context --- .../compiler/typechecker/TypeChecker.java | 8 ++-- .../typechecker/TypeCheckerGlobalContext.java | 14 ++---- .../typechecker/TypeCheckerNamespaceContext.java | 44 ++++++++----------- .../hobgoblin/compiler/typechecker/TypesInfo.java | 5 ++- .../compiler/typechecker/TypesInfoInMemory.java | 51 ++++++++++++++++++++-- 5 files changed, 75 insertions(+), 47 deletions(-) (limited to 'src') 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 841c934..cb6dae1 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeChecker.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeChecker.java @@ -185,8 +185,7 @@ public class TypeChecker { context.defineStructType(structType, typeCheckedFieldDefinitions.fields()); for (var sumType : context.variantOf(structType)) { - // TODO: better error if sum type fields aren't defined - var sumTypeFields = context.fieldsOf(sumType).orElseThrow(); + var sumTypeFields = context.fieldsOf(sumType); typeCheckVariantType( sumType, sumTypeFields, @@ -262,13 +261,12 @@ public class TypeChecker { context.defineSumType(sumType, variantTypes, typeCheckedFieldDefinitions.fields); for (var variantType : variantTypes) { - var variantTypeFields = context.fieldsOf(variantType); - if (variantTypeFields.isPresent()) { + if (context.isDefined(variantType)) { typeCheckVariantType( sumType, typeCheckedFieldDefinitions.fields, variantType, - variantTypeFields.get() + context.fieldsOf(variantType) ); } } diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerGlobalContext.java b/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerGlobalContext.java index 28ad532..cf61a22 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerGlobalContext.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerGlobalContext.java @@ -7,10 +7,8 @@ import java.util.*; public class TypeCheckerGlobalContext { private final Map nativeTypes = new HashMap<>(); - private final Map> enumVariants = new HashMap<>(); - private final Map> fieldsOf = new HashMap<>(); + private final TypesInfoInMemory typesInfo = TypesInfoInMemory.empty(); private final Map namespaceFieldsOf = new HashMap<>(); - private final ManyToMany sumVariants = new ManyToMany<>(); public static TypeCheckerGlobalContext initial() { return new TypeCheckerGlobalContext(); @@ -20,11 +18,7 @@ public class TypeCheckerGlobalContext { } public TypesInfo toTypesInfo() { - return new TypesInfoInMemory( - this.enumVariants, - this.fieldsOf, - this.sumVariants - ); + return this.typesInfo; } public void addNativeType(SimpleNativeType type) { @@ -39,9 +33,7 @@ public class TypeCheckerGlobalContext { return TypeCheckerNamespaceContext.initial( namespaceName, this.nativeTypes, - this.enumVariants, - this.fieldsOf, - this.sumVariants + this.typesInfo ); } 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 0df7e16..0ffbe66 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerNamespaceContext.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerNamespaceContext.java @@ -1,7 +1,6 @@ package org.zwobble.hobgoblin.compiler.typechecker; import org.zwobble.hobgoblin.compiler.types.*; -import org.zwobble.hobgoblin.compiler.util.ManyToMany; import java.util.HashMap; import java.util.List; @@ -12,37 +11,27 @@ public class TypeCheckerNamespaceContext { public static TypeCheckerNamespaceContext initial( NamespaceName namespaceName, Map nativeTypes, - Map> enumVariants, - Map> fieldsOf, - ManyToMany variants + TypesInfoInMemory typesInfo ) { return new TypeCheckerNamespaceContext( namespaceName, new HashMap<>(nativeTypes), - enumVariants, - fieldsOf, - variants + typesInfo ); } private final NamespaceName namespaceName; private final Map variables; - private final Map> enumVariants; - private final Map> fieldsOf; - private final ManyToMany sumVariants; + private final TypesInfoInMemory typesInfo; private TypeCheckerNamespaceContext( NamespaceName namespaceName, Map variables, - Map> enumVariants, - Map> fieldsOf, - ManyToMany sumVariants + TypesInfoInMemory typesInfo ) { this.namespaceName = namespaceName; this.variables = variables; - this.enumVariants = enumVariants; - this.fieldsOf = fieldsOf; - this.sumVariants = sumVariants; + this.typesInfo = typesInfo; } public NamespaceName namespaceName() { @@ -59,15 +48,19 @@ public class TypeCheckerNamespaceContext { } public void defineEnumType(EnumType enumType, List enumVariants) { - this.enumVariants.put(enumType, enumVariants); + this.typesInfo.defineEnumType(enumType, enumVariants); } public void defineStructType(StructType structType, List fields) { - this.fieldsOf.put(structType, fields); + this.typesInfo.defineStructType(structType, fields); } - public Optional> fieldsOf(StructType type) { - return Optional.ofNullable(this.fieldsOf.get(type)); + public boolean isDefined(StructType type) { + return this.typesInfo.isDefined(type); + } + + public List fieldsOf(StructType type) { + return this.typesInfo.fieldsOf(type); } public void defineSumType( @@ -75,17 +68,14 @@ public class TypeCheckerNamespaceContext { List variantTypes, List fields ) { - for (var variantType : variantTypes) { - this.sumVariants.add(sumType, variantType); - } - this.fieldsOf.put(sumType, fields); + this.typesInfo.defineSumType(sumType, variantTypes, fields); } public List variantOf(StructType variantType) { - return this.sumVariants.rightToLeft(variantType); + return this.typesInfo.variantOf(variantType); } - public Optional> fieldsOf(SumType type) { - return Optional.ofNullable(this.fieldsOf.get(type)); + public List fieldsOf(SumType type) { + return this.typesInfo.fieldsOf(type); } } diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypesInfo.java b/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypesInfo.java index b5bfb89..4e896dd 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypesInfo.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypesInfo.java @@ -5,8 +5,11 @@ import org.zwobble.hobgoblin.compiler.types.*; import java.util.List; public interface TypesInfo { - List enumVariants(EnumType enumType); + boolean isDefined(StructType type); List fieldsOf(StructType structType); + List variantOf(StructType variantType); List sumVariants(SumType type); + + List enumVariants(EnumType enumType); } diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypesInfoInMemory.java b/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypesInfoInMemory.java index dae23ea..d9c0c85 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypesInfoInMemory.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypesInfoInMemory.java @@ -3,11 +3,19 @@ package org.zwobble.hobgoblin.compiler.typechecker; import org.zwobble.hobgoblin.compiler.types.*; import org.zwobble.hobgoblin.compiler.util.ManyToMany; +import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.Optional; public class TypesInfoInMemory implements TypesInfo { + public static TypesInfoInMemory empty() { + return new TypesInfoInMemory( + new HashMap<>(), + new HashMap<>(), + new ManyToMany<>() + ); + } + private final Map> enumVariants; private final Map> fieldsOf; private final ManyToMany sumVariants; @@ -22,13 +30,41 @@ public class TypesInfoInMemory implements TypesInfo { this.sumVariants = sumVariants; } + public void defineEnumType(EnumType enumType, List enumVariants) { + this.enumVariants.put(enumType, enumVariants); + } + public List enumVariants(EnumType enumType) { return this.enumVariants.get(enumType); } + public void defineStructType(StructType structType, List fields) { + this.fieldsOf.put(structType, fields); + } + + @Override + public boolean isDefined(StructType type) { + return this.fieldsOf.containsKey(type); + } + public List fieldsOf(StructType structType) { - // TODO: handle error better - return Optional.ofNullable(this.fieldsOf.get(structType)).orElseThrow(); + var fields = this.fieldsOf.get(structType); + if (fields == null) { + return List.of(); + } else { + return fields; + } + } + + public void defineSumType( + SumType sumType, + List variantTypes, + List fields + ) { + for (var variantType : variantTypes) { + this.sumVariants.add(sumType, variantType); + } + this.fieldsOf.put(sumType, fields); } public List variantOf(StructType variantType) { @@ -38,4 +74,13 @@ public class TypesInfoInMemory implements TypesInfo { public List sumVariants(SumType type) { return this.sumVariants.leftToRight(type); } + + public List fieldsOf(SumType sumType) { + var fields = this.fieldsOf.get(sumType); + if (fields == null) { + return List.of(); + } else { + return fields; + } + } } -- cgit v1.2.3