Fix last test

This commit is contained in:
Greg Shuflin 2021-10-19 14:12:57 -07:00
parent 3060afd752
commit d1d3a70339

View File

@ -124,6 +124,9 @@ impl ScopeSegment {
pub struct SymbolTable { pub struct SymbolTable {
decl_locations: DeclLocations, //TODO delete this decl_locations: DeclLocations, //TODO delete this
symbol_path_to_symbol: HashMap<FullyQualifiedSymbolName, Symbol>, symbol_path_to_symbol: HashMap<FullyQualifiedSymbolName, Symbol>,
/// Used for import resolution.
symbol_trie: SymbolTrie, symbol_trie: SymbolTrie,
/// These tables are responsible for preventing duplicate names. /// These tables are responsible for preventing duplicate names.
@ -131,6 +134,7 @@ pub struct SymbolTable {
types: NameTable<TypeKind>, types: NameTable<TypeKind>,
/// A map of the `ItemId`s of instances of use of names to their fully-canonicalized FQSN form. /// A map of the `ItemId`s of instances of use of names to their fully-canonicalized FQSN form.
/// Updated by the item id resolver.
id_to_fqsn: HashMap<ItemId, FQSN>, id_to_fqsn: HashMap<ItemId, FQSN>,
/// A map of the FQSN of an AST definition to a Symbol data structure, which contains /// A map of the FQSN of an AST definition to a Symbol data structure, which contains
@ -153,6 +157,16 @@ impl SymbolTable {
} }
} }
/// The main entry point into the symbol table. This will traverse the AST in several
/// different ways and populate subtables with information that will be used further in the
/// compilation process.
pub fn process_ast(&mut self, ast: &ast::AST) -> Result<(), String> {
self.populate_name_tables(ast)?;
self.resolve_symbol_ids(ast)?;
Ok(())
}
pub fn lookup_symbol(&self, id: &ItemId) -> Option<&Symbol> { pub fn lookup_symbol(&self, id: &ItemId) -> Option<&Symbol> {
let fqsn = self.id_to_fqsn.get(id); let fqsn = self.id_to_fqsn.get(id);
fqsn.and_then(|fqsn| self.fqsn_to_symbol.get(fqsn)) fqsn.and_then(|fqsn| self.fqsn_to_symbol.get(fqsn))
@ -220,15 +234,6 @@ impl SymbolTable {
* later */ * later */
/// The main entry point into the symbol table. This will traverse the AST in several
/// different ways and populate subtables with information that will be used further in the
/// compilation process.
pub fn process_ast(&mut self, ast: &ast::AST) -> Result<(), String> {
self.populate_name_tables(ast)?;
self.resolve_symbol_ids(ast)?;
Ok(())
}
/// Walks the AST, matching the ID of an identifier used in some expression to /// Walks the AST, matching the ID of an identifier used in some expression to
/// the corresponding Symbol. /// the corresponding Symbol.
@ -249,6 +254,12 @@ impl SymbolTable {
Ok(()) Ok(())
} }
fn add_symbol(&mut self, fqsn: FQSN, symbol: Symbol) {
self.symbol_trie.insert(&fqsn);
self.fqsn_to_symbol.insert(fqsn, symbol);
}
//TODO this should probably return a vector of duplicate name errors //TODO this should probably return a vector of duplicate name errors
fn add_from_scope<'a>(&'a mut self, statements: &[Statement], scope_stack: &mut Vec<Scope>) -> Result<(), DuplicateName> { fn add_from_scope<'a>(&'a mut self, statements: &[Statement], scope_stack: &mut Vec<Scope>) -> Result<(), DuplicateName> {
for statement in statements { for statement in statements {
@ -262,7 +273,7 @@ impl SymbolTable {
self.fq_names.register(fq_function.clone(), NameSpec { location, kind: NameKind::Function })?; self.fq_names.register(fq_function.clone(), NameSpec { location, kind: NameKind::Function })?;
self.types.register(fq_function.clone(), NameSpec { location, kind: TypeKind } )?; self.types.register(fq_function.clone(), NameSpec { location, kind: TypeKind } )?;
self.fqsn_to_symbol.insert(fq_function, Symbol { self.add_symbol(fq_function, Symbol {
local_name: signature.name.clone(), local_name: signature.name.clone(),
spec: SymbolSpec::Func(vec![]), //TODO does this inner vec need to exist at all? spec: SymbolSpec::Func(vec![]), //TODO does this inner vec need to exist at all?
}); });
@ -274,7 +285,7 @@ impl SymbolTable {
self.fq_names.register(fq_function.clone(), NameSpec { location, kind: NameKind::Function })?; self.fq_names.register(fq_function.clone(), NameSpec { location, kind: NameKind::Function })?;
self.types.register(fq_function.clone(), NameSpec { location, kind: TypeKind } )?; self.types.register(fq_function.clone(), NameSpec { location, kind: TypeKind } )?;
self.fqsn_to_symbol.insert(fq_function, Symbol { self.add_symbol(fq_function, Symbol {
local_name: signature.name.clone(), local_name: signature.name.clone(),
spec: SymbolSpec::Func(vec![]), //TODO does this inner vec need to exist at all? spec: SymbolSpec::Func(vec![]), //TODO does this inner vec need to exist at all?
}); });
@ -294,7 +305,7 @@ impl SymbolTable {
StatementKind::Declaration(Declaration::Binding { name, .. }) => { StatementKind::Declaration(Declaration::Binding { name, .. }) => {
let fq_binding = FQSN::from_scope_stack(scope_stack.as_ref(), name.as_str().to_owned()); let fq_binding = FQSN::from_scope_stack(scope_stack.as_ref(), name.as_str().to_owned());
self.fq_names.register(fq_binding.clone(), NameSpec { location, kind: NameKind::Binding })?; self.fq_names.register(fq_binding.clone(), NameSpec { location, kind: NameKind::Binding })?;
self.fqsn_to_symbol.insert(fq_binding, Symbol { self.add_symbol(fq_binding, Symbol {
local_name: name.clone(), local_name: name.clone(),
spec: SymbolSpec::Binding, spec: SymbolSpec::Binding,
}); });
@ -328,7 +339,7 @@ impl SymbolTable {
_ => panic!("This should never happen"), _ => panic!("This should never happen"),
}; };
let symbol = Symbol { local_name, spec }; let symbol = Symbol { local_name, spec };
self.fqsn_to_symbol.insert(fqsn, symbol); self.add_symbol(fqsn, symbol);
}; };
}; };