Class AST gen

This commit is contained in:
2026-05-05 18:41:08 +02:00
parent 1f7db7ff1a
commit f01b9debe2
2 changed files with 53 additions and 1 deletions
+45
View File
@@ -210,6 +210,29 @@ impl From<pest::iterators::Pair<'_, Rule>> for StatementBranch {
}
}
impl From<pest::iterators::Pair<'_, Rule>> for ClassConstructor {
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
};
Self {
export,
params: ParamList::from(inner.next().unwrap()),
body: Block::from(inner.next().unwrap()),
}
}
}
impl From<pest::iterators::Pair<'_, Rule>> for TopLevelKind {
fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self {
let rule = pair.as_rule();
@@ -271,6 +294,28 @@ impl From<pest::iterators::Pair<'_, Rule>> for TopLevelKind {
fields,
}
}
Rule::class_decl => TopLevelKind::ClassDecl {
export: if let Some(first) = inner.peek() {
if first.as_rule() == Rule::export {
inner.next();
true
} else {
false
}
} else {
false
},
name: inner.next().unwrap().as_str().to_string(),
fields: inner
.next()
.unwrap()
.into_inner()
.map(VarDeclStmt::from)
.collect(),
constructor: ClassConstructor::from(inner.next().unwrap()),
},
_ => unimplemented!("{rule:#?}"),
}
}