Mercurial > python-compiler.rs
changeset 80:c6d3f0dabbba
Add ast.IfExp.
author | Bastien Orivel <eijebong@bananium.fr> |
---|---|
date | Mon, 13 Jun 2016 20:12:39 +0200 |
parents | 6bf54bff8dbd |
children | dc82a0d8f144 |
files | src/ast_convert.rs src/ast_dump.rs src/python_ast.rs tests/test_parse_files/test_ifelse.py |
diffstat | 4 files changed, 14 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/src/ast_convert.rs +++ b/src/ast_convert.rs @@ -308,6 +308,7 @@ fn parse_expr(py: Python, ast: PyObject) let setcomp_type = ast_module.get(py, "SetComp").unwrap(); 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(); assert!(is_instance(&ast, &ast_type)); @@ -453,6 +454,16 @@ fn parse_expr(py: Python, ast: PyObject) let body = parse_expr(py, body); expr::Lambda(Box::new(args), Box::new(body)) + } else if is_instance(&ast, &ifexp_type) { + let test = ast.getattr(py, "test").unwrap(); + let body = ast.getattr(py, "body").unwrap(); + let orelse = ast.getattr(py, "orelse").unwrap(); + + let test = parse_expr(py, test); + let body = parse_expr(py, body); + let orelse = parse_expr(py, orelse); + + expr::IfExp(Box::new(test), Box::new(body), Box::new(orelse)) } else { println!("expr {}", ast); unreachable!()
--- a/src/ast_dump.rs +++ b/src/ast_dump.rs @@ -198,6 +198,7 @@ impl to_string_able for expr { expr::SetComp(elt, generators) => format!("{{{} {}}}", elt.to_string(), vec_to_strings_vec(generators).join(" ")), 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()), } } }
--- a/src/python_ast.rs +++ b/src/python_ast.rs @@ -82,7 +82,7 @@ pub enum expr { BinOp(Box<expr>, operator, Box<expr>), UnaryOp(unaryop, Box<expr>), Lambda(Box<arguments>, Box<expr>), - //IfExp(Box<expr>, Box<expr>, Box<expr>) + IfExp(Box<expr>, Box<expr>, Box<expr>), //Dict(Vec<expr>, Vec<expr>) Set(Vec<expr>), ListComp(Box<expr>, Vec<comprehension>),