Mercurial > python-compiler.rs
changeset 7:680d15073f55
Add ast.List literal.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Tue, 31 May 2016 04:26:58 +0100 |
parents | 6f2bf13f4cb5 |
children | 94ff501bf336 |
files | src/ast_convert.rs src/ast_dump.rs src/ast_type.rs src/python_ast.rs |
diffstat | 4 files changed, 21 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/src/ast_convert.rs +++ b/src/ast_convert.rs @@ -119,6 +119,7 @@ fn parse_expr(py: Python, ast: PyObject) 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(); + let list_type = ast_module.get(py, "List").unwrap(); let compare_type = ast_module.get(py, "Compare").unwrap(); let call_type = ast_module.get(py, "Call").unwrap(); let alias_type = ast_module.get(py, "alias").unwrap(); @@ -153,6 +154,16 @@ fn parse_expr(py: Python, ast: PyObject) let s = ast.getattr(py, "s").unwrap(); let s = get_str(py, s); Expr::Str(s) + } else if is_instance(&ast, &list_type) { + let elts = ast.getattr(py, "elts").unwrap(); + + let mut elements = vec!(); + for elt in elts.iter(py).unwrap() { + let elt = elt.unwrap(); + elements.push(parse_expr(py, elt)); + } + + Expr::List(elements) } else if is_instance(&ast, &unary_op_type) { let op = ast.getattr(py, "op").unwrap(); let operand = ast.getattr(py, "operand").unwrap();
--- a/src/ast_dump.rs +++ b/src/ast_dump.rs @@ -70,6 +70,13 @@ impl Expr { Expr::NameConstant(name) => format!("{}", name), Expr::Str(s) => format!("\"{}\"", s), Expr::Num(n) => format!("{}", n), + Expr::List(elts) => format!("[{}]", { + let mut elements = vec!(); + for elt in elts { + elements.push(elt.to_string()); + } + elements.join(", ") + }), Expr::Error => "Expr::Error".to_string() } }
--- a/src/ast_type.rs +++ b/src/ast_type.rs @@ -9,6 +9,7 @@ enum Type { Bool, Int, Str, + List(Box<Type>), Function(Vec<Type>, Vec<Type>), Bottom } @@ -236,6 +237,7 @@ impl Visitor<Type> for Typing { }, Expr::Str(_) => Type::Str, Expr::Num(_) => Type::Int, + Expr::List(_) => Type::List(Box::new(Type::Bottom)), Expr::Error => { println!("Expr::Error"); panic!()