From db671deb11d9070f3c21101b38e4cd9cad05b36e Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Tue, 30 Jun 2026 20:30:14 +0100 Subject: De-duplicate writeWithSeparator --- .../hobgoblin/compiler/output/CodeWriter.java | 27 ++++++++++++++++++ .../compiler/output/lang/java/JavaWriter.java | 33 +++------------------- .../compiler/output/lang/rust/RustWriter.java | 32 ++------------------- 3 files changed, 34 insertions(+), 58 deletions(-) (limited to 'src/main/java/org') 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 fe2c3b9..a0b9005 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/CodeWriter.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/CodeWriter.java @@ -198,4 +198,31 @@ public class CodeWriter implements AutoCloseable { public void close() throws IOException { this.writer.close(); } + + public static void writeWithSeparator( + Iterable iterable, + WriteElement writeElement, + WriteSeparator writeSeparator + ) throws IOException { + var isFirst = true; + for (var element : iterable) { + if (!isFirst) { + writeSeparator.write(); + } + + writeElement.write(element); + + if (isFirst) { + isFirst = false; + } + } + } + + public interface WriteElement { + void write(T element) throws IOException; + } + + public interface WriteSeparator { + void write() throws IOException; + } } 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 3c6900d..b1bf96e 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 @@ -9,6 +9,8 @@ import java.io.StringWriter; import java.nio.file.Path; import java.util.List; +import static org.zwobble.hobgoblin.compiler.output.CodeWriter.writeWithSeparator; + public class JavaWriter implements AutoCloseable { private static final String LINE_COMMENT_START = "//"; @@ -351,7 +353,7 @@ public class JavaWriter implements AutoCloseable { this.writer.write("."); this.writeIdentifier(call.methodName()); this.writer.write("("); - this.writeWithSeparator( + writeWithSeparator( call.args(), this::writeExpression, () -> this.writer.write(", ") @@ -491,7 +493,7 @@ public class JavaWriter implements AutoCloseable { this.writer.write("."); this.writeIdentifier(call.methodName()); this.writer.write("("); - this.writeWithSeparator( + writeWithSeparator( call.args(), this::writeExpression, () -> this.writer.write(", ") @@ -552,33 +554,6 @@ public class JavaWriter implements AutoCloseable { }); } - private static void writeWithSeparator( - Iterable iterable, - WriteElement writeElement, - WriteSeparator writeSeparator - ) throws IOException { - var isFirst = true; - for (var element : iterable) { - if (!isFirst) { - writeSeparator.write(); - } - - writeElement.write(element); - - if (isFirst) { - isFirst = false; - } - } - } - - interface WriteElement { - void write(T element) throws IOException; - } - - interface WriteSeparator { - void write() throws IOException; - } - interface WriteString { void write(String string) throws IOException; } diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/RustWriter.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/RustWriter.java index 1ec1ca0..25335ee 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/RustWriter.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/RustWriter.java @@ -6,6 +6,8 @@ import org.zwobble.hobgoblin.compiler.output.lang.rust.ast.*; import java.io.IOException; import java.nio.file.Path; +import static org.zwobble.hobgoblin.compiler.output.CodeWriter.writeWithSeparator; + public class RustWriter implements AutoCloseable { private static final String LINE_COMMENT_START = "//"; @@ -87,7 +89,7 @@ public class RustWriter implements AutoCloseable { void writeType(RustType type) throws IOException { switch (type) { case RustTypePath typePath -> { - this.writeWithSeparator( + writeWithSeparator( typePath.segments(), this::writeTypePathSegment, () -> this.writer.write("::") @@ -107,32 +109,4 @@ public class RustWriter implements AutoCloseable { } } } - - // TODO: de-dupe with Java writer - private static void writeWithSeparator( - Iterable iterable, - WriteElement writeElement, - WriteSeparator writeSeparator - ) throws IOException { - var isFirst = true; - for (var element : iterable) { - if (!isFirst) { - writeSeparator.write(); - } - - writeElement.write(element); - - if (isFirst) { - isFirst = false; - } - } - } - - interface WriteElement { - void write(T element) throws IOException; - } - - interface WriteSeparator { - void write() throws IOException; - } } -- cgit v1.2.3