Skip to content

Commit 4dbbf93

Browse files
kmr-srbhubaidsk
andauthored
Fix symbolic pass for printing ListItem (#2620)
* Fix list item symbolic pass * Tests: Add tests and update test references * Tests: Update test references * Remove trailing whitespace Co-authored-by: Shaikh Ubaid <shaikhubaid769@gmail.com> * Simplify statement Co-authored-by: Shaikh Ubaid <shaikhubaid769@gmail.com> * Tests: Update test and test references --------- Co-authored-by: Shaikh Ubaid <shaikhubaid769@gmail.com>
1 parent c53ffed commit 4dbbf93

File tree

6 files changed

+78
-0
lines changed

6 files changed

+78
-0
lines changed

integration_tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,7 @@ RUN(NAME print_float LABELS cpython llvm c wasm wasm_x64)
471471
RUN(NAME print_list_tuple_01 LABELS cpython llvm c NOFAST)
472472
RUN(NAME print_list_tuple_02 LABELS cpython llvm c NOFAST)
473473
RUN(NAME print_list_tuple_03 LABELS cpython llvm c NOFAST)
474+
RUN(NAME test_list_item_mixed_print LABELS cpython llvm c NOFAST)
474475

475476
# CPython and LLVM
476477
RUN(NAME const_01 LABELS cpython llvm c wasm)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from lpython import i32, f64
2+
3+
# Test for verifying printing items of different types with a list:
4+
# 1. string and list item
5+
# 2. integer and list item
6+
# 3. float and list item
7+
# 4. tuple and list item
8+
#
9+
# Also test with a list item which is a nested list.
10+
def test_list_item_mixed_print():
11+
s_list: list[str] = ["Hello", "LPython"]
12+
13+
print("", s_list[0])
14+
print("This is", s_list[1])
15+
16+
i_list: list[i32] = [1, 2, 3, 4, 5]
17+
18+
print(i_list[0], i_list[1], i_list[2], "...", i_list[-3], i_list[-2], i_list[-1])
19+
print("The first element is:", i_list[0])
20+
21+
m: i32 = len(i_list) // 2
22+
print("The middle element is:", i_list[m])
23+
24+
f_list: list[f64] = [3.14, 6.28]
25+
26+
print(f_list[0], "* 2 =", f_list[1])
27+
print("Total:", f_list[0] + f_list[1])
28+
29+
t: tuple[i32, i32, i32] = (1, 2, 3)
30+
print(t, "is a tuple, but", i_list[0], "is a number.")
31+
32+
i_list2: list[i32] = [1, 2, 3]
33+
print(i_list2[0], i_list2[1], i_list2[2], sep=" is smaller than ")
34+
35+
i: i32
36+
for i in range(len(i_list)):
37+
print(i_list[i], end=" # ")
38+
print("\n")
39+
40+
n_list: list[list[i32]] = [[1, 2], [3, 4], [5, 6]]
41+
for i in range(len(n_list)):
42+
print("List ", i, ":", n_list[i])
43+
44+
test_list_item_mixed_print()

src/libasr/pass/replace_symbolic.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -842,6 +842,8 @@ class ReplaceSymbolicVisitor : public PassUtils::PassVisitor<ReplaceSymbolicVisi
842842
x.base.base.loc, list_item->m_a, list_item->m_pos,
843843
ASRUtils::TYPE(ASR::make_CPtr_t(al, x.base.base.loc)), nullptr));
844844
print_tmp.push_back(basic_str(x.base.base.loc, value));
845+
} else {
846+
print_tmp.push_back(val);
845847
}
846848
} else {
847849
print_tmp.push_back(x.m_values[i]);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"basename": "runtime-test_list_item_mixed_print-a3fd49f",
3+
"cmd": "lpython {infile}",
4+
"infile": "tests/../integration_tests/test_list_item_mixed_print.py",
5+
"infile_hash": "14ce4950ca0ff6c6f610df787ad8d260148866f4c7062ab0b856ec5a",
6+
"outfile": null,
7+
"outfile_hash": null,
8+
"stdout": "runtime-test_list_item_mixed_print-a3fd49f.stdout",
9+
"stdout_hash": "cffcdb43864ef4e234dec5a10923f9077b7c6d1f4d9c37df1882f375",
10+
"stderr": null,
11+
"stderr_hash": null,
12+
"returncode": 0
13+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Hello
2+
This is LPython
3+
1 2 3 ... 3 4 5
4+
The first element is: 1
5+
The middle element is: 3
6+
3.14000000000000012e+00 * 2 = 6.28000000000000025e+00
7+
Total: 9.41999999999999993e+00
8+
(1, 2, 3) is a tuple, but 1 is a number.
9+
1 is smaller than 2 is smaller than 3
10+
1 # 2 # 3 # 4 # 5 #
11+
12+
List 0 : [1, 2]
13+
List 1 : [3, 4]
14+
List 2 : [5, 6]

tests/tests.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,10 @@ pass = "print_list_tuple"
461461
filename = "../integration_tests/print_04.py"
462462
llvm = true
463463

464+
[[test]]
465+
filename = "../integration_tests/test_list_item_mixed_print.py"
466+
run = true
467+
464468
[[test]]
465469
filename = "../integration_tests/generics_01.py"
466470
asr = true

0 commit comments

Comments
 (0)