diff options
Diffstat (limited to 'src/main/java')
4 files changed, 108 insertions, 6 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 7ab09c7..60d7d60 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 @@ -111,14 +111,50 @@ public class RustWriter implements AutoCloseable { this.writer.write("}"); } + private void writeFunction(RustFunction function) throws IOException { + this.writer.write("fn "); + this.writeIdentifier(function.name()); + this.writer.write("() {"); + this.writer.newLine(); + this.writer.write("}"); + } + private void writeInherentImpl(RustInherentImpl inherentImpl) throws IOException { this.writer.write("impl "); this.writeType(inherentImpl.type()); this.writer.write(" {"); + + if (!inherentImpl.items().isEmpty()) { + this.writer.indent(); + this.writer.newLine(); + + writeWithSeparator( + inherentImpl.items(), + item -> { + + this.writeAssociatedItem(item); + }, + () -> { + this.writer.newLine(); + this.writer.newLine(); + } + ); + + this.writer.dedent(); + } + this.writer.newLine(); this.writer.write("}"); } + private void writeAssociatedItem(RustAssociatedItem item) throws IOException { + switch (item) { + case RustFunction function -> { + writeFunction(function); + } + } + } + private void writeStructStruct(RustStructStruct structStruct) throws IOException { this.writeDocComment(structStruct.docComment()); this.writer.write("pub struct "); diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustAssociatedItem.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustAssociatedItem.java new file mode 100644 index 0000000..7334441 --- /dev/null +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustAssociatedItem.java @@ -0,0 +1,18 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.output.lang.rust.ast; + +// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustAssociatedItem imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustAssociatedItem imports + +public sealed interface RustAssociatedItem permits org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustFunction { + public interface Builder { + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustAssociatedItem build(); + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustAssociatedItem.Builder body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustAssociatedItem.Builder body + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustAssociatedItem body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustAssociatedItem body +} diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustFunction.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustFunction.java new file mode 100644 index 0000000..aea16f0 --- /dev/null +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustFunction.java @@ -0,0 +1,32 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.output.lang.rust.ast; + +// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustFunction imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustFunction imports + +public record RustFunction(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIdentifier name) implements org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustAssociatedItem { + public static org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustFunction.Builder arbitrary() { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustFunction.Builder(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIdentifier.arbitrary().build()); + } + + public record Builder(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIdentifier name) implements org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustAssociatedItem.Builder { + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustFunction build() { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustFunction(name); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustFunction.Builder withName(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIdentifier name) { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustFunction.Builder(name); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustFunction.Builder withName(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIdentifier.Builder name) { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustFunction.Builder(name.build()); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustFunction.Builder body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustFunction.Builder body + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustFunction body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustFunction body +} diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustInherentImpl.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustInherentImpl.java index 55447bb..161dd5d 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustInherentImpl.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustInherentImpl.java @@ -5,22 +5,38 @@ package org.zwobble.hobgoblin.compiler.output.lang.rust.ast; // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustInherentImpl imports // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustInherentImpl imports -public record RustInherentImpl(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustType type) implements org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustItem { +public record RustInherentImpl(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustType type, java.util.List<org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustAssociatedItem> items) implements org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustItem { public static org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustInherentImpl.Builder arbitrary() { - return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustInherentImpl.Builder(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustTypePath.arbitrary().build()); + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustInherentImpl.Builder(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustTypePath.arbitrary().build(), java.util.List.of()); } - public record Builder(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustType type) implements org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustItem.Builder { + public record Builder(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustType type, java.util.List<org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustAssociatedItem> items) implements org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustItem.Builder { public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustInherentImpl build() { - return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustInherentImpl(type); + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustInherentImpl(type, items); } public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustInherentImpl.Builder withType(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustType type) { - return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustInherentImpl.Builder(type); + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustInherentImpl.Builder(type, items); } public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustInherentImpl.Builder withType(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustType.Builder type) { - return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustInherentImpl.Builder(type.build()); + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustInherentImpl.Builder(type.build(), items); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustInherentImpl.Builder withItems(java.util.List<org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustAssociatedItem> items) { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustInherentImpl.Builder(type, items); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustInherentImpl.Builder addItem(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustAssociatedItem item) { + var items = new java.util.ArrayList<org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustAssociatedItem>(this.items); + items.add(item); + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustInherentImpl.Builder(type, items); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustInherentImpl.Builder addItem(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustAssociatedItem.Builder item) { + var items = new java.util.ArrayList<org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustAssociatedItem>(this.items); + items.add(item.build()); + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustInherentImpl.Builder(type, items); } // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustInherentImpl.Builder body |
