From 114823dba737858fcdd6e3748f58865bf0204784 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 5 Jul 2026 13:05:30 +0100 Subject: Add ternary conditional to Java AST --- .../compiler/output/lang/java/JavaWriterTests.java | 13 ++++++ .../java/ast/JavaTernaryConditionalMatcher.java | 51 ++++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaTernaryConditionalMatcher.java (limited to 'src/test') 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 1ce78ea..0b5f959 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 @@ -1015,6 +1015,19 @@ public class JavaWriterTests { assertThat(string, equalTo("x")); } + @Test + public void ternaryConditional() throws IOException { + var java = new JavaTernaryConditional( + new JavaRef(JavaIdentifier.of("x")), + new JavaRef(JavaIdentifier.of("y")), + new JavaRef(JavaIdentifier.of("z")) + ); + + var string = write(writer -> writer.writeTopLevelExpression(java)); + + assertThat(string, equalTo("x ? y : z")); + } + @Test public void whenSubExpressionHasLowerPrecedenceThenSubExpressionIsParenthesized() throws IOException { var java = JavaBinaryOperation.arbitrary() diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaTernaryConditionalMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaTernaryConditionalMatcher.java new file mode 100644 index 0000000..5f39a26 --- /dev/null +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaTernaryConditionalMatcher.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.JavaTernaryConditionalMatcher imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaTernaryConditionalMatcher imports + +public class JavaTernaryConditionalMatcher implements org.zwobble.precisely.Matcher { + public static org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaTernaryConditionalMatcher isJavaTernaryConditional() { + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaTernaryConditionalMatcher(java.util.List.of()); + } + + private final java.util.List> submatchers; + + private JavaTernaryConditionalMatcher(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.JavaTernaryConditional.class, this.submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaTernaryConditionalMatcher 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.JavaTernaryConditional::condition, condition)); + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaTernaryConditionalMatcher(submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaTernaryConditionalMatcher withIfTrue(org.zwobble.precisely.Matcher ifTrue) { + var submatchers = new java.util.ArrayList>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("ifTrue", org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaTernaryConditional::ifTrue, ifTrue)); + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaTernaryConditionalMatcher(submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaTernaryConditionalMatcher withIfFalse(org.zwobble.precisely.Matcher ifFalse) { + var submatchers = new java.util.ArrayList>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("ifFalse", org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaTernaryConditional::ifFalse, ifFalse)); + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaTernaryConditionalMatcher(submatchers); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaTernaryConditionalMatcher body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaTernaryConditionalMatcher body +} -- cgit v1.2.3