Skip to content

Commit cc02ba9

Browse files
authored
Merge pull request #1476 from Smit-create/i-1472
C: Generalize handing of extra codegen
2 parents 259d01a + 62fb12b commit cc02ba9

File tree

4 files changed

+96
-88
lines changed

4 files changed

+96
-88
lines changed

integration_tests/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ RUN(NAME generics_02 LABELS cpython llvm c)
410410
RUN(NAME generics_array_01 LABELS cpython llvm)
411411
RUN(NAME generics_array_02 LABELS cpython llvm)
412412
RUN(NAME generics_array_03 LABELS cpython llvm)
413-
RUN(NAME generics_list_01 LABELS cpython llvm)
413+
RUN(NAME generics_list_01 LABELS cpython llvm c)
414414
RUN(NAME test_statistics LABELS cpython llvm)
415415
RUN(NAME test_str_attributes LABELS cpython llvm c)
416416
RUN(NAME kwargs_01 LABELS cpython llvm c)

integration_tests/print_list_tuple.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,17 @@ def test_print_list():
2020
print(x, y, z, t)
2121

2222

23-
test_print_list()
23+
def f(y: list[i32]) -> list[i32]:
24+
y.append(4)
25+
return y
26+
27+
def g():
28+
print(f([1,2,3]))
29+
30+
31+
def check():
32+
g()
33+
test_print_list()
34+
35+
36+
check()

src/libasr/codegen/asr_to_c.cpp

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,7 +1016,7 @@ R"(
10161016
void visit_Assert(const ASR::Assert_t &x) {
10171017
std::string indent(indentation_level*indentation_spaces, ' ');
10181018
std::string out = indent;
1019-
tmp_src.clear();
1019+
bracket_open++;
10201020
if (x.m_msg) {
10211021
out += "ASSERT_MSG(";
10221022
visit_expr(*x.m_test);
@@ -1028,12 +1028,8 @@ R"(
10281028
visit_expr(*x.m_test);
10291029
out += src + ");\n";
10301030
}
1031-
src = "";
1032-
if (!tmp_src.empty()) {
1033-
for (auto &s: tmp_src) src += s;
1034-
}
1035-
src += out;
1036-
tmp_src.clear();
1031+
bracket_open--;
1032+
src = check_tmp_buffer() + out;
10371033
}
10381034

10391035
void visit_CPtrToPointer(const ASR::CPtrToPointer_t& x) {
@@ -1069,6 +1065,7 @@ R"(
10691065
void visit_Print(const ASR::Print_t &x) {
10701066
std::string indent(indentation_level*indentation_spaces, ' ');
10711067
std::string tmp_gen = indent + "printf(\"", out = "";
1068+
bracket_open++;
10721069
std::vector<std::string> v;
10731070
std::string separator;
10741071
if (x.m_separator) {
@@ -1093,11 +1090,6 @@ R"(
10931090
}
10941091
tmp_gen += ");\n";
10951092
out += tmp_gen;
1096-
if (ASR::is_a<ASR::ListConstant_t>(*x.m_values[i]) ||
1097-
ASR::is_a<ASR::TupleConstant_t>(*x.m_values[i])) {
1098-
out += src;
1099-
src = const_var_names[get_hash((ASR::asr_t*)x.m_values[i])];
1100-
}
11011093
tmp_gen = indent + "printf(\"";
11021094
v.clear();
11031095
std::string p_func = c_ds_api->get_print_func(value_type);
@@ -1129,8 +1121,9 @@ R"(
11291121
}
11301122
}
11311123
tmp_gen += ");\n";
1124+
bracket_open--;
11321125
out += tmp_gen;
1133-
src = out;
1126+
src = this->check_tmp_buffer() + out;
11341127
}
11351128

11361129
void visit_ArraySize(const ASR::ArraySize_t& x) {

0 commit comments

Comments
 (0)