Updated type codegen #65

This commit is contained in:
2026-06-05 17:34:06 +02:00
parent 143d496ee8
commit b559215212
2 changed files with 51 additions and 50 deletions
+41 -41
View File
@@ -156,18 +156,6 @@ impl GetRust for Path {
}
}
impl GetRust for TypePostfix {
fn get_rust(&self) -> String {
match self {
TypePostfix::Ref => format!("&"),
TypePostfix::RefMut => format!("&mut "),
TypePostfix::RefLifetime(lifetime) => format!("&'{} ", lifetime.get_rust()),
TypePostfix::RefMutLifetime(lifetime) => format!("&'{} mut ", lifetime.get_rust()),
TypePostfix::Dyn => format!("dyn "),
}
}
}
impl GetRust for Visibility {
fn get_rust(&self) -> String {
match self {
@@ -186,12 +174,47 @@ impl GetRust for Identifier {
impl GetRust for TypeExpr {
fn get_rust(&self) -> String {
self.1
.iter()
.map(TypePostfix::get_rust)
.rev()
.collect::<String>()
+ &self.0.get_rust()
match self {
// TypePostfix::Ref => format!("&"),
// TypePostfix::RefMut => format!("&mut "),
// TypePostfix::RefLifetime(lifetime) => format!("&'{} ", lifetime.get_rust()),
// TypePostfix::RefMutLifetime(lifetime) => format!("&'{} mut ", lifetime.get_rust()),
// TypePostfix::Dyn => format!("dyn "),
Self::Path(path, generics) => {
if let Some(generics) = generics {
format!("{}{}", get_static_type_path(path), generics.get_rust())
} else {
get_static_type_path(path)
}
}
Self::Lifetime(name) => format!("'{}", name.get_rust()),
Self::Tuple(types) => format!(
"({})",
types
.into_iter()
.map(|t| t.get_rust())
.collect::<Vec<_>>()
.join(", ")
),
Self::Ref {
lifetime,
mutable,
ty,
} => {
let mutable = if *mutable { "mut " } else { "" };
if let Some(lifetime) = lifetime {
format!("&{}{mutable}{}", lifetime.get_rust(), ty.get_rust())
} else {
format!("&{mutable}{}", ty.get_rust())
}
}
Self::Dyn(ty) => {
format!("dyn {}", ty.get_rust())
}
}
}
}
@@ -205,29 +228,6 @@ pub fn get_static_type_path(path: &Path) -> String {
}
}
impl GetRust for TypeExprKind {
fn get_rust(&self) -> String {
match self {
TypeExprKind::Path(path, generics) => {
if let Some(generics) = generics {
format!("{}{}", get_static_type_path(path), generics.get_rust())
} else {
get_static_type_path(path)
}
}
TypeExprKind::Lifetime(name) => format!("'{}", name.get_rust()),
TypeExprKind::Tuple(types) => format!(
"({})",
types
.into_iter()
.map(|t| t.get_rust())
.collect::<Vec<_>>()
.join(", ")
),
}
}
}
impl GenRust for Pattern {
fn gen_rust(&self, ctx: &mut Context, cg: &mut RustCodegen) {
match self {
+9 -8
View File
@@ -234,10 +234,14 @@ impl GenRust for (&Vec<Spanned<FieldDeclStmt>>, &Spanned<ClassConstructor>) {
let mut constructor_params = vec![VarDecl {
name: Pattern::Path(false, Path(vec![Identifier(String::from("self"))])),
type_: Some(TypeExpr(
TypeExprKind::Path(Path(vec![Identifier(String::from("Self"))]), None),
vec![TypePostfix::RefMut],
type_: Some(TypeExpr::Ref {
lifetime: None,
mutable: true,
ty: Box::new(TypeExpr::Path(
Path(vec![Identifier(String::from("Self"))]),
None,
)),
}),
}];
constructor_params.append(&mut self.1.item.params.0.clone());
@@ -250,7 +254,7 @@ impl GenRust for (&Vec<Spanned<FieldDeclStmt>>, &Spanned<ClassConstructor>) {
name: Identifier(String::from("constructor")),
generics: self.1.item.generics.clone(),
params: ParamList(constructor_params),
return_type: TypeExpr::no_px(TypeExprKind::Tuple(Vec::new())),
return_type: TypeExpr::Tuple(Vec::new()),
body: Some(self.1.item.body.clone()),
},
}
@@ -423,10 +427,7 @@ impl GenRust for TopLevelKind {
let mut impl_ = impl_.clone();
impl_.item.trait_ = Some(impl_.item.target);
impl_.item.target = TypeExpr(
TypeExprKind::Path(Path(vec![name.clone()]), None),
Vec::new(),
);
impl_.item.target = TypeExpr::Path(Path(vec![name.clone()]), None);
impl_.gen_rust(ctx, cg);
}