Mercurial > python-compiler.rs
diff src/ast_convert.rs @ 68:c59ad5ccd8a6
Add ast.Try
author | Bastien Orivel <eijebong@bananium.fr> |
---|---|
date | Mon, 13 Jun 2016 03:03:13 +0200 |
parents | 8ce78e2ba48c |
children | a73eaf42bea1 |
line wrap: on
line diff
--- a/src/ast_convert.rs +++ b/src/ast_convert.rs @@ -1,4 +1,4 @@ -use python_ast::{Module, stmt, expr, expr_context, cmpop, boolop, operator, unaryop, arguments, arg, alias, comprehension, keyword, withitem}; +use python_ast::{Module, stmt, expr, expr_context, cmpop, boolop, operator, unaryop, arguments, arg, alias, comprehension, keyword, withitem, excepthandler}; use cpython::{Python, PyObject}; use cpython::ObjectProtocol; //for call method @@ -255,6 +255,18 @@ fn parse_operator(py: Python, ast: PyObj } } +fn parse_excepthandler(py: Python, ast: PyObject) -> excepthandler { + let type_ = ast.getattr(py, "type").unwrap(); + let name = ast.getattr(py, "name").unwrap(); + let body = ast.getattr(py, "body").unwrap(); + + let type_ = parse_optional_expr(py, type_); + let name = if name == py.None() { None } else { Some(get_str(py, name)) }; + let body = parse_list(py, body, parse_statement); + + excepthandler{type_: type_, name: name, body: body} +} + fn parse_optional_expr(py: Python, ast: PyObject) -> Option<expr> { if ast == py.None() { None @@ -531,6 +543,7 @@ fn parse_statement(py: Python, ast: PyOb let assert_type = ast_module.get(py, "Assert").unwrap(); let with_type = ast_module.get(py, "With").unwrap(); let raise_type = ast_module.get(py, "Raise").unwrap(); + let try_type = ast_module.get(py, "Try").unwrap(); assert!(is_instance(&ast, &ast_type)); @@ -695,6 +708,18 @@ fn parse_statement(py: Python, ast: PyOb let cause = parse_optional_expr(py, cause); stmt::Raise(exc, cause) + } else if is_instance(&ast, &try_type) { + let body = ast.getattr(py, "body").unwrap(); + let excepthandlers = ast.getattr(py, "handlers").unwrap(); + let orelse = ast.getattr(py, "orelse").unwrap(); + let finalbody = ast.getattr(py, "finalbody").unwrap(); + + let body = parse_list(py, body, parse_statement); + let excepthandlers = parse_list(py, excepthandlers, parse_excepthandler); + let orelse = parse_list(py, orelse, parse_statement); + let finalbody = parse_list(py, finalbody, parse_statement); + + stmt::Try(body, excepthandlers, orelse, finalbody) } else { println!("stmt {}", ast); panic!()