Skip to content

Commit

Permalink
ast: Remove variable assignment and use Assignment instead
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
CohenArthur committed Nov 13, 2024
1 parent 44cc7c4 commit 6d8278f
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 37 deletions.
6 changes: 3 additions & 3 deletions ast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ pub enum Node {
to_declare: Symbol,
value: Box<Ast>,
},
VarAssign {
Assignment {
to_assign: Symbol,
value: Box<Ast>,
},
Expand Down Expand Up @@ -429,7 +429,7 @@ pub trait Visitor {

Ok(Ast {
location,
node: Node::VarAssign { to_assign, value },
node: Node::Assignment { to_assign, value },
})
}

Expand Down Expand Up @@ -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),
Expand Down
6 changes: 3 additions & 3 deletions flatten/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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),
Expand Down
15 changes: 9 additions & 6 deletions typecheck/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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();

Expand Down
30 changes: 5 additions & 25 deletions xparser/src/constructs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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((
Expand Down Expand Up @@ -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),
},
Expand Down Expand Up @@ -1040,7 +1027,7 @@ fn type_inst_arg(input: ParseInput) -> ParseResult<ParseInput, Ast> {
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),
};
Expand Down Expand Up @@ -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());
}
Expand Down

0 comments on commit 6d8278f

Please sign in to comment.