diff options
| author | Michael Williamson <mike@zwobble.org> | 2026-05-03 11:42:44 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2026-05-03 11:42:44 +0100 |
| commit | 8b71116229a45965ffae361b6f765ff67c913119 (patch) | |
| tree | 7fab2f74128a39735947409992ddbaffa28f57f8 /src/main | |
| parent | dda4d7559276300877655c1e8c783ae8ef0cc793 (diff) | |
Add methods to Java classes
Diffstat (limited to 'src/main')
5 files changed, 56 insertions, 3 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"); } |
