From ea5daa6b523813b5d244a8b8aa72943e91e7d3f2 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 11 Jul 2026 15:36:00 +0100 Subject: Add if statements to Java AST --- hobgoblin/src/output/lang/java/ast.hob | 7 +++ .../compiler/output/lang/java/JavaWriter.java | 29 +++++++++ .../output/lang/java/ast/JavaBlockStatement.java | 2 +- .../output/lang/java/ast/JavaIfStatement.java | 48 +++++++++++++++ .../compiler/output/lang/java/JavaWriterTests.java | 69 ++++++++++++++++++++++ .../lang/java/ast/JavaIfStatementMatcher.java | 51 ++++++++++++++++ 6 files changed, 205 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaIfStatement.java create mode 100644 src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaIfStatementMatcher.java diff --git a/hobgoblin/src/output/lang/java/ast.hob b/hobgoblin/src/output/lang/java/ast.hob index 74611e6..dd5d030 100644 --- a/hobgoblin/src/output/lang/java/ast.hob +++ b/hobgoblin/src/output/lang/java/ast.hob @@ -129,6 +129,7 @@ sum JavaBlockStatement { variant JavaBasicForStatement; variant JavaEnhancedForStatement; variant JavaExpressionStatement; + variant JavaIfStatement; variant JavaLocalVariableDeclaration; variant JavaReturn; } @@ -151,6 +152,12 @@ struct JavaExpressionStatement { field expression: JavaExpression; } +struct JavaIfStatement { + field condition: JavaExpression; + field ifTrue: JavaBlock; + field ifFalse: JavaBlock; +} + struct JavaLocalVariableDeclaration { field type: Option[JavaTypeRef]; field name: JavaIdentifier; 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 aa1f3c1..48b39f5 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 @@ -408,6 +408,10 @@ public class JavaWriter implements AutoCloseable { writeExpressionStatement(expressionStatement); } + case JavaIfStatement ifStatement -> { + writeIfStatement(ifStatement); + } + case JavaLocalVariableDeclaration localVariableDeclaration -> { writeLocalVariableDeclaration(localVariableDeclaration); } @@ -475,6 +479,31 @@ public class JavaWriter implements AutoCloseable { this.writer.write(";"); } + 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("}"); + + if (!ifStatement.ifFalse().isEmpty()) { + this.writer.write(" else {"); + this.writer.indent(); + this.writer.newLine(); + this.writeBlock(ifStatement.ifFalse()); + this.writer.dedent(); + this.writer.newLine(); + this.writer.write("}"); + } + } + private void writeLocalVariableDeclaration(JavaLocalVariableDeclaration localVariableDeclaration) throws IOException { writeLocalVariableType(localVariableDeclaration.type()); 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 707134c..dd5add4 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.JavaLocalVariableDeclaration, org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaReturn { +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 { public interface Builder { public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBlockStatement build(); diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaIfStatement.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaIfStatement.java new file mode 100644 index 0000000..bb22ff5 --- /dev/null +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaIfStatement.java @@ -0,0 +1,48 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.output.lang.java.ast; + +// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIfStatement imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIfStatement imports + +public record JavaIfStatement(org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaExpression condition, org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBlock ifTrue, org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBlock ifFalse) implements org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBlockStatement { + public static org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIfStatement.Builder arbitrary() { + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIfStatement.Builder(org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBooleanLiteral.arbitrary().build(), org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBlock.arbitrary().build(), org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBlock.arbitrary().build()); + } + + public record Builder(org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaExpression condition, org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBlock ifTrue, org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBlock ifFalse) implements org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBlockStatement.Builder { + public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIfStatement build() { + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIfStatement(condition, ifTrue, ifFalse); + } + + public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIfStatement.Builder withCondition(org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaExpression condition) { + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIfStatement.Builder(condition, ifTrue, ifFalse); + } + + public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIfStatement.Builder withCondition(org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaExpression.Builder condition) { + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIfStatement.Builder(condition.build(), ifTrue, ifFalse); + } + + public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIfStatement.Builder withIfTrue(org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBlock ifTrue) { + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIfStatement.Builder(condition, ifTrue, ifFalse); + } + + public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIfStatement.Builder withIfTrue(org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBlock.Builder ifTrue) { + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIfStatement.Builder(condition, ifTrue.build(), ifFalse); + } + + public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIfStatement.Builder withIfFalse(org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBlock ifFalse) { + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIfStatement.Builder(condition, ifTrue, ifFalse); + } + + public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIfStatement.Builder withIfFalse(org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBlock.Builder ifFalse) { + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIfStatement.Builder(condition, ifTrue, ifFalse.build()); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIfStatement.Builder body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIfStatement.Builder body + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIfStatement body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIfStatement body +} diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriterTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriterTests.java index b28f590..508321e 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriterTests.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriterTests.java @@ -938,6 +938,75 @@ public class JavaWriterTests { assertThat(string, equalTo("42;")); } + // === If statements === + + @Test + public void ifStatementWithNonEmptyTrueAndFalseBodies() throws IOException { + var java = JavaIfStatement.arbitrary() + .withCondition(JavaRef.arbitrary().withName(JavaIdentifier.of("a"))) + .withIfTrue(JavaBlock.arbitrary() + .addStatement(JavaExpressionStatement.arbitrary() + .withExpression(JavaIntegerLiteral.arbitrary().withValue(0)) + ) + ) + .withIfFalse(JavaBlock.arbitrary() + .addStatement(JavaExpressionStatement.arbitrary() + .withExpression(JavaIntegerLiteral.arbitrary().withValue(1)) + ) + ) + .build(); + + var string = write(writer -> writer.writeBlockStatement(java)); + + assertThat(string, equalTo(""" + if (a) { + 0; + } else { + 1; + }""")); + } + + @Test + public void ifStatementWithEmptyTrueBody() throws IOException { + var java = JavaIfStatement.arbitrary() + .withCondition(JavaRef.arbitrary().withName(JavaIdentifier.of("a"))) + .withIfTrue(JavaBlock.arbitrary()) + .withIfFalse(JavaBlock.arbitrary() + .addStatement(JavaExpressionStatement.arbitrary() + .withExpression(JavaIntegerLiteral.arbitrary().withValue(1)) + ) + ) + .build(); + + var string = write(writer -> writer.writeBlockStatement(java)); + + assertThat(string, equalTo(""" + if (a) { + } else { + 1; + }""")); + } + + @Test + public void ifStatementWithEmptyFalseBody() throws IOException { + var java = JavaIfStatement.arbitrary() + .withCondition(JavaRef.arbitrary().withName(JavaIdentifier.of("a"))) + .withIfTrue(JavaBlock.arbitrary() + .addStatement(JavaExpressionStatement.arbitrary() + .withExpression(JavaIntegerLiteral.arbitrary().withValue(0)) + ) + ) + .withIfFalse(JavaBlock.arbitrary()) + .build(); + + var string = write(writer -> writer.writeBlockStatement(java)); + + assertThat(string, equalTo(""" + if (a) { + 0; + }""")); + } + // === Local variable declarations === @Test diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaIfStatementMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaIfStatementMatcher.java new file mode 100644 index 0000000..25d10a6 --- /dev/null +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaIfStatementMatcher.java @@ -0,0 +1,51 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.output.lang.java.ast; + +// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIfStatementMatcher imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIfStatementMatcher imports + +public class JavaIfStatementMatcher implements org.zwobble.precisely.Matcher { + public static org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIfStatementMatcher isJavaIfStatement() { + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIfStatementMatcher(java.util.List.of()); + } + + private final java.util.List> submatchers; + + private JavaIfStatementMatcher(java.util.List> submatchers) { + this.submatchers = submatchers; + } + + public org.zwobble.precisely.MatchResult match(java.lang.Object actual) { + return this.toMatcher().match(actual); + } + + public org.zwobble.precisely.TextTree describe() { + return this.toMatcher().describe(); + } + + private org.zwobble.precisely.Matcher toMatcher() { + return org.zwobble.precisely.Matchers.instanceOf(org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIfStatement.class, this.submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIfStatementMatcher withCondition(org.zwobble.precisely.Matcher condition) { + var submatchers = new java.util.ArrayList>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("condition", org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIfStatement::condition, condition)); + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIfStatementMatcher(submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIfStatementMatcher withIfTrue(org.zwobble.precisely.Matcher ifTrue) { + var submatchers = new java.util.ArrayList>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("ifTrue", org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIfStatement::ifTrue, ifTrue)); + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIfStatementMatcher(submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIfStatementMatcher withIfFalse(org.zwobble.precisely.Matcher ifFalse) { + var submatchers = new java.util.ArrayList>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("ifFalse", org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIfStatement::ifFalse, ifFalse)); + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIfStatementMatcher(submatchers); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIfStatementMatcher body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIfStatementMatcher body +} -- cgit v1.2.3