Mercurial > python-compiler.rs
diff src/ast_dump.rs @ 14:719a27f1c1c7
Add ast.ListComp
author | Bastien Orivel <eijebong@bananium.fr> |
---|---|
date | Thu, 02 Jun 2016 20:40:24 +0200 |
parents | 38b0d63697b1 |
children | b21a246349f3 |
line wrap: on
line diff
--- a/src/ast_dump.rs +++ b/src/ast_dump.rs @@ -1,4 +1,4 @@ -use python_ast::{Module, stmt, expr, boolop, operator, unaryop, cmpop, arguments, arg, keyword}; +use python_ast::{Module, stmt, expr, boolop, operator, unaryop, cmpop, arguments, arg, keyword, comprehension}; use std::iter; @@ -103,6 +103,16 @@ fn if_else_statements_to_string(indent: } } +fn generators_to_string(generators: Vec<comprehension>) -> String { + let mut result = vec!(); + for generator in generators { + result.push(format!("for {} in {}", generator.target.to_string(), generator.iter.to_string())); + for if_ in generator.ifs { + result.push(format!("if {}", if_.to_string())) + } + } + result.join(" ") +} impl expr { fn to_string(&self) -> String { match self.clone() { @@ -136,7 +146,8 @@ impl expr { expr::NameConstant(name) => format!("{}", name), expr::Attribute(value, attr, ctx) => format!("{}.{}", value.to_string(), attr), expr::Name(name, ctx) => format!("{}", name), - expr::List(elts, ctx) => format!("[{}]", args_to_string(elts)) + expr::List(elts, ctx) => format!("[{}]", args_to_string(elts)), + expr::ListComp(elt, generators) => format!("[{} {}]", elt.to_string(), generators_to_string(generators)) } } }