From 6e2adfc05f8ac952144dee35128ef9f3cb0965fc Mon Sep 17 00:00:00 2001 From: Naman Gera Date: Thu, 3 Feb 2022 18:13:49 +0530 Subject: [PATCH 1/9] Implement `visit_BoolOp` using last precedence --- src/libasr/asr_utils.h | 10 ++++++ src/libasr/codegen/asr_to_cpp.cpp | 39 ++++++++++++++++-------- tests/expr2.py | 3 ++ tests/reference/asr-expr2-2e78a12.json | 4 +-- tests/reference/asr-expr2-2e78a12.stdout | 2 +- tests/reference/ast-expr2-6642d4a.json | 4 +-- tests/reference/ast-expr2-6642d4a.stdout | 2 +- tests/reference/cpp-expr2-09c05ad.json | 4 +-- tests/reference/cpp-expr2-09c05ad.stdout | 7 +++-- 9 files changed, 53 insertions(+), 22 deletions(-) diff --git a/src/libasr/asr_utils.h b/src/libasr/asr_utils.h index a7334a3187..98a364ed44 100644 --- a/src/libasr/asr_utils.h +++ b/src/libasr/asr_utils.h @@ -161,6 +161,16 @@ static inline std::string cmpop_to_str(const ASR::cmpopType t) { } } +static inline std::string boolop_to_str(const ASR::boolopType t) { + switch (t) { + case (ASR::boolopType::And): { return " && "; } + case (ASR::boolopType::Or): { return " || "; } + case (ASR::boolopType::Eqv): { return " == "; } + case (ASR::boolopType::NEqv): { return " != "; } + default : throw LFortranException("Not implemented"); + } +} + static inline ASR::expr_t* expr_value(ASR::expr_t *f) { switch (f->type) { diff --git a/src/libasr/codegen/asr_to_cpp.cpp b/src/libasr/codegen/asr_to_cpp.cpp index 071b50ad7e..a7d2492d93 100644 --- a/src/libasr/codegen/asr_to_cpp.cpp +++ b/src/libasr/codegen/asr_to_cpp.cpp @@ -98,6 +98,9 @@ class ASRToCPPVisitor : public ASR::BaseVisitor std::string src; int indentation_level; int indentation_spaces; + // The precedence of the last expression, using the table: + // https://en.cppreference.com/w/cpp/language/operator_precedence + int last_expr_precedence; bool last_unary_plus; bool last_binary_plus; bool intrinsic_module = false; @@ -781,30 +784,42 @@ Kokkos::View from_std_vector(const std::vector &v) void visit_BoolOp(const ASR::BoolOp_t &x) { this->visit_expr(*x.m_left); - std::string left_val = "(" + src + ")"; + std::string left = std::move(src); + int left_precedence = last_expr_precedence; this->visit_expr(*x.m_right); - std::string right_val = "(" + src + ")"; + std::string right = std::move(src); + int right_precedence = last_expr_precedence; switch (x.m_op) { - case ASR::boolopType::And: { - src = left_val + " && " + right_val; + case (ASR::boolopType::And): { + last_expr_precedence = 14; break; } - case ASR::boolopType::Or: { - src = left_val + " || " + right_val; + case (ASR::boolopType::Or): { + last_expr_precedence = 15; break; } - case ASR::boolopType::NEqv: { - src = left_val + " != " + right_val; + case (ASR::boolopType::NEqv): { + last_expr_precedence = 10; break; } - case ASR::boolopType::Eqv: { - src = left_val + " == " + right_val; + case (ASR::boolopType::Eqv): { + last_expr_precedence = 10; break; } default : throw CodeGenError("Unhandled switch case"); } - last_binary_plus = false; - last_unary_plus = false; + + if (left_precedence >= last_expr_precedence) { + src += left; + } else { + src += "(" + left + ")"; + } + src += ASRUtils::boolop_to_str(x.m_op); + if (right_precedence >= last_expr_precedence) { + src += right; + } else { + src += "(" + right + ")"; + } } void visit_ConstantArray(const ASR::ConstantArray_t &x) { diff --git a/tests/expr2.py b/tests/expr2.py index 5fd66ba605..b26a96cc6e 100644 --- a/tests/expr2.py +++ b/tests/expr2.py @@ -6,3 +6,6 @@ def test_boolOp(): a = a and b b = a or True a = a or b + a = a and b == b + a = a and b != b + a = b or b diff --git a/tests/reference/asr-expr2-2e78a12.json b/tests/reference/asr-expr2-2e78a12.json index b0f2512dd0..fa77462aa1 100644 --- a/tests/reference/asr-expr2-2e78a12.json +++ b/tests/reference/asr-expr2-2e78a12.json @@ -2,11 +2,11 @@ "basename": "asr-expr2-2e78a12", "cmd": "lpython --show-asr --no-color {infile} -o {outfile}", "infile": "tests/expr2.py", - "infile_hash": "cded400dff0cf5559a8f8793a05743a98d813344a6a666799e98e994", + "infile_hash": "52d7d4d33553138f2cf55b9900047e5310c54d62e54b3ca1fa394024", "outfile": null, "outfile_hash": null, "stdout": "asr-expr2-2e78a12.stdout", - "stdout_hash": "3734e76d28a7a236562438ddb6e20143939a7b7e85246fa981aca8bf", + "stdout_hash": "2a0b2181d9a99d83d294a7319d15c6e0b1bce2079c8bd318d423d73b", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/asr-expr2-2e78a12.stdout b/tests/reference/asr-expr2-2e78a12.stdout index 8d83056910..b19b865a60 100644 --- a/tests/reference/asr-expr2-2e78a12.stdout +++ b/tests/reference/asr-expr2-2e78a12.stdout @@ -1 +1 @@ -(TranslationUnit (SymbolTable 1 {test_boolOp: (Subroutine (SymbolTable 2 {a: (Variable 2 a Local () () Default (Logical 1 []) Source Public Required .false.), b: (Variable 2 b Local () () Default (Logical 1 []) Source Public Required .false.)}) test_boolOp [] [(= (Var 2 a) (ConstantLogical .false. (Logical 1 [])) ()) (= (Var 2 b) (ConstantLogical .true. (Logical 1 [])) ()) (= (Var 2 a) (BoolOp (Var 2 a) And (Var 2 b) (Logical 1 []) ()) ()) (= (Var 2 b) (BoolOp (Var 2 a) Or (ConstantLogical .true. (Logical 1 [])) (Logical 1 []) ()) ()) (= (Var 2 a) (BoolOp (Var 2 a) Or (Var 2 b) (Logical 1 []) ()) ())] Source Public Implementation () .false. .false.)}) []) +(TranslationUnit (SymbolTable 1 {test_boolOp: (Subroutine (SymbolTable 2 {a: (Variable 2 a Local () () Default (Logical 1 []) Source Public Required .false.), b: (Variable 2 b Local () () Default (Logical 1 []) Source Public Required .false.)}) test_boolOp [] [(= (Var 2 a) (ConstantLogical .false. (Logical 1 [])) ()) (= (Var 2 b) (ConstantLogical .true. (Logical 1 [])) ()) (= (Var 2 a) (BoolOp (Var 2 a) And (Var 2 b) (Logical 1 []) ()) ()) (= (Var 2 b) (BoolOp (Var 2 a) Or (ConstantLogical .true. (Logical 1 [])) (Logical 1 []) ()) ()) (= (Var 2 a) (BoolOp (Var 2 a) Or (Var 2 b) (Logical 1 []) ()) ()) (= (Var 2 a) (BoolOp (Var 2 a) And (Compare (Var 2 b) Eq (Var 2 b) (Logical 4 []) () ()) (Logical 1 []) ()) ()) (= (Var 2 a) (BoolOp (Var 2 a) And (Compare (Var 2 b) NotEq (Var 2 b) (Logical 4 []) () ()) (Logical 1 []) ()) ()) (= (Var 2 a) (BoolOp (Var 2 b) Or (Var 2 b) (Logical 1 []) ()) ())] Source Public Implementation () .false. .false.)}) []) diff --git a/tests/reference/ast-expr2-6642d4a.json b/tests/reference/ast-expr2-6642d4a.json index e9d60e2c24..a322e2573e 100644 --- a/tests/reference/ast-expr2-6642d4a.json +++ b/tests/reference/ast-expr2-6642d4a.json @@ -2,11 +2,11 @@ "basename": "ast-expr2-6642d4a", "cmd": "lpython --show-ast --no-color {infile} -o {outfile}", "infile": "tests/expr2.py", - "infile_hash": "cded400dff0cf5559a8f8793a05743a98d813344a6a666799e98e994", + "infile_hash": "52d7d4d33553138f2cf55b9900047e5310c54d62e54b3ca1fa394024", "outfile": null, "outfile_hash": null, "stdout": "ast-expr2-6642d4a.stdout", - "stdout_hash": "45b0ac96530a1d80511113a6e59899c272ccda2d40bfa3ad71a1239b", + "stdout_hash": "c1b1fdf1c3bb69063de9c60f0398a89d5e8cba79ed6949093d0ee2fa", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/ast-expr2-6642d4a.stdout b/tests/reference/ast-expr2-6642d4a.stdout index 33b1b59719..57c3669931 100644 --- a/tests/reference/ast-expr2-6642d4a.stdout +++ b/tests/reference/ast-expr2-6642d4a.stdout @@ -1 +1 @@ -(Module [(FunctionDef test_boolOp ([] [] [] [] [] [] []) [(AnnAssign (Name a Store) (Name bool Load) () 1) (AnnAssign (Name b Store) (Name bool Load) () 1) (Assign [(Name a Store)] (ConstantBool .false. ()) ()) (Assign [(Name b Store)] (ConstantBool .true. ()) ()) (Assign [(Name a Store)] (BoolOp And [(Name a Load) (Name b Load)]) ()) (Assign [(Name b Store)] (BoolOp Or [(Name a Load) (ConstantBool .true. ())]) ()) (Assign [(Name a Store)] (BoolOp Or [(Name a Load) (Name b Load)]) ())] [] () ())] []) +(Module [(FunctionDef test_boolOp ([] [] [] [] [] [] []) [(AnnAssign (Name a Store) (Name bool Load) () 1) (AnnAssign (Name b Store) (Name bool Load) () 1) (Assign [(Name a Store)] (ConstantBool .false. ()) ()) (Assign [(Name b Store)] (ConstantBool .true. ()) ()) (Assign [(Name a Store)] (BoolOp And [(Name a Load) (Name b Load)]) ()) (Assign [(Name b Store)] (BoolOp Or [(Name a Load) (ConstantBool .true. ())]) ()) (Assign [(Name a Store)] (BoolOp Or [(Name a Load) (Name b Load)]) ()) (Assign [(Name a Store)] (BoolOp And [(Name a Load) (Compare (Name b Load) Eq [(Name b Load)])]) ()) (Assign [(Name a Store)] (BoolOp And [(Name a Load) (Compare (Name b Load) NotEq [(Name b Load)])]) ()) (Assign [(Name a Store)] (BoolOp Or [(Name b Load) (Name b Load)]) ())] [] () ())] []) diff --git a/tests/reference/cpp-expr2-09c05ad.json b/tests/reference/cpp-expr2-09c05ad.json index b820444ffc..9f5f11270c 100644 --- a/tests/reference/cpp-expr2-09c05ad.json +++ b/tests/reference/cpp-expr2-09c05ad.json @@ -2,11 +2,11 @@ "basename": "cpp-expr2-09c05ad", "cmd": "lpython --no-color --show-cpp {infile}", "infile": "tests/expr2.py", - "infile_hash": "cded400dff0cf5559a8f8793a05743a98d813344a6a666799e98e994", + "infile_hash": "52d7d4d33553138f2cf55b9900047e5310c54d62e54b3ca1fa394024", "outfile": null, "outfile_hash": null, "stdout": "cpp-expr2-09c05ad.stdout", - "stdout_hash": "7aa3d8c9e2d007628b35f4480f92275a4d91348ebf8dfc1e125138ec", + "stdout_hash": "c766eb3e9858298cfaea83251e22cc1333118db59bfb3e0be7c51a12", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/cpp-expr2-09c05ad.stdout b/tests/reference/cpp-expr2-09c05ad.stdout index 5368599937..e89ab9baf6 100644 --- a/tests/reference/cpp-expr2-09c05ad.stdout +++ b/tests/reference/cpp-expr2-09c05ad.stdout @@ -22,8 +22,11 @@ void test_boolOp() bool b; a = false; b = true; - a = (a) && (b); + a = a && b; b = (a) || (true); - a = (a) || (b); + a = a || b; + a = a && b == b; + a = a && b != b; + a = (b) || (b); } From 098028de8f5b1760b0b2dda08a4c207c106912d5 Mon Sep 17 00:00:00 2001 From: Naman Gera Date: Thu, 3 Feb 2022 18:39:27 +0530 Subject: [PATCH 2/9] Apply suggestions --- src/libasr/codegen/asr_to_cpp.cpp | 4 ++-- tests/reference/cpp-expr2-09c05ad.json | 2 +- tests/reference/cpp-expr2-09c05ad.stdout | 8 ++++---- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/libasr/codegen/asr_to_cpp.cpp b/src/libasr/codegen/asr_to_cpp.cpp index a7d2492d93..023db60ac6 100644 --- a/src/libasr/codegen/asr_to_cpp.cpp +++ b/src/libasr/codegen/asr_to_cpp.cpp @@ -809,13 +809,13 @@ Kokkos::View from_std_vector(const std::vector &v) default : throw CodeGenError("Unhandled switch case"); } - if (left_precedence >= last_expr_precedence) { + if (left_precedence <= last_expr_precedence) { src += left; } else { src += "(" + left + ")"; } src += ASRUtils::boolop_to_str(x.m_op); - if (right_precedence >= last_expr_precedence) { + if (right_precedence <= last_expr_precedence) { src += right; } else { src += "(" + right + ")"; diff --git a/tests/reference/cpp-expr2-09c05ad.json b/tests/reference/cpp-expr2-09c05ad.json index 9f5f11270c..8b2bac92fe 100644 --- a/tests/reference/cpp-expr2-09c05ad.json +++ b/tests/reference/cpp-expr2-09c05ad.json @@ -6,7 +6,7 @@ "outfile": null, "outfile_hash": null, "stdout": "cpp-expr2-09c05ad.stdout", - "stdout_hash": "c766eb3e9858298cfaea83251e22cc1333118db59bfb3e0be7c51a12", + "stdout_hash": "8e8aecb3c46b493cc5c81e215206858638e82408c8c9fb210a0c8a4a", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/cpp-expr2-09c05ad.stdout b/tests/reference/cpp-expr2-09c05ad.stdout index e89ab9baf6..5b7694f9ff 100644 --- a/tests/reference/cpp-expr2-09c05ad.stdout +++ b/tests/reference/cpp-expr2-09c05ad.stdout @@ -22,11 +22,11 @@ void test_boolOp() bool b; a = false; b = true; - a = a && b; - b = (a) || (true); + a = (a) && (b); + b = a || true; a = a || b; - a = a && b == b; + a = (a) && (b == b); a = a && b != b; - a = (b) || (b); + a = b || b; } From d6460707d3f24eaa565f5f3c93cf03baddd39597 Mon Sep 17 00:00:00 2001 From: Naman Gera Date: Thu, 3 Feb 2022 19:03:51 +0530 Subject: [PATCH 3/9] Modify visitor: `visit_Compare` --- src/libasr/asr_utils.h | 12 ++--- src/libasr/codegen/asr_to_cpp.cpp | 53 ++++++++++------------ tests/reference/cpp-assert1-ba60925.json | 2 +- tests/reference/cpp-assert1-ba60925.stdout | 2 +- tests/reference/cpp-expr2-09c05ad.json | 2 +- tests/reference/cpp-expr2-09c05ad.stdout | 4 +- tests/reference/cpp-expr6-f337f4f.json | 2 +- tests/reference/cpp-expr6-f337f4f.stdout | 4 +- tests/reference/cpp-loop1-0a8cf3b.json | 2 +- tests/reference/cpp-loop1-0a8cf3b.stdout | 2 +- tests/reference/cpp-loop2-0686fc4.json | 2 +- tests/reference/cpp-loop2-0686fc4.stdout | 4 +- tests/reference/cpp-loop3-6020091.json | 2 +- tests/reference/cpp-loop3-6020091.stdout | 2 +- 14 files changed, 44 insertions(+), 51 deletions(-) diff --git a/src/libasr/asr_utils.h b/src/libasr/asr_utils.h index 98a364ed44..1e1a4436e7 100644 --- a/src/libasr/asr_utils.h +++ b/src/libasr/asr_utils.h @@ -151,12 +151,12 @@ static inline std::string unop_to_str(const ASR::unaryopType t) { static inline std::string cmpop_to_str(const ASR::cmpopType t) { switch (t) { - case (ASR::cmpopType::Eq): { return "=="; } - case (ASR::cmpopType::NotEq): { return "!="; } - case (ASR::cmpopType::Lt): { return "<"; } - case (ASR::cmpopType::LtE): { return "<="; } - case (ASR::cmpopType::Gt): { return ">"; } - case (ASR::cmpopType::GtE): { return ">="; } + case (ASR::cmpopType::Eq): { return " == "; } + case (ASR::cmpopType::NotEq): { return " != "; } + case (ASR::cmpopType::Lt): { return " < "; } + case (ASR::cmpopType::LtE): { return " <= "; } + case (ASR::cmpopType::Gt): { return " > "; } + case (ASR::cmpopType::GtE): { return " >= "; } default : throw LFortranException("Not implemented"); } } diff --git a/src/libasr/codegen/asr_to_cpp.cpp b/src/libasr/codegen/asr_to_cpp.cpp index 023db60ac6..996c7c5579 100644 --- a/src/libasr/codegen/asr_to_cpp.cpp +++ b/src/libasr/codegen/asr_to_cpp.cpp @@ -622,37 +622,30 @@ Kokkos::View from_std_vector(const std::vector &v) void visit_Compare(const ASR::Compare_t &x) { this->visit_expr(*x.m_left); - std::string left = src; + std::string left = std::move(src); + int left_precedence = last_expr_precedence; this->visit_expr(*x.m_right); - std::string right = src; + std::string right = std::move(src); + int right_precedence = last_expr_precedence; switch (x.m_op) { - case (ASR::cmpopType::Eq) : { - src = left + " == " + right; - break; - } - case (ASR::cmpopType::Gt) : { - src = left + " > " + right; - break; - } - case (ASR::cmpopType::GtE) : { - src = left + " >= " + right; - break; - } - case (ASR::cmpopType::Lt) : { - src = left + " < " + right; - break; - } - case (ASR::cmpopType::LtE) : { - src = left + " <= " + right; - break; - } - case (ASR::cmpopType::NotEq) : { - src = left + " != " + right; - break; - } - default : { - throw CodeGenError("Comparison operator not implemented"); - } + case (ASR::cmpopType::Eq) : { last_expr_precedence = 10; break; } + case (ASR::cmpopType::Gt) : { last_expr_precedence = 9; break; } + case (ASR::cmpopType::GtE) : { last_expr_precedence = 9; break; } + case (ASR::cmpopType::Lt) : { last_expr_precedence = 9; break; } + case (ASR::cmpopType::LtE) : { last_expr_precedence = 9; break; } + case (ASR::cmpopType::NotEq): { last_expr_precedence = 10; break; } + default : LFORTRAN_ASSERT(false); // should never happen + } + if (left_precedence <= last_expr_precedence) { + src += left; + } else { + src += "(" + left + ")"; + } + src += ASRUtils::cmpop_to_str(x.m_op); + if (right_precedence <= last_expr_precedence) { + src += right; + } else { + src += "(" + right + ")"; } } @@ -814,7 +807,7 @@ Kokkos::View from_std_vector(const std::vector &v) } else { src += "(" + left + ")"; } - src += ASRUtils::boolop_to_str(x.m_op); + src += ASRUtils::boolop_to_str(x.m_op); if (right_precedence <= last_expr_precedence) { src += right; } else { diff --git a/tests/reference/cpp-assert1-ba60925.json b/tests/reference/cpp-assert1-ba60925.json index 77536006fc..28dfc2b65a 100644 --- a/tests/reference/cpp-assert1-ba60925.json +++ b/tests/reference/cpp-assert1-ba60925.json @@ -6,7 +6,7 @@ "outfile": null, "outfile_hash": null, "stdout": "cpp-assert1-ba60925.stdout", - "stdout_hash": "7607c69dce7cff6d39e1362ffaae0e6c68e261b5ab5f5b2de21d778b", + "stdout_hash": "19028c35513ef441d98f7fcfeb92c5221b417bb90622d3bb84c67421", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/cpp-assert1-ba60925.stdout b/tests/reference/cpp-assert1-ba60925.stdout index c7d2718935..4f925168cb 100644 --- a/tests/reference/cpp-assert1-ba60925.stdout +++ b/tests/reference/cpp-assert1-ba60925.stdout @@ -20,7 +20,7 @@ void test_assert() { int a; a = 5; - assert (("a is not 5", a == 5)); + assert (("a is not 5", (a) == (5))); assert (a != 10); } diff --git a/tests/reference/cpp-expr2-09c05ad.json b/tests/reference/cpp-expr2-09c05ad.json index 8b2bac92fe..3197a07e63 100644 --- a/tests/reference/cpp-expr2-09c05ad.json +++ b/tests/reference/cpp-expr2-09c05ad.json @@ -6,7 +6,7 @@ "outfile": null, "outfile_hash": null, "stdout": "cpp-expr2-09c05ad.stdout", - "stdout_hash": "8e8aecb3c46b493cc5c81e215206858638e82408c8c9fb210a0c8a4a", + "stdout_hash": "519f4fa06836586476055b9d63ae02c30062b477c765943261bdcf0a", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/cpp-expr2-09c05ad.stdout b/tests/reference/cpp-expr2-09c05ad.stdout index 5b7694f9ff..2cec60e17a 100644 --- a/tests/reference/cpp-expr2-09c05ad.stdout +++ b/tests/reference/cpp-expr2-09c05ad.stdout @@ -25,8 +25,8 @@ void test_boolOp() a = (a) && (b); b = a || true; a = a || b; - a = (a) && (b == b); - a = a && b != b; + a = (a) && (b) == (b); + a = a && (b) != (b); a = b || b; } diff --git a/tests/reference/cpp-expr6-f337f4f.json b/tests/reference/cpp-expr6-f337f4f.json index 4bb782c7a9..d4e0ce5aa7 100644 --- a/tests/reference/cpp-expr6-f337f4f.json +++ b/tests/reference/cpp-expr6-f337f4f.json @@ -6,7 +6,7 @@ "outfile": null, "outfile_hash": null, "stdout": "cpp-expr6-f337f4f.stdout", - "stdout_hash": "bcd16c3f69e0eb76c190d19115401fee0b68ef41bca09f04b9e70abf", + "stdout_hash": "501042b01196b529ab725169f9f5881ebe7fcab2fa917050c4ebfaa5", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/cpp-expr6-f337f4f.stdout b/tests/reference/cpp-expr6-f337f4f.stdout index c8ccae8a73..250cd0f68e 100644 --- a/tests/reference/cpp-expr6-f337f4f.stdout +++ b/tests/reference/cpp-expr6-f337f4f.stdout @@ -22,7 +22,7 @@ void test_ifexp() int b; bool c; a = 2; - b = (a == 2) ? (6) : (8); - c = (b > 5) ? (true) : (false); + b = ((a) == (2)) ? (6) : (8); + c = ((b) > (5)) ? (true) : (false); } diff --git a/tests/reference/cpp-loop1-0a8cf3b.json b/tests/reference/cpp-loop1-0a8cf3b.json index ef3e743ed5..8ac1d0b5d7 100644 --- a/tests/reference/cpp-loop1-0a8cf3b.json +++ b/tests/reference/cpp-loop1-0a8cf3b.json @@ -6,7 +6,7 @@ "outfile": null, "outfile_hash": null, "stdout": "cpp-loop1-0a8cf3b.stdout", - "stdout_hash": "1f37bcdfdd069546a01a119c56e3acc4432228a6d4904d2ae4874529", + "stdout_hash": "c9f7517eae594a38430ed490772f475daab9f95ed10d14761aafe918", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/cpp-loop1-0a8cf3b.stdout b/tests/reference/cpp-loop1-0a8cf3b.stdout index 00d4c4cc88..302f378f6f 100644 --- a/tests/reference/cpp-loop1-0a8cf3b.stdout +++ b/tests/reference/cpp-loop1-0a8cf3b.stdout @@ -20,7 +20,7 @@ int test_factorial_1(int x) { int _lpython_return_variable; int result; - if (x < 0) { + if ((x) < (0)) { _lpython_return_variable = 0; return _lpython_return_variable; } diff --git a/tests/reference/cpp-loop2-0686fc4.json b/tests/reference/cpp-loop2-0686fc4.json index 29ba9f7ca8..e131977b63 100644 --- a/tests/reference/cpp-loop2-0686fc4.json +++ b/tests/reference/cpp-loop2-0686fc4.json @@ -6,7 +6,7 @@ "outfile": null, "outfile_hash": null, "stdout": "cpp-loop2-0686fc4.stdout", - "stdout_hash": "4000d05d1f64673ea258bb401bab0cdc4d2547ef595bea3b21f082bb", + "stdout_hash": "b8487b468cfd75038213b8d35067df5fd276e4383dc1adb5419af617", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/cpp-loop2-0686fc4.stdout b/tests/reference/cpp-loop2-0686fc4.stdout index 0367af1e28..4bf41b85ff 100644 --- a/tests/reference/cpp-loop2-0686fc4.stdout +++ b/tests/reference/cpp-loop2-0686fc4.stdout @@ -20,10 +20,10 @@ void test_for() { int i; for (i=0; i<=10 - 1; i++) { - if (i == 0) { + if ((i) == (0)) { continue; } - if (i > 5) { + if ((i) > (5)) { break; } if (i == 3) { diff --git a/tests/reference/cpp-loop3-6020091.json b/tests/reference/cpp-loop3-6020091.json index b7bbd27402..d60aeb2105 100644 --- a/tests/reference/cpp-loop3-6020091.json +++ b/tests/reference/cpp-loop3-6020091.json @@ -6,7 +6,7 @@ "outfile": null, "outfile_hash": null, "stdout": "cpp-loop3-6020091.stdout", - "stdout_hash": "253df144c020ddd7a40efd9f389785dbeba08ff806e71f0dae76a6f6", + "stdout_hash": "92621926eaaae85146e30557b0a94b388e82b75bf47a64dffd8faf11", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/cpp-loop3-6020091.stdout b/tests/reference/cpp-loop3-6020091.stdout index 27363f32ac..bf3c0a3416 100644 --- a/tests/reference/cpp-loop3-6020091.stdout +++ b/tests/reference/cpp-loop3-6020091.stdout @@ -20,7 +20,7 @@ void test_pass() { int a; a = 1; - while (a > 0) { + while ((a) > (0)) { } } From b179fda3921dc72b1aa1f07ee86b9103044ab145 Mon Sep 17 00:00:00 2001 From: Naman Gera Date: Thu, 3 Feb 2022 22:08:20 +0530 Subject: [PATCH 4/9] Modify BinOp visitor --- src/libasr/asr_utils.h | 10 ++ src/libasr/codegen/asr_to_cpp.cpp | 96 +++++++------------ .../cpp-doconcurrentloop_01-4e9f274.json | 2 +- .../cpp-doconcurrentloop_01-4e9f274.stdout | 6 +- tests/reference/cpp-expr3-9c516d4.json | 2 +- tests/reference/cpp-expr3-9c516d4.stdout | 8 +- tests/reference/cpp-expr8-704cece.json | 2 +- tests/reference/cpp-expr8-704cece.stdout | 6 +- tests/reference/cpp-loop1-0a8cf3b.json | 2 +- tests/reference/cpp-loop1-0a8cf3b.stdout | 6 +- tests/reference/cpp-loop2-0686fc4.json | 2 +- tests/reference/cpp-loop2-0686fc4.stdout | 4 +- 12 files changed, 65 insertions(+), 81 deletions(-) diff --git a/src/libasr/asr_utils.h b/src/libasr/asr_utils.h index 1e1a4436e7..772c633942 100644 --- a/src/libasr/asr_utils.h +++ b/src/libasr/asr_utils.h @@ -149,6 +149,16 @@ static inline std::string unop_to_str(const ASR::unaryopType t) { } } +static inline std::string binop_to_str(const ASR::binopType t) { + switch (t) { + case (ASR::binopType::Add): { return " + "; } + case (ASR::binopType::Sub): { return " - "; } + case (ASR::binopType::Mul): { return " * "; } + case (ASR::binopType::Div): { return " / "; } + default : throw LFortranException("Not implemented"); + } +} + static inline std::string cmpop_to_str(const ASR::cmpopType t) { switch (t) { case (ASR::cmpopType::Eq): { return " == "; } diff --git a/src/libasr/codegen/asr_to_cpp.cpp b/src/libasr/codegen/asr_to_cpp.cpp index 996c7c5579..1292fb7c7e 100644 --- a/src/libasr/codegen/asr_to_cpp.cpp +++ b/src/libasr/codegen/asr_to_cpp.cpp @@ -516,8 +516,6 @@ Kokkos::View from_std_vector(const std::vector &v) } src = fn_name + "(" + args + ")"; } - last_unary_plus = false; - last_binary_plus = false; } void visit_Assignment(const ASR::Assignment_t &x) { @@ -538,26 +536,18 @@ Kokkos::View from_std_vector(const std::vector &v) void visit_ConstantInteger(const ASR::ConstantInteger_t &x) { src = std::to_string(x.m_n); - last_unary_plus = false; - last_binary_plus = false; } void visit_ConstantReal(const ASR::ConstantReal_t &x) { src = std::to_string(x.m_r); - last_unary_plus = false; - last_binary_plus = false; } void visit_ConstantString(const ASR::ConstantString_t &x) { src = "\"" + std::string(x.m_s) + "\""; - last_unary_plus = false; - last_binary_plus = false; } void visit_ConstantComplex(const ASR::ConstantComplex_t &x) { src = "{" + std::to_string(x.m_re) + ", " + std::to_string(x.m_im) + "}"; - last_unary_plus = false; - last_binary_plus = false; } void visit_ConstantLogical(const ASR::ConstantLogical_t &x) { @@ -566,15 +556,11 @@ Kokkos::View from_std_vector(const std::vector &v) } else { src = "false"; } - last_unary_plus = false; - last_binary_plus = false; } void visit_Var(const ASR::Var_t &x) { const ASR::symbol_t *s = ASRUtils::symbol_get_past_external(x.m_v); src = ASR::down_cast(s)->m_name; - last_unary_plus = false; - last_binary_plus = false; } void visit_ArrayRef(const ASR::ArrayRef_t &x) { @@ -592,8 +578,6 @@ Kokkos::View from_std_vector(const std::vector &v) } out += "-1]"; src = out; - last_unary_plus = false; - last_binary_plus = false; } void visit_ImplicitCast(const ASR::ImplicitCast_t &x) { @@ -704,58 +688,48 @@ Kokkos::View from_std_vector(const std::vector &v) void visit_BinOp(const ASR::BinOp_t &x) { this->visit_expr(*x.m_left); - std::string left_val = src; - if ((last_binary_plus || last_unary_plus) - && (x.m_op == ASR::binopType::Mul - || x.m_op == ASR::binopType::Div)) { - left_val = "(" + left_val + ")"; - } - if (last_unary_plus - && (x.m_op == ASR::binopType::Add - || x.m_op == ASR::binopType::Sub)) { - left_val = "(" + left_val + ")"; - } + std::string left = std::move(src); + int left_precedence = last_expr_precedence; this->visit_expr(*x.m_right); - std::string right_val = src; - if ((last_binary_plus || last_unary_plus) - && (x.m_op == ASR::binopType::Mul - || x.m_op == ASR::binopType::Div)) { - right_val = "(" + right_val + ")"; - } - if (last_unary_plus - && (x.m_op == ASR::binopType::Add - || x.m_op == ASR::binopType::Sub)) { - right_val = "(" + right_val + ")"; - } + std::string right = std::move(src); + int right_precedence = last_expr_precedence; switch (x.m_op) { - case ASR::binopType::Add: { - src = left_val + " + " + right_val; - last_binary_plus = true; - break; - } - case ASR::binopType::Sub: { - src = left_val + " - " + right_val; - last_binary_plus = true; - break; + case (ASR::binopType::Add) : { last_expr_precedence = 6; break; } + case (ASR::binopType::Sub) : { last_expr_precedence = 6; break; } + case (ASR::binopType::Mul) : { last_expr_precedence = 5; break; } + case (ASR::binopType::Div) : { last_expr_precedence = 5; break; } + case (ASR::binopType::Pow) : { + src = "std::pow(" + left + ", " + right + ")"; + return; } - case ASR::binopType::Mul: { - src = left_val + "*" + right_val; - last_binary_plus = false; - break; + default: throw CodeGenError("BinOp: operator not implemented yet"); + } + src = ""; + if (left_precedence == 3) { + src += "(" + left + ")"; + } else { + if (left_precedence <= last_expr_precedence) { + src += left; + } else { + src += "(" + left + ")"; } - case ASR::binopType::Div: { - src = left_val + "/" + right_val; - last_binary_plus = false; - break; + } + src += ASRUtils::binop_to_str(x.m_op); + if (right_precedence == 3) { + src += "(" + right + ")"; + } else if (x.m_op == ASR::binopType::Sub) { + if (right_precedence < last_expr_precedence) { + src += right; + } else { + src += "(" + right + ")"; } - case ASR::binopType::Pow: { - src = "std::pow(" + left_val + ", " + right_val + ")"; - last_binary_plus = false; - break; + } else { + if (right_precedence <= last_expr_precedence) { + src += right; + } else { + src += "(" + right + ")"; } - default : throw CodeGenError("Unhandled switch case"); } - last_unary_plus = false; } void visit_StrOp(const ASR::StrOp_t &x) { diff --git a/tests/reference/cpp-doconcurrentloop_01-4e9f274.json b/tests/reference/cpp-doconcurrentloop_01-4e9f274.json index 1c1fe314ed..927f5390ee 100644 --- a/tests/reference/cpp-doconcurrentloop_01-4e9f274.json +++ b/tests/reference/cpp-doconcurrentloop_01-4e9f274.json @@ -6,7 +6,7 @@ "outfile": null, "outfile_hash": null, "stdout": "cpp-doconcurrentloop_01-4e9f274.stdout", - "stdout_hash": "d12d0ebcf9ab541a2493792eff26246a3424e3ed5fadbf8d6c2f62d3", + "stdout_hash": "d69f8fd6d5c4ae7922888a890192282051495f33d5ff10c739bfd1be", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/cpp-doconcurrentloop_01-4e9f274.stdout b/tests/reference/cpp-doconcurrentloop_01-4e9f274.stdout index cd45c32c58..1932d1d857 100644 --- a/tests/reference/cpp-doconcurrentloop_01-4e9f274.stdout +++ b/tests/reference/cpp-doconcurrentloop_01-4e9f274.stdout @@ -27,7 +27,7 @@ int main() float scalar; scalar = 10; nsize = 1234; - Kokkos::parallel_for(Kokkos::RangePolicy(1, nsize - 1+1), KOKKOS_LAMBDA(const long i) { + Kokkos::parallel_for(Kokkos::RangePolicy(1, (nsize) - (1)+1), KOKKOS_LAMBDA(const long i) { a[i-1] = 5; b[i-1] = 5; }); @@ -41,8 +41,8 @@ void triad(const Kokkos::View &a, const Kokkos::View &b, float s { int N; N = 1234; - Kokkos::parallel_for(Kokkos::RangePolicy(1, N - 1+1), KOKKOS_LAMBDA(const long i) { - c[i-1] = a[i-1] + scalar*b[i-1]; + Kokkos::parallel_for(Kokkos::RangePolicy(1, N - (1)+1), KOKKOS_LAMBDA(const long i) { + c[i-1] = a[i-1] + (scalar) * (b[i-1]); }); } diff --git a/tests/reference/cpp-expr3-9c516d4.json b/tests/reference/cpp-expr3-9c516d4.json index 6148d26d5b..dadd85ca35 100644 --- a/tests/reference/cpp-expr3-9c516d4.json +++ b/tests/reference/cpp-expr3-9c516d4.json @@ -6,7 +6,7 @@ "outfile": null, "outfile_hash": null, "stdout": "cpp-expr3-9c516d4.stdout", - "stdout_hash": "8f4d456c790a3ad87289f3a23797d82ce9eaf14b5eebf25a7fc75aef", + "stdout_hash": "bfca607c13c2139925c0353cb21f8d918e0f9726fefd400b940e875f", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/cpp-expr3-9c516d4.stdout b/tests/reference/cpp-expr3-9c516d4.stdout index 4e08df4139..95f5ca2877 100644 --- a/tests/reference/cpp-expr3-9c516d4.stdout +++ b/tests/reference/cpp-expr3-9c516d4.stdout @@ -22,11 +22,11 @@ void test_cast() float b; a = 2; b = 4.200000; - a = (float)(a)*b; + a = ((float)(a)) * (b); b = b + (float)(1); a = 5; - a = (float)(a) - 3.900000; - a = (float)(a)/b; - b = (float)(3)/(float)(4); + a = (float)(a) - (3.900000); + a = ((float)(a)) / (b); + b = (float)(3) / (float)(4); } diff --git a/tests/reference/cpp-expr8-704cece.json b/tests/reference/cpp-expr8-704cece.json index c6c57918aa..5a1a8b7220 100644 --- a/tests/reference/cpp-expr8-704cece.json +++ b/tests/reference/cpp-expr8-704cece.json @@ -6,7 +6,7 @@ "outfile": null, "outfile_hash": null, "stdout": "cpp-expr8-704cece.stdout", - "stdout_hash": "dc0292c703db8937d8ebdc101cfdcad060115c9ee31f1efc3de61c16", + "stdout_hash": "707f2b21d994fa577fc95cb028d5309037158d3f8492b0e815359ef3", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/cpp-expr8-704cece.stdout b/tests/reference/cpp-expr8-704cece.stdout index 625a2869c8..f747026a1d 100644 --- a/tests/reference/cpp-expr8-704cece.stdout +++ b/tests/reference/cpp-expr8-704cece.stdout @@ -22,9 +22,9 @@ void test_binop() float x2; x = std::pow(2, 3); x = std::pow((float)(2), 3.500000); - x = 54 - 100; - x2 = 3.454000 - 765.430000 + 534.600000; - x2 = 5346.565000*3.450000; + x = (54) - (100); + x2 = 3.454000 - (765.430000) + 534.600000; + x2 = (5346.565000) * (3.450000); x2 = std::pow(5346.565000, 3.450000); } diff --git a/tests/reference/cpp-loop1-0a8cf3b.json b/tests/reference/cpp-loop1-0a8cf3b.json index 8ac1d0b5d7..454d1f12d5 100644 --- a/tests/reference/cpp-loop1-0a8cf3b.json +++ b/tests/reference/cpp-loop1-0a8cf3b.json @@ -6,7 +6,7 @@ "outfile": null, "outfile_hash": null, "stdout": "cpp-loop1-0a8cf3b.stdout", - "stdout_hash": "c9f7517eae594a38430ed490772f475daab9f95ed10d14761aafe918", + "stdout_hash": "0661022c05f65fc33c4b74f9f84daae0ab08792d5ef7d34ae4672797", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/cpp-loop1-0a8cf3b.stdout b/tests/reference/cpp-loop1-0a8cf3b.stdout index 302f378f6f..8b5e35136f 100644 --- a/tests/reference/cpp-loop1-0a8cf3b.stdout +++ b/tests/reference/cpp-loop1-0a8cf3b.stdout @@ -26,7 +26,7 @@ int test_factorial_1(int x) } result = 1; while (x > 0) { - result = result*x; + result = (result) * (x); x = x - 1; } _lpython_return_variable = result; @@ -39,8 +39,8 @@ int test_factorial_2(int x) int i; int result; result = 1; - for (i=1; i<=x + 1 - 1; i++) { - result = result*i; + for (i=1; i<=x + 1 - (1); i++) { + result = (result) * (i); } _lpython_return_variable = result; return _lpython_return_variable; diff --git a/tests/reference/cpp-loop2-0686fc4.json b/tests/reference/cpp-loop2-0686fc4.json index e131977b63..32fbb86d4c 100644 --- a/tests/reference/cpp-loop2-0686fc4.json +++ b/tests/reference/cpp-loop2-0686fc4.json @@ -6,7 +6,7 @@ "outfile": null, "outfile_hash": null, "stdout": "cpp-loop2-0686fc4.stdout", - "stdout_hash": "b8487b468cfd75038213b8d35067df5fd276e4383dc1adb5419af617", + "stdout_hash": "72a36821ba5358d04139f2643e6a44e7f3d276fb788d21e157f4eb0c", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/cpp-loop2-0686fc4.stdout b/tests/reference/cpp-loop2-0686fc4.stdout index 4bf41b85ff..5066a440e6 100644 --- a/tests/reference/cpp-loop2-0686fc4.stdout +++ b/tests/reference/cpp-loop2-0686fc4.stdout @@ -19,8 +19,8 @@ Kokkos::View from_std_vector(const std::vector &v) void test_for() { int i; - for (i=0; i<=10 - 1; i++) { - if ((i) == (0)) { + for (i=0; i<=(10) - (1); i++) { + if (i == 0) { continue; } if ((i) > (5)) { From 7793889152c05e336c29a2f28fc43f27d4eb5c94 Mon Sep 17 00:00:00 2001 From: Naman Gera Date: Thu, 3 Feb 2022 22:12:04 +0530 Subject: [PATCH 5/9] remove all occurences of `last_unary_plus` and `last_binary_plus` --- src/libasr/codegen/asr_to_cpp.cpp | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/src/libasr/codegen/asr_to_cpp.cpp b/src/libasr/codegen/asr_to_cpp.cpp index 1292fb7c7e..6ec84b71ea 100644 --- a/src/libasr/codegen/asr_to_cpp.cpp +++ b/src/libasr/codegen/asr_to_cpp.cpp @@ -101,8 +101,6 @@ class ASRToCPPVisitor : public ASR::BaseVisitor // The precedence of the last expression, using the table: // https://en.cppreference.com/w/cpp/language/operator_precedence int last_expr_precedence; - bool last_unary_plus; - bool last_binary_plus; bool intrinsic_module = false; const ASR::Function_t *current_function = nullptr; @@ -638,20 +636,12 @@ Kokkos::View from_std_vector(const std::vector &v) if (x.m_type->type == ASR::ttypeType::Integer) { if (x.m_op == ASR::unaryopType::UAdd) { // src = src; - last_unary_plus = false; - return; } else if (x.m_op == ASR::unaryopType::USub) { src = "-" + src; - last_unary_plus = true; - last_binary_plus = false; } else if (x.m_op == ASR::unaryopType::Invert) { src = "~" + src; - last_unary_plus = false; - last_binary_plus = false; } else if (x.m_op == ASR::unaryopType::Not) { src = "!" + src; - last_unary_plus = false; - last_binary_plus = false; } else { throw CodeGenError("Unary type not implemented yet for Integer"); } @@ -659,15 +649,10 @@ Kokkos::View from_std_vector(const std::vector &v) } else if (x.m_type->type == ASR::ttypeType::Real) { if (x.m_op == ASR::unaryopType::UAdd) { // src = src; - last_unary_plus = false; } else if (x.m_op == ASR::unaryopType::USub) { src = "-" + src; - last_unary_plus = true; - last_binary_plus = false; } else if (x.m_op == ASR::unaryopType::Not) { src = "!" + src; - last_unary_plus = false; - last_binary_plus = false; } else { throw CodeGenError("Unary type not implemented yet for Real"); } @@ -675,8 +660,6 @@ Kokkos::View from_std_vector(const std::vector &v) } else if (x.m_type->type == ASR::ttypeType::Logical) { if (x.m_op == ASR::unaryopType::Not) { src = "!" + src; - last_unary_plus = false; - last_binary_plus = false; return; } else { throw CodeGenError("Unary type not implemented yet for Logical"); @@ -746,7 +729,6 @@ Kokkos::View from_std_vector(const std::vector &v) // TODO: implement } } - last_unary_plus = false; } void visit_BoolOp(const ASR::BoolOp_t &x) { From 5d4d6f45936f335dd65c00611e21474d38e401e0 Mon Sep 17 00:00:00 2001 From: Naman Gera Date: Tue, 22 Feb 2022 03:26:03 +0530 Subject: [PATCH 6/9] Update the test results --- tests/reference/asr-expr2-2e78a12.json | 2 +- tests/reference/asr-expr2-2e78a12.stdout | 2 +- tests/reference/cpp-assert1-ba60925.json | 2 +- tests/reference/cpp-assert1-ba60925.stdout | 2 +- .../cpp-doconcurrentloop_01-4e9f274.json | 2 +- .../cpp-doconcurrentloop_01-4e9f274.stdout | 10 +++++----- tests/reference/cpp-expr2-09c05ad.json | 2 +- tests/reference/cpp-expr2-09c05ad.stdout | 10 +++++----- tests/reference/cpp-expr3-9c516d4.json | 2 +- tests/reference/cpp-expr3-9c516d4.stdout | 16 ++++++++++++---- tests/reference/cpp-expr6-f337f4f.json | 2 +- tests/reference/cpp-expr6-f337f4f.stdout | 4 ++-- tests/reference/cpp-expr8-704cece.json | 2 +- tests/reference/cpp-expr8-704cece.stdout | 6 +++--- tests/reference/cpp-loop1-0a8cf3b.json | 2 +- tests/reference/cpp-loop1-0a8cf3b.stdout | 8 ++++---- tests/reference/cpp-loop2-0686fc4.json | 2 +- tests/reference/cpp-loop2-0686fc4.stdout | 4 ++-- tests/reference/cpp-loop3-6020091.json | 2 +- tests/reference/cpp-loop3-6020091.stdout | 2 +- 20 files changed, 46 insertions(+), 38 deletions(-) diff --git a/tests/reference/asr-expr2-2e78a12.json b/tests/reference/asr-expr2-2e78a12.json index fa77462aa1..30174be1d8 100644 --- a/tests/reference/asr-expr2-2e78a12.json +++ b/tests/reference/asr-expr2-2e78a12.json @@ -6,7 +6,7 @@ "outfile": null, "outfile_hash": null, "stdout": "asr-expr2-2e78a12.stdout", - "stdout_hash": "2a0b2181d9a99d83d294a7319d15c6e0b1bce2079c8bd318d423d73b", + "stdout_hash": "30936f6b43898b5cad6c23422795ce8f8d0aa20f5a3cea6ff300d5ce", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/asr-expr2-2e78a12.stdout b/tests/reference/asr-expr2-2e78a12.stdout index b19b865a60..f277a35fbe 100644 --- a/tests/reference/asr-expr2-2e78a12.stdout +++ b/tests/reference/asr-expr2-2e78a12.stdout @@ -1 +1 @@ -(TranslationUnit (SymbolTable 1 {test_boolOp: (Subroutine (SymbolTable 2 {a: (Variable 2 a Local () () Default (Logical 1 []) Source Public Required .false.), b: (Variable 2 b Local () () Default (Logical 1 []) Source Public Required .false.)}) test_boolOp [] [(= (Var 2 a) (ConstantLogical .false. (Logical 1 [])) ()) (= (Var 2 b) (ConstantLogical .true. (Logical 1 [])) ()) (= (Var 2 a) (BoolOp (Var 2 a) And (Var 2 b) (Logical 1 []) ()) ()) (= (Var 2 b) (BoolOp (Var 2 a) Or (ConstantLogical .true. (Logical 1 [])) (Logical 1 []) ()) ()) (= (Var 2 a) (BoolOp (Var 2 a) Or (Var 2 b) (Logical 1 []) ()) ()) (= (Var 2 a) (BoolOp (Var 2 a) And (Compare (Var 2 b) Eq (Var 2 b) (Logical 4 []) () ()) (Logical 1 []) ()) ()) (= (Var 2 a) (BoolOp (Var 2 a) And (Compare (Var 2 b) NotEq (Var 2 b) (Logical 4 []) () ()) (Logical 1 []) ()) ()) (= (Var 2 a) (BoolOp (Var 2 b) Or (Var 2 b) (Logical 1 []) ()) ())] Source Public Implementation () .false. .false.)}) []) +(TranslationUnit (SymbolTable 1 {main_program: (Program (SymbolTable 3 {}) main_program [] []), test_boolOp: (Subroutine (SymbolTable 2 {a: (Variable 2 a Local () () Default (Logical 1 []) Source Public Required .false.), b: (Variable 2 b Local () () Default (Logical 1 []) Source Public Required .false.)}) test_boolOp [] [(= (Var 2 a) (ConstantLogical .false. (Logical 1 [])) ()) (= (Var 2 b) (ConstantLogical .true. (Logical 1 [])) ()) (= (Var 2 a) (BoolOp (Var 2 a) And (Var 2 b) (Logical 1 []) ()) ()) (= (Var 2 b) (BoolOp (Var 2 a) Or (ConstantLogical .true. (Logical 1 [])) (Logical 1 []) ()) ()) (= (Var 2 a) (BoolOp (Var 2 a) Or (Var 2 b) (Logical 1 []) ()) ()) (= (Var 2 a) (BoolOp (Var 2 a) And (Compare (Var 2 b) Eq (Var 2 b) (Logical 4 []) () ()) (Logical 1 []) ()) ()) (= (Var 2 a) (BoolOp (Var 2 a) And (Compare (Var 2 b) NotEq (Var 2 b) (Logical 4 []) () ()) (Logical 1 []) ()) ()) (= (Var 2 a) (BoolOp (Var 2 b) Or (Var 2 b) (Logical 1 []) ()) ())] Source Public Implementation () .false. .false.)}) []) diff --git a/tests/reference/cpp-assert1-ba60925.json b/tests/reference/cpp-assert1-ba60925.json index 28dfc2b65a..475ff3a486 100644 --- a/tests/reference/cpp-assert1-ba60925.json +++ b/tests/reference/cpp-assert1-ba60925.json @@ -6,7 +6,7 @@ "outfile": null, "outfile_hash": null, "stdout": "cpp-assert1-ba60925.stdout", - "stdout_hash": "19028c35513ef441d98f7fcfeb92c5221b417bb90622d3bb84c67421", + "stdout_hash": "341ab8b76eaf0af8a4b2d8b901fdf11fb8e3e3bfc541ed0dc078d5a7", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/cpp-assert1-ba60925.stdout b/tests/reference/cpp-assert1-ba60925.stdout index 7b734a9341..601e2052c0 100644 --- a/tests/reference/cpp-assert1-ba60925.stdout +++ b/tests/reference/cpp-assert1-ba60925.stdout @@ -20,7 +20,7 @@ void test_assert() { int a; a = 5; - assert (("a is not 5", (a) == (5))); + assert (("a is not 5", a == 5)); assert (a != 10); } diff --git a/tests/reference/cpp-doconcurrentloop_01-4e9f274.json b/tests/reference/cpp-doconcurrentloop_01-4e9f274.json index 0a33354d90..e61aa4466a 100644 --- a/tests/reference/cpp-doconcurrentloop_01-4e9f274.json +++ b/tests/reference/cpp-doconcurrentloop_01-4e9f274.json @@ -6,7 +6,7 @@ "outfile": null, "outfile_hash": null, "stdout": "cpp-doconcurrentloop_01-4e9f274.stdout", - "stdout_hash": "d69f8fd6d5c4ae7922888a890192282051495f33d5ff10c739bfd1be", + "stdout_hash": "ebbc468b9812b751d5770f98b4f9ea7772a30af7a9b2d30852c302ff", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/cpp-doconcurrentloop_01-4e9f274.stdout b/tests/reference/cpp-doconcurrentloop_01-4e9f274.stdout index 5fcb740185..c3e73b2336 100644 --- a/tests/reference/cpp-doconcurrentloop_01-4e9f274.stdout +++ b/tests/reference/cpp-doconcurrentloop_01-4e9f274.stdout @@ -30,9 +30,9 @@ void main0() float scalar; scalar = 10.000000; nsize = 1234; - Kokkos::parallel_for(Kokkos::RangePolicy(1, (nsize) - (1)+1), KOKKOS_LAMBDA(const long i) { - a[i-1] = 5; - b[i-1] = 5; + Kokkos::parallel_for(Kokkos::RangePolicy(1, nsize - 1+1), KOKKOS_LAMBDA(const long i) { + a[i-1] = 5.000000; + b[i-1] = 5.000000; }); triad(a, b, scalar, c); std::cout << "End Stream Triad" << std::endl; @@ -42,8 +42,8 @@ void triad(const Kokkos::View &a, const Kokkos::View &b, float s { int N; N = 1234; - Kokkos::parallel_for(Kokkos::RangePolicy(1, N - (1)+1), KOKKOS_LAMBDA(const long i) { - c[i-1] = a[i-1] + (scalar) * (b[i-1]); + Kokkos::parallel_for(Kokkos::RangePolicy(1, N - 1+1), KOKKOS_LAMBDA(const long i) { + c[i-1] = a[i-1] + scalar*b[i-1]; }); } diff --git a/tests/reference/cpp-expr2-09c05ad.json b/tests/reference/cpp-expr2-09c05ad.json index 3197a07e63..b9ad67af2d 100644 --- a/tests/reference/cpp-expr2-09c05ad.json +++ b/tests/reference/cpp-expr2-09c05ad.json @@ -6,7 +6,7 @@ "outfile": null, "outfile_hash": null, "stdout": "cpp-expr2-09c05ad.stdout", - "stdout_hash": "519f4fa06836586476055b9d63ae02c30062b477c765943261bdcf0a", + "stdout_hash": "56511b3950c2bf0ef5eefa28899dd953cff1a3a1d88561c019f86b46", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/cpp-expr2-09c05ad.stdout b/tests/reference/cpp-expr2-09c05ad.stdout index da67a1f111..9ed3151e75 100644 --- a/tests/reference/cpp-expr2-09c05ad.stdout +++ b/tests/reference/cpp-expr2-09c05ad.stdout @@ -23,11 +23,11 @@ void test_boolOp() a = false; b = true; a = (a) && (b); - b = a || true; - a = a || b; - a = (a) && (b) == (b); - a = a && (b) != (b); - a = b || b; + b = (a) || (true); + a = (a) || (b); + a = (a) && (b == b); + a = (a) && (b != b); + a = (b) || (b); } namespace { diff --git a/tests/reference/cpp-expr3-9c516d4.json b/tests/reference/cpp-expr3-9c516d4.json index 4004bed251..8e9b5cd77e 100644 --- a/tests/reference/cpp-expr3-9c516d4.json +++ b/tests/reference/cpp-expr3-9c516d4.json @@ -6,7 +6,7 @@ "outfile": null, "outfile_hash": null, "stdout": "cpp-expr3-9c516d4.stdout", - "stdout_hash": "bfca607c13c2139925c0353cb21f8d918e0f9726fefd400b940e875f", + "stdout_hash": "1d13d271de569db70607c1811c0c719440ae26a7fd5a357c8910a78a", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/cpp-expr3-9c516d4.stdout b/tests/reference/cpp-expr3-9c516d4.stdout index ddee1f8782..754d3a40e4 100644 --- a/tests/reference/cpp-expr3-9c516d4.stdout +++ b/tests/reference/cpp-expr3-9c516d4.stdout @@ -22,12 +22,20 @@ void test_cast() float b; a = 2; b = 4.200000; - a = ((float)(a)) * (b); + a = (float)(a)*b; b = b + (float)(1); a = 5; - a = (float)(a) - (3.900000); - a = ((float)(a)) / (b); - b = (float)(3) / (float)(4); + a = (float)(a) - 3.900000; + a = (float)(a)/b; + b = (float)(3)/(float)(4); + if ((float)(a) < b) { + std::cout << "a < b" << std::endl; + } +} + +namespace { + +void main2() { } } diff --git a/tests/reference/cpp-expr6-f337f4f.json b/tests/reference/cpp-expr6-f337f4f.json index d4e0ce5aa7..0ef22aa356 100644 --- a/tests/reference/cpp-expr6-f337f4f.json +++ b/tests/reference/cpp-expr6-f337f4f.json @@ -6,7 +6,7 @@ "outfile": null, "outfile_hash": null, "stdout": "cpp-expr6-f337f4f.stdout", - "stdout_hash": "501042b01196b529ab725169f9f5881ebe7fcab2fa917050c4ebfaa5", + "stdout_hash": "1171d1e704952c3d465082fbde015a10f9bc7dc675357b66e6080211", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/cpp-expr6-f337f4f.stdout b/tests/reference/cpp-expr6-f337f4f.stdout index 5274919065..0346806d15 100644 --- a/tests/reference/cpp-expr6-f337f4f.stdout +++ b/tests/reference/cpp-expr6-f337f4f.stdout @@ -22,8 +22,8 @@ void test_ifexp() int b; bool c; a = 2; - b = ((a) == (2)) ? (6) : (8); - c = ((b) > (5)) ? (true) : (false); + b = (a == 2) ? (6) : (8); + c = (b > 5) ? (true) : (false); } namespace { diff --git a/tests/reference/cpp-expr8-704cece.json b/tests/reference/cpp-expr8-704cece.json index 5a1a8b7220..97209a402c 100644 --- a/tests/reference/cpp-expr8-704cece.json +++ b/tests/reference/cpp-expr8-704cece.json @@ -6,7 +6,7 @@ "outfile": null, "outfile_hash": null, "stdout": "cpp-expr8-704cece.stdout", - "stdout_hash": "707f2b21d994fa577fc95cb028d5309037158d3f8492b0e815359ef3", + "stdout_hash": "8201234d9cf1e4521f7ef37c1ef3532c18044040aff13375e18ed5ad", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/cpp-expr8-704cece.stdout b/tests/reference/cpp-expr8-704cece.stdout index f391707c29..70204a536c 100644 --- a/tests/reference/cpp-expr8-704cece.stdout +++ b/tests/reference/cpp-expr8-704cece.stdout @@ -22,9 +22,9 @@ void test_binop() float x2; x = std::pow(2, 3); x = std::pow((float)(2), 3.500000); - x = (54) - (100); - x2 = 3.454000 - (765.430000) + 534.600000; - x2 = (5346.565000) * (3.450000); + x = 54 - 100; + x2 = 3.454000 - 765.430000 + 534.600000; + x2 = 5346.565000*3.450000; x2 = std::pow(5346.565000, 3.450000); } diff --git a/tests/reference/cpp-loop1-0a8cf3b.json b/tests/reference/cpp-loop1-0a8cf3b.json index 4398b0216f..4249ed711b 100644 --- a/tests/reference/cpp-loop1-0a8cf3b.json +++ b/tests/reference/cpp-loop1-0a8cf3b.json @@ -6,7 +6,7 @@ "outfile": null, "outfile_hash": null, "stdout": "cpp-loop1-0a8cf3b.stdout", - "stdout_hash": "0661022c05f65fc33c4b74f9f84daae0ab08792d5ef7d34ae4672797", + "stdout_hash": "4b9970204be628f2b99634dbc980e6219bda60ec7dd4a6f3860335fe", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/cpp-loop1-0a8cf3b.stdout b/tests/reference/cpp-loop1-0a8cf3b.stdout index b3d51e6058..b587034cc1 100644 --- a/tests/reference/cpp-loop1-0a8cf3b.stdout +++ b/tests/reference/cpp-loop1-0a8cf3b.stdout @@ -34,13 +34,13 @@ int test_factorial_1(int x) { int _lpython_return_variable; int result; - if ((x) < (0)) { + if (x < 0) { _lpython_return_variable = 0; return _lpython_return_variable; } result = 1; while (x > 0) { - result = (result) * (x); + result = result*x; x = x - 1; } _lpython_return_variable = result; @@ -53,8 +53,8 @@ int test_factorial_2(int x) int i; int result; result = 1; - for (i=1; i<=x + 1 - (1); i++) { - result = (result) * (i); + for (i=1; i<=x + 1 - 1; i++) { + result = result*i; } _lpython_return_variable = result; return _lpython_return_variable; diff --git a/tests/reference/cpp-loop2-0686fc4.json b/tests/reference/cpp-loop2-0686fc4.json index b134303177..b1529981c2 100644 --- a/tests/reference/cpp-loop2-0686fc4.json +++ b/tests/reference/cpp-loop2-0686fc4.json @@ -6,7 +6,7 @@ "outfile": null, "outfile_hash": null, "stdout": "cpp-loop2-0686fc4.stdout", - "stdout_hash": "72a36821ba5358d04139f2643e6a44e7f3d276fb788d21e157f4eb0c", + "stdout_hash": "eef4f1ff5b5b8880a2e873cd82808099e1f3612117a10667599f5d62", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/cpp-loop2-0686fc4.stdout b/tests/reference/cpp-loop2-0686fc4.stdout index 9cc0165d85..6b586afe53 100644 --- a/tests/reference/cpp-loop2-0686fc4.stdout +++ b/tests/reference/cpp-loop2-0686fc4.stdout @@ -19,11 +19,11 @@ Kokkos::View from_std_vector(const std::vector &v) void test_for() { int i; - for (i=0; i<=(10) - (1); i++) { + for (i=0; i<=10 - 1; i++) { if (i == 0) { continue; } - if ((i) > (5)) { + if (i > 5) { break; } if (i == 3) { diff --git a/tests/reference/cpp-loop3-6020091.json b/tests/reference/cpp-loop3-6020091.json index d60aeb2105..d674114412 100644 --- a/tests/reference/cpp-loop3-6020091.json +++ b/tests/reference/cpp-loop3-6020091.json @@ -6,7 +6,7 @@ "outfile": null, "outfile_hash": null, "stdout": "cpp-loop3-6020091.stdout", - "stdout_hash": "92621926eaaae85146e30557b0a94b388e82b75bf47a64dffd8faf11", + "stdout_hash": "72ed067095f13bce8cae196ddc18c063f5c4f3cce8761113f6f6ef37", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/cpp-loop3-6020091.stdout b/tests/reference/cpp-loop3-6020091.stdout index 8162be688b..cc15ea1570 100644 --- a/tests/reference/cpp-loop3-6020091.stdout +++ b/tests/reference/cpp-loop3-6020091.stdout @@ -20,7 +20,7 @@ void test_pass() { int a; a = 1; - while ((a) > (0)) { + while (a > 0) { } } From 0b7203ef429a1141357bf19578abf636050ea75a Mon Sep 17 00:00:00 2001 From: Naman Gera Date: Tue, 22 Feb 2022 03:31:48 +0530 Subject: [PATCH 7/9] No space around `*` and `/` --- src/libasr/asr_utils.h | 4 ++-- tests/reference/cpp-assert1-ba60925.json | 2 +- tests/reference/cpp-assert1-ba60925.stdout | 2 +- tests/reference/cpp-doconcurrentloop_01-4e9f274.json | 2 +- tests/reference/cpp-doconcurrentloop_01-4e9f274.stdout | 6 +++--- tests/reference/cpp-expr2-09c05ad.json | 2 +- tests/reference/cpp-expr2-09c05ad.stdout | 10 +++++----- tests/reference/cpp-expr3-9c516d4.json | 2 +- tests/reference/cpp-expr3-9c516d4.stdout | 6 +++--- tests/reference/cpp-expr6-f337f4f.json | 2 +- tests/reference/cpp-expr6-f337f4f.stdout | 4 ++-- tests/reference/cpp-expr8-704cece.json | 2 +- tests/reference/cpp-expr8-704cece.stdout | 6 +++--- tests/reference/cpp-loop1-0a8cf3b.json | 2 +- tests/reference/cpp-loop1-0a8cf3b.stdout | 10 +++++----- tests/reference/cpp-loop2-0686fc4.json | 2 +- tests/reference/cpp-loop2-0686fc4.stdout | 4 ++-- tests/reference/cpp-loop3-6020091.json | 2 +- tests/reference/cpp-loop3-6020091.stdout | 2 +- 19 files changed, 36 insertions(+), 36 deletions(-) diff --git a/src/libasr/asr_utils.h b/src/libasr/asr_utils.h index e6bd74af95..8c03e61267 100644 --- a/src/libasr/asr_utils.h +++ b/src/libasr/asr_utils.h @@ -157,8 +157,8 @@ static inline std::string binop_to_str(const ASR::binopType t) { switch (t) { case (ASR::binopType::Add): { return " + "; } case (ASR::binopType::Sub): { return " - "; } - case (ASR::binopType::Mul): { return " * "; } - case (ASR::binopType::Div): { return " / "; } + case (ASR::binopType::Mul): { return "*"; } + case (ASR::binopType::Div): { return "/"; } default : throw LFortranException("Not implemented"); } } diff --git a/tests/reference/cpp-assert1-ba60925.json b/tests/reference/cpp-assert1-ba60925.json index 475ff3a486..decff75b10 100644 --- a/tests/reference/cpp-assert1-ba60925.json +++ b/tests/reference/cpp-assert1-ba60925.json @@ -6,7 +6,7 @@ "outfile": null, "outfile_hash": null, "stdout": "cpp-assert1-ba60925.stdout", - "stdout_hash": "341ab8b76eaf0af8a4b2d8b901fdf11fb8e3e3bfc541ed0dc078d5a7", + "stdout_hash": "14c793b7b19362e829cdff759adedf2ae156cef0e663cca99a751cc0", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/cpp-assert1-ba60925.stdout b/tests/reference/cpp-assert1-ba60925.stdout index 601e2052c0..7b734a9341 100644 --- a/tests/reference/cpp-assert1-ba60925.stdout +++ b/tests/reference/cpp-assert1-ba60925.stdout @@ -20,7 +20,7 @@ void test_assert() { int a; a = 5; - assert (("a is not 5", a == 5)); + assert (("a is not 5", (a) == (5))); assert (a != 10); } diff --git a/tests/reference/cpp-doconcurrentloop_01-4e9f274.json b/tests/reference/cpp-doconcurrentloop_01-4e9f274.json index e61aa4466a..56e8d095c6 100644 --- a/tests/reference/cpp-doconcurrentloop_01-4e9f274.json +++ b/tests/reference/cpp-doconcurrentloop_01-4e9f274.json @@ -6,7 +6,7 @@ "outfile": null, "outfile_hash": null, "stdout": "cpp-doconcurrentloop_01-4e9f274.stdout", - "stdout_hash": "ebbc468b9812b751d5770f98b4f9ea7772a30af7a9b2d30852c302ff", + "stdout_hash": "cd85c3a1a360f70db5ee96b33a26258c8f985b73534e4c766128b79e", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/cpp-doconcurrentloop_01-4e9f274.stdout b/tests/reference/cpp-doconcurrentloop_01-4e9f274.stdout index c3e73b2336..57df167c78 100644 --- a/tests/reference/cpp-doconcurrentloop_01-4e9f274.stdout +++ b/tests/reference/cpp-doconcurrentloop_01-4e9f274.stdout @@ -30,7 +30,7 @@ void main0() float scalar; scalar = 10.000000; nsize = 1234; - Kokkos::parallel_for(Kokkos::RangePolicy(1, nsize - 1+1), KOKKOS_LAMBDA(const long i) { + Kokkos::parallel_for(Kokkos::RangePolicy(1, (nsize) - (1)+1), KOKKOS_LAMBDA(const long i) { a[i-1] = 5.000000; b[i-1] = 5.000000; }); @@ -42,8 +42,8 @@ void triad(const Kokkos::View &a, const Kokkos::View &b, float s { int N; N = 1234; - Kokkos::parallel_for(Kokkos::RangePolicy(1, N - 1+1), KOKKOS_LAMBDA(const long i) { - c[i-1] = a[i-1] + scalar*b[i-1]; + Kokkos::parallel_for(Kokkos::RangePolicy(1, N - (1)+1), KOKKOS_LAMBDA(const long i) { + c[i-1] = a[i-1] + (scalar)*(b[i-1]); }); } diff --git a/tests/reference/cpp-expr2-09c05ad.json b/tests/reference/cpp-expr2-09c05ad.json index b9ad67af2d..c0fa2e4a96 100644 --- a/tests/reference/cpp-expr2-09c05ad.json +++ b/tests/reference/cpp-expr2-09c05ad.json @@ -6,7 +6,7 @@ "outfile": null, "outfile_hash": null, "stdout": "cpp-expr2-09c05ad.stdout", - "stdout_hash": "56511b3950c2bf0ef5eefa28899dd953cff1a3a1d88561c019f86b46", + "stdout_hash": "fa6bfa261838534311f57b41cc1b67a574675a8286eb616a4ee28b7e", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/cpp-expr2-09c05ad.stdout b/tests/reference/cpp-expr2-09c05ad.stdout index 9ed3151e75..da67a1f111 100644 --- a/tests/reference/cpp-expr2-09c05ad.stdout +++ b/tests/reference/cpp-expr2-09c05ad.stdout @@ -23,11 +23,11 @@ void test_boolOp() a = false; b = true; a = (a) && (b); - b = (a) || (true); - a = (a) || (b); - a = (a) && (b == b); - a = (a) && (b != b); - a = (b) || (b); + b = a || true; + a = a || b; + a = (a) && (b) == (b); + a = a && (b) != (b); + a = b || b; } namespace { diff --git a/tests/reference/cpp-expr3-9c516d4.json b/tests/reference/cpp-expr3-9c516d4.json index 8e9b5cd77e..320f36508a 100644 --- a/tests/reference/cpp-expr3-9c516d4.json +++ b/tests/reference/cpp-expr3-9c516d4.json @@ -6,7 +6,7 @@ "outfile": null, "outfile_hash": null, "stdout": "cpp-expr3-9c516d4.stdout", - "stdout_hash": "1d13d271de569db70607c1811c0c719440ae26a7fd5a357c8910a78a", + "stdout_hash": "fe9bf45e7eabfcf0a8491dd85ae9d569a878909ad0455062207e1a94", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/cpp-expr3-9c516d4.stdout b/tests/reference/cpp-expr3-9c516d4.stdout index 754d3a40e4..4803c0adfd 100644 --- a/tests/reference/cpp-expr3-9c516d4.stdout +++ b/tests/reference/cpp-expr3-9c516d4.stdout @@ -22,11 +22,11 @@ void test_cast() float b; a = 2; b = 4.200000; - a = (float)(a)*b; + a = ((float)(a))*(b); b = b + (float)(1); a = 5; - a = (float)(a) - 3.900000; - a = (float)(a)/b; + a = (float)(a) - (3.900000); + a = ((float)(a))/(b); b = (float)(3)/(float)(4); if ((float)(a) < b) { std::cout << "a < b" << std::endl; diff --git a/tests/reference/cpp-expr6-f337f4f.json b/tests/reference/cpp-expr6-f337f4f.json index 0ef22aa356..a06578f7fd 100644 --- a/tests/reference/cpp-expr6-f337f4f.json +++ b/tests/reference/cpp-expr6-f337f4f.json @@ -6,7 +6,7 @@ "outfile": null, "outfile_hash": null, "stdout": "cpp-expr6-f337f4f.stdout", - "stdout_hash": "1171d1e704952c3d465082fbde015a10f9bc7dc675357b66e6080211", + "stdout_hash": "98c4ae8b307378dcfcc21a4d2d39f5cbb4d560e1f04bb0eedde32ce6", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/cpp-expr6-f337f4f.stdout b/tests/reference/cpp-expr6-f337f4f.stdout index 0346806d15..5274919065 100644 --- a/tests/reference/cpp-expr6-f337f4f.stdout +++ b/tests/reference/cpp-expr6-f337f4f.stdout @@ -22,8 +22,8 @@ void test_ifexp() int b; bool c; a = 2; - b = (a == 2) ? (6) : (8); - c = (b > 5) ? (true) : (false); + b = ((a) == (2)) ? (6) : (8); + c = ((b) > (5)) ? (true) : (false); } namespace { diff --git a/tests/reference/cpp-expr8-704cece.json b/tests/reference/cpp-expr8-704cece.json index 97209a402c..b874e9be8f 100644 --- a/tests/reference/cpp-expr8-704cece.json +++ b/tests/reference/cpp-expr8-704cece.json @@ -6,7 +6,7 @@ "outfile": null, "outfile_hash": null, "stdout": "cpp-expr8-704cece.stdout", - "stdout_hash": "8201234d9cf1e4521f7ef37c1ef3532c18044040aff13375e18ed5ad", + "stdout_hash": "7ee2bbf33a887c51a69b8a04620a44e888e43e3eae7dda60c0fa0902", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/cpp-expr8-704cece.stdout b/tests/reference/cpp-expr8-704cece.stdout index 70204a536c..3a5def1c12 100644 --- a/tests/reference/cpp-expr8-704cece.stdout +++ b/tests/reference/cpp-expr8-704cece.stdout @@ -22,9 +22,9 @@ void test_binop() float x2; x = std::pow(2, 3); x = std::pow((float)(2), 3.500000); - x = 54 - 100; - x2 = 3.454000 - 765.430000 + 534.600000; - x2 = 5346.565000*3.450000; + x = (54) - (100); + x2 = 3.454000 - (765.430000) + 534.600000; + x2 = (5346.565000)*(3.450000); x2 = std::pow(5346.565000, 3.450000); } diff --git a/tests/reference/cpp-loop1-0a8cf3b.json b/tests/reference/cpp-loop1-0a8cf3b.json index 4249ed711b..56cc1954a1 100644 --- a/tests/reference/cpp-loop1-0a8cf3b.json +++ b/tests/reference/cpp-loop1-0a8cf3b.json @@ -6,7 +6,7 @@ "outfile": null, "outfile_hash": null, "stdout": "cpp-loop1-0a8cf3b.stdout", - "stdout_hash": "4b9970204be628f2b99634dbc980e6219bda60ec7dd4a6f3860335fe", + "stdout_hash": "e596712869c18025d8e9993c6207a63136f83ca10e84cdd8f46a7db7", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/cpp-loop1-0a8cf3b.stdout b/tests/reference/cpp-loop1-0a8cf3b.stdout index b587034cc1..643f8e6419 100644 --- a/tests/reference/cpp-loop1-0a8cf3b.stdout +++ b/tests/reference/cpp-loop1-0a8cf3b.stdout @@ -34,13 +34,13 @@ int test_factorial_1(int x) { int _lpython_return_variable; int result; - if (x < 0) { + if ((x) < (0)) { _lpython_return_variable = 0; return _lpython_return_variable; } result = 1; while (x > 0) { - result = result*x; + result = (result)*(x); x = x - 1; } _lpython_return_variable = result; @@ -53,8 +53,8 @@ int test_factorial_2(int x) int i; int result; result = 1; - for (i=1; i<=x + 1 - 1; i++) { - result = result*i; + for (i=1; i<=x + 1 - (1); i++) { + result = (result)*(i); } _lpython_return_variable = result; return _lpython_return_variable; @@ -70,7 +70,7 @@ long long test_factorial_3(int x) } result = 1; while (x > 0) { - result = result*x; + result = (result)*(x); x = x - 1; } _lpython_return_variable = result; diff --git a/tests/reference/cpp-loop2-0686fc4.json b/tests/reference/cpp-loop2-0686fc4.json index b1529981c2..0193d65a3f 100644 --- a/tests/reference/cpp-loop2-0686fc4.json +++ b/tests/reference/cpp-loop2-0686fc4.json @@ -6,7 +6,7 @@ "outfile": null, "outfile_hash": null, "stdout": "cpp-loop2-0686fc4.stdout", - "stdout_hash": "eef4f1ff5b5b8880a2e873cd82808099e1f3612117a10667599f5d62", + "stdout_hash": "d8b2dfb87470779026f1a7b6fba2764d9f6c0f9fdd87645fe7f64e27", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/cpp-loop2-0686fc4.stdout b/tests/reference/cpp-loop2-0686fc4.stdout index 6b586afe53..9cc0165d85 100644 --- a/tests/reference/cpp-loop2-0686fc4.stdout +++ b/tests/reference/cpp-loop2-0686fc4.stdout @@ -19,11 +19,11 @@ Kokkos::View from_std_vector(const std::vector &v) void test_for() { int i; - for (i=0; i<=10 - 1; i++) { + for (i=0; i<=(10) - (1); i++) { if (i == 0) { continue; } - if (i > 5) { + if ((i) > (5)) { break; } if (i == 3) { diff --git a/tests/reference/cpp-loop3-6020091.json b/tests/reference/cpp-loop3-6020091.json index d674114412..ee51031a19 100644 --- a/tests/reference/cpp-loop3-6020091.json +++ b/tests/reference/cpp-loop3-6020091.json @@ -6,7 +6,7 @@ "outfile": null, "outfile_hash": null, "stdout": "cpp-loop3-6020091.stdout", - "stdout_hash": "72ed067095f13bce8cae196ddc18c063f5c4f3cce8761113f6f6ef37", + "stdout_hash": "3dbf6499114cbfd179acb4c331577a68384de4c3096d6aeedc41bdbb", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/cpp-loop3-6020091.stdout b/tests/reference/cpp-loop3-6020091.stdout index cc15ea1570..8162be688b 100644 --- a/tests/reference/cpp-loop3-6020091.stdout +++ b/tests/reference/cpp-loop3-6020091.stdout @@ -20,7 +20,7 @@ void test_pass() { int a; a = 1; - while (a > 0) { + while ((a) > (0)) { } } From 79cf0233adc4c67198a5aa985e8c69e8263355db Mon Sep 17 00:00:00 2001 From: Naman Gera Date: Tue, 22 Feb 2022 03:38:09 +0530 Subject: [PATCH 8/9] Apply suggestions from code review --- src/libasr/asr_utils.h | 6 +- src/libasr/codegen/asr_to_cpp.cpp | 60 +++++++++++++++++-- tests/reference/cpp-assert1-ba60925.json | 2 +- tests/reference/cpp-assert1-ba60925.stdout | 2 +- .../cpp-doconcurrentloop_01-4e9f274.json | 2 +- .../cpp-doconcurrentloop_01-4e9f274.stdout | 6 +- tests/reference/cpp-expr2-09c05ad.json | 2 +- tests/reference/cpp-expr2-09c05ad.stdout | 6 +- tests/reference/cpp-expr3-9c516d4.json | 2 +- tests/reference/cpp-expr3-9c516d4.stdout | 6 +- tests/reference/cpp-expr6-f337f4f.json | 2 +- tests/reference/cpp-expr6-f337f4f.stdout | 4 +- tests/reference/cpp-expr8-704cece.json | 2 +- tests/reference/cpp-expr8-704cece.stdout | 6 +- tests/reference/cpp-loop1-0a8cf3b.json | 2 +- tests/reference/cpp-loop1-0a8cf3b.stdout | 10 ++-- tests/reference/cpp-loop2-0686fc4.json | 2 +- tests/reference/cpp-loop2-0686fc4.stdout | 4 +- tests/reference/cpp-loop3-6020091.json | 2 +- tests/reference/cpp-loop3-6020091.stdout | 2 +- 20 files changed, 89 insertions(+), 41 deletions(-) diff --git a/src/libasr/asr_utils.h b/src/libasr/asr_utils.h index 8c03e61267..95293fe47e 100644 --- a/src/libasr/asr_utils.h +++ b/src/libasr/asr_utils.h @@ -159,7 +159,7 @@ static inline std::string binop_to_str(const ASR::binopType t) { case (ASR::binopType::Sub): { return " - "; } case (ASR::binopType::Mul): { return "*"; } case (ASR::binopType::Div): { return "/"; } - default : throw LFortranException("Not implemented"); + default : throw LFortranException("Cannot represent the binary operator as a string"); } } @@ -171,7 +171,7 @@ static inline std::string cmpop_to_str(const ASR::cmpopType t) { case (ASR::cmpopType::LtE): { return " <= "; } case (ASR::cmpopType::Gt): { return " > "; } case (ASR::cmpopType::GtE): { return " >= "; } - default : throw LFortranException("Not implemented"); + default : throw LFortranException("Cannot represent the comparison as a string"); } } @@ -181,7 +181,7 @@ static inline std::string boolop_to_str(const ASR::boolopType t) { case (ASR::boolopType::Or): { return " || "; } case (ASR::boolopType::Eqv): { return " == "; } case (ASR::boolopType::NEqv): { return " != "; } - default : throw LFortranException("Not implemented"); + default : throw LFortranException("Cannot represent the boolean operator as a string"); } } diff --git a/src/libasr/codegen/asr_to_cpp.cpp b/src/libasr/codegen/asr_to_cpp.cpp index bf187fceb1..fcd30c1809 100644 --- a/src/libasr/codegen/asr_to_cpp.cpp +++ b/src/libasr/codegen/asr_to_cpp.cpp @@ -519,6 +519,7 @@ Kokkos::View from_std_vector(const std::vector &v) } src = fn_name + "(" + args + ")"; } + last_expr_precedence = 2; } void visit_Assignment(const ASR::Assignment_t &x) { @@ -539,18 +540,22 @@ Kokkos::View from_std_vector(const std::vector &v) void visit_ConstantInteger(const ASR::ConstantInteger_t &x) { src = std::to_string(x.m_n); + last_expr_precedence = 2; } void visit_ConstantReal(const ASR::ConstantReal_t &x) { src = std::to_string(x.m_r); + last_expr_precedence = 2; } void visit_ConstantString(const ASR::ConstantString_t &x) { src = "\"" + std::string(x.m_s) + "\""; + last_expr_precedence = 2; } void visit_ConstantComplex(const ASR::ConstantComplex_t &x) { src = "{" + std::to_string(x.m_re) + ", " + std::to_string(x.m_im) + "}"; + last_expr_precedence = 2; } void visit_ConstantLogical(const ASR::ConstantLogical_t &x) { @@ -559,6 +564,7 @@ Kokkos::View from_std_vector(const std::vector &v) } else { src = "false"; } + last_expr_precedence = 2; } void visit_ConstantSet(const ASR::ConstantSet_t &x) { @@ -571,11 +577,13 @@ Kokkos::View from_std_vector(const std::vector &v) } out += "}"; src = out; + last_expr_precedence = 2; } void visit_Var(const ASR::Var_t &x) { const ASR::symbol_t *s = ASRUtils::symbol_get_past_external(x.m_v); src = ASR::down_cast(s)->m_name; + last_expr_precedence = 2; } void visit_ArrayRef(const ASR::ArrayRef_t &x) { @@ -592,6 +600,7 @@ Kokkos::View from_std_vector(const std::vector &v) if (i < x.n_args-1) out += ", "; } out += "-1]"; + last_expr_precedence = 2; src = out; } @@ -608,6 +617,7 @@ Kokkos::View from_std_vector(const std::vector &v) } out += "}"; src = out; + last_expr_precedence = 2; } void visit_ImplicitCast(const ASR::ImplicitCast_t &x) { @@ -637,6 +647,7 @@ Kokkos::View from_std_vector(const std::vector &v) } default : throw CodeGenError("Cast kind " + std::to_string(x.m_kind) + " not implemented"); } + last_expr_precedence = 2; } void visit_Compare(const ASR::Compare_t &x) { @@ -670,15 +681,33 @@ Kokkos::View from_std_vector(const std::vector &v) void visit_UnaryOp(const ASR::UnaryOp_t &x) { this->visit_expr(*x.m_operand); + int expr_precedence = last_expr_precedence; if (x.m_type->type == ASR::ttypeType::Integer) { if (x.m_op == ASR::unaryopType::UAdd) { // src = src; + // Skip unary plus, keep the previous precedence } else if (x.m_op == ASR::unaryopType::USub) { - src = "-" + src; + last_expr_precedence = 3; + if (expr_precedence <= last_expr_precedence) { + src = "-" + src; + } else { + src = "-(" + src + ")"; + } } else if (x.m_op == ASR::unaryopType::Invert) { - src = "~" + src; + last_expr_precedence = 3; + if (expr_precedence <= last_expr_precedence) { + src = "~" + src; + } else { + src = "~(" + src + ")"; + } + } else if (x.m_op == ASR::unaryopType::Not) { - src = "!" + src; + last_expr_precedence = 3; + if (expr_precedence <= last_expr_precedence) { + src = "!" + src; + } else { + src = "!(" + src + ")"; + } } else { throw CodeGenError("Unary type not implemented yet for Integer"); } @@ -686,17 +715,33 @@ Kokkos::View from_std_vector(const std::vector &v) } else if (x.m_type->type == ASR::ttypeType::Real) { if (x.m_op == ASR::unaryopType::UAdd) { // src = src; + // Skip unary plus, keep the previous precedence } else if (x.m_op == ASR::unaryopType::USub) { - src = "-" + src; + last_expr_precedence = 3; + if (expr_precedence <= last_expr_precedence) { + src = "-" + src; + } else { + src = "-(" + src + ")"; + } } else if (x.m_op == ASR::unaryopType::Not) { - src = "!" + src; + last_expr_precedence = 3; + if (expr_precedence <= last_expr_precedence) { + src = "!" + src; + } else { + src = "!(" + src + ")"; + } } else { throw CodeGenError("Unary type not implemented yet for Real"); } return; } else if (x.m_type->type == ASR::ttypeType::Logical) { if (x.m_op == ASR::unaryopType::Not) { - src = "!" + src; + last_expr_precedence = 3; + if (expr_precedence <= last_expr_precedence) { + src = "!" + src; + } else { + src = "!(" + src + ")"; + } return; } else { throw CodeGenError("Unary type not implemented yet for Logical"); @@ -817,6 +862,7 @@ Kokkos::View from_std_vector(const std::vector &v) } out += "})"; src = out; + last_expr_precedence = 2; } void visit_Print(const ASR::Print_t &x) { @@ -958,6 +1004,7 @@ Kokkos::View from_std_vector(const std::vector &v) std::string indent(indentation_level*indentation_spaces, ' '); std::string out = indent + " /* FIXME: implied do loop */ "; src = out; + last_expr_precedence = 2; } void visit_DoLoop(const ASR::DoLoop_t &x) { @@ -1077,6 +1124,7 @@ Kokkos::View from_std_vector(const std::vector &v) visit_expr(*x.m_orelse); out += src + ")"; src = out; + last_expr_precedence = 16; } void visit_SubroutineCall(const ASR::SubroutineCall_t &x) { diff --git a/tests/reference/cpp-assert1-ba60925.json b/tests/reference/cpp-assert1-ba60925.json index decff75b10..475ff3a486 100644 --- a/tests/reference/cpp-assert1-ba60925.json +++ b/tests/reference/cpp-assert1-ba60925.json @@ -6,7 +6,7 @@ "outfile": null, "outfile_hash": null, "stdout": "cpp-assert1-ba60925.stdout", - "stdout_hash": "14c793b7b19362e829cdff759adedf2ae156cef0e663cca99a751cc0", + "stdout_hash": "341ab8b76eaf0af8a4b2d8b901fdf11fb8e3e3bfc541ed0dc078d5a7", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/cpp-assert1-ba60925.stdout b/tests/reference/cpp-assert1-ba60925.stdout index 7b734a9341..601e2052c0 100644 --- a/tests/reference/cpp-assert1-ba60925.stdout +++ b/tests/reference/cpp-assert1-ba60925.stdout @@ -20,7 +20,7 @@ void test_assert() { int a; a = 5; - assert (("a is not 5", (a) == (5))); + assert (("a is not 5", a == 5)); assert (a != 10); } diff --git a/tests/reference/cpp-doconcurrentloop_01-4e9f274.json b/tests/reference/cpp-doconcurrentloop_01-4e9f274.json index 56e8d095c6..e61aa4466a 100644 --- a/tests/reference/cpp-doconcurrentloop_01-4e9f274.json +++ b/tests/reference/cpp-doconcurrentloop_01-4e9f274.json @@ -6,7 +6,7 @@ "outfile": null, "outfile_hash": null, "stdout": "cpp-doconcurrentloop_01-4e9f274.stdout", - "stdout_hash": "cd85c3a1a360f70db5ee96b33a26258c8f985b73534e4c766128b79e", + "stdout_hash": "ebbc468b9812b751d5770f98b4f9ea7772a30af7a9b2d30852c302ff", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/cpp-doconcurrentloop_01-4e9f274.stdout b/tests/reference/cpp-doconcurrentloop_01-4e9f274.stdout index 57df167c78..c3e73b2336 100644 --- a/tests/reference/cpp-doconcurrentloop_01-4e9f274.stdout +++ b/tests/reference/cpp-doconcurrentloop_01-4e9f274.stdout @@ -30,7 +30,7 @@ void main0() float scalar; scalar = 10.000000; nsize = 1234; - Kokkos::parallel_for(Kokkos::RangePolicy(1, (nsize) - (1)+1), KOKKOS_LAMBDA(const long i) { + Kokkos::parallel_for(Kokkos::RangePolicy(1, nsize - 1+1), KOKKOS_LAMBDA(const long i) { a[i-1] = 5.000000; b[i-1] = 5.000000; }); @@ -42,8 +42,8 @@ void triad(const Kokkos::View &a, const Kokkos::View &b, float s { int N; N = 1234; - Kokkos::parallel_for(Kokkos::RangePolicy(1, N - (1)+1), KOKKOS_LAMBDA(const long i) { - c[i-1] = a[i-1] + (scalar)*(b[i-1]); + Kokkos::parallel_for(Kokkos::RangePolicy(1, N - 1+1), KOKKOS_LAMBDA(const long i) { + c[i-1] = a[i-1] + scalar*b[i-1]; }); } diff --git a/tests/reference/cpp-expr2-09c05ad.json b/tests/reference/cpp-expr2-09c05ad.json index c0fa2e4a96..8563dad7a6 100644 --- a/tests/reference/cpp-expr2-09c05ad.json +++ b/tests/reference/cpp-expr2-09c05ad.json @@ -6,7 +6,7 @@ "outfile": null, "outfile_hash": null, "stdout": "cpp-expr2-09c05ad.stdout", - "stdout_hash": "fa6bfa261838534311f57b41cc1b67a574675a8286eb616a4ee28b7e", + "stdout_hash": "e47a4114241b0783c7f5e9fbda982d0af447f1cc14d18fc85253ec05", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/cpp-expr2-09c05ad.stdout b/tests/reference/cpp-expr2-09c05ad.stdout index da67a1f111..a7a88691e3 100644 --- a/tests/reference/cpp-expr2-09c05ad.stdout +++ b/tests/reference/cpp-expr2-09c05ad.stdout @@ -22,11 +22,11 @@ void test_boolOp() bool b; a = false; b = true; - a = (a) && (b); + a = a && b; b = a || true; a = a || b; - a = (a) && (b) == (b); - a = a && (b) != (b); + a = a && b == b; + a = a && b != b; a = b || b; } diff --git a/tests/reference/cpp-expr3-9c516d4.json b/tests/reference/cpp-expr3-9c516d4.json index 320f36508a..8e9b5cd77e 100644 --- a/tests/reference/cpp-expr3-9c516d4.json +++ b/tests/reference/cpp-expr3-9c516d4.json @@ -6,7 +6,7 @@ "outfile": null, "outfile_hash": null, "stdout": "cpp-expr3-9c516d4.stdout", - "stdout_hash": "fe9bf45e7eabfcf0a8491dd85ae9d569a878909ad0455062207e1a94", + "stdout_hash": "1d13d271de569db70607c1811c0c719440ae26a7fd5a357c8910a78a", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/cpp-expr3-9c516d4.stdout b/tests/reference/cpp-expr3-9c516d4.stdout index 4803c0adfd..754d3a40e4 100644 --- a/tests/reference/cpp-expr3-9c516d4.stdout +++ b/tests/reference/cpp-expr3-9c516d4.stdout @@ -22,11 +22,11 @@ void test_cast() float b; a = 2; b = 4.200000; - a = ((float)(a))*(b); + a = (float)(a)*b; b = b + (float)(1); a = 5; - a = (float)(a) - (3.900000); - a = ((float)(a))/(b); + a = (float)(a) - 3.900000; + a = (float)(a)/b; b = (float)(3)/(float)(4); if ((float)(a) < b) { std::cout << "a < b" << std::endl; diff --git a/tests/reference/cpp-expr6-f337f4f.json b/tests/reference/cpp-expr6-f337f4f.json index a06578f7fd..0ef22aa356 100644 --- a/tests/reference/cpp-expr6-f337f4f.json +++ b/tests/reference/cpp-expr6-f337f4f.json @@ -6,7 +6,7 @@ "outfile": null, "outfile_hash": null, "stdout": "cpp-expr6-f337f4f.stdout", - "stdout_hash": "98c4ae8b307378dcfcc21a4d2d39f5cbb4d560e1f04bb0eedde32ce6", + "stdout_hash": "1171d1e704952c3d465082fbde015a10f9bc7dc675357b66e6080211", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/cpp-expr6-f337f4f.stdout b/tests/reference/cpp-expr6-f337f4f.stdout index 5274919065..0346806d15 100644 --- a/tests/reference/cpp-expr6-f337f4f.stdout +++ b/tests/reference/cpp-expr6-f337f4f.stdout @@ -22,8 +22,8 @@ void test_ifexp() int b; bool c; a = 2; - b = ((a) == (2)) ? (6) : (8); - c = ((b) > (5)) ? (true) : (false); + b = (a == 2) ? (6) : (8); + c = (b > 5) ? (true) : (false); } namespace { diff --git a/tests/reference/cpp-expr8-704cece.json b/tests/reference/cpp-expr8-704cece.json index b874e9be8f..97209a402c 100644 --- a/tests/reference/cpp-expr8-704cece.json +++ b/tests/reference/cpp-expr8-704cece.json @@ -6,7 +6,7 @@ "outfile": null, "outfile_hash": null, "stdout": "cpp-expr8-704cece.stdout", - "stdout_hash": "7ee2bbf33a887c51a69b8a04620a44e888e43e3eae7dda60c0fa0902", + "stdout_hash": "8201234d9cf1e4521f7ef37c1ef3532c18044040aff13375e18ed5ad", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/cpp-expr8-704cece.stdout b/tests/reference/cpp-expr8-704cece.stdout index 3a5def1c12..70204a536c 100644 --- a/tests/reference/cpp-expr8-704cece.stdout +++ b/tests/reference/cpp-expr8-704cece.stdout @@ -22,9 +22,9 @@ void test_binop() float x2; x = std::pow(2, 3); x = std::pow((float)(2), 3.500000); - x = (54) - (100); - x2 = 3.454000 - (765.430000) + 534.600000; - x2 = (5346.565000)*(3.450000); + x = 54 - 100; + x2 = 3.454000 - 765.430000 + 534.600000; + x2 = 5346.565000*3.450000; x2 = std::pow(5346.565000, 3.450000); } diff --git a/tests/reference/cpp-loop1-0a8cf3b.json b/tests/reference/cpp-loop1-0a8cf3b.json index 56cc1954a1..4249ed711b 100644 --- a/tests/reference/cpp-loop1-0a8cf3b.json +++ b/tests/reference/cpp-loop1-0a8cf3b.json @@ -6,7 +6,7 @@ "outfile": null, "outfile_hash": null, "stdout": "cpp-loop1-0a8cf3b.stdout", - "stdout_hash": "e596712869c18025d8e9993c6207a63136f83ca10e84cdd8f46a7db7", + "stdout_hash": "4b9970204be628f2b99634dbc980e6219bda60ec7dd4a6f3860335fe", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/cpp-loop1-0a8cf3b.stdout b/tests/reference/cpp-loop1-0a8cf3b.stdout index 643f8e6419..b587034cc1 100644 --- a/tests/reference/cpp-loop1-0a8cf3b.stdout +++ b/tests/reference/cpp-loop1-0a8cf3b.stdout @@ -34,13 +34,13 @@ int test_factorial_1(int x) { int _lpython_return_variable; int result; - if ((x) < (0)) { + if (x < 0) { _lpython_return_variable = 0; return _lpython_return_variable; } result = 1; while (x > 0) { - result = (result)*(x); + result = result*x; x = x - 1; } _lpython_return_variable = result; @@ -53,8 +53,8 @@ int test_factorial_2(int x) int i; int result; result = 1; - for (i=1; i<=x + 1 - (1); i++) { - result = (result)*(i); + for (i=1; i<=x + 1 - 1; i++) { + result = result*i; } _lpython_return_variable = result; return _lpython_return_variable; @@ -70,7 +70,7 @@ long long test_factorial_3(int x) } result = 1; while (x > 0) { - result = (result)*(x); + result = result*x; x = x - 1; } _lpython_return_variable = result; diff --git a/tests/reference/cpp-loop2-0686fc4.json b/tests/reference/cpp-loop2-0686fc4.json index 0193d65a3f..b1529981c2 100644 --- a/tests/reference/cpp-loop2-0686fc4.json +++ b/tests/reference/cpp-loop2-0686fc4.json @@ -6,7 +6,7 @@ "outfile": null, "outfile_hash": null, "stdout": "cpp-loop2-0686fc4.stdout", - "stdout_hash": "d8b2dfb87470779026f1a7b6fba2764d9f6c0f9fdd87645fe7f64e27", + "stdout_hash": "eef4f1ff5b5b8880a2e873cd82808099e1f3612117a10667599f5d62", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/cpp-loop2-0686fc4.stdout b/tests/reference/cpp-loop2-0686fc4.stdout index 9cc0165d85..6b586afe53 100644 --- a/tests/reference/cpp-loop2-0686fc4.stdout +++ b/tests/reference/cpp-loop2-0686fc4.stdout @@ -19,11 +19,11 @@ Kokkos::View from_std_vector(const std::vector &v) void test_for() { int i; - for (i=0; i<=(10) - (1); i++) { + for (i=0; i<=10 - 1; i++) { if (i == 0) { continue; } - if ((i) > (5)) { + if (i > 5) { break; } if (i == 3) { diff --git a/tests/reference/cpp-loop3-6020091.json b/tests/reference/cpp-loop3-6020091.json index ee51031a19..d674114412 100644 --- a/tests/reference/cpp-loop3-6020091.json +++ b/tests/reference/cpp-loop3-6020091.json @@ -6,7 +6,7 @@ "outfile": null, "outfile_hash": null, "stdout": "cpp-loop3-6020091.stdout", - "stdout_hash": "3dbf6499114cbfd179acb4c331577a68384de4c3096d6aeedc41bdbb", + "stdout_hash": "72ed067095f13bce8cae196ddc18c063f5c4f3cce8761113f6f6ef37", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/cpp-loop3-6020091.stdout b/tests/reference/cpp-loop3-6020091.stdout index 8162be688b..cc15ea1570 100644 --- a/tests/reference/cpp-loop3-6020091.stdout +++ b/tests/reference/cpp-loop3-6020091.stdout @@ -20,7 +20,7 @@ void test_pass() { int a; a = 1; - while ((a) > (0)) { + while (a > 0) { } } From 0ad0add8a62773c7d9fa0586e6301a2ef01ee476 Mon Sep 17 00:00:00 2001 From: Naman Gera Date: Wed, 23 Feb 2022 05:15:48 +0530 Subject: [PATCH 9/9] Codegen: Use `last_expr_precedence` in expr visitors --- src/libasr/codegen/asr_to_cpp.cpp | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/src/libasr/codegen/asr_to_cpp.cpp b/src/libasr/codegen/asr_to_cpp.cpp index fcd30c1809..a2f3fb1c5a 100644 --- a/src/libasr/codegen/asr_to_cpp.cpp +++ b/src/libasr/codegen/asr_to_cpp.cpp @@ -799,17 +799,22 @@ Kokkos::View from_std_vector(const std::vector &v) void visit_StrOp(const ASR::StrOp_t &x) { this->visit_expr(*x.m_left); - std::string left_val = src; + std::string left = std::move(src); + int left_precedence = last_expr_precedence; this->visit_expr(*x.m_right); - std::string right_val = src; - switch (x.m_op) { - case (ASR::stropType::Concat): { - src = "std::string(" + left_val + ") + std::string(" + right_val + ")"; - break; - } - case (ASR::stropType::Repeat): { - // TODO: implement - } + std::string right = std::move(src); + int right_precedence = last_expr_precedence; + last_expr_precedence = 6; + if (left_precedence <= last_expr_precedence) { + src += "std::string(" + left + ")"; + } else { + src += left; + } + src += " + "; // handle only concatenation for now + if (right_precedence <= last_expr_precedence) { + src += "std::string(" + right + ")"; + } else { + src += right; } }