Skip to content

Commit

Permalink
Merge pull request #482 from erg-lang/torch_d
Browse files Browse the repository at this point in the history
enhance torch type decls
  • Loading branch information
mtshiba committed Feb 1, 2024
2 parents 20b1993 + 6018d0f commit e0f4bf5
Show file tree
Hide file tree
Showing 39 changed files with 528 additions and 149 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@
tqdm = pyimport "tqdm"

print! sin pi # 1.2246467991473532e-16
for! tqdm.Tqdm!(0..99), i =>
for! tqdm.tqdm(0..99), i =>
time.sleep! 0.01 * i
```

Expand Down
2 changes: 1 addition & 1 deletion README_JA.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@
tqdm = pyimport("tqdm")

print! sin pi # 1.2246467991473532e-16
for! tqdm.Tqdm!(0..99), i =>
for! tqdm.tqdm(0..99), i =>
time.sleep! 0.01 * i
```

Expand Down
2 changes: 1 addition & 1 deletion README_zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@
tqdm = pyimport "tqdm"

print! sin pi # 1.2246467991473532e-16
for! tqdm.Tqdm!(0..99), i =>
for! tqdm.tqdm(0..99), i =>
time.sleep! 0.01 * i
```

Expand Down
2 changes: 1 addition & 1 deletion README_zh-TW.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@
tqdm = pyimport "tqdm"

print! sin pi # 1.2246467991473532e-16
for! tqdm.Tqdm!(0..99), i =>
for! tqdm.tqdm(0..99), i =>
time.sleep! 0.01 * i
```

Expand Down
22 changes: 10 additions & 12 deletions crates/els/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ use std::time::Duration;

use erg_common::consts::PYTHON_MODE;
use erg_common::dict::Dict;
use erg_common::pathutil::{project_entry_file_of, project_root_dir_of};
use erg_common::spawn::{safe_yield, spawn_new_thread};
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;
Expand All @@ -30,7 +32,7 @@ use crate::channels::WorkerMessage;
use crate::diff::{ASTDiff, HIRDiff};
use crate::server::{DefaultFeatures, ELSResult, RedirectableStdout, Server};
use crate::server::{ASK_AUTO_SAVE_ID, HEALTH_CHECKER_ID};
use crate::util::{self, project_root_of, NormalizedUrl};
use crate::util::{self, NormalizedUrl};

#[derive(Debug)]
pub enum BuildASTError {
Expand Down Expand Up @@ -114,7 +116,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,
Expand All @@ -132,7 +134,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());
Expand All @@ -154,19 +156,19 @@ 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();
let ctx = checker.pop_context().unwrap();
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 {
Expand Down Expand Up @@ -407,19 +409,15 @@ impl<Checker: BuildRunnable, Parser: Parsable> Server<Checker, Parser> {
"method": "window/workDoneProgress/create",
"params": progress_token,
}));
let Some(project_root) = project_root_of(&current_dir().unwrap()) else {
let Some(project_root) = project_root_dir_of(&current_dir().unwrap()) else {
work_done!(token);
};
let src_dir = if project_root.join("src").is_dir() {
project_root.join("src")
} else {
project_root
};
let main_path = if src_dir.join("main.er").exists() {
src_dir.join("main.er")
} else if src_dir.join("lib.er").exists() {
src_dir.join("lib.er")
} else {
let Some(main_path) = project_entry_file_of(&current_dir().unwrap()) else {
work_done!(token);
};
let Ok(main_uri) = NormalizedUrl::from_file_path(main_path) else {
Expand Down
13 changes: 8 additions & 5 deletions crates/els/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use erg_common::config::ErgConfig;
use erg_common::consts::PYTHON_MODE;
use erg_common::dict::Dict;
use erg_common::env::erg_path;
use erg_common::pathutil::NormalizedPathBuf;
use erg_common::pathutil::{project_root_dir_of, NormalizedPathBuf};
use erg_common::shared::{MappedRwLockReadGuard, Shared};
use erg_common::spawn::{safe_yield, spawn_new_thread};
use erg_common::traits::Stream;
Expand Down Expand Up @@ -61,7 +61,7 @@ use crate::file_cache::FileCache;
use crate::hir_visitor::{ExprKind, HIRVisitor};
use crate::message::{ErrorMessage, LSPResult};
use crate::scheduler::Scheduler;
use crate::util::{self, loc_to_pos, project_root_of, NormalizedUrl};
use crate::util::{self, loc_to_pos, NormalizedUrl};

pub const HEALTH_CHECKER_ID: i64 = 10000;
pub const ASK_AUTO_SAVE_ID: i64 = 10001;
Expand Down Expand Up @@ -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(
Expand All @@ -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);
}

Expand Down Expand Up @@ -949,7 +952,7 @@ impl<Checker: BuildRunnable, Parser: Parsable> Server<Checker, Parser> {
}

pub(crate) fn get_workspace_ctxs(&self) -> Vec<&Context> {
let project_root = project_root_of(&self.home).unwrap_or(self.home.clone());
let project_root = project_root_dir_of(&self.home).unwrap_or(self.home.clone());
let mut ctxs = vec![];
for (path, ent) in self.shared.raw_path_and_modules() {
if path.starts_with(&project_root) {
Expand Down
14 changes: 0 additions & 14 deletions crates/els/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,17 +183,3 @@ pub(crate) fn abs_loc_to_lsp_loc(loc: &AbsLocation) -> Option<lsp_types::Locatio
let range = loc_to_range(loc.loc)?;
Some(lsp_types::Location::new(uri, range))
}

pub(crate) fn project_root_of(path: &Path) -> Option<PathBuf> {
if path.is_dir() && path.join("package.er").exists() {
return Some(path.to_path_buf());
}
let mut path = path.to_path_buf();
while let Some(parent) = path.parent() {
if parent.join("package.er").exists() {
return Some(parent.to_path_buf());
}
path = parent.to_path_buf();
}
None
}
29 changes: 28 additions & 1 deletion crates/erg_common/pathutil.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{normalize_path, Str};
/// `PathBuf` may give false equivalence decisions in non-case-sensitive file systems.
/// Use this for dictionary keys, etc.
/// See also: `els::util::NormalizedUrl`
#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)]
#[derive(Debug, Clone, PartialEq, Eq, Hash, Default, PartialOrd, Ord)]
pub struct NormalizedPathBuf(PathBuf);

impl fmt::Display for NormalizedPathBuf {
Expand Down Expand Up @@ -254,3 +254,30 @@ pub fn mod_name(path: &Path) -> Str {
}
Str::from(name)
}

pub fn project_root_dir_of(path: &Path) -> Option<PathBuf> {
if path.is_dir() && path.join("package.er").exists() {
return Some(path.to_path_buf());
}
let mut path = path.to_path_buf();
while let Some(parent) = path.parent() {
if parent.join("package.er").exists() {
return Some(parent.to_path_buf());
}
path = parent.to_path_buf();
}
None
}

pub fn project_entry_file_of(path: &Path) -> Option<PathBuf> {
let project_root = project_root_dir_of(path)?;
if project_root.join("src/lib.er").exists() {
Some(project_root.join("src/lib.er"))
} else if project_root.join("src/main.er").exists() {
Some(project_root.join("src/main.er"))
} else if project_root.join("src/lib.d.er").exists() {
Some(project_root.join("src/lib.d.er"))
} else {
None
}
}
7 changes: 7 additions & 0 deletions crates/erg_common/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ impl PartialEq<str> for Str {
}
}

impl PartialEq<&str> for Str {
#[inline]
fn eq(&self, other: &&str) -> bool {
self[..] == other[..]
}
}

impl PartialEq<String> for Str {
#[inline]
fn eq(&self, other: &String) -> bool {
Expand Down
4 changes: 4 additions & 0 deletions crates/erg_common/tsort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ impl<T: Eq + Hash, U> Node<T, U> {
self.depends_on.insert(dep);
}

pub fn pop_dep(&mut self, dep: &T) -> bool {
self.depends_on.remove(dep)
}

pub fn depends_on(&self, dep: &T) -> bool {
self.depends_on.contains(dep)
}
Expand Down
Loading

0 comments on commit e0f4bf5

Please sign in to comment.