use crate::symbol_table::{ScopeSegment, ScopeSegmentKind, FullyQualifiedSymbolName}; use crate::ast::*; pub struct ScopeResolver { } impl ScopeResolver { pub fn new() -> ScopeResolver { ScopeResolver { } } pub fn resolve(&mut self, ast: &mut AST) -> Result<(), String> { println!("Resolving scopes - nothing so far!"); for statement in ast.0.iter_mut() { match statement.mut_node() { Statement::Declaration(ref mut decl) => self.decl(decl), Statement::ExpressionStatement(ref mut expr) => self.expr(expr), }?; } Ok(()) } fn decl(&mut self, decl: &mut Declaration) -> Result<(), String> { match decl { Declaration::Binding { expr, .. } => self.expr(expr), _ => Ok(()), } } fn expr(&mut self, expr: &mut Meta) -> Result<(), String> { use ExpressionKind::*; let inner_expr = expr.mut_node(); match &mut inner_expr.kind { ExpressionKind::Value(qualified_name) => { let fqsn = lookup_name_in_scope(&qualified_name.node()); expr.fqsn = Some(fqsn); }, NamedStruct { name, .. } => { let fqsn = lookup_name_in_scope(&name.node()); expr.fqsn = Some(fqsn); }, BinExp(_, ref mut lhs, ref mut rhs) => { self.expr(lhs)?; self.expr(rhs)?; }, PrefixExp(_, ref mut arg) => { self.expr(arg)?; }, TupleLiteral(exprs) => { for expr in exprs.iter_mut() { self.expr(expr)?; } }, Call { ref mut f, arguments } => { self.expr(f)?; for arg in arguments.iter_mut() { self.invoc(arg)?; } } _ => () }; Ok(()) } fn invoc(&mut self, invoc: &mut InvocationArgument) -> Result<(), String> { use InvocationArgument::*; match invoc { Positional(expr) => self.expr(expr), Keyword { expr, .. } => self.expr(expr), _ => Ok(()) } } } //TODO this is incomplete fn lookup_name_in_scope(sym_name: &QualifiedName) -> FullyQualifiedSymbolName { let QualifiedName(vec) = sym_name; let len = vec.len(); let new_vec: Vec = vec.iter().enumerate().map(|(i, name)| { let kind = if i == (len - 1) { ScopeSegmentKind::Terminal } else { ScopeSegmentKind::Type }; ScopeSegment { name: name.clone(), kind } }).collect(); FullyQualifiedSymbolName(new_vec) } #[cfg(test)] mod tests { #[test] fn basic_scope() { } }