Skip to content

Commit

Permalink
Handle initialisation at declaration inside for loop
Browse files Browse the repository at this point in the history
  • Loading branch information
czgdp1807 committed Mar 13, 2023
1 parent 7f2e744 commit 270dfb2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
9 changes: 9 additions & 0 deletions integration_tests/structs_21.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,19 @@ def f(c: CPtr):
print(A)
assert A == 3

def g(c: CPtr):
i: i32
for i in range(3):
p: Pointer[S] = c_p_pointer(c, S)
A: i32 = p.a
print(A)
assert A == 3

def main():
s: S = S(3)
p: CPtr = empty_c_void_p()
p_c_pointer(pointer(s, S), p)
f(p)
g(p)

main()
3 changes: 3 additions & 0 deletions src/libasr/codegen/asr_to_c_cpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,8 @@ R"(#include <stdio.h>
std::string close_paranthesis = indent + "}\n";
indent += std::string(indentation_spaces, ' ');
indentation_level += 1;
SymbolTable* current_scope_copy = current_scope;
current_scope = block->m_symtab;
std::vector<std::string> var_order = ASRUtils::determine_variable_declaration_order(block->m_symtab);
for (auto &item : var_order) {
ASR::symbol_t* var_sym = block->m_symtab->get_symbol(item);
Expand All @@ -343,6 +345,7 @@ R"(#include <stdio.h>
decl += check_tmp_buffer();
src = open_paranthesis + decl + body + close_paranthesis;
indentation_level -= 1;
current_scope = current_scope_copy;
}

// Returns the declaration, no semi colon at the end
Expand Down

0 comments on commit 270dfb2

Please sign in to comment.