Qualified lookups return params

This commit is contained in:
Agus Zubiaga 2024-05-09 21:56:41 -03:00
parent 96e2d32fa6
commit 9d26adb228
No known key found for this signature in database
6 changed files with 112 additions and 42 deletions

View file

@ -622,6 +622,22 @@ impl ModuleIds {
}
}
#[derive(Debug, Clone)]
pub struct LookedupSymbol {
pub symbol: Symbol,
pub params: Option<Symbol>,
}
impl LookedupSymbol {
pub fn new(symbol: Symbol, params: Option<Symbol>) -> Self {
Self { symbol, params }
}
pub fn no_params(symbol: Symbol) -> Self {
Self::new(symbol, None)
}
}
#[derive(Debug, Clone)]
pub struct ScopeModules {
modules: VecMap<ModuleName, ModuleId>,
@ -629,6 +645,22 @@ pub struct ScopeModules {
params: Vec<Option<Symbol>>,
}
pub struct LookedupModule {
pub id: ModuleId,
pub params: Option<Symbol>,
}
impl LookedupModule {
pub fn into_symbol(&self, symbol: Symbol) -> LookedupSymbol {
debug_assert_eq!(symbol.module_id(), self.id);
LookedupSymbol {
symbol,
params: self.params,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ScopeModuleSource {
Builtin,
@ -637,12 +669,22 @@ pub enum ScopeModuleSource {
}
impl ScopeModules {
pub fn get_id(&self, module_name: &ModuleName) -> Option<ModuleId> {
self.modules.get(module_name).copied()
pub fn lookup(&self, module_name: &ModuleName) -> Option<LookedupModule> {
self.modules
.get_with_index(module_name)
.map(|(index, module_id)| LookedupModule {
id: *module_id,
params: self.params.get(index).copied().unwrap(),
})
}
pub fn has_id(&self, module_id: ModuleId) -> bool {
self.sources.contains_key(&module_id)
pub fn lookup_by_id(&self, module_id: &ModuleId) -> Option<LookedupModule> {
self.modules
.get_index_by_value(module_id)
.map(|index| LookedupModule {
id: *module_id,
params: self.params.get(index).copied().unwrap(),
})
}
pub fn available_names(&self) -> impl Iterator<Item = &ModuleName> {