From f0644a3c66265f75580fa6df28f9993e608d4c98 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Fri, 10 Jul 2026 20:51:25 +0100 Subject: Add basic for statement to Java AST --- .../compiler/output/lang/java/JavaWriterTests.java | 41 ++++++++++++++++ .../java/ast/JavaBasicForStatementMatcher.java | 57 ++++++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaBasicForStatementMatcher.java (limited to 'src/test/java/org/zwobble') 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 96dfe16..02f471c 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 @@ -836,6 +836,47 @@ public class JavaWriterTests { assertThat(string, equalTo("return 42;\nreturn 47;")); } + // === Basic for statements === + + @Test + public void basicForStatement() throws IOException { + var java = JavaBasicForStatement.arbitrary() + .withInit(JavaLocalVariableDeclaration.arbitrary() + .withName(JavaIdentifier.of("index")) + .withInitializer(JavaIntegerLiteral.arbitrary().withValue(0)) + ) + .withCondition(JavaBinaryOperation.arbitrary() + .withOperator(JavaBinaryOperator.NOT_EQUAL_TO) + .withLeft(JavaRef.arbitrary().withName(JavaIdentifier.of("index"))) + .withRight(JavaIntegerLiteral.arbitrary().withValue(4)) + ) + .withUpdate(JavaBinaryOperation.arbitrary() + .withOperator(JavaBinaryOperator.ASSIGN) + .withLeft(JavaRef.arbitrary().withName(JavaIdentifier.of("index"))) + .withRight(JavaBinaryOperation.arbitrary() + .withOperator(JavaBinaryOperator.LEFT_SHIFT) + .withLeft(JavaRef.arbitrary().withName(JavaIdentifier.of("index"))) + .withRight(JavaIntegerLiteral.arbitrary().withValue(1)) + ) + ) + .withBody(JavaBlock.arbitrary() + .addStatement(JavaExpressionStatement.arbitrary().withExpression( + JavaStaticMethodCall.arbitrary() + .withType(JavaTypeRef.topLevel(JavaPackageName.EMPTY, JavaIdentifier.of("System"))) + .withMethodName(JavaIdentifier.of("println")) + .addArg(JavaRef.arbitrary().withName(JavaIdentifier.of("index"))) + )) + ) + .build(); + + var string = write(writer -> writer.writeBlockStatement(java)); + + assertThat(string, equalTo(""" + for (var index = 0; index != 4; index = index << 1) { + System.println(index); + }""")); + } + // === Enhanced for statements === @Test diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaBasicForStatementMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaBasicForStatementMatcher.java new file mode 100644 index 0000000..6683952 --- /dev/null +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaBasicForStatementMatcher.java @@ -0,0 +1,57 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.output.lang.java.ast; + +// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBasicForStatementMatcher imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBasicForStatementMatcher imports + +public class JavaBasicForStatementMatcher implements org.zwobble.precisely.Matcher { + public static org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBasicForStatementMatcher isJavaBasicForStatement() { + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBasicForStatementMatcher(java.util.List.of()); + } + + private final java.util.List> submatchers; + + private JavaBasicForStatementMatcher(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.JavaBasicForStatement.class, this.submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBasicForStatementMatcher withInit(org.zwobble.precisely.Matcher> init) { + var submatchers = new java.util.ArrayList>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("init", org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBasicForStatement::init, init)); + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBasicForStatementMatcher(submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBasicForStatementMatcher 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.JavaBasicForStatement::condition, condition)); + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBasicForStatementMatcher(submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBasicForStatementMatcher withUpdate(org.zwobble.precisely.Matcher> update) { + var submatchers = new java.util.ArrayList>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("update", org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBasicForStatement::update, update)); + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBasicForStatementMatcher(submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBasicForStatementMatcher withBody(org.zwobble.precisely.Matcher body) { + var submatchers = new java.util.ArrayList>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("body", org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBasicForStatement::body, body)); + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBasicForStatementMatcher(submatchers); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBasicForStatementMatcher body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBasicForStatementMatcher body +} -- cgit v1.2.3