deimos-lang/src/name_analysis/symbol_tree.rs

58 lines
1.9 KiB
Rust

use std::cell::RefCell;
use crate::name_analysis::symbol::class_symbol::ClassSymbol;
use crate::name_analysis::symbol::function_symbol::FunctionSymbol;
use crate::name_analysis::symbol::interface_symbol::InterfaceSymbol;
use std::collections::HashMap;
use std::rc::Rc;
pub struct SymbolTree {
children: Box<HashMap<String, SymbolTree>>,
classes: Box<HashMap<String, Rc<RefCell<ClassSymbol>>>>,
interfaces: Box<HashMap<String, Rc<RefCell<InterfaceSymbol>>>>,
functions: Box<HashMap<String, Rc<RefCell<FunctionSymbol>>>>,
}
impl SymbolTree {
pub fn new() -> Self {
Self {
children: Box::new(HashMap::new()),
classes: Box::new(HashMap::new()),
interfaces: Box::new(HashMap::new()),
functions: Box::new(HashMap::new()),
}
}
pub fn find_class(&self, fqn_parts: &[&str]) -> Option<Rc<RefCell<ClassSymbol>>> {
match fqn_parts.len() {
0 => None,
1 => self.classes.get(fqn_parts[0]).cloned(),
_ => self
.children
.get(fqn_parts[0])
.and_then(|child_tree| child_tree.find_class(&fqn_parts[1..])),
}
}
pub fn find_interface(&self, fqn_parts: &[&str]) -> Option<Rc<RefCell<InterfaceSymbol>>> {
match fqn_parts.len() {
0 => None,
1 => self.interfaces.get(fqn_parts[0]).cloned(),
_ => self
.children
.get(fqn_parts[0])
.and_then(|child_tree| child_tree.find_interface(&fqn_parts[1..])),
}
}
pub fn find_function(&self, fqn_parts: &[&str]) -> Option<Rc<RefCell<FunctionSymbol>>> {
match fqn_parts.len() {
0 => None,
1 => self.functions.get(fqn_parts[0]).cloned(),
_ => self
.children
.get(fqn_parts[0])
.and_then(|child_tree| child_tree.find_function(&fqn_parts[1..])),
}
}
}