2019-09-24 03:28:59 -07:00
|
|
|
use std::rc::Rc;
|
|
|
|
|
2019-10-21 04:17:30 -07:00
|
|
|
use crate::schala::SymbolTableHandle;
|
|
|
|
use crate::symbol_table::{ScopeSegment, FullyQualifiedSymbolName};
|
2019-09-03 01:42:28 -07:00
|
|
|
use crate::ast::*;
|
2019-09-24 03:28:59 -07:00
|
|
|
use crate::util::ScopeStack;
|
|
|
|
|
|
|
|
type FQSNPrefix = Vec<ScopeSegment>;
|
2019-09-03 01:42:28 -07:00
|
|
|
|
2019-09-09 17:45:34 -07:00
|
|
|
pub struct ScopeResolver<'a> {
|
2019-10-21 04:17:30 -07:00
|
|
|
symbol_table_handle: SymbolTableHandle,
|
2019-09-24 03:28:59 -07:00
|
|
|
name_scope_stack: ScopeStack<'a, Rc<String>, FQSNPrefix>,
|
2019-09-03 02:19:37 -07:00
|
|
|
}
|
|
|
|
|
2019-10-15 19:06:07 -07:00
|
|
|
impl<'a> ASTVisitor for ScopeResolver<'a> {
|
2019-10-15 23:59:37 -07:00
|
|
|
fn import(&mut self, import_spec: &ImportSpecifier) {
|
2019-10-21 04:17:30 -07:00
|
|
|
let ref symbol_table = self.symbol_table_handle.borrow();
|
2019-09-24 18:42:01 -07:00
|
|
|
let ImportSpecifier { ref path_components, ref imported_names, .. } = &import_spec;
|
|
|
|
match imported_names {
|
2019-10-18 09:55:26 -07:00
|
|
|
ImportedNames::All => {
|
2019-10-18 18:24:57 -07:00
|
|
|
let prefix = FullyQualifiedSymbolName(path_components.iter().map(|c| ScopeSegment {
|
|
|
|
name: c.clone(),
|
2019-10-18 09:55:26 -07:00
|
|
|
}).collect());
|
2019-10-21 04:17:30 -07:00
|
|
|
let members = symbol_table.lookup_children_of_fqsn(&prefix);
|
2019-10-18 09:55:26 -07:00
|
|
|
for member in members.into_iter() {
|
|
|
|
let local_name = member.0.last().unwrap().name.clone();
|
|
|
|
self.name_scope_stack.insert(local_name.clone(), member.0);
|
|
|
|
}
|
|
|
|
},
|
2019-09-24 18:42:01 -07:00
|
|
|
ImportedNames::LastOfPath => {
|
|
|
|
let name = path_components.last().unwrap(); //TODO handle better
|
|
|
|
let fqsn_prefix = path_components.iter().map(|c| ScopeSegment {
|
2019-10-18 18:24:57 -07:00
|
|
|
name: c.clone(),
|
2019-09-24 18:42:01 -07:00
|
|
|
}).collect();
|
|
|
|
self.name_scope_stack.insert(name.clone(), fqsn_prefix);
|
|
|
|
}
|
2019-09-24 19:24:07 -07:00
|
|
|
ImportedNames::List(ref names) => {
|
|
|
|
let fqsn_prefix: FQSNPrefix = path_components.iter().map(|c| ScopeSegment {
|
2019-10-18 18:24:57 -07:00
|
|
|
name: c.clone(),
|
2019-09-24 19:24:07 -07:00
|
|
|
}).collect();
|
|
|
|
for name in names.iter() {
|
|
|
|
self.name_scope_stack.insert(name.clone(), fqsn_prefix.clone());
|
|
|
|
}
|
|
|
|
}
|
2019-09-24 18:42:01 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-10-15 23:59:37 -07:00
|
|
|
fn qualified_name(&mut self, qualified_name: &QualifiedName) {
|
2019-10-21 04:17:30 -07:00
|
|
|
let ref mut symbol_table = self.symbol_table_handle.borrow_mut();
|
2019-10-15 23:59:37 -07:00
|
|
|
let fqsn = self.lookup_name_in_scope(&qualified_name);
|
|
|
|
let ref id = qualified_name.id;
|
2019-10-21 04:17:30 -07:00
|
|
|
symbol_table.map_id_to_fqsn(id, fqsn);
|
2019-09-03 10:23:38 -07:00
|
|
|
}
|
2019-09-08 02:11:15 -07:00
|
|
|
|
2019-10-15 23:59:37 -07:00
|
|
|
fn named_struct(&mut self, name: &QualifiedName, _fields: &Vec<(Rc<String>, Expression)>) {
|
2019-10-21 04:17:30 -07:00
|
|
|
let ref mut symbol_table = self.symbol_table_handle.borrow_mut();
|
2019-10-15 23:59:37 -07:00
|
|
|
let ref id = name.id;
|
|
|
|
let fqsn = self.lookup_name_in_scope(&name);
|
2019-10-21 04:17:30 -07:00
|
|
|
symbol_table.map_id_to_fqsn(id, fqsn);
|
2019-10-10 18:33:34 -07:00
|
|
|
}
|
|
|
|
|
2019-10-15 23:59:37 -07:00
|
|
|
fn pattern(&mut self, pat: &Pattern) {
|
2019-09-08 02:11:15 -07:00
|
|
|
use Pattern::*;
|
|
|
|
match pat {
|
|
|
|
Ignored => (),
|
2019-10-16 10:39:48 -07:00
|
|
|
TuplePattern(_) => (),
|
2019-09-08 02:11:15 -07:00
|
|
|
Literal(_) => (),
|
2019-10-15 23:59:37 -07:00
|
|
|
TupleStruct(name, _) => self.qualified_name_in_pattern(name),
|
|
|
|
Record(name, _) => self.qualified_name_in_pattern(name),
|
|
|
|
VarOrName(name) => self.qualified_name_in_pattern(name),
|
2019-09-08 02:11:15 -07:00
|
|
|
};
|
2019-10-15 23:59:37 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> ScopeResolver<'a> {
|
2019-10-21 04:17:30 -07:00
|
|
|
pub fn new(symbol_table_handle: SymbolTableHandle) -> ScopeResolver<'static> {
|
2019-10-15 23:59:37 -07:00
|
|
|
let name_scope_stack = ScopeStack::new(None);
|
2019-10-21 04:17:30 -07:00
|
|
|
ScopeResolver { symbol_table_handle, name_scope_stack }
|
2019-10-15 23:59:37 -07:00
|
|
|
}
|
|
|
|
pub fn resolve(&mut self, ast: &mut AST) -> Result<(), String> {
|
|
|
|
walk_ast(self, ast);
|
2019-10-16 10:39:48 -07:00
|
|
|
Ok(())
|
2019-10-15 23:59:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn lookup_name_in_scope(&self, sym_name: &QualifiedName) -> FullyQualifiedSymbolName {
|
2019-10-21 02:56:21 -07:00
|
|
|
let QualifiedName { components, .. } = sym_name;
|
|
|
|
let first_component = &components[0];
|
|
|
|
match self.name_scope_stack.lookup(first_component) {
|
|
|
|
None => {
|
|
|
|
FullyQualifiedSymbolName(components.iter().map(|name| ScopeSegment { name: name.clone() }).collect())
|
|
|
|
},
|
|
|
|
Some(fqsn_prefix) => {
|
|
|
|
let mut full_name = fqsn_prefix.clone();
|
|
|
|
let rest_of_name: FQSNPrefix = components[1..].iter().map(|name| ScopeSegment { name: name.clone() }).collect();
|
|
|
|
full_name.extend_from_slice(&rest_of_name);
|
|
|
|
FullyQualifiedSymbolName(full_name)
|
|
|
|
}
|
|
|
|
}
|
2019-09-08 02:11:15 -07:00
|
|
|
}
|
|
|
|
|
2019-09-08 04:27:04 -07:00
|
|
|
/// this might be a variable or a pattern. if a variable, set to none
|
2019-09-20 02:21:39 -07:00
|
|
|
fn qualified_name_in_pattern(&mut self, qualified_name: &QualifiedName) {
|
2019-10-21 04:17:30 -07:00
|
|
|
let ref mut symbol_table = self.symbol_table_handle.borrow_mut();
|
2019-09-20 02:03:10 -07:00
|
|
|
let ref id = qualified_name.id;
|
2019-09-25 03:18:54 -07:00
|
|
|
let fqsn = self.lookup_name_in_scope(qualified_name);
|
2019-10-21 04:17:30 -07:00
|
|
|
if symbol_table.lookup_by_fqsn(&fqsn).is_some() {
|
|
|
|
symbol_table.map_id_to_fqsn(&id, fqsn);
|
2019-09-20 01:44:20 -07:00
|
|
|
}
|
2019-09-08 02:11:15 -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
|
|
|
}
|