Skip to content

Commit 945f90a

Browse files
czgdp1807ubaidsk
authored andcommitted
done
1 parent 4552c22 commit 945f90a

File tree

3 files changed

+24
-13
lines changed

3 files changed

+24
-13
lines changed

src/libasr/asr_scopes.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,18 +71,10 @@ SymbolTable::SymbolTable(SymbolTable *parent) : parent{parent} {
7171
counter = symbol_table_counter;
7272
}
7373

74-
void SymbolTable::set_global_counter(unsigned int counter_) {
75-
symbol_table_counter = counter_;
76-
}
77-
7874
void SymbolTable::reset_global_counter() {
7975
symbol_table_counter = 0;
8076
}
8177

82-
unsigned int SymbolTable::get_global_counter() {
83-
return symbol_table_counter;
84-
}
85-
8678
void SymbolTable::mark_all_variables_external(Allocator &/*al*/) {
8779
for (auto &a : scope) {
8880
switch (a.second->type) {

src/libasr/asr_scopes.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ struct SymbolTable {
3333
return std::to_string(counter);
3434
}
3535
static void reset_global_counter(); // Resets the internal global counter
36-
static void set_global_counter(unsigned counter_);
37-
static unsigned int get_global_counter();
3836

3937
// Resolves the symbol `name` recursively in current and parent scopes.
4038
// Returns `nullptr` if symbol not found.

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,26 @@ namespace CastingUtil {
175175
int save_pyc_files(const LFortran::ASR::TranslationUnit_t &u,
176176
std::string infile) {
177177
LFORTRAN_ASSERT(LFortran::asr_verify(u));
178+
Allocator al(4*1024);
179+
LFortran::SymbolTable *symtab =
180+
al.make_new<LFortran::SymbolTable>(nullptr);
181+
std::vector<std::pair<ASR::Module_t*, SymbolTable*>> module_parent;
182+
for (auto &item : u.m_global_scope->get_scope()) {
183+
if (LFortran::ASR::is_a<LFortran::ASR::Module_t>(*item.second)) {
184+
LFortran::ASR::Module_t *m = LFortran::ASR::down_cast<LFortran::ASR::Module_t>(item.second);
185+
186+
symtab->add_symbol(std::string(m->m_name), item.second);
187+
module_parent.push_back(std::make_pair(m, m->m_symtab->parent));
188+
m->m_symtab->parent = symtab;
189+
}
190+
}
191+
192+
LFortran::Location loc;
193+
LFortran::ASR::asr_t *asr = LFortran::ASR::make_TranslationUnit_t(al, loc,
194+
symtab, nullptr, 0);
195+
LFortran::ASR::TranslationUnit_t *tu =
196+
LFortran::ASR::down_cast2<LFortran::ASR::TranslationUnit_t>(asr);
197+
LFORTRAN_ASSERT(LFortran::asr_verify(*tu));
178198

179199
std::string modfile_binary = LFortran::save_pycfile(u);
180200

@@ -187,6 +207,10 @@ int save_pyc_files(const LFortran::ASR::TranslationUnit_t &u,
187207
out.open(modfile, std::ofstream::out | std::ofstream::binary);
188208
out << modfile_binary;
189209
}
210+
211+
for( auto& mod_par: module_parent ) {
212+
mod_par.first->m_symtab->parent = mod_par.second;
213+
}
190214
return 0;
191215
}
192216

@@ -265,11 +289,8 @@ ASR::TranslationUnit_t* compile_module_till_asr(Allocator& al,
265289
// Convert the module from AST to ASR
266290
LFortran::LocationManager lm;
267291
lm.in_filename = infile;
268-
unsigned int symtab_global_counter = SymbolTable::get_global_counter();
269-
SymbolTable::reset_global_counter();
270292
Result<ASR::TranslationUnit_t*> r2 = python_ast_to_asr(al, *ast,
271293
diagnostics, false, true, false, infile, "");
272-
SymbolTable::set_global_counter(symtab_global_counter);
273294
save_pyc_files(*r2.result, infile + "c");
274295
std::string input;
275296
read_file(infile, input);

0 commit comments

Comments
 (0)