Parser - Re-use for loop body if it's a block and an incr statement is needed

This commit is contained in:
Emmanuel BENOîT 2023-01-01 19:47:04 +01:00
parent f95fcc4e3e
commit b9e659350b

View file

@ -236,10 +236,14 @@ impl Parser {
// inside a specific block if the initializer declares a variable. // inside a specific block if the initializer declares a variable.
let body_stmt = self.parse_statement()?; let body_stmt = self.parse_statement()?;
let body_with_incr = if let Some(incr) = increment { let body_with_incr = if let Some(incr) = increment {
ast::StmtNode::Block(vec![ let incr_stmt = Box::new(ast::StmtNode::Expression(incr));
Box::new(body_stmt), let body_block = if let ast::StmtNode::Block(mut body_block) = body_stmt {
Box::new(ast::StmtNode::Expression(incr)), body_block.push(incr_stmt);
]) body_block
} else {
vec![Box::new(body_stmt), incr_stmt]
};
ast::StmtNode::Block(body_block)
} else { } else {
body_stmt body_stmt
}; };