Merge pull request #912 from sapir/repl-underscore

Repl underscore
This commit is contained in:
Windel Bouwman authored and GitHub committed 2019-05-05 11:21:20 +02:00
commit 6228a207e1
2 files changed
+39 -8

No files matched your search

+16 -2
View File
@@ -147,9 +147,23 @@ fn test_run_script() {
fn shell_exec(vm: &VirtualMachine, source: &str, scope: Scope) -> Result<(), CompileError> {
match compile::compile(vm, source, &compile::Mode::Single, "<stdin>".to_string()) {
Ok(code) => {
if let Err(err) = vm.run_code_obj(code, scope) {
print_exception(vm, &err);
match vm.run_code_obj(code, scope.clone()) {
Ok(value) => {
// Save non-None values as "_"
use rustpython_vm::pyobject::{IdProtocol, IntoPyObject, ItemProtocol};
if !value.is(&vm.get_none()) {
let key = objstr::PyString::from("_").into_pyobject(vm);
scope.globals.set_item(key, value, vm).unwrap();
}
}
Err(err) => {
print_exception(vm, &err);
}
}
Ok(())
}
// Don't inject syntax errors for line continuation
+23 -6
View File
@@ -130,18 +130,35 @@ impl Compiler {
symbol_scope: SymbolScope,
) -> Result<(), CompileError> {
self.scope_stack.push(symbol_scope);
for statement in &program.statements {
let mut emitted_return = false;
for (i, statement) in program.statements.iter().enumerate() {
let is_last = i == program.statements.len() - 1;
if let ast::Statement::Expression { ref expression } = statement.node {
self.compile_expression(expression)?;
self.emit(Instruction::PrintExpr);
if is_last {
self.emit(Instruction::Duplicate);
self.emit(Instruction::PrintExpr);
self.emit(Instruction::ReturnValue);
emitted_return = true;
} else {
self.emit(Instruction::PrintExpr);
}
} else {
self.compile_statement(&statement)?;
}
}
self.emit(Instruction::LoadConst {
value: bytecode::Constant::None,
});
self.emit(Instruction::ReturnValue);
if !emitted_return {
self.emit(Instruction::LoadConst {
value: bytecode::Constant::None,
});
self.emit(Instruction::ReturnValue);
}
Ok(())
}