generics codege

This commit is contained in:
2026-05-08 16:54:11 +02:00
parent 205e2d9fd3
commit 0e6c73e85c
2 changed files with 38 additions and 4 deletions
+36 -4
View File
@@ -1,6 +1,6 @@
use parser::ast::{ use parser::ast::{
Attribute, BinaryOp, Block, EnumItem, Expression, FunctionDecl, Identifier, Literal, Path, Attribute, BinaryOp, Block, EnumItem, Expression, FunctionDecl, Generics, Identifier, Literal,
Pattern, Postfix, Prefix, Statement, StatementBranch, TopLevel, TopLevelKind, TypeExpr, Path, Pattern, Postfix, Prefix, Statement, StatementBranch, TopLevel, TopLevelKind, TypeExpr,
TypeExprKind, TypePostfix, VarAssignStmt, VarDecl, VarDeclStmt, Visibility, TypeExprKind, TypePostfix, VarAssignStmt, VarDecl, VarDeclStmt, Visibility,
}; };
@@ -316,12 +316,14 @@ impl ToRust for TopLevelKind {
Self::StructDecl { Self::StructDecl {
visibility, visibility,
name, name,
generics,
fields, fields,
} => { } => {
cg.addln(&format!( cg.addln(&format!(
"{}struct {} {{", "{}struct {}{} {{",
visibility.get_rust(), visibility.get_rust(),
name.get_rust() name.get_rust(),
generics.get_rust()
)); ));
cg.indent += 1; cg.indent += 1;
@@ -703,6 +705,36 @@ impl GetRust for Pattern {
} }
} }
impl GetRust for Generics {
fn get_rust(&self) -> String {
if self.0.len() == 0 {
String::new()
} else {
format!(
"<{}>",
self.0
.iter()
.map(|generic| generic.0.get_rust()
+ &(if generic.1.len() == 0 {
String::new()
} else {
format!(
": {}",
generic
.1
.iter()
.map(Path::get_rust)
.collect::<Vec<_>>()
.join("+")
)
}))
.collect::<Vec<_>>()
.join(", ")
)
}
}
}
pub fn get_static_type_path(path: &Path) -> String { pub fn get_static_type_path(path: &Path) -> String {
let rust_path = path.get_rust(); let rust_path = path.get_rust();
+2
View File
@@ -2,6 +2,8 @@ mod my_class;
use <my_class::MyClass>; use <my_class::MyClass>;
use <my_class::MyEnum>; use <my_class::MyEnum>;
struct Test<T> {}
void main() { void main() {
var my_class = new MyClass("World"); var my_class = new MyClass("World");
my_class.greet(); my_class.greet();