From 919d0963a19abbd6964025a0b4a8d9c3949a3342 Mon Sep 17 00:00:00 2001 From: Oshanath Rajawasam Date: Mon, 28 Mar 2022 20:01:39 +0530 Subject: [PATCH 1/8] complex fix --- examples/a.py | 10 ++++++++++ src/libasr/asr_utils.cpp | 34 +++++++++++++++++++++++++++++++++- src/libasr/asr_utils.h | 2 ++ 3 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 examples/a.py diff --git a/examples/a.py b/examples/a.py new file mode 100644 index 0000000000..4297c2dfc7 --- /dev/null +++ b/examples/a.py @@ -0,0 +1,10 @@ +def main0(): + # a:f32 + # a = 5.6 + 4 + # print(a) + + b:c32 + b = 3.4 + 5j + print(b) + +main0() diff --git a/src/libasr/asr_utils.cpp b/src/libasr/asr_utils.cpp index 5b5f7ecd39..1010fadf2d 100644 --- a/src/libasr/asr_utils.cpp +++ b/src/libasr/asr_utils.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -713,7 +714,38 @@ ASR::asr_t* symbol_resolve_external_generic_procedure_without_eval( } } - } // namespace ASRUtils +ASR::asr_t* make_ImplicitCast_t_value(Allocator &al, const Location &a_loc, ASR::expr_t* a_arg, ASR::cast_kindType a_kind, ASR::ttype_t* a_type) { + ASR::ImplicitCast_t *n = (ASR::ImplicitCast_t *)ASR::make_ImplicitCast_t( + al, a_loc, a_arg, a_kind, a_type, nullptr); + + if (ASRUtils::expr_value(a_arg) != nullptr){ + // calculate value + if(a_kind == ASR::cast_kindType::RealToInteger){ + int64_t value = ASR::down_cast(ASRUtils::expr_value(a_arg))->m_r; + n->m_value = ASR::down_cast(ASR::make_ConstantInteger_t(al, a_loc, value, a_type)); + } + else if(a_kind == ASR::cast_kindType::IntegerToReal){ + double value = ASR::down_cast(ASRUtils::expr_value(a_arg))->m_n; + n->m_value = ASR::down_cast(ASR::make_ConstantReal_t(al, a_loc, value, a_type)); + } + else if(a_kind == ASR::cast_kindType::IntegerToComplex){ + int64_t int_value = ASR::down_cast(ASRUtils::expr_value(a_arg))->m_n; + std::complex value(int_value, 0); + n->m_value = ASR::down_cast(ASR::make_ConstantComplex_t(al, a_loc, + std::real(value), std::imag(value), a_type)); + } + else if(a_kind == ASR::cast_kindType::RealToComplex){ + double double_value = ASR::down_cast(ASRUtils::expr_value(a_arg))->m_r; + std::complex value(double_value, 0); + n->m_value = ASR::down_cast(ASR::make_ConstantComplex_t(al, a_loc, + std::real(value), std::imag(value), a_type)); + } + } + + return (ASR::asr_t*)n; +} + +} // namespace ASRUtils } // namespace LFortran diff --git a/src/libasr/asr_utils.h b/src/libasr/asr_utils.h index 07151b0502..1e7a9c9af6 100644 --- a/src/libasr/asr_utils.h +++ b/src/libasr/asr_utils.h @@ -873,6 +873,8 @@ ASR::asr_t* symbol_resolve_external_generic_procedure_without_eval( SymbolTable* current_scope, Allocator& al, const std::function err); +ASR::asr_t* make_ImplicitCast_t_value(Allocator &al, const Location &a_loc, ASR::expr_t* a_arg, ASR::cast_kindType a_kind, ASR::ttype_t* a_type); + } // namespace ASRUtils } // namespace LFortran From aa93254def2a2b6dc934fef1b2c682e4440a14e7 Mon Sep 17 00:00:00 2001 From: Oshanath Rajawasam Date: Mon, 28 Mar 2022 21:04:21 +0530 Subject: [PATCH 2/8] middle --- examples/a.py | 10 ++--- integration_tests/test_builtin_pow.py | 24 +++++------ src/libasr/asr_utils.cpp | 44 +++++++++++++++++---- src/lpython/semantics/python_ast_to_asr.cpp | 25 ++++++------ 4 files changed, 64 insertions(+), 39 deletions(-) diff --git a/examples/a.py b/examples/a.py index 4297c2dfc7..b79f409b42 100644 --- a/examples/a.py +++ b/examples/a.py @@ -1,10 +1,6 @@ def main0(): - # a:f32 - # a = 5.6 + 4 - # print(a) - - b:c32 - b = 3.4 + 5j - print(b) + eps: f64 + eps = 1e-12 + print(pow(2, -2)) main0() diff --git a/integration_tests/test_builtin_pow.py b/integration_tests/test_builtin_pow.py index bcc6137185..b84bd549f6 100644 --- a/integration_tests/test_builtin_pow.py +++ b/integration_tests/test_builtin_pow.py @@ -38,18 +38,18 @@ def test_pow(): assert abs(pow(x, 5.5) - 420.8883462392372) < eps assert abs(pow(2, -1) - 0.5) < eps - assert abs(pow(6, -4) - 0.0007716049382716049) < eps - assert abs(pow(-3, -5) + 0.00411522633744856) < eps - assert abs(pow(6, -4) - 0.0007716049382716049) < eps - assert abs(pow(4.5, 2.3) - 31.7971929089206) < eps - assert abs(pow(2.3, 0.0) - 1.0) < eps - assert abs(pow(2.3, -1.5) - 0.2866871623459944) < eps - assert abs(pow(2, 3.4) - 10.556063286183154) < eps - assert abs(pow(2, -3.4) - 0.09473228540689989) < eps - assert abs(pow(3.4, 9) - 60716.99276646398) < eps - assert abs(pow(0.0, 53) - 0.0) < eps - assert pow(4, 2) == 16 - assert abs(pow(-4235.0, 52) - 3.948003805985264e+188) < eps + # assert abs(pow(6, -4) - 0.0007716049382716049) < eps + # assert abs(pow(-3, -5) + 0.00411522633744856) < eps + # assert abs(pow(6, -4) - 0.0007716049382716049) < eps + # assert abs(pow(4.5, 2.3) - 31.7971929089206) < eps + # assert abs(pow(2.3, 0.0) - 1.0) < eps + # assert abs(pow(2.3, -1.5) - 0.2866871623459944) < eps + # assert abs(pow(2, 3.4) - 10.556063286183154) < eps + # assert abs(pow(2, -3.4) - 0.09473228540689989) < eps + # assert abs(pow(3.4, 9) - 60716.99276646398) < eps + # assert abs(pow(0.0, 53) - 0.0) < eps + # assert pow(4, 2) == 16 + # assert abs(pow(-4235.0, 52) - 3.948003805985264e+188) < eps test_pow() diff --git a/src/libasr/asr_utils.cpp b/src/libasr/asr_utils.cpp index 1010fadf2d..5c45ed256e 100644 --- a/src/libasr/asr_utils.cpp +++ b/src/libasr/asr_utils.cpp @@ -724,22 +724,50 @@ ASR::asr_t* make_ImplicitCast_t_value(Allocator &al, const Location &a_loc, ASR: int64_t value = ASR::down_cast(ASRUtils::expr_value(a_arg))->m_r; n->m_value = ASR::down_cast(ASR::make_ConstantInteger_t(al, a_loc, value, a_type)); } - else if(a_kind == ASR::cast_kindType::IntegerToReal){ - double value = ASR::down_cast(ASRUtils::expr_value(a_arg))->m_n; + else if(a_kind == ASR::cast_kindType::RealToReal){ + double value = ASR::down_cast(ASRUtils::expr_value(a_arg))->m_r; n->m_value = ASR::down_cast(ASR::make_ConstantReal_t(al, a_loc, value, a_type)); } - else if(a_kind == ASR::cast_kindType::IntegerToComplex){ - int64_t int_value = ASR::down_cast(ASRUtils::expr_value(a_arg))->m_n; - std::complex value(int_value, 0); - n->m_value = ASR::down_cast(ASR::make_ConstantComplex_t(al, a_loc, - std::real(value), std::imag(value), a_type)); - } else if(a_kind == ASR::cast_kindType::RealToComplex){ double double_value = ASR::down_cast(ASRUtils::expr_value(a_arg))->m_r; std::complex value(double_value, 0); n->m_value = ASR::down_cast(ASR::make_ConstantComplex_t(al, a_loc, std::real(value), std::imag(value), a_type)); } + + else if(a_kind == ASR::cast_kindType::IntegerToReal){ + int64_t value = ASR::down_cast(ASRUtils::expr_value(a_arg))->m_n; + n->m_value = ASR::down_cast(ASR::make_ConstantReal_t(al, a_loc, (double)value, a_type)); + } + // else if(a_kind == ASR::cast_kindType::IntegerToComplex){ + // int64_t int_value = ASR::down_cast(ASRUtils::expr_value(a_arg))->m_n; + // std::complex value(int_value, 0); + // n->m_value = ASR::down_cast(ASR::make_ConstantComplex_t(al, a_loc, + // std::real(value), std::imag(value), a_type)); + // } + // else if(a_kind == ASR::cast_kindType::IntegerToInteger){ + // int64_t value = ASR::down_cast(ASRUtils::expr_value(a_arg))->m_n; + // n->m_value = ASR::down_cast(ASR::make_ConstantInteger_t(al, a_loc, value, a_type)); + // } + // else if(a_kind == ASR::cast_kindType::IntegerToLogical){ + // // int64_t value = ASR::down_cast(ASRUtils::expr_value(a_arg))->m_n; + // // n->m_value = ASR::down_cast(ASR::make_ConstantInteger_t(al, a_loc, value, a_type)); + // std::cout << "not yet implemented\n"; + // } + + + // else if(a_kind == ASR::cast_kindType::ComplexToComplex){ + // ASR::ConstantComplex_t* value_complex = ASR::down_cast(ASRUtils::expr_value(a_arg)); + // double real = value_complex->m_re; + // double imag = value_complex->m_im; + // n->m_value = ASR::down_cast(ASR::make_ConstantComplex_t(al, a_loc, real, imag, a_type)); + // } + // else if(a_kind == ASR::cast_kindType::ComplexToReal){ + // ASR::ConstantComplex_t* value_complex = ASR::down_cast(ASRUtils::expr_value(a_arg)); + // double real = value_complex->m_re; + // n->m_value = ASR::down_cast(ASR::make_ConstantReal_t(al, a_loc, real, a_type)); + // } + } return (ASR::asr_t*)n; diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 5bf17e8a2a..1c882ccbd0 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -456,30 +456,30 @@ class CommonVisitor : public AST::BaseVisitor { int lkind = ASR::down_cast(left_type)->m_kind; int rkind = ASR::down_cast(right_type)->m_kind; if ((is_assign && (lkind != rkind)) || (lkind > rkind)) { - return ASR::down_cast(ASR::make_ImplicitCast_t( + return ASR::down_cast(ASRUtils::make_ImplicitCast_t_value( al, right->base.loc, right, ASR::cast_kindType::IntegerToInteger, - left_type, nullptr)); + left_type)); } } else if (ASRUtils::is_real(*left_type) && ASRUtils::is_real(*right_type)) { bool is_l64 = ASR::down_cast(left_type)->m_kind == 8; bool is_r64 = ASR::down_cast(right_type)->m_kind == 8; if ((is_assign && (is_l64 != is_r64)) || (is_l64 && !is_r64)) { - return ASR::down_cast(ASR::make_ImplicitCast_t( + return ASR::down_cast(ASRUtils::make_ImplicitCast_t_value( al, right->base.loc, right, ASR::cast_kindType::RealToReal, - left_type, nullptr)); + left_type)); } } else if (ASRUtils::is_complex(*left_type) && ASRUtils::is_complex(*right_type)) { bool is_l64 = ASR::down_cast(left_type)->m_kind == 8; bool is_r64 = ASR::down_cast(right_type)->m_kind == 8; if ((is_assign && (is_l64 != is_r64)) || (is_l64 && !is_r64)) { - return ASR::down_cast(ASR::make_ImplicitCast_t( + return ASR::down_cast(ASRUtils::make_ImplicitCast_t_value( al, right->base.loc, right, ASR::cast_kindType::ComplexToComplex, - left_type, nullptr)); + left_type)); } } else if (!is_assign && ASRUtils::is_real(*left_type) && ASRUtils::is_integer(*right_type)) { - return ASR::down_cast(ASR::make_ImplicitCast_t( + return ASR::down_cast(ASRUtils::make_ImplicitCast_t_value( al, right->base.loc, right, ASR::cast_kindType::IntegerToReal, - left_type, nullptr)); + left_type)); } else if (is_assign && ASRUtils::is_real(*left_type) && ASRUtils::is_integer(*right_type)) { throw SemanticError("Assigning integer to float is not supported", right->base.loc); @@ -488,13 +488,13 @@ class CommonVisitor : public AST::BaseVisitor { right->base.loc); } else if (!is_assign && ASRUtils::is_complex(*left_type) && !ASRUtils::is_complex(*right_type)) { if (ASRUtils::is_real(*right_type)) { - return ASR::down_cast(ASR::make_ImplicitCast_t( + return ASR::down_cast(ASRUtils::make_ImplicitCast_t_value( al, right->base.loc, right, ASR::cast_kindType::RealToComplex, - left_type, nullptr)); + left_type)); } else if (ASRUtils::is_integer(*right_type)) { - return ASR::down_cast(ASR::make_ImplicitCast_t( + return ASR::down_cast(ASRUtils::make_ImplicitCast_t_value( al, right->base.loc, right, ASR::cast_kindType::IntegerToComplex, - left_type, nullptr)); + left_type)); } else { std::string rtype = ASRUtils::type_to_str(right_type); throw SemanticError("Casting " + rtype + " to complex is not Implemented", @@ -568,6 +568,7 @@ class CommonVisitor : public AST::BaseVisitor { left = implicitcast_helper(ASRUtils::expr_type(right), left); right = implicitcast_helper(ASRUtils::expr_type(left), right); dest_type = ASRUtils::expr_type(left); + } else if ((right_is_int || left_is_int) && op == ASR::binopType::Mul) { // string repeat ASR::stropType ops = ASR::stropType::Repeat; From c0d05c3ba2a9460aa95f5ef18bc20a0d23d1c7cb Mon Sep 17 00:00:00 2001 From: Oshanath Rajawasam Date: Tue, 29 Mar 2022 09:14:30 +0530 Subject: [PATCH 3/8] switching --- examples/a.py | 5 ++- src/lpython/semantics/python_ast_to_asr.cpp | 42 +++++++++++++++------ 2 files changed, 34 insertions(+), 13 deletions(-) diff --git a/examples/a.py b/examples/a.py index b79f409b42..f2989d7609 100644 --- a/examples/a.py +++ b/examples/a.py @@ -1,6 +1,7 @@ def main0(): eps: f64 - eps = 1e-12 - print(pow(2, -2)) + eps = pow(2, -1) + # print(abs(pow(2, -1) - 0.5)) + main0() diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 1c882ccbd0..6d6e0b3d1f 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -561,13 +561,30 @@ class CommonVisitor : public AST::BaseVisitor { value)); } } - } else if((ASRUtils::is_integer(*left_type) || ASRUtils::is_real(*left_type) || + } + else if((ASRUtils::is_integer(*left_type) || ASRUtils::is_real(*left_type) || ASRUtils::is_complex(*left_type)) && (ASRUtils::is_integer(*right_type) || ASRUtils::is_real(*right_type) || ASRUtils::is_complex(*right_type))) { - left = implicitcast_helper(ASRUtils::expr_type(right), left); - right = implicitcast_helper(ASRUtils::expr_type(left), right); - dest_type = ASRUtils::expr_type(left); + std::cout << op << "\n"; + if(op == ASR::binopType::Pow + && ASRUtils::is_integer(*left_type) + && ASRUtils::is_integer(*right_type) + // && ASRUtils::expr_value(right) != nullptr + // && ASR::down_cast(ASRUtils::expr_value(right))->m_n < 0 + ){ + + dest_type = ASRUtils::TYPE(ASR::make_Real_t(al, loc, 8, nullptr, 0)); + left = implicitcast_helper(dest_type, left); + right = implicitcast_helper(dest_type, right); + std::cout << "converting to real \n"; + } + + else{ + left = implicitcast_helper(ASRUtils::expr_type(right), left); + right = implicitcast_helper(ASRUtils::expr_type(left), right); + dest_type = ASRUtils::expr_type(left); + } } else if ((right_is_int || left_is_int) && op == ASR::binopType::Mul) { // string repeat @@ -675,23 +692,26 @@ class CommonVisitor : public AST::BaseVisitor { // Now, compute the result of the binary operations if (ASRUtils::expr_value(left) != nullptr && ASRUtils::expr_value(right) != nullptr) { if (ASRUtils::is_integer(*dest_type)) { + std::cout << "int\n"; int64_t left_value = ASR::down_cast( ASRUtils::expr_value(left))->m_n; int64_t right_value = ASR::down_cast( ASRUtils::expr_value(right))->m_n; int64_t result; switch (op) { - case (ASR::binopType::Add): { result = left_value + right_value; break; } - case (ASR::binopType::Sub): { result = left_value - right_value; break; } - case (ASR::binopType::Mul): { result = left_value * right_value; break; } - case (ASR::binopType::Div): { result = left_value / right_value; break; } - case (ASR::binopType::Pow): { result = std::pow(left_value, right_value); break; } + case (ASR::binopType::Add): {std::cout << "here1\n"; result = left_value + right_value; break; } + case (ASR::binopType::Sub): {std::cout << "here2\n"; result = left_value - right_value; break; } + case (ASR::binopType::Mul): {std::cout << "here3\n"; result = left_value * right_value; break; } + case (ASR::binopType::Div): {std::cout << "here4\n"; result = left_value / right_value; break; } + case (ASR::binopType::Pow): {std::cout << "here5\n"; result = std::pow(left_value, right_value); break; } default: { LFORTRAN_ASSERT(false); } // should never happen } + std::cout << "result " << result << std::endl; value = ASR::down_cast(ASR::make_ConstantInteger_t( al, loc, result, dest_type)); } else if (ASRUtils::is_real(*dest_type)) { + std::cout << "real\n"; double left_value = ASR::down_cast( ASRUtils::expr_value(left))->m_r; double right_value = ASR::down_cast( @@ -699,10 +719,10 @@ class CommonVisitor : public AST::BaseVisitor { double result; switch (op) { case (ASR::binopType::Add): { result = left_value + right_value; break; } - case (ASR::binopType::Sub): { result = left_value - right_value; break; } + case (ASR::binopType::Sub): {std::cout << "here22\n"; result = left_value - right_value; break; } case (ASR::binopType::Mul): { result = left_value * right_value; break; } case (ASR::binopType::Div): { result = left_value / right_value; break; } - case (ASR::binopType::Pow): { result = std::pow(left_value, right_value); break; } + case (ASR::binopType::Pow): {std::cout << "here55\n"; result = std::pow(left_value, right_value); break; } default: { LFORTRAN_ASSERT(false); } } value = ASR::down_cast(ASR::make_ConstantReal_t( From b07fe77aa38b15f0bab4fef01c407d0d1ecea237 Mon Sep 17 00:00:00 2001 From: Oshanath Rajawasam Date: Tue, 29 Mar 2022 10:41:36 +0530 Subject: [PATCH 4/8] step 1 finished --- examples/a.py | 7 --- integration_tests/test_complex.py | 2 +- src/libasr/asr_utils.cpp | 64 +++++++++++---------- src/lpython/semantics/python_ast_to_asr.cpp | 40 ++++--------- 4 files changed, 47 insertions(+), 66 deletions(-) delete mode 100644 examples/a.py diff --git a/examples/a.py b/examples/a.py deleted file mode 100644 index f2989d7609..0000000000 --- a/examples/a.py +++ /dev/null @@ -1,7 +0,0 @@ -def main0(): - eps: f64 - eps = pow(2, -1) - # print(abs(pow(2, -1) - 0.5)) - - -main0() diff --git a/integration_tests/test_complex.py b/integration_tests/test_complex.py index 31962e7460..6482002711 100644 --- a/integration_tests/test_complex.py +++ b/integration_tests/test_complex.py @@ -11,7 +11,7 @@ def test_real_imag(): b = x.imag assert abs(a - 2) < eps # TODO: below test should work - # assert abs(b - 3) < eps + assert abs(b - 3) < eps def test_complex(): x: c64 diff --git a/src/libasr/asr_utils.cpp b/src/libasr/asr_utils.cpp index 5c45ed256e..9bdf74e144 100644 --- a/src/libasr/asr_utils.cpp +++ b/src/libasr/asr_utils.cpp @@ -736,37 +736,43 @@ ASR::asr_t* make_ImplicitCast_t_value(Allocator &al, const Location &a_loc, ASR: } else if(a_kind == ASR::cast_kindType::IntegerToReal){ - int64_t value = ASR::down_cast(ASRUtils::expr_value(a_arg))->m_n; - n->m_value = ASR::down_cast(ASR::make_ConstantReal_t(al, a_loc, (double)value, a_type)); - } - // else if(a_kind == ASR::cast_kindType::IntegerToComplex){ - // int64_t int_value = ASR::down_cast(ASRUtils::expr_value(a_arg))->m_n; - // std::complex value(int_value, 0); - // n->m_value = ASR::down_cast(ASR::make_ConstantComplex_t(al, a_loc, - // std::real(value), std::imag(value), a_type)); - // } - // else if(a_kind == ASR::cast_kindType::IntegerToInteger){ - // int64_t value = ASR::down_cast(ASRUtils::expr_value(a_arg))->m_n; - // n->m_value = ASR::down_cast(ASR::make_ConstantInteger_t(al, a_loc, value, a_type)); - // } - // else if(a_kind == ASR::cast_kindType::IntegerToLogical){ - // // int64_t value = ASR::down_cast(ASRUtils::expr_value(a_arg))->m_n; - // // n->m_value = ASR::down_cast(ASR::make_ConstantInteger_t(al, a_loc, value, a_type)); - // std::cout << "not yet implemented\n"; - // } + try{ + int64_t value = ASR::down_cast(ASRUtils::expr_value(a_arg))->m_n; + n->m_value = ASR::down_cast(ASR::make_ConstantReal_t(al, a_loc, (double)value, a_type)); + } catch(LFortranException e){} + } + else if(a_kind == ASR::cast_kindType::IntegerToComplex){ + int64_t int_value = ASR::down_cast(ASRUtils::expr_value(a_arg))->m_n; + std::complex value(int_value, 0); + n->m_value = ASR::down_cast(ASR::make_ConstantComplex_t(al, a_loc, + std::real(value), std::imag(value), a_type)); + } + else if(a_kind == ASR::cast_kindType::IntegerToInteger){ + // TODO: implement + // ASR::ttype_t *int_type = ASRUtils::TYPE(ASR::make_Integer_t(al, a_loc, 4, nullptr, 0)); + // std::cout << a_type->type << std::endl; + // int64_t value = ASR::down_cast(ASRUtils::expr_value(a_arg))->m_n; + // n->m_value = ASR::down_cast(ASR::make_ConstantInteger_t(al, a_loc, value, a_type)); + + } + else if(a_kind == ASR::cast_kindType::IntegerToLogical){ + // TODO implement + // int64_t value = ASR::down_cast(ASRUtils::expr_value(a_arg))->m_n; + // n->m_value = ASR::down_cast(ASR::make_ConstantInteger_t(al, a_loc, value, a_type)); + } - // else if(a_kind == ASR::cast_kindType::ComplexToComplex){ - // ASR::ConstantComplex_t* value_complex = ASR::down_cast(ASRUtils::expr_value(a_arg)); - // double real = value_complex->m_re; - // double imag = value_complex->m_im; - // n->m_value = ASR::down_cast(ASR::make_ConstantComplex_t(al, a_loc, real, imag, a_type)); - // } - // else if(a_kind == ASR::cast_kindType::ComplexToReal){ - // ASR::ConstantComplex_t* value_complex = ASR::down_cast(ASRUtils::expr_value(a_arg)); - // double real = value_complex->m_re; - // n->m_value = ASR::down_cast(ASR::make_ConstantReal_t(al, a_loc, real, a_type)); - // } + else if(a_kind == ASR::cast_kindType::ComplexToComplex){ + ASR::ConstantComplex_t* value_complex = ASR::down_cast(ASRUtils::expr_value(a_arg)); + double real = value_complex->m_re; + double imag = value_complex->m_im; + n->m_value = ASR::down_cast(ASR::make_ConstantComplex_t(al, a_loc, real, imag, a_type)); + } + else if(a_kind == ASR::cast_kindType::ComplexToReal){ + ASR::ConstantComplex_t* value_complex = ASR::down_cast(ASRUtils::expr_value(a_arg)); + double real = value_complex->m_re; + n->m_value = ASR::down_cast(ASR::make_ConstantReal_t(al, a_loc, real, a_type)); + } } diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 6d6e0b3d1f..56c8183e61 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -566,25 +566,10 @@ class CommonVisitor : public AST::BaseVisitor { ASRUtils::is_complex(*left_type)) && (ASRUtils::is_integer(*right_type) || ASRUtils::is_real(*right_type) || ASRUtils::is_complex(*right_type))) { - std::cout << op << "\n"; - if(op == ASR::binopType::Pow - && ASRUtils::is_integer(*left_type) - && ASRUtils::is_integer(*right_type) - // && ASRUtils::expr_value(right) != nullptr - // && ASR::down_cast(ASRUtils::expr_value(right))->m_n < 0 - ){ - - dest_type = ASRUtils::TYPE(ASR::make_Real_t(al, loc, 8, nullptr, 0)); - left = implicitcast_helper(dest_type, left); - right = implicitcast_helper(dest_type, right); - std::cout << "converting to real \n"; - } - - else{ - left = implicitcast_helper(ASRUtils::expr_type(right), left); - right = implicitcast_helper(ASRUtils::expr_type(left), right); - dest_type = ASRUtils::expr_type(left); - } + + left = implicitcast_helper(ASRUtils::expr_type(right), left); + right = implicitcast_helper(ASRUtils::expr_type(left), right); + dest_type = ASRUtils::expr_type(left); } else if ((right_is_int || left_is_int) && op == ASR::binopType::Mul) { // string repeat @@ -692,26 +677,23 @@ class CommonVisitor : public AST::BaseVisitor { // Now, compute the result of the binary operations if (ASRUtils::expr_value(left) != nullptr && ASRUtils::expr_value(right) != nullptr) { if (ASRUtils::is_integer(*dest_type)) { - std::cout << "int\n"; int64_t left_value = ASR::down_cast( ASRUtils::expr_value(left))->m_n; int64_t right_value = ASR::down_cast( ASRUtils::expr_value(right))->m_n; int64_t result; switch (op) { - case (ASR::binopType::Add): {std::cout << "here1\n"; result = left_value + right_value; break; } - case (ASR::binopType::Sub): {std::cout << "here2\n"; result = left_value - right_value; break; } - case (ASR::binopType::Mul): {std::cout << "here3\n"; result = left_value * right_value; break; } - case (ASR::binopType::Div): {std::cout << "here4\n"; result = left_value / right_value; break; } - case (ASR::binopType::Pow): {std::cout << "here5\n"; result = std::pow(left_value, right_value); break; } + case (ASR::binopType::Add): {result = left_value + right_value; break; } + case (ASR::binopType::Sub): {result = left_value - right_value; break; } + case (ASR::binopType::Mul): {result = left_value * right_value; break; } + case (ASR::binopType::Div): {result = left_value / right_value; break; } + case (ASR::binopType::Pow): {result = std::pow(left_value, right_value); break; } default: { LFORTRAN_ASSERT(false); } // should never happen } - std::cout << "result " << result << std::endl; value = ASR::down_cast(ASR::make_ConstantInteger_t( al, loc, result, dest_type)); } else if (ASRUtils::is_real(*dest_type)) { - std::cout << "real\n"; double left_value = ASR::down_cast( ASRUtils::expr_value(left))->m_r; double right_value = ASR::down_cast( @@ -719,10 +701,10 @@ class CommonVisitor : public AST::BaseVisitor { double result; switch (op) { case (ASR::binopType::Add): { result = left_value + right_value; break; } - case (ASR::binopType::Sub): {std::cout << "here22\n"; result = left_value - right_value; break; } + case (ASR::binopType::Sub): {result = left_value - right_value; break; } case (ASR::binopType::Mul): { result = left_value * right_value; break; } case (ASR::binopType::Div): { result = left_value / right_value; break; } - case (ASR::binopType::Pow): {std::cout << "here55\n"; result = std::pow(left_value, right_value); break; } + case (ASR::binopType::Pow): {result = std::pow(left_value, right_value); break; } default: { LFORTRAN_ASSERT(false); } } value = ASR::down_cast(ASR::make_ConstantReal_t( From 773416598e8c55ea9138737f6d2c6828b630535d Mon Sep 17 00:00:00 2001 From: Oshanath Rajawasam Date: Tue, 29 Mar 2022 10:47:57 +0530 Subject: [PATCH 5/8] parts of the make_Implicitcast_t complete --- src/libasr/asr_utils.cpp | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/src/libasr/asr_utils.cpp b/src/libasr/asr_utils.cpp index 9bdf74e144..3e313eb3d6 100644 --- a/src/libasr/asr_utils.cpp +++ b/src/libasr/asr_utils.cpp @@ -736,10 +736,8 @@ ASR::asr_t* make_ImplicitCast_t_value(Allocator &al, const Location &a_loc, ASR: } else if(a_kind == ASR::cast_kindType::IntegerToReal){ - try{ - int64_t value = ASR::down_cast(ASRUtils::expr_value(a_arg))->m_n; - n->m_value = ASR::down_cast(ASR::make_ConstantReal_t(al, a_loc, (double)value, a_type)); - } catch(LFortranException e){} + int64_t value = ASR::down_cast(ASRUtils::expr_value(a_arg))->m_n; + n->m_value = ASR::down_cast(ASR::make_ConstantReal_t(al, a_loc, (double)value, a_type)); } else if(a_kind == ASR::cast_kindType::IntegerToComplex){ int64_t int_value = ASR::down_cast(ASRUtils::expr_value(a_arg))->m_n; @@ -749,16 +747,11 @@ ASR::asr_t* make_ImplicitCast_t_value(Allocator &al, const Location &a_loc, ASR: } else if(a_kind == ASR::cast_kindType::IntegerToInteger){ // TODO: implement - // ASR::ttype_t *int_type = ASRUtils::TYPE(ASR::make_Integer_t(al, a_loc, 4, nullptr, 0)); - // std::cout << a_type->type << std::endl; // int64_t value = ASR::down_cast(ASRUtils::expr_value(a_arg))->m_n; // n->m_value = ASR::down_cast(ASR::make_ConstantInteger_t(al, a_loc, value, a_type)); - } else if(a_kind == ASR::cast_kindType::IntegerToLogical){ // TODO implement - // int64_t value = ASR::down_cast(ASRUtils::expr_value(a_arg))->m_n; - // n->m_value = ASR::down_cast(ASR::make_ConstantInteger_t(al, a_loc, value, a_type)); } From f5779011b0a8cc2d3815768e091cf2cde8f24a84 Mon Sep 17 00:00:00 2001 From: Oshanath Rajawasam Date: Tue, 29 Mar 2022 10:52:09 +0530 Subject: [PATCH 6/8] all integration tests pass --- integration_tests/test_builtin_pow.py | 24 ++++++++++++------------ src/libasr/asr_utils.cpp | 5 +++-- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/integration_tests/test_builtin_pow.py b/integration_tests/test_builtin_pow.py index b84bd549f6..bcc6137185 100644 --- a/integration_tests/test_builtin_pow.py +++ b/integration_tests/test_builtin_pow.py @@ -38,18 +38,18 @@ def test_pow(): assert abs(pow(x, 5.5) - 420.8883462392372) < eps assert abs(pow(2, -1) - 0.5) < eps - # assert abs(pow(6, -4) - 0.0007716049382716049) < eps - # assert abs(pow(-3, -5) + 0.00411522633744856) < eps - # assert abs(pow(6, -4) - 0.0007716049382716049) < eps - # assert abs(pow(4.5, 2.3) - 31.7971929089206) < eps - # assert abs(pow(2.3, 0.0) - 1.0) < eps - # assert abs(pow(2.3, -1.5) - 0.2866871623459944) < eps - # assert abs(pow(2, 3.4) - 10.556063286183154) < eps - # assert abs(pow(2, -3.4) - 0.09473228540689989) < eps - # assert abs(pow(3.4, 9) - 60716.99276646398) < eps - # assert abs(pow(0.0, 53) - 0.0) < eps - # assert pow(4, 2) == 16 - # assert abs(pow(-4235.0, 52) - 3.948003805985264e+188) < eps + assert abs(pow(6, -4) - 0.0007716049382716049) < eps + assert abs(pow(-3, -5) + 0.00411522633744856) < eps + assert abs(pow(6, -4) - 0.0007716049382716049) < eps + assert abs(pow(4.5, 2.3) - 31.7971929089206) < eps + assert abs(pow(2.3, 0.0) - 1.0) < eps + assert abs(pow(2.3, -1.5) - 0.2866871623459944) < eps + assert abs(pow(2, 3.4) - 10.556063286183154) < eps + assert abs(pow(2, -3.4) - 0.09473228540689989) < eps + assert abs(pow(3.4, 9) - 60716.99276646398) < eps + assert abs(pow(0.0, 53) - 0.0) < eps + assert pow(4, 2) == 16 + assert abs(pow(-4235.0, 52) - 3.948003805985264e+188) < eps test_pow() diff --git a/src/libasr/asr_utils.cpp b/src/libasr/asr_utils.cpp index 3e313eb3d6..13942a2750 100644 --- a/src/libasr/asr_utils.cpp +++ b/src/libasr/asr_utils.cpp @@ -736,8 +736,9 @@ ASR::asr_t* make_ImplicitCast_t_value(Allocator &al, const Location &a_loc, ASR: } else if(a_kind == ASR::cast_kindType::IntegerToReal){ - int64_t value = ASR::down_cast(ASRUtils::expr_value(a_arg))->m_n; - n->m_value = ASR::down_cast(ASR::make_ConstantReal_t(al, a_loc, (double)value, a_type)); + // TODO clashes with the pow functions + // int64_t value = ASR::down_cast(ASRUtils::expr_value(a_arg))->m_n; + // n->m_value = ASR::down_cast(ASR::make_ConstantReal_t(al, a_loc, (double)value, a_type)); } else if(a_kind == ASR::cast_kindType::IntegerToComplex){ int64_t int_value = ASR::down_cast(ASRUtils::expr_value(a_arg))->m_n; From 19443a0ef5fb62c2440d879a17f60a7a7716b06f Mon Sep 17 00:00:00 2001 From: Oshanath Rajawasam Date: Tue, 29 Mar 2022 10:53:42 +0530 Subject: [PATCH 7/8] added some spaces --- src/lpython/semantics/python_ast_to_asr.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 56c8183e61..df3ceccaa8 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -683,11 +683,11 @@ class CommonVisitor : public AST::BaseVisitor { ASRUtils::expr_value(right))->m_n; int64_t result; switch (op) { - case (ASR::binopType::Add): {result = left_value + right_value; break; } - case (ASR::binopType::Sub): {result = left_value - right_value; break; } - case (ASR::binopType::Mul): {result = left_value * right_value; break; } - case (ASR::binopType::Div): {result = left_value / right_value; break; } - case (ASR::binopType::Pow): {result = std::pow(left_value, right_value); break; } + case (ASR::binopType::Add): { result = left_value + right_value; break; } + case (ASR::binopType::Sub): { result = left_value - right_value; break; } + case (ASR::binopType::Mul): { result = left_value * right_value; break; } + case (ASR::binopType::Div): { result = left_value / right_value; break; } + case (ASR::binopType::Pow): { result = std::pow(left_value, right_value); break; } default: { LFORTRAN_ASSERT(false); } // should never happen } value = ASR::down_cast(ASR::make_ConstantInteger_t( @@ -701,10 +701,10 @@ class CommonVisitor : public AST::BaseVisitor { double result; switch (op) { case (ASR::binopType::Add): { result = left_value + right_value; break; } - case (ASR::binopType::Sub): {result = left_value - right_value; break; } + case (ASR::binopType::Sub): { result = left_value - right_value; break; } case (ASR::binopType::Mul): { result = left_value * right_value; break; } case (ASR::binopType::Div): { result = left_value / right_value; break; } - case (ASR::binopType::Pow): {result = std::pow(left_value, right_value); break; } + case (ASR::binopType::Pow): { result = std::pow(left_value, right_value); break; } default: { LFORTRAN_ASSERT(false); } } value = ASR::down_cast(ASR::make_ConstantReal_t( From e4a81f4c6bd75ea75038115a96b8ca3a8b515948 Mon Sep 17 00:00:00 2001 From: Oshanath Rajawasam Date: Tue, 29 Mar 2022 10:55:27 +0530 Subject: [PATCH 8/8] formatting --- src/lpython/semantics/python_ast_to_asr.cpp | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index df3ceccaa8..6a7969592a 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -561,16 +561,13 @@ class CommonVisitor : public AST::BaseVisitor { value)); } } - } - else if((ASRUtils::is_integer(*left_type) || ASRUtils::is_real(*left_type) || + } else if((ASRUtils::is_integer(*left_type) || ASRUtils::is_real(*left_type) || ASRUtils::is_complex(*left_type)) && (ASRUtils::is_integer(*right_type) || ASRUtils::is_real(*right_type) || ASRUtils::is_complex(*right_type))) { - left = implicitcast_helper(ASRUtils::expr_type(right), left); right = implicitcast_helper(ASRUtils::expr_type(left), right); dest_type = ASRUtils::expr_type(left); - } else if ((right_is_int || left_is_int) && op == ASR::binopType::Mul) { // string repeat ASR::stropType ops = ASR::stropType::Repeat;