diff options
| author | Michael Williamson <mike@zwobble.org> | 2026-07-26 00:13:52 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2026-07-26 00:13:52 +0100 |
| commit | 35e5a39b044f5916fb528ec1c80d05c05e4389c4 (patch) | |
| tree | 9d970f6aa578f70e4211d59c808049d5adb5b002 | |
| parent | 051ea60dc8a775f0fc82ebbc5f65a8c5f57157c0 (diff) | |
Add less than operator to Rust AST
4 files changed, 18 insertions, 0 deletions
diff --git a/hobgoblin/src/output/lang/rust/ast.hob b/hobgoblin/src/output/lang/rust/ast.hob index bce56f8..2a2bc26 100644 --- a/hobgoblin/src/output/lang/rust/ast.hob +++ b/hobgoblin/src/output/lang/rust/ast.hob @@ -162,6 +162,7 @@ struct RustBinaryExpression { enum RustBinaryOperator { variant add; variant equal; + variant lessThan; variant notEqual; } 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 53b9a38..af6c1a7 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 @@ -443,6 +443,7 @@ public class RustWriter implements AutoCloseable { return switch (operator) { case ADD -> RustPrecedence.ADDITIVE; case EQUAL -> RustPrecedence.COMPARISON; + case LESS_THAN -> RustPrecedence.COMPARISON; case NOT_EQUAL -> RustPrecedence.COMPARISON; }; } @@ -478,6 +479,7 @@ public class RustWriter implements AutoCloseable { this.writer.write(switch (binaryExpression.operator()) { case ADD -> "+"; case EQUAL -> "=="; + case LESS_THAN -> "<"; case NOT_EQUAL -> "!="; }); this.writer.write(" "); @@ -492,6 +494,7 @@ public class RustWriter implements AutoCloseable { return switch (binaryExpression.operator()) { case ADD -> RustAssociativity.LEFT; case EQUAL -> RustAssociativity.NONE; + case LESS_THAN -> RustAssociativity.NONE; case NOT_EQUAL -> RustAssociativity.NONE; }; } diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustBinaryOperator.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustBinaryOperator.java index 1d9fff7..05eca58 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustBinaryOperator.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustBinaryOperator.java @@ -8,6 +8,7 @@ package org.zwobble.hobgoblin.compiler.output.lang.rust.ast; public enum RustBinaryOperator { ADD, EQUAL, + LESS_THAN, NOT_EQUAL // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustBinaryOperator 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 55c8f54..b5dd515 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 @@ -437,6 +437,19 @@ public class RustWriterTests { } @Test + public void binaryExpressionLessThan() throws IOException { + var rust = RustBinaryExpression.arbitrary() + .withOperator(RustBinaryOperator.LESS_THAN) + .withLeft(RustPath.of("x")) + .withRight(RustPath.of("y")) + .build(); + + var string = write(writer -> writer.writeTopLevelExpression(rust)); + + assertThat(string, equalTo("x < y")); + } + + @Test public void binaryExpressionNotEqual() throws IOException { var rust = RustBinaryExpression.arbitrary() .withOperator(RustBinaryOperator.NOT_EQUAL) |
