Mercurial > python-compiler.rs
diff 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 |
line wrap: on
line diff
--- a/src/python_dump.rs +++ b/src/python_dump.rs @@ -30,6 +30,7 @@ fn dump(py: Python, indent: usize, ast: let eq_type = ast_module.get(py, "Eq").unwrap(); let lt_type = ast_module.get(py, "Lt").unwrap(); let if_type = ast_module.get(py, "If").unwrap(); + let for_type = ast_module.get(py, "For").unwrap(); let compare_type = ast_module.get(py, "Compare").unwrap(); let expr_type = ast_module.get(py, "Expr").unwrap(); let call_type = ast_module.get(py, "Call").unwrap(); @@ -91,6 +92,26 @@ fn dump(py: Python, indent: usize, ast: let indent = indent.as_str(); let body = statements.join(indent); format!("{}{}", declaration, body) + } else if is_instance(&ast, &for_type) { + let target = ast.getattr(py, "target").unwrap(); + let iter = ast.getattr(py, "iter").unwrap(); + let body = ast.getattr(py, "body").unwrap(); + //let orelse = ast.getattr(py, "orelse").unwrap(); + + let target = dump(py, indent, target); + let iter = dump(py, indent, iter); + + let declaration = format!("for {} in {}:", target, iter); + let mut statements = vec!("".to_string()); + for statement in body.iter(py).unwrap() { + let statement = dump(py, indent + 1, statement.unwrap()); + statements.push(statement); + } + let indent: String = iter::repeat(" ").take(indent + 1).collect(); + let indent = format!("\n{}", indent); + let indent = indent.as_str(); + let body = statements.join(indent); + format!("{}{}", declaration, body) } else if is_instance(&ast, &arguments_type) { let args_list = ast.getattr(py, "args").unwrap(); let mut arguments = vec!();