summaryrefslogtreecommitdiff
path: root/src/main
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2026-06-30 20:30:14 +0100
committerMichael Williamson <mike@zwobble.org>2026-06-30 20:30:14 +0100
commitdb671deb11d9070f3c21101b38e4cd9cad05b36e (patch)
tree5e66d071b064ac176eb079667e4ceb12dcb810ce /src/main
parentacbf42728a450afcff868a318371dec72631ade0 (diff)
De-duplicate writeWithSeparator
Diffstat (limited to 'src/main')
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/CodeWriter.java27
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriter.java33
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/RustWriter.java32
3 files changed, 34 insertions, 58 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 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 <T> void writeWithSeparator(
+ Iterable<T> iterable,
+ WriteElement<T> 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<T> {
+ 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 <T> void writeWithSeparator(
- Iterable<T> iterable,
- WriteElement<T> 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<T> {
- 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 <T> void writeWithSeparator(
- Iterable<T> iterable,
- WriteElement<T> 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<T> {
- void write(T element) throws IOException;
- }
-
- interface WriteSeparator {
- void write() throws IOException;
- }
}