Add compile::Mode::BlockExpr

This commit is contained in:
Jeong Yunwon 2022-05-02 04:11:59 +09:00
parent 2262971b12
commit cb5324d023
3 changed files with 58 additions and 6 deletions

View file

@ -104,11 +104,16 @@ impl CompileContext {
pub fn compile_top(
ast: &ast::Mod,
source_path: String,
mode: Mode,
opts: CompileOpts,
) -> CompileResult<CodeObject> {
match ast {
ast::Mod::Module { body, .. } => compile_program(body, source_path, opts),
ast::Mod::Interactive { body } => compile_program_single(body, source_path, opts),
ast::Mod::Interactive { body } => match mode {
Mode::Single => compile_program_single(body, source_path, opts),
Mode::BlockExpr => compile_block_expression(body, source_path, opts),
_ => unreachable!("only Single and BlockExpr parsed to Interactive"),
},
ast::Mod::Expression { body } => compile_expression(body, source_path, opts),
ast::Mod::FunctionType { .. } => panic!("can't compile a FunctionType"),
}
@ -164,6 +169,20 @@ pub fn compile_program_single(
)
}
pub fn compile_block_expression(
ast: &[ast::Stmt],
source_path: String,
opts: CompileOpts,
) -> CompileResult<CodeObject> {
compile_impl(
ast,
source_path,
opts,
make_symbol_table,
Compiler::compile_block_expr,
)
}
pub fn compile_expression(
ast: &ast::Expr,
source_path: String,
@ -370,6 +389,35 @@ impl Compiler {
Ok(())
}
fn compile_block_expr(
&mut self,
body: &[ast::Stmt],
symbol_table: SymbolTable,
) -> CompileResult<()> {
self.symbol_table_stack.push(symbol_table);
self.compile_statements(body)?;
if let Some(last_statement) = body.last() {
match last_statement.node {
ast::StmtKind::Expr { .. } => {
self.current_block().instructions.pop(); // pop Instruction::Pop
}
ast::StmtKind::FunctionDef { .. }
| ast::StmtKind::AsyncFunctionDef { .. }
| ast::StmtKind::ClassDef { .. } => {
let store_inst = self.current_block().instructions.pop().unwrap(); // pop Instruction::Store
self.emit(Instruction::Duplicate);
self.current_block().instructions.push(store_inst);
}
_ => self.emit_constant(ConstantData::None),
}
}
self.emit(Instruction::ReturnValue);
Ok(())
}
// Compile statement in eval mode:
fn compile_eval(
&mut self,

View file

@ -3,10 +3,13 @@ pub enum Mode {
Exec,
Eval,
Single,
BlockExpr,
}
impl std::str::FromStr for Mode {
type Err = ModeParseError;
// To support `builtins.compile()` `mode` argument
fn from_str(s: &str) -> Result<Self, ModeParseError> {
match s {
"exec" => Ok(Mode::Exec),