diff options
Diffstat (limited to 'src/main')
| -rw-r--r-- | src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/RustWriter.java | 124 |
1 files changed, 67 insertions, 57 deletions
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 b732390..6cd5860 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 @@ -73,6 +73,70 @@ public class RustWriter implements AutoCloseable { } } + // === Functions === + + private void writeFunction(RustFunction function) throws IOException { + this.writer.write("pub fn "); + this.writeIdentifier(function.name()); + this.writer.write("("); + + writeWithSeparator( + function.params(), + param -> { + this.writeIdentifier(param.name()); + this.writer.write(": "); + this.writeType(param.type()); + }, + () -> { + this.writer.write(", "); + } + ); + + this.writer.write(")"); + + if (function.returnType().isPresent()) { + this.writer.write(" "); + this.writer.write("-> "); + this.writeType(function.returnType().get()); + } + + if (function.body().isPresent()) { + this.writer.write(" "); + this.writeBlockExpression(function.body().get()); + } else { + this.writer.write(";"); + } + } + + // === Structs === + + private void writeStructStruct(RustStructStruct structStruct) throws IOException { + this.writeDocComment(structStruct.docComment()); + this.writer.write("pub struct "); + this.writeIdentifier(structStruct.name()); + this.writer.write(" {"); + + if (!structStruct.fields().isEmpty()) { + this.writer.indent(); + + for (var field : structStruct.fields()) { + this.writer.newLine(); + this.writer.write("pub "); + this.writeIdentifier(field.name()); + this.writer.write(": "); + this.writeType(field.type()); + this.writer.write(","); + } + + this.writer.dedent(); + } + + this.writer.newLine(); + this.writer.write("}"); + } + + // === Enumerations === + private void writeEnum(RustEnum enum_) throws IOException { this.writeDocComment(enum_.docComment()); this.writer.write("pub enum "); @@ -112,38 +176,7 @@ public class RustWriter implements AutoCloseable { this.writer.write("}"); } - private void writeFunction(RustFunction function) throws IOException { - this.writer.write("pub fn "); - this.writeIdentifier(function.name()); - this.writer.write("("); - - writeWithSeparator( - function.params(), - param -> { - this.writeIdentifier(param.name()); - this.writer.write(": "); - this.writeType(param.type()); - }, - () -> { - this.writer.write(", "); - } - ); - - this.writer.write(")"); - - if (function.returnType().isPresent()) { - this.writer.write(" "); - this.writer.write("-> "); - this.writeType(function.returnType().get()); - } - - if (function.body().isPresent()) { - this.writer.write(" "); - this.writeBlockExpression(function.body().get()); - } else { - this.writer.write(";"); - } - } + // === Implementations === private void writeInherentImpl(RustInherentImpl inherentImpl) throws IOException { this.writer.write("impl "); @@ -173,6 +206,8 @@ public class RustWriter implements AutoCloseable { this.writer.write("}"); } + // === Associated items === + void writeAssociatedItem(RustAssociatedItem item) throws IOException { switch (item) { case RustFunction function -> { @@ -181,31 +216,6 @@ public class RustWriter implements AutoCloseable { } } - private void writeStructStruct(RustStructStruct structStruct) throws IOException { - this.writeDocComment(structStruct.docComment()); - this.writer.write("pub struct "); - this.writeIdentifier(structStruct.name()); - this.writer.write(" {"); - - if (!structStruct.fields().isEmpty()) { - this.writer.indent(); - - for (var field : structStruct.fields()) { - this.writer.newLine(); - this.writer.write("pub "); - this.writeIdentifier(field.name()); - this.writer.write(": "); - this.writeType(field.type()); - this.writer.write(","); - } - - this.writer.dedent(); - } - - this.writer.newLine(); - this.writer.write("}"); - } - // == Statements == void writeStatement(RustStatement statement) throws IOException { |
