summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2026-06-26 16:18:38 +0100
committerMichael Williamson <mike@zwobble.org>2026-06-26 16:18:38 +0100
commitc0cbdea8e3ca03ae507ece22eb530977b3149793 (patch)
tree7c649ee20e977746895054f3d716d43519cbcd32 /src
parent32912393354d2a1d4dfa96e91e539181490020ee (diff)
Use TypesInfoInMemory in context
Diffstat (limited to 'src')
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeChecker.java8
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerGlobalContext.java14
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerNamespaceContext.java44
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypesInfo.java5
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypesInfoInMemory.java51
5 files changed, 75 insertions, 47 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 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<String, Type> nativeTypes = new HashMap<>();
- private final Map<EnumType, List<EnumVariant>> enumVariants = new HashMap<>();
- private final Map<Type, List<Field>> fieldsOf = new HashMap<>();
+ private final TypesInfoInMemory typesInfo = TypesInfoInMemory.empty();
private final Map<NamespaceName, Fields> namespaceFieldsOf = new HashMap<>();
- private final ManyToMany<SumType, Type> 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<String, Type> nativeTypes,
- Map<EnumType, List<EnumVariant>> enumVariants,
- Map<Type, List<Field>> fieldsOf,
- ManyToMany<SumType, Type> variants
+ TypesInfoInMemory typesInfo
) {
return new TypeCheckerNamespaceContext(
namespaceName,
new HashMap<>(nativeTypes),
- enumVariants,
- fieldsOf,
- variants
+ typesInfo
);
}
private final NamespaceName namespaceName;
private final Map<String, Type> variables;
- private final Map<EnumType, List<EnumVariant>> enumVariants;
- private final Map<Type, List<Field>> fieldsOf;
- private final ManyToMany<SumType, Type> sumVariants;
+ private final TypesInfoInMemory typesInfo;
private TypeCheckerNamespaceContext(
NamespaceName namespaceName,
Map<String, Type> variables,
- Map<EnumType, List<EnumVariant>> enumVariants,
- Map<Type, List<Field>> fieldsOf,
- ManyToMany<SumType, Type> 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<EnumVariant> enumVariants) {
- this.enumVariants.put(enumType, enumVariants);
+ this.typesInfo.defineEnumType(enumType, enumVariants);
}
public void defineStructType(StructType structType, List<Field> fields) {
- this.fieldsOf.put(structType, fields);
+ this.typesInfo.defineStructType(structType, fields);
}
- public Optional<List<Field>> fieldsOf(StructType type) {
- return Optional.ofNullable(this.fieldsOf.get(type));
+ public boolean isDefined(StructType type) {
+ return this.typesInfo.isDefined(type);
+ }
+
+ public List<Field> fieldsOf(StructType type) {
+ return this.typesInfo.fieldsOf(type);
}
public void defineSumType(
@@ -75,17 +68,14 @@ public class TypeCheckerNamespaceContext {
List<StructType> variantTypes,
List<Field> fields
) {
- for (var variantType : variantTypes) {
- this.sumVariants.add(sumType, variantType);
- }
- this.fieldsOf.put(sumType, fields);
+ this.typesInfo.defineSumType(sumType, variantTypes, fields);
}
public List<SumType> variantOf(StructType variantType) {
- return this.sumVariants.rightToLeft(variantType);
+ return this.typesInfo.variantOf(variantType);
}
- public Optional<List<Field>> fieldsOf(SumType type) {
- return Optional.ofNullable(this.fieldsOf.get(type));
+ public List<Field> 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<EnumVariant> enumVariants(EnumType enumType);
+ boolean isDefined(StructType type);
List<Field> fieldsOf(StructType structType);
+
List<SumType> variantOf(StructType variantType);
List<Type> sumVariants(SumType type);
+
+ List<EnumVariant> 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<EnumType, List<EnumVariant>> enumVariants;
private final Map<Type, List<Field>> fieldsOf;
private final ManyToMany<SumType, Type> sumVariants;
@@ -22,13 +30,41 @@ public class TypesInfoInMemory implements TypesInfo {
this.sumVariants = sumVariants;
}
+ public void defineEnumType(EnumType enumType, List<EnumVariant> enumVariants) {
+ this.enumVariants.put(enumType, enumVariants);
+ }
+
public List<EnumVariant> enumVariants(EnumType enumType) {
return this.enumVariants.get(enumType);
}
+ public void defineStructType(StructType structType, List<Field> fields) {
+ this.fieldsOf.put(structType, fields);
+ }
+
+ @Override
+ public boolean isDefined(StructType type) {
+ return this.fieldsOf.containsKey(type);
+ }
+
public List<Field> 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<StructType> variantTypes,
+ List<Field> fields
+ ) {
+ for (var variantType : variantTypes) {
+ this.sumVariants.add(sumType, variantType);
+ }
+ this.fieldsOf.put(sumType, fields);
}
public List<SumType> variantOf(StructType variantType) {
@@ -38,4 +74,13 @@ public class TypesInfoInMemory implements TypesInfo {
public List<Type> sumVariants(SumType type) {
return this.sumVariants.leftToRight(type);
}
+
+ public List<Field> fieldsOf(SumType sumType) {
+ var fields = this.fieldsOf.get(sumType);
+ if (fields == null) {
+ return List.of();
+ } else {
+ return fields;
+ }
+ }
}