Skip to content

Commit de29e7f

Browse files
committed
Add spiffy dir function
1 parent bbc1930 commit de29e7f

5 files changed

Lines changed: 75 additions & 38 deletions

File tree

vm/src/builtins.rs

Lines changed: 51 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
use std::collections::HashMap;
33
use std::io::{self, Write};
44

5+
use super::pyobject::DictProtocol;
56
use super::pyobject::{Executor, PyContext, PyObject, PyObjectKind, PyObjectRef, PyResult};
67

78
/*
@@ -38,6 +39,39 @@ pub fn print(args: Vec<Rc<NativeType>>) -> NativeType {
3839
}
3940
*/
4041

42+
fn get_locals(rt: &mut Executor) -> PyObjectRef {
43+
let mut d = rt.new_dict();
44+
// TODO: implement dict_iter_items?
45+
let locals = rt.get_locals();
46+
match locals.borrow().kind {
47+
PyObjectKind::Dict { ref elements } => {
48+
for l in elements {
49+
d.set_item(l.0, l.1.clone());
50+
}
51+
}
52+
_ => {}
53+
};
54+
d
55+
}
56+
57+
fn dir_locals(rt: &mut Executor) -> PyObjectRef {
58+
get_locals(rt)
59+
}
60+
61+
fn dir_object(rt: &mut Executor, obj: PyObjectRef) -> PyObjectRef {
62+
let d = rt.new_dict();
63+
d
64+
}
65+
66+
pub fn dir(rt: &mut Executor, args: Vec<PyObjectRef>) -> PyResult {
67+
if args.is_empty() {
68+
Ok(dir_locals(rt))
69+
} else {
70+
let obj = args.into_iter().next().unwrap();
71+
Ok(dir_object(rt, obj))
72+
}
73+
}
74+
4175
pub fn print(rt: &mut Executor, args: Vec<PyObjectRef>) -> PyResult {
4276
// println!("Woot: {:?}", args);
4377
trace!("print called with {:?}", args);
@@ -55,52 +89,35 @@ pub fn compile(rt: &mut Executor, args: Vec<PyObjectRef>) -> PyResult {
5589
}
5690

5791
pub fn locals(rt: &mut Executor, args: Vec<PyObjectRef>) -> PyResult {
58-
// TODO
59-
Ok(rt.new_bool(true))
92+
Ok(rt.get_locals())
6093
}
6194

62-
/*
63-
* TODO
64-
pub fn len(args: Vec<Rc<NativeType>>) -> NativeType {
95+
pub fn len(rt: &mut Executor, args: Vec<PyObjectRef>) -> PyResult {
6596
if args.len() != 1 {
6697
panic!("len(s) expects exactly one parameter");
6798
}
68-
let len = match args[0].deref() {
69-
&NativeType::List(ref l) => l.borrow().len(),
70-
&NativeType::Tuple(ref t) => t.len(),
71-
&NativeType::Str(ref s) => s.len(),
72-
_ => panic!("TypeError: object of this type has no len()")
99+
let len = match args[0].borrow().kind {
100+
PyObjectKind::List { ref elements } => elements.len(),
101+
PyObjectKind::Tuple { ref elements } => elements.len(),
102+
PyObjectKind::String { ref value } => value.len(),
103+
_ => {
104+
return Err(rt.context()
105+
.new_str("TypeError: object of this type has no len()".to_string()))
106+
}
73107
};
74-
NativeType::Int(len as i32)
108+
Ok(rt.context().new_int(len as i32))
75109
}
76-
*/
77110

78111
pub fn make_module(ctx: &PyContext) -> PyObjectRef {
79112
// scope[String::from("print")] = print;
80113
let mut dict = HashMap::new();
81-
dict.insert(
82-
String::from("print"),
83-
PyObject::new(
84-
PyObjectKind::RustFunction { function: print },
85-
ctx.type_type.clone(),
86-
),
87-
);
88-
// locals.insert("len".to_string(), Rc::new(NativeType::NativeFunction(builtins::len)));
114+
dict.insert(String::from("print"), ctx.new_rustfunc(print));
89115
dict.insert(String::from("type"), ctx.type_type.clone());
90-
dict.insert(
91-
String::from("all"),
92-
PyObject::new(
93-
PyObjectKind::RustFunction { function: all },
94-
ctx.type_type.clone(),
95-
),
96-
);
97-
dict.insert(
98-
String::from("any"),
99-
PyObject::new(
100-
PyObjectKind::RustFunction { function: any },
101-
ctx.type_type.clone(),
102-
),
103-
);
116+
dict.insert(String::from("all"), ctx.new_rustfunc(all));
117+
dict.insert(String::from("any"), ctx.new_rustfunc(any));
118+
dict.insert(String::from("dir"), ctx.new_rustfunc(dir));
119+
dict.insert(String::from("locals"), ctx.new_rustfunc(locals));
120+
dict.insert("len".to_string(), ctx.new_rustfunc(len));
104121
let obj = PyObject::new(
105122
PyObjectKind::Module {
106123
name: "__builtins__".to_string(),

vm/src/objlist.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,5 +73,6 @@ pub fn append(rt: &mut Executor, l: PyObjectRef, other: PyObjectRef) -> PyResult
7373
pub fn make_type() -> PyObjectRef {
7474
7575
// dict.insert("__set_item__".to_string(), set_item);
76+
dict.insert("append".to_string(), append)
7677
}
7778
*/

vm/src/pyobject.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,13 @@ impl PyContext {
9494
)
9595
}
9696

97+
pub fn new_rustfunc(&self, function: RustPyFunc) -> PyObjectRef {
98+
PyObject::new(
99+
PyObjectKind::RustFunction { function: function },
100+
self.type_type.clone(),
101+
)
102+
}
103+
97104
pub fn new_class(&self, name: String) -> PyObjectRef {
98105
PyObject::new(PyObjectKind::Class { name: name }, self.type_type.clone())
99106
}
@@ -112,6 +119,7 @@ pub trait Executor {
112119
fn new_dict(&self) -> PyObjectRef;
113120
fn get_none(&self) -> PyObjectRef;
114121
fn get_type(&self) -> PyObjectRef;
122+
fn get_locals(&self) -> PyObjectRef;
115123
fn context(&self) -> &PyContext;
116124
}
117125

@@ -265,15 +273,23 @@ impl PyObject {
265273
"[{}]",
266274
elements
267275
.iter()
268-
.map(|elem| elem.borrow_mut().str())
276+
.map(|elem| elem.borrow().str())
269277
.collect::<Vec<_>>()
270278
.join(", ")
271279
),
272280
PyObjectKind::Tuple { ref elements } => format!(
273-
"{{{}}}",
281+
"({})",
282+
elements
283+
.iter()
284+
.map(|elem| elem.borrow().str())
285+
.collect::<Vec<_>>()
286+
.join(", ")
287+
),
288+
PyObjectKind::Dict { ref elements } => format!(
289+
"{{ {} }}",
274290
elements
275291
.iter()
276-
.map(|elem| elem.borrow_mut().str())
292+
.map(|elem| format!("{}: {}", elem.0, elem.1.borrow().str()))
277293
.collect::<Vec<_>>()
278294
.join(", ")
279295
),

vm/src/sysmodule.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use super::pyobject::{Executor, PyObject, PyObjectKind, PyObjectRef};
44
* The magic sys module.
55
*/
66

7-
87
/*
98
* TODO:
109
fn mk_module(rt: &mut Executor) -> PyObjectRef {

vm/src/vm.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ impl Executor for VirtualMachine {
5959
self.ctx.type_type.clone()
6060
}
6161

62+
fn get_locals(&self) -> PyObjectRef {
63+
self.frames.last().unwrap().locals.clone()
64+
}
65+
6266
fn context(&self) -> &PyContext {
6367
&self.ctx
6468
}

0 commit comments

Comments
 (0)