Skip to content

Commit

Permalink
Merge pull request #3224 from Shaikh-Ubaid/fix_print
Browse files Browse the repository at this point in the history
Fix print related bugs
  • Loading branch information
Shaikh-Ubaid committed Jan 26, 2024
2 parents 32f2dfe + 34caf23 commit 6712771
Show file tree
Hide file tree
Showing 34 changed files with 410 additions and 358 deletions.
53 changes: 38 additions & 15 deletions src/lfortran/semantics/ast_body_visitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,7 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
*args[i] = ASRUtils::EXPR(tmp);
}
}
std::vector<ASR::asr_t*> newline_for_advance;
for( std::uint32_t i = 0; i < n_kwargs; i++ ) {
AST::kw_argstar_t kwarg = m_kwargs[i];
std::string m_arg_str(kwarg.m_arg);
Expand Down Expand Up @@ -569,7 +570,7 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
nullptr, 0, nullptr, newline)));
// TODO: Compare with "no" (case-insensitive) in else part
// Throw runtime error if advance expression does not match "no"
tmp_vec.push_back(ASR::make_If_t(al, loc, test, body.p,
newline_for_advance.push_back(ASR::make_If_t(al, loc, test, body.p,
body.size(), nullptr, 0));
a_end = empty;
}
Expand All @@ -579,15 +580,18 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
throw SemanticError(R"""(List directed format(*) is not allowed with a ADVANCE= specifier)""",
loc);
}
if (_type == AST::stmtType::Write) {
a_fmt_constant = a_fmt;
} else if (_type == AST::stmtType::Write && a_fmt == nullptr
if (_type == AST::stmtType::Write && a_fmt == nullptr
&& compiler_options.print_leading_space) {
ASR::ttype_t *str_type_len_0 = ASRUtils::TYPE(ASR::make_Character_t(
al, loc, 1, 0, nullptr));
ASR::expr_t *empty_string = ASRUtils::EXPR(ASR::make_StringConstant_t(
al, loc, s2c(al, ""), str_type_len_0));
a_values_vec.push_back(al, empty_string);
ASR::asr_t* file_write_asr_t = construct_leading_space(false, loc);
ASR::FileWrite_t* file_write = ASR::down_cast<ASR::FileWrite_t>(ASRUtils::STMT(file_write_asr_t));
file_write->m_id = a_id;
file_write->m_iomsg = a_iomsg;
file_write->m_iostat = a_iostat;
file_write->m_unit = a_unit;
file_write->m_label = m_label;
tmp_vec.push_back(file_write_asr_t);
} else if (_type == AST::stmtType::Write) {
a_fmt_constant = a_fmt;
}
for( std::uint32_t i = 0; i < n_values; i++ ) {
this->visit_expr(*m_values[i]);
Expand Down Expand Up @@ -642,7 +646,8 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
}
}

tmp_vec.insert(tmp_vec.begin(), tmp);
tmp_vec.push_back(tmp);
tmp_vec.insert(tmp_vec.end(), newline_for_advance.begin(), newline_for_advance.end());
tmp = nullptr;
}

Expand Down Expand Up @@ -2792,6 +2797,28 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
throw LCompilersException("Argument not found");
}

ASR::asr_t* construct_leading_space(bool print, const Location &loc) {
ASR::ttype_t *str_type_len_0 = ASRUtils::TYPE(ASR::make_Character_t(
al, loc, 1, 0, nullptr));
ASR::expr_t *empty_string = ASRUtils::EXPR(ASR::make_StringConstant_t(
al, loc, s2c(al, ""), str_type_len_0));
ASR::ttype_t *str_type_len_1 = ASRUtils::TYPE(ASR::make_Character_t(
al, loc, 1, 1, nullptr));
ASR::expr_t *space = ASRUtils::EXPR(ASR::make_StringConstant_t(
al, loc, s2c(al, " "), str_type_len_1));
Vec<ASR::expr_t*> args;
args.reserve(al, 1);
args.push_back(al, space);

if (print) {
return ASR::make_Print_t(al, loc,
args.p, args.size(), nullptr, empty_string);
} else {
return ASR::make_FileWrite_t(al, loc, 0, nullptr, nullptr,
nullptr, nullptr, args.p, args.size(), nullptr, empty_string);
}
}

void visit_Print(const AST::Print_t &x) {
Vec<ASR::expr_t*> body;
body.reserve(al, x.n_values);
Expand All @@ -2802,11 +2829,7 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
fmt = ASRUtils::EXPR(tmp);
} else {
if (compiler_options.print_leading_space) {
ASR::ttype_t *str_type_len_0 = ASRUtils::TYPE(ASR::make_Character_t(
al, x.base.base.loc, 1, 0, nullptr));
ASR::expr_t *empty_string = ASRUtils::EXPR(ASR::make_StringConstant_t(
al, x.base.base.loc, s2c(al, ""), str_type_len_0));
body.push_back(al, empty_string);
current_body->push_back(al, ASRUtils::STMT(construct_leading_space(true, x.base.base.loc)));
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/libasr/asr_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1441,7 +1441,7 @@ void make_ArrayBroadcast_t_util(Allocator& al, const Location& loc,
Vec<ASR::expr_t*> shape_args;
shape_args.reserve(al, 1);
shape_args.push_back(al, expr1);
bool is_value_character_array = ASR::is_a<ASR::Character_t>(*ASRUtils::type_get_past_array(ASRUtils::type_get_past_allocatable(ASRUtils::expr_type(expr2))));
bool is_value_character_array = ASRUtils::is_character(*ASRUtils::expr_type(expr2));

Vec<ASR::dimension_t> dims;
dims.reserve(al, 1);
Expand Down Expand Up @@ -1564,7 +1564,7 @@ int64_t compute_leading_zeros(int64_t number, int64_t kind) {
if (number%2 == 0) {
leading_zeros++;
} else {
leading_zeros = 0;
leading_zeros = 0;
}
number = number/2;
total_bits--;
Expand Down
4 changes: 3 additions & 1 deletion src/libasr/codegen/asr_to_llvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7770,7 +7770,9 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
for (size_t i=0; i<x.n_values; i++) {
if (i != 0) {
fmt.push_back("%s");
if (global_sep_space && !ASR::is_a<ASR::Character_t>(*ASRUtils::type_get_past_allocatable(ASRUtils::type_get_past_array(ASRUtils::expr_type(x.m_values[i-1]))))) {
if (global_sep_space &&
!(ASRUtils::is_character(*ASRUtils::expr_type(x.m_values[i]))
&& ASRUtils::is_character(*ASRUtils::expr_type(x.m_values[i - 1])))) {
args.push_back(sep);
} else {
sep_no_space = sep_no_space != nullptr ? sep_no_space : builder->CreateGlobalStringPtr("");
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/asr-print3-5f4fc26.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outfile": null,
"outfile_hash": null,
"stdout": "asr-print3-5f4fc26.stdout",
"stdout_hash": "6048701ca9858c74f2376251ba655c5f775af9692dc769236951a0ba",
"stdout_hash": "ca47f6102543449b6d2a0ae1ee52a4beca2b8b475dedb33c24a7b465",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
50 changes: 48 additions & 2 deletions tests/reference/asr-print3-5f4fc26.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,40 @@
)
(Print
[(StringConstant
" "
(Character 1 1 ())
)]
()
(StringConstant
""
(Character 1 0 ())
)
(StringConstant
)
(Print
[(StringConstant
"x is "
(Character 1 5 ())
)
(Var 2 x)]
()
()
)
(FileWrite
0
()
()
()
()
[(StringConstant
" "
(Character 1 1 ())
)]
()
(StringConstant
""
(Character 1 0 ())
)
)
(FileWrite
0
()
Expand All @@ -60,16 +83,39 @@
)
(Print
[(StringConstant
" "
(Character 1 1 ())
)]
()
(StringConstant
""
(Character 1 0 ())
)
(StringConstant
)
(Print
[(StringConstant
"ok"
(Character 1 2 ())
)]
()
()
)
(FileWrite
0
()
()
()
()
[(StringConstant
" "
(Character 1 1 ())
)]
()
(StringConstant
""
(Character 1 0 ())
)
)
(FileWrite
0
()
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/llvm-case_02-a38c2d8.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outfile": null,
"outfile_hash": null,
"stdout": "llvm-case_02-a38c2d8.stdout",
"stdout_hash": "076935608adff38e844b67a2460187766248daf42ea9fe1163ecb652",
"stdout_hash": "240f92ac3f84d11d9dd23f6542676bb14bb411fce5bbbf7db91fc312",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down

0 comments on commit 6712771

Please sign in to comment.