mirror of
https://github.com/RustPython/Parser.git
synced 2025-07-16 09:35:19 +00:00
compile_single checks last expr out of loop
This commit is contained in:
parent
b53f647e57
commit
753c0337bc
1 changed files with 14 additions and 15 deletions
|
@ -337,31 +337,30 @@ impl Compiler {
|
|||
) -> CompileResult<()> {
|
||||
self.symbol_table_stack.push(symbol_table);
|
||||
|
||||
let mut emitted_return = false;
|
||||
|
||||
for (i, statement) in body.iter().enumerate() {
|
||||
let is_last = i == body.len() - 1;
|
||||
let (last, body) = if let Some(splited) = body.split_last() {
|
||||
splited
|
||||
} else {
|
||||
return Ok(());
|
||||
};
|
||||
|
||||
for statement in body {
|
||||
if let ast::StmtKind::Expr { value } = &statement.node {
|
||||
self.compile_expression(value)?;
|
||||
|
||||
if is_last {
|
||||
self.emit(Instruction::Duplicate);
|
||||
self.emit(Instruction::PrintExpr);
|
||||
self.emit(Instruction::ReturnValue);
|
||||
emitted_return = true;
|
||||
} else {
|
||||
self.emit(Instruction::PrintExpr);
|
||||
}
|
||||
self.emit(Instruction::PrintExpr);
|
||||
} else {
|
||||
self.compile_statement(statement)?;
|
||||
}
|
||||
}
|
||||
|
||||
if !emitted_return {
|
||||
if let ast::StmtKind::Expr { value } = &last.node {
|
||||
self.compile_expression(value)?;
|
||||
self.emit(Instruction::Duplicate);
|
||||
self.emit(Instruction::PrintExpr);
|
||||
} else {
|
||||
self.compile_statement(last)?;
|
||||
self.emit_constant(ConstantData::None);
|
||||
self.emit(Instruction::ReturnValue);
|
||||
}
|
||||
self.emit(Instruction::ReturnValue);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue