comparison src/ast_convert.rs @ 18:51ef651266b1

Fix expr_contexts. Do not always use Load, get the real context from the node.
author Bastien Orivel <eijebong@bananium.fr>
date Fri, 03 Jun 2016 00:10:41 +0200
parents f1c77634a2eb
children 0cb53a31ac12
comparison
equal deleted inserted replaced
17:f1c77634a2eb 18:51ef651266b1
5 5
6 fn get_str(py: Python, object: PyObject) -> String { 6 fn get_str(py: Python, object: PyObject) -> String {
7 let pystring = object.str(py).unwrap(); 7 let pystring = object.str(py).unwrap();
8 let mut string = pystring.to_string(py).unwrap(); 8 let mut string = pystring.to_string(py).unwrap();
9 string.to_mut().to_string() 9 string.to_mut().to_string()
10 }
11
12 fn get_ctx(py: Python, object: PyObject) -> expr_context {
13 let builtins_module = py.import("builtins").unwrap();
14 let isinstance = builtins_module.get(py, "isinstance").unwrap();
15
16 let is_instance = |object: &PyObject, type_: &PyObject| {
17 return isinstance.call(py, (object, type_), None).unwrap().is_true(py).unwrap();
18 };
19
20 let ast_module = py.import("ast").unwrap();
21 let store_type = ast_module.get(py, "Store").unwrap();
22 let load_type = ast_module.get(py, "Load").unwrap();
23
24 let ctx = object.getattr(py, "ctx").unwrap();
25 if is_instance(&ctx, &store_type) {
26 expr_context::Store
27 } else if is_instance(&ctx, &load_type) {
28 expr_context::Load
29 } else{
30 unreachable!();
31 }
10 } 32 }
11 33
12 fn parse_expr_vec(py: Python, ast: PyObject) -> Vec<String> { 34 fn parse_expr_vec(py: Python, ast: PyObject) -> Vec<String> {
13 let builtins_module = py.import("builtins").unwrap(); 35 let builtins_module = py.import("builtins").unwrap();
14 let isinstance = builtins_module.get(py, "isinstance").unwrap(); 36 let isinstance = builtins_module.get(py, "isinstance").unwrap();
222 assert!(is_instance(&ast, &ast_type)); 244 assert!(is_instance(&ast, &ast_type));
223 245
224 if is_instance(&ast, &arg_type) { 246 if is_instance(&ast, &arg_type) {
225 let arg = ast.getattr(py, "arg").unwrap(); 247 let arg = ast.getattr(py, "arg").unwrap();
226 let arg = get_str(py, arg); 248 let arg = get_str(py, arg);
227 expr::Name(arg, expr_context::Load) 249 expr::Name(arg, get_ctx(py, ast))
228 } else if is_instance(&ast, &attribute_type) { 250 } else if is_instance(&ast, &attribute_type) {
229 let value = ast.getattr(py, "value").unwrap(); 251 let value = ast.getattr(py, "value").unwrap();
230 let attr = ast.getattr(py, "attr").unwrap(); 252 let attr = ast.getattr(py, "attr").unwrap();
231 253
232 let value = parse_expr(py, value); 254 let value = parse_expr(py, value);
233 let attr = get_str(py, attr); 255 let attr = get_str(py, attr);
234 256
235 expr::Attribute(Box::new(value), attr, expr_context::Load) 257 expr::Attribute(Box::new(value), attr, get_ctx(py, ast))
236 } else if is_instance(&ast, &name_type) { 258 } else if is_instance(&ast, &name_type) {
237 let id = ast.getattr(py, "id").unwrap(); 259 let id = ast.getattr(py, "id").unwrap();
238 let id = get_str(py, id); 260 let id = get_str(py, id);
239 expr::Name(id, expr_context::Load) 261 expr::Name(id, get_ctx(py, ast))
240 } else if is_instance(&ast, &name_constant_type) { 262 } else if is_instance(&ast, &name_constant_type) {
241 let value = ast.getattr(py, "value").unwrap(); 263 let value = ast.getattr(py, "value").unwrap();
242 let value = get_str(py, value); 264 let value = get_str(py, value);
243 expr::NameConstant(value) 265 expr::NameConstant(value)
244 } else if is_instance(&ast, &num_type) { 266 } else if is_instance(&ast, &num_type) {
256 for elt in elts.iter(py).unwrap() { 278 for elt in elts.iter(py).unwrap() {
257 let elt = elt.unwrap(); 279 let elt = elt.unwrap();
258 elements.push(parse_expr(py, elt)); 280 elements.push(parse_expr(py, elt));
259 } 281 }
260 282
261 expr::List(elements, expr_context::Load) 283 expr::List(elements, get_ctx(py, ast))
262 } else if is_instance(&ast, &unary_op_type) { 284 } else if is_instance(&ast, &unary_op_type) {
263 let op = ast.getattr(py, "op").unwrap(); 285 let op = ast.getattr(py, "op").unwrap();
264 let operand = ast.getattr(py, "operand").unwrap(); 286 let operand = ast.getattr(py, "operand").unwrap();
265 287
266 let op = parse_unaryop(py, op); 288 let op = parse_unaryop(py, op);
361 for elt in elts { 383 for elt in elts {
362 let elt = elt.unwrap(); 384 let elt = elt.unwrap();
363 let elt = parse_expr(py, elt); 385 let elt = parse_expr(py, elt);
364 new_elts.push(elt); 386 new_elts.push(elt);
365 } 387 }
366 expr::Tuple(new_elts, expr_context::Load) 388 expr::Tuple(new_elts, get_ctx(py, ast))
367 } else { 389 } else {
368 println!("expr {}", ast); 390 println!("expr {}", ast);
369 unreachable!() 391 unreachable!()
370 } 392 }
371 } 393 }