Deduped methods
This commit is contained in:
+9
-9
@@ -15,6 +15,12 @@ pub enum TypePostfix {
|
||||
RefMut,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub enum Visibility {
|
||||
Public,
|
||||
Private,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub enum Attribute {
|
||||
/// #[test]
|
||||
@@ -67,19 +73,13 @@ pub enum TopLevelKind {
|
||||
name: String,
|
||||
fields: FieldList,
|
||||
},
|
||||
FunctionDecl {
|
||||
export: bool,
|
||||
name: String,
|
||||
params: ParamList,
|
||||
return_type: TypeExpr,
|
||||
body: Block,
|
||||
},
|
||||
FunctionDecl(FunctionDecl),
|
||||
ClassDecl {
|
||||
export: bool,
|
||||
name: String,
|
||||
fields: Vec<VarDeclStmt>,
|
||||
constructor: ClassConstructor,
|
||||
methods: Vec<ClassMethod>,
|
||||
methods: Vec<FunctionDecl>,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -91,7 +91,7 @@ pub struct ClassConstructor {
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct ClassMethod {
|
||||
pub struct FunctionDecl {
|
||||
pub export: bool,
|
||||
pub name: String,
|
||||
pub params: ParamList,
|
||||
|
||||
+4
-34
@@ -242,42 +242,12 @@ impl From<pest::iterators::Pair<'_, Rule>> for ClassConstructor {
|
||||
impl From<pest::iterators::Pair<'_, Rule>> for TopLevelKind {
|
||||
fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self {
|
||||
let rule = pair.as_rule();
|
||||
let mut inner = pair.into_inner();
|
||||
let mut inner = pair.clone().into_inner();
|
||||
|
||||
match rule {
|
||||
Rule::import => TopLevelKind::Include(Path::from(inner.next().unwrap())),
|
||||
|
||||
Rule::function_decl => {
|
||||
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 params = if inner.peek().unwrap().as_rule() == Rule::param_list {
|
||||
ParamList::from(inner.next().unwrap())
|
||||
} else {
|
||||
ParamList(Vec::new())
|
||||
};
|
||||
|
||||
let body = Block::from(inner.next().unwrap());
|
||||
|
||||
TopLevelKind::FunctionDecl {
|
||||
export,
|
||||
name,
|
||||
params,
|
||||
return_type,
|
||||
body,
|
||||
}
|
||||
}
|
||||
Rule::function_decl => TopLevelKind::FunctionDecl(FunctionDecl::from(pair)),
|
||||
|
||||
Rule::struct_decl => {
|
||||
let export = if let Some(first) = inner.peek() {
|
||||
@@ -320,7 +290,7 @@ impl From<pest::iterators::Pair<'_, Rule>> for TopLevelKind {
|
||||
.map(VarDeclStmt::from)
|
||||
.collect(),
|
||||
constructor: ClassConstructor::from(inner.next().unwrap()),
|
||||
methods: inner.into_iter().map(ClassMethod::from).collect(),
|
||||
methods: inner.into_iter().map(FunctionDecl::from).collect(),
|
||||
},
|
||||
|
||||
_ => unimplemented!("{rule:#?}"),
|
||||
@@ -596,7 +566,7 @@ impl From<pest::iterators::Pair<'_, Rule>> for VarDecl {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<pest::iterators::Pair<'_, Rule>> for ClassMethod {
|
||||
impl From<pest::iterators::Pair<'_, Rule>> for FunctionDecl {
|
||||
fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self {
|
||||
let mut inner = pair.into_inner();
|
||||
|
||||
|
||||
+5
-34
@@ -1,7 +1,7 @@
|
||||
use parser::ast::{
|
||||
Attribute, BinaryOp, Block, ClassMethod, Expression, Literal, Path, Postfix, Prefix, Statement,
|
||||
StatementBranch, TopLevel, TopLevelKind, TypeExpr, TypeExprKind, TypePostfix, VarAssignStmt,
|
||||
VarDecl, VarDeclStmt,
|
||||
Attribute, BinaryOp, Block, Expression, FunctionDecl, Literal, Path, Postfix, Prefix,
|
||||
Statement, StatementBranch, TopLevel, TopLevelKind, TypeExpr, TypeExprKind, TypePostfix,
|
||||
VarAssignStmt, VarDecl, VarDeclStmt,
|
||||
};
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -310,7 +310,7 @@ impl ToRust for TopLevelKind {
|
||||
Self::Include(path) => {
|
||||
cg.addln(&format!("use {};", path.get_rust()));
|
||||
}
|
||||
|
||||
Self::FunctionDecl(decl) => decl.to_rust(cg),
|
||||
Self::StructDecl {
|
||||
export,
|
||||
name,
|
||||
@@ -328,35 +328,6 @@ impl ToRust for TopLevelKind {
|
||||
cg.indent -= 1;
|
||||
cg.addln("}\n");
|
||||
}
|
||||
|
||||
Self::FunctionDecl {
|
||||
export,
|
||||
name,
|
||||
params,
|
||||
return_type,
|
||||
body,
|
||||
} => {
|
||||
let vis = if *export { "pub " } else { "" };
|
||||
|
||||
let params_str = params
|
||||
.0
|
||||
.iter()
|
||||
.map(VarDecl::get_rust)
|
||||
.collect::<Vec<_>>()
|
||||
.join(", ");
|
||||
|
||||
cg.addln(&format!(
|
||||
"{}fn {}({}) -> {} {{",
|
||||
vis,
|
||||
name,
|
||||
params_str,
|
||||
return_type.get_rust()
|
||||
));
|
||||
cg.indent += 1;
|
||||
body.to_rust(cg);
|
||||
cg.indent -= 1;
|
||||
cg.addln("}\n");
|
||||
}
|
||||
Self::ClassDecl {
|
||||
export,
|
||||
name,
|
||||
@@ -547,7 +518,7 @@ impl ToRust for Statement {
|
||||
}
|
||||
}
|
||||
|
||||
impl ToRust for ClassMethod {
|
||||
impl ToRust for FunctionDecl {
|
||||
fn to_rust(&self, cg: &mut RustCodegen) {
|
||||
let vis = if self.export { "pub " } else { "" };
|
||||
|
||||
|
||||
Reference in New Issue
Block a user