diff options
Diffstat (limited to 'src')
5 files changed, 133 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 6febdde..b27a755 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.writeFieldExpression(fieldExpression); } + case RustIfExpression ifExpression -> { + this.writeIfExpression(ifExpression); + } + case RustIntegerLiteral integerLiteral -> { this.writeIntegerLiteral(integerLiteral); } @@ -403,6 +407,15 @@ public class RustWriter implements AutoCloseable { this.writeIdentifier(fieldExpression.fieldName()); } + private void writeIfExpression(RustIfExpression ifExpression) throws IOException { + this.writer.write("if "); + this.writeExpression(ifExpression.condition()); + this.writer.write(" "); + this.writeBlockExpression(ifExpression.ifTrue()); + this.writer.write(" else "); + this.writeBlockExpression(ifExpression.ifFalse()); + } + private void writeIntegerLiteral(RustIntegerLiteral integerLiteral) throws IOException { this.writer.write(Long.toString(integerLiteral.value())); } 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 be39797..124f395 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.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.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 { +public sealed interface RustExpression permits org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustArrayRepeatExpression, 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 { 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/RustIfExpression.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustIfExpression.java new file mode 100644 index 0000000..26f3cce --- /dev/null +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustIfExpression.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.RustIfExpression imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIfExpression imports + +public record RustIfExpression(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression condition, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustBlockExpression ifTrue, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustBlockExpression ifFalse) implements org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression { + public static org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIfExpression.Builder arbitrary() { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIfExpression.Builder(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustBlockExpression.arbitrary().build(), 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 condition, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustBlockExpression ifTrue, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustBlockExpression ifFalse) implements org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression.Builder { + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIfExpression build() { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIfExpression(condition, ifTrue, ifFalse); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIfExpression.Builder withCondition(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression condition) { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIfExpression.Builder(condition, ifTrue, ifFalse); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIfExpression.Builder withCondition(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression.Builder condition) { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIfExpression.Builder(condition.build(), ifTrue, ifFalse); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIfExpression.Builder withIfTrue(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustBlockExpression ifTrue) { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIfExpression.Builder(condition, ifTrue, ifFalse); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIfExpression.Builder withIfTrue(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustBlockExpression.Builder ifTrue) { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIfExpression.Builder(condition, ifTrue.build(), ifFalse); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIfExpression.Builder withIfFalse(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustBlockExpression ifFalse) { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIfExpression.Builder(condition, ifTrue, ifFalse); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIfExpression.Builder withIfFalse(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustBlockExpression.Builder ifFalse) { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIfExpression.Builder(condition, ifTrue, ifFalse.build()); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIfExpression.Builder body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIfExpression.Builder body + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIfExpression body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIfExpression 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 c17d96f..2568c42 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 @@ -537,6 +537,26 @@ public class RustWriterTests { assertThat(string, equalTo("x.y")); } + // === If expressions === + + @Test + public void ifExpression() throws IOException { + var rust = RustIfExpression.arbitrary() + .withCondition(RustPath.of("a")) + .withIfTrue(RustBlockExpression.arbitrary().withFinalOperand(RustPath.of("b"))) + .withIfFalse(RustBlockExpression.arbitrary().withFinalOperand(RustPath.of("c"))) + .build(); + + var string = write(writer -> writer.writeExpression(rust)); + + assertThat(string, equalTo(""" + if a { + b + } else { + c + }""")); + } + // === Integer literals === @Test diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustIfExpressionMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustIfExpressionMatcher.java new file mode 100644 index 0000000..16fc324 --- /dev/null +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustIfExpressionMatcher.java @@ -0,0 +1,51 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.output.lang.rust.ast; + +// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIfExpressionMatcher imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIfExpressionMatcher imports + +public class RustIfExpressionMatcher implements org.zwobble.precisely.Matcher<java.lang.Object> { + public static org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIfExpressionMatcher isRustIfExpression() { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIfExpressionMatcher(java.util.List.of()); + } + + private final java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIfExpression>> submatchers; + + private RustIfExpressionMatcher(java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIfExpression>> 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.RustIfExpression.class, this.submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIfExpressionMatcher withCondition(org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression> condition) { + var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIfExpression>>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("condition", org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIfExpression::condition, condition)); + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIfExpressionMatcher(submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIfExpressionMatcher withIfTrue(org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustBlockExpression> ifTrue) { + var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIfExpression>>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("ifTrue", org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIfExpression::ifTrue, ifTrue)); + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIfExpressionMatcher(submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIfExpressionMatcher withIfFalse(org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustBlockExpression> ifFalse) { + var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIfExpression>>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("ifFalse", org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIfExpression::ifFalse, ifFalse)); + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIfExpressionMatcher(submatchers); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIfExpressionMatcher body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIfExpressionMatcher body +} |
