Skip to content

Commit f9d7013

Browse files
XX Wrap all the symbols from global scope into a function
1 parent d916a2b commit f9d7013

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

src/libasr/asr_scopes.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,26 @@ std::string SymbolTable::get_unique_name(const std::string &name) {
134134
return unique_name;
135135
}
136136

137+
void SymbolTable::move_symbols_from_global_scope(SymbolTable *fn_scope,
138+
std::vector<std::string> &syms) {
139+
for (auto &a : scope) {
140+
switch (a.second->type) {
141+
case (ASR::symbolType::Variable) : {
142+
ASR::Variable_t *v = ASR::down_cast<ASR::Variable_t>(a.second);
143+
v->m_parent_symtab = fn_scope;
144+
fn_scope->add_symbol(a.first, (ASR::symbol_t *)v);
145+
syms.push_back(a.first);
146+
break;
147+
}
148+
case (ASR::symbolType::Function) : {
149+
// Pass
150+
break;
151+
}
152+
default : {
153+
LCompilersException("Not implemented!");
154+
};
155+
}
156+
}
157+
}
158+
137159
} // namespace LFortran

src/libasr/asr_scopes.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ struct SymbolTable {
8080
size_t n_scope_names, char **m_scope_names);
8181

8282
std::string get_unique_name(const std::string &name);
83+
84+
void move_symbols_from_global_scope(SymbolTable *fn_scope,
85+
std::vector<std::string> &syms);
8386
};
8487

8588
} // namespace LFortran

src/libasr/pass/global_stmts.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@ void pass_wrap_global_stmts_into_function(Allocator &al,
100100
throw LCompilersException("Unsupported type of global scope node");
101101
}
102102
}
103+
std::vector<std::string> symbols;
104+
unit.m_global_scope->move_symbols_from_global_scope(fn_scope, symbols);
105+
for (auto &a: symbols) {
106+
unit.m_global_scope->erase_symbol(a);
107+
}
103108

104109
if (return_var) {
105110
// The last item was an expression, create a function returning it

0 commit comments

Comments
 (0)