diff options
| author | Michael Williamson <mike@zwobble.org> | 2026-07-14 21:00:45 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2026-07-14 21:00:45 +0100 |
| commit | e5f4a367a810d70a6f2cbe048ca1fd78549f1d28 (patch) | |
| tree | 3db2e6a3cd5537aa8f9b493a0ce5673a4eeced25 | |
| parent | 7b4c0375059aaaf06cb8b8ccebebfbc0d20e01f2 (diff) | |
Add struct expressions to Rust AST
8 files changed, 251 insertions, 1 deletions
diff --git a/hobgoblin/src/output/lang/rust/ast.hob b/hobgoblin/src/output/lang/rust/ast.hob index 970ae7f..5f1d878 100644 --- a/hobgoblin/src/output/lang/rust/ast.hob +++ b/hobgoblin/src/output/lang/rust/ast.hob @@ -119,6 +119,7 @@ sum RustExpression { variant RustCallExpression; variant RustFieldExpression; variant RustPathInExpression; + variant RustStructExpression; } struct RustBlockExpression { @@ -140,6 +141,16 @@ struct RustFieldExpression { field fieldName: RustIdentifier; } +struct RustStructExpression { + field structPath: RustPathInExpression; + field fieldValues: List[RustStructExprField]; +} + +struct RustStructExprField { + field name: RustIdentifier; + field value: 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 5a29448..26a2737 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 @@ -312,6 +312,10 @@ public class RustWriter implements AutoCloseable { case RustPathInExpression pathInExpression -> { this.writePathInExpression(pathInExpression); } + + case RustStructExpression structExpression -> { + this.writeStructExpression(structExpression); + } } } @@ -357,6 +361,29 @@ public class RustWriter implements AutoCloseable { this.writeIdentifier(fieldExpression.fieldName()); } + private void writeStructExpression(RustStructExpression structExpression) throws IOException { + this.writePathInExpression(structExpression.structPath()); + this.writer.write(" {"); + + if (!structExpression.fieldValues().isEmpty()) { + this.writer.write(" "); + writeWithSeparator( + structExpression.fieldValues(), + fieldValue -> { + this.writeIdentifier(fieldValue.name()); + this.writer.write(": "); + this.writeExpression(fieldValue.value()); + }, + () -> { + this.writer.write(", "); + } + ); + 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/RustExpression.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustExpression.java index f850a72..1918fab 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.RustCallExpression, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustFieldExpression, 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.RustFieldExpression, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathInExpression, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExpression { public interface Builder { public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression build(); diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustStructExprField.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustStructExprField.java new file mode 100644 index 0000000..56c221f --- /dev/null +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustStructExprField.java @@ -0,0 +1,40 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.output.lang.rust.ast; + +// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExprField imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExprField imports + +public record RustStructExprField(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIdentifier name, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression value) { + public static org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExprField.Builder arbitrary() { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExprField.Builder(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIdentifier.arbitrary().build(), org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustBlockExpression.arbitrary().build()); + } + + public record Builder(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIdentifier name, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression value) { + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExprField build() { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExprField(name, value); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExprField.Builder withName(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIdentifier name) { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExprField.Builder(name, value); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExprField.Builder withName(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIdentifier.Builder name) { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExprField.Builder(name.build(), value); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExprField.Builder withValue(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression value) { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExprField.Builder(name, value); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExprField.Builder withValue(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression.Builder value) { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExprField.Builder(name, value.build()); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExprField.Builder body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExprField.Builder body + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExprField body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExprField body +} diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustStructExpression.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustStructExpression.java new file mode 100644 index 0000000..370e17c --- /dev/null +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustStructExpression.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.RustStructExpression imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExpression imports + +public record RustStructExpression(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathInExpression structPath, java.util.List<org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExprField> fieldValues) implements org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression { + public static org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExpression.Builder arbitrary() { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExpression.Builder(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathInExpression.arbitrary().build(), java.util.List.of()); + } + + public record Builder(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathInExpression structPath, java.util.List<org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExprField> fieldValues) implements org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression.Builder { + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExpression build() { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExpression(structPath, fieldValues); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExpression.Builder withStructPath(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathInExpression structPath) { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExpression.Builder(structPath, fieldValues); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExpression.Builder withStructPath(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathInExpression.Builder structPath) { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExpression.Builder(structPath.build(), fieldValues); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExpression.Builder withFieldValues(java.util.List<org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExprField> fieldValues) { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExpression.Builder(structPath, fieldValues); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExpression.Builder addFieldValue(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExprField fieldValue) { + var fieldValues = new java.util.ArrayList<org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExprField>(this.fieldValues); + fieldValues.add(fieldValue); + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExpression.Builder(structPath, fieldValues); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExpression.Builder addFieldValue(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExprField.Builder fieldValue) { + var fieldValues = new java.util.ArrayList<org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExprField>(this.fieldValues); + fieldValues.add(fieldValue.build()); + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExpression.Builder(structPath, fieldValues); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExpression.Builder body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExpression.Builder body + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExpression body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExpression body +} 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 21f8833..67d0a42 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 @@ -480,6 +480,40 @@ public class RustWriterTests { assertThat(string, equalTo("x.y")); } + // === Struct expressions === + + @Test + public void structExpressionWithNoFields() throws IOException { + var rust = RustStructExpression.arbitrary() + .withStructPath(RustPathInExpression.of("A")) + .build(); + + var string = write(writer -> writer.writeExpression(rust)); + + assertThat(string, equalTo("A {}")); + } + + @Test + public void structExpressionWithFields() throws IOException { + var rust = RustStructExpression.arbitrary() + .withStructPath(RustPathInExpression.of("X")) + .addFieldValue( + RustStructExprField.arbitrary() + .withName(RustIdentifier.of("a")) + .withValue(RustPathInExpression.of("b")) + ) + .addFieldValue( + RustStructExprField.arbitrary() + .withName(RustIdentifier.of("c")) + .withValue(RustPathInExpression.of("d")) + ) + .build(); + + var string = write(writer -> writer.writeExpression(rust)); + + assertThat(string, equalTo("X { a: b, c: d }")); + } + // == Types == @Test diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustStructExprFieldMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustStructExprFieldMatcher.java new file mode 100644 index 0000000..2c8ac77 --- /dev/null +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustStructExprFieldMatcher.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.RustStructExprFieldMatcher imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExprFieldMatcher imports + +public class RustStructExprFieldMatcher implements org.zwobble.precisely.Matcher<java.lang.Object> { + public static org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExprFieldMatcher isRustStructExprField() { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExprFieldMatcher(java.util.List.of()); + } + + private final java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExprField>> submatchers; + + private RustStructExprFieldMatcher(java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExprField>> 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.RustStructExprField.class, this.submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExprFieldMatcher withName(org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIdentifier> name) { + var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExprField>>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("name", org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExprField::name, name)); + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExprFieldMatcher(submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExprFieldMatcher withValue(org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression> value) { + var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExprField>>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("value", org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExprField::value, value)); + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExprFieldMatcher(submatchers); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExprFieldMatcher body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExprFieldMatcher body +} diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustStructExpressionMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustStructExpressionMatcher.java new file mode 100644 index 0000000..5597d00 --- /dev/null +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustStructExpressionMatcher.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.RustStructExpressionMatcher imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExpressionMatcher imports + +public class RustStructExpressionMatcher implements org.zwobble.precisely.Matcher<java.lang.Object> { + public static org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExpressionMatcher isRustStructExpression() { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExpressionMatcher(java.util.List.of()); + } + + private final java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExpression>> submatchers; + + private RustStructExpressionMatcher(java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExpression>> 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.RustStructExpression.class, this.submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExpressionMatcher withStructPath(org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathInExpression> structPath) { + var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExpression>>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("structPath", org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExpression::structPath, structPath)); + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExpressionMatcher(submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExpressionMatcher withFieldValues(org.zwobble.precisely.Matcher<? super java.util.List<org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExprField>> fieldValues) { + var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExpression>>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("fieldValues", org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExpression::fieldValues, fieldValues)); + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExpressionMatcher(submatchers); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExpressionMatcher body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStructExpressionMatcher body +} |
