changeset 4:f27a4aee9dfa

Add ast.Attribute.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Tue, 31 May 2016 03:59:05 +0100
parents 326d7f2a94d4
children ddf372373a77
files example/class.py src/ast_convert.rs src/ast_dump.rs src/ast_type.rs src/python_ast.rs
diffstat 5 files changed, 22 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/example/class.py
+++ b/example/class.py
@@ -5,3 +5,9 @@ class A:
 class B(A):
     def b(self):
         print('b')
+
+a = A()
+b = B()
+a.a()
+b.b()
+b.a()
--- a/src/ast_convert.rs
+++ b/src/ast_convert.rs
@@ -80,6 +80,7 @@ fn parse_expr(py: Python, ast: PyObject)
     let arg_type = ast_module.get(py, "arg").unwrap();
     let bin_op_type = ast_module.get(py, "BinOp").unwrap();
     let name_constant_type = ast_module.get(py, "NameConstant").unwrap();
+    let attribute_type = ast_module.get(py, "Attribute").unwrap();
     let name_type = ast_module.get(py, "Name").unwrap();
     let num_type = ast_module.get(py, "Num").unwrap();
     let str_type = ast_module.get(py, "Str").unwrap();
@@ -93,6 +94,14 @@ fn parse_expr(py: Python, ast: PyObject)
         let arg = ast.getattr(py, "arg").unwrap();
         let arg = get_str(py, arg);
         Expr::Name(arg)
+    } else if is_instance(&ast, &attribute_type) {
+        let value = ast.getattr(py, "value").unwrap();
+        let attr = ast.getattr(py, "attr").unwrap();
+
+        let value = parse_expr(py, value);
+        let attr = get_str(py, attr);
+
+        Expr::Attribute(Box::new(value), attr)
     } else if is_instance(&ast, &name_type) {
         let id = ast.getattr(py, "id").unwrap();
         let id = get_str(py, id);
--- a/src/ast_dump.rs
+++ b/src/ast_dump.rs
@@ -51,6 +51,7 @@ impl Expr {
                     Some(asname) => format!("{} as {}", name, asname)
                 }
             }
+            Expr::Attribute(lhs, rhs) => format!("{}.{}", lhs.to_string(), rhs),
             Expr::Name(name) => format!("{}", name),
             Expr::NameConstant(name) => format!("{}", name),
             Expr::Str(s) => format!("\"{}\"", s),
--- a/src/ast_type.rs
+++ b/src/ast_type.rs
@@ -177,6 +177,11 @@ impl Visitor<Type> for Typing {
                 }
             },
             Expr::Alias(_, _) => Type::Bottom,
+            Expr::Attribute(lhs, rhs) => {
+                let type_left = self.visit_expr(*lhs);
+                println!("coucou {:?}", type_left);
+                Type::Bottom
+            },
             Expr::Name(id) => {
                 let env = match self.environment.pop() {
                     Some(env) => env,
--- a/src/python_ast.rs
+++ b/src/python_ast.rs
@@ -23,6 +23,7 @@ pub enum Expr {
     Compare(Box<Expr>, Vec<BinOp>, Vec<Expr>),
     Call(Box<Expr>, Vec<Expr>),
     Alias(String, Option<String>),
+    Attribute(Box<Expr>, String),
     Name(String),
     NameConstant(String),
     Str(String),