Mercurial > python-compiler.rs
comparison src/ast_convert.rs @ 14:719a27f1c1c7
Add ast.ListComp
author | Bastien Orivel <eijebong@bananium.fr> |
---|---|
date | Thu, 02 Jun 2016 20:40:24 +0200 |
parents | 38b0d63697b1 |
children | a0fb169fe0f9 |
comparison
equal
deleted
inserted
replaced
13:38b0d63697b1 | 14:719a27f1c1c7 |
---|---|
1 use python_ast::{Module, stmt, expr, expr_context, cmpop, operator, unaryop, arguments, arg, alias}; | 1 use python_ast::{Module, stmt, expr, expr_context, cmpop, 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 { |
96 } else if is_instance(&ast, >_type) { | 96 } else if is_instance(&ast, >_type) { |
97 cmpop::Gt | 97 cmpop::Gt |
98 } else { | 98 } else { |
99 unreachable!() | 99 unreachable!() |
100 } | 100 } |
101 } | |
102 | |
103 fn parse_comprehension(py: Python, ast: PyObject) -> comprehension { | |
104 let target = ast.getattr(py, "target").unwrap(); | |
105 let iter = ast.getattr(py, "iter").unwrap(); | |
106 let ifs = ast.getattr(py, "ifs").unwrap(); | |
107 let ifs = ifs.iter(py).unwrap(); | |
108 | |
109 let target = parse_expr(py, target); | |
110 let iter = parse_expr(py, iter); | |
111 | |
112 let mut new_ifs = vec!(); | |
113 for if_ in ifs { | |
114 let if_ = parse_expr(py, if_.unwrap()); | |
115 new_ifs.push(if_); | |
116 } | |
117 comprehension {target: target, iter: iter, ifs: new_ifs} | |
118 | |
101 } | 119 } |
102 | 120 |
103 fn parse_operator(py: Python, ast: PyObject) -> operator { | 121 fn parse_operator(py: Python, ast: PyObject) -> operator { |
104 let builtins_module = py.import("builtins").unwrap(); | 122 let builtins_module = py.import("builtins").unwrap(); |
105 let isinstance = builtins_module.get(py, "isinstance").unwrap(); | 123 let isinstance = builtins_module.get(py, "isinstance").unwrap(); |
177 let num_type = ast_module.get(py, "Num").unwrap(); | 195 let num_type = ast_module.get(py, "Num").unwrap(); |
178 let str_type = ast_module.get(py, "Str").unwrap(); | 196 let str_type = ast_module.get(py, "Str").unwrap(); |
179 let list_type = ast_module.get(py, "List").unwrap(); | 197 let list_type = ast_module.get(py, "List").unwrap(); |
180 let compare_type = ast_module.get(py, "Compare").unwrap(); | 198 let compare_type = ast_module.get(py, "Compare").unwrap(); |
181 let call_type = ast_module.get(py, "Call").unwrap(); | 199 let call_type = ast_module.get(py, "Call").unwrap(); |
200 let listcomp_type = ast_module.get(py, "ListComp").unwrap(); | |
182 | 201 |
183 assert!(is_instance(&ast, &ast_type)); | 202 assert!(is_instance(&ast, &ast_type)); |
184 | 203 |
185 if is_instance(&ast, &arg_type) { | 204 if is_instance(&ast, &arg_type) { |
186 let arg = ast.getattr(py, "arg").unwrap(); | 205 let arg = ast.getattr(py, "arg").unwrap(); |
282 let comparator = parse_expr(py, comparator); | 301 let comparator = parse_expr(py, comparator); |
283 new_comparators.push(comparator); | 302 new_comparators.push(comparator); |
284 } | 303 } |
285 | 304 |
286 expr::Compare(Box::new(left), new_ops, new_comparators) | 305 expr::Compare(Box::new(left), new_ops, new_comparators) |
287 } else { | 306 } else if is_instance(&ast, &listcomp_type) { |
307 let elt = ast.getattr(py, "elt").unwrap(); | |
308 let generators = ast.getattr(py, "generators").unwrap(); | |
309 let generators = generators.iter(py).unwrap(); | |
310 | |
311 let elt = parse_expr(py, elt); | |
312 | |
313 let mut new_gens = vec!(); | |
314 for gen in generators { | |
315 let gen = gen.unwrap(); | |
316 let gen = parse_comprehension(py, gen); | |
317 new_gens.push(gen); | |
318 } | |
319 expr::ListComp(Box::new(elt), new_gens) | |
320 } | |
321 else { | |
288 println!("expr {}", ast); | 322 println!("expr {}", ast); |
289 unreachable!() | 323 unreachable!() |
290 } | 324 } |
291 } | 325 } |
292 | 326 |