Skip to content

Initial attempt at ast builtin module #134

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions tests/snippets/ast_snippet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

import ast
print(ast)

source = """
def foo():
print('bar')
"""
n = ast.parse(source)
print(n)
print(n.body)
print(n.body[0].name)
assert n.body[0].name == 'foo'
print(n.body[0].body)
print(n.body[0].body[0])
print(n.body[0].body[0].value.func.id)
assert n.body[0].body[0].value.func.id == 'print'
21 changes: 8 additions & 13 deletions vm/src/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::path::PathBuf;

use self::rustpython_parser::parser;
use super::compile;
use super::pyobject::{DictProtocol, PyObject, PyObjectKind, PyResult};
use super::pyobject::{DictProtocol, PyObjectKind, PyResult};
use super::vm::VirtualMachine;

fn import_module(vm: &mut VirtualMachine, module: &String) -> PyResult {
Expand Down Expand Up @@ -50,28 +50,23 @@ fn import_module(vm: &mut VirtualMachine, module: &String) -> PyResult {
};

let builtins = vm.get_builtin_scope();
let scope = vm.context().new_scope(Some(builtins));
let scope = vm.ctx.new_scope(Some(builtins));

match vm.run_code_obj(code_obj, scope.clone()) {
Ok(_) => {}
Err(value) => return Err(value),
}
Ok(scope)
let py_module = vm.ctx.new_module(module, scope);
Ok(py_module)
}

pub fn import(vm: &mut VirtualMachine, module: &String, symbol: &Option<String>) -> PyResult {
let scope = import_module(vm, module)?;
pub fn import(vm: &mut VirtualMachine, module_name: &String, symbol: &Option<String>) -> PyResult {
let module = import_module(vm, module_name)?;
// If we're importing a symbol, look it up and use it, otherwise construct a module and return
// that
let obj = match symbol {
Some(symbol) => scope.get_item(symbol).unwrap(),
None => PyObject::new(
PyObjectKind::Module {
name: module.clone(),
dict: scope.clone(),
},
vm.get_type(),
),
Some(symbol) => module.get_item(symbol).unwrap(),
None => module,
};
Ok(obj)
}
Expand Down
14 changes: 13 additions & 1 deletion vm/src/pyobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ pub struct PyContext {
pub tuple_type: PyObjectRef,
pub str_type: PyObjectRef,
pub function_type: PyObjectRef,
pub module_type: PyObjectRef,
pub bound_method_type: PyObjectRef,
pub member_descriptor_type: PyObjectRef,
pub object: PyObjectRef,
Expand Down Expand Up @@ -111,6 +112,7 @@ impl PyContext {
objobject::create_object(type_type.clone(), object_type.clone(), dict_type.clone());
objdict::create_type(type_type.clone(), object_type.clone(), dict_type.clone());

let module_type = create_type("module", &type_type, &object_type, &dict_type);
let function_type = create_type("function", &type_type, &object_type, &dict_type);
let bound_method_type = create_type("method", &type_type, &object_type, &dict_type);
let member_descriptor_type =
Expand Down Expand Up @@ -145,6 +147,7 @@ impl PyContext {
str_type: str_type,
object: object_type,
function_type: function_type,
module_type: module_type,
bound_method_type: bound_method_type,
member_descriptor_type: member_descriptor_type,
type_type: type_type,
Expand Down Expand Up @@ -213,6 +216,15 @@ impl PyContext {
self.object.clone()
}

pub fn new_object(&self) -> PyObjectRef {
PyObject::new(
PyObjectKind::Instance {
dict: self.new_dict(),
},
self.object(),
)
}

pub fn new_int(&self, i: i32) -> PyObjectRef {
PyObject::new(PyObjectKind::Integer { value: i }, self.int_type())
}
Expand Down Expand Up @@ -275,7 +287,7 @@ impl PyContext {
name: name.clone(),
dict: scope.clone(),
},
self.type_type(),
self.module_type.clone(),
)
}

Expand Down
Loading