comparison src/ast_scope.rs @ 13:38b0d63697b1

Import the full AST grammar from CPython 3.5.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Thu, 02 Jun 2016 05:33:23 +0100
parents 0e96c5bc401d
children
comparison
equal deleted inserted replaced
12:0e96c5bc401d 13:38b0d63697b1
1 use python_ast::{Module, Statement, Expr}; 1 use python_ast::{Module, stmt, expr, alias};
2 2
3 use std::collections::HashMap; 3 use std::collections::HashMap;
4 4
5 #[derive(Debug)] 5 #[derive(Debug)]
6 struct BlockStatement { 6 struct BlockStatement {
7 statement: Statement, 7 statement: stmt,
8 block: Option<Block> 8 block: Option<Block>
9 } 9 }
10 10
11 #[derive(Debug)] 11 #[derive(Debug)]
12 struct Block { 12 struct Block {
17 #[derive(Debug)] 17 #[derive(Debug)]
18 pub struct Scoping { 18 pub struct Scoping {
19 modules: HashMap<String, Block> 19 modules: HashMap<String, Block>
20 } 20 }
21 21
22 fn scope_expr(expr: Expr, block: &mut Block) { 22 fn scope_expr(expr: expr, block: &mut Block) {
23 println!("{:?}", expr); 23 println!("{:?}", expr);
24 } 24 }
25 25
26 fn scope_statement(statement: Statement, block: &mut Block) { 26 fn scope_statement(statement: stmt, block: &mut Block) {
27 let new_block = match statement.clone() { 27 let new_block = match statement.clone() {
28 Statement::Assign(targets, value) => { 28 stmt::Assign(targets, value) => {
29 //scope_expr(value, &mut block); 29 //scope_expr(value, &mut block);
30 for target in targets { 30 for target in targets {
31 match target { 31 match target {
32 Expr::Name(name) => block.bindings.push(name), 32 expr::Name(name, ctx) => block.bindings.push(name),
33 _ => () // No new binding. 33 _ => () // No new binding.
34 } 34 }
35 } 35 }
36 None 36 None
37 }, 37 },
38 Statement::FunctionDef(name, args, statements) => { 38 stmt::FunctionDef(name, args, statements) => {
39 block.bindings.push(name.clone()); 39 block.bindings.push(name.clone());
40 let mut function_block = Block{statements: vec!(), bindings: args.clone()}; 40 let mut function_block = Block{statements: vec!(), bindings: vec!()};
41 for statement in statements { 41 for statement in statements {
42 scope_statement(statement, &mut function_block); 42 scope_statement(statement, &mut function_block);
43 } 43 }
44 Some(function_block) 44 Some(function_block)
45 }, 45 },
46 Statement::ImportFrom(module, names) => { 46 stmt::ImportFrom(module, names, level) => {
47 for name in names { 47 /*
48 let name = match name { 48 for alias in names {
49 Expr::Alias(name, asname) => (name, asname), 49 let asname = alias.asname;
50 _ => panic!() 50 let name = alias.name;
51 }; 51 match asname {
52 let nameas = name.1;
53 let name = name.0;
54 match nameas {
55 Some(name) => block.bindings.push(name), 52 Some(name) => block.bindings.push(name),
56 None => block.bindings.push(name) 53 None => block.bindings.push(name)
57 } 54 }
58 } 55 }
56 */
59 None 57 None
60 }, 58 },
61 _ => None 59 _ => None
62 }; 60 };
63 block.statements.push(BlockStatement{statement: statement, block: new_block}); 61 block.statements.push(BlockStatement{statement: statement, block: new_block});
73 } 71 }
74 scoping.modules.insert(module.name, block); 72 scoping.modules.insert(module.name, block);
75 } 73 }
76 scoping 74 scoping
77 } 75 }
76
77 /*
78 #[derive(Debug)]
79 struct NewBlockStatement {
80 statement: stmt,
81 block: Option<NewBlock>
82 }
83
84 #[derive(Debug)]
85 enum Binding {
86 Bound,
87 Free
88 }
89
90 #[derive(Debug)]
91 struct NewBlock {
92 statements: Vec<NewBlockStatement>,
93 bindings: Vec<String>,
94 variables: HashMap<String, Binding>
95 }
96
97 #[derive(Debug)]
98 pub struct NewScoping {
99 modules: HashMap<String, NewBlock>
100 }
101
102 fn check_expr(expr: expr, block: Block) {
103 println!("{:?}", expr);
104 }
105
106 fn check_block(block: Block) -> NewBlock {
107 let mut variables = HashMap::new();
108 let mut statements = vec!();
109 for blocked_statement in block.statements {
110 let statement = match blocked_statement.statement {
111 stmt::FunctionDef(name, args, body) => {
112 // No need to recurse here, this will be in a new block.
113 variables.insert(name.clone(), Binding::Bound);
114 stmt::FunctionDef(name, args, body)
115 },
116 stmt::Assign(targets, value) => {
117 for target in targets.clone() {
118 match target {
119 expr::Name(name) => {
120 variables.insert(name, Binding::Bound);
121 },
122 _ => panic!()
123 }
124 }
125 stmt::Assign(targets, value)
126 },
127 any => any
128 };
129 let new_block = match blocked_statement.block {
130 Some(block) => Some(check_block(block)),
131 None => None
132 };
133 statements.push(NewBlockStatement{statement: statement, block: new_block});
134 }
135 NewBlock{statements: statements, bindings: block.bindings, variables: variables}
136 }
137
138 #[allow(dead_code)]
139 pub fn check_scoping(scoping: Scoping) -> NewScoping {
140 let mut modules = HashMap::new();
141 for (name, block) in scoping.modules {
142 modules.insert(name, check_block(block));
143 }
144 let mut new_scoping = NewScoping{modules: modules};
145 new_scoping
146 }
147 */