Skip to content

Commit

Permalink
feat: add rsimport
Browse files Browse the repository at this point in the history
  • Loading branch information
mtshiba committed Nov 7, 2023
1 parent ebf803d commit e4a3bc1
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 4 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
1 change: 1 addition & 0 deletions crates/erg_common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pretty = []
large_thread = []
els = []
py_compat = []
gal = []
no_std = []
full-repl = ["dep:crossterm"]
experimental = []
Expand Down
1 change: 1 addition & 0 deletions crates/erg_common/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
1 change: 1 addition & 0 deletions crates/erg_compiler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
11 changes: 10 additions & 1 deletion crates/erg_compiler/context/initialize/funcs.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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(
Expand Down
1 change: 1 addition & 0 deletions crates/erg_compiler/context/initialize/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
2 changes: 1 addition & 1 deletion crates/erg_compiler/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 2 additions & 0 deletions crates/erg_compiler/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
_ => {
Expand Down Expand Up @@ -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,
Expand Down
12 changes: 10 additions & 2 deletions crates/erg_parser/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use crate::token::{Token, TokenKind, EQUAL};
pub enum OperationKind {
Import,
PyImport,
RsImport,
Del,
Assert,
Class,
Expand All @@ -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)
}
}

Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -4597,6 +4599,7 @@ pub enum DefKind {
StructuralTrait,
ErgImport,
PyImport,
RsImport,
Patch,
/// type alias included
Other,
Expand All @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit e4a3bc1

Please sign in to comment.