From 53475694bb32e0df968a9d86b45e4a899fa166e9 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Fri, 3 Jul 2026 14:39:20 +0100 Subject: Add call expressions to Rust AST --- hobgoblin/src/output/lang/rust/ast.hob | 6 +++ .../compiler/output/lang/rust/RustWriter.java | 17 ++++++++ .../output/lang/rust/ast/RustCallExpression.java | 48 ++++++++++++++++++++++ .../output/lang/rust/ast/RustExpression.java | 2 +- .../compiler/output/lang/rust/RustWriterTests.java | 24 +++++++++++ .../lang/rust/ast/RustCallExpressionMatcher.java | 45 ++++++++++++++++++++ 6 files changed, 141 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustCallExpression.java create mode 100644 src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustCallExpressionMatcher.java diff --git a/hobgoblin/src/output/lang/rust/ast.hob b/hobgoblin/src/output/lang/rust/ast.hob index 42497ff..ffa6ee0 100644 --- a/hobgoblin/src/output/lang/rust/ast.hob +++ b/hobgoblin/src/output/lang/rust/ast.hob @@ -89,6 +89,7 @@ struct RustExpressionStatement { sum RustExpression { variant RustBlockExpression; variant RustBoolLiteral; + variant RustCallExpression; variant RustPathInExpression; } @@ -101,6 +102,11 @@ struct RustBoolLiteral { field value: Bool; } +struct RustCallExpression { + field function: RustExpression; + field args: List[RustExpression]; +} + // == Types == sum RustType { 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 9f14153..541a739 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 @@ -232,6 +232,10 @@ public class RustWriter implements AutoCloseable { this.writeBoolLiteral(boolLiteral); } + case RustCallExpression callExpression -> { + this.writeCallExpression(callExpression); + } + case RustPathInExpression pathInExpression -> { this.writePathInExpression(pathInExpression); } @@ -261,6 +265,19 @@ public class RustWriter implements AutoCloseable { this.writer.write(boolLiteral.value() ? "true" : "false"); } + private void writeCallExpression(RustCallExpression callExpression) throws IOException { + this.writeExpression(callExpression.function()); + this.writer.write("("); + writeWithSeparator( + callExpression.args(), + this::writeExpression, + () -> { + this.writer.write(", "); + } + ); + this.writer.write(")"); + } + // == Types == void writeType(RustType type) throws IOException { diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustCallExpression.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustCallExpression.java new file mode 100644 index 0000000..8ddeb2b --- /dev/null +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustCallExpression.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.RustCallExpression imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustCallExpression imports + +public record RustCallExpression(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression function, java.util.List args) implements org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression { + public static org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustCallExpression.Builder arbitrary() { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustCallExpression.Builder(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustBlockExpression.arbitrary().build(), java.util.List.of()); + } + + public record Builder(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression function, java.util.List args) implements org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression.Builder { + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustCallExpression build() { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustCallExpression(function, args); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustCallExpression.Builder withFunction(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression function) { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustCallExpression.Builder(function, args); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustCallExpression.Builder withFunction(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression.Builder function) { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustCallExpression.Builder(function.build(), args); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustCallExpression.Builder withArgs(java.util.List args) { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustCallExpression.Builder(function, args); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustCallExpression.Builder addArg(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression arg) { + var args = new java.util.ArrayList(this.args); + args.add(arg); + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustCallExpression.Builder(function, args); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustCallExpression.Builder addArg(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression.Builder arg) { + var args = new java.util.ArrayList(this.args); + args.add(arg.build()); + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustCallExpression.Builder(function, args); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustCallExpression.Builder body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustCallExpression.Builder body + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustCallExpression body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustCallExpression 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 ec2a3c0..4fea607 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.RustBlockExpression, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustBoolLiteral, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathInExpression { +public sealed interface RustExpression permits 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.RustPathInExpression { 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 2090c1e..2709df9 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 @@ -357,6 +357,30 @@ public class RustWriterTests { assertThat(string, equalTo("false")); } + @Test + public void callExpressionWithNoArgs() throws IOException { + var rust = RustCallExpression.arbitrary() + .withFunction(RustPathInExpression.of("f")) + .build(); + + var string = write(writer -> writer.writeExpression(rust)); + + assertThat(string, equalTo("f()")); + } + + @Test + public void callExpressionWithArgs() throws IOException { + var rust = RustCallExpression.arbitrary() + .withFunction(RustPathInExpression.of("f")) + .addArg(RustPathInExpression.of("x")) + .addArg(RustPathInExpression.of("y")) + .build(); + + var string = write(writer -> writer.writeExpression(rust)); + + assertThat(string, equalTo("f(x, y)")); + } + // == Paths == @Test diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustCallExpressionMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustCallExpressionMatcher.java new file mode 100644 index 0000000..a17d04d --- /dev/null +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustCallExpressionMatcher.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.RustCallExpressionMatcher imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustCallExpressionMatcher imports + +public class RustCallExpressionMatcher implements org.zwobble.precisely.Matcher { + public static org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustCallExpressionMatcher isRustCallExpression() { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustCallExpressionMatcher(java.util.List.of()); + } + + private final java.util.List> submatchers; + + private RustCallExpressionMatcher(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.RustCallExpression.class, this.submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustCallExpressionMatcher withFunction(org.zwobble.precisely.Matcher function) { + var submatchers = new java.util.ArrayList>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("function", org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustCallExpression::function, function)); + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustCallExpressionMatcher(submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustCallExpressionMatcher withArgs(org.zwobble.precisely.Matcher> args) { + var submatchers = new java.util.ArrayList>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("args", org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustCallExpression::args, args)); + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustCallExpressionMatcher(submatchers); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustCallExpressionMatcher body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustCallExpressionMatcher body +} -- cgit v1.2.3