diff options
| author | Michael Williamson <mike@zwobble.org> | 2026-07-17 17:23:35 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2026-07-17 17:23:35 +0100 |
| commit | e830f34ceb4401ca423740f843835db057ee7411 (patch) | |
| tree | 62b6e6ac25b3fff14dca0d11cc34bf9b88c7c615 /src/main/java/org/zwobble | |
| parent | 001a701103c98581b96e41653d87156de667774d (diff) | |
Add tuple type expression to Rust AST
Diffstat (limited to 'src/main/java/org/zwobble')
3 files changed, 62 insertions, 1 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 4acbb0e..ea3a38b 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 @@ -481,6 +481,10 @@ public class RustWriter implements AutoCloseable { case RustSharedReferenceType sharedReferenceType -> { writeSharedReferenceType(sharedReferenceType); } + + case RustTupleType tupleType -> { + writeTupleType(tupleType); + } } } @@ -499,6 +503,23 @@ public class RustWriter implements AutoCloseable { this.writeType(sharedReferenceType.referencedType()); } + private void writeTupleType(RustTupleType tupleType) throws IOException { + this.writer.write("("); + if (tupleType.elementTypes().size() == 1) { + this.writeType(tupleType.elementTypes().getFirst()); + this.writer.write(","); + } else if (tupleType.elementTypes().size() > 1) { + writeWithSeparator( + tupleType.elementTypes(), + this::writeType, + () -> { + this.writer.write(", "); + } + ); + } + this.writer.write(")"); + } + // == Paths == private void writePath(RustPath path) throws IOException { diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustTupleType.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustTupleType.java new file mode 100644 index 0000000..9948f78 --- /dev/null +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustTupleType.java @@ -0,0 +1,40 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.output.lang.rust.ast; + +// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustTupleType imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustTupleType imports + +public record RustTupleType(java.util.List<org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustType> elementTypes) implements org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustType { + public static org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustTupleType.Builder arbitrary() { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustTupleType.Builder(java.util.List.of()); + } + + public record Builder(java.util.List<org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustType> elementTypes) implements org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustType.Builder { + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustTupleType build() { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustTupleType(elementTypes); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustTupleType.Builder withElementTypes(java.util.List<org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustType> elementTypes) { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustTupleType.Builder(elementTypes); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustTupleType.Builder addElementType(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustType elementType) { + var elementTypes = new java.util.ArrayList<org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustType>(this.elementTypes); + elementTypes.add(elementType); + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustTupleType.Builder(elementTypes); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustTupleType.Builder addElementType(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustType.Builder elementType) { + var elementTypes = new java.util.ArrayList<org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustType>(this.elementTypes); + elementTypes.add(elementType.build()); + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustTupleType.Builder(elementTypes); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustTupleType.Builder body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustTupleType.Builder body + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustTupleType body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustTupleType body +} diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustType.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustType.java index 9c797ac..6e13102 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustType.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustType.java @@ -5,7 +5,7 @@ package org.zwobble.hobgoblin.compiler.output.lang.rust.ast; // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustType imports // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustType imports -public sealed interface RustType permits org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustImplTraitType, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMutableReferenceType, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPath, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustSharedReferenceType { +public sealed interface RustType permits org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustImplTraitType, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustMutableReferenceType, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPath, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustSharedReferenceType, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustTupleType { public interface Builder { public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustType build(); |
