comparison src/ast_convert.rs @ 16:b21a246349f3

Add ast.DictComp
author Bastien Orivel <eijebong@bananium.fr>
date Thu, 02 Jun 2016 22:20:48 +0200
parents a0fb169fe0f9
children f1c77634a2eb
comparison
equal deleted inserted replaced
15:a0fb169fe0f9 16:b21a246349f3
214 let str_type = ast_module.get(py, "Str").unwrap(); 214 let str_type = ast_module.get(py, "Str").unwrap();
215 let list_type = ast_module.get(py, "List").unwrap(); 215 let list_type = ast_module.get(py, "List").unwrap();
216 let compare_type = ast_module.get(py, "Compare").unwrap(); 216 let compare_type = ast_module.get(py, "Compare").unwrap();
217 let call_type = ast_module.get(py, "Call").unwrap(); 217 let call_type = ast_module.get(py, "Call").unwrap();
218 let listcomp_type = ast_module.get(py, "ListComp").unwrap(); 218 let listcomp_type = ast_module.get(py, "ListComp").unwrap();
219 let dictcomp_type = ast_module.get(py, "DictComp").unwrap();
219 220
220 assert!(is_instance(&ast, &ast_type)); 221 assert!(is_instance(&ast, &ast_type));
221 222
222 if is_instance(&ast, &arg_type) { 223 if is_instance(&ast, &arg_type) {
223 let arg = ast.getattr(py, "arg").unwrap(); 224 let arg = ast.getattr(py, "arg").unwrap();
333 let gen = gen.unwrap(); 334 let gen = gen.unwrap();
334 let gen = parse_comprehension(py, gen); 335 let gen = parse_comprehension(py, gen);
335 new_gens.push(gen); 336 new_gens.push(gen);
336 } 337 }
337 expr::ListComp(Box::new(elt), new_gens) 338 expr::ListComp(Box::new(elt), new_gens)
338 } 339 } else if is_instance(&ast, &dictcomp_type) {
339 else { 340 let key = ast.getattr(py, "key").unwrap();
341 let value = ast.getattr(py, "value").unwrap();
342 let generators = ast.getattr(py, "generators").unwrap();
343 let generators = generators.iter(py).unwrap();
344
345 let key = parse_expr(py, key);
346 let value = parse_expr(py, value);
347
348 let mut new_gens = vec!();
349 for gen in generators {
350 let gen = gen.unwrap();
351 let gen = parse_comprehension(py, gen);
352 new_gens.push(gen);
353 }
354 expr::DictComp(Box::new(key), Box::new(value), new_gens)
355 } else {
340 println!("expr {}", ast); 356 println!("expr {}", ast);
341 unreachable!() 357 unreachable!()
342 } 358 }
343 } 359 }
344 360