Class impl

This commit is contained in:
2026-05-10 17:35:33 +02:00
parent bb9ad547a3
commit 28d4d784f2
4 changed files with 53 additions and 9 deletions
+6
View File
@@ -58,4 +58,10 @@ public class Logger {
} }
} }
} }
impl fmt::Display {
std::fmt::Result fmt(self*, std::fmt::Formatter<'_> mut* f) {
return write!(f, "logger ({})", self.prefix);
}
}
} }
+7 -1
View File
@@ -94,10 +94,16 @@ pub enum TopLevelKind {
generics: Generics, generics: Generics,
fields: Vec<FieldDeclStmt>, fields: Vec<FieldDeclStmt>,
constructor: ClassConstructor, constructor: ClassConstructor,
items: Vec<FunctionDecl>, items: Vec<ClassItem>,
}, },
} }
#[derive(Debug, Clone, Serialize)]
pub enum ClassItem {
Method(FunctionDecl),
ImplDecl(ImplDecl),
}
#[derive(Debug, Clone, Serialize, Default)] #[derive(Debug, Clone, Serialize, Default)]
pub struct Generics(pub Vec<Generic>); pub struct Generics(pub Vec<Generic>);
+15 -1
View File
@@ -289,7 +289,7 @@ impl From<pest::iterators::Pair<'_, Rule>> for TopLevelKind {
.map(FieldDeclStmt::from) .map(FieldDeclStmt::from)
.collect(), .collect(),
constructor: ClassConstructor::from(inner.next().unwrap()), constructor: ClassConstructor::from(inner.next().unwrap()),
items: inner.into_iter().map(FunctionDecl::from).collect(), items: inner.into_iter().map(ClassItem::from).collect(),
}, },
Rule::enum_decl => TopLevelKind::EnumDecl { Rule::enum_decl => TopLevelKind::EnumDecl {
@@ -310,6 +310,20 @@ impl From<pest::iterators::Pair<'_, Rule>> for TopLevelKind {
} }
} }
impl From<pest::iterators::Pair<'_, Rule>> for ClassItem {
fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self {
let rule = pair.as_rule();
match rule {
Rule::impl_decl | Rule::impl_for_decl => ClassItem::ImplDecl(ImplDecl::from(pair)),
Rule::method => ClassItem::Method(FunctionDecl::from(pair)),
_ => unimplemented!("{rule:#?}"),
}
}
}
impl From<pest::iterators::Pair<'_, Rule>> for ImplDecl { impl From<pest::iterators::Pair<'_, Rule>> for ImplDecl {
fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self { fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self {
let rule = pair.as_rule(); let rule = pair.as_rule();
+25 -7
View File
@@ -1,8 +1,8 @@
use parser::ast::{ use parser::ast::{
Attribute, BinaryOp, Block, EnumItem, Expression, FieldDecl, FunctionDecl, Generic, Generics, Attribute, BinaryOp, Block, ClassItem, EnumItem, Expression, FieldDecl, FunctionDecl, Generic,
Identifier, ImplDecl, Literal, Path, Pattern, Postfix, Prefix, Statement, StatementBranch, Generics, Identifier, ImplDecl, Literal, Path, Pattern, Postfix, Prefix, Statement,
TopLevel, TopLevelKind, TypeExpr, TypeExprKind, TypePostfix, VarAssignStmt, VarDecl, StatementBranch, TopLevel, TopLevelKind, TypeExpr, TypeExprKind, TypePostfix, VarAssignStmt,
VarDeclStmt, Visibility, VarDecl, VarDeclStmt, Visibility,
}; };
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
@@ -364,7 +364,7 @@ impl ToRust for TopLevelKind {
generics, generics,
fields, fields,
constructor, constructor,
items: methods, items,
} => { } => {
// Struct decl // Struct decl
cg.addln(&format!( cg.addln(&format!(
@@ -458,12 +458,30 @@ impl ToRust for TopLevelKind {
cg.indent -= 1; cg.indent -= 1;
cg.add_indentedln("}\n"); cg.add_indentedln("}\n");
for method in methods { for item in items {
method.to_rust(cg); match item {
ClassItem::ImplDecl(_) => {}
ClassItem::Method(method) => method.to_rust(cg),
}
} }
cg.indent -= 1; cg.indent -= 1;
cg.addln("}\n"); cg.addln("}\n");
for item in items {
match item {
ClassItem::ImplDecl(impl_) => {
let mut impl_ = impl_.clone();
impl_.trait_ = Some(impl_.target);
impl_.target =
TypeExpr(TypeExprKind::Path(Path(vec![name.clone()])), Vec::new());
impl_.to_rust(cg);
}
ClassItem::Method(_) => {}
}
}
} }
} }
} }