diff options
| author | Michael Williamson <mike@zwobble.org> | 2026-07-03 12:09:15 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2026-07-03 12:09:15 +0100 |
| commit | caeef0039e93a78ffc9de251ffc8b09ed329a822 (patch) | |
| tree | 84c33a97cf176f337d136e2af366c1621a973acb /src | |
| parent | 30db9ed9ab6922ea703321b09f03e9fa2eb28235 (diff) | |
Add Rust bool literals
Diffstat (limited to 'src')
5 files changed, 119 insertions, 0 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 1e93d1d..299a8b5 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 @@ -202,6 +202,20 @@ public class RustWriter implements AutoCloseable { this.writer.write("}"); } + // == Expressions == + + void writeExpression(RustExpression expression) throws IOException { + switch (expression) { + case RustBoolLiteral boolLiteral -> { + this.writeBoolLiteral(boolLiteral); + } + } + } + + private void writeBoolLiteral(RustBoolLiteral boolLiteral) throws IOException { + this.writer.write(boolLiteral.value() ? "true" : "false"); + } + // == Types == void writeType(RustType type) throws IOException { diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustBoolLiteral.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustBoolLiteral.java new file mode 100644 index 0000000..5e6a90a --- /dev/null +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustBoolLiteral.java @@ -0,0 +1,28 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.output.lang.rust.ast; + +// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustBoolLiteral imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustBoolLiteral imports + +public record RustBoolLiteral(boolean value) implements org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression { + public static org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustBoolLiteral.Builder arbitrary() { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustBoolLiteral.Builder(false); + } + + public record Builder(boolean value) implements org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression.Builder { + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustBoolLiteral build() { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustBoolLiteral(value); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustBoolLiteral.Builder withValue(boolean value) { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustBoolLiteral.Builder(value); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustBoolLiteral.Builder body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustBoolLiteral.Builder body + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustBoolLiteral body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustBoolLiteral 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 new file mode 100644 index 0000000..f665e26 --- /dev/null +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustExpression.java @@ -0,0 +1,18 @@ +// Generated by hobgoblin. + +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.RustBoolLiteral { + public interface Builder { + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression build(); + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression.Builder body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression.Builder body + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression 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 b7e3047..96cdc83 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 @@ -245,6 +245,26 @@ public class RustWriterTests { }""")); } + // == Expressions == + + @Test + public void trueLiteral() throws IOException { + var rust = RustBoolLiteral.arbitrary().withValue(true).build(); + + var string = write(writer -> writer.writeExpression(rust)); + + assertThat(string, equalTo("true")); + } + + @Test + public void falseLiteral() throws IOException { + var rust = RustBoolLiteral.arbitrary().withValue(false).build(); + + var string = write(writer -> writer.writeExpression(rust)); + + assertThat(string, equalTo("false")); + } + // == Types == @Test diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustBoolLiteralMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustBoolLiteralMatcher.java new file mode 100644 index 0000000..11baf44 --- /dev/null +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustBoolLiteralMatcher.java @@ -0,0 +1,39 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.output.lang.rust.ast; + +// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustBoolLiteralMatcher imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustBoolLiteralMatcher imports + +public class RustBoolLiteralMatcher implements org.zwobble.precisely.Matcher<java.lang.Object> { + public static org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustBoolLiteralMatcher isRustBoolLiteral() { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustBoolLiteralMatcher(java.util.List.of()); + } + + private final java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustBoolLiteral>> submatchers; + + private RustBoolLiteralMatcher(java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustBoolLiteral>> 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.RustBoolLiteral.class, this.submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustBoolLiteralMatcher withValue(org.zwobble.precisely.Matcher<? super java.lang.Boolean> value) { + var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustBoolLiteral>>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("value", org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustBoolLiteral::value, value)); + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustBoolLiteralMatcher(submatchers); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustBoolLiteralMatcher body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustBoolLiteralMatcher body +} |
