summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2026-05-03 11:42:44 +0100
committerMichael Williamson <mike@zwobble.org>2026-05-03 11:42:44 +0100
commit8b71116229a45965ffae361b6f765ff67c913119 (patch)
tree7fab2f74128a39735947409992ddbaffa28f57f8 /src
parentdda4d7559276300877655c1e8c783ae8ef0cc793 (diff)
Add methods to Java classes
Diffstat (limited to 'src')
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/CodeWriter.java20
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriter.java24
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaClassDeclaration.java7
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaMethodDeclaration.java7
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaTypeRef.java1
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriterTests.java42
6 files changed, 91 insertions, 10 deletions
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/CodeWriter.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/CodeWriter.java
index 6a5f784..3e08c85 100644
--- a/src/main/java/org/zwobble/hobgoblin/compiler/output/CodeWriter.java
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/CodeWriter.java
@@ -14,17 +14,37 @@ public class CodeWriter implements AutoCloseable {
}
private final Writer writer;
+ private int indent;
+ private boolean pendingIndent;
public CodeWriter(Writer writer) {
this.writer = writer;
+ this.indent = 0;
+ this.pendingIndent = false;
}
public void write(String value) throws IOException {
+ if (this.pendingIndent) {
+ for (var i = 0; i < this.indent; i++) {
+ this.writer.write(" ");
+ }
+ this.pendingIndent = false;
+ }
+
this.writer.write(value);
}
public void newLine() throws IOException {
this.writer.write("\n");
+ this.pendingIndent = true;
+ }
+
+ public void indent() {
+ this.indent += 1;
+ }
+
+ public void dedent() {
+ this.indent -= 1;
}
@Override
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriter.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriter.java
index 0b56fa6..a213113 100644
--- a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriter.java
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriter.java
@@ -25,6 +25,7 @@ public class JavaWriter implements AutoCloseable {
this.writer.newLine();
writeTypeDeclaration(compilationUnit.typeDeclaration());
+ this.writer.newLine();
}
void writeTypeDeclaration(JavaTypeDeclaration typeDeclaration) throws IOException {
@@ -47,9 +48,30 @@ public class JavaWriter implements AutoCloseable {
this.writer.write("public class ");
this.writer.write(classDeclaration.name());
this.writer.write(" {");
+ this.writer.indent();
+
+ writeWithSeparator(
+ classDeclaration.methods(),
+ method -> {
+ writer.newLine();
+ writeMethodDeclaration(method);
+ },
+ () -> writer.newLine()
+ );
+
+ this.writer.dedent();
this.writer.newLine();
this.writer.write("}");
+ }
+
+ private void writeMethodDeclaration(JavaMethodDeclaration method) throws IOException {
+ this.writer.write("public ");
+ writeTypeRef(method.returnType());
+ this.writer.write(" ");
+ this.writer.write(method.name());
+ this.writer.write("() {");
this.writer.newLine();
+ this.writer.write("}");
}
private void writeInterfaceDeclaration(JavaInterfaceDeclaration interfaceDeclaration) throws IOException {
@@ -70,7 +92,6 @@ public class JavaWriter implements AutoCloseable {
this.writer.write("{");
this.writer.newLine();
this.writer.write("}");
- this.writer.newLine();
}
private void writeRecordDeclaration(JavaRecordDeclaration recordDeclaration) throws IOException {
@@ -104,7 +125,6 @@ public class JavaWriter implements AutoCloseable {
this.writer.newLine();
this.writer.write("}");
- this.writer.newLine();
}
private void writeTypeRef(JavaTypeRef type) throws IOException {
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaClassDeclaration.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaClassDeclaration.java
index 6ca6a26..b44d9b0 100644
--- a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaClassDeclaration.java
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaClassDeclaration.java
@@ -1,4 +1,9 @@
package org.zwobble.hobgoblin.compiler.output.lang.java.ast;
-public record JavaClassDeclaration(String name) implements JavaTypeDeclaration {
+import java.util.List;
+
+public record JavaClassDeclaration(
+ String name,
+ List<JavaMethodDeclaration> methods
+) implements JavaTypeDeclaration {
}
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaMethodDeclaration.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaMethodDeclaration.java
new file mode 100644
index 0000000..5fe313e
--- /dev/null
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaMethodDeclaration.java
@@ -0,0 +1,7 @@
+package org.zwobble.hobgoblin.compiler.output.lang.java.ast;
+
+public record JavaMethodDeclaration(
+ String name,
+ JavaTypeRef returnType
+) {
+}
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaTypeRef.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaTypeRef.java
index fafa7af..9ea840d 100644
--- a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaTypeRef.java
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaTypeRef.java
@@ -3,4 +3,5 @@ package org.zwobble.hobgoblin.compiler.output.lang.java.ast;
import java.util.List;
public record JavaTypeRef(List<String> packageName, String name) {
+ public static final JavaTypeRef VOID = new JavaTypeRef(List.of(), "void");
}
diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriterTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriterTests.java
index 13c2c00..da0f26f 100644
--- a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriterTests.java
+++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriterTests.java
@@ -4,6 +4,7 @@ import org.junit.jupiter.api.Test;
import org.zwobble.hobgoblin.compiler.output.CodeWriter;
import org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaClassDeclaration;
import org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaInterfaceDeclaration;
+import org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaMethodDeclaration;
import org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaTypeRef;
import java.io.IOException;
@@ -17,15 +18,44 @@ public class JavaWriterTests {
@Test
public void emptyClassDeclaration() throws IOException {
var java = new JavaClassDeclaration(
- "Rectangle"
+ "Rectangle",
+ List.of()
);
var string = write(writer -> writer.writeTypeDeclaration(java));
assertThat(string, equalTo("""
public class Rectangle {
- }
- """
+ }"""
+ ));
+ }
+
+ @Test
+ public void classDeclarationWithMethods() throws IOException {
+ var java = new JavaClassDeclaration(
+ "Rectangle",
+ List.of(
+ new JavaMethodDeclaration(
+ "a",
+ JavaTypeRef.VOID
+ ),
+ new JavaMethodDeclaration(
+ "b",
+ JavaTypeRef.VOID
+ )
+ )
+ );
+
+ var string = write(writer -> writer.writeTypeDeclaration(java));
+
+ assertThat(string, equalTo("""
+ public class Rectangle {
+ public void a() {
+ }
+
+ public void b() {
+ }
+ }"""
));
}
@@ -40,8 +70,7 @@ public class JavaWriterTests {
assertThat(string, equalTo("""
public sealed interface Shape {
- }
- """
+ }"""
));
}
@@ -59,8 +88,7 @@ public class JavaWriterTests {
assertThat(string, equalTo("""
public sealed interface Shape permits abc.def.One, abc.def.Two {
- }
- """
+ }"""
));
}