From eb4ce8b7ebceac4bbf68b779b9f6d6f4439ddbba Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 11 Jul 2026 17:11:54 +0100 Subject: Add switch statement to Java AST --- hobgoblin/src/output/lang/java/ast.hob | 12 +++++ .../compiler/output/lang/java/JavaWriter.java | 28 ++++++++++++ .../output/lang/java/ast/JavaBlockStatement.java | 2 +- .../output/lang/java/ast/JavaSwitchRule.java | 44 +++++++++++++++++++ .../output/lang/java/ast/JavaSwitchStatement.java | 48 ++++++++++++++++++++ .../compiler/output/lang/java/JavaWriterTests.java | 37 ++++++++++++++++ .../lang/java/ast/JavaSwitchRuleMatcher.java | 51 ++++++++++++++++++++++ .../lang/java/ast/JavaSwitchStatementMatcher.java | 45 +++++++++++++++++++ 8 files changed, 266 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaSwitchRule.java create mode 100644 src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaSwitchStatement.java create mode 100644 src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaSwitchRuleMatcher.java create mode 100644 src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaSwitchStatementMatcher.java diff --git a/hobgoblin/src/output/lang/java/ast.hob b/hobgoblin/src/output/lang/java/ast.hob index 16000ed..4553236 100644 --- a/hobgoblin/src/output/lang/java/ast.hob +++ b/hobgoblin/src/output/lang/java/ast.hob @@ -132,6 +132,7 @@ sum JavaBlockStatement { variant JavaIfStatement; variant JavaLocalVariableDeclaration; variant JavaReturn; + variant JavaSwitchStatement; } struct JavaBasicForStatement { @@ -168,6 +169,17 @@ struct JavaReturn { field value: JavaExpression; } +struct JavaSwitchStatement { + field selectorExpression: JavaExpression; + field rules: List[JavaSwitchRule]; +} + +struct JavaSwitchRule { + field type: JavaTypeRef; + field variableName: JavaIdentifier; + field body: JavaBlock; +} + // == Expressions == sum JavaExpression { 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 0b24366..893e30b 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 @@ -419,6 +419,10 @@ public class JavaWriter implements AutoCloseable { case JavaReturn returnStatement -> { writeReturnStatement(returnStatement); } + + case JavaSwitchStatement switchStatement -> { + writeSwitchStatement(switchStatement); + } } } @@ -530,6 +534,30 @@ public class JavaWriter implements AutoCloseable { this.writer.write(";"); } + private void writeSwitchStatement(JavaSwitchStatement switchStatement) throws IOException { + this.writer.write("switch ("); + this.writeTopLevelExpression(switchStatement.selectorExpression()); + this.writer.write(") {"); + this.writer.indent(); + for (var rule : switchStatement.rules()) { + this.writer.newLine(); + this.writer.write("case "); + this.writeTypeRef(rule.type()); + this.writer.write(" "); + this.writeIdentifier(rule.variableName()); + this.writer.write(" -> {"); + this.writer.indent(); + this.writer.newLine(); + this.writeBlock(rule.body()); + this.writer.dedent(); + this.writer.newLine(); + this.writer.write("}"); + } + this.writer.dedent(); + this.writer.newLine(); + this.writer.write("}"); + } + // == Expressions == void writeTopLevelExpression(JavaExpression expression) throws IOException { 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 dd5add4..6f6096d 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 { +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 { 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/JavaSwitchRule.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaSwitchRule.java new file mode 100644 index 0000000..7d4b3c2 --- /dev/null +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaSwitchRule.java @@ -0,0 +1,44 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.output.lang.java.ast; + +// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchRule imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchRule imports + +public record JavaSwitchRule(org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaTypeRef type, org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIdentifier variableName, org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBlock body) { + public static org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchRule.Builder arbitrary() { + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchRule.Builder(org.zwobble.hobgoblin.compiler.output.lang.java.ast.HobgoblinNativeAst.arbitraryJavaTypeRef(), org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIdentifier.arbitrary().build(), org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBlock.arbitrary().build()); + } + + public record Builder(org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaTypeRef type, org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIdentifier variableName, org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBlock body) { + public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchRule build() { + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchRule(type, variableName, body); + } + + public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchRule.Builder withType(org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaTypeRef type) { + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchRule.Builder(type, variableName, body); + } + + public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchRule.Builder withVariableName(org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIdentifier variableName) { + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchRule.Builder(type, variableName, body); + } + + public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchRule.Builder withVariableName(org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIdentifier.Builder variableName) { + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchRule.Builder(type, variableName.build(), body); + } + + public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchRule.Builder withBody(org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBlock body) { + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchRule.Builder(type, variableName, body); + } + + public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchRule.Builder withBody(org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBlock.Builder body) { + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchRule.Builder(type, variableName, body.build()); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchRule.Builder body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchRule.Builder body + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchRule body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchRule body +} diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaSwitchStatement.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaSwitchStatement.java new file mode 100644 index 0000000..62575e2 --- /dev/null +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaSwitchStatement.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.JavaSwitchStatement imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchStatement imports + +public record JavaSwitchStatement(org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaExpression selectorExpression, java.util.List rules) implements org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBlockStatement { + public static org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchStatement.Builder arbitrary() { + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchStatement.Builder(org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBooleanLiteral.arbitrary().build(), java.util.List.of()); + } + + public record Builder(org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaExpression selectorExpression, java.util.List rules) implements org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBlockStatement.Builder { + public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchStatement build() { + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchStatement(selectorExpression, rules); + } + + public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchStatement.Builder withSelectorExpression(org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaExpression selectorExpression) { + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchStatement.Builder(selectorExpression, rules); + } + + public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchStatement.Builder withSelectorExpression(org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaExpression.Builder selectorExpression) { + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchStatement.Builder(selectorExpression.build(), rules); + } + + public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchStatement.Builder withRules(java.util.List rules) { + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchStatement.Builder(selectorExpression, rules); + } + + public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchStatement.Builder addRule(org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchRule rule) { + var rules = new java.util.ArrayList(this.rules); + rules.add(rule); + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchStatement.Builder(selectorExpression, rules); + } + + public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchStatement.Builder addRule(org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchRule.Builder rule) { + var rules = new java.util.ArrayList(this.rules); + rules.add(rule.build()); + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchStatement.Builder(selectorExpression, rules); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchStatement.Builder body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchStatement.Builder body + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchStatement body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchStatement 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 b26b8f5..eab48ae 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 @@ -1070,6 +1070,43 @@ public class JavaWriterTests { assertThat(string, equalTo("return 42;")); } + // === Switch statements === + + @Test + public void switchStatement() throws IOException { + var java = new JavaSwitchStatement( + new JavaRef(JavaIdentifier.of("x")), + List.of( + new JavaSwitchRule( + JavaTypeRef.topLevel(JavaPackageName.of("org", "example"), JavaIdentifier.of("A")), + JavaIdentifier.of("a"), + new JavaBlock(List.of( + new JavaReturn(new JavaIntegerLiteral(0)) + )) + ), + new JavaSwitchRule( + JavaTypeRef.topLevel(JavaPackageName.of("org", "example"), JavaIdentifier.of("B")), + JavaIdentifier.of("b"), + new JavaBlock(List.of( + new JavaReturn(new JavaIntegerLiteral(1)) + )) + ) + ) + ); + + var string = write(writer -> writer.writeBlockStatement(java)); + + assertThat(string, equalTo(""" + switch (x) { + case org.example.A a -> { + return 0; + } + case org.example.B b -> { + return 1; + } + }""")); + } + // == Expressions == // === Binary operations === diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaSwitchRuleMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaSwitchRuleMatcher.java new file mode 100644 index 0000000..fc6f33d --- /dev/null +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaSwitchRuleMatcher.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.JavaSwitchRuleMatcher imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchRuleMatcher imports + +public class JavaSwitchRuleMatcher implements org.zwobble.precisely.Matcher { + public static org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchRuleMatcher isJavaSwitchRule() { + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchRuleMatcher(java.util.List.of()); + } + + private final java.util.List> submatchers; + + private JavaSwitchRuleMatcher(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.JavaSwitchRule.class, this.submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchRuleMatcher withType(org.zwobble.precisely.Matcher type) { + var submatchers = new java.util.ArrayList>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("type", org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchRule::type, type)); + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchRuleMatcher(submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchRuleMatcher withVariableName(org.zwobble.precisely.Matcher variableName) { + var submatchers = new java.util.ArrayList>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("variableName", org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchRule::variableName, variableName)); + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchRuleMatcher(submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchRuleMatcher 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.JavaSwitchRule::body, body)); + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchRuleMatcher(submatchers); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchRuleMatcher body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchRuleMatcher body +} diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaSwitchStatementMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaSwitchStatementMatcher.java new file mode 100644 index 0000000..5cf7d28 --- /dev/null +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaSwitchStatementMatcher.java @@ -0,0 +1,45 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.output.lang.java.ast; + +// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchStatementMatcher imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchStatementMatcher imports + +public class JavaSwitchStatementMatcher implements org.zwobble.precisely.Matcher { + public static org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchStatementMatcher isJavaSwitchStatement() { + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchStatementMatcher(java.util.List.of()); + } + + private final java.util.List> submatchers; + + private JavaSwitchStatementMatcher(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.JavaSwitchStatement.class, this.submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchStatementMatcher withSelectorExpression(org.zwobble.precisely.Matcher selectorExpression) { + var submatchers = new java.util.ArrayList>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("selectorExpression", org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchStatement::selectorExpression, selectorExpression)); + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchStatementMatcher(submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchStatementMatcher withRules(org.zwobble.precisely.Matcher> rules) { + var submatchers = new java.util.ArrayList>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("rules", org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchStatement::rules, rules)); + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchStatementMatcher(submatchers); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchStatementMatcher body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchStatementMatcher body +} -- cgit v1.2.3