# HG changeset patch # User Bastien Orivel # Date 1465841194 -7200 # Node ID 6bf54bff8dbde354c8e91111695209c4aa2a4064 # Parent f1a845e4121b729894eb5f4b29c637fe2e756fbd Add ast.Lambda. diff --git a/src/ast_convert.rs b/src/ast_convert.rs --- a/src/ast_convert.rs +++ b/src/ast_convert.rs @@ -307,6 +307,7 @@ fn parse_expr(py: Python, ast: PyObject) let set_type = ast_module.get(py, "Set").unwrap(); 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(); assert!(is_instance(&ast, &ast_type)); @@ -444,6 +445,14 @@ fn parse_expr(py: Python, ast: PyObject) let generators = parse_list(py, generators, parse_comprehension); expr::GeneratorExp(Box::new(elt), generators) + } else if is_instance(&ast, &lambda_type) { + let args = ast.getattr(py, "args").unwrap(); + let body = ast.getattr(py, "body").unwrap(); + + let args = parse_arguments(py, args); + let body = parse_expr(py, body); + + expr::Lambda(Box::new(args), Box::new(body)) } 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 @@ -197,6 +197,7 @@ impl to_string_able for expr { expr::Set(elts) => format!("{{{}}}", vec_to_strings_vec(elts).join(", ")), 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()), } } } diff --git a/src/python_ast.rs b/src/python_ast.rs --- a/src/python_ast.rs +++ b/src/python_ast.rs @@ -81,7 +81,7 @@ pub enum expr { BoolOp(boolop, Vec), BinOp(Box, operator, Box), UnaryOp(unaryop, Box), - //Lambda(arguments, Box) + Lambda(Box, Box), //IfExp(Box, Box, Box) //Dict(Vec, Vec) Set(Vec), diff --git a/tests/test_parse_files/test_lambda.py b/tests/test_parse_files/test_lambda.py new file mode 100644 --- /dev/null +++ b/tests/test_parse_files/test_lambda.py @@ -0,0 +1,1 @@ +a = lambda x, y: (x, y)