mirror of
https://github.com/erg-lang/erg.git
synced 2025-08-04 18:58:30 +00:00
Update lower.rs
This commit is contained in:
parent
37a9b024be
commit
c75dc9c77a
7 changed files with 96 additions and 25 deletions
|
@ -13,6 +13,7 @@ use erg_common::style::*;
|
|||
use erg_common::traits::Stream;
|
||||
use erg_common::{fn_name, lsp_log};
|
||||
use erg_compiler::artifact::BuildRunnable;
|
||||
use erg_compiler::build_package::CheckStatus;
|
||||
use erg_compiler::erg_parser::ast::Module;
|
||||
use erg_compiler::erg_parser::error::IncompleteArtifact;
|
||||
use erg_compiler::erg_parser::parse::Parsable;
|
||||
|
@ -114,7 +115,7 @@ impl<Checker: BuildRunnable, Parser: Parsable> Server<Checker, Parser> {
|
|||
"exec"
|
||||
};
|
||||
let mut checker = self.get_checker(path.clone());
|
||||
let artifact = match checker.build(code.into(), mode) {
|
||||
let (artifact, status) = match checker.build(code.into(), mode) {
|
||||
Ok(artifact) => {
|
||||
_log!(
|
||||
self,
|
||||
|
@ -132,7 +133,7 @@ impl<Checker: BuildRunnable, Parser: Parsable> Server<Checker, Parser> {
|
|||
_log!(self, "{uri}, warns: {}", diags.len());
|
||||
self.send_diagnostics(uri, diags)?;
|
||||
}
|
||||
artifact.into()
|
||||
(artifact.into(), CheckStatus::Succeed)
|
||||
}
|
||||
Err(artifact) => {
|
||||
_log!(self, "found errors: {}", artifact.errors.len());
|
||||
|
@ -154,7 +155,7 @@ impl<Checker: BuildRunnable, Parser: Parsable> Server<Checker, Parser> {
|
|||
_log!(self, "{uri}, errs & warns: {}", diags.len());
|
||||
self.send_diagnostics(uri, diags)?;
|
||||
}
|
||||
artifact
|
||||
(artifact, CheckStatus::Failed)
|
||||
}
|
||||
};
|
||||
let ast = self.build_ast(&uri).ok();
|
||||
|
@ -162,11 +163,11 @@ impl<Checker: BuildRunnable, Parser: Parsable> Server<Checker, Parser> {
|
|||
if mode == "declare" {
|
||||
self.shared
|
||||
.py_mod_cache
|
||||
.register(path, ast, artifact.object, ctx);
|
||||
.register(path, ast, artifact.object, ctx, status);
|
||||
} else {
|
||||
self.shared
|
||||
.mod_cache
|
||||
.register(path, ast, artifact.object, ctx);
|
||||
.register(path, ast, artifact.object, ctx, status);
|
||||
}
|
||||
let dependents = self.dependents_of(&uri);
|
||||
for dep in dependents {
|
||||
|
|
|
@ -881,7 +881,10 @@ impl<Checker: BuildRunnable, Parser: Parsable> Server<Checker, Parser> {
|
|||
let path = uri.to_file_path().ok()?;
|
||||
let module = self.shared.remove_module(&path)?;
|
||||
let lowerer = ASTLowerer::new_with_ctx(module.module);
|
||||
Some((lowerer, IRs::new(module.id, module.ast, module.hir)))
|
||||
Some((
|
||||
lowerer,
|
||||
IRs::new(module.id, module.ast, module.hir, module.status),
|
||||
))
|
||||
}
|
||||
|
||||
pub(crate) fn restore_lowerer(
|
||||
|
@ -891,7 +894,7 @@ impl<Checker: BuildRunnable, Parser: Parsable> Server<Checker, Parser> {
|
|||
irs: IRs,
|
||||
) {
|
||||
let module = lowerer.pop_mod_ctx().unwrap();
|
||||
let entry = ModuleEntry::new(irs.id, irs.ast, irs.hir, module);
|
||||
let entry = ModuleEntry::new(irs.id, irs.ast, irs.hir, module, irs.status);
|
||||
self.restore_entry(uri, entry);
|
||||
}
|
||||
|
||||
|
|
|
@ -71,6 +71,12 @@ impl std::str::FromStr for CheckStatus {
|
|||
}
|
||||
}
|
||||
|
||||
impl CheckStatus {
|
||||
pub const fn is_succeed(&self) -> bool {
|
||||
matches!(self, CheckStatus::Succeed)
|
||||
}
|
||||
}
|
||||
|
||||
/// format:
|
||||
/// ```python
|
||||
/// #[pylyzer] succeed foo.py 1234567890
|
||||
|
@ -724,6 +730,7 @@ impl<ASTBuilder: ASTBuildable, HIRBuilder: Buildable>
|
|||
raw_ast,
|
||||
Some(artifact.object),
|
||||
builder.pop_context().unwrap(),
|
||||
CheckStatus::Succeed,
|
||||
);
|
||||
shared.warns.extend(artifact.warns);
|
||||
}
|
||||
|
@ -733,6 +740,7 @@ impl<ASTBuilder: ASTBuildable, HIRBuilder: Buildable>
|
|||
raw_ast,
|
||||
artifact.object,
|
||||
builder.pop_context().unwrap(),
|
||||
CheckStatus::Failed,
|
||||
);
|
||||
shared.warns.extend(artifact.warns);
|
||||
shared.errors.extend(artifact.errors);
|
||||
|
@ -759,12 +767,18 @@ impl<ASTBuilder: ASTBuildable, HIRBuilder: Buildable>
|
|||
match builder.build_from_ast(ast, "declare") {
|
||||
Ok(artifact) => {
|
||||
let ctx = builder.pop_context().unwrap();
|
||||
py_mod_cache.register(path.clone(), raw_ast, Some(artifact.object), ctx);
|
||||
py_mod_cache.register(
|
||||
path.clone(),
|
||||
raw_ast,
|
||||
Some(artifact.object),
|
||||
ctx,
|
||||
CheckStatus::Succeed,
|
||||
);
|
||||
self.shared.warns.extend(artifact.warns);
|
||||
}
|
||||
Err(artifact) => {
|
||||
let ctx = builder.pop_context().unwrap();
|
||||
py_mod_cache.register(path, raw_ast, artifact.object, ctx);
|
||||
py_mod_cache.register(path, raw_ast, artifact.object, ctx, CheckStatus::Failed);
|
||||
self.shared.warns.extend(artifact.warns);
|
||||
self.shared.errors.extend(artifact.errors);
|
||||
}
|
||||
|
|
|
@ -24,6 +24,7 @@ use erg_common::{set, unique_in_place};
|
|||
|
||||
use erg_parser::ast::{DefId, VarName};
|
||||
|
||||
use crate::build_package::CheckStatus;
|
||||
use crate::context::initialize::const_func::*;
|
||||
use crate::context::instantiate_spec::ConstTemplate;
|
||||
use crate::context::{
|
||||
|
@ -1167,9 +1168,13 @@ impl Context {
|
|||
ctx.init_builtin_classes();
|
||||
ctx.init_builtin_patches();
|
||||
let module = ModuleContext::new(ctx, dict! {});
|
||||
shared
|
||||
.mod_cache
|
||||
.register(PathBuf::from("<builtins>"), None, None, module);
|
||||
shared.mod_cache.register(
|
||||
PathBuf::from("<builtins>"),
|
||||
None,
|
||||
None,
|
||||
module,
|
||||
CheckStatus::Succeed,
|
||||
);
|
||||
}
|
||||
|
||||
pub(crate) fn build_module_unsound(&self) {
|
||||
|
@ -1247,7 +1252,8 @@ impl Context {
|
|||
let module = Module::new(vec![Expr::Def(eval), Expr::Def(exec), Expr::Def(perform)]);
|
||||
let hir = HIR::new("unsound".into(), module);
|
||||
let ctx = ModuleContext::new(ctx, dict! {});
|
||||
self.mod_cache().register(path, None, Some(hir), ctx);
|
||||
self.mod_cache()
|
||||
.register(path, None, Some(hir), ctx, CheckStatus::Succeed);
|
||||
}
|
||||
|
||||
pub fn new_module<S: Into<Str>>(
|
||||
|
|
|
@ -26,6 +26,7 @@ use erg_parser::token::{Token, TokenKind};
|
|||
use erg_parser::Parser;
|
||||
|
||||
use crate::artifact::{BuildRunnable, Buildable, CompleteArtifact, IncompleteArtifact};
|
||||
use crate::build_package::CheckStatus;
|
||||
use crate::module::SharedCompilerResource;
|
||||
use crate::ty::constructors::{
|
||||
array_t, free_var, func, guard, mono, poly, proc, refinement, set_t, singleton, ty_tp,
|
||||
|
@ -2982,7 +2983,13 @@ impl<A: ASTBuildable> GenericASTLowerer<A> {
|
|||
) -> hir::Call {
|
||||
log!(info "entered {}", fn_name!());
|
||||
let path = NormalizedPathBuf::from(inline.input.path().to_path_buf());
|
||||
if self.module.context.mod_cached(&path) {
|
||||
if self
|
||||
.module
|
||||
.context
|
||||
.shared()
|
||||
.get_module(&path)
|
||||
.is_some_and(|ent| ent.is_complete())
|
||||
{
|
||||
return self.lower_call(inline.import, expect);
|
||||
}
|
||||
let parent = self.get_mod_ctx().context.get_module().unwrap().clone();
|
||||
|
@ -2996,15 +3003,15 @@ impl<A: ASTBuildable> GenericASTLowerer<A> {
|
|||
} else {
|
||||
"exec"
|
||||
};
|
||||
let hir = match builder.check(inline.ast, mode) {
|
||||
let (hir, status) = match builder.check(inline.ast, mode) {
|
||||
Ok(art) => {
|
||||
self.warns.extend(art.warns);
|
||||
Some(art.object)
|
||||
(Some(art.object), CheckStatus::Succeed)
|
||||
}
|
||||
Err(art) => {
|
||||
self.warns.extend(art.warns);
|
||||
self.errs.extend(art.errors);
|
||||
art.object
|
||||
(art.object, CheckStatus::Failed)
|
||||
}
|
||||
};
|
||||
let ctx = builder.pop_mod_ctx().unwrap();
|
||||
|
@ -3013,7 +3020,7 @@ impl<A: ASTBuildable> GenericASTLowerer<A> {
|
|||
} else {
|
||||
&self.module.context.shared().mod_cache
|
||||
};
|
||||
cache.register(path.to_path_buf(), None, hir, ctx);
|
||||
cache.register(path.to_path_buf(), None, hir, ctx, status);
|
||||
self.lower_call(inline.import, expect)
|
||||
}
|
||||
|
||||
|
|
|
@ -12,6 +12,7 @@ use erg_common::shared::{
|
|||
use erg_common::Str;
|
||||
use erg_parser::ast::Module;
|
||||
|
||||
use crate::build_package::CheckStatus;
|
||||
use crate::context::ModuleContext;
|
||||
use crate::hir::HIR;
|
||||
|
||||
|
@ -37,6 +38,7 @@ pub struct ModuleEntry {
|
|||
pub ast: Option<Module>,
|
||||
pub hir: Option<HIR>,
|
||||
pub module: ModuleContext,
|
||||
pub status: CheckStatus,
|
||||
}
|
||||
|
||||
impl fmt::Display for ModuleEntry {
|
||||
|
@ -50,12 +52,19 @@ impl fmt::Display for ModuleEntry {
|
|||
}
|
||||
|
||||
impl ModuleEntry {
|
||||
pub const fn new(id: ModId, ast: Option<Module>, hir: Option<HIR>, ctx: ModuleContext) -> Self {
|
||||
pub const fn new(
|
||||
id: ModId,
|
||||
ast: Option<Module>,
|
||||
hir: Option<HIR>,
|
||||
ctx: ModuleContext,
|
||||
status: CheckStatus,
|
||||
) -> Self {
|
||||
Self {
|
||||
id,
|
||||
ast,
|
||||
hir,
|
||||
module: ctx,
|
||||
status,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -65,6 +74,7 @@ impl ModuleEntry {
|
|||
ast: None,
|
||||
hir: None,
|
||||
module: ctx,
|
||||
status: CheckStatus::Succeed,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -73,7 +83,7 @@ impl ModuleEntry {
|
|||
}
|
||||
|
||||
pub const fn is_complete(&self) -> bool {
|
||||
self.ast.is_some() && self.hir.is_some()
|
||||
self.status.is_succeed() && self.ast.is_some() && self.hir.is_some()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -82,11 +92,22 @@ pub struct IRs {
|
|||
pub id: ModId,
|
||||
pub ast: Option<Module>,
|
||||
pub hir: Option<HIR>,
|
||||
pub status: CheckStatus,
|
||||
}
|
||||
|
||||
impl IRs {
|
||||
pub const fn new(id: ModId, ast: Option<Module>, hir: Option<HIR>) -> Self {
|
||||
Self { id, ast, hir }
|
||||
pub const fn new(
|
||||
id: ModId,
|
||||
ast: Option<Module>,
|
||||
hir: Option<HIR>,
|
||||
status: CheckStatus,
|
||||
) -> Self {
|
||||
Self {
|
||||
id,
|
||||
ast,
|
||||
hir,
|
||||
status,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -137,10 +158,11 @@ impl ModuleCache {
|
|||
ast: Option<Module>,
|
||||
hir: Option<HIR>,
|
||||
ctx: ModuleContext,
|
||||
status: CheckStatus,
|
||||
) {
|
||||
self.last_id += 1;
|
||||
let id = ModId::new(self.last_id);
|
||||
let entry = ModuleEntry::new(id, ast, hir, ctx);
|
||||
let entry = ModuleEntry::new(id, ast, hir, ctx, status);
|
||||
self.cache.insert(path, entry);
|
||||
}
|
||||
|
||||
|
@ -281,8 +303,11 @@ impl SharedModuleCache {
|
|||
ast: Option<Module>,
|
||||
hir: Option<HIR>,
|
||||
ctx: ModuleContext,
|
||||
status: CheckStatus,
|
||||
) {
|
||||
self.0.borrow_mut().register(path.into(), ast, hir, ctx);
|
||||
self.0
|
||||
.borrow_mut()
|
||||
.register(path.into(), ast, hir, ctx, status);
|
||||
}
|
||||
|
||||
pub fn insert(&self, path: NormalizedPathBuf, entry: ModuleEntry) {
|
||||
|
@ -322,7 +347,13 @@ impl SharedModuleCache {
|
|||
return;
|
||||
};
|
||||
self.0.borrow_mut().clear();
|
||||
self.register(builtin_path, None, None, builtin.module);
|
||||
self.register(
|
||||
builtin_path,
|
||||
None,
|
||||
None,
|
||||
builtin.module,
|
||||
CheckStatus::Succeed,
|
||||
);
|
||||
}
|
||||
|
||||
pub fn rename_path<P: Into<NormalizedPathBuf>>(&self, path: &NormalizedPathBuf, new: P) {
|
||||
|
|
|
@ -42,6 +42,15 @@ impl SharedCompileErrors {
|
|||
.retain(|e| &NormalizedPathBuf::from(e.input.path()) != path);
|
||||
}
|
||||
|
||||
pub fn get(&self, path: &NormalizedPathBuf) -> CompileErrors {
|
||||
self.0
|
||||
.borrow()
|
||||
.iter()
|
||||
.filter(|e| &NormalizedPathBuf::from(e.input.path()) == path)
|
||||
.cloned()
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn raw_iter(&self) -> impl Iterator<Item = &CompileError> {
|
||||
let _ref = self.0.borrow();
|
||||
let ref_ = unsafe { self.0.as_ptr().as_ref().unwrap() };
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue