Start work on symbol table lookup by type name

This commit is contained in:
greg 2019-08-21 10:08:39 -07:00
parent a74027bb1f
commit 3d6447abb4

View File

@ -28,14 +28,16 @@ enum ScopeSegmentKind {
//cf. p. 150 or so of Language Implementation Patterns //cf. p. 150 or so of Language Implementation Patterns
pub struct SymbolTable { pub struct SymbolTable {
values: HashMap<PathToSymbol, Symbol>, symbol_path_to_symbol: HashMap<PathToSymbol, Symbol>,
type_name_to_symbol: HashMap<Rc<String>, PathToSymbol>
} }
//TODO add various types of lookups here, maybe multiple hash tables internally? //TODO add various types of lookups here, maybe multiple hash tables internally?
impl SymbolTable { impl SymbolTable {
pub fn new() -> SymbolTable { pub fn new() -> SymbolTable {
SymbolTable { SymbolTable {
values: HashMap::new(), symbol_path_to_symbol: HashMap::new(),
type_name_to_symbol: HashMap::new(),
} }
} }
@ -44,7 +46,7 @@ impl SymbolTable {
vec.push(name.clone()); vec.push(name.clone());
let symbol_path = PathToSymbol(vec); let symbol_path = PathToSymbol(vec);
let symbol = Symbol { name: name.clone(), scopes: scope_path.to_vec(), spec }; let symbol = Symbol { name: name.clone(), scopes: scope_path.to_vec(), spec };
self.values.insert(symbol_path, symbol); self.symbol_path_to_symbol.insert(symbol_path, symbol);
} }
pub fn lookup_by_name(&self, name: &Rc<String>) -> Option<&Symbol> { pub fn lookup_by_name(&self, name: &Rc<String>) -> Option<&Symbol> {
@ -55,7 +57,11 @@ impl SymbolTable {
let mut vec = path.clone(); let mut vec = path.clone();
vec.push(name.clone()); vec.push(name.clone());
let symbol_path = PathToSymbol(vec); let symbol_path = PathToSymbol(vec);
self.values.get(&symbol_path) self.symbol_path_to_symbol.get(&symbol_path)
}
pub fn lookup_by_type_and_tag(&self, type_name: &Rc<String>, tag: usize) -> Option<&Symbol> {
unimplemented!()
} }
} }
@ -163,7 +169,7 @@ impl SymbolTable {
} }
pub fn debug_symbol_table(&self) -> String { pub fn debug_symbol_table(&self) -> String {
let mut output = format!("Symbol table\n"); let mut output = format!("Symbol table\n");
for (name, sym) in &self.values { for (name, sym) in &self.symbol_path_to_symbol {
write!(output, "{:?} -> {}\n", name, sym).unwrap(); write!(output, "{:?} -> {}\n", name, sym).unwrap();
} }
output output