changeset 17:f1c77634a2eb

Add ast.Tuple
author Bastien Orivel <eijebong@bananium.fr>
date Thu, 02 Jun 2016 23:47:54 +0200
parents b21a246349f3
children 51ef651266b1
files example/dictcomp.py src/ast_convert.rs src/ast_dump.rs src/python_ast.rs
diffstat 4 files changed, 16 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/example/dictcomp.py
+++ b/example/dictcomp.py
@@ -1,1 +1,1 @@
-d = {key:  4 for key in imaginary_list if key % 12}
+d = {key:  value for key, value in imaginary_dict.items() if key % 12}
--- a/src/ast_convert.rs
+++ b/src/ast_convert.rs
@@ -217,6 +217,7 @@ fn parse_expr(py: Python, ast: PyObject)
     let call_type = ast_module.get(py, "Call").unwrap();
     let listcomp_type = ast_module.get(py, "ListComp").unwrap();
     let dictcomp_type = ast_module.get(py, "DictComp").unwrap();
+    let tuple_type = ast_module.get(py, "Tuple").unwrap();
 
     assert!(is_instance(&ast, &ast_type));
 
@@ -352,6 +353,17 @@ fn parse_expr(py: Python, ast: PyObject)
             new_gens.push(gen);
         }
         expr::DictComp(Box::new(key), Box::new(value), new_gens)
+    } else if is_instance(&ast, &tuple_type) {
+        let elts = ast.getattr(py, "elts").unwrap();
+        let elts = elts.iter(py).unwrap();
+
+        let mut new_elts = vec!();
+        for elt in elts {
+            let elt = elt.unwrap();
+            let elt = parse_expr(py, elt);
+            new_elts.push(elt);
+        }
+        expr::Tuple(new_elts, expr_context::Load)
     } else {
         println!("expr {}", ast);
         unreachable!()
--- a/src/ast_dump.rs
+++ b/src/ast_dump.rs
@@ -148,7 +148,8 @@ impl expr {
             expr::Name(name, ctx) => format!("{}", name),
             expr::List(elts, ctx) => format!("[{}]", args_to_string(elts)),
             expr::ListComp(elt, generators) => format!("[{} {}]", elt.to_string(), generators_to_string(generators)),
-            expr::DictComp(key, value, generators) => format!("{{{}: {} {}}}", key.to_string(), value.to_string(), generators_to_string(generators))
+            expr::DictComp(key, value, generators) => format!("{{{}: {} {}}}", key.to_string(), value.to_string(), generators_to_string(generators)),
+            expr::Tuple(elts, ctx) => format!("({})", args_to_string(elts))
         }
     }
 }
--- a/src/python_ast.rs
+++ b/src/python_ast.rs
@@ -111,7 +111,7 @@ pub enum expr {
     //Starred(Box<expr>, expr_context),
     Name(String, expr_context),
     List(Vec<expr>, expr_context),
-    //Tuple(Vec<expr>, expr_context),
+    Tuple(Vec<expr>, expr_context),
 }
 
 #[derive(Clone, Debug, PartialEq, Eq, Hash)]