11use std:: rc:: Rc ;
2- // use std::fmt;
2+ use std:: fmt;
33use super :: bytecode;
44use super :: objint;
5+ use super :: objtype;
56use std:: cell:: RefCell ;
67use std:: collections:: HashMap ;
78use std:: ops:: { Add , Mul , Sub } ;
8- // use super::objtype;
99
1010/* Python objects and references.
1111
@@ -36,35 +36,66 @@ impl fmt::Display for PyObjectRef {
3636 }
3737}*/
3838
39- pub struct Context {
40- int_type : PyObjectRef ,
39+ #[ derive( Debug ) ]
40+ pub struct PyContext {
41+ pub type_type : PyObjectRef ,
42+ pub int_type : PyObjectRef ,
4143}
4244
4345// Basic objects:
44- impl Context {
45- fn new ( ) -> Context {
46- let type_type = objint :: create_type ( ) ;
47- let int_type = objint:: create_type ( ) ;
46+ impl PyContext {
47+ pub fn new ( ) -> PyContext {
48+ let type_type = objtype :: create_type ( ) ;
49+ let int_type = objint:: create_type ( type_type . clone ( ) ) ;
4850 // let str_type = objstr::make_type();
49- Context {
50- // type_type: type_type,
51+ PyContext {
52+ type_type : type_type,
5153 int_type : int_type,
5254 }
5355 }
56+
57+ pub fn new_int ( & self , i : i32 ) -> PyObjectRef {
58+ PyObject :: new ( PyObjectKind :: Integer { value : i } , self . type_type . clone ( ) )
59+ }
60+
61+ pub fn new_str ( & self , s : String ) -> PyObjectRef {
62+ PyObject :: new ( PyObjectKind :: String { value : s } , self . type_type . clone ( ) )
63+ }
64+
65+ pub fn new_bool ( & self , b : bool ) -> PyObjectRef {
66+ PyObject :: new ( PyObjectKind :: Boolean { value : b } , self . type_type . clone ( ) )
67+ }
5468}
5569
5670pub trait Executor {
57- fn call ( & self , PyObjectRef ) -> PyResult ;
71+ fn call ( & mut self , PyObjectRef ) -> PyResult ;
72+ fn new_str ( & self , s : String ) -> PyObjectRef ;
73+ fn new_bool ( & self , b : bool ) -> PyObjectRef ;
74+ fn get_none ( & self ) -> PyObjectRef ;
75+ fn get_type ( & self ) -> PyObjectRef ;
76+ fn context ( & self ) -> & PyContext ;
5877}
5978
6079#[ derive( Debug ) ]
6180pub struct PyObject {
6281 pub kind : PyObjectKind ,
63- // typ: PyObjectRef,
82+ pub typ : Option < PyObjectRef > ,
6483 pub dict : HashMap < String , PyObjectRef > , // __dict__ member
6584}
6685
67- #[ derive( Debug ) ]
86+ impl Default for PyObject {
87+ fn default ( ) -> PyObject {
88+ PyObject {
89+ kind : PyObjectKind :: None ,
90+ typ : None ,
91+ dict : HashMap :: new ( ) ,
92+ }
93+ }
94+ }
95+
96+ type RustPyFunc = fn ( rt : & mut Executor , Vec < PyObjectRef > ) -> PyResult ;
97+
98+ // #[derive(Debug)]
6899pub enum PyObjectKind {
69100 String {
70101 value : String ,
@@ -105,22 +136,28 @@ pub enum PyObjectKind {
105136 None ,
106137 Type ,
107138 RustFunction {
108- function : fn ( Vec < PyObjectRef > ) -> PyResult ,
139+ function : RustPyFunc ,
109140 } ,
110141}
111142
143+ impl fmt:: Debug for PyObjectKind {
144+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
145+ write ! ( f, "Some kind of python obj" )
146+ }
147+ }
148+
112149impl PyObject {
113- pub fn new ( kind : PyObjectKind ) -> PyObjectRef {
150+ pub fn new ( kind : PyObjectKind , typ : PyObjectRef ) -> PyObjectRef {
114151 PyObject {
115152 kind : kind,
116- // typ: PyO
153+ typ : Some ( typ ) ,
117154 dict : HashMap :: new ( ) ,
118155 } . into_ref ( )
119156 }
120157
121- pub fn call ( & self , args : Vec < PyObjectRef > ) -> Result < PyObjectRef , PyObjectRef > {
158+ pub fn call ( & self , rt : & mut Executor , args : Vec < PyObjectRef > ) -> PyResult {
122159 match self . kind {
123- PyObjectKind :: RustFunction { ref function } => function ( args) ,
160+ PyObjectKind :: RustFunction { ref function } => function ( rt , args) ,
124161 _ => {
125162 println ! ( "Not impl {:?}" , self ) ;
126163 panic ! ( "Not impl" ) ;
@@ -190,17 +227,6 @@ impl PyObject {
190227 Rc :: new ( RefCell :: new ( self ) )
191228 }
192229
193- pub fn new_int ( i : i32 ) -> PyObjectRef {
194- PyObject :: new ( PyObjectKind :: Integer { value : i } )
195- }
196-
197- pub fn new_str ( s : String ) -> PyObjectRef {
198- PyObject :: new ( PyObjectKind :: String { value : s } )
199- }
200-
201- pub fn new_bool ( b : bool ) -> PyObjectRef {
202- PyObject :: new ( PyObjectKind :: Boolean { value : b } )
203- }
204230}
205231
206232impl < ' a > Add < & ' a PyObject > for & ' a PyObject {
@@ -319,12 +345,13 @@ impl PartialEq for PyObject {
319345
320346#[ cfg( test) ]
321347mod tests {
322- use super :: { PyObject , PyObjectKind , PyObjectRef } ;
348+ use super :: { PyObjectKind , PyContext } ;
323349
324350 #[ test]
325351 fn test_add_py_integers ( ) {
326- let a = PyObject :: new_int ( 33 ) ;
327- let b = PyObject :: new_int ( 12 ) ;
352+ let ctx = PyContext :: new ( ) ;
353+ let a = ctx. new_int ( 33 ) ;
354+ let b = ctx. new_int ( 12 ) ;
328355 let c = & * a. borrow ( ) + & * b. borrow ( ) ;
329356 match c {
330357 PyObjectKind :: Integer { value } => assert_eq ! ( value, 45 ) ,
@@ -334,8 +361,9 @@ mod tests {
334361
335362 #[ test]
336363 fn test_multiply_str ( ) {
337- let a = PyObject :: new_str ( String :: from ( "Hello " ) ) ;
338- let b = PyObject :: new_int ( 4 ) ;
364+ let ctx = PyContext :: new ( ) ;
365+ let a = ctx. new_str ( String :: from ( "Hello " ) ) ;
366+ let b = ctx. new_int ( 4 ) ;
339367 let c = & * a. borrow ( ) * & * b. borrow ( ) ;
340368 match c {
341369 PyObjectKind :: String { value } => {
@@ -345,4 +373,8 @@ mod tests {
345373 }
346374 }
347375
376+ #[ test]
377+ fn test_type_type ( ) {
378+ let ctx = PyContext :: new ( ) ;
379+ }
348380}
0 commit comments