diff options
| author | Michael Williamson <mike@zwobble.org> | 2026-07-12 11:15:26 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2026-07-12 11:15:26 +0100 |
| commit | c33905c4351a02327358284e6de4579a5e311326 (patch) | |
| tree | d7620deded05b4f00bb10a6fa54b380f409c0eb6 /src/main | |
| parent | eb4ce8b7ebceac4bbf68b779b9f6d6f4439ddbba (diff) | |
Support case constants in Java AST
Diffstat (limited to 'src/main')
5 files changed, 106 insertions, 17 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 893e30b..893b055 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 @@ -542,9 +542,16 @@ public class JavaWriter implements AutoCloseable { for (var rule : switchStatement.rules()) { this.writer.newLine(); this.writer.write("case "); - this.writeTypeRef(rule.type()); - this.writer.write(" "); - this.writeIdentifier(rule.variableName()); + switch (rule.label()) { + case JavaCaseConstant caseConstant -> { + this.writeTopLevelExpression(caseConstant.value()); + } + case JavaCasePattern casePattern -> { + this.writeTypeRef(casePattern.type()); + this.writer.write(" "); + this.writeIdentifier(casePattern.variableName()); + } + } this.writer.write(" -> {"); this.writer.indent(); this.writer.newLine(); diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaCaseConstant.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaCaseConstant.java new file mode 100644 index 0000000..8775fbb --- /dev/null +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaCaseConstant.java @@ -0,0 +1,32 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.output.lang.java.ast; + +// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCaseConstant imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCaseConstant imports + +public record JavaCaseConstant(org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaExpression value) implements org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchLabel { + public static org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCaseConstant.Builder arbitrary() { + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCaseConstant.Builder(org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBooleanLiteral.arbitrary().build()); + } + + public record Builder(org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaExpression value) implements org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchLabel.Builder { + public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCaseConstant build() { + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCaseConstant(value); + } + + public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCaseConstant.Builder withValue(org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaExpression value) { + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCaseConstant.Builder(value); + } + + public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCaseConstant.Builder withValue(org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaExpression.Builder value) { + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCaseConstant.Builder(value.build()); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCaseConstant.Builder body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCaseConstant.Builder body + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCaseConstant body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCaseConstant body +} diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaCasePattern.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaCasePattern.java new file mode 100644 index 0000000..11f2778 --- /dev/null +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaCasePattern.java @@ -0,0 +1,36 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.output.lang.java.ast; + +// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCasePattern imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCasePattern imports + +public record JavaCasePattern(org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaTypeRef type, org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIdentifier variableName) implements org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchLabel { + public static org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCasePattern.Builder arbitrary() { + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCasePattern.Builder(org.zwobble.hobgoblin.compiler.output.lang.java.ast.HobgoblinNativeAst.arbitraryJavaTypeRef(), org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIdentifier.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) implements org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchLabel.Builder { + public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCasePattern build() { + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCasePattern(type, variableName); + } + + public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCasePattern.Builder withType(org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaTypeRef type) { + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCasePattern.Builder(type, variableName); + } + + public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCasePattern.Builder withVariableName(org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIdentifier variableName) { + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCasePattern.Builder(type, variableName); + } + + public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCasePattern.Builder withVariableName(org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIdentifier.Builder variableName) { + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCasePattern.Builder(type, variableName.build()); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCasePattern.Builder body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCasePattern.Builder body + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCasePattern body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCasePattern body +} diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaSwitchLabel.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaSwitchLabel.java new file mode 100644 index 0000000..7978458 --- /dev/null +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaSwitchLabel.java @@ -0,0 +1,18 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.output.lang.java.ast; + +// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchLabel imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchLabel imports + +public sealed interface JavaSwitchLabel permits org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCaseConstant, org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCasePattern { + public interface Builder { + public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchLabel build(); + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchLabel.Builder body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchLabel.Builder body + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchLabel body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchLabel body +} 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 index 7d4b3c2..9e791d5 100644 --- 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 @@ -5,34 +5,30 @@ 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 record JavaSwitchRule(org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchLabel label, 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()); + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchRule.Builder(org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCaseConstant.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 record Builder(org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchLabel label, 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); + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchRule(label, 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 withLabel(org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchLabel label) { + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchRule.Builder(label, 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 withLabel(org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchLabel.Builder label) { + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchRule.Builder(label.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); + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchRule.Builder(label, 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()); + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchRule.Builder(label, body.build()); } // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchRule.Builder body |
