mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-28 18:59:04 +08:00
2 files changed
+39
-8
No files matched your search
+16
-2
@@ -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
@@ -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(())
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user