This commit is contained in:
Windel Bouwman
2018-09-11 19:21:13 +02:00
4 changed files with 37 additions and 26 deletions

View File

@@ -1,4 +1,6 @@
// See also: file:///usr/share/doc/python/html/reference/grammar.html?highlight=grammar
#![allow(unknown_lints,clippy)]
use super::ast;
use super::lexer;
use std::iter::FromIterator;

View File

@@ -50,9 +50,14 @@ fn main() {
}
}
fn _run_string(source: &String, source_path: Option<String>) {
fn _run_string(source: &str, source_path: Option<String>) {
let mut vm = VirtualMachine::new();
let code_obj = compile::compile(&mut vm, &source, compile::Mode::Exec, source_path).unwrap();
let code_obj = compile::compile(
&mut vm,
&source.to_string(),
compile::Mode::Exec,
source_path,
).unwrap();
debug!("Code object: {:?}", code_obj.borrow());
let builtins = vm.get_builtin_scope();
let vars = vm.context().new_scope(Some(builtins)); // Keep track of local variables
@@ -74,7 +79,7 @@ fn run_command(source: &mut String) {
_run_string(source, None)
}
fn run_script(script_file: &String) {
fn run_script(script_file: &str) {
debug!("Running file {}", script_file);
// Parse an ast from it:
let filepath = Path::new(script_file);
@@ -87,10 +92,10 @@ fn run_script(script_file: &String) {
}
}
fn shell_exec(vm: &mut VirtualMachine, source: &String, scope: PyObjectRef) -> bool {
match compile::compile(vm, source, compile::Mode::Single, None) {
fn shell_exec(vm: &mut VirtualMachine, source: &str, scope: PyObjectRef) -> bool {
match compile::compile(vm, &source.to_string(), compile::Mode::Single, None) {
Ok(code) => {
match vm.run_code_obj(code, scope.clone()) {
match vm.run_code_obj(code, scope) {
Ok(_value) => {
// Printed already.
}
@@ -114,7 +119,7 @@ fn shell_exec(vm: &mut VirtualMachine, source: &String, scope: PyObjectRef) -> b
fn read_until_empty_line(input: &mut String) -> Result<i32, std::io::Error> {
loop {
print!("..... ");
io::stdout().flush().ok().expect("Could not flush stdout");
io::stdout().flush().expect("Could not flush stdout");
let mut line = String::new();
match io::stdin().read_line(&mut line) {
Ok(0) => {
@@ -146,7 +151,7 @@ fn run_shell() {
let mut input = String::new();
loop {
print!(">>>>> "); // Use 5 items. pypy has 4, cpython has 3.
io::stdout().flush().ok().expect("Could not flush stdout");
io::stdout().flush().expect("Could not flush stdout");
match io::stdin().read_line(&mut input) {
Ok(0) => {
break;

View File

@@ -13,13 +13,7 @@ use super::compile;
use super::pyobject::{DictProtocol, PyObjectKind, PyResult};
use super::vm::VirtualMachine;
fn import_module(vm: &mut VirtualMachine, module: &String) -> PyResult {
// First, see if we already loaded the module:
let sys_modules = vm.sys_module.get_item("modules").unwrap();
if let Some(module) = sys_modules.get_item(module) {
return Ok(module);
}
fn import_uncached_module(vm: &mut VirtualMachine, module: &String) -> PyResult {
// Check for Rust-native modules
if let Some(module) = vm.stdlib_inits.get(module) {
return Ok(module(&vm.ctx).clone());
@@ -56,8 +50,18 @@ fn import_module(vm: &mut VirtualMachine, module: &String) -> PyResult {
Ok(_) => {}
Err(value) => return Err(value),
}
let py_module = vm.ctx.new_module(module, scope);
Ok(py_module)
Ok(vm.ctx.new_module(module, scope))
}
fn import_module(vm: &mut VirtualMachine, module_name: &String) -> PyResult {
// First, see if we already loaded the module:
let sys_modules = vm.sys_module.get_item("modules").unwrap();
if let Some(module) = sys_modules.get_item(module_name) {
return Ok(module);
}
let module = import_uncached_module(vm, module_name)?;
sys_modules.set_item(module_name, module.clone());
Ok(module)
}
pub fn import(vm: &mut VirtualMachine, module_name: &String, symbol: &Option<String>) -> PyResult {

View File

@@ -208,7 +208,7 @@ impl VirtualMachine {
match block {
Some(Block::TryExcept { handler }) => {
self.push_value(exc);
self.jump(&handler);
self.jump(handler);
return None;
}
Some(Block::With { .. }) => {
@@ -922,7 +922,7 @@ impl VirtualMachine {
} else {
panic!("Wrong block type")
};
self.jump(&end_label);
self.jump(end_label);
None
} else {
Some(Err(next_error))
@@ -995,7 +995,7 @@ impl VirtualMachine {
}
}
bytecode::Instruction::Jump { target } => {
self.jump(target);
self.jump(*target);
None
}
bytecode::Instruction::JumpIf { target } => {
@@ -1003,7 +1003,7 @@ impl VirtualMachine {
match objbool::boolval(self, obj) {
Ok(value) => {
if value {
self.jump(target);
self.jump(*target);
}
None
}
@@ -1016,7 +1016,7 @@ impl VirtualMachine {
match objbool::boolval(self, obj) {
Ok(value) => {
if !value {
self.jump(target);
self.jump(*target);
}
None
}
@@ -1050,7 +1050,7 @@ impl VirtualMachine {
bytecode::Instruction::Break => {
let block = self.unwind_loop();
if let Block::Loop { start: _, end } = block {
self.jump(&end);
self.jump(end);
}
None
}
@@ -1061,7 +1061,7 @@ impl VirtualMachine {
bytecode::Instruction::Continue => {
let block = self.unwind_loop();
if let Block::Loop { start, end: _ } = block {
self.jump(&start);
self.jump(start);
} else {
assert!(false);
}
@@ -1121,9 +1121,9 @@ impl VirtualMachine {
}
}
fn jump(&mut self, label: &bytecode::Label) {
fn jump(&mut self, label: bytecode::Label) {
let current_frame = self.current_frame_mut();
let target_pc = current_frame.code.label_map[label];
let target_pc = current_frame.code.label_map[&label];
trace!(
"program counter from {:?} to {:?}",
current_frame.lasti,