Mercurial > python-compiler.rs
changeset 61:6b73843c5b4a
Add a parse_optional_expr function and use it instead of let var = if var == py.None() { None } else { Some(parse_expr(var) };
author | Bastien Orivel <eijebong@bananium.fr> |
---|---|
date | Mon, 13 Jun 2016 01:48:33 +0200 |
parents | 9f0f457a67f6 |
children | d255a932553f |
files | src/ast_convert.rs |
diffstat | 1 files changed, 22 insertions(+), 33 deletions(-) [+] |
line wrap: on
line diff
--- a/src/ast_convert.rs +++ b/src/ast_convert.rs @@ -255,6 +255,15 @@ fn parse_operator(py: Python, ast: PyObj } } +fn parse_optional_expr(py: Python, ast: PyObject) -> Option<expr> { + if ast == py.None() { + None + } else { + let ast = parse_expr(py, ast); + Some(ast) + } +} + fn parse_expr(py: Python, ast: PyObject) -> expr { let builtins_module = py.import("builtins").unwrap(); let isinstance = builtins_module.get(py, "isinstance").unwrap(); @@ -392,11 +401,7 @@ fn parse_arg(py: Python, ast: PyObject) let annotation = ast.getattr(py, "annotation").unwrap(); let arg = get_str(py, arg); - let annotation = if annotation == py.None() { - None - } else { - Some(parse_expr(py, annotation)) - }; + let annotation = parse_optional_expr(py, annotation); arg{arg: arg, annotation: annotation} } @@ -458,11 +463,8 @@ fn parse_arguments(py: Python, ast: PyOb let mut arguments = vec!(); for arg in kw_defaults.iter(py).unwrap() { let arg = arg.unwrap(); - let arg = if arg == py.None() { - None - } else { - Some(parse_expr(py, arg)) - }; + let arg = parse_optional_expr(py, arg); + arguments.push(arg); } arguments @@ -650,12 +652,9 @@ fn parse_statement(py: Python, ast: PyOb stmt::Import(names) } else if is_instance(&ast, &return_type) { let value = ast.getattr(py, "value").unwrap(); - if value == py.None() { - stmt::Return(None) - } else { - let value = parse_expr(py, value); - stmt::Return(Some(value)) - } + let value = parse_optional_expr(py, value); + + stmt::Return(value) } else if is_instance(&ast, &expr_type) { let value = ast.getattr(py, "value").unwrap(); let value = parse_expr(py, value); @@ -672,15 +671,12 @@ fn parse_statement(py: Python, ast: PyOb stmt::Continue } else if is_instance(&ast, &assert_type) { let test = ast.getattr(py, "test").unwrap(); - let test = parse_expr(py, test); let msg = ast.getattr(py, "msg").unwrap(); - if msg == py.None() { - stmt::Assert(test, None) - } else { - let msg = parse_expr(py, msg); - stmt::Assert(test, Some(msg)) - } + let test = parse_expr(py, test); + let msg = parse_optional_expr(py, msg); + + stmt::Assert(test, msg) } else if is_instance(&ast, &with_type) { let items = ast.getattr(py, "items").unwrap(); let body = ast.getattr(py, "body").unwrap(); @@ -691,18 +687,11 @@ fn parse_statement(py: Python, ast: PyOb } else if is_instance(&ast, &raise_type) { let exc = ast.getattr(py, "exc").unwrap(); let cause = ast.getattr(py, "cause").unwrap(); - let mut exc_ = None; - let mut cause_ = None; - if exc != py.None() { - exc_ = Some(parse_expr(py, exc)); - } + let exc = parse_optional_expr(py, exc); + let cause = parse_optional_expr(py, cause); - if cause != py.None() { - cause_ = Some(parse_expr(py, cause)); - } - - stmt::Raise(exc_, cause_) + stmt::Raise(exc, cause) } else { println!("stmt {}", ast); panic!()