@@ -137,10 +137,12 @@ std::string SymbolTable::get_unique_name(const std::string &name) {
137137
138138void SymbolTable::move_symbols_from_global_scope (Allocator &al,
139139 SymbolTable *module_scope, Vec<char *> &syms,
140- Vec<char *> &mod_dependencies, Vec<ASR::stmt_t *> &var_init) {
140+ Vec<char *> &mod_dependencies, Vec<char *> &func_dependencies,
141+ Vec<ASR::stmt_t *> &var_init) {
141142 // TODO: This isn't scalable. We have write a visitor in asdl_cpp.py
142143 syms.reserve (al, 4 );
143144 mod_dependencies.reserve (al, 4 );
145+ func_dependencies.reserve (al, 4 );
144146 var_init.reserve (al, 4 );
145147 for (auto &a : scope) {
146148 switch (a.second ->type ) {
@@ -202,6 +204,49 @@ void SymbolTable::move_symbols_from_global_scope(Allocator &al,
202204 mod_dependencies.push_back (al, es->m_module_name );
203205 }
204206 es->m_parent_symtab = module_scope;
207+ ASR::symbol_t *s = ASRUtils::symbol_get_past_external (a.second );
208+ LCOMPILERS_ASSERT (s);
209+ if (ASR::is_a<ASR::Variable_t>(*s)) {
210+ ASR::Variable_t *v = ASR::down_cast<ASR::Variable_t>(s);
211+ if (v->m_symbolic_value && !ASR::is_a<ASR::Const_t>(*v->m_type )
212+ && ASR::is_a<ASR::List_t>(*v->m_type )) {
213+ ASR::expr_t * target = ASRUtils::EXPR (ASR::make_Var_t (
214+ al, v->base .base .loc , (ASR::symbol_t *) es));
215+ ASR::expr_t *value = v->m_symbolic_value ;
216+ v->m_symbolic_value = nullptr ;
217+ v->m_value = nullptr ;
218+ if (ASR::is_a<ASR::FunctionCall_t>(*value)) {
219+ ASR::FunctionCall_t *call =
220+ ASR::down_cast<ASR::FunctionCall_t>(value);
221+ ASR::Module_t *m = ASRUtils::get_sym_module (s);
222+ ASR::symbol_t *func = m->m_symtab ->get_symbol (
223+ ASRUtils::symbol_name (call->m_name ));
224+ ASR::Function_t *f = ASR::down_cast<ASR::Function_t>(func);
225+ std::string func_name = std::string (m->m_name ) +
226+ " @" + f->m_name ;
227+ ASR::symbol_t *es_func;
228+ if (!module_scope->get_symbol (func_name)) {
229+ es_func = ASR::down_cast<ASR::symbol_t >(
230+ ASR::make_ExternalSymbol_t (al, f->base .base .loc ,
231+ module_scope, s2c (al, func_name), func, m->m_name ,
232+ nullptr , 0 , s2c (al, f->m_name ), ASR::accessType::Public));
233+ module_scope->add_symbol (func_name, es_func);
234+ if (!present (func_dependencies, s2c (al, func_name))) {
235+ func_dependencies.push_back (al, s2c (al,func_name));
236+ }
237+ } else {
238+ es_func = module_scope->get_symbol (func_name);
239+ }
240+ value = ASRUtils::EXPR (ASR::make_FunctionCall_t (al,
241+ call->base .base .loc , es_func, call->m_original_name ,
242+ call->m_args , call->n_args , call->m_type ,
243+ call->m_value , call->m_dt ));
244+ }
245+ ASR::asr_t * assign = ASR::make_Assignment_t (al,
246+ v->base .base .loc , target, value, nullptr );
247+ var_init.push_back (al, ASRUtils::STMT (assign));
248+ }
249+ }
205250 module_scope->add_symbol (a.first , (ASR::symbol_t *) es);
206251 syms.push_back (al, s2c (al, a.first ));
207252 break ;
@@ -250,7 +295,7 @@ void SymbolTable::move_symbols_from_global_scope(Allocator &al,
250295 } default : {
251296 throw LCompilersException (" Moving the symbol:`" + a.first +
252297 " ` from global scope is not implemented yet" );
253- };
298+ }
254299 }
255300 }
256301}
0 commit comments