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
+8 -1
View File
@@ -78,10 +78,17 @@ pub enum TopLevelKind {
export: bool, export: bool,
name: String, name: String,
fields: Vec<VarDeclStmt>, fields: Vec<VarDeclStmt>,
constructor: (ParamList, Block), constructor: ClassConstructor,
}, },
} }
#[derive(Debug, Clone, Serialize)]
pub struct ClassConstructor {
pub export: bool,
pub params: ParamList,
pub body: Block,
}
#[derive(Debug, Clone, Serialize)] #[derive(Debug, Clone, Serialize)]
pub enum Postfix { pub enum Postfix {
FieldAccess(String), FieldAccess(String),
+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 { impl From<pest::iterators::Pair<'_, Rule>> for TopLevelKind {
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();
@@ -271,6 +294,28 @@ impl From<pest::iterators::Pair<'_, Rule>> for TopLevelKind {
fields, 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:#?}"), _ => unimplemented!("{rule:#?}"),
} }
} }