summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2026-07-19 22:08:05 +0100
committerMichael Williamson <mike@zwobble.org>2026-07-19 22:08:05 +0100
commit0a2046ff66bcac5c1d26178f3e4d60a5b00c826b (patch)
tree9e5862e28590d42abfb08ef53d091cd19d14d7d8
parent7d0eb6d99ac4a3dba96eb60dc373315082d52a63 (diff)
Add closure expressions to Rust AST
-rw-r--r--hobgoblin/src/output/lang/rust/ast.hob6
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/RustWriter.java24
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustClosureExpression.java48
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustExpression.java2
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/RustWriterTests.java39
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustClosureExpressionMatcher.java45
6 files changed, 163 insertions, 1 deletions
diff --git a/hobgoblin/src/output/lang/rust/ast.hob b/hobgoblin/src/output/lang/rust/ast.hob
index d1c7097..18b5978 100644
--- a/hobgoblin/src/output/lang/rust/ast.hob
+++ b/hobgoblin/src/output/lang/rust/ast.hob
@@ -127,6 +127,7 @@ sum RustExpression {
variant RustBlockExpression;
variant RustBoolLiteral;
variant RustCallExpression;
+ variant RustClosureExpression;
variant RustFieldExpression;
variant RustIfExpression;
variant RustIntegerLiteral;
@@ -177,6 +178,11 @@ struct RustCallExpression {
field args: List[RustExpression];
}
+struct RustClosureExpression {
+ field params: List[RustPattern];
+ field body: RustExpression;
+}
+
struct RustFieldExpression {
field containerOperand: RustExpression;
field fieldName: RustIdentifier;
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<org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPattern> 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<org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPattern> 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<org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPattern> 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<org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPattern>(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<org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPattern>(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<java.lang.Object> {
+ 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<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustClosureExpression>> submatchers;
+
+ private RustClosureExpressionMatcher(java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustClosureExpression>> 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<java.lang.Object> 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<? super java.util.List<org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPattern>> params) {
+ var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustClosureExpression>>(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<? super org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression> body) {
+ var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustClosureExpression>>(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
+}