diff --git a/compiler/erg_common/traits.rs b/compiler/erg_common/traits.rs index 10017ba6..282adfdb 100644 --- a/compiler/erg_common/traits.rs +++ b/compiler/erg_common/traits.rs @@ -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; fn exec(&mut self) -> Result; diff --git a/compiler/erg_compiler/build_hir.rs b/compiler/erg_compiler/build_hir.rs index 921fd81e..0434f100 100644 --- a/compiler/erg_compiler/build_hir.rs +++ b/compiler/erg_compiler/build_hir.rs @@ -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 { let mut builder = ASTBuilder::new(self.cfg().copy()); diff --git a/compiler/erg_compiler/compile.rs b/compiler/erg_compiler/compile.rs index acc527b9..f7a74809 100644 --- a/compiler/erg_compiler/compile.rs +++ b/compiler/erg_compiler/compile.rs @@ -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(); } diff --git a/compiler/erg_compiler/context/mod.rs b/compiler/erg_compiler/context/mod.rs index 90957cdf..a4d5a7f5 100644 --- a/compiler/erg_compiler/context/mod.rs +++ b/compiler/erg_compiler/context/mod.rs @@ -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, diff --git a/compiler/erg_compiler/lower.rs b/compiler/erg_compiler/lower.rs index 3827169b..281c1be2 100644 --- a/compiler/erg_compiler/lower.rs +++ b/compiler/erg_compiler/lower.rs @@ -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(); diff --git a/compiler/erg_compiler/mod_cache.rs b/compiler/erg_compiler/mod_cache.rs index 24c3bb4f..6808855b 100644 --- a/compiler/erg_compiler/mod_cache.rs +++ b/compiler/erg_compiler/mod_cache.rs @@ -201,4 +201,16 @@ impl SharedModuleCache { pub fn get_similar_name(&self, name: &str) -> Option { self.0.borrow().get_similar_name(name) } + + pub fn initialize(&mut self) { + let builtin_path = PathBuf::from(""); + 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()); + } } diff --git a/compiler/erg_compiler/transpile.rs b/compiler/erg_compiler/transpile.rs index af58354f..cc5bd0c9 100644 --- a/compiler/erg_compiler/transpile.rs +++ b/compiler/erg_compiler/transpile.rs @@ -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 { diff --git a/compiler/erg_parser/build_ast.rs b/compiler/erg_parser/build_ast.rs index d2a33050..12a6e5e2 100644 --- a/compiler/erg_parser/build_ast.rs +++ b/compiler/erg_parser/build_ast.rs @@ -33,6 +33,9 @@ impl Runnable for ASTBuilder { #[inline] fn finish(&mut self) {} + #[inline] + fn initialize(&mut self) {} + #[inline] fn clear(&mut self) {} diff --git a/compiler/erg_parser/lex.rs b/compiler/erg_parser/lex.rs index c38abd99..33b5e3a1 100644 --- a/compiler/erg_parser/lex.rs +++ b/compiler/erg_parser/lex.rs @@ -35,6 +35,9 @@ impl Runnable for LexerRunner { #[inline] fn finish(&mut self) {} + #[inline] + fn initialize(&mut self) {} + #[inline] fn clear(&mut self) {} diff --git a/compiler/erg_parser/parse.rs b/compiler/erg_parser/parse.rs index bf8bd106..842192b1 100644 --- a/compiler/erg_parser/parse.rs +++ b/compiler/erg_parser/parse.rs @@ -187,6 +187,9 @@ impl Runnable for ParserRunner { #[inline] fn finish(&mut self) {} + #[inline] + fn initialize(&mut self) {} + #[inline] fn clear(&mut self) {} diff --git a/src/dummy.rs b/src/dummy.rs index b132da3f..b6613a18 100644 --- a/src/dummy.rs +++ b/src/dummy.rs @@ -100,6 +100,10 @@ impl Runnable for DummyVM { } } + fn initialize(&mut self) { + self.compiler.initialize(); + } + fn clear(&mut self) { self.compiler.clear(); }