Skip to content

Commit

Permalink
feat: support glob import
Browse files Browse the repository at this point in the history
  • Loading branch information
mtshiba committed Aug 18, 2024
1 parent ce12285 commit 67a65b5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
26 changes: 26 additions & 0 deletions crates/py2erg/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2193,6 +2193,29 @@ impl ASTConverter {
}
}

fn convert_glob_import(&mut self, location: PyLocation, module: String) -> Expr {
let import_acc = Expr::Accessor(Accessor::Ident(
self.convert_ident("__import__".to_string(), location),
));
let cont = if module == "." {
"\"__init__\"".to_string()
} else {
format!("\"{module}\"")
};
let mod_name = Expr::Literal(Literal::new(Token::new(
TokenKind::StrLit,
cont,
location.row.get(),
location.column.to_zero_indexed(),
)));
let call = import_acc.clone().call1(mod_name);
let var = VarSignature::new(VarPattern::Glob(Token::DUMMY), None);
Expr::Def(Def::new(
Signature::Var(var),
DefBody::new(EQUAL, Block::new(vec![call]), DefId(0)),
))
}

/**
```erg
from foo import bar # if bar, baz are modules
Expand Down Expand Up @@ -2242,6 +2265,9 @@ impl ASTConverter {
let call = import_acc.clone().call1(mod_name);
let mut exprs = vec![];
let mut imports = vec![];
if names.len() == 1 && names[0].name.as_str() == "*" {
return self.convert_glob_import(location, module);
}
for name in names {
let name_path = self
.cfg
Expand Down
3 changes: 3 additions & 0 deletions tests/import.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import datetime as dt
from http.client import HTTPResponse
import http
from math import *

i = random.randint(0, 1)
print(i + 1)
Expand Down Expand Up @@ -43,3 +44,5 @@
assert export.http.client.HTTPResponse == Resp

_ = bar.Baz

_ = sin(acos(exp(0))) # OK

0 comments on commit 67a65b5

Please sign in to comment.