Skip to content

Commit b8c050c

Browse files
Support Annotated assignment in the global_scope
1 parent 2ec8413 commit b8c050c

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

src/libasr/asr_scopes.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,11 @@ std::string SymbolTable::get_unique_name(const std::string &name) {
137137

138138
void SymbolTable::move_symbols_from_global_scope(Allocator &al,
139139
SymbolTable *module_scope, Vec<char *> &syms,
140-
Vec<char *> &mod_dependencies) {
140+
Vec<char *> &mod_dependencies, Vec<ASR::stmt_t*> &var_init) {
141141
// TODO: This isn't scalable. We have write a visitor in asdl_cpp.py
142142
syms.reserve(al, 4);
143143
mod_dependencies.reserve(al, 4);
144+
var_init.reserve(al, 4);
144145
for (auto &a : scope) {
145146
switch (a.second->type) {
146147
case (ASR::symbolType::Module): {
@@ -225,6 +226,21 @@ void SymbolTable::move_symbols_from_global_scope(Allocator &al,
225226
} case (ASR::symbolType::Variable) : {
226227
ASR::Variable_t *v = ASR::down_cast<ASR::Variable_t>(a.second);
227228
v->m_parent_symtab = module_scope;
229+
if (v->m_symbolic_value && !ASR::is_a<ASR::Const_t>(*v->m_type)) {
230+
ASR::expr_t* v_expr = ASRUtils::EXPR(ASR::make_Var_t(
231+
al, v->base.base.loc, (ASR::symbol_t *) v));
232+
ASR::asr_t* assign = ASR::make_Assignment_t(al,
233+
v->base.base.loc, v_expr, v->m_symbolic_value, nullptr);
234+
var_init.push_back(al, ASRUtils::STMT(assign));
235+
v->m_symbolic_value = nullptr;
236+
v->m_value = nullptr;
237+
Vec<char*> v_dependencies;
238+
v_dependencies.reserve(al, 1);
239+
ASRUtils::collect_variable_dependencies(al,
240+
v_dependencies, v->m_type);
241+
v->m_dependencies = v_dependencies.p;
242+
v->n_dependencies = v_dependencies.size();
243+
}
228244
module_scope->add_symbol(a.first, (ASR::symbol_t *) v);
229245
syms.push_back(al, s2c(al, a.first));
230246
break;

src/libasr/asr_scopes.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ namespace LCompilers {
1010

1111
namespace ASR {
1212
struct asr_t;
13+
struct stmt_t;
1314
struct symbol_t;
1415
}
1516

@@ -84,7 +85,7 @@ struct SymbolTable {
8485

8586
void move_symbols_from_global_scope(Allocator &al,
8687
SymbolTable *module_scope, Vec<char *> &syms,
87-
Vec<char *> &mod_dependencies);
88+
Vec<char *> &mod_dependencies, Vec<ASR::stmt_t*> &var_init);
8889
};
8990

9091
} // namespace LCompilers

src/libasr/pass/global_symbols.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,35 @@ namespace LCompilers {
1616

1717
void pass_wrap_global_syms_into_module(Allocator &al,
1818
ASR::TranslationUnit_t &unit,
19-
const LCompilers::PassOptions& /*pass_options*/) {
19+
const LCompilers::PassOptions& pass_options) {
2020
Location loc = unit.base.base.loc;
2121
char *module_name = s2c(al, "_global_symbols");
2222
SymbolTable *module_scope = al.make_new<SymbolTable>(unit.m_global_scope);
2323
Vec<char *> moved_symbols;
2424
Vec<char *> mod_dependencies;
25+
Vec<ASR::stmt_t*> var_init;
2526

2627
// Move all the symbols from global into the module scope
2728
unit.m_global_scope->move_symbols_from_global_scope(al, module_scope,
28-
moved_symbols, mod_dependencies);
29+
moved_symbols, mod_dependencies, var_init);
2930

3031
// Erase the symbols that are moved into the module
3132
for (auto &sym: moved_symbols) {
3233
unit.m_global_scope->erase_symbol(sym);
3334
}
3435

36+
if (module_scope->get_symbol(pass_options.run_fun) && var_init.n > 0) {
37+
ASR::Function_t *f = ASR::down_cast<ASR::Function_t>(
38+
module_scope->get_symbol(pass_options.run_fun));
39+
for (size_t i = 0; i < f->n_body; i++) {
40+
var_init.push_back(al, f->m_body[i]);
41+
}
42+
f->m_body = var_init.p;
43+
f->n_body = var_init.n;
44+
// Overwrites the function: `_lpython_main_program`
45+
module_scope->add_symbol(f->m_name, (ASR::symbol_t *) f);
46+
}
47+
3548
Vec<char *> m_dependencies;
3649
m_dependencies.reserve(al, mod_dependencies.size());
3750
for( auto &dep: mod_dependencies) {

0 commit comments

Comments
 (0)