From 790b52e283d33aff6373876100d732996f43d5ce Mon Sep 17 00:00:00 2001 From: Arthur Cohen Date: Thu, 29 Aug 2024 15:02:02 +0200 Subject: [PATCH] ast: Remove variable assignment and use Assignment instead This commit renames the VarAssign nodes to Assignments instead. Before, these nodes were used for both field assignments and var assignemnts, but removing mutability from the language means variable assignments are no longer necessary. --- ast/src/lib.rs | 6 +++--- flatten/src/lib.rs | 6 +++--- typecheck/src/lib.rs | 15 +++++++++------ xparser/src/constructs.rs | 30 +++++------------------------- 4 files changed, 20 insertions(+), 37 deletions(-) diff --git a/ast/src/lib.rs b/ast/src/lib.rs index e4f8cde8..32353804 100644 --- a/ast/src/lib.rs +++ b/ast/src/lib.rs @@ -192,7 +192,7 @@ pub enum Node { to_declare: Symbol, value: Box, }, - VarAssign { + Assignment { to_assign: Symbol, value: Box, }, @@ -429,7 +429,7 @@ pub trait Visitor { Ok(Ast { location, - node: Node::VarAssign { to_assign, value }, + node: Node::Assignment { to_assign, value }, }) } @@ -500,7 +500,7 @@ pub trait Visitor { Node::VarDeclaration { to_declare, value } => { self.visit_var_declaration(ast.location, to_declare, value) } - Node::VarAssign { to_assign, value } => { + Node::Assignment { to_assign, value } => { self.visit_var_assign(ast.location, to_assign, value) } Node::VarOrEmptyType(name) => self.visit_var_or_empty_type(ast.location, name), diff --git a/flatten/src/lib.rs b/flatten/src/lib.rs index e0de7aec..aefc588b 100644 --- a/flatten/src/lib.rs +++ b/flatten/src/lib.rs @@ -225,7 +225,7 @@ impl<'ast> AstInfo<'ast> { .. } | AstNode::VarOrEmptyType(sym) - | AstNode::VarAssign { to_assign: sym, .. } + | AstNode::Assignment { to_assign: sym, .. } | AstNode::VarDeclaration { to_declare: sym, .. } => Some(sym), @@ -826,7 +826,7 @@ impl<'ast> Ctx<'ast> { } fn handle_field_instantiation(self, instantiation: &'ast Ast) -> (Ctx<'ast>, RefIdx) { - let AstNode::VarAssign { value, .. } = &instantiation.node else { + let AstNode::Assignment { value, .. } = &instantiation.node else { // FIXME: Ugly? unreachable!( "invalid AST: non var-assign in field instantiation, in type instantiation" @@ -902,7 +902,7 @@ impl<'ast> Ctx<'ast> { AstNode::VarDeclaration { to_declare, value } => { self.visit_var_declaration(node, to_declare, value) } - AstNode::VarAssign { to_assign, value } => { + AstNode::Assignment { to_assign, value } => { self.visit_var_assign(node, to_assign, value) } AstNode::VarOrEmptyType(_) => self.visit_var_or_empty_type(node), diff --git a/typecheck/src/lib.rs b/typecheck/src/lib.rs index e32602f7..0aa6f62c 100644 --- a/typecheck/src/lib.rs +++ b/typecheck/src/lib.rs @@ -286,11 +286,12 @@ mod tests { let ast = ast! { type Marker0; - where mut a = Marker0; - a = Marker0; + func f(a: Marker0) {} - where mut b = Marker0; - b = a; + f(Marker0); + + where b = Marker0; + f(b); }; let fir = fir!(ast).type_check(); @@ -303,8 +304,10 @@ mod tests { type Marker0; type Marker1; - where mut a = Marker0; - a = Marker1; + func f(a: Marker0) {} + + f(Marker0); + f(Marker1); }; let fir = fir!(ast).type_check(); diff --git a/xparser/src/constructs.rs b/xparser/src/constructs.rs index 2ba0e20b..97ff4893 100644 --- a/xparser/src/constructs.rs +++ b/xparser/src/constructs.rs @@ -876,21 +876,8 @@ fn func_type_or_var( generic_func_or_type_inst_args(next(input), id, start_loc) } else if let Ok((input, _)) = tokens::left_parenthesis(input) { func_or_type_inst_args(next(input), id, vec![], start_loc) - } else if let Ok((input, _)) = tokens::equal(input) { - let (input, value) = expr(input)?; - let (input, end_loc) = position(input)?; - let var_assign = Node::VarAssign { - to_assign: Symbol::from(id), - value: Box::new(value), - }; - Ok(( - input, - Ast { - location: pos_to_loc(input, start_loc, end_loc), - node: var_assign, - }, - )) } else { + // TODO: Is this correct? we need to do some error handling here and warn if there are unexpected tokens probably? let (input, end_loc) = position(input)?; let var_or_et = Node::VarOrEmptyType(Symbol::from(id)); Ok(( @@ -926,7 +913,7 @@ fn func_or_type_inst_args( 0, Ast { location: pos_to_loc(input, first_attr_start_loc, first_attr_end_loc), - node: Node::VarAssign { + node: Node::Assignment { to_assign: Symbol::from(first_attr), value: Box::new(first_attr_val), }, @@ -1040,7 +1027,7 @@ fn type_inst_arg(input: ParseInput) -> ParseResult { let input = next(input); let location = pos_to_loc(input, start_loc, end_loc); - let node = Node::VarAssign { + let node = Node::Assignment { to_assign: Symbol::from(id), value: Box::new(value), }; @@ -2267,15 +2254,8 @@ mod tests { } #[test] - fn mutable_declaration() { - let input = span!("where mut x = 14"); - - assert!(expr(input).is_ok()); - } - - #[test] - fn mutable_declaration_complex() { - let input = span!("where mut x = if value { 14 } else { 15 }"); + fn immutable_declaration_complex() { + let input = span!("where x = if value { 14 } else { 15 }"); assert!(expr(input).is_ok()); }