Mercurial > python-compiler.rs
changeset 81:dc82a0d8f144
Add ast.Dict.
author | Bastien Orivel <eijebong@bananium.fr> |
---|---|
date | Mon, 13 Jun 2016 20:45:19 +0200 |
parents | c6d3f0dabbba |
children | 2d906d1cb940 |
files | src/ast_convert.rs src/ast_dump.rs src/python_ast.rs tests/test_parse_files/test_dict.py |
diffstat | 4 files changed, 27 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/src/ast_convert.rs +++ b/src/ast_convert.rs @@ -309,6 +309,7 @@ fn parse_expr(py: Python, ast: PyObject) let generatorexp_type = ast_module.get(py, "GeneratorExp").unwrap(); let lambda_type = ast_module.get(py, "Lambda").unwrap(); let ifexp_type = ast_module.get(py, "IfExp").unwrap(); + let dict_type = ast_module.get(py, "Dict").unwrap(); assert!(is_instance(&ast, &ast_type)); @@ -464,6 +465,14 @@ fn parse_expr(py: Python, ast: PyObject) let orelse = parse_expr(py, orelse); expr::IfExp(Box::new(test), Box::new(body), Box::new(orelse)) + } else if is_instance(&ast, &dict_type) { + let keys = ast.getattr(py, "keys").unwrap(); + let values = ast.getattr(py, "values").unwrap(); + + let keys = parse_list(py, keys, parse_optional_expr); + let values = parse_list(py, values, parse_expr); + + expr::Dict(keys, values) } else { println!("expr {}", ast); unreachable!()
--- a/src/ast_dump.rs +++ b/src/ast_dump.rs @@ -199,6 +199,21 @@ impl to_string_able for expr { expr::GeneratorExp(elt, generators) => format!("({} {})", elt.to_string(), vec_to_strings_vec(generators).join(" ")), expr::Lambda(args, body) => format!("lambda {}: {}", args.to_string(), body.to_string()), expr::IfExp(test, body, orelse) => format!("{} if {} else {}", body.to_string(), test.to_string(), orelse.to_string()), + expr::Dict(keys, values) => { + format!("{{{}}}", + { + let mut items = vec!(); + for (key, value) in keys.iter().zip(values.iter()) { + let item = match *key { + None => format!("**{}", value.to_string()), + Some(ref key) => format!("{}: {}", key.to_string(), value.to_string()), + }; + items.push(item); + } + items.join(", ") + } + ) + }, } } }
--- a/src/python_ast.rs +++ b/src/python_ast.rs @@ -83,7 +83,7 @@ pub enum expr { UnaryOp(unaryop, Box<expr>), Lambda(Box<arguments>, Box<expr>), IfExp(Box<expr>, Box<expr>, Box<expr>), - //Dict(Vec<expr>, Vec<expr>) + Dict(Vec<Option<expr>>, Vec<expr>), Set(Vec<expr>), ListComp(Box<expr>, Vec<comprehension>), SetComp(Box<expr>, Vec<comprehension>),