comparison src/python_dump.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 b90e49ab734b
children a0b23123901b
comparison
equal deleted inserted replaced
4:f27a4aee9dfa 5:ddf372373a77
28 let add_type = ast_module.get(py, "Add").unwrap(); 28 let add_type = ast_module.get(py, "Add").unwrap();
29 let mult_type = ast_module.get(py, "Mult").unwrap(); 29 let mult_type = ast_module.get(py, "Mult").unwrap();
30 let eq_type = ast_module.get(py, "Eq").unwrap(); 30 let eq_type = ast_module.get(py, "Eq").unwrap();
31 let lt_type = ast_module.get(py, "Lt").unwrap(); 31 let lt_type = ast_module.get(py, "Lt").unwrap();
32 let if_type = ast_module.get(py, "If").unwrap(); 32 let if_type = ast_module.get(py, "If").unwrap();
33 let for_type = ast_module.get(py, "For").unwrap();
33 let compare_type = ast_module.get(py, "Compare").unwrap(); 34 let compare_type = ast_module.get(py, "Compare").unwrap();
34 let expr_type = ast_module.get(py, "Expr").unwrap(); 35 let expr_type = ast_module.get(py, "Expr").unwrap();
35 let call_type = ast_module.get(py, "Call").unwrap(); 36 let call_type = ast_module.get(py, "Call").unwrap();
36 let import_from_type = ast_module.get(py, "ImportFrom").unwrap(); 37 let import_from_type = ast_module.get(py, "ImportFrom").unwrap();
37 let alias_type = ast_module.get(py, "alias").unwrap(); 38 let alias_type = ast_module.get(py, "alias").unwrap();
79 let body = ast.getattr(py, "body").unwrap(); 80 let body = ast.getattr(py, "body").unwrap();
80 81
81 let test = dump(py, indent, test); 82 let test = dump(py, indent, test);
82 83
83 let declaration = format!("if {}:", test); 84 let declaration = format!("if {}:", test);
85 let mut statements = vec!("".to_string());
86 for statement in body.iter(py).unwrap() {
87 let statement = dump(py, indent + 1, statement.unwrap());
88 statements.push(statement);
89 }
90 let indent: String = iter::repeat(" ").take(indent + 1).collect();
91 let indent = format!("\n{}", indent);
92 let indent = indent.as_str();
93 let body = statements.join(indent);
94 format!("{}{}", declaration, body)
95 } else if is_instance(&ast, &for_type) {
96 let target = ast.getattr(py, "target").unwrap();
97 let iter = ast.getattr(py, "iter").unwrap();
98 let body = ast.getattr(py, "body").unwrap();
99 //let orelse = ast.getattr(py, "orelse").unwrap();
100
101 let target = dump(py, indent, target);
102 let iter = dump(py, indent, iter);
103
104 let declaration = format!("for {} in {}:", target, iter);
84 let mut statements = vec!("".to_string()); 105 let mut statements = vec!("".to_string());
85 for statement in body.iter(py).unwrap() { 106 for statement in body.iter(py).unwrap() {
86 let statement = dump(py, indent + 1, statement.unwrap()); 107 let statement = dump(py, indent + 1, statement.unwrap());
87 statements.push(statement); 108 statements.push(statement);
88 } 109 }