1
+ use bend:: imp;
1
2
use num_traits:: ToPrimitive ;
2
3
use parser:: Parser ;
3
- use pyo3:: prelude:: * ;
4
- use pyo3:: types:: { PyFunction , PyString , PyTuple } ;
4
+ use pyo3:: {
5
+ prelude:: * ,
6
+ types:: { PyDict , PyFunction , PyString , PyTuple } ,
7
+ } ;
5
8
use rustpython_parser:: { parse, Mode } ;
6
- use types:: u24:: u24;
9
+ use types:: tree:: Tree ;
10
+ use types:: {
11
+ extract_type,
12
+ tree:: { Leaf , Node } ,
13
+ u24:: u24,
14
+ } ;
7
15
mod benda_ffi;
8
16
mod parser;
9
17
mod types;
10
18
11
- #[ pyfunction]
12
- fn sum_as_string ( a : usize , b : usize ) -> PyResult < String > {
13
- Ok ( ( a + b) . to_string ( ) )
14
- }
15
-
16
19
#[ pyfunction]
17
20
fn switch ( ) -> PyResult < String > {
18
21
Ok ( "Ok" . to_string ( ) )
19
22
}
20
23
24
+ #[ pyclass( name = "bjit" ) ]
25
+ pub struct PyBjit {
26
+ wraps : Py < PyAny > ,
27
+ }
28
+
29
+ #[ pymethods]
30
+ impl PyBjit {
31
+ #[ new]
32
+ fn __new__ ( wraps : Py < PyAny > ) -> Self {
33
+ PyBjit { wraps }
34
+ }
35
+ #[ pyo3( signature = ( * args, * * _kwargs) ) ]
36
+ fn __call__ (
37
+ & self ,
38
+ py : Python < ' _ > ,
39
+ args : & Bound < ' _ , PyTuple > ,
40
+ _kwargs : Option < & Bound < ' _ , PyDict > > ,
41
+ ) -> PyResult < Py < PyAny > > {
42
+ let arg_names_temp: Bound < PyAny > ;
43
+
44
+ let ( name, filename, arg_names, argcount) =
45
+ match self . wraps . downcast_bound :: < PyFunction > ( py) {
46
+ Ok ( inner) => {
47
+ let name = inner. getattr ( "__name__" ) . unwrap ( ) ;
48
+ let code = inner. getattr ( "__code__" ) . unwrap ( ) ;
49
+ let filename = code. getattr ( "co_filename" ) . unwrap ( ) ;
50
+
51
+ arg_names_temp = code. getattr ( "co_varnames" ) . unwrap ( ) ;
52
+ let arg_names = arg_names_temp. downcast :: < PyTuple > ( ) . unwrap ( ) ;
53
+ let argcount = code
54
+ . getattr ( "co_argcount" )
55
+ . unwrap ( )
56
+ . to_string ( )
57
+ . parse :: < u32 > ( )
58
+ . unwrap ( ) ;
59
+
60
+ ( name, filename, arg_names, argcount)
61
+ }
62
+ Err ( _) => todo ! ( ) ,
63
+ } ;
64
+
65
+ let mut arg_list: Vec < String > = vec ! [ ] ;
66
+
67
+ for ( index, arg) in arg_names. iter ( ) . enumerate ( ) {
68
+ if index >= argcount. to_usize ( ) . unwrap ( ) {
69
+ break ;
70
+ }
71
+
72
+ arg_list. push ( arg. to_string ( ) ) ;
73
+ }
74
+
75
+ let mut parsed_types: Vec < ( String , imp:: Expr ) > = vec ! [ ] ;
76
+
77
+ for ( index, arg) in args. downcast :: < PyTuple > ( ) . unwrap ( ) . iter ( ) . enumerate ( ) {
78
+ parsed_types. push ( (
79
+ arg_list. get ( index) . unwrap ( ) . to_string ( ) ,
80
+ extract_type ( arg) . unwrap ( ) ,
81
+ ) ) ;
82
+ }
83
+
84
+ let code = std:: fs:: read_to_string ( filename. to_string ( ) ) . unwrap ( ) ;
85
+ let module = parse ( code. as_str ( ) , Mode :: Module , "main.py" ) . unwrap ( ) ;
86
+
87
+ let mut val: Option < Bound < PyString > > = None ;
88
+
89
+ match module {
90
+ rustpython_parser:: ast:: Mod :: Module ( mods) => {
91
+ for ( index, stmt) in mods. body . iter ( ) . enumerate ( ) {
92
+ if let rustpython_parser:: ast:: Stmt :: FunctionDef ( fun_def) = stmt {
93
+ if fun_def. name == name. to_string ( ) {
94
+ let mut parser =
95
+ Parser :: new ( mods. body . clone ( ) , index, parsed_types. clone ( ) ) ;
96
+ let return_val = parser. parse ( fun_def. name . as_ref ( ) , & [ ] ) ;
97
+ val = Some ( PyString :: new_bound ( py, return_val. as_str ( ) ) ) ;
98
+ break ;
99
+ }
100
+ }
101
+ }
102
+ }
103
+ _ => unimplemented ! ( ) ,
104
+ }
105
+
106
+ Ok ( val. unwrap ( ) . into ( ) )
107
+ }
108
+ }
109
+
21
110
#[ pyfunction]
22
- fn bjit ( fun : Bound < PyFunction > , py : Python ) -> PyResult < Py < PyAny > > {
111
+ fn bjit_test ( fun : Bound < PyFunction > , py : Python ) -> PyResult < Py < PyAny > > {
23
112
let arg_names_temp: Bound < PyAny > ;
24
113
25
- let ( name, filename, arg_names, argcount) =
26
- match fun. clone ( ) . downcast :: < PyFunction > ( ) {
27
- Ok ( inner) => {
28
- let name = inner. getattr ( "__name__" ) . unwrap ( ) ;
29
- let code = inner. getattr ( "__code__" ) . unwrap ( ) ;
30
- let filename = code. getattr ( "co_filename" ) . unwrap ( ) ;
31
-
32
- arg_names_temp = code. getattr ( "co_varnames" ) . unwrap ( ) ;
33
- let arg_names = arg_names_temp. downcast :: < PyTuple > ( ) . unwrap ( ) ;
34
- let argcount = code
35
- . getattr ( "co_argcount" )
36
- . unwrap ( )
37
- . to_string ( )
38
- . parse :: < u32 > ( )
39
- . unwrap ( ) ;
40
-
41
- ( name, filename, arg_names, argcount)
42
- }
43
- Err ( _) => todo ! ( ) ,
44
- } ;
114
+ let ( name, filename, arg_names, argcount) = match fun. clone ( ) . downcast :: < PyFunction > ( ) {
115
+ Ok ( inner) => {
116
+ let name = inner. getattr ( "__name__" ) . unwrap ( ) ;
117
+ let code = inner. getattr ( "__code__" ) . unwrap ( ) ;
118
+ let filename = code. getattr ( "co_filename" ) . unwrap ( ) ;
119
+
120
+ arg_names_temp = code. getattr ( "co_varnames" ) . unwrap ( ) ;
121
+ let arg_names = arg_names_temp. downcast :: < PyTuple > ( ) . unwrap ( ) ;
122
+ let argcount = code
123
+ . getattr ( "co_argcount" )
124
+ . unwrap ( )
125
+ . to_string ( )
126
+ . parse :: < u32 > ( )
127
+ . unwrap ( ) ;
128
+
129
+ ( name, filename, arg_names, argcount)
130
+ }
131
+ Err ( _) => todo ! ( ) ,
132
+ } ;
45
133
46
134
let code = std:: fs:: read_to_string ( filename. to_string ( ) ) . unwrap ( ) ;
47
135
let module = parse ( code. as_str ( ) , Mode :: Module , "main.py" ) . unwrap ( ) ;
@@ -56,19 +144,16 @@ fn bjit(fun: Bound<PyFunction>, py: Python) -> PyResult<Py<PyAny>> {
56
144
arg_list. push ( arg. to_string ( ) ) ;
57
145
}
58
146
59
- let mut val: Option < Bound < PyString > > = None ;
147
+ let val: Option < Bound < PyString > > = None ;
60
148
61
149
match module {
62
150
rustpython_parser:: ast:: Mod :: Module ( mods) => {
63
151
for stmt in mods. body . iter ( ) {
64
- if let rustpython_parser:: ast:: Stmt :: FunctionDef ( fun_def) = stmt
65
- {
152
+ if let rustpython_parser:: ast:: Stmt :: FunctionDef ( fun_def) = stmt {
66
153
if fun_def. name == name. to_string ( ) {
67
- let mut parser = Parser :: new ( mods. body . clone ( ) , 0 ) ;
68
- let return_val =
69
- parser. parse ( fun_def. name . as_ref ( ) , & arg_list) ;
70
- val =
71
- Some ( PyString :: new_bound ( py, return_val. as_str ( ) ) ) ;
154
+ //let mut parser = Parser::new(mods.body.clone(), 0);
155
+ //let return_val = parser.parse(fun_def.name.as_ref(), &arg_list);
156
+ //val = Some(PyString::new_bound(py, return_val.as_str()));
72
157
}
73
158
}
74
159
}
@@ -94,12 +179,14 @@ fn bjit(fun: Bound<PyFunction>, py: Python) -> PyResult<Py<PyAny>> {
94
179
Ok ( fun)
95
180
}
96
181
97
- /// A Python module implemented in Rust.
98
182
#[ pymodule]
99
183
fn benda ( _py : Python , m : & Bound < ' _ , PyModule > ) -> PyResult < ( ) > {
100
- m. add_function ( wrap_pyfunction ! ( sum_as_string, m) ?) ?;
101
- m. add_function ( wrap_pyfunction ! ( bjit, m) ?) ?;
184
+ m. add_function ( wrap_pyfunction ! ( bjit_test, m) ?) ?;
102
185
m. add_function ( wrap_pyfunction ! ( switch, m) ?) ?;
186
+ m. add_class :: < PyBjit > ( ) ?;
103
187
m. add_class :: < u24 > ( ) ?;
188
+ m. add_class :: < Tree > ( ) ?;
189
+ m. add_class :: < Node > ( ) ?;
190
+ m. add_class :: < Leaf > ( ) ?;
104
191
Ok ( ( ) )
105
192
}
0 commit comments