Skip to content

Commit 79cf023

Browse files
committed
Apply suggestions from code review
1 parent 0b7203e commit 79cf023

20 files changed

+89
-41
lines changed

src/libasr/asr_utils.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ static inline std::string binop_to_str(const ASR::binopType t) {
159159
case (ASR::binopType::Sub): { return " - "; }
160160
case (ASR::binopType::Mul): { return "*"; }
161161
case (ASR::binopType::Div): { return "/"; }
162-
default : throw LFortranException("Not implemented");
162+
default : throw LFortranException("Cannot represent the binary operator as a string");
163163
}
164164
}
165165

@@ -171,7 +171,7 @@ static inline std::string cmpop_to_str(const ASR::cmpopType t) {
171171
case (ASR::cmpopType::LtE): { return " <= "; }
172172
case (ASR::cmpopType::Gt): { return " > "; }
173173
case (ASR::cmpopType::GtE): { return " >= "; }
174-
default : throw LFortranException("Not implemented");
174+
default : throw LFortranException("Cannot represent the comparison as a string");
175175
}
176176
}
177177

@@ -181,7 +181,7 @@ static inline std::string boolop_to_str(const ASR::boolopType t) {
181181
case (ASR::boolopType::Or): { return " || "; }
182182
case (ASR::boolopType::Eqv): { return " == "; }
183183
case (ASR::boolopType::NEqv): { return " != "; }
184-
default : throw LFortranException("Not implemented");
184+
default : throw LFortranException("Cannot represent the boolean operator as a string");
185185
}
186186
}
187187

src/libasr/codegen/asr_to_cpp.cpp

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,7 @@ Kokkos::View<T*> from_std_vector(const std::vector<T> &v)
519519
}
520520
src = fn_name + "(" + args + ")";
521521
}
522+
last_expr_precedence = 2;
522523
}
523524

524525
void visit_Assignment(const ASR::Assignment_t &x) {
@@ -539,18 +540,22 @@ Kokkos::View<T*> from_std_vector(const std::vector<T> &v)
539540

540541
void visit_ConstantInteger(const ASR::ConstantInteger_t &x) {
541542
src = std::to_string(x.m_n);
543+
last_expr_precedence = 2;
542544
}
543545

544546
void visit_ConstantReal(const ASR::ConstantReal_t &x) {
545547
src = std::to_string(x.m_r);
548+
last_expr_precedence = 2;
546549
}
547550

548551
void visit_ConstantString(const ASR::ConstantString_t &x) {
549552
src = "\"" + std::string(x.m_s) + "\"";
553+
last_expr_precedence = 2;
550554
}
551555

552556
void visit_ConstantComplex(const ASR::ConstantComplex_t &x) {
553557
src = "{" + std::to_string(x.m_re) + ", " + std::to_string(x.m_im) + "}";
558+
last_expr_precedence = 2;
554559
}
555560

556561
void visit_ConstantLogical(const ASR::ConstantLogical_t &x) {
@@ -559,6 +564,7 @@ Kokkos::View<T*> from_std_vector(const std::vector<T> &v)
559564
} else {
560565
src = "false";
561566
}
567+
last_expr_precedence = 2;
562568
}
563569

564570
void visit_ConstantSet(const ASR::ConstantSet_t &x) {
@@ -571,11 +577,13 @@ Kokkos::View<T*> from_std_vector(const std::vector<T> &v)
571577
}
572578
out += "}";
573579
src = out;
580+
last_expr_precedence = 2;
574581
}
575582

576583
void visit_Var(const ASR::Var_t &x) {
577584
const ASR::symbol_t *s = ASRUtils::symbol_get_past_external(x.m_v);
578585
src = ASR::down_cast<ASR::Variable_t>(s)->m_name;
586+
last_expr_precedence = 2;
579587
}
580588

581589
void visit_ArrayRef(const ASR::ArrayRef_t &x) {
@@ -592,6 +600,7 @@ Kokkos::View<T*> from_std_vector(const std::vector<T> &v)
592600
if (i < x.n_args-1) out += ", ";
593601
}
594602
out += "-1]";
603+
last_expr_precedence = 2;
595604
src = out;
596605
}
597606

@@ -608,6 +617,7 @@ Kokkos::View<T*> from_std_vector(const std::vector<T> &v)
608617
}
609618
out += "}";
610619
src = out;
620+
last_expr_precedence = 2;
611621
}
612622

613623
void visit_ImplicitCast(const ASR::ImplicitCast_t &x) {
@@ -637,6 +647,7 @@ Kokkos::View<T*> from_std_vector(const std::vector<T> &v)
637647
}
638648
default : throw CodeGenError("Cast kind " + std::to_string(x.m_kind) + " not implemented");
639649
}
650+
last_expr_precedence = 2;
640651
}
641652

642653
void visit_Compare(const ASR::Compare_t &x) {
@@ -670,33 +681,67 @@ Kokkos::View<T*> from_std_vector(const std::vector<T> &v)
670681

671682
void visit_UnaryOp(const ASR::UnaryOp_t &x) {
672683
this->visit_expr(*x.m_operand);
684+
int expr_precedence = last_expr_precedence;
673685
if (x.m_type->type == ASR::ttypeType::Integer) {
674686
if (x.m_op == ASR::unaryopType::UAdd) {
675687
// src = src;
688+
// Skip unary plus, keep the previous precedence
676689
} else if (x.m_op == ASR::unaryopType::USub) {
677-
src = "-" + src;
690+
last_expr_precedence = 3;
691+
if (expr_precedence <= last_expr_precedence) {
692+
src = "-" + src;
693+
} else {
694+
src = "-(" + src + ")";
695+
}
678696
} else if (x.m_op == ASR::unaryopType::Invert) {
679-
src = "~" + src;
697+
last_expr_precedence = 3;
698+
if (expr_precedence <= last_expr_precedence) {
699+
src = "~" + src;
700+
} else {
701+
src = "~(" + src + ")";
702+
}
703+
680704
} else if (x.m_op == ASR::unaryopType::Not) {
681-
src = "!" + src;
705+
last_expr_precedence = 3;
706+
if (expr_precedence <= last_expr_precedence) {
707+
src = "!" + src;
708+
} else {
709+
src = "!(" + src + ")";
710+
}
682711
} else {
683712
throw CodeGenError("Unary type not implemented yet for Integer");
684713
}
685714
return;
686715
} else if (x.m_type->type == ASR::ttypeType::Real) {
687716
if (x.m_op == ASR::unaryopType::UAdd) {
688717
// src = src;
718+
// Skip unary plus, keep the previous precedence
689719
} else if (x.m_op == ASR::unaryopType::USub) {
690-
src = "-" + src;
720+
last_expr_precedence = 3;
721+
if (expr_precedence <= last_expr_precedence) {
722+
src = "-" + src;
723+
} else {
724+
src = "-(" + src + ")";
725+
}
691726
} else if (x.m_op == ASR::unaryopType::Not) {
692-
src = "!" + src;
727+
last_expr_precedence = 3;
728+
if (expr_precedence <= last_expr_precedence) {
729+
src = "!" + src;
730+
} else {
731+
src = "!(" + src + ")";
732+
}
693733
} else {
694734
throw CodeGenError("Unary type not implemented yet for Real");
695735
}
696736
return;
697737
} else if (x.m_type->type == ASR::ttypeType::Logical) {
698738
if (x.m_op == ASR::unaryopType::Not) {
699-
src = "!" + src;
739+
last_expr_precedence = 3;
740+
if (expr_precedence <= last_expr_precedence) {
741+
src = "!" + src;
742+
} else {
743+
src = "!(" + src + ")";
744+
}
700745
return;
701746
} else {
702747
throw CodeGenError("Unary type not implemented yet for Logical");
@@ -817,6 +862,7 @@ Kokkos::View<T*> from_std_vector(const std::vector<T> &v)
817862
}
818863
out += "})";
819864
src = out;
865+
last_expr_precedence = 2;
820866
}
821867

822868
void visit_Print(const ASR::Print_t &x) {
@@ -958,6 +1004,7 @@ Kokkos::View<T*> from_std_vector(const std::vector<T> &v)
9581004
std::string indent(indentation_level*indentation_spaces, ' ');
9591005
std::string out = indent + " /* FIXME: implied do loop */ ";
9601006
src = out;
1007+
last_expr_precedence = 2;
9611008
}
9621009

9631010
void visit_DoLoop(const ASR::DoLoop_t &x) {
@@ -1077,6 +1124,7 @@ Kokkos::View<T*> from_std_vector(const std::vector<T> &v)
10771124
visit_expr(*x.m_orelse);
10781125
out += src + ")";
10791126
src = out;
1127+
last_expr_precedence = 16;
10801128
}
10811129

10821130
void visit_SubroutineCall(const ASR::SubroutineCall_t &x) {

tests/reference/cpp-assert1-ba60925.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "cpp-assert1-ba60925.stdout",
9-
"stdout_hash": "14c793b7b19362e829cdff759adedf2ae156cef0e663cca99a751cc0",
9+
"stdout_hash": "341ab8b76eaf0af8a4b2d8b901fdf11fb8e3e3bfc541ed0dc078d5a7",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

tests/reference/cpp-assert1-ba60925.stdout

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ void test_assert()
2020
{
2121
int a;
2222
a = 5;
23-
assert (("a is not 5", (a) == (5)));
23+
assert (("a is not 5", a == 5));
2424
assert (a != 10);
2525
}
2626

tests/reference/cpp-doconcurrentloop_01-4e9f274.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "cpp-doconcurrentloop_01-4e9f274.stdout",
9-
"stdout_hash": "cd85c3a1a360f70db5ee96b33a26258c8f985b73534e4c766128b79e",
9+
"stdout_hash": "ebbc468b9812b751d5770f98b4f9ea7772a30af7a9b2d30852c302ff",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

tests/reference/cpp-doconcurrentloop_01-4e9f274.stdout

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ void main0()
3030
float scalar;
3131
scalar = 10.000000;
3232
nsize = 1234;
33-
Kokkos::parallel_for(Kokkos::RangePolicy<Kokkos::DefaultExecutionSpace>(1, (nsize) - (1)+1), KOKKOS_LAMBDA(const long i) {
33+
Kokkos::parallel_for(Kokkos::RangePolicy<Kokkos::DefaultExecutionSpace>(1, nsize - 1+1), KOKKOS_LAMBDA(const long i) {
3434
a[i-1] = 5.000000;
3535
b[i-1] = 5.000000;
3636
});
@@ -42,8 +42,8 @@ void triad(const Kokkos::View<float*> &a, const Kokkos::View<float*> &b, float s
4242
{
4343
int N;
4444
N = 1234;
45-
Kokkos::parallel_for(Kokkos::RangePolicy<Kokkos::DefaultExecutionSpace>(1, N - (1)+1), KOKKOS_LAMBDA(const long i) {
46-
c[i-1] = a[i-1] + (scalar)*(b[i-1]);
45+
Kokkos::parallel_for(Kokkos::RangePolicy<Kokkos::DefaultExecutionSpace>(1, N - 1+1), KOKKOS_LAMBDA(const long i) {
46+
c[i-1] = a[i-1] + scalar*b[i-1];
4747
});
4848
}
4949

tests/reference/cpp-expr2-09c05ad.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "cpp-expr2-09c05ad.stdout",
9-
"stdout_hash": "fa6bfa261838534311f57b41cc1b67a574675a8286eb616a4ee28b7e",
9+
"stdout_hash": "e47a4114241b0783c7f5e9fbda982d0af447f1cc14d18fc85253ec05",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

tests/reference/cpp-expr2-09c05ad.stdout

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ void test_boolOp()
2222
bool b;
2323
a = false;
2424
b = true;
25-
a = (a) && (b);
25+
a = a && b;
2626
b = a || true;
2727
a = a || b;
28-
a = (a) && (b) == (b);
29-
a = a && (b) != (b);
28+
a = a && b == b;
29+
a = a && b != b;
3030
a = b || b;
3131
}
3232

tests/reference/cpp-expr3-9c516d4.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "cpp-expr3-9c516d4.stdout",
9-
"stdout_hash": "fe9bf45e7eabfcf0a8491dd85ae9d569a878909ad0455062207e1a94",
9+
"stdout_hash": "1d13d271de569db70607c1811c0c719440ae26a7fd5a357c8910a78a",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

tests/reference/cpp-expr3-9c516d4.stdout

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ void test_cast()
2222
float b;
2323
a = 2;
2424
b = 4.200000;
25-
a = ((float)(a))*(b);
25+
a = (float)(a)*b;
2626
b = b + (float)(1);
2727
a = 5;
28-
a = (float)(a) - (3.900000);
29-
a = ((float)(a))/(b);
28+
a = (float)(a) - 3.900000;
29+
a = (float)(a)/b;
3030
b = (float)(3)/(float)(4);
3131
if ((float)(a) < b) {
3232
std::cout << "a < b" << std::endl;

tests/reference/cpp-expr6-f337f4f.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "cpp-expr6-f337f4f.stdout",
9-
"stdout_hash": "98c4ae8b307378dcfcc21a4d2d39f5cbb4d560e1f04bb0eedde32ce6",
9+
"stdout_hash": "1171d1e704952c3d465082fbde015a10f9bc7dc675357b66e6080211",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

tests/reference/cpp-expr6-f337f4f.stdout

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ void test_ifexp()
2222
int b;
2323
bool c;
2424
a = 2;
25-
b = ((a) == (2)) ? (6) : (8);
26-
c = ((b) > (5)) ? (true) : (false);
25+
b = (a == 2) ? (6) : (8);
26+
c = (b > 5) ? (true) : (false);
2727
}
2828

2929
namespace {

tests/reference/cpp-expr8-704cece.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "cpp-expr8-704cece.stdout",
9-
"stdout_hash": "7ee2bbf33a887c51a69b8a04620a44e888e43e3eae7dda60c0fa0902",
9+
"stdout_hash": "8201234d9cf1e4521f7ef37c1ef3532c18044040aff13375e18ed5ad",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

tests/reference/cpp-expr8-704cece.stdout

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ void test_binop()
2222
float x2;
2323
x = std::pow(2, 3);
2424
x = std::pow((float)(2), 3.500000);
25-
x = (54) - (100);
26-
x2 = 3.454000 - (765.430000) + 534.600000;
27-
x2 = (5346.565000)*(3.450000);
25+
x = 54 - 100;
26+
x2 = 3.454000 - 765.430000 + 534.600000;
27+
x2 = 5346.565000*3.450000;
2828
x2 = std::pow(5346.565000, 3.450000);
2929
}
3030

tests/reference/cpp-loop1-0a8cf3b.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "cpp-loop1-0a8cf3b.stdout",
9-
"stdout_hash": "e596712869c18025d8e9993c6207a63136f83ca10e84cdd8f46a7db7",
9+
"stdout_hash": "4b9970204be628f2b99634dbc980e6219bda60ec7dd4a6f3860335fe",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

tests/reference/cpp-loop1-0a8cf3b.stdout

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ int test_factorial_1(int x)
3434
{
3535
int _lpython_return_variable;
3636
int result;
37-
if ((x) < (0)) {
37+
if (x < 0) {
3838
_lpython_return_variable = 0;
3939
return _lpython_return_variable;
4040
}
4141
result = 1;
4242
while (x > 0) {
43-
result = (result)*(x);
43+
result = result*x;
4444
x = x - 1;
4545
}
4646
_lpython_return_variable = result;
@@ -53,8 +53,8 @@ int test_factorial_2(int x)
5353
int i;
5454
int result;
5555
result = 1;
56-
for (i=1; i<=x + 1 - (1); i++) {
57-
result = (result)*(i);
56+
for (i=1; i<=x + 1 - 1; i++) {
57+
result = result*i;
5858
}
5959
_lpython_return_variable = result;
6060
return _lpython_return_variable;
@@ -70,7 +70,7 @@ long long test_factorial_3(int x)
7070
}
7171
result = 1;
7272
while (x > 0) {
73-
result = (result)*(x);
73+
result = result*x;
7474
x = x - 1;
7575
}
7676
_lpython_return_variable = result;

tests/reference/cpp-loop2-0686fc4.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "cpp-loop2-0686fc4.stdout",
9-
"stdout_hash": "d8b2dfb87470779026f1a7b6fba2764d9f6c0f9fdd87645fe7f64e27",
9+
"stdout_hash": "eef4f1ff5b5b8880a2e873cd82808099e1f3612117a10667599f5d62",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

0 commit comments

Comments
 (0)