comparison src/ast_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 f27a4aee9dfa
children 6f2bf13f4cb5
comparison
equal deleted inserted replaced
4:f27a4aee9dfa 5:ddf372373a77
1 use python_ast::{Module, Statement, Expr, BinOp}; 1 use python_ast::{Module, Statement, Expr, BinOp, UnaryOp};
2 2
3 use std::iter; 3 use std::iter;
4
5 impl UnaryOp {
6 fn to_string(&self) -> &'static str {
7 match *self {
8 UnaryOp::UAdd => "+",
9 UnaryOp::USub => "-",
10 UnaryOp::Error => "BinOp::Error"
11 }
12 }
13 }
4 14
5 impl BinOp { 15 impl BinOp {
6 fn to_string(&self) -> &'static str { 16 fn to_string(&self) -> &'static str {
7 match *self { 17 match *self {
8 BinOp::BinAdd => "+", 18 BinOp::BinAdd => "+",
9 BinOp::BinMult => "*", 19 BinOp::BinMult => "*",
10 BinOp::BinEq => "==", 20 BinOp::BinEq => "==",
11 BinOp::BinLt => "<", 21 BinOp::BinLt => "<",
22 BinOp::Sub => "-",
23 BinOp::Div => "/",
12 BinOp::Error => "BinOp::Error" 24 BinOp::Error => "BinOp::Error"
13 } 25 }
14 } 26 }
15 } 27 }
16 28
17 impl Expr { 29 impl Expr {
18 fn to_string(&self) -> String { 30 fn to_string(&self) -> String {
19 match self.clone() { 31 match self.clone() {
32 Expr::UnaryOp(op, operand) => format!("{}{}", op.to_string(), operand.to_string()),
20 Expr::BinOp(a, op, b) => format!("{} {} {}", a.to_string(), op.to_string(), b.to_string()), 33 Expr::BinOp(a, op, b) => format!("{} {} {}", a.to_string(), op.to_string(), b.to_string()),
21 Expr::Compare(left, ops, comparators) => format!("{} {}", left.to_string(), { 34 Expr::Compare(left, ops, comparators) => format!("{} {}", left.to_string(), {
22 let mut arguments = vec!(); 35 let mut arguments = vec!();
23 36
24 // XXX: wrong order! 37 // XXX: wrong order!
111 statements.join("\n") 124 statements.join("\n")
112 } else { 125 } else {
113 format!("{}\n{}else:\n{}", statements.join("\n"), make_indent(indent), orelse_.join("\n")) 126 format!("{}\n{}else:\n{}", statements.join("\n"), make_indent(indent), orelse_.join("\n"))
114 } 127 }
115 }), 128 }),
129 Statement::For(target, iter, body, orelse) => format!("{}for {} in {}:\n{}", make_indent(indent), target.to_string(), iter.to_string(), {
130 let mut statements = vec!();
131 for arg in body {
132 statements.push(arg.to_string(indent + 1));
133 }
134
135 let mut orelse_ = vec!();
136 for arg in orelse {
137 orelse_.push(arg.to_string(indent + 1));
138 }
139
140 if orelse_.is_empty() {
141 statements.join("\n")
142 } else {
143 format!("{}\n{}else:\n{}", statements.join("\n"), make_indent(indent), orelse_.join("\n"))
144 }
145 }),
116 Statement::Assign(targets, value) => format!("{}{} = {}", make_indent(indent), { 146 Statement::Assign(targets, value) => format!("{}{} = {}", make_indent(indent), {
117 let mut exprs = vec!(); 147 let mut exprs = vec!();
118 for target in targets { 148 for target in targets {
119 exprs.push(target.to_string()); 149 exprs.push(target.to_string());
120 } 150 }