summaryrefslogtreecommitdiff
path: root/src/main/java/org/zwobble
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2026-07-10 20:44:19 +0100
committerMichael Williamson <mike@zwobble.org>2026-07-10 20:44:19 +0100
commit3fef371e57cf6b943457f2896712193445f60c64 (patch)
treee5ebc5c252a2b2fbc7fd91d8b8b81d8bd5a59b77 /src/main/java/org/zwobble
parent888892b8c17179e53cdc997abfa785333ece26cd (diff)
Extract writeLocalVariableType()
Diffstat (limited to 'src/main/java/org/zwobble')
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriter.java21
1 files changed, 11 insertions, 10 deletions
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 4a21d88..52683b5 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
@@ -8,6 +8,7 @@ import java.io.IOException;
import java.io.StringWriter;
import java.nio.file.Path;
import java.util.List;
+import java.util.Optional;
import static org.zwobble.hobgoblin.compiler.output.CodeWriter.writeWithSeparator;
@@ -416,11 +417,7 @@ public class JavaWriter implements AutoCloseable {
private void writeEnhancedForStatement(JavaEnhancedForStatement enhancedForStatement) throws IOException {
this.writer.write("for (");
- if (enhancedForStatement.elementType().isPresent()) {
- this.writeTypeRef(enhancedForStatement.elementType().get());
- } else {
- this.writer.write("var");
- }
+ writeLocalVariableType(enhancedForStatement.elementType());
this.writer.write(" ");
this.writeIdentifier(enhancedForStatement.elementName());
@@ -445,11 +442,7 @@ public class JavaWriter implements AutoCloseable {
}
private void writeLocalVariableDeclaration(JavaLocalVariableDeclaration localVariableDeclaration) throws IOException {
- if (localVariableDeclaration.type().isPresent()) {
- this.writeTypeRef(localVariableDeclaration.type().get());
- } else {
- this.writer.write("var");
- }
+ writeLocalVariableType(localVariableDeclaration.type());
this.writer.write(" ");
this.writeIdentifier(localVariableDeclaration.name());
@@ -458,6 +451,14 @@ public class JavaWriter implements AutoCloseable {
this.writer.write(";");
}
+ private void writeLocalVariableType(Optional<JavaTypeRef> type) throws IOException {
+ if (type.isPresent()) {
+ this.writeTypeRef(type.get());
+ } else {
+ this.writer.write("var");
+ }
+ }
+
private void writeReturnStatement(JavaReturn returnStatement) throws IOException {
this.writer.write("return ");
writeTopLevelExpression(returnStatement.value());