diff options
| author | Michael Williamson <mike@zwobble.org> | 2026-07-25 23:48:53 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2026-07-25 23:48:53 +0100 |
| commit | 051ea60dc8a775f0fc82ebbc5f65a8c5f57157c0 (patch) | |
| tree | 9dbf7cb23dddcb51ea6dbec56bbbe7cf060033a8 | |
| parent | 8f5993f7dc213f8130204c3ea6facd814373fb3d (diff) | |
Add type cases to Rust AST
6 files changed, 117 insertions, 1 deletions
diff --git a/hobgoblin/src/output/lang/rust/ast.hob b/hobgoblin/src/output/lang/rust/ast.hob index a592f4e..bce56f8 100644 --- a/hobgoblin/src/output/lang/rust/ast.hob +++ b/hobgoblin/src/output/lang/rust/ast.hob @@ -139,6 +139,7 @@ sum RustExpression { variant RustStructExpression; variant RustTryPropagationExpression; variant RustTupleExpression; + variant RustTypeCastExpression; variant RustVecRepeatExpression; } @@ -249,6 +250,11 @@ struct RustTupleExpression { field elements: List[RustExpression]; } +struct RustTypeCastExpression { + field left: RustExpression; + field right: RustType; +} + struct RustVecRepeatExpression { field repeatOperand: RustExpression; field lengthOperand: 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 aa8d6cb..53b9a38 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 @@ -383,6 +383,10 @@ public class RustWriter implements AutoCloseable { this.writeTupleExpression(tupleExpression); } + case RustTypeCastExpression typeCastExpression -> { + this.writeTypeCastExpression(typeCastExpression); + } + case RustVecRepeatExpression vecRepeatExpression -> { this.writeVecRepeatExpression(vecRepeatExpression); } @@ -430,6 +434,7 @@ public class RustWriter implements AutoCloseable { case RustStructExpression _ -> RustPrecedence.PRIMARY; case RustTryPropagationExpression _ -> RustPrecedence.TRY_PROPAGATION; case RustTupleExpression _ -> RustPrecedence.PRIMARY; + case RustTypeCastExpression _ -> RustPrecedence.AS; case RustVecRepeatExpression _ -> RustPrecedence.PRIMARY; }; } @@ -668,6 +673,12 @@ public class RustWriter implements AutoCloseable { this.writer.write(")"); } + private void writeTypeCastExpression(RustTypeCastExpression typeCastExpression) throws IOException { + this.writeSubExpression(typeCastExpression.left(), RustPrecedence.AS, true); + this.writer.write(" as "); + this.writeType(typeCastExpression.right()); + } + private void writeVecRepeatExpression(RustVecRepeatExpression vecRepeatExpression) throws IOException { this.writer.write("vec