summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/generators/javapreciselymatchers/JavaPreciselyMatchersGenerator.java87
1 files changed, 87 insertions, 0 deletions
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/javapreciselymatchers/JavaPreciselyMatchersGenerator.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/javapreciselymatchers/JavaPreciselyMatchersGenerator.java
index a528639..cbdcc90 100644
--- a/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/javapreciselymatchers/JavaPreciselyMatchersGenerator.java
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/javapreciselymatchers/JavaPreciselyMatchersGenerator.java
@@ -1,14 +1,24 @@
package org.zwobble.hobgoblin.compiler.output.generators.javapreciselymatchers;
+import org.zwobble.hobgoblin.compiler.ast.DocComment;
import org.zwobble.hobgoblin.compiler.ast.typed.TypedNamespaceNode;
+import org.zwobble.hobgoblin.compiler.ast.typed.TypedNativeTypeDefinitionNode;
+import org.zwobble.hobgoblin.compiler.ast.typed.TypedStructDefinitionNode;
+import org.zwobble.hobgoblin.compiler.ast.typed.TypedSumDefinitionNode;
import org.zwobble.hobgoblin.compiler.config.OutputConfig;
import org.zwobble.hobgoblin.compiler.output.generators.Generator;
+import org.zwobble.hobgoblin.compiler.output.generators.java.JavaGenerator;
+import org.zwobble.hobgoblin.compiler.output.generators.javatypes.JavaTypesGenerator;
+import org.zwobble.hobgoblin.compiler.output.lang.java.ast.*;
import org.zwobble.hobgoblin.compiler.typechecker.TypesInfo;
+import org.zwobble.hobgoblin.compiler.types.*;
import org.zwobble.json5.reader.Json5ObjectReader;
import java.io.IOException;
import java.nio.file.Path;
+import java.util.ArrayList;
import java.util.List;
+import java.util.Optional;
public class JavaPreciselyMatchersGenerator implements Generator {
public static final String NAME = "java-precisely-matchers";
@@ -33,6 +43,83 @@ public class JavaPreciselyMatchersGenerator implements Generator {
@Override
public void generate(List<TypedNamespaceNode> namespaces, TypesInfo typesInfo) throws IOException {
+ var context = new Context(typesInfo);
+ for (var namespace : namespaces) {
+ var javaCompilationUnits = generateNamespace(namespace, context);
+
+ JavaGenerator.write(this.sourceRootDirectory, javaCompilationUnits);
+ }
+ }
+
+ private List<JavaCompilationUnit> generateNamespace(TypedNamespaceNode namespace, Context context) {
+ var packageParts = namespaceToJavaPackageParts(namespace.namespaceName());
+
+ var javaCompilationUnits = new ArrayList<JavaCompilationUnit>();
+
+ for (var statement : namespace.body()) {
+ switch (statement) {
+ case TypedNativeTypeDefinitionNode nativeTypeDefinition -> {
+ }
+
+ case TypedStructDefinitionNode structDefinition -> {
+ var javaCompilationUnit = new JavaCompilationUnit(
+ packageParts,
+ generateStructDefinition(structDefinition, context)
+ );
+
+ javaCompilationUnits.add(javaCompilationUnit);
+ }
+
+ case TypedSumDefinitionNode sumDefinition -> {
+ }
+ }
+ }
+
+ return javaCompilationUnits;
+ }
+
+ private JavaTypeDeclaration generateStructDefinition(
+ TypedStructDefinitionNode structDefinition,
+ Context context
+ ) {
+ var matcherTypeName = structDefinition.name() + "Matcher";
+ var structType = structDefinition.type();
+
+ var javaTypeRef = JavaTypeRef.topLevel(
+ namespaceToJavaPackageParts(structType.namespaceName()),
+ matcherTypeName
+ );
+
+ return new JavaClassDeclaration(
+ matcherTypeName,
+ List.of(),
+ JavaCustomArea.type(javaTypeRef, "body"),
+ DocComment.EMPTY
+ );
+ }
+
+ private List<String> namespaceToJavaPackageParts(NamespaceName namespaceName) {
+ return JavaGenerator.namespaceToJavaPackageParts(this.packageName, namespaceName);
+ }
+
+ private static class Context {
+ private final TypesInfo typesInfo;
+
+ public Context(TypesInfo typesInfo) {
+ this.typesInfo = typesInfo;
+ }
+
+ public List<Field> fieldsOf(StructType type) {
+ return this.typesInfo.fieldsOf(type);
+ }
+
+ public List<SumType> variantOf(StructType type) {
+ return this.typesInfo.variantOf(type);
+ }
+
+ public List<Type> variants(SumType type) {
+ return this.typesInfo.variants(type);
+ }
}
}