mirror of
https://github.com/erg-lang/erg.git
synced 2025-09-29 12:24:45 +00:00
Add Runnable::initialize
This commit is contained in:
parent
64dcc17399
commit
7bcc766500
11 changed files with 80 additions and 2 deletions
|
@ -357,6 +357,9 @@ pub trait Runnable: Sized + Default {
|
|||
fn new(cfg: ErgConfig) -> Self;
|
||||
fn cfg(&self) -> &ErgConfig;
|
||||
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 eval(&mut self, src: String) -> Result<String, Self::Errs>;
|
||||
fn exec(&mut self) -> Result<i32, Self::Errs>;
|
||||
|
|
|
@ -45,7 +45,15 @@ impl Runnable for HIRBuilder {
|
|||
#[inline]
|
||||
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> {
|
||||
let mut builder = ASTBuilder::new(self.cfg().copy());
|
||||
|
|
|
@ -129,7 +129,14 @@ impl Runnable for Compiler {
|
|||
#[inline]
|
||||
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) {
|
||||
self.builder.clear();
|
||||
self.code_generator.clear();
|
||||
}
|
||||
|
||||
|
|
|
@ -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(
|
||||
&mut self,
|
||||
name: &str,
|
||||
|
|
|
@ -84,6 +84,12 @@ impl Runnable for ASTLowerer {
|
|||
#[inline]
|
||||
fn finish(&mut self) {}
|
||||
|
||||
fn initialize(&mut self) {
|
||||
self.ctx.initialize();
|
||||
self.errs.clear();
|
||||
self.warns.clear();
|
||||
}
|
||||
|
||||
fn clear(&mut self) {
|
||||
self.errs.clear();
|
||||
self.warns.clear();
|
||||
|
|
|
@ -201,4 +201,16 @@ impl SharedModuleCache {
|
|||
pub fn get_similar_name(&self, name: &str) -> Option<Str> {
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -65,8 +65,14 @@ impl Runnable for Transpiler {
|
|||
#[inline]
|
||||
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) {
|
||||
// self.builder.clear();
|
||||
self.builder.clear();
|
||||
}
|
||||
|
||||
fn exec(&mut self) -> Result<i32, Self::Errs> {
|
||||
|
|
|
@ -33,6 +33,9 @@ impl Runnable for ASTBuilder {
|
|||
#[inline]
|
||||
fn finish(&mut self) {}
|
||||
|
||||
#[inline]
|
||||
fn initialize(&mut self) {}
|
||||
|
||||
#[inline]
|
||||
fn clear(&mut self) {}
|
||||
|
||||
|
|
|
@ -35,6 +35,9 @@ impl Runnable for LexerRunner {
|
|||
#[inline]
|
||||
fn finish(&mut self) {}
|
||||
|
||||
#[inline]
|
||||
fn initialize(&mut self) {}
|
||||
|
||||
#[inline]
|
||||
fn clear(&mut self) {}
|
||||
|
||||
|
|
|
@ -187,6 +187,9 @@ impl Runnable for ParserRunner {
|
|||
#[inline]
|
||||
fn finish(&mut self) {}
|
||||
|
||||
#[inline]
|
||||
fn initialize(&mut self) {}
|
||||
|
||||
#[inline]
|
||||
fn clear(&mut self) {}
|
||||
|
||||
|
|
|
@ -100,6 +100,10 @@ impl Runnable for DummyVM {
|
|||
}
|
||||
}
|
||||
|
||||
fn initialize(&mut self) {
|
||||
self.compiler.initialize();
|
||||
}
|
||||
|
||||
fn clear(&mut self) {
|
||||
self.compiler.clear();
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue