1
1
use bend:: imp;
2
2
use num_traits:: ToPrimitive ;
3
3
use parser:: Parser ;
4
- use pyo3:: {
5
- prelude:: * ,
6
- types:: { PyDict , PyFunction , PyString , PyTuple } ,
7
- } ;
4
+ use pyo3:: prelude:: * ;
5
+ use pyo3:: types:: { PyDict , PyFunction , PyString , PyTuple } ;
8
6
use rustpython_parser:: { parse, Mode } ;
9
- use types:: tree:: Tree ;
10
- use types:: {
11
- extract_type,
12
- tree:: { Leaf , Node } ,
13
- u24:: u24,
14
- } ;
7
+ use types:: extract_type;
8
+ use types:: tree:: { Leaf , Node , Tree } ;
9
+ use types:: u24:: u24;
15
10
mod benda_ffi;
16
11
mod parser;
17
12
mod types;
@@ -49,7 +44,8 @@ impl PyBjit {
49
44
let filename = code. getattr ( "co_filename" ) . unwrap ( ) ;
50
45
51
46
arg_names_temp = code. getattr ( "co_varnames" ) . unwrap ( ) ;
52
- let arg_names = arg_names_temp. downcast :: < PyTuple > ( ) . unwrap ( ) ;
47
+ let arg_names =
48
+ arg_names_temp. downcast :: < PyTuple > ( ) . unwrap ( ) ;
53
49
let argcount = code
54
50
. getattr ( "co_argcount" )
55
51
. unwrap ( )
@@ -74,7 +70,9 @@ impl PyBjit {
74
70
75
71
let mut parsed_types: Vec < ( String , imp:: Expr ) > = vec ! [ ] ;
76
72
77
- for ( index, arg) in args. downcast :: < PyTuple > ( ) . unwrap ( ) . iter ( ) . enumerate ( ) {
73
+ for ( index, arg) in
74
+ args. downcast :: < PyTuple > ( ) . unwrap ( ) . iter ( ) . enumerate ( )
75
+ {
78
76
parsed_types. push ( (
79
77
arg_list. get ( index) . unwrap ( ) . to_string ( ) ,
80
78
extract_type ( arg) . unwrap ( ) ,
@@ -89,12 +87,21 @@ impl PyBjit {
89
87
match module {
90
88
rustpython_parser:: ast:: Mod :: Module ( mods) => {
91
89
for ( index, stmt) in mods. body . iter ( ) . enumerate ( ) {
92
- if let rustpython_parser:: ast:: Stmt :: FunctionDef ( fun_def) = stmt {
90
+ if let rustpython_parser:: ast:: Stmt :: FunctionDef ( fun_def) =
91
+ stmt
92
+ {
93
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 ( ) ) ) ;
94
+ let mut parser = Parser :: new (
95
+ mods. body . clone ( ) ,
96
+ index,
97
+ parsed_types. clone ( ) ,
98
+ ) ;
99
+ let return_val =
100
+ parser. parse ( fun_def. name . as_ref ( ) , & [ ] ) ;
101
+ val = Some ( PyString :: new_bound (
102
+ py,
103
+ return_val. as_str ( ) ,
104
+ ) ) ;
98
105
break ;
99
106
}
100
107
}
@@ -111,25 +118,26 @@ impl PyBjit {
111
118
fn bjit_test ( fun : Bound < PyFunction > , py : Python ) -> PyResult < Py < PyAny > > {
112
119
let arg_names_temp: Bound < PyAny > ;
113
120
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
- } ;
121
+ let ( name, filename, arg_names, argcount) =
122
+ match fun. clone ( ) . downcast :: < PyFunction > ( ) {
123
+ Ok ( inner) => {
124
+ let name = inner. getattr ( "__name__" ) . unwrap ( ) ;
125
+ let code = inner. getattr ( "__code__" ) . unwrap ( ) ;
126
+ let filename = code. getattr ( "co_filename" ) . unwrap ( ) ;
127
+
128
+ arg_names_temp = code. getattr ( "co_varnames" ) . unwrap ( ) ;
129
+ let arg_names = arg_names_temp. downcast :: < PyTuple > ( ) . unwrap ( ) ;
130
+ let argcount = code
131
+ . getattr ( "co_argcount" )
132
+ . unwrap ( )
133
+ . to_string ( )
134
+ . parse :: < u32 > ( )
135
+ . unwrap ( ) ;
136
+
137
+ ( name, filename, arg_names, argcount)
138
+ }
139
+ Err ( _) => todo ! ( ) ,
140
+ } ;
133
141
134
142
let code = std:: fs:: read_to_string ( filename. to_string ( ) ) . unwrap ( ) ;
135
143
let module = parse ( code. as_str ( ) , Mode :: Module , "main.py" ) . unwrap ( ) ;
@@ -149,7 +157,8 @@ fn bjit_test(fun: Bound<PyFunction>, py: Python) -> PyResult<Py<PyAny>> {
149
157
match module {
150
158
rustpython_parser:: ast:: Mod :: Module ( mods) => {
151
159
for stmt in mods. body . iter ( ) {
152
- if let rustpython_parser:: ast:: Stmt :: FunctionDef ( fun_def) = stmt {
160
+ if let rustpython_parser:: ast:: Stmt :: FunctionDef ( fun_def) = stmt
161
+ {
153
162
if fun_def. name == name. to_string ( ) {
154
163
//let mut parser = Parser::new(mods.body.clone(), 0);
155
164
//let return_val = parser.parse(fun_def.name.as_ref(), &arg_list);
0 commit comments