From 7007aa32f1ece9f385a3b2177e5c2f5981ec2189 Mon Sep 17 00:00:00 2001 From: Selimaj Dev Date: Thu, 28 May 2026 11:45:26 +0200 Subject: [PATCH] Refactored mutability --- crates/codegen/src/lib.rs | 1 - crates/codegen/src/statement.rs | 1 - crates/codegen/src/top_level.rs | 3 +-- crates/parser/src/ast/expr.rs | 1 - crates/parser/src/ast/statement.rs | 2 -- crates/parser/src/parser/common/decl.rs | 6 +----- crates/parser/src/parser/common/mod.rs | 2 -- crates/parser/src/parser/common/statement.rs | 2 -- crates/parser/src/parser/items/function.rs | 5 +---- 9 files changed, 3 insertions(+), 20 deletions(-) diff --git a/crates/codegen/src/lib.rs b/crates/codegen/src/lib.rs index bbdf66c..cf88d9d 100644 --- a/crates/codegen/src/lib.rs +++ b/crates/codegen/src/lib.rs @@ -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 "); diff --git a/crates/codegen/src/statement.rs b/crates/codegen/src/statement.rs index 13b1a59..aa7a37a 100644 --- a/crates/codegen/src/statement.rs +++ b/crates/codegen/src/statement.rs @@ -158,7 +158,6 @@ impl GenRust for Statement { } Statement::For { - mutable, pattern, iterator, body, diff --git a/crates/codegen/src/top_level.rs b/crates/codegen/src/top_level.rs index 34bd69f..b13c531 100644 --- a/crates/codegen/src/top_level.rs +++ b/crates/codegen/src/top_level.rs @@ -212,8 +212,7 @@ impl GenRust for (&Vec>, &Spanned) { 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], diff --git a/crates/parser/src/ast/expr.rs b/crates/parser/src/ast/expr.rs index a1fc4ad..fcec338 100644 --- a/crates/parser/src/ast/expr.rs +++ b/crates/parser/src/ast/expr.rs @@ -37,7 +37,6 @@ pub enum Pattern { Tuple(Vec), Literal(Literal), Path(bool, Path), - Id(Identifier), } #[derive(Debug, Clone, Serialize)] diff --git a/crates/parser/src/ast/statement.rs b/crates/parser/src/ast/statement.rs index d4102ea..0196e72 100644 --- a/crates/parser/src/ast/statement.rs +++ b/crates/parser/src/ast/statement.rs @@ -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, } diff --git a/crates/parser/src/parser/common/decl.rs b/crates/parser/src/parser/common/decl.rs index 4cf8bfb..288745c 100644 --- a/crates/parser/src/parser/common/decl.rs +++ b/crates/parser/src/parser/common/decl.rs @@ -2,8 +2,7 @@ use crate::{ Rule, ast::*, ast_expr, - error::{AstError, AstResult, IntoErr}, - parser::listen_rule, + error::{AstError, IntoErr}, }; impl<'a> TryFrom> for VarDeclStmt { @@ -63,13 +62,10 @@ impl<'a> TryFrom> 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, }) } diff --git a/crates/parser/src/parser/common/mod.rs b/crates/parser/src/parser/common/mod.rs index 3057472..3b79896 100644 --- a/crates/parser/src/parser/common/mod.rs +++ b/crates/parser/src/parser/common/mod.rs @@ -74,8 +74,6 @@ impl<'a> TryFrom> 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() diff --git a/crates/parser/src/parser/common/statement.rs b/crates/parser/src/parser/common/statement.rs index 3f2db09..fddee57 100644 --- a/crates/parser/src/parser/common/statement.rs +++ b/crates/parser/src/parser/common/statement.rs @@ -3,7 +3,6 @@ use crate::{ ast::*, ast_ensure, ast_expr, error::{AstError, AstResult, IntoErr, collect_recovered}, - parser::listen_rule, }; impl<'a> TryFrom> for Block { @@ -97,7 +96,6 @@ impl<'a> TryFrom> 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(), diff --git a/crates/parser/src/parser/items/function.rs b/crates/parser/src/parser/items/function.rs index 766de4c..2b52253 100644 --- a/crates/parser/src/parser/items/function.rs +++ b/crates/parser/src/parser/items/function.rs @@ -23,14 +23,11 @@ impl<'a> TryFrom> 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())])),