diff options
5 files changed, 38 insertions, 9 deletions
diff --git a/hobgoblin/src/output/lang/rust/ast.hob b/hobgoblin/src/output/lang/rust/ast.hob index c37d72e..2bc9cad 100644 --- a/hobgoblin/src/output/lang/rust/ast.hob +++ b/hobgoblin/src/output/lang/rust/ast.hob @@ -115,6 +115,7 @@ struct RustExpressionStatement { struct RustLetStatement { field variableName: RustIdentifier; + field mutable: Bool; field value: RustExpression; } 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 19b194f..6febdde 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 @@ -300,6 +300,11 @@ public class RustWriter implements AutoCloseable { private void writeLetStatement(RustLetStatement letStatement) throws IOException { this.writer.write("let "); + + if (letStatement.mutable()) { + this.writer.write("mut "); + } + this.writeIdentifier(letStatement.variableName()); this.writer.write(" = "); this.writeExpression(letStatement.value()); 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 index 810218f..876351f 100644 --- 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 @@ -5,30 +5,34 @@ 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 record RustLetStatement(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIdentifier variableName, boolean mutable, 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()); + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLetStatement.Builder(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIdentifier.arbitrary().build(), false, 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 record Builder(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIdentifier variableName, boolean mutable, 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); + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLetStatement(variableName, mutable, 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); + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLetStatement.Builder(variableName, mutable, 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); + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLetStatement.Builder(variableName.build(), mutable, value); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLetStatement.Builder withMutable(boolean mutable) { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLetStatement.Builder(variableName, mutable, 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); + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLetStatement.Builder(variableName, mutable, 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()); + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLetStatement.Builder(variableName, mutable, value.build()); } // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLetStatement.Builder 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 e4cee1e..c17d96f 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 @@ -367,16 +367,29 @@ public class RustWriterTests { // === Let statements === @Test - public void letStatementDeclaringSingleVariable() throws IOException { + public void letStatementDeclaringSingleImmutableVariable() throws IOException { var rust = RustLetStatement.arbitrary() .withVariableName(RustIdentifier.of("x")) + .withMutable(false) .withValue(RustBoolLiteral.arbitrary().withValue(true).build()) .build(); var string = write(writer -> writer.writeStatement(rust)); assertThat(string, equalTo("let x = true;")); + } + + @Test + public void letStatementDeclaringSingleMutableVariable() throws IOException { + var rust = RustLetStatement.arbitrary() + .withVariableName(RustIdentifier.of("x")) + .withMutable(true) + .withValue(RustBoolLiteral.arbitrary().withValue(true).build()) + .build(); + + var string = write(writer -> writer.writeStatement(rust)); + assertThat(string, equalTo("let mut x = true;")); } // == 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 index c71dd98..b48d485 100644 --- 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 @@ -34,6 +34,12 @@ public class RustLetStatementMatcher implements org.zwobble.precisely.Matcher<ja return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLetStatementMatcher(submatchers); } + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLetStatementMatcher withMutable(org.zwobble.precisely.Matcher<? super java.lang.Boolean> mutable) { + 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("mutable", org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustLetStatement::mutable, mutable)); + 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)); |
