Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle initialisation at declaration inside for loop #1576

Merged
merged 1 commit into from
Mar 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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