Mercurial > python-compiler.rs
comparison src/ast_convert.rs @ 59:407b3b217928
Add ast.With.
author | Bastien Orivel <eijebong@bananium.fr> |
---|---|
date | Sun, 12 Jun 2016 20:14:27 +0200 |
parents | e5a808ec31c0 |
children | 9f0f457a67f6 |
comparison
equal
deleted
inserted
replaced
58:a0b23123901b | 59:407b3b217928 |
---|---|
1 use python_ast::{Module, stmt, expr, expr_context, cmpop, boolop, operator, unaryop, arguments, arg, alias, comprehension, keyword}; | 1 use python_ast::{Module, stmt, expr, expr_context, cmpop, boolop, operator, unaryop, arguments, arg, alias, comprehension, keyword, withitem}; |
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 { |
49 let asname = ast.getattr(py, "asname").unwrap(); | 49 let asname = ast.getattr(py, "asname").unwrap(); |
50 if asname == py.None() { | 50 if asname == py.None() { |
51 alias{name: ast_alias, asname: None} | 51 alias{name: ast_alias, asname: None} |
52 } else { | 52 } else { |
53 alias{name: ast_alias, asname: Some(get_str(py, asname))} | 53 alias{name: ast_alias, asname: Some(get_str(py, asname))} |
54 } | |
55 } | |
56 | |
57 fn parse_withitem(py: Python, ast: PyObject) -> withitem { | |
58 let context_expr = ast.getattr(py, "context_expr").unwrap(); | |
59 let optional_vars = ast.getattr(py, "optional_vars").unwrap(); | |
60 | |
61 let context_expr = parse_expr(py, context_expr); | |
62 | |
63 if optional_vars == py.None() { | |
64 withitem{context_expr: context_expr, optional_vars: None} | |
65 } else { | |
66 let optional_vars = parse_expr(py, optional_vars); | |
67 withitem{context_expr: context_expr, optional_vars: Some(optional_vars)} | |
54 } | 68 } |
55 } | 69 } |
56 | 70 |
57 fn parse_unaryop(py: Python, ast: PyObject) -> unaryop { | 71 fn parse_unaryop(py: Python, ast: PyObject) -> unaryop { |
58 let builtins_module = py.import("builtins").unwrap(); | 72 let builtins_module = py.import("builtins").unwrap(); |
511 let break_type = ast_module.get(py, "Break").unwrap(); | 525 let break_type = ast_module.get(py, "Break").unwrap(); |
512 let delete_type = ast_module.get(py, "Delete").unwrap(); | 526 let delete_type = ast_module.get(py, "Delete").unwrap(); |
513 let pass_type = ast_module.get(py, "Pass").unwrap(); | 527 let pass_type = ast_module.get(py, "Pass").unwrap(); |
514 let continue_type = ast_module.get(py, "Continue").unwrap(); | 528 let continue_type = ast_module.get(py, "Continue").unwrap(); |
515 let assert_type = ast_module.get(py, "Assert").unwrap(); | 529 let assert_type = ast_module.get(py, "Assert").unwrap(); |
530 let with_type = ast_module.get(py, "With").unwrap(); | |
516 | 531 |
517 assert!(is_instance(&ast, &ast_type)); | 532 assert!(is_instance(&ast, &ast_type)); |
518 | 533 |
519 /* | 534 /* |
520 // TODO: implement Hash for PyObject. (trivial) | 535 // TODO: implement Hash for PyObject. (trivial) |
663 stmt::Assert(test, None) | 678 stmt::Assert(test, None) |
664 } else { | 679 } else { |
665 let msg = parse_expr(py, msg); | 680 let msg = parse_expr(py, msg); |
666 stmt::Assert(test, Some(msg)) | 681 stmt::Assert(test, Some(msg)) |
667 } | 682 } |
683 } else if is_instance(&ast, &with_type) { | |
684 let items = ast.getattr(py, "items").unwrap(); | |
685 let body = ast.getattr(py, "body").unwrap(); | |
686 | |
687 let items = parse_list(py, items, parse_withitem); | |
688 let body = parse_list(py, body, parse_statement); | |
689 stmt::With(items, body) | |
668 } else { | 690 } else { |
669 println!("stmt {}", ast); | 691 println!("stmt {}", ast); |
670 panic!() | 692 panic!() |
671 } | 693 } |
672 } | 694 } |