Skip to content

Commit

Permalink
feat: add special variables module/global
Browse files Browse the repository at this point in the history
  • Loading branch information
mtshiba committed May 25, 2023
1 parent 30eac6d commit 4af0c4c
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 17 deletions.
53 changes: 41 additions & 12 deletions crates/erg_compiler/context/initialize/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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(
Expand All @@ -973,6 +967,39 @@ impl Context {
Visibility::BUILTIN_PRIVATE,
Some(NONE),
);
if ERG_MODE {
self.register_builtin_py_impl(
FUNC_GLOBAL,
module(TyParam::value("<builtins>")),
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) {
Expand Down Expand Up @@ -1001,14 +1028,16 @@ impl Context {
cfg: ErgConfig,
shared: SharedCompilerResource,
) -> Self {
Context::new(
let mut ctx = Context::new(
name.into(),
cfg,
ContextKind::Module,
vec![],
None,
Some(shared),
Context::TOP_LEVEL,
)
);
ctx.init_module_consts();
ctx
}
}
21 changes: 16 additions & 5 deletions crates/erg_compiler/link_hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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"
/// ```
Expand All @@ -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
Expand Down

0 comments on commit 4af0c4c

Please sign in to comment.