Skip to content

Commit

Permalink
Add implicitcast LogicalToInteger
Browse files Browse the repository at this point in the history
  • Loading branch information
Smit-create committed Mar 30, 2022
1 parent 345b873 commit fecdd9e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 6 deletions.
1 change: 1 addition & 0 deletions grammar/ASR.asdl
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ cast_kind
| IntegerToLogical
| ComplexToComplex
| ComplexToReal
| LogicalToInteger

dimension = (expr? start, expr? end)

Expand Down
52 changes: 46 additions & 6 deletions src/lpython/semantics/python_ast_to_asr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -495,12 +495,56 @@ class CommonVisitor : public AST::BaseVisitor<Derived> {
return ASR::down_cast<ASR::expr_t>(ASR::make_ImplicitCast_t(
al, right->base.loc, right, ASR::cast_kindType::IntegerToComplex,
left_type, nullptr));
} else if (ASRUtils::is_logical(*right_type)) {
ASR::ttype_t* int_type = ASRUtils::TYPE(ASR::make_Integer_t(al,
right->base.loc, 4, nullptr, 0));
right = ASR::down_cast<ASR::expr_t>(ASR::make_ImplicitCast_t(
al, right->base.loc, right, ASR::cast_kindType::LogicalToInteger, int_type,
nullptr));
return ASR::down_cast<ASR::expr_t>(ASR::make_ImplicitCast_t(
al, right->base.loc, right, ASR::cast_kindType::IntegerToComplex, left_type,
nullptr));
} else {
std::string rtype = ASRUtils::type_to_str(right_type);
throw SemanticError("Casting " + rtype + " to complex is not Implemented",
right->base.loc);
}
}
if (!is_assign) { // This will only be used for BinOp
if (ASRUtils::is_logical(*left_type) && ASRUtils::is_logical(*right_type)) {
ASR::ttype_t* int_type = ASRUtils::TYPE(ASR::make_Integer_t(al,
right->base.loc, 4, nullptr, 0));
return ASR::down_cast<ASR::expr_t>(ASR::make_ImplicitCast_t(
al, right->base.loc, right, ASR::cast_kindType::LogicalToInteger, int_type,
nullptr));
} else if (ASRUtils::is_logical(*right_type)) {
ASR::ttype_t* int_type = ASRUtils::TYPE(ASR::make_Integer_t(al,
right->base.loc, 4, nullptr, 0));
if (ASRUtils::is_integer(*left_type)) {
return ASR::down_cast<ASR::expr_t>(ASR::make_ImplicitCast_t(
al, right->base.loc, right, ASR::cast_kindType::LogicalToInteger, int_type,
nullptr));
} else if (ASRUtils::is_real(*left_type)) {
right = ASR::down_cast<ASR::expr_t>(ASR::make_ImplicitCast_t(
al, right->base.loc, right, ASR::cast_kindType::LogicalToInteger, int_type,
nullptr));
return ASR::down_cast<ASR::expr_t>(ASR::make_ImplicitCast_t(
al, right->base.loc, right, ASR::cast_kindType::IntegerToReal, left_type,
nullptr));
} else if (ASRUtils::is_complex(*left_type)) {
right = ASR::down_cast<ASR::expr_t>(ASR::make_ImplicitCast_t(
al, right->base.loc, right, ASR::cast_kindType::LogicalToInteger, int_type,
nullptr));
return ASR::down_cast<ASR::expr_t>(ASR::make_ImplicitCast_t(
al, right->base.loc, right, ASR::cast_kindType::IntegerToComplex, left_type,
nullptr));
} else {
std::string ltype = ASRUtils::type_to_str(left_type);
throw SemanticError("Binary Operation not implemented for bool and " + ltype,
right->base.loc);
}
}
}
return right;
}

Expand Down Expand Up @@ -562,9 +606,9 @@ class CommonVisitor : public AST::BaseVisitor<Derived> {
}
}
} else if((ASRUtils::is_integer(*left_type) || ASRUtils::is_real(*left_type) ||
ASRUtils::is_complex(*left_type)) &&
ASRUtils::is_complex(*left_type) || ASRUtils::is_logical(*left_type)) &&
(ASRUtils::is_integer(*right_type) || ASRUtils::is_real(*right_type) ||
ASRUtils::is_complex(*right_type))) {
ASRUtils::is_complex(*right_type) || ASRUtils::is_logical(*left_type))) {
left = implicitcast_helper(ASRUtils::expr_type(right), left);
right = implicitcast_helper(ASRUtils::expr_type(left), right);
dest_type = ASRUtils::expr_type(left);
Expand Down Expand Up @@ -638,10 +682,6 @@ class CommonVisitor : public AST::BaseVisitor<Derived> {
value);
return;

} else if (ASRUtils::is_complex(*left_type) && ASRUtils::is_complex(*right_type)) {
dest_type = left_type;
} else if (ASRUtils::is_logical(*left_type) && ASRUtils::is_logical(*right_type)) {
dest_type = left_type;
} else {
std::string ltype = ASRUtils::type_to_str(ASRUtils::expr_type(left));
std::string rtype = ASRUtils::type_to_str(ASRUtils::expr_type(right));
Expand Down

0 comments on commit fecdd9e

Please sign in to comment.