diff --git a/crates/erg_compiler/context/initialize/mod.rs b/crates/erg_compiler/context/initialize/mod.rs index 0a06aa8fc..b20cb3176 100644 --- a/crates/erg_compiler/context/initialize/mod.rs +++ b/crates/erg_compiler/context/initialize/mod.rs @@ -33,7 +33,7 @@ use crate::module::SharedCompilerResource; use crate::ty::constructors::*; use crate::ty::free::Constraint; use crate::ty::value::ValueObj; -use crate::ty::{BuiltinConstSubr, ConstSubr, ParamTy, Predicate, Type, Visibility}; +use crate::ty::{BuiltinConstSubr, ConstSubr, ParamTy, Predicate, TyParam, Type, Visibility}; use crate::varinfo::{AbsLocation, Mutability, VarInfo, VarKind}; use Mutability::*; use ParamSpec as PS; @@ -198,6 +198,8 @@ const FUNC_CO_LNOTAB: &str = "co_lnotab"; const FUNC_CO_NLOCALS: &str = "co_nlocals"; const FUNC_CO_KWONLYARGCOUNT: &str = "co_kwonlyargcount"; const FUNC_CO_POSONLYARGCOUNT: &str = "co_posonlyargcount"; +const FUNC_MODULE: &str = "module"; +const FUNC_GLOBAL: &str = "global"; const GENERIC_MODULE: &str = "GenericModule"; const PATH: &str = "Path"; const MODULE: &str = "Module"; @@ -927,14 +929,6 @@ impl Context { } else { Visibility::BUILTIN_PRIVATE }; - // TODO: this is not a const, but a special property - self.register_builtin_py_impl( - FUNDAMENTAL_NAME, - Str, - Immutable, - vis.clone(), - Some(FUNDAMENTAL_NAME), - ); self.register_builtin_py_impl( LICENSE, mono(SITEBUILTINS_PRINTER), @@ -963,7 +957,7 @@ impl Context { vis.clone(), Some(NOT_IMPLEMENTED), ); - self.register_builtin_py_impl(ELLIPSIS, Ellipsis, Const, vis, Some(ELLIPSIS)); + self.register_builtin_py_impl(ELLIPSIS, Ellipsis, Const, vis.clone(), Some(ELLIPSIS)); self.register_builtin_py_impl(TRUE, Bool, Const, Visibility::BUILTIN_PRIVATE, Some(TRUE)); self.register_builtin_py_impl(FALSE, Bool, Const, Visibility::BUILTIN_PRIVATE, Some(FALSE)); self.register_builtin_py_impl( @@ -973,6 +967,39 @@ impl Context { Visibility::BUILTIN_PRIVATE, Some(NONE), ); + if ERG_MODE { + self.register_builtin_py_impl( + FUNC_GLOBAL, + module(TyParam::value("")), + Immutable, + vis, + None, + ); + } + } + + fn init_module_consts(&mut self) { + let vis = if PYTHON_MODE { + Visibility::BUILTIN_PUBLIC + } else { + Visibility::BUILTIN_PRIVATE + }; + self.register_builtin_py_impl( + FUNDAMENTAL_NAME, + Str, + Immutable, + vis.clone(), + Some(FUNDAMENTAL_NAME), + ); + if ERG_MODE { + self.register_builtin_py_impl( + FUNC_MODULE, + module(TyParam::value(self.get_module().unwrap().name.clone())), + Immutable, + vis, + None, + ); + } } pub(crate) fn init_builtins(cfg: ErgConfig, shared: SharedCompilerResource) { @@ -1001,7 +1028,7 @@ impl Context { cfg: ErgConfig, shared: SharedCompilerResource, ) -> Self { - Context::new( + let mut ctx = Context::new( name.into(), cfg, ContextKind::Module, @@ -1009,6 +1036,8 @@ impl Context { None, Some(shared), Context::TOP_LEVEL, - ) + ); + ctx.init_module_consts(); + ctx } } diff --git a/crates/erg_compiler/link_hir.rs b/crates/erg_compiler/link_hir.rs index 8d817b884..2937d968a 100644 --- a/crates/erg_compiler/link_hir.rs +++ b/crates/erg_compiler/link_hir.rs @@ -176,7 +176,15 @@ impl<'a> HIRLinker<'a> { Accessor::Attr(attr) => { self.replace_import(&mut attr.obj); } - Accessor::Ident(_) => {} + Accessor::Ident(ident) => match &ident.inspect()[..] { + "module" => { + *expr = Self::self_module(); + } + "global" => { + *expr = Expr::from(Identifier::public("__builtins__")); + } + _ => {} + }, } } Expr::Array(array) => match array { @@ -286,6 +294,12 @@ impl<'a> HIRLinker<'a> { } } + fn self_module() -> Expr { + let __import__ = Identifier::public("__import__"); + let __name__ = Identifier::public("__name__"); + Expr::from(__import__).call1(Expr::from(__name__)) + } + /// ```erg /// x = import "mod" /// ``` @@ -311,10 +325,7 @@ impl<'a> HIRLinker<'a> { // self = __import__(__name__) if matches!((path.canonicalize(), self.cfg.input.unescaped_path().canonicalize()), (Ok(l), Ok(r)) if l == r) { - let __import__ = Identifier::public("__import__"); - let __name__ = Identifier::public("__name__"); - let call = Expr::from(__import__).call1(Expr::from(__name__)); - *expr = call; + *expr = Self::self_module(); return; } // In the case of REPL, entries cannot be used up