schala/schala-lang/language/src/symbol_table/resolver.rs

110 lines
3.7 KiB
Rust
Raw Normal View History

2021-10-19 13:48:00 -07:00
use std::rc::Rc;
2021-10-19 21:14:15 -07:00
use crate::symbol_table::{SymbolTable, Fqsn, Scope};
2021-10-19 13:48:00 -07:00
use crate::ast::*;
use crate::util::ScopeStack;
2021-10-19 21:14:15 -07:00
type FqsnPrefix = Vec<Scope>;
2021-10-19 13:48:00 -07:00
pub struct Resolver<'a> {
symbol_table: &'a mut super::SymbolTable,
2021-10-19 21:14:15 -07:00
name_scope_stack: ScopeStack<'a, Rc<String>, FqsnPrefix>,
2021-10-19 13:48:00 -07:00
}
impl<'a> Resolver<'a> {
pub fn new(symbol_table: &'a mut SymbolTable) -> Self {
2021-10-19 21:14:15 -07:00
let name_scope_stack: ScopeStack<'a, Rc<String>, FqsnPrefix> = ScopeStack::new(None);
2021-10-19 13:48:00 -07:00
Self { symbol_table, name_scope_stack }
}
2021-10-19 19:19:21 -07:00
pub fn resolve(&mut self, ast: &AST) {
2021-10-19 13:48:00 -07:00
walk_ast(self, ast);
}
2021-10-19 21:14:15 -07:00
fn lookup_name_in_scope(&self, sym_name: &QualifiedName) -> Fqsn {
2021-10-19 13:48:00 -07:00
let QualifiedName { components, .. } = sym_name;
let first_component = &components[0];
match self.name_scope_stack.lookup(first_component) {
None => {
2021-10-19 21:14:15 -07:00
Fqsn {
2021-10-19 13:48:00 -07:00
scopes: components.iter()
2021-10-19 17:22:35 -07:00
.map(|name| Scope::Name(name.clone()))
2021-10-19 13:48:00 -07:00
.collect()
}
},
Some(fqsn_prefix) => {
let mut full_name = fqsn_prefix.clone();
2021-10-19 21:14:15 -07:00
let rest_of_name: FqsnPrefix = components[1..].iter().map(|name| Scope::Name(name.clone())).collect();
2021-10-19 13:48:00 -07:00
full_name.extend_from_slice(&rest_of_name);
2021-10-19 21:14:15 -07:00
Fqsn {
2021-10-19 13:48:00 -07:00
scopes: full_name
}
}
}
}
// This might be a variable or a pattern, depending on whether this symbol
// already exists in the table.
fn qualified_name_in_pattern(&mut self, qualified_name: &QualifiedName) {
let fqsn = self.lookup_name_in_scope(qualified_name);
if self.symbol_table.fqsn_to_symbol.get(&fqsn).is_some() {
self.symbol_table.id_to_fqsn.insert(qualified_name.id.clone(), fqsn); //TODO maybe set this to an explicit value if none?
}
}
}
impl<'a> ASTVisitor for Resolver<'a> {
//TODO need to un-insert these - maybe need to rethink visitor
fn import(&mut self, import_spec: &ImportSpecifier) {
let ImportSpecifier { ref path_components, ref imported_names, .. } = &import_spec;
match imported_names {
ImportedNames::All => {
2021-10-19 21:14:15 -07:00
let prefix = Fqsn {
2021-10-19 17:22:35 -07:00
scopes: path_components.iter().map(|c| Scope::Name(c.clone())).collect()
2021-10-19 13:48:00 -07:00
};
let members = self.symbol_table.symbol_trie.get_children(&prefix);
for member in members.into_iter() {
2021-10-19 13:54:32 -07:00
let Scope::Name(n) = member.scopes.last().unwrap();
2021-10-19 17:22:35 -07:00
let local_name = n.clone();
2021-10-19 13:54:32 -07:00
self.name_scope_stack.insert(local_name, member.scopes);
2021-10-19 13:48:00 -07:00
}
},
ImportedNames::LastOfPath => {
let name = path_components.last().unwrap(); //TODO handle better
let fqsn_prefix = path_components.iter()
2021-10-19 17:22:35 -07:00
.map(|c| Scope::Name(c.clone()))
2021-10-19 13:48:00 -07:00
.collect();
self.name_scope_stack.insert(name.clone(), fqsn_prefix);
}
ImportedNames::List(ref names) => {
2021-10-19 21:14:15 -07:00
let fqsn_prefix: FqsnPrefix = path_components.iter()
2021-10-19 17:22:35 -07:00
.map(|c| Scope::Name(c.clone()))
2021-10-19 13:48:00 -07:00
.collect();
for name in names.iter() {
self.name_scope_stack.insert(name.clone(), fqsn_prefix.clone());
}
}
};
}
fn qualified_name(&mut self, qualified_name: &QualifiedName) {
2021-10-19 21:18:57 -07:00
let fqsn = self.lookup_name_in_scope(qualified_name);
2021-10-19 13:48:00 -07:00
self.symbol_table.id_to_fqsn.insert(qualified_name.id.clone(), fqsn);
}
2021-10-19 22:24:27 -07:00
fn named_struct(&mut self, qualified_name: &QualifiedName, _fields: &[ (Rc<String>, Expression) ]) {
2021-10-19 21:18:57 -07:00
let fqsn = self.lookup_name_in_scope(qualified_name);
2021-10-19 13:48:00 -07:00
self.symbol_table.id_to_fqsn.insert(qualified_name.id.clone(), fqsn);
}
fn pattern(&mut self, pat: &Pattern) {
use Pattern::*;
match pat {
//TODO I think not handling TuplePattern is an oversight
TuplePattern(_) => (),
Literal(_) | Ignored => (),
TupleStruct(name, _) | Record(name, _) | VarOrName(name) => self.qualified_name_in_pattern(name),
};
}
}