Mercurial > python-compiler.rs
diff 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 |
line wrap: on
line diff
--- a/src/ast_scope.rs +++ b/src/ast_scope.rs @@ -1,10 +1,10 @@ -use python_ast::{Module, Statement, Expr}; +use python_ast::{Module, stmt, expr, alias}; use std::collections::HashMap; #[derive(Debug)] struct BlockStatement { - statement: Statement, + statement: stmt, block: Option<Block> } @@ -19,43 +19,41 @@ pub struct Scoping { modules: HashMap<String, Block> } -fn scope_expr(expr: Expr, block: &mut Block) { +fn scope_expr(expr: expr, block: &mut Block) { println!("{:?}", expr); } -fn scope_statement(statement: Statement, block: &mut Block) { +fn scope_statement(statement: stmt, block: &mut Block) { let new_block = match statement.clone() { - Statement::Assign(targets, value) => { + stmt::Assign(targets, value) => { //scope_expr(value, &mut block); for target in targets { match target { - Expr::Name(name) => block.bindings.push(name), + expr::Name(name, ctx) => block.bindings.push(name), _ => () // No new binding. } } None }, - Statement::FunctionDef(name, args, statements) => { + stmt::FunctionDef(name, args, statements) => { block.bindings.push(name.clone()); - let mut function_block = Block{statements: vec!(), bindings: args.clone()}; + let mut function_block = Block{statements: vec!(), bindings: vec!()}; for statement in statements { scope_statement(statement, &mut function_block); } Some(function_block) }, - Statement::ImportFrom(module, names) => { - for name in names { - let name = match name { - Expr::Alias(name, asname) => (name, asname), - _ => panic!() - }; - let nameas = name.1; - let name = name.0; - match nameas { + stmt::ImportFrom(module, names, level) => { + /* + for alias in names { + let asname = alias.asname; + let name = alias.name; + match asname { Some(name) => block.bindings.push(name), None => block.bindings.push(name) } } + */ None }, _ => None @@ -75,3 +73,75 @@ pub fn scope_ast(modules: Vec<Module>) - } scoping } + +/* +#[derive(Debug)] +struct NewBlockStatement { + statement: stmt, + block: Option<NewBlock> +} + +#[derive(Debug)] +enum Binding { + Bound, + Free +} + +#[derive(Debug)] +struct NewBlock { + statements: Vec<NewBlockStatement>, + bindings: Vec<String>, + variables: HashMap<String, Binding> +} + +#[derive(Debug)] +pub struct NewScoping { + modules: HashMap<String, NewBlock> +} + +fn check_expr(expr: expr, block: Block) { + println!("{:?}", expr); +} + +fn check_block(block: Block) -> NewBlock { + let mut variables = HashMap::new(); + let mut statements = vec!(); + for blocked_statement in block.statements { + let statement = match blocked_statement.statement { + stmt::FunctionDef(name, args, body) => { + // No need to recurse here, this will be in a new block. + variables.insert(name.clone(), Binding::Bound); + stmt::FunctionDef(name, args, body) + }, + stmt::Assign(targets, value) => { + for target in targets.clone() { + match target { + expr::Name(name) => { + variables.insert(name, Binding::Bound); + }, + _ => panic!() + } + } + stmt::Assign(targets, value) + }, + any => any + }; + let new_block = match blocked_statement.block { + Some(block) => Some(check_block(block)), + None => None + }; + statements.push(NewBlockStatement{statement: statement, block: new_block}); + } + NewBlock{statements: statements, bindings: block.bindings, variables: variables} +} + +#[allow(dead_code)] +pub fn check_scoping(scoping: Scoping) -> NewScoping { + let mut modules = HashMap::new(); + for (name, block) in scoping.modules { + modules.insert(name, check_block(block)); + } + let mut new_scoping = NewScoping{modules: modules}; + new_scoping +} +*/