Add missing codes for empty statement body

Fix #3704
Compared to previous version of `compile_program_single`, below two
codes are skipped when an empty body is given.
```rust
self.emit_constant(ConstantData::None);
self.emit(Instruction::ReturnValue);
```

Signed-off-by: snowapril <sinjihng@gmail.com>
This commit is contained in:
snowapril 2022-05-15 22:00:25 +09:00
parent be43bfaa80
commit dc1d6616ca

View file

@ -344,31 +344,29 @@ impl Compiler {
) -> CompileResult<()> { ) -> CompileResult<()> {
self.symbol_table_stack.push(symbol_table); self.symbol_table_stack.push(symbol_table);
let (last, body) = if let Some(splited) = body.split_last() { if let Some((last, body)) = body.split_last() {
splited for statement in body {
} else { if let ast::StmtKind::Expr { value } = &statement.node {
return Ok(()); self.compile_expression(value)?;
}; self.emit(Instruction::PrintExpr);
} else {
self.compile_statement(statement)?;
}
}
for statement in body { if let ast::StmtKind::Expr { value } = &last.node {
if let ast::StmtKind::Expr { value } = &statement.node {
self.compile_expression(value)?; self.compile_expression(value)?;
self.emit(Instruction::Duplicate);
self.emit(Instruction::PrintExpr); self.emit(Instruction::PrintExpr);
} else { } else {
self.compile_statement(statement)?; self.compile_statement(last)?;
self.emit_constant(ConstantData::None);
} }
}
if let ast::StmtKind::Expr { value } = &last.node {
self.compile_expression(value)?;
self.emit(Instruction::Duplicate);
self.emit(Instruction::PrintExpr);
} else { } else {
self.compile_statement(last)?;
self.emit_constant(ConstantData::None); self.emit_constant(ConstantData::None);
} };
self.emit(Instruction::ReturnValue);
self.emit(Instruction::ReturnValue);
Ok(()) Ok(())
} }