summaryrefslogtreecommitdiff
path: root/src/main/java/org/zwobble
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2026-05-18 19:16:03 +0100
committerMichael Williamson <mike@zwobble.org>2026-05-18 19:16:03 +0100
commit89fea0eb309e943a01d45aa5bcc2cb1901d3b1c7 (patch)
tree684aea23ac7d0e4e07afa1859d1be7c152d96c8a /src/main/java/org/zwobble
parent701b6e6f87ccffabe315b299b6ec4965e4fd7c6f (diff)
Extract context for JavaTypesGenerator
Diffstat (limited to 'src/main/java/org/zwobble')
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/generators/javatypes/JavaTypesGenerator.java54
1 files changed, 36 insertions, 18 deletions
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/javatypes/JavaTypesGenerator.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/javatypes/JavaTypesGenerator.java
index 82ef0cd..42839c9 100644
--- a/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/javatypes/JavaTypesGenerator.java
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/javatypes/JavaTypesGenerator.java
@@ -64,21 +64,7 @@ public class JavaTypesGenerator implements Generator {
private List<JavaCompilationUnit> generateNamespace(TypedNamespaceNode namespace) {
var packageParts = namespaceToJavaPackageParts(namespace.namespaceName());
- var javaClassToImplementsType = new HashMap<String, List<JavaTypeRef>>();
- for (var statement : namespace.body()) {
- if (statement instanceof TypedSumDefinitionNode sumDefinition) {
- for (var variant : sumDefinition.variants()) {
- // TODO: handle not struct types
- var variantTypeName = ((StructType) variant.type().value()).name();
- javaClassToImplementsType.putIfAbsent(
- variantTypeName,
- new ArrayList<>()
- );
- javaClassToImplementsType.get(variantTypeName)
- .add(generateTypeRef(sumDefinition.type()));
- }
- }
- }
+ var context = contextForNamespace(namespace);
var javaCompilationUnits = new ArrayList<JavaCompilationUnit>();
@@ -87,7 +73,7 @@ public class JavaTypesGenerator implements Generator {
case TypedStructDefinitionNode structDefinition -> {
var javaCompilationUnit = new JavaCompilationUnit(
packageParts,
- generateStructDefinition(structDefinition, javaClassToImplementsType)
+ generateStructDefinition(structDefinition, context)
);
javaCompilationUnits.add(javaCompilationUnit);
@@ -115,7 +101,7 @@ public class JavaTypesGenerator implements Generator {
private JavaRecordDeclaration generateStructDefinition(
TypedStructDefinitionNode structDefinition,
- HashMap<String, List<JavaTypeRef>> javaClassToImplementsType
+ Context context
) {
var components = structDefinition.fields().stream()
.map(field -> new JavaRecordComponent(
@@ -127,7 +113,7 @@ public class JavaTypesGenerator implements Generator {
return new JavaRecordDeclaration(
structDefinition.name(),
components,
- javaClassToImplementsType.getOrDefault(structDefinition.name(), List.of()),
+ context.implementsType(structDefinition.type()),
List.of(
new JavaRecordDeclaration(
"Builder",
@@ -188,4 +174,36 @@ public class JavaTypesGenerator implements Generator {
throw new UnsupportedOperationException("TODO");
};
}
+
+ private Context contextForNamespace(JavaTypesGenerator this, TypedNamespaceNode namespace) {
+ var javaClassToImplementsType = new HashMap<String, List<JavaTypeRef>>();
+ for (var statement : namespace.body()) {
+ if (statement instanceof TypedSumDefinitionNode sumDefinition) {
+ for (var variant : sumDefinition.variants()) {
+ // TODO: handle not struct types
+ var variantTypeName = ((StructType) variant.type().value()).name();
+ javaClassToImplementsType.putIfAbsent(
+ variantTypeName,
+ new ArrayList<>()
+ );
+ javaClassToImplementsType.get(variantTypeName)
+ .add(generateTypeRef(sumDefinition.type()));
+ }
+ }
+ }
+
+ return new Context(javaClassToImplementsType);
+ }
+
+ private static class Context {
+ private final HashMap<String, List<JavaTypeRef>> javaClassToImplementsType;
+
+ public Context(HashMap<String, List<JavaTypeRef>> javaClassToImplementsType) {
+ this.javaClassToImplementsType = javaClassToImplementsType;
+ }
+
+ public List<JavaTypeRef> implementsType(StructType type) {
+ return this.javaClassToImplementsType.getOrDefault(type.name(), List.of());
+ }
+ }
}