summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriter.java29
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaBlockStatement.java2
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaIfStatement.java48
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriterTests.java69
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaIfStatementMatcher.java51
5 files changed, 198 insertions, 1 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 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<java.lang.Object> {
+ 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<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIfStatement>> submatchers;
+
+ private JavaIfStatementMatcher(java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIfStatement>> 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<java.lang.Object> 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<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaExpression> condition) {
+ var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIfStatement>>(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<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBlock> ifTrue) {
+ var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIfStatement>>(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<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBlock> ifFalse) {
+ var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIfStatement>>(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
+}