comparison 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
comparison
equal deleted inserted replaced
67:8ce78e2ba48c 68:c59ad5ccd8a6
1 use python_ast::{Module, stmt, expr, expr_context, cmpop, boolop, operator, unaryop, arguments, arg, alias, comprehension, keyword, withitem}; 1 use python_ast::{Module, stmt, expr, expr_context, cmpop, boolop, operator, unaryop, arguments, arg, alias, comprehension, keyword, withitem, excepthandler};
2 2
3 use cpython::{Python, PyObject}; 3 use cpython::{Python, PyObject};
4 use cpython::ObjectProtocol; //for call method 4 use cpython::ObjectProtocol; //for call method
5 5
6 fn get_str(py: Python, object: PyObject) -> String { 6 fn get_str(py: Python, object: PyObject) -> String {
251 operator::FloorDiv 251 operator::FloorDiv
252 } else { 252 } else {
253 println!("operator {}", ast); 253 println!("operator {}", ast);
254 panic!() 254 panic!()
255 } 255 }
256 }
257
258 fn parse_excepthandler(py: Python, ast: PyObject) -> excepthandler {
259 let type_ = ast.getattr(py, "type").unwrap();
260 let name = ast.getattr(py, "name").unwrap();
261 let body = ast.getattr(py, "body").unwrap();
262
263 let type_ = parse_optional_expr(py, type_);
264 let name = if name == py.None() { None } else { Some(get_str(py, name)) };
265 let body = parse_list(py, body, parse_statement);
266
267 excepthandler{type_: type_, name: name, body: body}
256 } 268 }
257 269
258 fn parse_optional_expr(py: Python, ast: PyObject) -> Option<expr> { 270 fn parse_optional_expr(py: Python, ast: PyObject) -> Option<expr> {
259 if ast == py.None() { 271 if ast == py.None() {
260 None 272 None
529 let pass_type = ast_module.get(py, "Pass").unwrap(); 541 let pass_type = ast_module.get(py, "Pass").unwrap();
530 let continue_type = ast_module.get(py, "Continue").unwrap(); 542 let continue_type = ast_module.get(py, "Continue").unwrap();
531 let assert_type = ast_module.get(py, "Assert").unwrap(); 543 let assert_type = ast_module.get(py, "Assert").unwrap();
532 let with_type = ast_module.get(py, "With").unwrap(); 544 let with_type = ast_module.get(py, "With").unwrap();
533 let raise_type = ast_module.get(py, "Raise").unwrap(); 545 let raise_type = ast_module.get(py, "Raise").unwrap();
546 let try_type = ast_module.get(py, "Try").unwrap();
534 547
535 assert!(is_instance(&ast, &ast_type)); 548 assert!(is_instance(&ast, &ast_type));
536 549
537 /* 550 /*
538 // TODO: implement Hash for PyObject. (trivial) 551 // TODO: implement Hash for PyObject. (trivial)
693 706
694 let exc = parse_optional_expr(py, exc); 707 let exc = parse_optional_expr(py, exc);
695 let cause = parse_optional_expr(py, cause); 708 let cause = parse_optional_expr(py, cause);
696 709
697 stmt::Raise(exc, cause) 710 stmt::Raise(exc, cause)
711 } else if is_instance(&ast, &try_type) {
712 let body = ast.getattr(py, "body").unwrap();
713 let excepthandlers = ast.getattr(py, "handlers").unwrap();
714 let orelse = ast.getattr(py, "orelse").unwrap();
715 let finalbody = ast.getattr(py, "finalbody").unwrap();
716
717 let body = parse_list(py, body, parse_statement);
718 let excepthandlers = parse_list(py, excepthandlers, parse_excepthandler);
719 let orelse = parse_list(py, orelse, parse_statement);
720 let finalbody = parse_list(py, finalbody, parse_statement);
721
722 stmt::Try(body, excepthandlers, orelse, finalbody)
698 } else { 723 } else {
699 println!("stmt {}", ast); 724 println!("stmt {}", ast);
700 panic!() 725 panic!()
701 } 726 }
702 } 727 }