From 32912393354d2a1d4dfa96e91e539181490020ee Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Fri, 26 Jun 2026 16:03:10 +0100 Subject: Extract TypesInfo interface --- .../typechecker/TypeCheckerGlobalContext.java | 2 +- .../hobgoblin/compiler/typechecker/TypesInfo.java | 39 +++----------------- .../compiler/typechecker/TypesInfoInMemory.java | 41 ++++++++++++++++++++++ 3 files changed, 47 insertions(+), 35 deletions(-) create mode 100644 src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypesInfoInMemory.java (limited to 'src/main/java/org/zwobble') 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 ca2d731..28ad532 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerGlobalContext.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerGlobalContext.java @@ -20,7 +20,7 @@ public class TypeCheckerGlobalContext { } public TypesInfo toTypesInfo() { - return new TypesInfo( + return new TypesInfoInMemory( this.enumVariants, this.fieldsOf, this.sumVariants 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 63d21b6..b5bfb89 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypesInfo.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypesInfo.java @@ -1,41 +1,12 @@ package org.zwobble.hobgoblin.compiler.typechecker; import org.zwobble.hobgoblin.compiler.types.*; -import org.zwobble.hobgoblin.compiler.util.ManyToMany; import java.util.List; -import java.util.Map; -import java.util.Optional; -public class TypesInfo { - private final Map> enumVariants; - private final Map> fieldsOf; - private final ManyToMany sumVariants; - - public TypesInfo( - Map> enumVariants, - Map> fieldsOf, - ManyToMany sumVariants - ) { - this.enumVariants = enumVariants; - this.fieldsOf = fieldsOf; - this.sumVariants = sumVariants; - } - - public List enumVariants(EnumType enumType) { - return this.enumVariants.get(enumType); - } - - public List fieldsOf(StructType structType) { - // TODO: handle error better - return Optional.ofNullable(this.fieldsOf.get(structType)).orElseThrow(); - } - - public List variantOf(StructType variantType) { - return this.sumVariants.rightToLeft(variantType); - } - - public List sumVariants(SumType type) { - return this.sumVariants.leftToRight(type); - } +public interface TypesInfo { + List enumVariants(EnumType enumType); + List fieldsOf(StructType structType); + List variantOf(StructType variantType); + List sumVariants(SumType type); } diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypesInfoInMemory.java b/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypesInfoInMemory.java new file mode 100644 index 0000000..dae23ea --- /dev/null +++ b/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypesInfoInMemory.java @@ -0,0 +1,41 @@ +package org.zwobble.hobgoblin.compiler.typechecker; + +import org.zwobble.hobgoblin.compiler.types.*; +import org.zwobble.hobgoblin.compiler.util.ManyToMany; + +import java.util.List; +import java.util.Map; +import java.util.Optional; + +public class TypesInfoInMemory implements TypesInfo { + private final Map> enumVariants; + private final Map> fieldsOf; + private final ManyToMany sumVariants; + + public TypesInfoInMemory( + Map> enumVariants, + Map> fieldsOf, + ManyToMany sumVariants + ) { + this.enumVariants = enumVariants; + this.fieldsOf = fieldsOf; + this.sumVariants = sumVariants; + } + + public List enumVariants(EnumType enumType) { + return this.enumVariants.get(enumType); + } + + public List fieldsOf(StructType structType) { + // TODO: handle error better + return Optional.ofNullable(this.fieldsOf.get(structType)).orElseThrow(); + } + + public List variantOf(StructType variantType) { + return this.sumVariants.rightToLeft(variantType); + } + + public List sumVariants(SumType type) { + return this.sumVariants.leftToRight(type); + } +} -- cgit v1.2.3