Add Runnable::initialize

This commit is contained in:
Shunsuke Shibayama 2022-11-23 19:43:56 +09:00
parent 64dcc17399
commit 7bcc766500
11 changed files with 80 additions and 2 deletions

View file

@ -357,6 +357,9 @@ pub trait Runnable: Sized + Default {
fn new(cfg: ErgConfig) -> Self; fn new(cfg: ErgConfig) -> Self;
fn cfg(&self) -> &ErgConfig; fn cfg(&self) -> &ErgConfig;
fn finish(&mut self); // called when the :exit command is received. fn finish(&mut self); // called when the :exit command is received.
/// Erase all but immutable information.
fn initialize(&mut self);
/// Erase information that will no longer be meaningful in the next iteration
fn clear(&mut self); fn clear(&mut self);
fn eval(&mut self, src: String) -> Result<String, Self::Errs>; fn eval(&mut self, src: String) -> Result<String, Self::Errs>;
fn exec(&mut self) -> Result<i32, Self::Errs>; fn exec(&mut self) -> Result<i32, Self::Errs>;

View file

@ -45,7 +45,15 @@ impl Runnable for HIRBuilder {
#[inline] #[inline]
fn finish(&mut self) {} fn finish(&mut self) {}
fn clear(&mut self) {} fn initialize(&mut self) {
self.lowerer.initialize();
self.ownership_checker = OwnershipChecker::new(self.cfg().copy());
}
fn clear(&mut self) {
self.lowerer.clear();
self.ownership_checker = OwnershipChecker::new(self.cfg().copy());
}
fn exec(&mut self) -> Result<i32, Self::Errs> { fn exec(&mut self) -> Result<i32, Self::Errs> {
let mut builder = ASTBuilder::new(self.cfg().copy()); let mut builder = ASTBuilder::new(self.cfg().copy());

View file

@ -129,7 +129,14 @@ impl Runnable for Compiler {
#[inline] #[inline]
fn finish(&mut self) {} fn finish(&mut self) {}
fn initialize(&mut self) {
self.builder.initialize();
self.code_generator.clear();
// .mod_cache will be initialized in .builder
}
fn clear(&mut self) { fn clear(&mut self) {
self.builder.clear();
self.code_generator.clear(); self.code_generator.clear();
} }

View file

@ -900,6 +900,29 @@ impl Context {
} }
} }
/// This method is intended to be called __only__ in the top-level module.
/// `.cfg` is not initialized and is used around.
pub fn initialize(&mut self) {
let mut mod_cache = mem::take(&mut self.mod_cache);
if let Some(mod_cache) = &mut mod_cache {
mod_cache.initialize();
}
let mut py_mod_cache = mem::take(&mut self.py_mod_cache);
if let Some(py_mod_cache) = &mut py_mod_cache {
py_mod_cache.initialize();
}
*self = Self::new(
self.name.clone(),
self.cfg.clone(),
self.kind.clone(),
vec![],
None,
mod_cache,
py_mod_cache,
self.level,
);
}
pub(crate) fn grow( pub(crate) fn grow(
&mut self, &mut self,
name: &str, name: &str,

View file

@ -84,6 +84,12 @@ impl Runnable for ASTLowerer {
#[inline] #[inline]
fn finish(&mut self) {} fn finish(&mut self) {}
fn initialize(&mut self) {
self.ctx.initialize();
self.errs.clear();
self.warns.clear();
}
fn clear(&mut self) { fn clear(&mut self) {
self.errs.clear(); self.errs.clear();
self.warns.clear(); self.warns.clear();

View file

@ -201,4 +201,16 @@ impl SharedModuleCache {
pub fn get_similar_name(&self, name: &str) -> Option<Str> { pub fn get_similar_name(&self, name: &str) -> Option<Str> {
self.0.borrow().get_similar_name(name) self.0.borrow().get_similar_name(name)
} }
pub fn initialize(&mut self) {
let builtin_path = PathBuf::from("<builtins>");
let builtin = self.remove(&builtin_path).unwrap();
for path in self.0.borrow().cache.keys() {
self.remove(path);
}
/*for path in self.0.borrow().cache.keys().cloned() {
self.remove(&path);
}*/
self.register(builtin_path, None, Rc::try_unwrap(builtin.ctx).unwrap());
}
} }

View file

@ -65,8 +65,14 @@ impl Runnable for Transpiler {
#[inline] #[inline]
fn finish(&mut self) {} fn finish(&mut self) {}
fn initialize(&mut self) {
self.builder.initialize();
// mod_cache will be cleared by the builder
// self.mod_cache.initialize();
}
fn clear(&mut self) { fn clear(&mut self) {
// self.builder.clear(); self.builder.clear();
} }
fn exec(&mut self) -> Result<i32, Self::Errs> { fn exec(&mut self) -> Result<i32, Self::Errs> {

View file

@ -33,6 +33,9 @@ impl Runnable for ASTBuilder {
#[inline] #[inline]
fn finish(&mut self) {} fn finish(&mut self) {}
#[inline]
fn initialize(&mut self) {}
#[inline] #[inline]
fn clear(&mut self) {} fn clear(&mut self) {}

View file

@ -35,6 +35,9 @@ impl Runnable for LexerRunner {
#[inline] #[inline]
fn finish(&mut self) {} fn finish(&mut self) {}
#[inline]
fn initialize(&mut self) {}
#[inline] #[inline]
fn clear(&mut self) {} fn clear(&mut self) {}

View file

@ -187,6 +187,9 @@ impl Runnable for ParserRunner {
#[inline] #[inline]
fn finish(&mut self) {} fn finish(&mut self) {}
#[inline]
fn initialize(&mut self) {}
#[inline] #[inline]
fn clear(&mut self) {} fn clear(&mut self) {}

View file

@ -100,6 +100,10 @@ impl Runnable for DummyVM {
} }
} }
fn initialize(&mut self) {
self.compiler.initialize();
}
fn clear(&mut self) { fn clear(&mut self) {
self.compiler.clear(); self.compiler.clear();
} }