Add builder & linker

This commit is contained in:
Shunsuke Shibayama 2022-09-21 01:21:17 +09:00
parent 671fbee518
commit 201b313cd2
19 changed files with 241 additions and 109 deletions

View file

@ -27,24 +27,24 @@ impl ModId {
#[derive(Debug)]
pub struct ModuleEntry {
_id: ModId, // builtin == 0, __main__ == 1
_hir: Option<HIR>,
id: ModId, // builtin == 0, __main__ == 1
pub hir: Option<HIR>,
ctx: Rc<Context>,
}
impl ModuleEntry {
pub fn new(id: ModId, hir: Option<HIR>, ctx: Context) -> Self {
Self {
_id: id,
_hir: hir,
id,
hir,
ctx: Rc::new(ctx),
}
}
pub fn builtin(ctx: Context) -> Self {
Self {
_id: ModId::builtin(),
_hir: None,
id: ModId::builtin(),
hir: None,
ctx: Rc::new(ctx),
}
}
@ -53,11 +53,15 @@ impl ModuleEntry {
#[derive(Debug, Default)]
pub struct ModuleCache {
cache: Dict<VarName, ModuleEntry>,
last_id: usize,
}
impl ModuleCache {
pub fn new() -> Self {
Self { cache: Dict::new() }
Self {
cache: Dict::new(),
last_id: 0,
}
}
pub fn get<Q: Eq + Hash + ?Sized>(&self, name: &Q) -> Option<&ModuleEntry>
@ -67,7 +71,10 @@ impl ModuleCache {
self.cache.get(name)
}
pub fn register(&mut self, name: VarName, entry: ModuleEntry) {
pub fn register(&mut self, name: VarName, hir: Option<HIR>, ctx: Context) {
self.last_id += 1;
let id = ModId::new(self.last_id);
let entry = ModuleEntry::new(id, hir, ctx);
self.cache.insert(name, entry);
}
@ -77,6 +84,20 @@ impl ModuleCache {
{
self.cache.remove(name)
}
pub fn remove_by_id(&mut self, id: ModId) -> Option<ModuleEntry> {
if let Some(name) = self.cache.iter().find_map(|(name, ent)| {
if ent.id == id {
Some(name.clone())
} else {
None
}
}) {
self.remove(&name)
} else {
None
}
}
}
#[derive(Debug, Clone, Default)]
@ -102,14 +123,18 @@ impl SharedModuleCache {
ref_.get(name).map(|entry| entry.ctx.as_ref())
}
pub fn register(&self, name: VarName, entry: ModuleEntry) {
self.0.borrow_mut().register(name, entry);
pub fn register(&self, name: VarName, hir: Option<HIR>, ctx: Context) {
self.0.borrow_mut().register(name, hir, ctx);
}
pub fn remove<Q: Eq + Hash + ?Sized>(&mut self, name: &Q) -> Option<ModuleEntry>
pub fn remove<Q: Eq + Hash + ?Sized>(&self, name: &Q) -> Option<ModuleEntry>
where
VarName: Borrow<Q>,
{
self.0.borrow_mut().remove(name)
}
pub fn remove_by_id(&self, id: ModId) -> Option<ModuleEntry> {
self.0.borrow_mut().remove_by_id(id)
}
}