Mercurial > python-compiler.rs
comparison src/ast_convert.rs @ 19:0cb53a31ac12
Implement ast.BoolOp, and improve its display.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Fri, 03 Jun 2016 00:55:24 +0100 |
parents | 51ef651266b1 |
children | ace12d6b9855 |
comparison
equal
deleted
inserted
replaced
18:51ef651266b1 | 19:0cb53a31ac12 |
---|---|
1 use python_ast::{Module, stmt, expr, expr_context, cmpop, operator, unaryop, arguments, arg, alias, comprehension}; | 1 use python_ast::{Module, stmt, expr, expr_context, cmpop, boolop, operator, unaryop, arguments, arg, alias, comprehension}; |
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 { |
29 } else{ | 29 } else{ |
30 unreachable!(); | 30 unreachable!(); |
31 } | 31 } |
32 } | 32 } |
33 | 33 |
34 fn parse_expr_vec(py: Python, ast: PyObject) -> Vec<String> { | 34 fn parse_expr_vec(py: Python, list: PyObject) -> Vec<expr> { |
35 let mut exprs = vec!(); | |
36 for item in list.iter(py).unwrap() { | |
37 let item = item.unwrap(); | |
38 let item = parse_expr(py, item); | |
39 exprs.push(item); | |
40 } | |
41 exprs | |
42 } | |
43 | |
44 fn parse_string_vec(py: Python, ast: PyObject) -> Vec<String> { | |
35 let builtins_module = py.import("builtins").unwrap(); | 45 let builtins_module = py.import("builtins").unwrap(); |
36 let isinstance = builtins_module.get(py, "isinstance").unwrap(); | 46 let isinstance = builtins_module.get(py, "isinstance").unwrap(); |
37 | 47 |
38 let is_instance = |object: &PyObject, type_: &PyObject| { | 48 let is_instance = |object: &PyObject, type_: &PyObject| { |
39 return isinstance.call(py, (object, type_), None).unwrap().is_true(py).unwrap(); | 49 return isinstance.call(py, (object, type_), None).unwrap().is_true(py).unwrap(); |
85 unaryop::Not | 95 unaryop::Not |
86 } else if is_instance(&ast, &uadd_type) { | 96 } else if is_instance(&ast, &uadd_type) { |
87 unaryop::UAdd | 97 unaryop::UAdd |
88 } else if is_instance(&ast, &usub_type) { | 98 } else if is_instance(&ast, &usub_type) { |
89 unaryop::USub | 99 unaryop::USub |
100 } else { | |
101 unreachable!() | |
102 } | |
103 } | |
104 | |
105 fn parse_boolop(py: Python, ast: PyObject) -> boolop { | |
106 let builtins_module = py.import("builtins").unwrap(); | |
107 let isinstance = builtins_module.get(py, "isinstance").unwrap(); | |
108 | |
109 let is_instance = |object: &PyObject, type_: &PyObject| { | |
110 return isinstance.call(py, (object, type_), None).unwrap().is_true(py).unwrap(); | |
111 }; | |
112 | |
113 let ast_module = py.import("ast").unwrap(); | |
114 let ast_type = ast_module.get(py, "AST").unwrap(); | |
115 let and_type = ast_module.get(py, "And").unwrap(); | |
116 let or_type = ast_module.get(py, "Or").unwrap(); | |
117 | |
118 assert!(is_instance(&ast, &ast_type)); | |
119 | |
120 if is_instance(&ast, &and_type) { | |
121 boolop::And | |
122 } else if is_instance(&ast, &or_type) { | |
123 boolop::Or | |
90 } else { | 124 } else { |
91 unreachable!() | 125 unreachable!() |
92 } | 126 } |
93 } | 127 } |
94 | 128 |
226 | 260 |
227 let ast_module = py.import("ast").unwrap(); | 261 let ast_module = py.import("ast").unwrap(); |
228 let ast_type = ast_module.get(py, "AST").unwrap(); | 262 let ast_type = ast_module.get(py, "AST").unwrap(); |
229 let arg_type = ast_module.get(py, "arg").unwrap(); | 263 let arg_type = ast_module.get(py, "arg").unwrap(); |
230 let unary_op_type = ast_module.get(py, "UnaryOp").unwrap(); | 264 let unary_op_type = ast_module.get(py, "UnaryOp").unwrap(); |
265 let bool_op_type = ast_module.get(py, "BoolOp").unwrap(); | |
231 let bin_op_type = ast_module.get(py, "BinOp").unwrap(); | 266 let bin_op_type = ast_module.get(py, "BinOp").unwrap(); |
232 let name_constant_type = ast_module.get(py, "NameConstant").unwrap(); | 267 let name_constant_type = ast_module.get(py, "NameConstant").unwrap(); |
233 let attribute_type = ast_module.get(py, "Attribute").unwrap(); | 268 let attribute_type = ast_module.get(py, "Attribute").unwrap(); |
234 let name_type = ast_module.get(py, "Name").unwrap(); | 269 let name_type = ast_module.get(py, "Name").unwrap(); |
235 let num_type = ast_module.get(py, "Num").unwrap(); | 270 let num_type = ast_module.get(py, "Num").unwrap(); |
287 | 322 |
288 let op = parse_unaryop(py, op); | 323 let op = parse_unaryop(py, op); |
289 let operand = parse_expr(py, operand); | 324 let operand = parse_expr(py, operand); |
290 | 325 |
291 expr::UnaryOp(op, Box::new(operand)) | 326 expr::UnaryOp(op, Box::new(operand)) |
327 } else if is_instance(&ast, &bool_op_type) { | |
328 let op = ast.getattr(py, "op").unwrap(); | |
329 let values = ast.getattr(py, "values").unwrap(); | |
330 | |
331 let op = parse_boolop(py, op); | |
332 let values = parse_expr_vec(py, values); | |
333 | |
334 expr::BoolOp(op, values) | |
292 } else if is_instance(&ast, &bin_op_type) { | 335 } else if is_instance(&ast, &bin_op_type) { |
293 let left = ast.getattr(py, "left").unwrap(); | 336 let left = ast.getattr(py, "left").unwrap(); |
294 let op = ast.getattr(py, "op").unwrap(); | 337 let op = ast.getattr(py, "op").unwrap(); |
295 let right = ast.getattr(py, "right").unwrap(); | 338 let right = ast.getattr(py, "right").unwrap(); |
296 | 339 |