Mercurial > python-compiler.rs
diff src/ast_convert.rs @ 6:6f2bf13f4cb5
Add ast.While and ast.Break.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Tue, 31 May 2016 04:22:35 +0100 |
parents | ddf372373a77 |
children | 680d15073f55 |
line wrap: on
line diff
--- a/src/ast_convert.rs +++ b/src/ast_convert.rs @@ -75,6 +75,7 @@ fn parse_binop(py: Python, ast: PyObject let mult_type = ast_module.get(py, "Mult").unwrap(); let eq_type = ast_module.get(py, "Eq").unwrap(); let lt_type = ast_module.get(py, "Lt").unwrap(); + let gt_type = ast_module.get(py, "Gt").unwrap(); let sub_type = ast_module.get(py, "Sub").unwrap(); let div_type = ast_module.get(py, "Div").unwrap(); @@ -88,6 +89,8 @@ fn parse_binop(py: Python, ast: PyObject BinOp::BinEq } else if is_instance(&ast, <_type) { BinOp::BinLt + } else if is_instance(&ast, >_type) { + BinOp::BinGt } else if is_instance(&ast, &sub_type) { BinOp::Sub } else if is_instance(&ast, &div_type) { @@ -244,8 +247,10 @@ fn parse_statement(py: Python, ast: PyOb let return_type = ast_module.get(py, "Return").unwrap(); let import_from_type = ast_module.get(py, "ImportFrom").unwrap(); let if_type = ast_module.get(py, "If").unwrap(); + let while_type = ast_module.get(py, "While").unwrap(); let for_type = ast_module.get(py, "For").unwrap(); let expr_type = ast_module.get(py, "Expr").unwrap(); + let break_type = ast_module.get(py, "Break").unwrap(); assert!(is_instance(&ast, &ast_type)); @@ -342,6 +347,28 @@ fn parse_statement(py: Python, ast: PyOb } Statement::If(test, statements, orelse_) + } else if is_instance(&ast, &while_type) { + let test = ast.getattr(py, "test").unwrap(); + let body = ast.getattr(py, "body").unwrap(); + let orelse = ast.getattr(py, "orelse").unwrap(); + + let test = parse_expr(py, test); + + let mut statements = vec!(); + for statement in body.iter(py).unwrap() { + let statement = statement.unwrap(); + let statement = parse_statement(py, statement); + statements.push(statement); + } + + let mut orelse_ = vec!(); + for statement in orelse.iter(py).unwrap() { + let statement = statement.unwrap(); + let statement = parse_statement(py, statement); + orelse_.push(statement); + } + + Statement::While(test, statements, orelse_) } else if is_instance(&ast, &for_type) { let target = ast.getattr(py, "target").unwrap(); let iter = ast.getattr(py, "iter").unwrap(); @@ -403,6 +430,8 @@ fn parse_statement(py: Python, ast: PyOb let value = ast.getattr(py, "value").unwrap(); let value = parse_expr(py, value); Statement::Expr(value) + } else if is_instance(&ast, &break_type) { + Statement::Break } else { println!("Statement {}", ast); Statement::Error