diff options
Diffstat (limited to 'src/main/java/org/zwobble')
2 files changed, 191 insertions, 41 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 d67ccd6..46ebb20 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 @@ -8,6 +8,8 @@ import org.zwobble.hobgoblin.compiler.output.generators.rust.RustGenerator; import org.zwobble.hobgoblin.compiler.output.generators.rust.RustGeneratorConfig; import org.zwobble.hobgoblin.compiler.output.lang.rust.ast.*; import org.zwobble.hobgoblin.compiler.typechecker.TypesInfo; +import org.zwobble.hobgoblin.compiler.types.NamespaceName; +import org.zwobble.hobgoblin.compiler.types.SimpleNativeType; import org.zwobble.hobgoblin.compiler.types.Type; import org.zwobble.json5.reader.Json5ObjectReader; @@ -52,6 +54,8 @@ public class RustTransient0Generator implements Generator { @Override public void generate(List<TypedNamespaceNode> namespaces, TypesInfo typesInfo) throws IOException { + generateBuiltins(); + for (var namespace : namespaces) { var rustModule = generateNamespace(namespace); @@ -59,9 +63,138 @@ public class RustTransient0Generator implements Generator { } } + private void generateBuiltins() throws IOException { + var rustModuleName = this.generateTransient0ModuleName(NamespaceName.of()); + var rustModule = new RustModule( + rustModuleName, + List.of( + generateEncodeBoolFunction(), + generateDecodeBoolFunction(), + generateEncodeInt32Function(), + generateDecodeInt32Function(), + generateEncodeInt64Function(), + generateDecodeInt64Function(), + generateEncodeStringFunction(), + generateDecodeStringFunction() + ) + ); + + this.rustGenerator.write(rustModule); + } + + private RustItem generateEncodeBoolFunction() { + return generateEncodeFunction( + NativeTypes.BOOL, + new RustBlockExpression( + List.of( + new RustExpressionStatement(generateTodo()) + ), + Optional.empty() + ) + ); + } + + private RustItem generateDecodeBoolFunction() { + return generateDecodeFunction( + NativeTypes.BOOL, + new RustBlockExpression( + List.of(), + Optional.of(generateTodo()) + ) + ); + } + + private RustItem generateEncodeInt32Function() { + return generateEncodeFunction( + NativeTypes.INT_32, + new RustBlockExpression( + List.of( + new RustExpressionStatement( + generateWriterWrite(new RustPrefixExpression( + RustPrefixOperator.BORROW, + new RustCallExpression( + new RustFieldExpression( + RustPath.of(VALUE_NAME), + RustIdentifier.of("to_le_bytes") + ), + List.of() + ) + )) + ) + ), + Optional.empty() + ) + ); + } + + private RustItem generateDecodeInt32Function() { + var bytes = RustIdentifier.of("bytes"); + + return generateDecodeFunction( + NativeTypes.INT_32, + new RustBlockExpression( + List.of( + new RustLetStatement( + bytes, + false, + generateReaderReadExact(4) + ) + ), + Optional.of(new RustCallExpression( + RustPath.of("i32", "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, + new RustBlockExpression( + List.of( + new RustExpressionStatement(generateTodo()) + ), + Optional.empty() + ) + ); + } + + private RustItem generateDecodeStringFunction() { + return generateDecodeFunction( + NativeTypes.STRING, + new RustBlockExpression( + List.of(), + Optional.of(generateTodo()) + ) + ); + } + private RustModule generateNamespace(TypedNamespaceNode namespace) { - var moduleName = new ArrayList<>(this.rustGenerator.namespaceNameToRustCrateModulePath(namespace.namespaceName())); - moduleName.add(RustIdentifier.of("transient_0")); + var namespaceName = namespace.namespaceName(); + var moduleName = generateTransient0ModuleName(namespaceName); return new RustModule( moduleName, @@ -71,6 +204,12 @@ public class RustTransient0Generator implements Generator { ); } + private ArrayList<RustIdentifier> generateTransient0ModuleName(NamespaceName namespaceName) { + var moduleName = new ArrayList<>(this.rustGenerator.namespaceNameToRustCrateModulePath(namespaceName)); + moduleName.add(RustIdentifier.of("transient_0")); + return moduleName; + } + private Stream<RustItem> generateNamespaceStatement( TypedNamespaceStatementNode statement ) { @@ -84,7 +223,6 @@ public class RustTransient0Generator implements Generator { } case TypedStructDefinitionNode structDefinition -> { - // TODO: remove cast var rustType = this.rustGenerator.generateRustTypeExpression(structDefinition.type()); yield Stream.of( @@ -93,9 +231,12 @@ public class RustTransient0Generator implements Generator { new RustBlockExpression( structDefinition.fields().stream() .flatMap(field -> generateEncode( - new RustFieldExpression( - RustPath.of(VALUE_NAME), - this.rustGenerator.generateFieldName(field.name()) + new RustPrefixExpression( + RustPrefixOperator.BORROW, + new RustFieldExpression( + RustPath.of(VALUE_NAME), + this.rustGenerator.generateFieldName(field.name()) + ) ), field.type().value() ).stream()) @@ -169,45 +310,50 @@ public class RustTransient0Generator implements Generator { } private List<RustStatement> generateEncode(RustExpression value, Type type) { - if (type.equals(NativeTypes.INT_32)) { - return List.of( - new RustExpressionStatement( - generateWriterWrite(new RustPrefixExpression( - RustPrefixOperator.BORROW, - new RustCallExpression( - new RustFieldExpression( - value, - RustIdentifier.of("to_le_bytes") - ), - List.of() - ) + return switch (type) { + case SimpleNativeType nativeType -> { + var rustEncodeFunctionPathSegments = new ArrayList<>( + this.generateTransient0ModuleName(nativeType.namespaceName()) + ); + rustEncodeFunctionPathSegments.add(encodeMethodName(nativeType)); + var rustEncodeFunctionPath = RustPath.crate( + rustEncodeFunctionPathSegments + ); + + yield List.of( + new RustExpressionStatement(new RustCallExpression( + rustEncodeFunctionPath, + List.of(value, RustPath.of(WRITER_NAME)) )) - ) - ); - } else { - return List.of(new RustExpressionStatement(generateTodo())); - } + ); + } + default -> { + yield List.of(new RustExpressionStatement(generateTodo())); + } + }; } private RustExpression generateDecode(Type type) { - if (type.equals(NativeTypes.INT_32)) { - var bytes = RustIdentifier.of("bytes"); - return new RustBlockExpression( - List.of( - new RustLetStatement( - bytes, - false, - generateReaderReadExact(4) - ) - ), - Optional.of(new RustCallExpression( - RustPath.of("i32", "from_le_bytes"), - List.of(RustPath.of(bytes)) - )) - ); - } else { - return generateTodo(); - } + return switch (type) { + case SimpleNativeType nativeType -> { + var rustDecodeFunctionPathSegments = new ArrayList<>( + this.generateTransient0ModuleName(nativeType.namespaceName()) + ); + rustDecodeFunctionPathSegments.add(decodeMethodName(nativeType)); + var rustDecodeFunctionPath = RustPath.crate( + rustDecodeFunctionPathSegments + ); + + yield new RustCallExpression( + rustDecodeFunctionPath, + List.of(RustPath.of(READER_NAME)) + ); + } + + default -> { + yield generateTodo(); + } + }; } private RustExpression generateWriterWrite(RustExpression valueToWrite) { 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 315271b..137df41 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 @@ -73,6 +73,10 @@ public record RustPath(java.util.List<org.zwobble.hobgoblin.compiler.output.lang return qualified(RustPathSegment.global(), names); } + public static RustPath crate(List<RustIdentifier> names) { + return qualified(RustPathSegment.crate(), names.toArray(new RustIdentifier[0])); + } + public static RustPath selfType() { return qualified(RustPathSegment.selfType(), new String[0]); } |
