Skip to content

Commit a7121a4

Browse files
authored
Supporting Logical Binop cases through symbolic pass (#2709)
* Supporting Logical Binop cases through symbolic pass * Added tests
1 parent 04de1f1 commit a7121a4

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

integration_tests/symbolics_02.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,16 @@ def test_symbolic_operations():
108108
assert(b.is_positive == False)
109109
assert(c.is_positive == False)
110110

111+
# logical binop check
112+
l1: bool = True and p.func == Pow
113+
l2: bool = False or p.func == Pow
114+
l3: bool = False and u.func == Mul
115+
l4: bool = True or u.func == Add
116+
if p.func == Pow and u.func == Mul:
117+
print(True)
118+
assert(l1)
119+
assert(l2)
120+
assert(not l3)
121+
assert(l4)
122+
111123
test_symbolic_operations()

src/libasr/pass/replace_symbolic.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,32 @@ class ReplaceSymbolicVisitor : public PassUtils::PassVisitor<ReplaceSymbolicVisi
722722
ASR::stmt_t* stmt = ASRUtils::STMT(ASR::make_Assignment_t(al, x.base.base.loc, x.m_target, function_call, nullptr));
723723
pass_result.push_back(al, stmt);
724724
}
725+
} else if (ASR::is_a<ASR::LogicalBinOp_t>(*x.m_value)) {
726+
ASR::LogicalBinOp_t* logical_binop = ASR::down_cast<ASR::LogicalBinOp_t>(x.m_value);
727+
ASR::expr_t* function_call_left = logical_binop->m_left;
728+
ASR::expr_t* function_call_right = logical_binop->m_right;
729+
730+
if (ASR::is_a<ASR::IntrinsicElementalFunction_t>(*logical_binop->m_left)) {
731+
ASR::IntrinsicElementalFunction_t* left = ASR::down_cast<ASR::IntrinsicElementalFunction_t>(logical_binop->m_left);
732+
if (left->m_type->type == ASR::ttypeType::Logical) {
733+
if (is_logical_intrinsic_symbolic(logical_binop->m_left)) {
734+
function_call_left = process_attributes(x.base.base.loc, logical_binop->m_left);
735+
}
736+
}
737+
}
738+
if (ASR::is_a<ASR::IntrinsicElementalFunction_t>(*logical_binop->m_right)) {
739+
ASR::IntrinsicElementalFunction_t* right = ASR::down_cast<ASR::IntrinsicElementalFunction_t>(logical_binop->m_right);
740+
if (right->m_type->type == ASR::ttypeType::Logical) {
741+
if (is_logical_intrinsic_symbolic(logical_binop->m_right)) {
742+
function_call_right = process_attributes(x.base.base.loc, logical_binop->m_right);
743+
}
744+
}
745+
}
746+
747+
ASR::expr_t* new_logical_binop = ASRUtils::EXPR(ASR::make_LogicalBinOp_t(al, x.base.base.loc,
748+
function_call_left, logical_binop->m_op, function_call_right, logical_binop->m_type, logical_binop->m_value));
749+
ASR::stmt_t* stmt = ASRUtils::STMT(ASR::make_Assignment_t(al, x.base.base.loc, x.m_target, new_logical_binop, nullptr));
750+
pass_result.push_back(al, stmt);
725751
}
726752
}
727753

@@ -761,6 +787,31 @@ class ReplaceSymbolicVisitor : public PassUtils::PassVisitor<ReplaceSymbolicVisi
761787
ASR::stmt_t* stmt = ASRUtils::STMT(ASR::make_If_t(al, xx.base.base.loc, function_call,
762788
xx.m_body, xx.n_body, xx.m_orelse, xx.n_orelse));
763789
pass_result.push_back(al, stmt);
790+
} else if (ASR::is_a<ASR::LogicalBinOp_t>(*xx.m_test)) {
791+
ASR::LogicalBinOp_t* logical_binop = ASR::down_cast<ASR::LogicalBinOp_t>(xx.m_test);
792+
ASR::expr_t* function_call_left = logical_binop->m_left;
793+
ASR::expr_t* function_call_right = logical_binop->m_right;
794+
795+
if (ASR::is_a<ASR::IntrinsicElementalFunction_t>(*logical_binop->m_left)) {
796+
ASR::IntrinsicElementalFunction_t* left = ASR::down_cast<ASR::IntrinsicElementalFunction_t>(logical_binop->m_left);
797+
if (left->m_type->type == ASR::ttypeType::Logical) {
798+
if (is_logical_intrinsic_symbolic(logical_binop->m_left)) {
799+
function_call_left = process_attributes(xx.base.base.loc, logical_binop->m_left);
800+
}
801+
}
802+
}
803+
if (ASR::is_a<ASR::IntrinsicElementalFunction_t>(*logical_binop->m_right)) {
804+
ASR::IntrinsicElementalFunction_t* right = ASR::down_cast<ASR::IntrinsicElementalFunction_t>(logical_binop->m_right);
805+
if (right->m_type->type == ASR::ttypeType::Logical) {
806+
if (is_logical_intrinsic_symbolic(logical_binop->m_right)) {
807+
function_call_right = process_attributes(xx.base.base.loc, logical_binop->m_right);
808+
}
809+
}
810+
}
811+
812+
ASR::expr_t* new_logical_binop = ASRUtils::EXPR(ASR::make_LogicalBinOp_t(al, xx.base.base.loc,
813+
function_call_left, logical_binop->m_op, function_call_right, logical_binop->m_type, logical_binop->m_value));
814+
xx.m_test = new_logical_binop;
764815
}
765816
}
766817

0 commit comments

Comments
 (0)