comparison 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
comparison
equal deleted inserted replaced
5:ddf372373a77 6:6f2bf13f4cb5
73 let ast_type = ast_module.get(py, "AST").unwrap(); 73 let ast_type = ast_module.get(py, "AST").unwrap();
74 let add_type = ast_module.get(py, "Add").unwrap(); 74 let add_type = ast_module.get(py, "Add").unwrap();
75 let mult_type = ast_module.get(py, "Mult").unwrap(); 75 let mult_type = ast_module.get(py, "Mult").unwrap();
76 let eq_type = ast_module.get(py, "Eq").unwrap(); 76 let eq_type = ast_module.get(py, "Eq").unwrap();
77 let lt_type = ast_module.get(py, "Lt").unwrap(); 77 let lt_type = ast_module.get(py, "Lt").unwrap();
78 let gt_type = ast_module.get(py, "Gt").unwrap();
78 let sub_type = ast_module.get(py, "Sub").unwrap(); 79 let sub_type = ast_module.get(py, "Sub").unwrap();
79 let div_type = ast_module.get(py, "Div").unwrap(); 80 let div_type = ast_module.get(py, "Div").unwrap();
80 81
81 assert!(is_instance(&ast, &ast_type)); 82 assert!(is_instance(&ast, &ast_type));
82 83
86 BinOp::BinMult 87 BinOp::BinMult
87 } else if is_instance(&ast, &eq_type) { 88 } else if is_instance(&ast, &eq_type) {
88 BinOp::BinEq 89 BinOp::BinEq
89 } else if is_instance(&ast, &lt_type) { 90 } else if is_instance(&ast, &lt_type) {
90 BinOp::BinLt 91 BinOp::BinLt
92 } else if is_instance(&ast, &gt_type) {
93 BinOp::BinGt
91 } else if is_instance(&ast, &sub_type) { 94 } else if is_instance(&ast, &sub_type) {
92 BinOp::Sub 95 BinOp::Sub
93 } else if is_instance(&ast, &div_type) { 96 } else if is_instance(&ast, &div_type) {
94 BinOp::Div 97 BinOp::Div
95 } else { 98 } else {
242 let global_type = ast_module.get(py, "Global").unwrap(); 245 let global_type = ast_module.get(py, "Global").unwrap();
243 let assign_type = ast_module.get(py, "Assign").unwrap(); 246 let assign_type = ast_module.get(py, "Assign").unwrap();
244 let return_type = ast_module.get(py, "Return").unwrap(); 247 let return_type = ast_module.get(py, "Return").unwrap();
245 let import_from_type = ast_module.get(py, "ImportFrom").unwrap(); 248 let import_from_type = ast_module.get(py, "ImportFrom").unwrap();
246 let if_type = ast_module.get(py, "If").unwrap(); 249 let if_type = ast_module.get(py, "If").unwrap();
250 let while_type = ast_module.get(py, "While").unwrap();
247 let for_type = ast_module.get(py, "For").unwrap(); 251 let for_type = ast_module.get(py, "For").unwrap();
248 let expr_type = ast_module.get(py, "Expr").unwrap(); 252 let expr_type = ast_module.get(py, "Expr").unwrap();
253 let break_type = ast_module.get(py, "Break").unwrap();
249 254
250 assert!(is_instance(&ast, &ast_type)); 255 assert!(is_instance(&ast, &ast_type));
251 256
252 /* 257 /*
253 // TODO: implement Hash for PyObject. (trivial) 258 // TODO: implement Hash for PyObject. (trivial)
340 let statement = parse_statement(py, statement); 345 let statement = parse_statement(py, statement);
341 orelse_.push(statement); 346 orelse_.push(statement);
342 } 347 }
343 348
344 Statement::If(test, statements, orelse_) 349 Statement::If(test, statements, orelse_)
350 } else if is_instance(&ast, &while_type) {
351 let test = ast.getattr(py, "test").unwrap();
352 let body = ast.getattr(py, "body").unwrap();
353 let orelse = ast.getattr(py, "orelse").unwrap();
354
355 let test = parse_expr(py, test);
356
357 let mut statements = vec!();
358 for statement in body.iter(py).unwrap() {
359 let statement = statement.unwrap();
360 let statement = parse_statement(py, statement);
361 statements.push(statement);
362 }
363
364 let mut orelse_ = vec!();
365 for statement in orelse.iter(py).unwrap() {
366 let statement = statement.unwrap();
367 let statement = parse_statement(py, statement);
368 orelse_.push(statement);
369 }
370
371 Statement::While(test, statements, orelse_)
345 } else if is_instance(&ast, &for_type) { 372 } else if is_instance(&ast, &for_type) {
346 let target = ast.getattr(py, "target").unwrap(); 373 let target = ast.getattr(py, "target").unwrap();
347 let iter = ast.getattr(py, "iter").unwrap(); 374 let iter = ast.getattr(py, "iter").unwrap();
348 let body = ast.getattr(py, "body").unwrap(); 375 let body = ast.getattr(py, "body").unwrap();
349 let orelse = ast.getattr(py, "orelse").unwrap(); 376 let orelse = ast.getattr(py, "orelse").unwrap();
401 Statement::Return(value) 428 Statement::Return(value)
402 } else if is_instance(&ast, &expr_type) { 429 } else if is_instance(&ast, &expr_type) {
403 let value = ast.getattr(py, "value").unwrap(); 430 let value = ast.getattr(py, "value").unwrap();
404 let value = parse_expr(py, value); 431 let value = parse_expr(py, value);
405 Statement::Expr(value) 432 Statement::Expr(value)
433 } else if is_instance(&ast, &break_type) {
434 Statement::Break
406 } else { 435 } else {
407 println!("Statement {}", ast); 436 println!("Statement {}", ast);
408 Statement::Error 437 Statement::Error
409 } 438 }
410 } 439 }