diff options
Diffstat (limited to 'src/main/java')
| -rw-r--r-- | src/main/java/org/zwobble/hobgoblin/compiler/output/generators/rusttransient0/RustTransient0Generator.java | 68 |
1 files changed, 66 insertions, 2 deletions
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 6d24c0a..a17ae6b 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 @@ -403,7 +403,11 @@ public class RustTransient0Generator implements Generator { private List<RustStatement> generateEncode(RustExpression value, Type type) { return switch (type) { case ConstructedNativeType constructedNativeType -> { - yield List.of(new RustExpressionStatement(generateTodo())); + if (constructedNativeType.constructor().equals(NativeTypes.LIST)) { + yield generateEncodeList(value, constructedNativeType.args().getFirst()); + } else { + yield List.of(new RustExpressionStatement(generateTodo())); + } } case EnumType enumType -> { @@ -435,7 +439,11 @@ public class RustTransient0Generator implements Generator { private RustExpression generateDecode(Type type) { return switch (type) { case ConstructedNativeType constructedNativeType -> { - yield generateTodo(); + if (constructedNativeType.constructor().equals(NativeTypes.LIST)) { + yield generateDecodeList(constructedNativeType.args().getFirst()); + } else { + yield generateTodo(); + } } case EnumType enumType -> { @@ -496,6 +504,62 @@ public class RustTransient0Generator implements Generator { )); } + private List<RustStatement> generateEncodeList(RustExpression value, Type elementType) { + var element = RustIdentifier.of("element"); + + return List.of( + new RustExpressionStatement(new RustBlockExpression( + generateEncode( + new RustPrefixExpression( + RustPrefixOperator.BORROW, + tryIntoOrUnwrap(methodCall(value, RustIdentifier.of("len"), List.of())) + ), + NativeTypes.INT_64 + ), + Optional.empty() + )), + new RustExpressionStatement(new RustIteratorLoopExpression( + element, + value, + new RustBlockExpression( + generateEncode(RustPath.of(element), elementType), + Optional.empty() + ) + )) + ); + } + + private RustExpression generateDecodeList(Type elementType) { + var length = RustIdentifier.of("len"); + var elements = RustIdentifier.of("elements"); + + return new RustBlockExpression( + List.of( + new RustLetStatement(length, false, generateDecode(NativeTypes.INT_64)), + new RustLetStatement(elements, true, new RustCallExpression( + RustPath.global("std", "vec", "Vec", "with_capacity"), + List.of(tryIntoOrUnwrap(RustPath.of(length))) + )), + new RustExpressionStatement(new RustIteratorLoopExpression( + // TODO: remove unnecessary variable + RustIdentifier.of("element_index"), + new RustRangeExpr(new RustIntegerLiteral(0, Optional.empty()), RustPath.of(length)), + new RustBlockExpression( + List.of(new RustExpressionStatement( + methodCall( + RustPath.of(elements), + RustIdentifier.of("push"), + List.of(generateDecode(elementType)) + ) + )), + Optional.empty() + ) + )) + ), + Optional.of(RustPath.of(elements)) + ); + } + private RustExpression generateWriterWrite(RustExpression valueToWrite) { return new RustTryPropagationExpression(new RustCallExpression( new RustFieldExpression( |
