Add missing codes for empty statement body
Fix
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);
```
@@ -344,12 +344,7 @@ impl Compiler {
) -> CompileResult<()> {
self.symbol_table_stack.push(symbol_table);
- let (last, body) = if let Some(splited) = body.split_last() {
- splited
- } else {
- return Ok(());
- };
-
+ if let Some((last, body)) = body.split_last() {
for statement in body {
if let ast::StmtKind::Expr { value } = &statement.node {
self.compile_expression(value)?;
@@ -367,8 +362,11 @@ impl Compiler {
self.compile_statement(last)?;
self.emit_constant(ConstantData::None);
}
- self.emit(Instruction::ReturnValue);
+ } else {
+ self.emit_constant(ConstantData::None);
+ };
+ self.emit(Instruction::ReturnValue);
Ok(())
}
