22use std:: collections:: HashMap ;
33use std:: io:: { self , Write } ;
44
5+ use super :: compile;
56use super :: pyobject:: DictProtocol ;
67use super :: pyobject:: { Executor , PyContext , PyObject , PyObjectKind , PyObjectRef , PyResult } ;
8+ use super :: objbool;
79
8- /*
9- * Original impl:
10- pub fn print(args: Vec<Rc<NativeType>>) -> NativeType {
11- for elem in args {
12- // TODO: figure out how python's print vectors
13- match elem.deref() {
14- &NativeType::NoneType => println!("None"),
15- &NativeType::Boolean(ref b)=> {
16- if *b {
17- println!("True");
18- } else {
19- println!("False");
20- }
21- },
22- &NativeType::Int(ref x) => println!("{}", x),
23- &NativeType::Float(ref x) => println!("{}", x),
24- &NativeType::Str(ref x) => println!("{}", x),
25- &NativeType::Unicode(ref x) => println!("{}", x),
26- _ => panic!("Print for {:?} not implemented yet", elem),
27- /*
28- List(Vec<NativeType>),
29- Tuple(Vec<NativeType>),
30- Iter(Vec<NativeType>), // TODO: use Iterator instead
31- Code(PyCodeObject),
32- Function(Function),
33- #[serde(skip_serializing, skip_deserializing)]
34- NativeFunction(fn(Vec<NativeType>) -> NativeType ),
35- */
36- }
37- }
38- NativeType::NoneType
39- }
40- */
4110
4211fn get_locals ( rt : & mut Executor ) -> PyObjectRef {
4312 let mut d = rt. new_dict ( ) ;
@@ -63,7 +32,7 @@ fn dir_object(rt: &mut Executor, obj: PyObjectRef) -> PyObjectRef {
6332 d
6433}
6534
66- pub fn dir ( rt : & mut Executor , args : Vec < PyObjectRef > ) -> PyResult {
35+ pub fn builtin_dir ( rt : & mut Executor , args : Vec < PyObjectRef > ) -> PyResult {
6736 if args. is_empty ( ) {
6837 Ok ( dir_locals ( rt) )
6938 } else {
@@ -72,8 +41,7 @@ pub fn dir(rt: &mut Executor, args: Vec<PyObjectRef>) -> PyResult {
7241 }
7342}
7443
75- pub fn print ( rt : & mut Executor , args : Vec < PyObjectRef > ) -> PyResult {
76- // println!("Woot: {:?}", args);
44+ pub fn builtin_print ( rt : & mut Executor , args : Vec < PyObjectRef > ) -> PyResult {
7745 trace ! ( "print called with {:?}" , args) ;
7846 for a in args {
7947 print ! ( "{} " , a. borrow( ) . str ( ) ) ;
@@ -83,9 +51,18 @@ pub fn print(rt: &mut Executor, args: Vec<PyObjectRef>) -> PyResult {
8351 Ok ( rt. get_none ( ) )
8452}
8553
86- pub fn compile ( rt : & mut Executor , args : Vec < PyObjectRef > ) -> PyResult {
87- // TODO
88- Ok ( rt. new_bool ( true ) )
54+ pub fn builtin_compile ( rt : & mut Executor , args : Vec < PyObjectRef > ) -> PyResult {
55+ if args. len ( ) < 1 {
56+ return Err ( rt. new_exception ( "Expected more arguments" . to_string ( ) ) )
57+ }
58+ // TODO:
59+ let mode = compile:: Mode :: Eval ;
60+ let source = args[ 0 ] . borrow ( ) . str ( ) ;
61+
62+ match compile:: compile ( rt, & source, mode) {
63+ Ok ( value) => Ok ( value) ,
64+ Err ( msg) => Err ( rt. new_exception ( msg) ) ,
65+ }
8966}
9067
9168pub fn locals ( rt : & mut Executor , args : Vec < PyObjectRef > ) -> PyResult {
@@ -111,12 +88,13 @@ pub fn len(rt: &mut Executor, args: Vec<PyObjectRef>) -> PyResult {
11188pub fn make_module ( ctx : & PyContext ) -> PyObjectRef {
11289 // scope[String::from("print")] = print;
11390 let mut dict = HashMap :: new ( ) ;
114- dict. insert ( String :: from ( "print" ) , ctx. new_rustfunc ( print ) ) ;
91+ dict. insert ( String :: from ( "print" ) , ctx. new_rustfunc ( builtin_print ) ) ;
11592 dict. insert ( String :: from ( "type" ) , ctx. type_type . clone ( ) ) ;
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 ) ) ;
93+ dict. insert ( String :: from ( "all" ) , ctx. new_rustfunc ( builtin_all ) ) ;
94+ dict. insert ( String :: from ( "any" ) , ctx. new_rustfunc ( builtin_any ) ) ;
95+ dict. insert ( String :: from ( "dir" ) , ctx. new_rustfunc ( builtin_dir ) ) ;
11996 dict. insert ( String :: from ( "locals" ) , ctx. new_rustfunc ( locals) ) ;
97+ dict. insert ( String :: from ( "compile" ) , ctx. new_rustfunc ( builtin_compile) ) ;
12098 dict. insert ( "len" . to_string ( ) , ctx. new_rustfunc ( len) ) ;
12199 let obj = PyObject :: new (
122100 PyObjectKind :: Module {
@@ -128,12 +106,10 @@ pub fn make_module(ctx: &PyContext) -> PyObjectRef {
128106 obj
129107}
130108
131- fn any ( rt : & mut Executor , args : Vec < PyObjectRef > ) -> PyResult {
132- // TODO
133- Ok ( rt. new_bool ( true ) )
109+ fn builtin_any ( rt : & mut Executor , args : Vec < PyObjectRef > ) -> PyResult {
110+ Ok ( rt. new_bool ( args. into_iter ( ) . any ( |e| objbool:: boolval ( e) ) ) )
134111}
135112
136- fn all ( rt : & mut Executor , args : Vec < PyObjectRef > ) -> PyResult {
137- // TODO
138- Ok ( rt. new_bool ( true ) )
113+ fn builtin_all ( rt : & mut Executor , args : Vec < PyObjectRef > ) -> PyResult {
114+ Ok ( rt. new_bool ( args. into_iter ( ) . all ( |e| objbool:: boolval ( e) ) ) )
139115}
0 commit comments