diff --git a/integration_tests/CMakeLists.txt b/integration_tests/CMakeLists.txt index df22150e2a..953d778624 100644 --- a/integration_tests/CMakeLists.txt +++ b/integration_tests/CMakeLists.txt @@ -228,6 +228,7 @@ RUN(NAME exit_02c FAIL LABELS cpython llvm c) RUN(NAME print_01 LABELS cpython llvm c wasm) # wasm not yet supports sep and end keywords RUN(NAME print_03 LABELS x86 c wasm wasm_x86 wasm_x64) # simple test case specifically for x86, wasm_x86 and wasm_x64 RUN(NAME print_04 LABELS cpython llvm c) +RUN(NAME print_06 LABELS cpython llvm c) RUN(NAME print_05 LABELS cpython llvm wasm wasm_x64) RUN(NAME print_float LABELS cpython llvm wasm wasm_x64) RUN(NAME print_list_tuple LABELS cpython c) # TODO: llvm doesn't completely support printing list diff --git a/integration_tests/print_06.py b/integration_tests/print_06.py new file mode 100644 index 0000000000..e3f05cf7f8 --- /dev/null +++ b/integration_tests/print_06.py @@ -0,0 +1,26 @@ +def main0(): + a: i32 = 1 + b: str = "abc" + c: list[i32] = [1, 2] + d: list[i32] = [3, 4] + e: tuple[i32, i32, i32] = (1, 2, 3) + f: tuple[i32, i32] = (4, 5) + print(a, b, c) + print(a, c, b) + print(c, a, b) + print(a, b, c, sep = "pqr") + print(a, c, b, sep = "pqr") + print(c, a, b, sep = "pqr") + print(a, b, c, end = "xyz\n") + print(a, c, b, end = "xyz\n") + print(c, a, b, end = "xyz\n") + print(a, b, c, sep = "pqr", end = "xyz\n") + print(a, c, b, sep = "pqr", end = "xyz\n") + print(c, a, b, sep = "pqr", end = "xyz\n") + # Tring out few cases with Lists and Tuples together + print(c, e) + print(c, d, a, sep = "pqr") + print(c, e, d, end = "xyz\n") + print(c, e, d, f, sep = "pqr", end = "xyz\n") + +main0() \ No newline at end of file diff --git a/src/libasr/pass/print_arr.cpp b/src/libasr/pass/print_arr.cpp index c55e27dc31..4776abc09b 100644 --- a/src/libasr/pass/print_arr.cpp +++ b/src/libasr/pass/print_arr.cpp @@ -60,6 +60,10 @@ class PrintArrVisitor : public PassUtils::PassVisitor ASR::stmt_t* doloop = nullptr; ASR::stmt_t* empty_print_endl = ASRUtils::STMT(ASR::make_Print_t(al, loc, nullptr, nullptr, 0, nullptr, nullptr)); + ASR::ttype_t *str_type_len_1 = ASRUtils::TYPE(ASR::make_Character_t( + al, loc, 1, 1, nullptr, nullptr, 0)); + ASR::expr_t *space = ASRUtils::EXPR(ASR::make_StringConstant_t( + al, loc, s2c(al, " "), str_type_len_1)); for( int i = n_dims - 1; i >= 0; i-- ) { ASR::do_loop_head_t head; head.m_v = idx_vars[i]; @@ -75,7 +79,7 @@ class PrintArrVisitor : public PassUtils::PassVisitor print_args.reserve(al, 1); print_args.push_back(al, ref); ASR::stmt_t* print_stmt = ASRUtils::STMT(ASR::make_Print_t(al, loc, nullptr, - print_args.p, print_args.size(), nullptr, nullptr)); + print_args.p, print_args.size(), nullptr, space)); doloop_body.push_back(al, print_stmt); } else { doloop_body.push_back(al, doloop); @@ -88,9 +92,16 @@ class PrintArrVisitor : public PassUtils::PassVisitor void visit_Print(const ASR::Print_t& x) { std::vector print_body; - ASR::stmt_t* empty_print_endl = ASRUtils::STMT(ASR::make_Print_t(al, x.base.base.loc, - nullptr, nullptr, 0, nullptr, nullptr)); + ASR::stmt_t* empty_print_endl; ASR::stmt_t* print_stmt; + ASR::ttype_t *str_type_len_1 = ASRUtils::TYPE(ASR::make_Character_t( + al, x.base.base.loc, 1, 1, nullptr, nullptr, 0)); + ASR::expr_t *space = ASRUtils::EXPR(ASR::make_StringConstant_t( + al, x.base.base.loc, s2c(al, " "), str_type_len_1)); + ASR::expr_t *backspace = ASRUtils::EXPR(ASR::make_StringConstant_t( + al, x.base.base.loc, s2c(al, "\b"), str_type_len_1)); + ASR::stmt_t* back = ASRUtils::STMT(ASR::make_Print_t(al, x.base.base.loc, + nullptr, nullptr, 0, nullptr, backspace)); for (size_t i=0; i for (size_t j=0; j } print_stmt = ASRUtils::STMT(ASR::make_Print_t( al, x.base.base.loc, nullptr, body.p, body.size(), - nullptr, nullptr)); + x.m_separator, x.m_end)); pass_result.push_back(al, print_stmt); print_body.clear(); } diff --git a/src/libasr/pass/print_list.cpp b/src/libasr/pass/print_list.cpp index ed64d9bda5..1033f8fccb 100644 --- a/src/libasr/pass/print_list.cpp +++ b/src/libasr/pass/print_list.cpp @@ -18,8 +18,7 @@ comma_space, brackets and newline. The function print(a, b, l, sep="pqr", end="xyz") # l is a list (but not a & b) to: - - print(a, b, sep="pqr") + print(a, b, sep="pqr", end="") print("[", end="") for i in range(len(l)): print(l[i], end="") @@ -300,26 +299,61 @@ class PrintListVisitor void visit_Print(const ASR::Print_t &x) { std::vector print_tmp; + ASR::ttype_t *str_type_len_1 = ASRUtils::TYPE(ASR::make_Character_t( + al, x.base.base.loc, 1, 1, nullptr, nullptr, 0)); + ASR::expr_t *space = ASRUtils::EXPR(ASR::make_StringConstant_t( + al, x.base.base.loc, s2c(al, " "), str_type_len_1)); for (size_t i=0; i(*ASRUtils::expr_type(x.m_values[i])) || ASR::is_a(*ASRUtils::expr_type(x.m_values[i]))) { if (!print_tmp.empty()) { Vec tmp_vec; + ASR::stmt_t *print_stmt; tmp_vec.reserve(al, print_tmp.size()); for (auto &e: print_tmp) { tmp_vec.push_back(al, e); } - ASR::stmt_t *print_stmt = ASRUtils::STMT( - ASR::make_Print_t(al, x.base.base.loc, nullptr, tmp_vec.p, tmp_vec.size(), - x.m_separator, nullptr)); + if (x.m_separator) { + print_stmt = ASRUtils::STMT(ASR::make_Print_t(al, + x.base.base.loc, nullptr, tmp_vec.p, tmp_vec.size(), + x.m_separator, x.m_separator)); + } else { + print_stmt = ASRUtils::STMT(ASR::make_Print_t(al, + x.base.base.loc, nullptr, tmp_vec.p, tmp_vec.size(), + x.m_separator, space)); + } print_tmp.clear(); pass_result.push_back(al, print_stmt); - } - if (ASR::is_a(*ASRUtils::expr_type(x.m_values[i]))) - print_list_helper(x.m_values[i], x.m_separator, nullptr, x.base.base.loc); - else - print_tuple_helper(x.m_values[i], x.m_separator, nullptr, x.base.base.loc); + if (ASR::is_a(*ASRUtils::expr_type(x.m_values[i]))){ + if (x.m_separator) { + if (i == x.n_values - 1) { + print_list_helper(x.m_values[i], x.m_separator, x.m_end, x.base.base.loc); + } else { + print_list_helper(x.m_values[i], x.m_separator, x.m_separator, x.base.base.loc); + } + } else { + if (i == x.n_values - 1) { + print_list_helper(x.m_values[i], x.m_separator, x.m_end, x.base.base.loc); + } else { + print_list_helper(x.m_values[i], x.m_separator, space, x.base.base.loc); + } + } + } else { + if (x.m_separator){ + if (i == x.n_values - 1) { + print_tuple_helper(x.m_values[i], x.m_separator, x.m_end, x.base.base.loc); + } else { + print_tuple_helper(x.m_values[i], x.m_separator, x.m_separator, x.base.base.loc); + } + } else { + if (i == x.n_values - 1) { + print_tuple_helper(x.m_values[i], x.m_separator, x.m_end, x.base.base.loc); + } else { + print_tuple_helper(x.m_values[i], x.m_separator, space, x.base.base.loc); + } + } + } for (size_t j=0; j