Skip to content

Commit a0a0835

Browse files
sbilligcburgdorf
authored andcommitted
ast: change for stmt target to string
1 parent 0f65e3b commit a0a0835

7 files changed

Lines changed: 9 additions & 17 deletions

File tree

analyzer/src/traversal/expressions.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -125,15 +125,6 @@ pub fn assignable_expr(
125125
Ok(attributes)
126126
}
127127

128-
/// Retrieves the String value of a name expression.
129-
pub fn expr_name_string(exp: &Node<fe::Expr>) -> Result<String, SemanticError> {
130-
if let fe::Expr::Name(name) = &exp.kind {
131-
return Ok(name.to_owned());
132-
}
133-
134-
unreachable!()
135-
}
136-
137128
fn expr_tuple(
138129
scope: Shared<BlockScope>,
139130
context: Shared<Context>,

analyzer/src/traversal/functions.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,7 @@ fn for_loop(
186186
let body_scope = BlockScope::from_block_scope(BlockScopeType::Loop, Rc::clone(&scope));
187187
// Make sure iter is in the function scope & it should be an array.
188188
let target_type = verify_is_array(scope, Rc::clone(&context), iter)?;
189-
let target_name = expressions::expr_name_string(target)?;
190-
body_scope.borrow_mut().add_var(&target_name, target_type)?;
189+
body_scope.borrow_mut().add_var(&target.kind, target_type)?;
191190
// Traverse the statements within the `for loop` body scope.
192191
traverse_statements(body_scope, context, body)
193192
}

compiler/src/lowering/mappers/functions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ fn func_stmt(context: &Context, stmt: Node<fe::FuncStmt>) -> Vec<Node<fe::FuncSt
6767
}],
6868
fe::FuncStmt::AugAssign { target, op, value } => aug_assign(context, target, op, value),
6969
fe::FuncStmt::For { target, iter, body } => vec![fe::FuncStmt::For {
70-
target: expressions::expr(context, target),
70+
target,
7171
iter: expressions::expr(context, iter),
7272
body: multiple_stmts(context, body),
7373
}],

compiler/src/yul/mappers/functions.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ fn func_stmt(context: &Context, stmt: &Node<fe::FuncStmt>) -> yul::Statement {
6464
fe::FuncStmt::VarDecl { .. } => declarations::var_decl(context, stmt),
6565
fe::FuncStmt::Assign { .. } => assignments::assign(context, stmt),
6666
fe::FuncStmt::Emit { .. } => emit(context, stmt),
67-
fe::FuncStmt::AugAssign { .. } => unimplemented!(),
67+
fe::FuncStmt::AugAssign { .. } => panic!("AugAssign should be lowered"),
6868
fe::FuncStmt::For { .. } => for_loop(context, stmt),
6969
fe::FuncStmt::While { .. } => while_loop(context, stmt),
7070
fe::FuncStmt::If { .. } => if_statement(context, stmt),
@@ -80,7 +80,7 @@ fn func_stmt(context: &Context, stmt: &Node<fe::FuncStmt>) -> yul::Statement {
8080
fn for_loop(context: &Context, stmt: &Node<fe::FuncStmt>) -> yul::Statement {
8181
if let fe::FuncStmt::For { target, iter, body } = &stmt.kind {
8282
let iterator = expressions::expr(context, iter);
83-
let target_var = names::var_name(&expressions::expr_name_string(target));
83+
let target_var = names::var_name(&target.kind);
8484
let yul_body = multiple_func_stmt(context, body);
8585
return if let Some(ExpressionAttributes {
8686
typ: Type::Array(array),

parser/src/ast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ pub enum FuncStmt {
149149
value: Node<Expr>,
150150
},
151151
For {
152-
target: Node<Expr>, // TODO: change to Vec<Node<String>>
152+
target: Node<String>,
153153
iter: Node<Expr>,
154154
body: Vec<Node<FuncStmt>>,
155155
},

parser/src/grammar/functions.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,9 @@ pub fn parse_while_stmt(par: &mut Parser) -> ParseResult<Node<FuncStmt>> {
360360
pub fn parse_for_stmt(par: &mut Parser) -> ParseResult<Node<FuncStmt>> {
361361
let for_tok = par.assert(TokenKind::For);
362362

363-
let target = parse_expr(par)?;
363+
let target = par
364+
.expect(TokenKind::Name, "failed to parse `for` statement")?
365+
.into();
364366
par.expect(TokenKind::In, "failed to parse `for` statement")?;
365367
let iter = parse_expr(par)?;
366368
par.enter_block(for_tok.span + iter.span, "`for` statement")?;

parser/tests/cases/snapshots/cases__parse_ast__stmt_for.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ expression: "ast_string(stringify!(stmt_for), functions::parse_stmt,\n
66
Node(
77
kind: For(
88
target: Node(
9-
kind: Name("a"),
9+
kind: "a",
1010
span: Span(
1111
start: 4,
1212
end: 5,

0 commit comments

Comments
 (0)