diff options
Diffstat (limited to 'src')
5 files changed, 113 insertions, 1 deletions
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 b584c69..d4228b0 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 @@ -366,6 +366,10 @@ public class RustWriter implements AutoCloseable { case RustTupleExpression tupleExpression -> { this.writeTupleExpression(tupleExpression); } + + case RustVecRepeatExpression vecRepeatExpression -> { + this.writeVecRepeatExpression(vecRepeatExpression); + } } } @@ -406,6 +410,7 @@ public class RustWriter implements AutoCloseable { case RustStructExpression _ -> RustPrecedence.PRIMARY; case RustTryPropagationExpression _ -> RustPrecedence.TRY_PROPAGATION; case RustTupleExpression _ -> RustPrecedence.PRIMARY; + case RustVecRepeatExpression _ -> RustPrecedence.PRIMARY; }; } @@ -574,6 +579,14 @@ public class RustWriter implements AutoCloseable { this.writer.write(")"); } + private void writeVecRepeatExpression(RustVecRepeatExpression vecRepeatExpression) throws IOException { + this.writer.write("vec!["); + this.writeTopLevelExpression(vecRepeatExpression.repeatOperand()); + this.writer.write("; "); + this.writeTopLevelExpression(vecRepeatExpression.lengthOperand()); + 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 19e1988..95ca9f8 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.RustPath, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPrefixExpression, 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 { +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.RustPath, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPrefixExpression, 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/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustVecRepeatExpression.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustVecRepeatExpression.java new file mode 100644 index 0000000..09ffcd8 --- /dev/null +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustVecRepeatExpression.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.RustVecRepeatExpression imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustVecRepeatExpression imports + +public record RustVecRepeatExpression(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression repeatOperand, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression lengthOperand) implements org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression { + public static org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustVecRepeatExpression.Builder arbitrary() { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustVecRepeatExpression.Builder(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustBlockExpression.arbitrary().build(), org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustBlockExpression.arbitrary().build()); + } + + public record Builder(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression repeatOperand, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression lengthOperand) implements org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression.Builder { + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustVecRepeatExpression build() { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustVecRepeatExpression(repeatOperand, lengthOperand); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustVecRepeatExpression.Builder withRepeatOperand(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression repeatOperand) { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustVecRepeatExpression.Builder(repeatOperand, lengthOperand); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustVecRepeatExpression.Builder withRepeatOperand(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression.Builder repeatOperand) { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustVecRepeatExpression.Builder(repeatOperand.build(), lengthOperand); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustVecRepeatExpression.Builder withLengthOperand(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression lengthOperand) { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustVecRepeatExpression.Builder(repeatOperand, lengthOperand); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustVecRepeatExpression.Builder withLengthOperand(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression.Builder lengthOperand) { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustVecRepeatExpression.Builder(repeatOperand, lengthOperand.build()); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustVecRepeatExpression.Builder body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustVecRepeatExpression.Builder body + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustVecRepeatExpression body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustVecRepeatExpression 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 4279f49..bf93c1d 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 @@ -741,6 +741,20 @@ public class RustWriterTests { assertThat(string, equalTo("(a, b, c)")); } + // === Vec repeat expressions === + + @Test + public void vecRepeatExpression() throws IOException { + var rust = RustVecRepeatExpression.arbitrary() + .withRepeatOperand(RustIntegerLiteral.arbitrary().withValue(42)) + .withLengthOperand(RustIntegerLiteral.arbitrary().withValue(47)) + .build(); + + var string = write(writer -> writer.writeTopLevelExpression(rust)); + + assertThat(string, equalTo("vec![42; 47]")); + } + // === Precedence handling === @Test diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustVecRepeatExpressionMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustVecRepeatExpressionMatcher.java new file mode 100644 index 0000000..9519a7b --- /dev/null +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustVecRepeatExpressionMatcher.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.RustVecRepeatExpressionMatcher imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustVecRepeatExpressionMatcher imports + +public class RustVecRepeatExpressionMatcher implements org.zwobble.precisely.Matcher<java.lang.Object> { + public static org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustVecRepeatExpressionMatcher isRustVecRepeatExpression() { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustVecRepeatExpressionMatcher(java.util.List.of()); + } + + private final java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustVecRepeatExpression>> submatchers; + + private RustVecRepeatExpressionMatcher(java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustVecRepeatExpression>> 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.RustVecRepeatExpression.class, this.submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustVecRepeatExpressionMatcher withRepeatOperand(org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression> repeatOperand) { + var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustVecRepeatExpression>>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("repeatOperand", org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustVecRepeatExpression::repeatOperand, repeatOperand)); + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustVecRepeatExpressionMatcher(submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustVecRepeatExpressionMatcher withLengthOperand(org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression> lengthOperand) { + var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustVecRepeatExpression>>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("lengthOperand", org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustVecRepeatExpression::lengthOperand, lengthOperand)); + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustVecRepeatExpressionMatcher(submatchers); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustVecRepeatExpressionMatcher body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustVecRepeatExpressionMatcher body +} |
