From 3dbb3be9811c9ad93fa1df400297ac935aad8250 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 5 Jul 2026 11:23:14 +0100 Subject: Add cast to Java AST --- .../compiler/output/lang/java/JavaWriterTests.java | 9 +++++ .../output/lang/java/ast/JavaCastMatcher.java | 45 ++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaCastMatcher.java (limited to 'src/test/java') 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 5b07ecb..b888375 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 @@ -831,6 +831,15 @@ public class JavaWriterTests { assertThat(string, equalTo("true")); } + @Test + public void cast() throws IOException { + var java = new JavaCast(JavaTypeRef.INT, new JavaIntegerLiteral(42)); + + var string = write(writer -> writer.writeTopLevelExpression(java)); + + assertThat(string, equalTo("(int) 42")); + } + @Test public void integerLiteral() throws IOException { var java = new JavaIntegerLiteral(42); diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaCastMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaCastMatcher.java new file mode 100644 index 0000000..4edd959 --- /dev/null +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaCastMatcher.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.JavaCastMatcher imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCastMatcher imports + +public class JavaCastMatcher implements org.zwobble.precisely.Matcher { + public static org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCastMatcher isJavaCast() { + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCastMatcher(java.util.List.of()); + } + + private final java.util.List> submatchers; + + private JavaCastMatcher(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.JavaCast.class, this.submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCastMatcher 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.JavaCast::type, type)); + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCastMatcher(submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCastMatcher withOperand(org.zwobble.precisely.Matcher operand) { + var submatchers = new java.util.ArrayList>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("operand", org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCast::operand, operand)); + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCastMatcher(submatchers); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCastMatcher body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCastMatcher body +} -- cgit v1.2.3