# HG changeset patch # User Bastien Orivel # Date 1465841559 -7200 # Node ID c6d3f0dabbba57f47d23c1bea5f7aa6f7dba132a # Parent 6bf54bff8dbde354c8e91111695209c4aa2a4064 Add ast.IfExp. diff --git a/src/ast_convert.rs b/src/ast_convert.rs --- 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!() diff --git a/src/ast_dump.rs b/src/ast_dump.rs --- 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()), } } } diff --git a/src/python_ast.rs b/src/python_ast.rs --- a/src/python_ast.rs +++ b/src/python_ast.rs @@ -82,7 +82,7 @@ pub enum expr { BinOp(Box, operator, Box), UnaryOp(unaryop, Box), Lambda(Box, Box), - //IfExp(Box, Box, Box) + IfExp(Box, Box, Box), //Dict(Vec, Vec) Set(Vec), ListComp(Box, Vec), diff --git a/tests/test_parse_files/test_ifelse.py b/tests/test_parse_files/test_ifelse.py new file mode 100644 --- /dev/null +++ b/tests/test_parse_files/test_ifelse.py @@ -0,0 +1,1 @@ +a = 5 if True else 4