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