This commit is contained in:
Shunsuke Shibayama 2024-05-03 13:29:31 +09:00
parent 29a70b0632
commit 50787a075f
4 changed files with 45 additions and 0 deletions

View file

@ -233,6 +233,11 @@ impl<'a> HIRLinker<'a> {
match acc {
Accessor::Attr(attr) => {
self.replace_import(&mut attr.obj);
if attr.ident.inspect() == "__file__"
&& attr.ident.vi.def_loc.module.is_none()
{
*expr = self.__file__();
}
}
Accessor::Ident(ident) => match &ident.inspect()[..] {
"module" => {
@ -241,6 +246,9 @@ impl<'a> HIRLinker<'a> {
"global" => {
*expr = Expr::from(Identifier::static_public("__builtins__"));
}
"__file__" if ident.vi.def_loc.module.is_none() => {
*expr = self.__file__();
}
_ => {}
},
}
@ -363,6 +371,22 @@ impl<'a> HIRLinker<'a> {
Expr::from(__import__).call1(Expr::from(__name__))
}
fn __file__(&self) -> Expr {
let path = self.cfg.input.path().to_path_buf();
let token = Token::new_fake(
TokenKind::StrLit,
format!(
"\"{}\"",
path.canonicalize().unwrap_or(path).to_string_lossy()
),
0,
0,
0,
);
let lit = Literal::try_from(token).unwrap();
Expr::from(lit)
}
/// ```erg
/// x = import "mod"
/// ```