Mercurial > python-compiler.rs
comparison src/ast_dump.rs @ 55:32c78b275d5a
Implement comprehension’s to_string.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Sun, 12 Jun 2016 02:27:57 +0100 |
parents | ad2453a55820 |
children | c3cc16b933d2 |
comparison
equal
deleted
inserted
replaced
54:ad2453a55820 | 55:32c78b275d5a |
---|---|
95 } else { | 95 } else { |
96 format!("{}\n{}else:\n{}", body, make_indent(indent), statements_to_string(indent, orelse)) | 96 format!("{}\n{}else:\n{}", body, make_indent(indent), statements_to_string(indent, orelse)) |
97 } | 97 } |
98 } | 98 } |
99 | 99 |
100 fn generators_to_string(generators: Vec<comprehension>) -> String { | 100 impl to_string_able for comprehension { |
101 let mut result = vec!(); | 101 fn to_string(&self) -> String { |
102 for generator in generators { | 102 let mut result = vec!(); |
103 result.push(format!("for {} in {}", generator.target.to_string(), generator.iter.to_string())); | 103 result.push(format!("for {} in {}", self.target.to_string(), self.iter.to_string())); |
104 for if_ in generator.ifs { | 104 for if_ in self.ifs.clone() { |
105 result.push(format!("if {}", if_.to_string())) | 105 result.push(format!("if {}", if_.to_string())) |
106 } | 106 } |
107 } | 107 result.join(" ") |
108 result.join(" ") | 108 } |
109 } | 109 } |
110 | 110 |
111 impl to_string_able for expr { | 111 impl to_string_able for expr { |
112 fn to_string(&self) -> String { | 112 fn to_string(&self) -> String { |
113 match self.clone() { | 113 match self.clone() { |
153 expr::Str(s) => format!("\"{}\"", s), | 153 expr::Str(s) => format!("\"{}\"", s), |
154 expr::NameConstant(name) => format!("{}", name), | 154 expr::NameConstant(name) => format!("{}", name), |
155 expr::Attribute(value, attr, ctx) => format!("{}.{}", value.to_string(), attr), | 155 expr::Attribute(value, attr, ctx) => format!("{}.{}", value.to_string(), attr), |
156 expr::Name(name, ctx) => format!("{}", name), | 156 expr::Name(name, ctx) => format!("{}", name), |
157 expr::List(elts, ctx) => format!("[{}]", vec_to_strings_vec(elts).join(", ")), | 157 expr::List(elts, ctx) => format!("[{}]", vec_to_strings_vec(elts).join(", ")), |
158 expr::ListComp(elt, generators) => format!("[{} {}]", elt.to_string(), generators_to_string(generators)), | 158 expr::ListComp(elt, generators) => format!("[{} {}]", elt.to_string(), vec_to_strings_vec(generators).join(" ")), |
159 expr::DictComp(key, value, generators) => format!("{{{}: {} {}}}", key.to_string(), value.to_string(), generators_to_string(generators)), | 159 expr::DictComp(key, value, generators) => format!("{{{}: {} {}}}", key.to_string(), value.to_string(), vec_to_strings_vec(generators).join(" ")), |
160 expr::Tuple(elts, ctx) => format!("({})", vec_to_strings_vec(elts).join(", ")), | 160 expr::Tuple(elts, ctx) => format!("({})", vec_to_strings_vec(elts).join(", ")), |
161 expr::Ellipsis => format!("..."), | 161 expr::Ellipsis => format!("..."), |
162 } | 162 } |
163 } | 163 } |
164 } | 164 } |