@@ -4722,12 +4722,12 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
47224722 ASR::TranslationUnit_t *unit = ASR::down_cast2<ASR::TranslationUnit_t>(asr);
47234723 current_scope = unit->m_global_scope ;
47244724 LCOMPILERS_ASSERT (current_scope != nullptr );
4725- ASR::symbol_t * main_module_sym = current_scope-> get_symbol (module_name) ;
4725+ ASR::symbol_t * module_sym = nullptr ;
47264726 ASR::Module_t* mod = nullptr ;
4727- if ( main_module_sym ) {
4728- mod = ASR::down_cast<ASR::Module_t>(main_module_sym);
4729- }
4727+
47304728 if (!main_module) {
4729+ module_sym = current_scope->get_symbol (module_name);
4730+ mod = ASR::down_cast<ASR::Module_t>(module_sym);
47314731 current_scope = mod->m_symtab ;
47324732 LCOMPILERS_ASSERT (current_scope != nullptr );
47334733 }
@@ -4749,72 +4749,69 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
47494749 tmp_vec.clear ();
47504750 }
47514751 }
4752+
47524753 if ( mod ) {
47534754 for ( size_t i = 0 ; i < mod->n_dependencies ; i++ ) {
47544755 current_module_dependencies.push_back (al, mod->m_dependencies [i]);
47554756 }
47564757 mod->m_dependencies = current_module_dependencies.p ;
4757- mod->n_dependencies = current_module_dependencies.size ();
4758- }
4759-
4760- if (global_init.n > 0 && main_module_sym) {
4761- // unit->m_items is used and set to nullptr in the
4762- // `pass_wrap_global_stmts_into_function` pass
4763- unit->m_items = global_init.p ;
4764- unit->n_items = global_init.size ();
4765- std::string func_name = " global_initializer" ;
4766- LCompilers::PassOptions pass_options;
4767- pass_options.run_fun = func_name;
4768- pass_wrap_global_stmts (al, *unit, pass_options);
4769-
4770- ASR::Module_t *mod = ASR::down_cast<ASR::Module_t>(main_module_sym);
4771- ASR::symbol_t *f_sym = unit->m_global_scope ->get_symbol (func_name);
4772- if (f_sym) {
4773- // Add the `global_initilaizer` function into the `__main__`
4774- // module and later call this function to initialize the
4775- // global variables like list, ...
4776- ASR::Function_t *f = ASR::down_cast<ASR::Function_t>(f_sym);
4777- f->m_symtab ->parent = mod->m_symtab ;
4778- mod->m_symtab ->add_symbol (func_name, (ASR::symbol_t *) f);
4779- // Erase the function in TranslationUnit
4780- unit->m_global_scope ->erase_symbol (func_name);
4781- }
4782- global_init.p = nullptr ;
4783- global_init.n = 0 ;
4784- }
4785-
4786- if (global_init.n > 0 ) {
4787- // copy all the item's from `items` (global_statements)
4788- // into `global_init`
4789- for (auto &i: items) {
4790- global_init.push_back (al, i);
4758+ mod->n_dependencies = current_module_dependencies.n ;
4759+
4760+ if (global_init.n > 0 ) {
4761+ // unit->m_items is used and set to nullptr in the
4762+ // `pass_wrap_global_stmts_into_function` pass
4763+ unit->m_items = global_init.p ;
4764+ unit->n_items = global_init.size ();
4765+ std::string func_name = " global_initializer" ;
4766+ LCompilers::PassOptions pass_options;
4767+ pass_options.run_fun = func_name;
4768+ pass_wrap_global_stmts (al, *unit, pass_options);
4769+
4770+ ASR::symbol_t *f_sym = unit->m_global_scope ->get_symbol (func_name);
4771+ if (f_sym) {
4772+ // Add the `global_initilaizer` function into the
4773+ // module and later call this function to initialize the
4774+ // global variables like list, ...
4775+ ASR::Function_t *f = ASR::down_cast<ASR::Function_t>(f_sym);
4776+ f->m_symtab ->parent = mod->m_symtab ;
4777+ mod->m_symtab ->add_symbol (func_name, (ASR::symbol_t *) f);
4778+ // Erase the function in TranslationUnit
4779+ unit->m_global_scope ->erase_symbol (func_name);
4780+ }
4781+ global_init.p = nullptr ;
4782+ global_init.n = 0 ;
4783+ }
4784+
4785+ if (items.n > 0 ) {
4786+ unit->m_items = items.p ;
4787+ unit->n_items = items.size ();
4788+ std::string func_name = " global_statements" ;
4789+ // Wrap all the global statements into a Function
4790+ LCompilers::PassOptions pass_options;
4791+ pass_options.run_fun = func_name;
4792+ pass_wrap_global_stmts (al, *unit, pass_options);
4793+
4794+ ASR::symbol_t *f_sym = unit->m_global_scope ->get_symbol (func_name);
4795+ if (f_sym) {
4796+ // Add the `global_statements` function into the
4797+ // module and later call this function to execute the
4798+ // global_statements
4799+ ASR::Function_t *f = ASR::down_cast<ASR::Function_t>(f_sym);
4800+ f->m_symtab ->parent = mod->m_symtab ;
4801+ mod->m_symtab ->add_symbol (func_name, (ASR::symbol_t *) f);
4802+ // Erase the function in TranslationUnit
4803+ unit->m_global_scope ->erase_symbol (func_name);
4804+ }
4805+ items.p = nullptr ;
4806+ items.n = 0 ;
47914807 }
4792- unit->m_items = global_init.p ;
4793- unit->n_items = global_init.size ();
47944808 } else {
4795- unit->m_items = items.p ;
4796- unit->n_items = items.size ();
4797- }
4798-
4799- if (items.n > 0 && main_module_sym) {
4800- std::string func_name = " global_statements" ;
4801- // Wrap all the global statements into a Function
4802- LCompilers::PassOptions pass_options;
4803- pass_options.run_fun = func_name;
4804- pass_wrap_global_stmts (al, *unit, pass_options);
4805-
4806- ASR::Module_t *mod = ASR::down_cast<ASR::Module_t>(main_module_sym);
4807- ASR::symbol_t *f_sym = unit->m_global_scope ->get_symbol (func_name);
4808- if (f_sym) {
4809- // Add the `global_statements` function into the `__main__`
4810- // module and later call this function to execute the
4811- // global_statements
4812- ASR::Function_t *f = ASR::down_cast<ASR::Function_t>(f_sym);
4813- f->m_symtab ->parent = mod->m_symtab ;
4814- mod->m_symtab ->add_symbol (func_name, (ASR::symbol_t *) f);
4815- // Erase the function in TranslationUnit
4816- unit->m_global_scope ->erase_symbol (func_name);
4809+ // It is main_module
4810+ for (auto item:items) {
4811+ global_init.push_back (al, item);
48174812 }
4813+ unit->m_items = global_init.p ;
4814+ unit->n_items = global_init.size ();
48184815 }
48194816
48204817 tmp = asr;
0 commit comments