Class method AST gen
This commit is contained in:
+6
-5
@@ -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)]
|
||||||
|
|||||||
@@ -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 =================
|
||||||
|
|||||||
@@ -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,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -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));
|
||||||
|
|||||||
Reference in New Issue
Block a user