Mercurial > python-compiler.rs
diff 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 |
line wrap: on
line diff
--- a/src/ast_dump.rs +++ b/src/ast_dump.rs @@ -1,7 +1,17 @@ -use python_ast::{Module, Statement, Expr, BinOp}; +use python_ast::{Module, Statement, Expr, BinOp, UnaryOp}; use std::iter; +impl UnaryOp { + fn to_string(&self) -> &'static str { + match *self { + UnaryOp::UAdd => "+", + UnaryOp::USub => "-", + UnaryOp::Error => "BinOp::Error" + } + } +} + impl BinOp { fn to_string(&self) -> &'static str { match *self { @@ -9,6 +19,8 @@ impl BinOp { BinOp::BinMult => "*", BinOp::BinEq => "==", BinOp::BinLt => "<", + BinOp::Sub => "-", + BinOp::Div => "/", BinOp::Error => "BinOp::Error" } } @@ -17,6 +29,7 @@ impl BinOp { impl Expr { fn to_string(&self) -> String { match self.clone() { + Expr::UnaryOp(op, operand) => format!("{}{}", op.to_string(), operand.to_string()), Expr::BinOp(a, op, b) => format!("{} {} {}", a.to_string(), op.to_string(), b.to_string()), Expr::Compare(left, ops, comparators) => format!("{} {}", left.to_string(), { let mut arguments = vec!(); @@ -113,6 +126,23 @@ impl Statement { format!("{}\n{}else:\n{}", statements.join("\n"), make_indent(indent), orelse_.join("\n")) } }), + Statement::For(target, iter, body, orelse) => format!("{}for {} in {}:\n{}", make_indent(indent), target.to_string(), iter.to_string(), { + let mut statements = vec!(); + for arg in body { + statements.push(arg.to_string(indent + 1)); + } + + let mut orelse_ = vec!(); + for arg in orelse { + orelse_.push(arg.to_string(indent + 1)); + } + + if orelse_.is_empty() { + statements.join("\n") + } else { + format!("{}\n{}else:\n{}", statements.join("\n"), make_indent(indent), orelse_.join("\n")) + } + }), Statement::Assign(targets, value) => format!("{}{} = {}", make_indent(indent), { let mut exprs = vec!(); for target in targets {