summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2026-07-14 21:37:50 +0100
committerMichael Williamson <mike@zwobble.org>2026-07-14 21:37:50 +0100
commitbd750da1b6972c56358675f1675e0bb276eac3de (patch)
tree37b03835a0bcfe84b6d22be225b7706a26d57d6f /src
parente55af8bb139e6db7fda83541119ab996507562d9 (diff)
Add simple let statements to Rust AST
Diffstat (limited to 'src')
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/RustWriter.java12
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustLetStatement.java40
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustStatement.java2
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/RustWriterTests.java17
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustLetStatementMatcher.java45
5 files changed, 115 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 9d6e2e0..60641c8 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
@@ -281,6 +281,10 @@ public class RustWriter implements AutoCloseable {
case RustExpressionStatement expressionStatement -> {
this.writeExpressionStatement(expressionStatement);
}
+
+ case RustLetStatement letStatement -> {
+ this.writeLetStatement(letStatement);
+ }
}
}
@@ -289,6 +293,14 @@ public class RustWriter implements AutoCloseable {
this.writer.write(";");
}
+ private void writeLetStatement(RustLetStatement letStatement) throws IOException {
+ this.writer.write("let ");
+ this.writeIdentifier(letStatement.variableName());
+ this.writer.write(" = ");
+ this.writeExpression(letStatement.value());
+ this.writer.write(";");
+ }
+
// == Expressions ==
void writeExpression(RustExpression expression) throws IOException {
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustLetStatement.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustLetStatement.java
new file mode 100644
index 0000000..810218f
--- /dev/null
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustLetStatement.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.RustLetStatement imports
+// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLetStatement imports
+
+public record RustLetStatement(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIdentifier variableName, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression value) implements org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStatement {
+ public static org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLetStatement.Builder arbitrary() {
+ return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLetStatement.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 variableName, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression value) implements org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStatement.Builder {
+ public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLetStatement build() {
+ return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLetStatement(variableName, value);
+ }
+
+ public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLetStatement.Builder withVariableName(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIdentifier variableName) {
+ return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLetStatement.Builder(variableName, value);
+ }
+
+ public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLetStatement.Builder withVariableName(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIdentifier.Builder variableName) {
+ return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLetStatement.Builder(variableName.build(), value);
+ }
+
+ public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLetStatement.Builder withValue(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression value) {
+ return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLetStatement.Builder(variableName, value);
+ }
+
+ public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLetStatement.Builder withValue(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression.Builder value) {
+ return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLetStatement.Builder(variableName, value.build());
+ }
+
+ // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLetStatement.Builder body
+ // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLetStatement.Builder body
+ }
+
+ // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLetStatement body
+ // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLetStatement body
+}
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustStatement.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustStatement.java
index 0e49c03..c17c66a 100644
--- a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustStatement.java
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustStatement.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.RustStatement imports
// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStatement imports
-public sealed interface RustStatement permits org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpressionStatement {
+public sealed interface RustStatement permits org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpressionStatement, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLetStatement {
public interface Builder {
public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustStatement 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 358d99d..625526b 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
@@ -338,6 +338,8 @@ public class RustWriterTests {
// == Statements ==
+ // === Expression statements ===
+
@Test
public void expressionStatementIsExpressionFollowedBySemicolon() throws IOException {
var rust = RustExpressionStatement.arbitrary()
@@ -349,6 +351,21 @@ public class RustWriterTests {
assertThat(string, equalTo("true;"));
}
+ // === Let statements ===
+
+ @Test
+ public void letStatementDeclaringSingleVariable() throws IOException {
+ var rust = RustLetStatement.arbitrary()
+ .withVariableName(RustIdentifier.of("x"))
+ .withValue(RustBoolLiteral.arbitrary().withValue(true).build())
+ .build();
+
+ var string = write(writer -> writer.writeStatement(rust));
+
+ assertThat(string, equalTo("let x = true;"));
+
+ }
+
// == Expressions ==
// === Block expressions ===
diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustLetStatementMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustLetStatementMatcher.java
new file mode 100644
index 0000000..c71dd98
--- /dev/null
+++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustLetStatementMatcher.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.RustLetStatementMatcher imports
+// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLetStatementMatcher imports
+
+public class RustLetStatementMatcher implements org.zwobble.precisely.Matcher<java.lang.Object> {
+ public static org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLetStatementMatcher isRustLetStatement() {
+ return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLetStatementMatcher(java.util.List.of());
+ }
+
+ private final java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLetStatement>> submatchers;
+
+ private RustLetStatementMatcher(java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLetStatement>> 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.RustLetStatement.class, this.submatchers);
+ }
+
+ public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLetStatementMatcher withVariableName(org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIdentifier> variableName) {
+ var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLetStatement>>(this.submatchers);
+ submatchers.add(org.zwobble.precisely.Matchers.has("variableName", org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLetStatement::variableName, variableName));
+ return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLetStatementMatcher(submatchers);
+ }
+
+ public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLetStatementMatcher 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.RustLetStatement>>(this.submatchers);
+ submatchers.add(org.zwobble.precisely.Matchers.has("value", org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLetStatement::value, value));
+ return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLetStatementMatcher(submatchers);
+ }
+
+ // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLetStatementMatcher body
+ // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLetStatementMatcher body
+}