From 3fef371e57cf6b943457f2896712193445f60c64 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Fri, 10 Jul 2026 20:44:19 +0100 Subject: Extract writeLocalVariableType() --- .../compiler/output/lang/java/JavaWriter.java | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) (limited to 'src/main') 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 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()); -- cgit v1.2.3