Skip to content

Commit 4af0c4c

Browse files
committed
feat: add special variables module/global
1 parent 30eac6d commit 4af0c4c

File tree

2 files changed

+57
-17
lines changed

2 files changed

+57
-17
lines changed

crates/erg_compiler/context/initialize/mod.rs

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use crate::module::SharedCompilerResource;
3333
use crate::ty::constructors::*;
3434
use crate::ty::free::Constraint;
3535
use crate::ty::value::ValueObj;
36-
use crate::ty::{BuiltinConstSubr, ConstSubr, ParamTy, Predicate, Type, Visibility};
36+
use crate::ty::{BuiltinConstSubr, ConstSubr, ParamTy, Predicate, TyParam, Type, Visibility};
3737
use crate::varinfo::{AbsLocation, Mutability, VarInfo, VarKind};
3838
use Mutability::*;
3939
use ParamSpec as PS;
@@ -198,6 +198,8 @@ const FUNC_CO_LNOTAB: &str = "co_lnotab";
198198
const FUNC_CO_NLOCALS: &str = "co_nlocals";
199199
const FUNC_CO_KWONLYARGCOUNT: &str = "co_kwonlyargcount";
200200
const FUNC_CO_POSONLYARGCOUNT: &str = "co_posonlyargcount";
201+
const FUNC_MODULE: &str = "module";
202+
const FUNC_GLOBAL: &str = "global";
201203
const GENERIC_MODULE: &str = "GenericModule";
202204
const PATH: &str = "Path";
203205
const MODULE: &str = "Module";
@@ -927,14 +929,6 @@ impl Context {
927929
} else {
928930
Visibility::BUILTIN_PRIVATE
929931
};
930-
// TODO: this is not a const, but a special property
931-
self.register_builtin_py_impl(
932-
FUNDAMENTAL_NAME,
933-
Str,
934-
Immutable,
935-
vis.clone(),
936-
Some(FUNDAMENTAL_NAME),
937-
);
938932
self.register_builtin_py_impl(
939933
LICENSE,
940934
mono(SITEBUILTINS_PRINTER),
@@ -963,7 +957,7 @@ impl Context {
963957
vis.clone(),
964958
Some(NOT_IMPLEMENTED),
965959
);
966-
self.register_builtin_py_impl(ELLIPSIS, Ellipsis, Const, vis, Some(ELLIPSIS));
960+
self.register_builtin_py_impl(ELLIPSIS, Ellipsis, Const, vis.clone(), Some(ELLIPSIS));
967961
self.register_builtin_py_impl(TRUE, Bool, Const, Visibility::BUILTIN_PRIVATE, Some(TRUE));
968962
self.register_builtin_py_impl(FALSE, Bool, Const, Visibility::BUILTIN_PRIVATE, Some(FALSE));
969963
self.register_builtin_py_impl(
@@ -973,6 +967,39 @@ impl Context {
973967
Visibility::BUILTIN_PRIVATE,
974968
Some(NONE),
975969
);
970+
if ERG_MODE {
971+
self.register_builtin_py_impl(
972+
FUNC_GLOBAL,
973+
module(TyParam::value("<builtins>")),
974+
Immutable,
975+
vis,
976+
None,
977+
);
978+
}
979+
}
980+
981+
fn init_module_consts(&mut self) {
982+
let vis = if PYTHON_MODE {
983+
Visibility::BUILTIN_PUBLIC
984+
} else {
985+
Visibility::BUILTIN_PRIVATE
986+
};
987+
self.register_builtin_py_impl(
988+
FUNDAMENTAL_NAME,
989+
Str,
990+
Immutable,
991+
vis.clone(),
992+
Some(FUNDAMENTAL_NAME),
993+
);
994+
if ERG_MODE {
995+
self.register_builtin_py_impl(
996+
FUNC_MODULE,
997+
module(TyParam::value(self.get_module().unwrap().name.clone())),
998+
Immutable,
999+
vis,
1000+
None,
1001+
);
1002+
}
9761003
}
9771004

9781005
pub(crate) fn init_builtins(cfg: ErgConfig, shared: SharedCompilerResource) {
@@ -1001,14 +1028,16 @@ impl Context {
10011028
cfg: ErgConfig,
10021029
shared: SharedCompilerResource,
10031030
) -> Self {
1004-
Context::new(
1031+
let mut ctx = Context::new(
10051032
name.into(),
10061033
cfg,
10071034
ContextKind::Module,
10081035
vec![],
10091036
None,
10101037
Some(shared),
10111038
Context::TOP_LEVEL,
1012-
)
1039+
);
1040+
ctx.init_module_consts();
1041+
ctx
10131042
}
10141043
}

crates/erg_compiler/link_hir.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,15 @@ impl<'a> HIRLinker<'a> {
176176
Accessor::Attr(attr) => {
177177
self.replace_import(&mut attr.obj);
178178
}
179-
Accessor::Ident(_) => {}
179+
Accessor::Ident(ident) => match &ident.inspect()[..] {
180+
"module" => {
181+
*expr = Self::self_module();
182+
}
183+
"global" => {
184+
*expr = Expr::from(Identifier::public("__builtins__"));
185+
}
186+
_ => {}
187+
},
180188
}
181189
}
182190
Expr::Array(array) => match array {
@@ -286,6 +294,12 @@ impl<'a> HIRLinker<'a> {
286294
}
287295
}
288296

297+
fn self_module() -> Expr {
298+
let __import__ = Identifier::public("__import__");
299+
let __name__ = Identifier::public("__name__");
300+
Expr::from(__import__).call1(Expr::from(__name__))
301+
}
302+
289303
/// ```erg
290304
/// x = import "mod"
291305
/// ```
@@ -311,10 +325,7 @@ impl<'a> HIRLinker<'a> {
311325
// self = __import__(__name__)
312326
if matches!((path.canonicalize(), self.cfg.input.unescaped_path().canonicalize()), (Ok(l), Ok(r)) if l == r)
313327
{
314-
let __import__ = Identifier::public("__import__");
315-
let __name__ = Identifier::public("__name__");
316-
let call = Expr::from(__import__).call1(Expr::from(__name__));
317-
*expr = call;
328+
*expr = Self::self_module();
318329
return;
319330
}
320331
// In the case of REPL, entries cannot be used up

0 commit comments

Comments
 (0)