Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove mutability from the language #679

Merged
merged 2 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 7 additions & 15 deletions ast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,10 @@ pub enum Node {
else_block: Option<Box<Ast>>,
},
VarDeclaration {
mutable: bool,
to_declare: Symbol,
value: Box<Ast>,
},
VarAssign {
Assignment {
to_assign: Symbol,
value: Box<Ast>,
},
Expand Down Expand Up @@ -409,19 +408,14 @@ pub trait Visitor {
fn visit_var_declaration(
&mut self,
location: SpanTuple,
mutable: bool,
to_declare: Symbol,
value: Box<Ast>,
) -> Result<Ast, Error> {
let value = self.boxed(value)?;

Ok(Ast {
location,
node: Node::VarDeclaration {
mutable,
to_declare,
value,
},
node: Node::VarDeclaration { to_declare, value },
})
}

Expand All @@ -435,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 @@ -503,12 +497,10 @@ pub trait Visitor {
if_block,
else_block,
} => self.visit_if_else(ast.location, if_condition, if_block, else_block),
Node::VarDeclaration {
mutable,
to_declare,
value,
} => self.visit_var_declaration(ast.location, mutable, to_declare, value),
Node::VarAssign { to_assign, value } => {
Node::VarDeclaration { to_declare, value } => {
self.visit_var_declaration(ast.location, to_declare, 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
17 changes: 7 additions & 10 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 @@ -768,7 +768,6 @@ impl<'ast> Ctx<'ast> {
fn visit_var_declaration(
self,
ast: AstInfo<'ast>,
_mutable: bool,
_to_declare: &Symbol,
value: &'ast Ast,
) -> (Ctx<'ast>, RefIdx) {
Expand Down Expand Up @@ -827,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 @@ -900,12 +899,10 @@ impl<'ast> Ctx<'ast> {
if_block,
else_block,
} => self.visit_if_else(node, if_condition, if_block, else_block),
AstNode::VarDeclaration {
mutable,
to_declare,
value,
} => self.visit_var_declaration(node, *mutable, to_declare, value),
AstNode::VarAssign { to_assign, value } => {
AstNode::VarDeclaration { to_declare, value } => {
self.visit_var_declaration(node, to_declare, 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 Expand Up @@ -1013,7 +1010,7 @@ mod tests {
fn where_expr() {
let ast = ast! {
where x = 15;
where mut y = x;
where y = x;
};

let fir = ast.flatten();
Expand Down
1 change: 0 additions & 1 deletion loop_desugar/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ fn var_declare<S: Into<Symbol>>(loc: &SpanTuple, to_declare: S, value: Box<Ast>)
Ast {
location: loc.clone(),
node: Node::VarDeclaration {
mutable: false,
to_declare: to_declare.into(),
value,
},
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
36 changes: 6 additions & 30 deletions xparser/src/constructs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,6 @@ fn method_or_field(
///
/// | 'type' type_id '(' named_args
/// | 'incl' spaced_identifier [ 'as' next IDENTIFIER ]
/// | 'mut' spaced_identifier '=' expr (* mutable variable assigment *)
/// | '@' spaced_identifier '(' args
///
/// | 'extern' 'func' function_declaration ';'
Expand Down Expand Up @@ -617,19 +616,16 @@ fn unit_type_decl(input: ParseInput, start_loc: Location) -> ParseResult<ParseIn
))
}

/// mut? [`spaced_identifier`] '=' expr
/// [`spaced_identifier`] '=' expr
fn unit_var_decl(input: ParseInput) -> ParseResult<ParseInput, Ast> {
let input = next(input);
let (input, mutable) = opt(tokens::mut_tok)(input)?;
let mutable = mutable.is_some();

let (input, (symbol, (start_loc, _))) = spaced_identifier(input)?;
let (input, _) = tokens::equal(input)?;
let (input, value) = expr(input)?;
let (input, end_loc) = position(input)?;

let assignment = Node::VarDeclaration {
mutable,
to_declare: Symbol::from(symbol),
value: Box::new(value),
};
Expand Down Expand Up @@ -880,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 @@ -930,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 @@ -1044,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 @@ -2271,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
4 changes: 0 additions & 4 deletions xparser/src/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,6 @@ pub fn in_tok(input: ParseInput) -> ParseResult<ParseInput, ParseInput> {
specific_token(input, "in")
}

pub fn mut_tok(input: ParseInput) -> ParseResult<ParseInput, ParseInput> {
specific_token(input, "mut")
}

pub fn if_tok(input: ParseInput) -> ParseResult<ParseInput, ParseInput> {
specific_token(input, "if")
}
Expand Down
Loading