From e4a3bc172087c1a0ff2c1392a5ddbd9efbaaffd3 Mon Sep 17 00:00:00 2001 From: Shunsuke Shibayama Date: Tue, 7 Nov 2023 12:59:33 +0900 Subject: [PATCH] feat: add `rsimport` --- Cargo.toml | 1 + crates/erg_common/Cargo.toml | 1 + crates/erg_common/consts.rs | 1 + crates/erg_compiler/Cargo.toml | 1 + crates/erg_compiler/context/initialize/funcs.rs | 11 ++++++++++- crates/erg_compiler/context/initialize/mod.rs | 1 + crates/erg_compiler/context/mod.rs | 2 +- crates/erg_compiler/hir.rs | 2 ++ crates/erg_parser/ast.rs | 12 ++++++++++-- 9 files changed, 28 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e503d7027..b54e4b1d7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -59,6 +59,7 @@ large_thread = [ "els/large_thread", ] py_compat = ["erg_compiler/py_compat", "els/py_compat"] +gal = ["erg_common/gal", "erg_compiler/gal"] els = ["erg_common/els", "erg_compiler/els", "dep:els"] full-repl = ["erg_common/full-repl"] full = ["els", "full-repl", "unicode", "pretty"] diff --git a/crates/erg_common/Cargo.toml b/crates/erg_common/Cargo.toml index 0b841c7eb..01505b99f 100644 --- a/crates/erg_common/Cargo.toml +++ b/crates/erg_common/Cargo.toml @@ -20,6 +20,7 @@ pretty = [] large_thread = [] els = [] py_compat = [] +gal = [] no_std = [] full-repl = ["dep:crossterm"] experimental = [] diff --git a/crates/erg_common/consts.rs b/crates/erg_common/consts.rs index 0b0731ba0..d19905ac5 100644 --- a/crates/erg_common/consts.rs +++ b/crates/erg_common/consts.rs @@ -9,3 +9,4 @@ pub const ELS: bool = cfg!(feature = "els"); pub const DEBUG_MODE: bool = cfg!(feature = "debug"); pub const EXPERIMENTAL_MODE: bool = cfg!(feature = "experimental"); pub const BACKTRACE_MODE: bool = cfg!(feature = "backtrace"); +pub const GAL: bool = cfg!(feature = "gal"); diff --git a/crates/erg_compiler/Cargo.toml b/crates/erg_compiler/Cargo.toml index 6d6f80629..34a73905f 100644 --- a/crates/erg_compiler/Cargo.toml +++ b/crates/erg_compiler/Cargo.toml @@ -35,6 +35,7 @@ large_thread = [ "erg_parser/large_thread", ] py_compat = ["erg_common/py_compat"] +gal = ["erg_common/gal"] els = ["erg_common/els"] no_std = ["erg_common/no_std"] full-repl = ["erg_common/full-repl"] diff --git a/crates/erg_compiler/context/initialize/funcs.rs b/crates/erg_compiler/context/initialize/funcs.rs index ee9c7138b..a6d868645 100644 --- a/crates/erg_compiler/context/initialize/funcs.rs +++ b/crates/erg_compiler/context/initialize/funcs.rs @@ -1,4 +1,4 @@ -use erg_common::consts::{DEBUG_MODE, ERG_MODE, PYTHON_MODE}; +use erg_common::consts::{DEBUG_MODE, ERG_MODE, GAL, PYTHON_MODE}; #[allow(unused_imports)] use erg_common::log; @@ -515,6 +515,15 @@ impl Context { Some(FUNDAMENTAL_IMPORT), ); } + if GAL { + self.register_builtin_py_impl( + RSIMPORT, + t_pyimport.clone(), + Immutable, + vis.clone(), + Some(FUNDAMENTAL_IMPORT), + ); + } if ERG_MODE { self.register_builtin_py_impl(FUNC_IF, t_if, Immutable, vis.clone(), Some(FUNC_IF__)); self.register_builtin_py_impl( diff --git a/crates/erg_compiler/context/initialize/mod.rs b/crates/erg_compiler/context/initialize/mod.rs index 2e1b21f97..4e4b0f1ae 100644 --- a/crates/erg_compiler/context/initialize/mod.rs +++ b/crates/erg_compiler/context/initialize/mod.rs @@ -481,6 +481,7 @@ const NOT_IMPLEMENTED: &str = "NotImplemented"; const ELLIPSIS: &str = "Ellipsis"; const SITEBUILTINS_PRINTER: &str = "_sitebuiltins._Printer"; const PY: &str = "py"; +const RSIMPORT: &str = "rsimport"; const PYIMPORT: &str = "pyimport"; const PYCOMPILE: &str = "pycompile"; const F_BUILTINS: &str = "f_builtins"; diff --git a/crates/erg_compiler/context/mod.rs b/crates/erg_compiler/context/mod.rs index 64921168c..6197444bf 100644 --- a/crates/erg_compiler/context/mod.rs +++ b/crates/erg_compiler/context/mod.rs @@ -407,7 +407,7 @@ impl From<&Def> for ContextKind { DefKind::Class | DefKind::Inherit => Self::Class, DefKind::Trait | DefKind::Subsume => Self::Trait, DefKind::StructuralTrait => Self::StructuralTrait, - DefKind::ErgImport | DefKind::PyImport => Self::Module, + DefKind::ErgImport | DefKind::PyImport | DefKind::RsImport => Self::Module, DefKind::Other => { if def.is_subr() { if def.sig.ident().unwrap().is_procedural() { diff --git a/crates/erg_compiler/hir.rs b/crates/erg_compiler/hir.rs index 481fb330f..3adc8ab42 100644 --- a/crates/erg_compiler/hir.rs +++ b/crates/erg_compiler/hir.rs @@ -1491,6 +1491,7 @@ impl Call { self.obj.show_acc().and_then(|s| match &s[..] { "import" => Some(OperationKind::Import), "pyimport" | "py" | "__import__" => Some(OperationKind::PyImport), + "rsimport" => Some(OperationKind::RsImport), "Del" => Some(OperationKind::Del), "assert" => Some(OperationKind::Assert), _ => { @@ -2274,6 +2275,7 @@ impl Def { Some("Patch") => DefKind::Patch, Some("import") => DefKind::ErgImport, Some("pyimport") | Some("__import__") => DefKind::PyImport, + Some("rsimport") => DefKind::RsImport, #[cfg(feature = "debug")] Some("py") => DefKind::PyImport, _ => DefKind::Other, diff --git a/crates/erg_parser/ast.rs b/crates/erg_parser/ast.rs index 7fa888486..ed06f9235 100644 --- a/crates/erg_parser/ast.rs +++ b/crates/erg_parser/ast.rs @@ -24,6 +24,7 @@ use crate::token::{Token, TokenKind, EQUAL}; pub enum OperationKind { Import, PyImport, + RsImport, Del, Assert, Class, @@ -43,7 +44,7 @@ impl OperationKind { matches!(self, Self::PyImport) } pub const fn is_import(&self) -> bool { - matches!(self, Self::Import | Self::PyImport) + matches!(self, Self::Import | Self::PyImport | Self::RsImport) } } @@ -1315,6 +1316,7 @@ impl Call { self.obj.get_name().and_then(|s| match &s[..] { "import" => Some(OperationKind::Import), "pyimport" | "py" | "__import__" => Some(OperationKind::PyImport), + "rsimport" => Some(OperationKind::RsImport), "Del" => Some(OperationKind::Del), "Class" => Some(OperationKind::Class), "Inherit" => Some(OperationKind::Inherit), @@ -4597,6 +4599,7 @@ pub enum DefKind { StructuralTrait, ErgImport, PyImport, + RsImport, Patch, /// type alias included Other, @@ -4623,8 +4626,12 @@ impl DefKind { matches!(self, Self::PyImport) } + pub const fn is_rs_import(&self) -> bool { + matches!(self, Self::RsImport) + } + pub const fn is_import(&self) -> bool { - self.is_erg_import() || self.is_py_import() + self.is_erg_import() || self.is_py_import() || self.is_rs_import() } pub const fn is_other(&self) -> bool { @@ -4671,6 +4678,7 @@ impl DefBody { Some("Patch") => DefKind::Patch, Some("import") => DefKind::ErgImport, Some("pyimport") | Some("py") | Some("__import__") => DefKind::PyImport, + Some("rsimport") => DefKind::RsImport, _ => DefKind::Other, }, _ => DefKind::Other,