Mercurial > python-compiler.rs
changeset 57:e5a808ec31c0
Add ast.Assert.
author | Bastien Orivel <eijebong@bananium.fr> |
---|---|
date | Sun, 12 Jun 2016 18:21:15 +0200 |
parents | c3cc16b933d2 |
children | a0b23123901b |
files | src/ast_convert.rs src/ast_dump.rs src/python_ast.rs tests/test_parse_files/test_assert.py |
diffstat | 4 files changed, 29 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
--- a/src/ast_convert.rs +++ b/src/ast_convert.rs @@ -512,6 +512,7 @@ fn parse_statement(py: Python, ast: PyOb let delete_type = ast_module.get(py, "Delete").unwrap(); let pass_type = ast_module.get(py, "Pass").unwrap(); let continue_type = ast_module.get(py, "Continue").unwrap(); + let assert_type = ast_module.get(py, "Assert").unwrap(); assert!(is_instance(&ast, &ast_type)); @@ -653,6 +654,17 @@ fn parse_statement(py: Python, ast: PyOb stmt::Pass } else if is_instance(&ast, &continue_type) { 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)) + } } else { println!("stmt {}", ast); panic!()
--- a/src/ast_dump.rs +++ b/src/ast_dump.rs @@ -265,7 +265,19 @@ impl stmt { stmt::Break => format!("{}break", current_indent), stmt::Delete(targets) => format!("{}del {}", current_indent, vec_to_strings_vec(targets).join(", ")), stmt::Pass => format!("{}pass", current_indent), - stmt::Continue => format!("{}continue", current_indent) + stmt::Continue => format!("{}continue", current_indent), + stmt::Assert(test, msg) => { + format!("{}assert {}{}", + current_indent, + test.to_string(), + { + match msg { + Some(msg) => format!(", {}", msg.to_string()), + None => format!("") + } + } + ) + } } } }