From f19321fcc099fdcb22c30bcca5bdad04c7396876 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 25 Jul 2026 17:32:36 +0100 Subject: Add block as Java statement --- .../compiler/output/lang/java/JavaWriter.java | 90 +++++++--------------- .../compiler/output/lang/java/ast/JavaBlock.java | 4 +- .../output/lang/java/ast/JavaBlockStatement.java | 2 +- 3 files changed, 32 insertions(+), 64 deletions(-) (limited to 'src/main/java/org/zwobble') 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 173cfc8..0edaf5d 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 @@ -257,17 +257,8 @@ public class JavaWriter implements AutoCloseable { () -> writer.write(", ") ); - this.writer.write(")"); - - this.writer.write(" {"); - this.writer.indent(); - if (!constructor.body().isEmpty()) { - this.writer.newLine(); - writeBlock(constructor.body()); - } - this.writer.dedent(); - this.writer.newLine(); - this.writer.write("}"); + this.writer.write(") "); + writeBlock(constructor.body()); } private void writeFieldDeclaration(JavaFieldDeclaration fieldDeclaration) throws IOException { @@ -335,15 +326,8 @@ public class JavaWriter implements AutoCloseable { if (method.body().isEmpty()) { this.writer.write(";"); } else { - this.writer.write(" {"); - this.writer.indent(); - if (!method.body().get().isEmpty()) { - this.writer.newLine(); - writeBlock(method.body().get()); - } - this.writer.dedent(); - this.writer.newLine(); - this.writer.write("}"); + this.writer.write(" "); + writeBlock(method.body().get()); } } @@ -406,11 +390,21 @@ public class JavaWriter implements AutoCloseable { // == Statements == void writeBlock(JavaBlock block) throws IOException { - writeWithSeparator( - block.statements(), - this::writeBlockStatement, - () -> this.writer.newLine() - ); + this.writer.write("{"); + if (!block.isEmpty()) { + this.writer.indent(); + this.writer.newLine(); + + writeWithSeparator( + block.statements(), + this::writeBlockStatement, + () -> this.writer.newLine() + ); + + this.writer.dedent(); + } + this.writer.newLine(); + this.writer.write("}"); } void writeBlockStatement(JavaBlockStatement statement) throws IOException { @@ -419,6 +413,10 @@ public class JavaWriter implements AutoCloseable { writeBasicForStatement(basicForStatement); } + case JavaBlock block -> { + writeBlock(block); + } + case JavaEnhancedForStatement enhancedForStatement -> { writeEnhancedForStatement(enhancedForStatement); } @@ -468,15 +466,9 @@ public class JavaWriter implements AutoCloseable { this.writeTopLevelExpression(basicForStatement.update().get()); } - this.writer.write(") {"); - this.writer.indent(); - this.writer.newLine(); + this.writer.write(") "); this.writeBlock(basicForStatement.body()); - - this.writer.dedent(); - this.writer.newLine(); - this.writer.write("}"); } private void writeEnhancedForStatement(JavaEnhancedForStatement enhancedForStatement) throws IOException { @@ -490,15 +482,9 @@ public class JavaWriter implements AutoCloseable { this.writeTopLevelExpression(enhancedForStatement.elements()); - this.writer.write(") {"); - this.writer.indent(); - this.writer.newLine(); + this.writer.write(") "); this.writeBlock(enhancedForStatement.body()); - - this.writer.dedent(); - this.writer.newLine(); - this.writer.write("}"); } private void writeExpressionStatement(JavaExpressionStatement expressionStatement) throws IOException { @@ -509,25 +495,12 @@ public class JavaWriter implements AutoCloseable { private void writeIfStatement(JavaIfStatement ifStatement) throws IOException { this.writer.write("if ("); this.writeTopLevelExpression(ifStatement.condition()); - this.writer.write(") {"); - this.writer.indent(); - if (!ifStatement.ifTrue().isEmpty()) { - - this.writer.newLine(); - this.writeBlock(ifStatement.ifTrue()); - } - this.writer.dedent(); - this.writer.newLine(); - this.writer.write("}"); + this.writer.write(") "); + this.writeBlock(ifStatement.ifTrue()); if (!ifStatement.ifFalse().isEmpty()) { - this.writer.write(" else {"); - this.writer.indent(); - this.writer.newLine(); + this.writer.write(" else "); this.writeBlock(ifStatement.ifFalse()); - this.writer.dedent(); - this.writer.newLine(); - this.writer.write("}"); } } @@ -581,13 +554,8 @@ public class JavaWriter implements AutoCloseable { this.writeIdentifier(casePattern.variableName()); } } - this.writer.write(" -> {"); - this.writer.indent(); - this.writer.newLine(); + this.writer.write(" -> "); this.writeBlock(rule.body()); - this.writer.dedent(); - this.writer.newLine(); - this.writer.write("}"); } this.writer.dedent(); this.writer.newLine(); diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaBlock.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaBlock.java index 2d8b899..5ee3f53 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaBlock.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaBlock.java @@ -5,12 +5,12 @@ package org.zwobble.hobgoblin.compiler.output.lang.java.ast; // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBlock imports // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBlock imports -public record JavaBlock(java.util.List statements) { +public record JavaBlock(java.util.List statements) implements org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBlockStatement { public static org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBlock.Builder arbitrary() { return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBlock.Builder(java.util.List.of()); } - public record Builder(java.util.List statements) { + public record Builder(java.util.List statements) implements org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBlockStatement.Builder { public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBlock build() { return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBlock(statements); } diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaBlockStatement.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaBlockStatement.java index 50d6970..a04796a 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaBlockStatement.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaBlockStatement.java @@ -5,7 +5,7 @@ package org.zwobble.hobgoblin.compiler.output.lang.java.ast; // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBlockStatement imports // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBlockStatement imports -public sealed interface JavaBlockStatement permits org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBasicForStatement, org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnhancedForStatement, org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaExpressionStatement, org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIfStatement, org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaLocalVariableDeclaration, org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaReturn, org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchStatement, org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaThrowStatement { +public sealed interface JavaBlockStatement permits org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBasicForStatement, org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBlock, org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnhancedForStatement, org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaExpressionStatement, org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIfStatement, org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaLocalVariableDeclaration, org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaReturn, org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchStatement, org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaThrowStatement { public interface Builder { public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBlockStatement build(); -- cgit v1.2.3