Skip to content

Commit

Permalink
chore: add some helper methods
Browse files Browse the repository at this point in the history
  • Loading branch information
mtshiba committed Jul 6, 2024
1 parent 2cf0e67 commit 1e23c6b
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 11 deletions.
6 changes: 6 additions & 0 deletions crates/erg_common/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ impl fmt::Display for ErgMode {
}
}

impl ErgMode {
pub const fn is_language_server(&self) -> bool {
matches!(self, Self::LanguageServer)
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum TranspileTarget {
Python,
Expand Down
7 changes: 6 additions & 1 deletion crates/erg_common/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::process;
use std::process::Stdio;

use crate::config::ErgConfig;
use crate::consts::EXPERIMENTAL_MODE;
use crate::consts::{EXPERIMENTAL_MODE, PYTHON_MODE};
use crate::env::{erg_path, erg_pkgs_path, erg_pystd_path, erg_std_path, python_site_packages};
use crate::pathutil::{add_postfix_foreach, remove_postfix};
use crate::python_util::get_sys_path;
Expand Down Expand Up @@ -625,6 +625,11 @@ impl Input {
return Some(path);
}
}
if PYTHON_MODE {
if let Ok(path) = self.resolve_py(path) {
return Some(path);
}
}
None
}

Expand Down
4 changes: 4 additions & 0 deletions crates/erg_compiler/build_package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,10 @@ impl<ASTBuilder: ASTBuildable, HIRBuilder: Buildable>
}
}

pub fn shared(&self) -> &SharedCompilerResource {
&self.shared
}

pub fn finalize(&mut self) {
self.cyclic.clear();
self.inlines.clear();
Expand Down
24 changes: 14 additions & 10 deletions crates/erg_compiler/context/inquire.rs
Original file line number Diff line number Diff line change
Expand Up @@ -622,19 +622,23 @@ impl Context {
}
_ => {}
}
} else if let Some((name, _vi)) = self
} else if let Some((name, vi)) = self
.future_defined_locals
.get_key_value(&ident.inspect()[..])
{
return Triple::Err(TyCheckError::access_before_def_error(
input.clone(),
line!() as usize,
ident.loc(),
namespace.name.to_string(),
ident.inspect(),
name.ln_begin().unwrap_or(0),
self.get_similar_name(ident.inspect()),
));
if name.loc().is_real() && ident.loc() != name.loc() {
return Triple::Err(TyCheckError::access_before_def_error(
input.clone(),
line!() as usize,
ident.loc(),
namespace.name.to_string(),
ident.inspect(),
name.ln_begin().unwrap_or(100),
self.get_similar_name(ident.inspect()),
));
} else if ident.loc() == name.loc() {
return Triple::Ok(vi.clone());
}
} else if let Some((name, _vi)) = self.deleted_locals.get_key_value(&ident.inspect()[..]) {
return Triple::Err(TyCheckError::access_deleted_var_error(
input.clone(),
Expand Down
8 changes: 8 additions & 0 deletions crates/erg_compiler/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2561,6 +2561,14 @@ impl ClassDef {
}
joined
}

pub fn get_all_methods(methods_list: &[Methods]) -> Vec<&Expr> {
let mut joined = vec![];
for methods in methods_list {
joined.extend(methods.defs.iter());
}
joined
}
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
Expand Down
20 changes: 20 additions & 0 deletions crates/erg_compiler/module/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,15 @@ impl fmt::Display for ModuleCache {
}
}

impl IntoIterator for ModuleCache {
type Item = (NormalizedPathBuf, ModuleEntry);
type IntoIter = std::collections::hash_map::IntoIter<NormalizedPathBuf, ModuleEntry>;

fn into_iter(self) -> Self::IntoIter {
self.cache.into_iter()
}
}

impl ModuleCache {
pub fn new() -> Self {
Self {
Expand Down Expand Up @@ -216,6 +225,13 @@ impl ModuleCache {
pub fn clear(&mut self) {
self.cache.clear();
}

pub fn take(&mut self) -> Self {
Self {
cache: std::mem::take(&mut self.cache),
last_id: self.last_id,
}
}
}

#[derive(Debug, Clone, Default)]
Expand Down Expand Up @@ -402,6 +418,10 @@ impl SharedModuleCache {
let ref_ = unsafe { self.0.as_ptr().as_ref().unwrap() };
ref_.iter()
}

pub fn take(&self) -> ModuleCache {
self.0.borrow_mut().take()
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
Expand Down

0 comments on commit 1e23c6b

Please sign in to comment.