2019-09-03 03:20:17 -07:00
|
|
|
use std::rc::Rc;
|
|
|
|
|
|
|
|
use crate::symbol_table::{ScopeSegment, ScopeSegmentKind, FullyQualifiedSymbolName};
|
2019-09-03 01:42:28 -07:00
|
|
|
use crate::ast::*;
|
|
|
|
|
2019-09-03 02:59:19 -07:00
|
|
|
pub struct ScopeResolver {
|
2019-09-03 02:19:37 -07:00
|
|
|
}
|
|
|
|
|
2019-09-03 02:59:19 -07:00
|
|
|
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> {
|
2019-09-03 03:20:17 -07:00
|
|
|
match decl {
|
|
|
|
Declaration::Binding { expr, .. } => self.expr(expr),
|
|
|
|
_ => Ok(()),
|
|
|
|
}
|
2019-09-03 02:59:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn expr(&mut self, expr: &mut Meta<Expression>) -> Result<(), String> {
|
2019-09-03 03:20:17 -07:00
|
|
|
match &expr.node().kind {
|
|
|
|
|
|
|
|
//TODO this needs to fully recurse
|
|
|
|
ExpressionKind::Value(qualified_name) => {
|
|
|
|
//TODO fill this out
|
|
|
|
let fqsn = lookup_name_in_scope(&qualified_name);
|
|
|
|
expr.fqsn = Some(fqsn);
|
|
|
|
},
|
|
|
|
_ => ()
|
|
|
|
};
|
2019-09-03 02:59:19 -07:00
|
|
|
Ok(())
|
|
|
|
}
|
2019-09-03 03:20:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//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<ScopeSegment> = 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)
|
|
|
|
}
|
2019-09-03 02:59:19 -07:00
|
|
|
|
2019-09-03 03:20:17 -07:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
#[test]
|
|
|
|
fn basic_scope() {
|
|
|
|
|
|
|
|
}
|
2019-09-03 01:42:28 -07:00
|
|
|
}
|