Skip to content

Commit 800e186

Browse files
committed
wip
1 parent ffd8770 commit 800e186

File tree

1 file changed

+32
-4
lines changed

1 file changed

+32
-4
lines changed

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,10 @@ ASR::Module_t* load_module(Allocator &al, SymbolTable *symtab,
423423
ASR::symbol_t* import_from_module(Allocator &al, ASR::Module_t *m, SymbolTable *current_scope,
424424
std::string mname, std::string cur_sym_name, std::string new_sym_name,
425425
const Location &loc, bool skip_current_scope_check=false) {
426+
// std::cout<<"module_name: "<<m->m_name<<std::endl;
427+
// for( auto& itr: m->m_symtab->get_scope() ) {
428+
// std::cout<<"itr.first: "<<itr.first<<std::endl;
429+
// }
426430
ASR::symbol_t *t = m->m_symtab->resolve_symbol(cur_sym_name);
427431
if (!t) {
428432
throw SemanticError("The symbol '" + cur_sym_name + "' not found in the module '" + mname + "'",
@@ -481,11 +485,20 @@ ASR::symbol_t* import_from_module(Allocator &al, ASR::Module_t *m, SymbolTable *
481485
} else if (ASR::is_a<ASR::ExternalSymbol_t>(*t)) {
482486
ASR::ExternalSymbol_t *es = ASR::down_cast<ASR::ExternalSymbol_t>(t);
483487
SymbolTable *symtab = current_scope;
484-
while (symtab->parent != nullptr) symtab = symtab->parent;
485-
ASR::symbol_t *sym = symtab->get_symbol(es->m_module_name);
488+
// while (symtab->parent != nullptr) symtab = symtab->parent;
489+
ASR::symbol_t *sym = symtab->resolve_symbol(es->m_module_name);
486490
ASR::Module_t *m = ASR::down_cast<ASR::Module_t>(sym);
487-
return import_from_module(al, m, symtab, es->m_name,
491+
return import_from_module(al, m, symtab, es->m_module_name,
488492
cur_sym_name, new_sym_name, loc);
493+
} else if (ASR::is_a<ASR::Module_t>(*t)) {
494+
ASR::Module_t* mt = ASR::down_cast<ASR::Module_t>(t);
495+
std::string mangled_cur_sym_name = ASRUtils::get_mangled_name(mt, new_sym_name);
496+
// std::cout<<"names: "<<mt->m_name<<" "<<cur_sym_name<<" "<<mangled_cur_sym_name<<" "<<new_sym_name<<" "<<mname<<std::endl;
497+
if( cur_sym_name == mangled_cur_sym_name ) {
498+
throw SemanticError("Importing modules isn't supported yet.", loc);
499+
}
500+
return import_from_module(al, mt, current_scope, std::string(mt->m_name),
501+
cur_sym_name, new_sym_name, loc);
489502
} else {
490503
throw SemanticError("Only Subroutines, Functions, Variables and "
491504
"ExternalSymbol are currently supported in 'import'", loc);
@@ -3566,10 +3579,19 @@ class SymbolTableVisitor : public CommonVisitor<SymbolTableVisitor> {
35663579
for (auto &mod_sym : mods) {
35673580
bool ltypes, enum_py;
35683581
set_module_symbol(mod_sym, paths);
3582+
// std::cout<<"msym: "<<mod_sym<<" ";
3583+
// for( auto path: paths ) {
3584+
// std::cout<<path<<", ";
3585+
// }
3586+
// std::cout<<std::endl;
35693587
t = (ASR::symbol_t*)(load_module(al, st,
35703588
mod_sym, x.base.base.loc, diag, lm, false, paths, ltypes, enum_py,
35713589
[&](const std::string &msg, const Location &loc) { throw SemanticError(msg, loc); },
35723590
allow_implicit_casting));
3591+
// std::cout<<"module_name.imported: "<<ASRUtils::symbol_name(t)<<std::endl;
3592+
// for( auto& itr: ASRUtils::symbol_symtab(t)->get_scope() ) {
3593+
// std::cout<<"itr.first: "<<itr.first<<std::endl;
3594+
// }
35733595
if (ltypes || enum_py) {
35743596
// TODO: For now we skip ltypes import completely. Later on we should note what symbols
35753597
// got imported from it, and give an error message if an annotation is used without
@@ -5898,7 +5920,13 @@ Result<ASR::TranslationUnit_t*> python_ast_to_asr(Allocator &al, LocationManager
58985920
} else {
58995921
return res2.error;
59005922
}
5901-
LFORTRAN_ASSERT(asr_verify(*tu, true, diagnostics));
5923+
#if defined(WITH_LFORTRAN_ASSERT)
5924+
diag::Diagnostics diagnostics;
5925+
if (!asr_verify(*tu, true, diagnostics)) {
5926+
std::cerr << diagnostics.render2();
5927+
throw LCompilersException("Verify failed");
5928+
};
5929+
#endif
59025930
}
59035931

59045932
if (main_module) {

0 commit comments

Comments
 (0)