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 --- .../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 ++++++++++++++++++++++ 4 files changed, 121 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 (limited to 'src/main') 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 +} -- cgit v1.2.3