Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fix symbolic pass for printing ListItem #2620

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

Merged
merged 7 commits into from
Mar 23, 2024
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
1 change: 1 addition & 0 deletions integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,7 @@ RUN(NAME print_float LABELS cpython llvm c wasm wasm_x64)
RUN(NAME print_list_tuple_01 LABELS cpython llvm c NOFAST)
RUN(NAME print_list_tuple_02 LABELS cpython llvm c NOFAST)
RUN(NAME print_list_tuple_03 LABELS cpython llvm c NOFAST)
RUN(NAME test_list_item_mixed_print LABELS cpython llvm c NOFAST)

# CPython and LLVM
RUN(NAME const_01 LABELS cpython llvm c wasm)
Expand Down
44 changes: 44 additions & 0 deletions integration_tests/test_list_item_mixed_print.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from lpython import i32, f64

# Test for verifying printing items of different types with a list:
# 1. string and list item
# 2. integer and list item
# 3. float and list item
# 4. tuple and list item
#
# Also test with a list item which is a nested list.
def test_list_item_mixed_print():
s_list: list[str] = ["Hello", "LPython"]

print("", s_list[0])
print("This is", s_list[1])

i_list: list[i32] = [1, 2, 3, 4, 5]

print(i_list[0], i_list[1], i_list[2], "...", i_list[-3], i_list[-2], i_list[-1])
print("The first element is:", i_list[0])

m: i32 = len(i_list) // 2
print("The middle element is:", i_list[m])

f_list: list[f64] = [3.14, 6.28]

print(f_list[0], "* 2 =", f_list[1])
print("Total:", f_list[0] + f_list[1])

t: tuple[i32, i32, i32] = (1, 2, 3)
print(t, "is a tuple, but", i_list[0], "is a number.")

i_list2: list[i32] = [1, 2, 3]
print(i_list2[0], i_list2[1], i_list2[2], sep=" is smaller than ")

i: i32
for i in range(len(i_list)):
print(i_list[i], end=" # ")
print("\n")

n_list: list[list[i32]] = [[1, 2], [3, 4], [5, 6]]
for i in range(len(n_list)):
print("List ", i, ":", n_list[i])

test_list_item_mixed_print()
2 changes: 2 additions & 0 deletions src/libasr/pass/replace_symbolic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,8 @@ class ReplaceSymbolicVisitor : public PassUtils::PassVisitor<ReplaceSymbolicVisi
x.base.base.loc, list_item->m_a, list_item->m_pos,
ASRUtils::TYPE(ASR::make_CPtr_t(al, x.base.base.loc)), nullptr));
print_tmp.push_back(basic_str(x.base.base.loc, value));
} else {
print_tmp.push_back(val);
}
} else {
print_tmp.push_back(x.m_values[i]);
Expand Down
13 changes: 13 additions & 0 deletions tests/reference/runtime-test_list_item_mixed_print-a3fd49f.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"basename": "runtime-test_list_item_mixed_print-a3fd49f",
"cmd": "lpython {infile}",
"infile": "tests/../integration_tests/test_list_item_mixed_print.py",
"infile_hash": "14ce4950ca0ff6c6f610df787ad8d260148866f4c7062ab0b856ec5a",
"outfile": null,
"outfile_hash": null,
"stdout": "runtime-test_list_item_mixed_print-a3fd49f.stdout",
"stdout_hash": "cffcdb43864ef4e234dec5a10923f9077b7c6d1f4d9c37df1882f375",
"stderr": null,
"stderr_hash": null,
"returncode": 0
}
14 changes: 14 additions & 0 deletions tests/reference/runtime-test_list_item_mixed_print-a3fd49f.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Hello
This is LPython
1 2 3 ... 3 4 5
The first element is: 1
The middle element is: 3
3.14000000000000012e+00 * 2 = 6.28000000000000025e+00
Total: 9.41999999999999993e+00
(1, 2, 3) is a tuple, but 1 is a number.
1 is smaller than 2 is smaller than 3
1 # 2 # 3 # 4 # 5 #

List 0 : [1, 2]
List 1 : [3, 4]
List 2 : [5, 6]
4 changes: 4 additions & 0 deletions tests/tests.toml
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,10 @@ pass = "print_list_tuple"
filename = "../integration_tests/print_04.py"
llvm = true

[[test]]
filename = "../integration_tests/test_list_item_mixed_print.py"
run = true

[[test]]
filename = "../integration_tests/generics_01.py"
asr = true
Expand Down