Mercurial > python-compiler.rs
comparison src/ast_scope.rs @ 11:5c169d5807b5
Remove dead code.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Thu, 02 Jun 2016 01:22:28 +0100 |
parents | 3bf4903d1d2c |
children | 0e96c5bc401d |
comparison
equal
deleted
inserted
replaced
10:3bf4903d1d2c | 11:5c169d5807b5 |
---|---|
1 use python_ast::{Module, Statement, Expr}; | 1 use python_ast::{Module, Statement, Expr}; |
2 | 2 |
3 use std::collections::HashMap; | 3 use std::collections::HashMap; |
4 | |
5 /* | |
6 trait Visitor<T> { | |
7 fn visit(&mut self, modules: Vec<Module>) -> Vec<T>; | |
8 fn visit_module(&mut self, block: &mut Block, module: Module) -> T; | |
9 fn visit_statement(&mut self, block: &mut Block, statement: Statement) -> Option<Box<T>>; | |
10 fn visit_expr(&mut self, block: &mut Block, expr: Expr) -> (); | |
11 } | |
12 */ | |
13 | 4 |
14 #[derive(Debug)] | 5 #[derive(Debug)] |
15 struct Block { | 6 struct Block { |
16 statements: Vec<(Statement, Option<Box<Block>>)>, | 7 statements: Vec<(Statement, Option<Box<Block>>)>, |
17 bindings: Vec<String> | 8 bindings: Vec<String> |
19 | 10 |
20 #[derive(Debug)] | 11 #[derive(Debug)] |
21 pub struct Scoping { | 12 pub struct Scoping { |
22 modules: HashMap<String, Block> | 13 modules: HashMap<String, Block> |
23 } | 14 } |
24 | |
25 /* | |
26 impl Visitor<Block> for Scoping { | |
27 fn visit(&mut self, modules: Vec<Module>) -> Vec<Block> { | |
28 println!("Scoping all modules."); | |
29 let mut blocks = vec!(); | |
30 for module in modules { | |
31 let mut block = Block{statements: vec!(), bindings: vec!()}; | |
32 let block = self.visit_module(&mut block, module); | |
33 blocks.push(block); | |
34 } | |
35 blocks | |
36 } | |
37 | |
38 fn visit_module(&mut self, block: &mut Block, module: Module) -> Block { | |
39 println!("Scoping module {}.", module.name); | |
40 for statement in module.statements { | |
41 self.visit_statement(block, statement); | |
42 } | |
43 println!("Module block: {:?}", block); | |
44 } | |
45 | |
46 fn visit_statement(&mut self, block: &mut Block, statement: Statement) -> Option<Box<Block>> { | |
47 match statement { | |
48 //Statement::ClassDef(a, b, c) => println!("{:?} {:?} {:?}", a, b, c), | |
49 Statement::FunctionDef(name, args, statements) => { | |
50 block.bindings.push(name.clone()); | |
51 //block.statements.insert(module.name, block); | |
52 println!("{:?} {:?}", name, args); | |
53 let mut function_block = Block{statements: vec!(), bindings: vec!()}; | |
54 for statement in statements { | |
55 let option = self.visit_statement(block, statement); | |
56 function_block.statements.push((statement, option)); | |
57 } | |
58 Some(Box::new(function_block)) | |
59 }, | |
60 Statement::For(target, iter, statements, orelse) => { | |
61 println!("for"); | |
62 for statement in statements { | |
63 let option = self.visit_statement(block, statement); | |
64 } | |
65 block.statements.push(name.clone()); | |
66 Some(Box::new()) | |
67 }, | |
68 Statement::Assign(lhs, rhs) => { | |
69 for target in lhs.clone() { | |
70 match target.clone() { | |
71 Expr::Name(name) => block.bindings.push(name), | |
72 _ => panic!() | |
73 } | |
74 self.visit_expr(block, target); | |
75 } | |
76 println!("assign {:?} {:?}", lhs, rhs); | |
77 Some() | |
78 }, | |
79 _ => { | |
80 println!("something"); | |
81 None | |
82 } | |
83 } | |
84 } | |
85 | |
86 fn visit_expr(&mut self, block: &mut Block, expr: Expr) -> () { | |
87 } | |
88 } | |
89 */ | |
90 | |
91 /* | |
92 #[allow(dead_code)] | |
93 pub fn scope_ast(ast: Module) -> Scoping { | |
94 let mut modules = HashMap::new(); | |
95 let mut scoping = Scoping{modules: modules}; | |
96 scoping.visit(vec!(ast)); | |
97 scoping | |
98 } | |
99 */ | |
100 | 15 |
101 fn scope_expr(expr: Expr, block: &mut Block) { | 16 fn scope_expr(expr: Expr, block: &mut Block) { |
102 println!("{:?}", expr); | 17 println!("{:?}", expr); |
103 } | 18 } |
104 | 19 |