Mercurial > python-compiler.rs
comparison src/ast_convert.rs @ 13:38b0d63697b1
Import the full AST grammar from CPython 3.5.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Thu, 02 Jun 2016 05:33:23 +0100 |
parents | fa7e285f88e7 |
children | 719a27f1c1c7 |
comparison
equal
deleted
inserted
replaced
12:0e96c5bc401d | 13:38b0d63697b1 |
---|---|
1 use python_ast::{Module, Statement, Expr, BinOp, UnaryOp}; | 1 use python_ast::{Module, stmt, expr, expr_context, cmpop, operator, unaryop, arguments, arg, alias}; |
2 | 2 |
3 use cpython::{Python, PyObject}; | 3 use cpython::{Python, PyObject}; |
4 use cpython::ObjectProtocol; //for call method | 4 use cpython::ObjectProtocol; //for call method |
5 | 5 |
6 fn get_str(py: Python, object: PyObject) -> String { | 6 fn get_str(py: Python, object: PyObject) -> String { |
28 let mut arguments = vec!(); | 28 let mut arguments = vec!(); |
29 for arg in args.iter(py).unwrap() { | 29 for arg in args.iter(py).unwrap() { |
30 let arg = arg.unwrap(); | 30 let arg = arg.unwrap(); |
31 let arg = parse_expr(py, arg); | 31 let arg = parse_expr(py, arg); |
32 arguments.push(match arg { | 32 arguments.push(match arg { |
33 Expr::Name(arg) => arg, | 33 expr::Name(arg, expr_context::Load) => arg, |
34 _ => panic!() | 34 _ => panic!() |
35 }); | 35 }); |
36 } | 36 } |
37 arguments | 37 arguments |
38 } else { | 38 } else { |
39 panic!() | 39 panic!() |
40 } | 40 } |
41 } | 41 } |
42 | 42 |
43 fn parse_unaryop(py: Python, ast: PyObject) -> UnaryOp { | 43 fn parse_unaryop(py: Python, ast: PyObject) -> unaryop { |
44 let builtins_module = py.import("builtins").unwrap(); | 44 let builtins_module = py.import("builtins").unwrap(); |
45 let isinstance = builtins_module.get(py, "isinstance").unwrap(); | 45 let isinstance = builtins_module.get(py, "isinstance").unwrap(); |
46 | 46 |
47 let is_instance = |object: &PyObject, type_: &PyObject| { | 47 let is_instance = |object: &PyObject, type_: &PyObject| { |
48 return isinstance.call(py, (object, type_), None).unwrap().is_true(py).unwrap(); | 48 return isinstance.call(py, (object, type_), None).unwrap().is_true(py).unwrap(); |
49 }; | 49 }; |
50 | 50 |
51 let ast_module = py.import("ast").unwrap(); | 51 let ast_module = py.import("ast").unwrap(); |
52 let ast_type = ast_module.get(py, "AST").unwrap(); | 52 let ast_type = ast_module.get(py, "AST").unwrap(); |
53 let invert_type = ast_module.get(py, "Invert").unwrap(); | |
54 let not_type = ast_module.get(py, "Not").unwrap(); | |
53 let uadd_type = ast_module.get(py, "UAdd").unwrap(); | 55 let uadd_type = ast_module.get(py, "UAdd").unwrap(); |
54 let usub_type = ast_module.get(py, "USub").unwrap(); | 56 let usub_type = ast_module.get(py, "USub").unwrap(); |
55 | 57 |
56 assert!(is_instance(&ast, &ast_type)); | 58 assert!(is_instance(&ast, &ast_type)); |
57 | 59 |
58 if is_instance(&ast, &uadd_type) { | 60 if is_instance(&ast, &invert_type) { |
59 UnaryOp::UAdd | 61 unaryop::Invert |
62 } else if is_instance(&ast, ¬_type) { | |
63 unaryop::Not | |
64 } else if is_instance(&ast, &uadd_type) { | |
65 unaryop::UAdd | |
60 } else if is_instance(&ast, &usub_type) { | 66 } else if is_instance(&ast, &usub_type) { |
61 UnaryOp::USub | 67 unaryop::USub |
62 } else { | 68 } else { |
63 println!("UnaryOp {}", ast); | 69 unreachable!() |
64 UnaryOp::Error | 70 } |
65 } | 71 } |
66 } | 72 |
67 | 73 fn parse_cmpop(py: Python, ast: PyObject) -> cmpop { |
68 fn parse_binop(py: Python, ast: PyObject) -> BinOp { | 74 let builtins_module = py.import("builtins").unwrap(); |
69 let builtins_module = py.import("builtins").unwrap(); | 75 let isinstance = builtins_module.get(py, "isinstance").unwrap(); |
70 let isinstance = builtins_module.get(py, "isinstance").unwrap(); | 76 |
71 | 77 let is_instance = |object: &PyObject, type_: &PyObject| { |
72 let is_instance = |object: &PyObject, type_: &PyObject| { | 78 return isinstance.call(py, (object, type_), None).unwrap().is_true(py).unwrap(); |
73 return isinstance.call(py, (object, type_), None).unwrap().is_true(py).unwrap(); | 79 }; |
74 }; | 80 |
75 | 81 let ast_module = py.import("ast").unwrap(); |
76 let ast_module = py.import("ast").unwrap(); | 82 let ast_type = ast_module.get(py, "AST").unwrap(); |
77 let ast_type = ast_module.get(py, "AST").unwrap(); | |
78 let add_type = ast_module.get(py, "Add").unwrap(); | |
79 let mult_type = ast_module.get(py, "Mult").unwrap(); | |
80 let eq_type = ast_module.get(py, "Eq").unwrap(); | 83 let eq_type = ast_module.get(py, "Eq").unwrap(); |
84 let noteq_type = ast_module.get(py, "NotEq").unwrap(); | |
81 let lt_type = ast_module.get(py, "Lt").unwrap(); | 85 let lt_type = ast_module.get(py, "Lt").unwrap(); |
82 let gt_type = ast_module.get(py, "Gt").unwrap(); | 86 let gt_type = ast_module.get(py, "Gt").unwrap(); |
87 | |
88 assert!(is_instance(&ast, &ast_type)); | |
89 | |
90 if is_instance(&ast, &eq_type) { | |
91 cmpop::Eq | |
92 } else if is_instance(&ast, ¬eq_type) { | |
93 cmpop::NotEq | |
94 } else if is_instance(&ast, <_type) { | |
95 cmpop::Lt | |
96 } else if is_instance(&ast, >_type) { | |
97 cmpop::Gt | |
98 } else { | |
99 unreachable!() | |
100 } | |
101 } | |
102 | |
103 fn parse_operator(py: Python, ast: PyObject) -> operator { | |
104 let builtins_module = py.import("builtins").unwrap(); | |
105 let isinstance = builtins_module.get(py, "isinstance").unwrap(); | |
106 | |
107 let is_instance = |object: &PyObject, type_: &PyObject| { | |
108 return isinstance.call(py, (object, type_), None).unwrap().is_true(py).unwrap(); | |
109 }; | |
110 | |
111 let ast_module = py.import("ast").unwrap(); | |
112 let ast_type = ast_module.get(py, "AST").unwrap(); | |
113 let add_type = ast_module.get(py, "Add").unwrap(); | |
83 let sub_type = ast_module.get(py, "Sub").unwrap(); | 114 let sub_type = ast_module.get(py, "Sub").unwrap(); |
115 let mult_type = ast_module.get(py, "Mult").unwrap(); | |
116 let matmult_type = ast_module.get(py, "MatMult").unwrap(); | |
84 let div_type = ast_module.get(py, "Div").unwrap(); | 117 let div_type = ast_module.get(py, "Div").unwrap(); |
118 let mod_type = ast_module.get(py, "Mod").unwrap(); | |
119 let pow_type = ast_module.get(py, "Pow").unwrap(); | |
120 let lshift_type = ast_module.get(py, "LShift").unwrap(); | |
121 let rshift_type = ast_module.get(py, "RShift").unwrap(); | |
122 let bitor_type = ast_module.get(py, "BitOr").unwrap(); | |
123 let bitxor_type = ast_module.get(py, "BitXor").unwrap(); | |
124 let bitand_type = ast_module.get(py, "BitAnd").unwrap(); | |
125 let floordiv_type = ast_module.get(py, "FloorDiv").unwrap(); | |
85 | 126 |
86 assert!(is_instance(&ast, &ast_type)); | 127 assert!(is_instance(&ast, &ast_type)); |
87 | 128 |
88 if is_instance(&ast, &add_type) { | 129 if is_instance(&ast, &add_type) { |
89 BinOp::BinAdd | 130 operator::Add |
131 } else if is_instance(&ast, &sub_type) { | |
132 operator::Sub | |
90 } else if is_instance(&ast, &mult_type) { | 133 } else if is_instance(&ast, &mult_type) { |
91 BinOp::BinMult | 134 operator::Mult |
92 } else if is_instance(&ast, &eq_type) { | 135 } else if is_instance(&ast, &matmult_type) { |
93 BinOp::BinEq | 136 operator::MatMult |
94 } else if is_instance(&ast, <_type) { | |
95 BinOp::BinLt | |
96 } else if is_instance(&ast, >_type) { | |
97 BinOp::BinGt | |
98 } else if is_instance(&ast, &sub_type) { | |
99 BinOp::Sub | |
100 } else if is_instance(&ast, &div_type) { | 137 } else if is_instance(&ast, &div_type) { |
101 BinOp::Div | 138 operator::Div |
102 } else { | 139 } else if is_instance(&ast, &mod_type) { |
103 println!("BinOp {}", ast); | 140 operator::Mod |
104 BinOp::Error | 141 } else if is_instance(&ast, &pow_type) { |
105 } | 142 operator::Pow |
106 } | 143 } else if is_instance(&ast, &lshift_type) { |
107 | 144 operator::LShift |
108 fn parse_expr(py: Python, ast: PyObject) -> Expr { | 145 } else if is_instance(&ast, &rshift_type) { |
146 operator::RShift | |
147 } else if is_instance(&ast, &bitor_type) { | |
148 operator::BitOr | |
149 } else if is_instance(&ast, &bitxor_type) { | |
150 operator::BitXor | |
151 } else if is_instance(&ast, &bitand_type) { | |
152 operator::BitAnd | |
153 } else if is_instance(&ast, &floordiv_type) { | |
154 operator::FloorDiv | |
155 } else { | |
156 println!("operator {}", ast); | |
157 panic!() | |
158 } | |
159 } | |
160 | |
161 fn parse_expr(py: Python, ast: PyObject) -> expr { | |
109 let builtins_module = py.import("builtins").unwrap(); | 162 let builtins_module = py.import("builtins").unwrap(); |
110 let isinstance = builtins_module.get(py, "isinstance").unwrap(); | 163 let isinstance = builtins_module.get(py, "isinstance").unwrap(); |
111 | 164 |
112 let is_instance = |object: &PyObject, type_: &PyObject| { | 165 let is_instance = |object: &PyObject, type_: &PyObject| { |
113 return isinstance.call(py, (object, type_), None).unwrap().is_true(py).unwrap(); | 166 return isinstance.call(py, (object, type_), None).unwrap().is_true(py).unwrap(); |
124 let num_type = ast_module.get(py, "Num").unwrap(); | 177 let num_type = ast_module.get(py, "Num").unwrap(); |
125 let str_type = ast_module.get(py, "Str").unwrap(); | 178 let str_type = ast_module.get(py, "Str").unwrap(); |
126 let list_type = ast_module.get(py, "List").unwrap(); | 179 let list_type = ast_module.get(py, "List").unwrap(); |
127 let compare_type = ast_module.get(py, "Compare").unwrap(); | 180 let compare_type = ast_module.get(py, "Compare").unwrap(); |
128 let call_type = ast_module.get(py, "Call").unwrap(); | 181 let call_type = ast_module.get(py, "Call").unwrap(); |
129 let alias_type = ast_module.get(py, "alias").unwrap(); | |
130 | 182 |
131 assert!(is_instance(&ast, &ast_type)); | 183 assert!(is_instance(&ast, &ast_type)); |
132 | 184 |
133 if is_instance(&ast, &arg_type) { | 185 if is_instance(&ast, &arg_type) { |
134 let arg = ast.getattr(py, "arg").unwrap(); | 186 let arg = ast.getattr(py, "arg").unwrap(); |
135 let arg = get_str(py, arg); | 187 let arg = get_str(py, arg); |
136 Expr::Name(arg) | 188 expr::Name(arg, expr_context::Load) |
137 } else if is_instance(&ast, &attribute_type) { | 189 } else if is_instance(&ast, &attribute_type) { |
138 let value = ast.getattr(py, "value").unwrap(); | 190 let value = ast.getattr(py, "value").unwrap(); |
139 let attr = ast.getattr(py, "attr").unwrap(); | 191 let attr = ast.getattr(py, "attr").unwrap(); |
140 | 192 |
141 let value = parse_expr(py, value); | 193 let value = parse_expr(py, value); |
142 let attr = get_str(py, attr); | 194 let attr = get_str(py, attr); |
143 | 195 |
144 Expr::Attribute(Box::new(value), attr) | 196 expr::Attribute(Box::new(value), attr, expr_context::Load) |
145 } else if is_instance(&ast, &name_type) { | 197 } else if is_instance(&ast, &name_type) { |
146 let id = ast.getattr(py, "id").unwrap(); | 198 let id = ast.getattr(py, "id").unwrap(); |
147 let id = get_str(py, id); | 199 let id = get_str(py, id); |
148 Expr::Name(id) | 200 expr::Name(id, expr_context::Load) |
149 } else if is_instance(&ast, &name_constant_type) { | 201 } else if is_instance(&ast, &name_constant_type) { |
150 let value = ast.getattr(py, "value").unwrap(); | 202 let value = ast.getattr(py, "value").unwrap(); |
151 let value = get_str(py, value); | 203 let value = get_str(py, value); |
152 Expr::NameConstant(value) | 204 expr::NameConstant(value) |
153 } else if is_instance(&ast, &num_type) { | 205 } else if is_instance(&ast, &num_type) { |
154 let n = ast.getattr(py, "n").unwrap(); | 206 let n = ast.getattr(py, "n").unwrap(); |
155 let n = get_str(py, n); | 207 let n = get_str(py, n); |
156 Expr::Num(n) | 208 expr::Num(n) |
157 } else if is_instance(&ast, &str_type) { | 209 } else if is_instance(&ast, &str_type) { |
158 let s = ast.getattr(py, "s").unwrap(); | 210 let s = ast.getattr(py, "s").unwrap(); |
159 let s = get_str(py, s); | 211 let s = get_str(py, s); |
160 Expr::Str(s) | 212 expr::Str(s) |
161 } else if is_instance(&ast, &list_type) { | 213 } else if is_instance(&ast, &list_type) { |
162 let elts = ast.getattr(py, "elts").unwrap(); | 214 let elts = ast.getattr(py, "elts").unwrap(); |
163 | 215 |
164 let mut elements = vec!(); | 216 let mut elements = vec!(); |
165 for elt in elts.iter(py).unwrap() { | 217 for elt in elts.iter(py).unwrap() { |
166 let elt = elt.unwrap(); | 218 let elt = elt.unwrap(); |
167 elements.push(parse_expr(py, elt)); | 219 elements.push(parse_expr(py, elt)); |
168 } | 220 } |
169 | 221 |
170 Expr::List(elements) | 222 expr::List(elements, expr_context::Load) |
171 } else if is_instance(&ast, &unary_op_type) { | 223 } else if is_instance(&ast, &unary_op_type) { |
172 let op = ast.getattr(py, "op").unwrap(); | 224 let op = ast.getattr(py, "op").unwrap(); |
173 let operand = ast.getattr(py, "operand").unwrap(); | 225 let operand = ast.getattr(py, "operand").unwrap(); |
174 | 226 |
175 let op = parse_unaryop(py, op); | 227 let op = parse_unaryop(py, op); |
176 let operand = parse_expr(py, operand); | 228 let operand = parse_expr(py, operand); |
177 | 229 |
178 Expr::UnaryOp(op, Box::new(operand)) | 230 expr::UnaryOp(op, Box::new(operand)) |
179 } else if is_instance(&ast, &bin_op_type) { | 231 } else if is_instance(&ast, &bin_op_type) { |
180 let left = ast.getattr(py, "left").unwrap(); | 232 let left = ast.getattr(py, "left").unwrap(); |
181 let op = ast.getattr(py, "op").unwrap(); | 233 let op = ast.getattr(py, "op").unwrap(); |
182 let right = ast.getattr(py, "right").unwrap(); | 234 let right = ast.getattr(py, "right").unwrap(); |
183 | 235 |
184 let left = parse_expr(py, left); | 236 let left = parse_expr(py, left); |
185 let op = parse_binop(py, op); | 237 let op = parse_operator(py, op); |
186 let right = parse_expr(py, right); | 238 let right = parse_expr(py, right); |
187 | 239 |
188 Expr::BinOp(Box::new(left), op, Box::new(right)) | 240 expr::BinOp(Box::new(left), op, Box::new(right)) |
189 } else if is_instance(&ast, &call_type) { | 241 } else if is_instance(&ast, &call_type) { |
190 let func = ast.getattr(py, "func").unwrap(); | 242 let func = ast.getattr(py, "func").unwrap(); |
191 let args = ast.getattr(py, "args").unwrap(); | 243 let args = ast.getattr(py, "args").unwrap(); |
192 //let keywords = ast.getattr(py, "keywords").unwrap(); | 244 let keywords = ast.getattr(py, "keywords").unwrap(); |
193 | 245 |
194 let func = parse_expr(py, func); | 246 let func = parse_expr(py, func); |
195 | 247 |
196 let mut arguments = vec!(); | 248 let mut arguments = vec!(); |
197 for arg in args.iter(py).unwrap() { | 249 for arg in args.iter(py).unwrap() { |
198 let arg = arg.unwrap(); | 250 let arg = arg.unwrap(); |
199 arguments.push(parse_expr(py, arg)); | 251 arguments.push(parse_expr(py, arg)); |
200 } | 252 } |
201 | 253 |
202 Expr::Call(Box::new(func), arguments) | 254 /* |
203 } else if is_instance(&ast, &alias_type) { | 255 let mut kwargs = vec!(); |
204 let name = ast.getattr(py, "name").unwrap(); | 256 for arg in keywords.iter(py).unwrap() { |
205 let asname = ast.getattr(py, "asname").unwrap(); | 257 let arg = arg.unwrap(); |
206 | 258 kwargs.push(parse_expr(py, arg)); |
207 let name = get_str(py, name); | 259 } |
208 let asname = if asname == py.None() { | 260 */ |
209 None | 261 |
210 } else { | 262 expr::Call(Box::new(func), arguments, vec!()) |
211 Some(get_str(py, asname)) | |
212 }; | |
213 | |
214 Expr::Alias(name, asname) | |
215 } else if is_instance(&ast, &compare_type) { | 263 } else if is_instance(&ast, &compare_type) { |
216 let left = ast.getattr(py, "left").unwrap(); | 264 let left = ast.getattr(py, "left").unwrap(); |
217 let ops = ast.getattr(py, "ops").unwrap(); | 265 let ops = ast.getattr(py, "ops").unwrap(); |
218 let comparators = ast.getattr(py, "comparators").unwrap(); | 266 let comparators = ast.getattr(py, "comparators").unwrap(); |
219 | 267 |
222 let comparators = comparators.iter(py).unwrap(); | 270 let comparators = comparators.iter(py).unwrap(); |
223 | 271 |
224 let mut new_ops = vec!(); | 272 let mut new_ops = vec!(); |
225 for op in ops { | 273 for op in ops { |
226 let op = op.unwrap(); | 274 let op = op.unwrap(); |
227 let op = parse_binop(py, op); | 275 let op = parse_cmpop(py, op); |
228 new_ops.push(op); | 276 new_ops.push(op); |
229 } | 277 } |
230 | 278 |
231 let mut new_comparators = vec!(); | 279 let mut new_comparators = vec!(); |
232 for comparator in comparators { | 280 for comparator in comparators { |
233 let comparator = comparator.unwrap(); | 281 let comparator = comparator.unwrap(); |
234 let comparator = parse_expr(py, comparator); | 282 let comparator = parse_expr(py, comparator); |
235 new_comparators.push(comparator); | 283 new_comparators.push(comparator); |
236 } | 284 } |
237 | 285 |
238 Expr::Compare(Box::new(left), new_ops, new_comparators) | 286 expr::Compare(Box::new(left), new_ops, new_comparators) |
239 } else { | 287 } else { |
240 println!("Expr {}", ast); | 288 println!("expr {}", ast); |
241 Expr::Error | 289 unreachable!() |
242 } | 290 } |
243 } | 291 } |
244 | 292 |
245 fn parse_statement(py: Python, ast: PyObject) -> Statement { | 293 fn parse_arguments(py: Python, ast: PyObject) -> arguments { |
246 //Statement::FunctionDef(Expr::Name("function".to_string()), vec!(Expr::Name("a".to_string()), Expr::Name("b".to_string())), vec!()) | 294 let builtins_module = py.import("builtins").unwrap(); |
247 //Statement::If(Expr::BinOp(BinOp::BinEq, Box::new(Expr::Name("__name__".to_string())), Box::new(Expr::Str("__main__".to_string()))), vec!(Statement::Expr(Expr::Call(Box::new(Expr::Name("function".to_string())), vec!(Expr::Num(1), Expr::Num(2)))))) | 295 let isinstance = builtins_module.get(py, "isinstance").unwrap(); |
296 | |
297 let is_instance = |object: &PyObject, type_: &PyObject| { | |
298 return isinstance.call(py, (object, type_), None).unwrap().is_true(py).unwrap(); | |
299 }; | |
300 | |
301 let ast_module = py.import("ast").unwrap(); | |
302 let ast_type = ast_module.get(py, "AST").unwrap(); | |
303 let arguments_type = ast_module.get(py, "arguments").unwrap(); | |
304 let arg_type = ast_module.get(py, "arg").unwrap(); | |
305 | |
306 assert!(is_instance(&ast, &ast_type)); | |
307 | |
308 if is_instance(&ast, &arguments_type) { | |
309 let args = arguments{ | |
310 //args: Vec<arg>, | |
311 args: { | |
312 let args = ast.getattr(py, "args").unwrap(); | |
313 let mut arguments = vec!(); | |
314 for arg in args.iter(py).unwrap() { | |
315 let arg = arg.unwrap(); | |
316 assert!(is_instance(&arg, &arg_type)); | |
317 let arg = get_str(py, arg); | |
318 arguments.push(arg{arg: arg, annotation: None}); | |
319 } | |
320 arguments | |
321 }, | |
322 //vararg: Option<arg>, | |
323 vararg: None, | |
324 //kwonlyargs: Vec<arg>, | |
325 kwonlyargs: vec!(), | |
326 //kw_defaults: Vec<expr>, | |
327 kw_defaults: vec!(), | |
328 //kwarg: Option<arg>, | |
329 kwarg: None, | |
330 //defaults: Vec<expr> | |
331 defaults: vec!() | |
332 }; | |
333 args | |
334 } else { | |
335 println!("arguments {}", ast); | |
336 panic!() | |
337 } | |
338 } | |
339 | |
340 fn parse_statement(py: Python, ast: PyObject) -> stmt { | |
341 //stmt::FunctionDef(expr::Name("function".to_string()), vec!(expr::Name("a".to_string()), expr::Name("b".to_string())), vec!()) | |
342 //stmt::If(expr::BinOp(BinOp::BinEq, Box::new(expr::Name("__name__".to_string())), Box::new(expr::Str("__main__".to_string()))), vec!(stmt::Expr(expr::Call(Box::new(expr::Name("function".to_string())), vec!(expr::Num(1), expr::Num(2)))))) | |
248 | 343 |
249 let builtins_module = py.import("builtins").unwrap(); | 344 let builtins_module = py.import("builtins").unwrap(); |
250 let isinstance = builtins_module.get(py, "isinstance").unwrap(); | 345 let isinstance = builtins_module.get(py, "isinstance").unwrap(); |
251 | 346 |
252 let is_instance = |object: &PyObject, type_: &PyObject| { | 347 let is_instance = |object: &PyObject, type_: &PyObject| { |
305 let statement = statement.unwrap(); | 400 let statement = statement.unwrap(); |
306 let statement = parse_statement(py, statement); | 401 let statement = parse_statement(py, statement); |
307 statements.push(statement); | 402 statements.push(statement); |
308 } | 403 } |
309 | 404 |
310 Statement::ClassDef(name, nodes, statements) | 405 stmt::ClassDef(name, nodes, vec!(), statements, vec!()) |
311 } else if is_instance(&ast, &function_def_type) { | 406 } else if is_instance(&ast, &function_def_type) { |
312 let name = ast.getattr(py, "name").unwrap(); | 407 let name = ast.getattr(py, "name").unwrap(); |
313 let args = ast.getattr(py, "args").unwrap(); | 408 let args = ast.getattr(py, "args").unwrap(); |
314 let body = ast.getattr(py, "body").unwrap(); | 409 let body = ast.getattr(py, "body").unwrap(); |
315 | 410 |
316 let name = get_str(py, name); | 411 let name = get_str(py, name); |
317 let args = parse_expr_vec(py, args); | 412 let args = parse_arguments(py, args); |
318 /* | |
319 let mut arguments = vec!(); | |
320 for arg in args.iter(py).unwrap() { | |
321 let arg = parse_expr(py, arg.unwrap()); | |
322 arguments.push(arg); | |
323 } | |
324 */ | |
325 | 413 |
326 let mut statements = vec!(); | 414 let mut statements = vec!(); |
327 for statement in body.iter(py).unwrap() { | 415 for statement in body.iter(py).unwrap() { |
328 let statement = parse_statement(py, statement.unwrap()); | 416 let statement = parse_statement(py, statement.unwrap()); |
329 statements.push(statement); | 417 statements.push(statement); |
330 } | 418 } |
331 | 419 |
332 Statement::FunctionDef(name, args, statements) | 420 let decorators = vec!(); |
421 let returns = None; | |
422 | |
423 stmt::FunctionDef(name, args, statements, decorators, returns) | |
333 } else if is_instance(&ast, &global_type) { | 424 } else if is_instance(&ast, &global_type) { |
334 let names = ast.getattr(py, "names").unwrap(); | 425 let names = ast.getattr(py, "names").unwrap(); |
335 | 426 |
336 let mut globals = vec!(); | 427 let mut globals = vec!(); |
337 for name in names.iter(py).unwrap() { | 428 for name in names.iter(py).unwrap() { |
338 let name = name.unwrap(); | 429 let name = name.unwrap(); |
339 let name = get_str(py, name); | 430 let name = get_str(py, name); |
340 globals.push(name); | 431 globals.push(name); |
341 } | 432 } |
342 | 433 |
343 Statement::Global(globals) | 434 stmt::Global(globals) |
344 } else if is_instance(&ast, &if_type) { | 435 } else if is_instance(&ast, &if_type) { |
345 let test = ast.getattr(py, "test").unwrap(); | 436 let test = ast.getattr(py, "test").unwrap(); |
346 let body = ast.getattr(py, "body").unwrap(); | 437 let body = ast.getattr(py, "body").unwrap(); |
347 let orelse = ast.getattr(py, "orelse").unwrap(); | 438 let orelse = ast.getattr(py, "orelse").unwrap(); |
348 | 439 |
360 let statement = statement.unwrap(); | 451 let statement = statement.unwrap(); |
361 let statement = parse_statement(py, statement); | 452 let statement = parse_statement(py, statement); |
362 orelse_.push(statement); | 453 orelse_.push(statement); |
363 } | 454 } |
364 | 455 |
365 Statement::If(test, statements, orelse_) | 456 stmt::If(test, statements, orelse_) |
366 } else if is_instance(&ast, &while_type) { | 457 } else if is_instance(&ast, &while_type) { |
367 let test = ast.getattr(py, "test").unwrap(); | 458 let test = ast.getattr(py, "test").unwrap(); |
368 let body = ast.getattr(py, "body").unwrap(); | 459 let body = ast.getattr(py, "body").unwrap(); |
369 let orelse = ast.getattr(py, "orelse").unwrap(); | 460 let orelse = ast.getattr(py, "orelse").unwrap(); |
370 | 461 |
382 let statement = statement.unwrap(); | 473 let statement = statement.unwrap(); |
383 let statement = parse_statement(py, statement); | 474 let statement = parse_statement(py, statement); |
384 orelse_.push(statement); | 475 orelse_.push(statement); |
385 } | 476 } |
386 | 477 |
387 Statement::While(test, statements, orelse_) | 478 stmt::While(test, statements, orelse_) |
388 } else if is_instance(&ast, &for_type) { | 479 } else if is_instance(&ast, &for_type) { |
389 let target = ast.getattr(py, "target").unwrap(); | 480 let target = ast.getattr(py, "target").unwrap(); |
390 let iter = ast.getattr(py, "iter").unwrap(); | 481 let iter = ast.getattr(py, "iter").unwrap(); |
391 let body = ast.getattr(py, "body").unwrap(); | 482 let body = ast.getattr(py, "body").unwrap(); |
392 let orelse = ast.getattr(py, "orelse").unwrap(); | 483 let orelse = ast.getattr(py, "orelse").unwrap(); |
406 let statement = statement.unwrap(); | 497 let statement = statement.unwrap(); |
407 let statement = parse_statement(py, statement); | 498 let statement = parse_statement(py, statement); |
408 orelse_.push(statement); | 499 orelse_.push(statement); |
409 } | 500 } |
410 | 501 |
411 Statement::For(target, iter, statements, orelse_) | 502 stmt::For(target, iter, statements, orelse_) |
412 } else if is_instance(&ast, &assign_type) { | 503 } else if is_instance(&ast, &assign_type) { |
413 let targets = ast.getattr(py, "targets").unwrap(); | 504 let targets = ast.getattr(py, "targets").unwrap(); |
414 let value = ast.getattr(py, "value").unwrap(); | 505 let value = ast.getattr(py, "value").unwrap(); |
415 | 506 |
416 let mut arguments = vec!(); | 507 let mut arguments = vec!(); |
420 arguments.push(target); | 511 arguments.push(target); |
421 } | 512 } |
422 | 513 |
423 let value = parse_expr(py, value); | 514 let value = parse_expr(py, value); |
424 | 515 |
425 Statement::Assign(arguments, value) | 516 stmt::Assign(arguments, value) |
426 } else if is_instance(&ast, &aug_assign_type) { | 517 } else if is_instance(&ast, &aug_assign_type) { |
427 let target = ast.getattr(py, "target").unwrap(); | 518 let target = ast.getattr(py, "target").unwrap(); |
428 let op = ast.getattr(py, "op").unwrap(); | 519 let op = ast.getattr(py, "op").unwrap(); |
429 let value = ast.getattr(py, "value").unwrap(); | 520 let value = ast.getattr(py, "value").unwrap(); |
430 | 521 |
431 let target = parse_expr(py, target); | 522 let target = parse_expr(py, target); |
432 let op = parse_binop(py, op); | 523 let op = parse_operator(py, op); |
433 let value = parse_expr(py, value); | 524 let value = parse_expr(py, value); |
434 | 525 |
435 Statement::AugAssign(target, op, value) | 526 stmt::AugAssign(target, op, value) |
436 } else if is_instance(&ast, &import_from_type) { | 527 } else if is_instance(&ast, &import_from_type) { |
437 let module = ast.getattr(py, "module").unwrap(); | 528 let module = ast.getattr(py, "module").unwrap(); |
438 let names = ast.getattr(py, "names").unwrap(); | 529 let names = ast.getattr(py, "names").unwrap(); |
439 //let level = ast.getattr(py, "level").unwrap(); | 530 let level = ast.getattr(py, "level").unwrap(); |
440 | 531 |
441 let module = get_str(py, module); | 532 let module = get_str(py, module); |
442 | 533 |
443 let mut names_ = vec!(); | 534 let mut names_ = vec!(); |
444 for alias in names.iter(py).unwrap() { | 535 for alias in names.iter(py).unwrap() { |
445 let alias = alias.unwrap(); | 536 let alias = alias.unwrap(); |
446 let alias = parse_expr(py, alias); | 537 let alias = parse_expr(py, alias); |
447 names_.push(alias); | 538 println!("{:?}", alias); |
448 } | 539 // XXX |
449 | 540 //names_.push(alias{name: alias, asname: alias}); |
450 Statement::ImportFrom(module, names_) | 541 } |
542 | |
543 stmt::ImportFrom(module, names_, None) | |
451 } else if is_instance(&ast, &return_type) { | 544 } else if is_instance(&ast, &return_type) { |
452 let value = ast.getattr(py, "value").unwrap(); | 545 let value = ast.getattr(py, "value").unwrap(); |
453 let value = parse_expr(py, value); | 546 if value == py.None() { |
454 Statement::Return(value) | 547 stmt::Return(None) |
548 } else { | |
549 let value = parse_expr(py, value); | |
550 stmt::Return(Some(value)) | |
551 } | |
455 } else if is_instance(&ast, &expr_type) { | 552 } else if is_instance(&ast, &expr_type) { |
456 let value = ast.getattr(py, "value").unwrap(); | 553 let value = ast.getattr(py, "value").unwrap(); |
457 let value = parse_expr(py, value); | 554 let value = parse_expr(py, value); |
458 Statement::Expr(value) | 555 stmt::Expr(value) |
459 } else if is_instance(&ast, &break_type) { | 556 } else if is_instance(&ast, &break_type) { |
460 Statement::Break | 557 stmt::Break |
461 } else { | 558 } else { |
462 println!("Statement {}", ast); | 559 println!("stmt {}", ast); |
463 Statement::Error | 560 panic!() |
464 } | 561 } |
465 } | 562 } |
466 | 563 |
467 #[allow(dead_code)] | 564 #[allow(dead_code)] |
468 pub fn convert_ast(name: String, module: &PyObject) -> Module { | 565 pub fn convert_ast(name: String, module: &PyObject) -> Module { |