Mercurial > python-compiler.rs
diff src/ast_convert.rs @ 5:ddf372373a77
Add ast.For, ast.UnaryOp, and Sub and Div to ast.BinOp.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Tue, 31 May 2016 04:15:00 +0100 |
parents | f27a4aee9dfa |
children | 6f2bf13f4cb5 |
line wrap: on
line diff
--- a/src/ast_convert.rs +++ b/src/ast_convert.rs @@ -1,4 +1,4 @@ -use python_ast::{Module, Statement, Expr, BinOp}; +use python_ast::{Module, Statement, Expr, BinOp, UnaryOp}; use cpython::{Python, PyObject}; use cpython::ObjectProtocol; //for call method @@ -36,6 +36,31 @@ fn parse_expr_vec(py: Python, ast: PyObj } } +fn parse_unaryop(py: Python, ast: PyObject) -> UnaryOp { + let builtins_module = py.import("builtins").unwrap(); + let isinstance = builtins_module.get(py, "isinstance").unwrap(); + + let is_instance = |object: &PyObject, type_: &PyObject| { + return isinstance.call(py, (object, type_), None).unwrap().is_true(py).unwrap(); + }; + + let ast_module = py.import("ast").unwrap(); + let ast_type = ast_module.get(py, "AST").unwrap(); + let uadd_type = ast_module.get(py, "UAdd").unwrap(); + let usub_type = ast_module.get(py, "USub").unwrap(); + + assert!(is_instance(&ast, &ast_type)); + + if is_instance(&ast, &uadd_type) { + UnaryOp::UAdd + } else if is_instance(&ast, &usub_type) { + UnaryOp::USub + } else { + println!("UnaryOp {}", ast); + UnaryOp::Error + } +} + fn parse_binop(py: Python, ast: PyObject) -> BinOp { let builtins_module = py.import("builtins").unwrap(); let isinstance = builtins_module.get(py, "isinstance").unwrap(); @@ -50,6 +75,8 @@ 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 sub_type = ast_module.get(py, "Sub").unwrap(); + let div_type = ast_module.get(py, "Div").unwrap(); assert!(is_instance(&ast, &ast_type)); @@ -61,6 +88,10 @@ fn parse_binop(py: Python, ast: PyObject BinOp::BinEq } else if is_instance(&ast, <_type) { BinOp::BinLt + } else if is_instance(&ast, &sub_type) { + BinOp::Sub + } else if is_instance(&ast, &div_type) { + BinOp::Div } else { println!("BinOp {}", ast); BinOp::Error @@ -78,6 +109,7 @@ fn parse_expr(py: Python, ast: PyObject) let ast_module = py.import("ast").unwrap(); let ast_type = ast_module.get(py, "AST").unwrap(); let arg_type = ast_module.get(py, "arg").unwrap(); + let unary_op_type = ast_module.get(py, "UnaryOp").unwrap(); let bin_op_type = ast_module.get(py, "BinOp").unwrap(); let name_constant_type = ast_module.get(py, "NameConstant").unwrap(); let attribute_type = ast_module.get(py, "Attribute").unwrap(); @@ -118,6 +150,14 @@ fn parse_expr(py: Python, ast: PyObject) let s = ast.getattr(py, "s").unwrap(); let s = get_str(py, s); Expr::Str(s) + } else if is_instance(&ast, &unary_op_type) { + let op = ast.getattr(py, "op").unwrap(); + let operand = ast.getattr(py, "operand").unwrap(); + + let op = parse_unaryop(py, op); + let operand = parse_expr(py, operand); + + Expr::UnaryOp(op, Box::new(operand)) } else if is_instance(&ast, &bin_op_type) { let left = ast.getattr(py, "left").unwrap(); let op = ast.getattr(py, "op").unwrap(); @@ -204,6 +244,7 @@ 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 for_type = ast_module.get(py, "For").unwrap(); let expr_type = ast_module.get(py, "Expr").unwrap(); assert!(is_instance(&ast, &ast_type)); @@ -301,6 +342,30 @@ fn parse_statement(py: Python, ast: PyOb } Statement::If(test, statements, orelse_) + } else if is_instance(&ast, &for_type) { + let target = ast.getattr(py, "target").unwrap(); + let iter = ast.getattr(py, "iter").unwrap(); + let body = ast.getattr(py, "body").unwrap(); + let orelse = ast.getattr(py, "orelse").unwrap(); + + let target = parse_expr(py, target); + let iter = parse_expr(py, iter); + + 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::For(target, iter, statements, orelse_) } else if is_instance(&ast, &assign_type) { let targets = ast.getattr(py, "targets").unwrap(); let value = ast.getattr(py, "value").unwrap();