Mercurial > python-compiler.rs
comparison src/ast_convert.rs @ 61:6b73843c5b4a
Add a parse_optional_expr function and use it instead of let var = if var == py.None() { None } else { Some(parse_expr(var) };
author | Bastien Orivel <eijebong@bananium.fr> |
---|---|
date | Mon, 13 Jun 2016 01:48:33 +0200 |
parents | 9f0f457a67f6 |
children | 5df52b40fe54 |
comparison
equal
deleted
inserted
replaced
60:9f0f457a67f6 | 61:6b73843c5b4a |
---|---|
253 println!("operator {}", ast); | 253 println!("operator {}", ast); |
254 panic!() | 254 panic!() |
255 } | 255 } |
256 } | 256 } |
257 | 257 |
258 fn parse_optional_expr(py: Python, ast: PyObject) -> Option<expr> { | |
259 if ast == py.None() { | |
260 None | |
261 } else { | |
262 let ast = parse_expr(py, ast); | |
263 Some(ast) | |
264 } | |
265 } | |
266 | |
258 fn parse_expr(py: Python, ast: PyObject) -> expr { | 267 fn parse_expr(py: Python, ast: PyObject) -> expr { |
259 let builtins_module = py.import("builtins").unwrap(); | 268 let builtins_module = py.import("builtins").unwrap(); |
260 let isinstance = builtins_module.get(py, "isinstance").unwrap(); | 269 let isinstance = builtins_module.get(py, "isinstance").unwrap(); |
261 | 270 |
262 let is_instance = |object: &PyObject, type_: &PyObject| { | 271 let is_instance = |object: &PyObject, type_: &PyObject| { |
390 fn parse_arg(py: Python, ast: PyObject) -> arg { | 399 fn parse_arg(py: Python, ast: PyObject) -> arg { |
391 let arg = ast.getattr(py, "arg").unwrap(); | 400 let arg = ast.getattr(py, "arg").unwrap(); |
392 let annotation = ast.getattr(py, "annotation").unwrap(); | 401 let annotation = ast.getattr(py, "annotation").unwrap(); |
393 | 402 |
394 let arg = get_str(py, arg); | 403 let arg = get_str(py, arg); |
395 let annotation = if annotation == py.None() { | 404 let annotation = parse_optional_expr(py, annotation); |
396 None | |
397 } else { | |
398 Some(parse_expr(py, annotation)) | |
399 }; | |
400 | 405 |
401 arg{arg: arg, annotation: annotation} | 406 arg{arg: arg, annotation: annotation} |
402 } | 407 } |
403 | 408 |
404 fn parse_arguments(py: Python, ast: PyObject) -> arguments { | 409 fn parse_arguments(py: Python, ast: PyObject) -> arguments { |
456 kw_defaults: { | 461 kw_defaults: { |
457 let kw_defaults = ast.getattr(py, "kw_defaults").unwrap(); | 462 let kw_defaults = ast.getattr(py, "kw_defaults").unwrap(); |
458 let mut arguments = vec!(); | 463 let mut arguments = vec!(); |
459 for arg in kw_defaults.iter(py).unwrap() { | 464 for arg in kw_defaults.iter(py).unwrap() { |
460 let arg = arg.unwrap(); | 465 let arg = arg.unwrap(); |
461 let arg = if arg == py.None() { | 466 let arg = parse_optional_expr(py, arg); |
462 None | 467 |
463 } else { | |
464 Some(parse_expr(py, arg)) | |
465 }; | |
466 arguments.push(arg); | 468 arguments.push(arg); |
467 } | 469 } |
468 arguments | 470 arguments |
469 }, | 471 }, |
470 //kwarg: Option<arg>, | 472 //kwarg: Option<arg>, |
648 let names = parse_list(py, names, parse_alias); | 650 let names = parse_list(py, names, parse_alias); |
649 | 651 |
650 stmt::Import(names) | 652 stmt::Import(names) |
651 } else if is_instance(&ast, &return_type) { | 653 } else if is_instance(&ast, &return_type) { |
652 let value = ast.getattr(py, "value").unwrap(); | 654 let value = ast.getattr(py, "value").unwrap(); |
653 if value == py.None() { | 655 let value = parse_optional_expr(py, value); |
654 stmt::Return(None) | 656 |
655 } else { | 657 stmt::Return(value) |
656 let value = parse_expr(py, value); | |
657 stmt::Return(Some(value)) | |
658 } | |
659 } else if is_instance(&ast, &expr_type) { | 658 } else if is_instance(&ast, &expr_type) { |
660 let value = ast.getattr(py, "value").unwrap(); | 659 let value = ast.getattr(py, "value").unwrap(); |
661 let value = parse_expr(py, value); | 660 let value = parse_expr(py, value); |
662 stmt::Expr(value) | 661 stmt::Expr(value) |
663 } else if is_instance(&ast, &break_type) { | 662 } else if is_instance(&ast, &break_type) { |
670 stmt::Pass | 669 stmt::Pass |
671 } else if is_instance(&ast, &continue_type) { | 670 } else if is_instance(&ast, &continue_type) { |
672 stmt::Continue | 671 stmt::Continue |
673 } else if is_instance(&ast, &assert_type) { | 672 } else if is_instance(&ast, &assert_type) { |
674 let test = ast.getattr(py, "test").unwrap(); | 673 let test = ast.getattr(py, "test").unwrap(); |
674 let msg = ast.getattr(py, "msg").unwrap(); | |
675 | |
675 let test = parse_expr(py, test); | 676 let test = parse_expr(py, test); |
676 let msg = ast.getattr(py, "msg").unwrap(); | 677 let msg = parse_optional_expr(py, msg); |
677 | 678 |
678 if msg == py.None() { | 679 stmt::Assert(test, msg) |
679 stmt::Assert(test, None) | |
680 } else { | |
681 let msg = parse_expr(py, msg); | |
682 stmt::Assert(test, Some(msg)) | |
683 } | |
684 } else if is_instance(&ast, &with_type) { | 680 } else if is_instance(&ast, &with_type) { |
685 let items = ast.getattr(py, "items").unwrap(); | 681 let items = ast.getattr(py, "items").unwrap(); |
686 let body = ast.getattr(py, "body").unwrap(); | 682 let body = ast.getattr(py, "body").unwrap(); |
687 | 683 |
688 let items = parse_list(py, items, parse_withitem); | 684 let items = parse_list(py, items, parse_withitem); |
689 let body = parse_list(py, body, parse_statement); | 685 let body = parse_list(py, body, parse_statement); |
690 stmt::With(items, body) | 686 stmt::With(items, body) |
691 } else if is_instance(&ast, &raise_type) { | 687 } else if is_instance(&ast, &raise_type) { |
692 let exc = ast.getattr(py, "exc").unwrap(); | 688 let exc = ast.getattr(py, "exc").unwrap(); |
693 let cause = ast.getattr(py, "cause").unwrap(); | 689 let cause = ast.getattr(py, "cause").unwrap(); |
694 let mut exc_ = None; | 690 |
695 let mut cause_ = None; | 691 let exc = parse_optional_expr(py, exc); |
696 | 692 let cause = parse_optional_expr(py, cause); |
697 if exc != py.None() { | 693 |
698 exc_ = Some(parse_expr(py, exc)); | 694 stmt::Raise(exc, cause) |
699 } | |
700 | |
701 if cause != py.None() { | |
702 cause_ = Some(parse_expr(py, cause)); | |
703 } | |
704 | |
705 stmt::Raise(exc_, cause_) | |
706 } else { | 695 } else { |
707 println!("stmt {}", ast); | 696 println!("stmt {}", ast); |
708 panic!() | 697 panic!() |
709 } | 698 } |
710 } | 699 } |