-
Notifications
You must be signed in to change notification settings - Fork 171
Fix logical comparison #2598
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix logical comparison #2598
Changes from 8 commits
3e87bc9
e25820b
b79daf5
94530a2
bc2de09
bcc1c42
5a3213b
2eb2edd
2376719
60a4a1b
46ab7be
64b1fd2
1013838
430eca4
2473f76
6441912
5aebcb6
cc5aaa8
94041a2
e644814
4973f05
a827e1c
332a987
cd26f8f
74b8d51
a756338
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3326,29 +3326,48 @@ class CommonVisitor : public AST::BaseVisitor<Struct> { | |
x.base.base.loc); | ||
} | ||
} | ||
LCOMPILERS_ASSERT( | ||
ASRUtils::check_equal_type(ASRUtils::expr_type(lhs), ASRUtils::expr_type(rhs))); | ||
ASR::ttype_t *left_operand_type = ASRUtils::expr_type(lhs); | ||
ASR::ttype_t *right_operand_type = ASRUtils::expr_type(rhs); | ||
|
||
ASR::expr_t *value = nullptr; | ||
ASR::ttype_t *dest_type = ASRUtils::expr_type(lhs); | ||
ASR::ttype_t *dest_type = left_operand_type; | ||
|
||
if (ASR::is_a<ASR::Character_t>(*left_operand_type)) { | ||
throw SemanticError("Logical operation not supported on object of type 'str'", lhs->base.loc); | ||
} | ||
if (ASR::is_a<ASR::Character_t>(*right_operand_type)) { | ||
throw SemanticError("Logical operation not supported on object of type 'str'", rhs->base.loc); | ||
} | ||
if (ASRUtils::expr_value(lhs) != nullptr && ASRUtils::expr_value(rhs) != nullptr) { | ||
kmr-srbh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (ASR::is_a<ASR::Logical_t>(*left_operand_type) | ||
&& ASR::is_a<ASR::Logical_t>(*right_operand_type)) { | ||
|
||
LCOMPILERS_ASSERT(ASR::is_a<ASR::Logical_t>(*dest_type)); | ||
bool left_value = ASR::down_cast<ASR::LogicalConstant_t>( | ||
ASRUtils::expr_value(lhs))->m_value; | ||
bool right_value = ASR::down_cast<ASR::LogicalConstant_t>( | ||
ASRUtils::expr_value(rhs))->m_value; | ||
bool result; | ||
switch (op) { | ||
case (ASR::logicalbinopType::And): { result = left_value && right_value; break; } | ||
case (ASR::logicalbinopType::Or): { result = left_value || right_value; break; } | ||
default : { | ||
throw SemanticError("Boolean operator type not supported", | ||
x.base.base.loc); | ||
bool left_value = ASR::down_cast<ASR::LogicalConstant_t>( | ||
ASRUtils::expr_value(lhs))->m_value; | ||
bool right_value = ASR::down_cast<ASR::LogicalConstant_t>( | ||
ASRUtils::expr_value(rhs))->m_value; | ||
bool result; | ||
switch (op) { | ||
case (ASR::logicalbinopType::And): { result = left_value && right_value; break; } | ||
case (ASR::logicalbinopType::Or): { result = left_value || right_value; break; } | ||
default : { | ||
throw SemanticError("Boolean operator type not supported", | ||
x.base.base.loc); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No change was made in this section. Only indentation was added. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems an assert There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Shaikh-Ubaid Removal of that assert statement was intentional. In this case, indentation was required to add readability to the |
||
} | ||
value = ASR::down_cast<ASR::expr_t>(ASR::make_LogicalConstant_t( | ||
al, x.base.base.loc, result, dest_type)); | ||
} else if(ASR::is_a<ASR::Logical_t>(*left_operand_type) | ||
&& !ASR::is_a<ASR::Logical_t>(*right_operand_type)) { | ||
throw SemanticError("Type mismatch: '" + ASRUtils::type_to_str_python(left_operand_type) | ||
+ "' and '" + ASRUtils::type_to_str_python(right_operand_type) | ||
+ "'. Operand should be of type 'bool'", rhs->base.loc); | ||
} else if(!ASR::is_a<ASR::Logical_t>(*left_operand_type) | ||
&& ASR::is_a<ASR::Logical_t>(*right_operand_type)) { | ||
throw SemanticError("Type mismatch: '" + ASRUtils::type_to_str_python(left_operand_type) | ||
+ "' and '" + ASRUtils::type_to_str_python(right_operand_type) | ||
+ "'. Operand should be of type 'bool'", lhs->base.loc); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This checking will not be needed once we have https://github.com/lcompilers/lpython/pull/2598/files#r1527569879. |
||
value = ASR::down_cast<ASR::expr_t>(ASR::make_LogicalConstant_t( | ||
al, x.base.base.loc, result, dest_type)); | ||
} | ||
tmp = ASR::make_LogicalBinOp_t(al, x.base.base.loc, lhs, op, rhs, dest_type, value); | ||
} | ||
|
Uh oh!
There was an error while loading. Please reload this page.