summaryrefslogtreecommitdiff
path: root/src/main/java/org
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/org')
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedConstructedTypeNode.java4
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedStructFieldDefinitionNode.java4
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedSumVariantDefinitionNode.java4
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/generators/javatypes/JavaTypesGenerator.java35
4 files changed, 44 insertions, 3 deletions
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedConstructedTypeNode.java b/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedConstructedTypeNode.java
index 42f6a1f..fd332d7 100644
--- a/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedConstructedTypeNode.java
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedConstructedTypeNode.java
@@ -14,6 +14,10 @@ public record UntypedConstructedTypeNode(org.zwobble.hobgoblin.compiler.ast.unty
return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedConstructedTypeNode.Builder(receiver, args, source);
}
+ public org.zwobble.hobgoblin.compiler.ast.untyped.UntypedConstructedTypeNode.Builder withReceiver(org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTypeLevelExpressionNode.Builder receiver) {
+ return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedConstructedTypeNode.Builder(receiver.build(), args, source);
+ }
+
public org.zwobble.hobgoblin.compiler.ast.untyped.UntypedConstructedTypeNode.Builder withArgs(java.util.List<org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTypeLevelExpressionNode> args) {
return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedConstructedTypeNode.Builder(receiver, args, source);
}
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedStructFieldDefinitionNode.java b/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedStructFieldDefinitionNode.java
index 77af253..f221bec 100644
--- a/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedStructFieldDefinitionNode.java
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedStructFieldDefinitionNode.java
@@ -18,6 +18,10 @@ public record UntypedStructFieldDefinitionNode(String name, org.zwobble.hobgobli
return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedStructFieldDefinitionNode.Builder(name, type, source);
}
+ public org.zwobble.hobgoblin.compiler.ast.untyped.UntypedStructFieldDefinitionNode.Builder withType(org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTypeLevelExpressionNode.Builder type) {
+ return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedStructFieldDefinitionNode.Builder(name, type.build(), source);
+ }
+
public org.zwobble.hobgoblin.compiler.ast.untyped.UntypedStructFieldDefinitionNode.Builder withSource(org.zwobble.hobgoblin.compiler.sources.Source source) {
return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedStructFieldDefinitionNode.Builder(name, type, source);
}
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedSumVariantDefinitionNode.java b/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedSumVariantDefinitionNode.java
index ba7012c..f4edd89 100644
--- a/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedSumVariantDefinitionNode.java
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedSumVariantDefinitionNode.java
@@ -14,6 +14,10 @@ public record UntypedSumVariantDefinitionNode(org.zwobble.hobgoblin.compiler.ast
return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedSumVariantDefinitionNode.Builder(type, source);
}
+ public org.zwobble.hobgoblin.compiler.ast.untyped.UntypedSumVariantDefinitionNode.Builder withType(org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTypeLevelExpressionNode.Builder type) {
+ return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedSumVariantDefinitionNode.Builder(type.build(), source);
+ }
+
public org.zwobble.hobgoblin.compiler.ast.untyped.UntypedSumVariantDefinitionNode.Builder withSource(org.zwobble.hobgoblin.compiler.sources.Source source) {
return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedSumVariantDefinitionNode.Builder(type, source);
}
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 9202975..12c1451 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
@@ -244,13 +244,13 @@ public class JavaTypesGenerator implements Generator {
))
));
- for (var component : components) {
+ for (var field : structDefinition.fields()) {
body.add(new JavaMethodDeclaration(
- "with" + lowerCamelCaseToUpperCamelCase(component.name()),
+ "with" + lowerCamelCaseToUpperCamelCase(field.name()),
JavaMethodKind.INSTANCE,
builderJavaTypeRef,
List.of(
- new JavaParam(component.type(), component.name())
+ new JavaParam(generateTypeRef(field.type(), context), field.name())
),
new JavaBlock(List.of(
new JavaReturn(new JavaNewExpression(
@@ -259,6 +259,35 @@ public class JavaTypesGenerator implements Generator {
))
))
));
+
+ Optional<JavaTypeRef> fieldBuilderTypeRef = switch (field.type().value()) {
+ case StructType structType -> Optional.of(generateBuilderTypeRef(structType));
+ case SumType sumType -> Optional.of(generateBuilderTypeRef(sumType));
+ default -> Optional.empty();
+ };
+
+ if (fieldBuilderTypeRef.isPresent()) {
+ body.add(new JavaMethodDeclaration(
+ "with" + lowerCamelCaseToUpperCamelCase(field.name()),
+ JavaMethodKind.INSTANCE,
+ builderJavaTypeRef,
+ List.of(
+ new JavaParam(fieldBuilderTypeRef.get(), field.name())
+ ),
+ new JavaBlock(List.of(
+ new JavaReturn(new JavaNewExpression(
+ builderJavaTypeRef,
+ components.stream().<JavaExpression>map(argComponent -> {
+ if (argComponent.name().equals(field.name())) {
+ return new JavaMethodCall(new JavaRef(field.name()), "build", List.of());
+ } else {
+ return new JavaRef(argComponent.name());
+ }
+ }).toList()
+ ))
+ ))
+ ));
+ }
}
return new JavaRecordDeclaration(