diff options
| author | Michael Williamson <mike@zwobble.org> | 2026-07-17 10:42:38 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2026-07-17 10:42:38 +0100 |
| commit | 10647973b0eca00371f32bd7a4d4db700fe1fc32 (patch) | |
| tree | 5250fa10c69cf58c66f0c2b0dfe778640dc3f2b4 /src/main | |
| parent | 59db18c7d8ea0fdce8ff25eef5a13e59e1af4c58 (diff) | |
Extract generate{Encode,Decode}IntFunction
Diffstat (limited to 'src/main')
2 files changed, 42 insertions, 27 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 46ebb20..9e4ae50 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 @@ -105,8 +105,38 @@ public class RustTransient0Generator implements Generator { } private RustItem generateEncodeInt32Function() { + return generateEncodeIntFunction(NativeTypes.INT_32); + } + + private RustItem generateDecodeInt32Function() { + return generateDecodeIntFunction(NativeTypes.INT_32, 32); + } + + private RustItem generateEncodeInt64Function() { + return generateEncodeFunction( + NativeTypes.INT_64, + new RustBlockExpression( + List.of( + new RustExpressionStatement(generateTodo()) + ), + Optional.empty() + ) + ); + } + + private RustItem generateDecodeInt64Function() { + return generateDecodeFunction( + NativeTypes.INT_64, + new RustBlockExpression( + List.of(), + Optional.of(generateTodo()) + ) + ); + } + + private RustItem generateEncodeIntFunction(SimpleNativeType type) { return generateEncodeFunction( - NativeTypes.INT_32, + type, new RustBlockExpression( List.of( new RustExpressionStatement( @@ -127,49 +157,28 @@ public class RustTransient0Generator implements Generator { ); } - private RustItem generateDecodeInt32Function() { + private RustItem generateDecodeIntFunction(SimpleNativeType type, int bits) { var bytes = RustIdentifier.of("bytes"); return generateDecodeFunction( - NativeTypes.INT_32, + type, new RustBlockExpression( List.of( new RustLetStatement( bytes, false, - generateReaderReadExact(4) + generateReaderReadExact(bits / 8) ) ), Optional.of(new RustCallExpression( - RustPath.of("i32", "from_le_bytes"), + this.rustGenerator.generateRustTypeExpression(type) + .addSegment(RustPathSegment.of(RustIdentifier.of("from_le_bytes"))), List.of(RustPath.of(bytes)) )) ) ); } - private RustItem generateEncodeInt64Function() { - return generateEncodeFunction( - NativeTypes.INT_64, - new RustBlockExpression( - List.of( - new RustExpressionStatement(generateTodo()) - ), - Optional.empty() - ) - ); - } - - private RustItem generateDecodeInt64Function() { - return generateDecodeFunction( - NativeTypes.INT_64, - new RustBlockExpression( - List.of(), - Optional.of(generateTodo()) - ) - ); - } - private RustItem generateEncodeStringFunction() { return generateEncodeFunction( NativeTypes.STRING, diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustPath.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustPath.java index 137df41..8390293 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustPath.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustPath.java @@ -115,6 +115,12 @@ public record RustPath(java.util.List<org.zwobble.hobgoblin.compiler.output.lang return new RustPath(segments); } + public RustExpression addSegment(RustPathSegment segment) { + var segments = new ArrayList<>(this.segments); + segments.add(segment); + return new RustPath(segments); + } + public RustPath withArgs(List<RustType> args) { var segments = new ArrayList<>(this.segments); var lastSegment = segments.removeLast().withArgs(args); |
