Skip to content

Commit

Permalink
Raise Semantic Error for bitwise operations on floats
Browse files Browse the repository at this point in the history
  • Loading branch information
namannimmo10 authored and certik committed Jun 21, 2022
1 parent c40f41e commit 326383c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/libasr/asr_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,12 @@ 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 "/"; }
case (ASR::binopType::Mod): { return "%"; }
case (ASR::binopType::BitAnd): { return "&"; }
case (ASR::binopType::BitOr): { return "|"; }
case (ASR::binopType::BitXor): { return "^"; }
case (ASR::binopType::BitLShift): { return "<<"; }
case (ASR::binopType::BitRShift): { return ">>"; }
default : throw LFortranException("Cannot represent the binary operator as a string");
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/lpython/semantics/python_ast_to_asr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1279,6 +1279,11 @@ class CommonVisitor : public AST::BaseVisitor<Derived> {

} else if (ASRUtils::is_real(*dest_type)) {

if (op == ASR::binopType::BitAnd || op == ASR::binopType::BitOr || op == ASR::binopType::BitXor ||
op == ASR::binopType::BitLShift || op == ASR::binopType::BitRShift) {
throw SemanticError("Unsupported binary operation on floats: '" + ASRUtils::binop_to_str(op) + "'", loc);
}

if (ASRUtils::expr_value(left) != nullptr && ASRUtils::expr_value(right) != nullptr) {
double left_value = ASR::down_cast<ASR::RealConstant_t>(
ASRUtils::expr_value(left))->m_r;
Expand All @@ -1291,6 +1296,7 @@ class CommonVisitor : public AST::BaseVisitor<Derived> {
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::Mod): { result = std::fmod(left_value, right_value); break; }
default: { LFORTRAN_ASSERT(false); }
}
value = ASR::down_cast<ASR::expr_t>(ASR::make_RealConstant_t(
Expand Down

0 comments on commit 326383c

Please sign in to comment.