Class method AST gen

This commit is contained in:
2026-05-05 21:44:36 +02:00
parent 4837d39ab7
commit 5b0cb74cee
4 changed files with 92 additions and 8 deletions
+6 -5
View File
@@ -79,6 +79,7 @@ pub enum TopLevelKind {
name: String, name: String,
fields: Vec<VarDeclStmt>, fields: Vec<VarDeclStmt>,
constructor: ClassConstructor, constructor: ClassConstructor,
methods: Vec<ClassMethod>,
}, },
} }
@@ -91,11 +92,11 @@ pub struct ClassConstructor {
#[derive(Debug, Clone, Serialize)] #[derive(Debug, Clone, Serialize)]
pub struct ClassMethod { pub struct ClassMethod {
export: bool, pub export: bool,
name: String, pub name: String,
params: ParamList, pub params: ParamList,
return_type: TypeExpr, pub return_type: TypeExpr,
body: Block, pub body: Block,
} }
#[derive(Debug, Clone, Serialize)] #[derive(Debug, Clone, Serialize)]
+1 -3
View File
@@ -26,8 +26,6 @@ class_constructor = {
class_fields = { var_decl_statement* } class_fields = { var_decl_statement* }
class_methods = { method* }
self_param = { "self" ~ mutable? ~ deref_px? } self_param = { "self" ~ mutable? ~ deref_px? }
method = { method = {
@@ -35,7 +33,7 @@ method = {
} }
class_decl = { class_decl = {
"class" ~ identifier ~ "{" ~ class_fields ~ class_constructor ~ class_methods ~ "}" "class" ~ identifier ~ "{" ~ class_fields ~ class_constructor ~ method* ~ "}"
} }
// ================= TOP LEVEL ================= // ================= TOP LEVEL =================
+83
View File
@@ -320,6 +320,7 @@ impl From<pest::iterators::Pair<'_, Rule>> for TopLevelKind {
.map(VarDeclStmt::from) .map(VarDeclStmt::from)
.collect(), .collect(),
constructor: ClassConstructor::from(inner.next().unwrap()), constructor: ClassConstructor::from(inner.next().unwrap()),
methods: inner.into_iter().map(ClassMethod::from).collect(),
}, },
_ => unimplemented!("{rule:#?}"), _ => unimplemented!("{rule:#?}"),
@@ -594,3 +595,85 @@ impl From<pest::iterators::Pair<'_, Rule>> for VarDecl {
} }
} }
} }
impl From<pest::iterators::Pair<'_, Rule>> for ClassMethod {
fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self {
let mut inner = pair.into_inner();
let export = if let Some(first) = inner.peek() {
if first.as_rule() == Rule::export {
inner.next();
true
} else {
false
}
} else {
false
};
let return_type = TypeExpr::from(inner.next().unwrap());
let name = inner.next().unwrap().as_str().to_string();
let self_param = if inner.peek().unwrap().as_rule() == Rule::self_param {
let mut param = inner.next().unwrap().into_inner();
let name = format!("self");
let mutable = param
.peek()
.map(|p| p.as_rule() == Rule::mutable)
.unwrap_or_default();
if mutable {
param.next();
}
let is_ref = param
.peek()
.map(|p| p.as_rule() == Rule::deref_px)
.unwrap_or_default();
if is_ref {
param.next();
}
Some(VarDecl {
mutable: mutable && !is_ref,
name,
type_: Some(TypeExpr(
TypeExprKind::Path(Path(vec![format!("Self")])),
if is_ref {
vec![if mutable {
TypePostfix::RefMut
} else {
TypePostfix::Ref
}]
} else {
Vec::new()
},
)),
})
} else {
None
};
let params = if inner.peek().unwrap().as_rule() == Rule::param_list {
let mut params = ParamList::from(inner.next().unwrap());
if let Some(x) = self_param {
params.0.insert(0, x);
}
params
} else {
ParamList(self_param.into_iter().collect())
};
let body = Block::from(inner.next().unwrap());
Self {
export,
name,
params,
return_type,
body,
}
}
}
+2
View File
@@ -362,7 +362,9 @@ impl ToRust for TopLevelKind {
name, name,
fields, fields,
constructor, constructor,
methods,
} => { } => {
dbg!(methods);
// Struct decl // Struct decl
let vis = if *export { "pub " } else { "" }; let vis = if *export { "pub " } else { "" };
cg.addln(&format!("{}struct {} {{", vis, name)); cg.addln(&format!("{}struct {} {{", vis, name));