From e4ac6b36d3c633b3ec943ec279b82f79508b0f06 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 18 Jul 2026 10:27:39 +0100 Subject: Support Bool values in rust-transient-0 --- .../rusttransient0/RustTransient0Generator.java | 114 +++++++++++++++------ 1 file changed, 84 insertions(+), 30 deletions(-) (limited to 'src/main') diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/rusttransient0/RustTransient0Generator.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/rusttransient0/RustTransient0Generator.java index d96130b..5639ca2 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/rusttransient0/RustTransient0Generator.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/rusttransient0/RustTransient0Generator.java @@ -84,17 +84,60 @@ public class RustTransient0Generator implements Generator { return generateEncodeFunction( NativeTypes.BOOL, List.of( - new RustExpressionStatement(generateTodo()) + new RustExpressionStatement(new RustIfExpression( + new RustPrefixExpression( + RustPrefixOperator.DEREFERENCE, + RustPath.of(VALUE_NAME) + ), + new RustBlockExpression( + List.of( + generateWriterWriteInt( + new RustIntegerLiteral(1, Optional.of(RustIdentifier.of("u8"))) + ) + ), + Optional.empty() + ), + new RustBlockExpression( + List.of( + generateWriterWriteInt( + new RustIntegerLiteral(0, Optional.of(RustIdentifier.of("u8"))) + ) + ), + Optional.empty() + ) + )) ) ); } private RustItem generateDecodeBoolFunction() { + var intValue = RustIdentifier.of("int_value"); + return generateDecodeFunction( NativeTypes.BOOL, new RustBlockExpression( - List.of(), - Optional.of(generateTodo()) + List.of( + new RustLetStatement( + intValue, + false, + generateReaderReadInt(RustPath.of("u8"), 8) + ) + ), + Optional.of(new RustIfExpression( + new RustBinaryExpression( + RustBinaryOperator.EQUAL, + RustPath.of(intValue), + new RustIntegerLiteral(0, Optional.empty()) + ), + new RustBlockExpression( + List.of(), + Optional.of(new RustBoolLiteral(false)) + ), + new RustBlockExpression( + List.of(), + Optional.of(new RustBoolLiteral(true)) + ) + )) ) ); } @@ -119,44 +162,55 @@ public class RustTransient0Generator implements Generator { return generateEncodeFunction( type, List.of( - new RustExpressionStatement( - generateWriterWrite(new RustPrefixExpression( - RustPrefixOperator.BORROW, - new RustCallExpression( - new RustFieldExpression( - RustPath.of(VALUE_NAME), - RustIdentifier.of("to_le_bytes") - ), - List.of() - ) - )) - ) + generateWriterWriteInt(RustPath.of(VALUE_NAME)) ) ); } private RustItem generateDecodeIntFunction(SimpleNativeType type, int bits) { - var bytes = RustIdentifier.of("bytes"); - return generateDecodeFunction( type, - new RustBlockExpression( - List.of( - new RustLetStatement( - bytes, - false, - generateReaderReadExact(bits / 8) - ) - ), - Optional.of(new RustCallExpression( - this.rustGenerator.generateRustTypeExpression(type) - .addSegment(RustPathSegment.of(RustIdentifier.of("from_le_bytes"))), - List.of(RustPath.of(bytes)) - )) + generateReaderReadInt( + this.rustGenerator.generateRustTypeExpression(type), + bits ) ); } + private RustStatement generateWriterWriteInt(RustExpression intExpression) { + return new RustExpressionStatement( + generateWriterWrite(new RustPrefixExpression( + RustPrefixOperator.BORROW, + new RustCallExpression( + new RustFieldExpression( + intExpression, + RustIdentifier.of("to_le_bytes") + ), + List.of() + ) + )) + ); + } + + private RustBlockExpression generateReaderReadInt(RustPath type, int bits) { + var bytes = RustIdentifier.of("bytes"); + + return new RustBlockExpression( + List.of( + new RustLetStatement( + bytes, + false, + generateReaderReadExact(bits / 8) + ) + ), + Optional.of(new RustCallExpression( + type + .addSegment(RustPathSegment.of(RustIdentifier.of("from_le_bytes"))), + List.of(RustPath.of(bytes)) + )) + ); + } + private RustItem generateEncodeStringFunction() { return generateEncodeFunction( NativeTypes.STRING, -- cgit v1.2.3