comparison src/ast_convert.rs @ 5:ddf372373a77

Add ast.For, ast.UnaryOp, and Sub and Div to ast.BinOp.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Tue, 31 May 2016 04:15:00 +0100
parents f27a4aee9dfa
children 6f2bf13f4cb5
comparison
equal deleted inserted replaced
4:f27a4aee9dfa 5:ddf372373a77
1 use python_ast::{Module, Statement, Expr, BinOp}; 1 use python_ast::{Module, Statement, Expr, BinOp, UnaryOp};
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 {
34 } else { 34 } else {
35 vec!(Expr::Error) 35 vec!(Expr::Error)
36 } 36 }
37 } 37 }
38 38
39 fn parse_unaryop(py: Python, ast: PyObject) -> UnaryOp {
40 let builtins_module = py.import("builtins").unwrap();
41 let isinstance = builtins_module.get(py, "isinstance").unwrap();
42
43 let is_instance = |object: &PyObject, type_: &PyObject| {
44 return isinstance.call(py, (object, type_), None).unwrap().is_true(py).unwrap();
45 };
46
47 let ast_module = py.import("ast").unwrap();
48 let ast_type = ast_module.get(py, "AST").unwrap();
49 let uadd_type = ast_module.get(py, "UAdd").unwrap();
50 let usub_type = ast_module.get(py, "USub").unwrap();
51
52 assert!(is_instance(&ast, &ast_type));
53
54 if is_instance(&ast, &uadd_type) {
55 UnaryOp::UAdd
56 } else if is_instance(&ast, &usub_type) {
57 UnaryOp::USub
58 } else {
59 println!("UnaryOp {}", ast);
60 UnaryOp::Error
61 }
62 }
63
39 fn parse_binop(py: Python, ast: PyObject) -> BinOp { 64 fn parse_binop(py: Python, ast: PyObject) -> BinOp {
40 let builtins_module = py.import("builtins").unwrap(); 65 let builtins_module = py.import("builtins").unwrap();
41 let isinstance = builtins_module.get(py, "isinstance").unwrap(); 66 let isinstance = builtins_module.get(py, "isinstance").unwrap();
42 67
43 let is_instance = |object: &PyObject, type_: &PyObject| { 68 let is_instance = |object: &PyObject, type_: &PyObject| {
48 let ast_type = ast_module.get(py, "AST").unwrap(); 73 let ast_type = ast_module.get(py, "AST").unwrap();
49 let add_type = ast_module.get(py, "Add").unwrap(); 74 let add_type = ast_module.get(py, "Add").unwrap();
50 let mult_type = ast_module.get(py, "Mult").unwrap(); 75 let mult_type = ast_module.get(py, "Mult").unwrap();
51 let eq_type = ast_module.get(py, "Eq").unwrap(); 76 let eq_type = ast_module.get(py, "Eq").unwrap();
52 let lt_type = ast_module.get(py, "Lt").unwrap(); 77 let lt_type = ast_module.get(py, "Lt").unwrap();
78 let sub_type = ast_module.get(py, "Sub").unwrap();
79 let div_type = ast_module.get(py, "Div").unwrap();
53 80
54 assert!(is_instance(&ast, &ast_type)); 81 assert!(is_instance(&ast, &ast_type));
55 82
56 if is_instance(&ast, &add_type) { 83 if is_instance(&ast, &add_type) {
57 BinOp::BinAdd 84 BinOp::BinAdd
59 BinOp::BinMult 86 BinOp::BinMult
60 } else if is_instance(&ast, &eq_type) { 87 } else if is_instance(&ast, &eq_type) {
61 BinOp::BinEq 88 BinOp::BinEq
62 } else if is_instance(&ast, &lt_type) { 89 } else if is_instance(&ast, &lt_type) {
63 BinOp::BinLt 90 BinOp::BinLt
91 } else if is_instance(&ast, &sub_type) {
92 BinOp::Sub
93 } else if is_instance(&ast, &div_type) {
94 BinOp::Div
64 } else { 95 } else {
65 println!("BinOp {}", ast); 96 println!("BinOp {}", ast);
66 BinOp::Error 97 BinOp::Error
67 } 98 }
68 } 99 }
76 }; 107 };
77 108
78 let ast_module = py.import("ast").unwrap(); 109 let ast_module = py.import("ast").unwrap();
79 let ast_type = ast_module.get(py, "AST").unwrap(); 110 let ast_type = ast_module.get(py, "AST").unwrap();
80 let arg_type = ast_module.get(py, "arg").unwrap(); 111 let arg_type = ast_module.get(py, "arg").unwrap();
112 let unary_op_type = ast_module.get(py, "UnaryOp").unwrap();
81 let bin_op_type = ast_module.get(py, "BinOp").unwrap(); 113 let bin_op_type = ast_module.get(py, "BinOp").unwrap();
82 let name_constant_type = ast_module.get(py, "NameConstant").unwrap(); 114 let name_constant_type = ast_module.get(py, "NameConstant").unwrap();
83 let attribute_type = ast_module.get(py, "Attribute").unwrap(); 115 let attribute_type = ast_module.get(py, "Attribute").unwrap();
84 let name_type = ast_module.get(py, "Name").unwrap(); 116 let name_type = ast_module.get(py, "Name").unwrap();
85 let num_type = ast_module.get(py, "Num").unwrap(); 117 let num_type = ast_module.get(py, "Num").unwrap();
116 Expr::Num(n) 148 Expr::Num(n)
117 } else if is_instance(&ast, &str_type) { 149 } else if is_instance(&ast, &str_type) {
118 let s = ast.getattr(py, "s").unwrap(); 150 let s = ast.getattr(py, "s").unwrap();
119 let s = get_str(py, s); 151 let s = get_str(py, s);
120 Expr::Str(s) 152 Expr::Str(s)
153 } else if is_instance(&ast, &unary_op_type) {
154 let op = ast.getattr(py, "op").unwrap();
155 let operand = ast.getattr(py, "operand").unwrap();
156
157 let op = parse_unaryop(py, op);
158 let operand = parse_expr(py, operand);
159
160 Expr::UnaryOp(op, Box::new(operand))
121 } else if is_instance(&ast, &bin_op_type) { 161 } else if is_instance(&ast, &bin_op_type) {
122 let left = ast.getattr(py, "left").unwrap(); 162 let left = ast.getattr(py, "left").unwrap();
123 let op = ast.getattr(py, "op").unwrap(); 163 let op = ast.getattr(py, "op").unwrap();
124 let right = ast.getattr(py, "right").unwrap(); 164 let right = ast.getattr(py, "right").unwrap();
125 165
202 let global_type = ast_module.get(py, "Global").unwrap(); 242 let global_type = ast_module.get(py, "Global").unwrap();
203 let assign_type = ast_module.get(py, "Assign").unwrap(); 243 let assign_type = ast_module.get(py, "Assign").unwrap();
204 let return_type = ast_module.get(py, "Return").unwrap(); 244 let return_type = ast_module.get(py, "Return").unwrap();
205 let import_from_type = ast_module.get(py, "ImportFrom").unwrap(); 245 let import_from_type = ast_module.get(py, "ImportFrom").unwrap();
206 let if_type = ast_module.get(py, "If").unwrap(); 246 let if_type = ast_module.get(py, "If").unwrap();
247 let for_type = ast_module.get(py, "For").unwrap();
207 let expr_type = ast_module.get(py, "Expr").unwrap(); 248 let expr_type = ast_module.get(py, "Expr").unwrap();
208 249
209 assert!(is_instance(&ast, &ast_type)); 250 assert!(is_instance(&ast, &ast_type));
210 251
211 /* 252 /*
299 let statement = parse_statement(py, statement); 340 let statement = parse_statement(py, statement);
300 orelse_.push(statement); 341 orelse_.push(statement);
301 } 342 }
302 343
303 Statement::If(test, statements, orelse_) 344 Statement::If(test, statements, orelse_)
345 } else if is_instance(&ast, &for_type) {
346 let target = ast.getattr(py, "target").unwrap();
347 let iter = ast.getattr(py, "iter").unwrap();
348 let body = ast.getattr(py, "body").unwrap();
349 let orelse = ast.getattr(py, "orelse").unwrap();
350
351 let target = parse_expr(py, target);
352 let iter = parse_expr(py, iter);
353
354 let mut statements = vec!();
355 for statement in body.iter(py).unwrap() {
356 let statement = statement.unwrap();
357 let statement = parse_statement(py, statement);
358 statements.push(statement);
359 }
360
361 let mut orelse_ = vec!();
362 for statement in orelse.iter(py).unwrap() {
363 let statement = statement.unwrap();
364 let statement = parse_statement(py, statement);
365 orelse_.push(statement);
366 }
367
368 Statement::For(target, iter, statements, orelse_)
304 } else if is_instance(&ast, &assign_type) { 369 } else if is_instance(&ast, &assign_type) {
305 let targets = ast.getattr(py, "targets").unwrap(); 370 let targets = ast.getattr(py, "targets").unwrap();
306 let value = ast.getattr(py, "value").unwrap(); 371 let value = ast.getattr(py, "value").unwrap();
307 372
308 let mut arguments = vec!(); 373 let mut arguments = vec!();