From 0a2046ff66bcac5c1d26178f3e4d60a5b00c826b Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 19 Jul 2026 22:08:05 +0100 Subject: Add closure expressions to Rust AST --- .../compiler/output/lang/rust/RustWriter.java | 24 +++++++++++ .../lang/rust/ast/RustClosureExpression.java | 48 ++++++++++++++++++++++ .../output/lang/rust/ast/RustExpression.java | 2 +- .../compiler/output/lang/rust/RustWriterTests.java | 39 ++++++++++++++++++ .../rust/ast/RustClosureExpressionMatcher.java | 45 ++++++++++++++++++++ 5 files changed, 157 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustClosureExpression.java create mode 100644 src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustClosureExpressionMatcher.java (limited to 'src') diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/RustWriter.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/RustWriter.java index 3f3f3d8..d894253 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/RustWriter.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/RustWriter.java @@ -335,6 +335,10 @@ public class RustWriter implements AutoCloseable { this.writeCallExpression(callExpression); } + case RustClosureExpression closureExpression -> { + this.writeClosureExpression(closureExpression); + } + case RustFieldExpression fieldExpression -> { this.writeFieldExpression(fieldExpression); } @@ -414,6 +418,7 @@ public class RustWriter implements AutoCloseable { case RustBlockExpression _ -> RustPrecedence.PRIMARY; case RustBoolLiteral _ -> RustPrecedence.PRIMARY; case RustCallExpression _ -> RustPrecedence.FUNCTION_CALLS; + case RustClosureExpression _ -> RustPrecedence.PRIMARY; case RustFieldExpression _ -> RustPrecedence.FIELD_EXPRESSIONS; case RustIfExpression _ -> RustPrecedence.PRIMARY; case RustIntegerLiteral _ -> RustPrecedence.PRIMARY; @@ -522,6 +527,25 @@ public class RustWriter implements AutoCloseable { this.writer.write(")"); } + private void writeClosureExpression( + RustClosureExpression closureExpression + ) throws IOException { + this.writer.write("|"); + + writeWithSeparator( + closureExpression.params(), + this::writePattern, + () -> { + this.writer.write(", "); + } + ); + + this.writer.write("| "); + + this.writeTopLevelExpression(closureExpression.body()); + } + + private void writeFieldExpression(RustFieldExpression fieldExpression) throws IOException { this.writeSubExpression(fieldExpression.containerOperand(), precedence(fieldExpression), true); this.writer.write("."); diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustClosureExpression.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustClosureExpression.java new file mode 100644 index 0000000..2322750 --- /dev/null +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustClosureExpression.java @@ -0,0 +1,48 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.output.lang.rust.ast; + +// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustClosureExpression imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustClosureExpression imports + +public record RustClosureExpression(java.util.List params, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression body) implements org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression { + public static org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustClosureExpression.Builder arbitrary() { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustClosureExpression.Builder(java.util.List.of(), org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustBlockExpression.arbitrary().build()); + } + + public record Builder(java.util.List params, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression body) implements org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression.Builder { + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustClosureExpression build() { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustClosureExpression(params, body); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustClosureExpression.Builder withParams(java.util.List params) { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustClosureExpression.Builder(params, body); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustClosureExpression.Builder addParam(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPattern param) { + var params = new java.util.ArrayList(this.params); + params.add(param); + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustClosureExpression.Builder(params, body); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustClosureExpression.Builder addParam(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPattern.Builder param) { + var params = new java.util.ArrayList(this.params); + params.add(param.build()); + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustClosureExpression.Builder(params, body); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustClosureExpression.Builder withBody(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression body) { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustClosureExpression.Builder(params, body); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustClosureExpression.Builder withBody(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression.Builder body) { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustClosureExpression.Builder(params, body.build()); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustClosureExpression.Builder body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustClosureExpression.Builder body + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustClosureExpression body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustClosureExpression body +} diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustExpression.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustExpression.java index 8e55bc4..5f51d5e 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustExpression.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustExpression.java @@ -5,7 +5,7 @@ package org.zwobble.hobgoblin.compiler.output.lang.rust.ast; // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression imports // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression imports -public sealed interface RustExpression permits org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustArrayRepeatExpression, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustBinaryExpression, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustBlockExpression, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustBoolLiteral, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustCallExpression, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustFieldExpression, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIfExpression, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIntegerLiteral, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIteratorLoopExpression, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMatchExpression, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPath, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPrefixExpression, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustRangeExpr, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExpression, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustTryPropagationExpression, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustTupleExpression, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustVecRepeatExpression { +public sealed interface RustExpression permits org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustArrayRepeatExpression, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustBinaryExpression, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustBlockExpression, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustBoolLiteral, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustCallExpression, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustClosureExpression, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustFieldExpression, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIfExpression, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIntegerLiteral, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIteratorLoopExpression, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMatchExpression, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPath, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPrefixExpression, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustRangeExpr, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExpression, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustTryPropagationExpression, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustTupleExpression, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustVecRepeatExpression { public interface Builder { public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression build(); diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/RustWriterTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/RustWriterTests.java index 1d04b72..481d99e 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/RustWriterTests.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/RustWriterTests.java @@ -564,6 +564,45 @@ public class RustWriterTests { assertThat(string, equalTo("f(x, y)")); } + // === Closure expressions === + + @Test + public void closureExpressionWithNoParams() throws IOException { + var rust = RustClosureExpression.arbitrary() + .withBody(RustPath.of("a")) + .build(); + + var string = write(writer -> writer.writeTopLevelExpression(rust)); + + assertThat(string, equalTo("|| a")); + } + + @Test + public void closureExpressionWithOneParam() throws IOException { + var rust = RustClosureExpression.arbitrary() + .addParam(RustIdentifierPattern.arbitrary().withVariableName(RustIdentifier.of("a"))) + .withBody(RustPath.of("a")) + .build(); + + var string = write(writer -> writer.writeTopLevelExpression(rust)); + + assertThat(string, equalTo("|a| a")); + } + + @Test + public void closureExpressionWithMultipleParams() throws IOException { + var rust = RustClosureExpression.arbitrary() + .addParam(RustIdentifierPattern.arbitrary().withVariableName(RustIdentifier.of("a"))) + .addParam(RustIdentifierPattern.arbitrary().withVariableName(RustIdentifier.of("b"))) + .addParam(RustIdentifierPattern.arbitrary().withVariableName(RustIdentifier.of("c"))) + .withBody(RustPath.of("a")) + .build(); + + var string = write(writer -> writer.writeTopLevelExpression(rust)); + + assertThat(string, equalTo("|a, b, c| a")); + } + // === Field expressions === @Test diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustClosureExpressionMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustClosureExpressionMatcher.java new file mode 100644 index 0000000..d50cc13 --- /dev/null +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustClosureExpressionMatcher.java @@ -0,0 +1,45 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.output.lang.rust.ast; + +// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustClosureExpressionMatcher imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustClosureExpressionMatcher imports + +public class RustClosureExpressionMatcher implements org.zwobble.precisely.Matcher { + public static org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustClosureExpressionMatcher isRustClosureExpression() { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustClosureExpressionMatcher(java.util.List.of()); + } + + private final java.util.List> submatchers; + + private RustClosureExpressionMatcher(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.rust.ast.RustClosureExpression.class, this.submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustClosureExpressionMatcher withParams(org.zwobble.precisely.Matcher> params) { + var submatchers = new java.util.ArrayList>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("params", org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustClosureExpression::params, params)); + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustClosureExpressionMatcher(submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustClosureExpressionMatcher 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.rust.ast.RustClosureExpression::body, body)); + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustClosureExpressionMatcher(submatchers); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustClosureExpressionMatcher body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustClosureExpressionMatcher body +} -- cgit v1.2.3