Refactored mutability
This commit is contained in:
@@ -234,7 +234,6 @@ impl GetRust for TypeExprKind {
|
||||
impl GenRust for Pattern {
|
||||
fn gen_rust(&self, ctx: &mut Context, cg: &mut RustCodegen) {
|
||||
match self {
|
||||
Self::Id(id) => cg.add(&id.get_rust()),
|
||||
Self::Path(mutable, path) => {
|
||||
if *mutable {
|
||||
cg.add("mut ");
|
||||
|
||||
@@ -158,7 +158,6 @@ impl GenRust for Statement {
|
||||
}
|
||||
|
||||
Statement::For {
|
||||
mutable,
|
||||
pattern,
|
||||
iterator,
|
||||
body,
|
||||
|
||||
@@ -212,8 +212,7 @@ impl GenRust for (&Vec<Spanned<FieldDeclStmt>>, &Spanned<ClassConstructor>) {
|
||||
cg.add_indentedln("}\n");
|
||||
|
||||
let mut constructor_params = vec![VarDecl {
|
||||
mutable: false,
|
||||
name: Pattern::Id(Identifier(String::from("self"))),
|
||||
name: Pattern::Path(false, Path(vec![Identifier(String::from("self"))])),
|
||||
type_: Some(TypeExpr(
|
||||
TypeExprKind::Path(Path(vec![Identifier(String::from("Self"))])),
|
||||
vec![TypePostfix::RefMut],
|
||||
|
||||
@@ -37,7 +37,6 @@ pub enum Pattern {
|
||||
Tuple(Vec<Identifier>),
|
||||
Literal(Literal),
|
||||
Path(bool, Path),
|
||||
Id(Identifier),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
|
||||
@@ -31,7 +31,6 @@ pub enum Statement {
|
||||
body: StatementBody,
|
||||
},
|
||||
For {
|
||||
mutable: bool,
|
||||
pattern: Pattern,
|
||||
iterator: Expression,
|
||||
body: StatementBody,
|
||||
@@ -46,7 +45,6 @@ pub enum Statement {
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct VarDecl {
|
||||
pub mutable: bool,
|
||||
pub name: Pattern,
|
||||
pub type_: Option<TypeExpr>,
|
||||
}
|
||||
|
||||
@@ -2,8 +2,7 @@ use crate::{
|
||||
Rule,
|
||||
ast::*,
|
||||
ast_expr,
|
||||
error::{AstError, AstResult, IntoErr},
|
||||
parser::listen_rule,
|
||||
error::{AstError, IntoErr},
|
||||
};
|
||||
|
||||
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for VarDeclStmt {
|
||||
@@ -63,13 +62,10 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for VarDecl {
|
||||
})
|
||||
.transpose();
|
||||
|
||||
let mutable: AstResult<'_, bool> = Ok(listen_rule(&mut inner, Rule::mutable));
|
||||
|
||||
let name = Pattern::try_from(inner.next().unwrap());
|
||||
|
||||
ast_expr!(VarDecl {
|
||||
type_: type_,
|
||||
mutable: mutable,
|
||||
name: name,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -74,8 +74,6 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Pattern {
|
||||
|
||||
Rule::literal => ast_expr!(Pattern::Literal(pair.try_into())),
|
||||
|
||||
Rule::identifier => ast_expr!(Pattern::Id(pair.try_into())),
|
||||
|
||||
Rule::path_pattern => ast_expr!(Pattern::Path(
|
||||
Ok(listen_rule(&mut inner, Rule::mutable)) as AstResult<'_, bool>,
|
||||
inner.next().unwrap().try_into()
|
||||
|
||||
@@ -3,7 +3,6 @@ use crate::{
|
||||
ast::*,
|
||||
ast_ensure, ast_expr,
|
||||
error::{AstError, AstResult, IntoErr, collect_recovered},
|
||||
parser::listen_rule,
|
||||
};
|
||||
|
||||
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Block {
|
||||
@@ -97,7 +96,6 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Statement {
|
||||
}),
|
||||
|
||||
Rule::for_stmt => ast_expr!(Statement::For {
|
||||
mutable: Ok(listen_rule(&mut inner, Rule::mutable)) as AstResult<'_, bool>,
|
||||
pattern: inner.next().unwrap().try_into(),
|
||||
iterator: inner.next().unwrap().try_into(),
|
||||
body: inner.next().unwrap().try_into(),
|
||||
|
||||
@@ -23,14 +23,11 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for FunctionDecl {
|
||||
|
||||
let self_param = consume_rule(&mut inner, Rule::self_param).map(|param| {
|
||||
let mut param_inner = param.into_inner();
|
||||
let name = Pattern::Id(Identifier(String::from("self")));
|
||||
|
||||
let mutable = listen_rule(&mut param_inner, Rule::mutable);
|
||||
|
||||
let is_ref = listen_rule(&mut param_inner, Rule::deref_px);
|
||||
let name = Pattern::Path(mutable && !is_ref, Path(vec![Identifier(String::from("self"))]));
|
||||
|
||||
VarDecl {
|
||||
mutable: mutable && !is_ref,
|
||||
name: name.clone(),
|
||||
type_: Some(TypeExpr(
|
||||
TypeExprKind::Path(Path(vec![Identifier("Self".to_string())])),
|
||||
|
||||
Reference in New Issue
Block a user