Skip to content

Commit 6292d4f

Browse files
Compile simple python packages and load it into the ASR
1 parent 535fcd2 commit 6292d4f

File tree

1 file changed

+61
-3
lines changed

1 file changed

+61
-3
lines changed

src/lpython/semantics/python_ast_to_asr.cpp

+61-3
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,7 @@ ASR::Module_t* load_module(Allocator &al, SymbolTable *symtab,
307307
bool compile_module = true;
308308
ASR::TranslationUnit_t* mod1 = nullptr;
309309
std::string input;
310+
std::string module_dir_name = "";
310311
bool found = set_module_path(infile0c, rl_path, infile,
311312
path_used, input, ltypes, enum_py);
312313
if( !found ) {
@@ -335,7 +336,15 @@ ASR::Module_t* load_module(Allocator &al, SymbolTable *symtab,
335336

336337
// insert into `symtab`
337338
std::vector<std::pair<std::string, ASR::Module_t*>> children_modules;
338-
ASRUtils::extract_module_python(*mod1, children_modules, module_name);
339+
if (module_name == "__init__") {
340+
// remove `__init__.py`
341+
module_dir_name = infile.substr(0, infile.find_last_of('/'));
342+
// assign module directory name
343+
module_dir_name = module_dir_name.substr(module_dir_name.find_last_of('/') + 1);
344+
ASRUtils::extract_module_python(*mod1, children_modules, module_dir_name);
345+
} else {
346+
ASRUtils::extract_module_python(*mod1, children_modules, module_name);
347+
}
339348
ASR::Module_t* mod2 = nullptr;
340349
for( auto& a: children_modules ) {
341350
std::string a_name = a.first;
@@ -344,6 +353,10 @@ ASR::Module_t* load_module(Allocator &al, SymbolTable *symtab,
344353
a_mod->m_name = s2c(al, module_name);
345354
a_mod->m_intrinsic = intrinsic;
346355
mod2 = a_mod;
356+
} else if (a_name == module_dir_name) {
357+
a_mod->m_name = s2c(al, module_dir_name);
358+
a_mod->m_intrinsic = intrinsic;
359+
mod2 = a_mod;
347360
}
348361
symtab->add_symbol(a_name, (ASR::symbol_t*)a_mod);
349362
a_mod->m_symtab->parent = symtab;
@@ -429,9 +442,18 @@ ASR::symbol_t* import_from_module(Allocator &al, ASR::Module_t *m, SymbolTable *
429442
ASR::accessType::Public
430443
);
431444
return ASR::down_cast<ASR::symbol_t>(v);
445+
} else if (ASR::is_a<ASR::ExternalSymbol_t>(*t)) {
446+
ASR::ExternalSymbol_t *es = ASR::down_cast<ASR::ExternalSymbol_t>(t);
447+
SymbolTable *symtab = current_scope;
448+
while (symtab->parent != nullptr) symtab = symtab->parent;
449+
ASR::symbol_t *sym = symtab->get_symbol(es->m_module_name);
450+
ASR::Module_t *m = ASR::down_cast<ASR::Module_t>(sym);
451+
452+
return import_from_module(al, m, symtab, es->m_name,
453+
cur_sym_name, new_sym_name, loc);
432454
} else {
433-
throw SemanticError("Only Subroutines, Functions and Variables are currently supported in 'import'",
434-
loc);
455+
throw SemanticError("Only Subroutines, Functions, Variables and "
456+
"ExternalSymbol are currently supported in 'import'", loc);
435457
}
436458
LFORTRAN_ASSERT(false);
437459
return nullptr;
@@ -3157,6 +3179,24 @@ class SymbolTableVisitor : public CommonVisitor<SymbolTableVisitor> {
31573179
st = st->parent;
31583180
}
31593181
bool ltypes, enum_py;
3182+
if (msym != "ltypes") {
3183+
if (import_path != "" &&
3184+
!path_exits(paths[0] + '/' + msym + ".py")) {
3185+
paths = {import_path};
3186+
if (parent_dir != "") paths[0] += '/' + parent_dir;
3187+
if(is_directory(paths[0])) {
3188+
paths[0] += '/' + msym;
3189+
msym = "__init__";
3190+
}
3191+
} else if (is_directory(msym)) {
3192+
paths = {rl_path, msym};
3193+
msym = "__init__";
3194+
} else if (paths[1] != ""
3195+
&& is_directory(paths[1] + '/' + msym)) {
3196+
paths[1] += '/' + msym;
3197+
msym = "__init__";
3198+
}
3199+
}
31603200
t = (ASR::symbol_t*)(load_module(al, st,
31613201
msym, x.base.base.loc, false, paths, ltypes, enum_py,
31623202
[&](const std::string &msg, const Location &loc) { throw SemanticError(msg, loc); }
@@ -3202,6 +3242,24 @@ class SymbolTableVisitor : public CommonVisitor<SymbolTableVisitor> {
32023242
}
32033243
for (auto &mod_sym : mods) {
32043244
bool ltypes, enum_py;
3245+
if (mod_sym != "ltypes") {
3246+
if (import_path != "" &&
3247+
!path_exits(paths[0] + '/' + mod_sym + ".py")) {
3248+
paths = {import_path};
3249+
if (parent_dir != "") paths[0] += '/' + parent_dir;
3250+
if(is_directory(paths[0])) {
3251+
paths[0] += '/' + mod_sym;
3252+
mod_sym = "__init__";
3253+
}
3254+
} else if (is_directory(mod_sym)) {
3255+
paths = {rl_path, mod_sym};
3256+
mod_sym = "__init__";
3257+
} else if (paths[1] != ""
3258+
&& is_directory(paths[1] + '/' + mod_sym)) {
3259+
paths[1] += '/' + mod_sym;
3260+
mod_sym = "__init__";
3261+
}
3262+
}
32053263
t = (ASR::symbol_t*)(load_module(al, st,
32063264
mod_sym, x.base.base.loc, false, paths, ltypes, enum_py,
32073265
[&](const std::string &msg, const Location &loc) { throw SemanticError(msg, loc); }

0 commit comments

Comments
 (0)