double Evaluator::eval(BinaryOp &e) { double left = e.getLeft()->eval(*this); double right = e.getRight()->eval(*this); if (right == 0 && (e.getType() == Operator::DIV_OP || e.getType() == Operator::MOD_OP)) { LOG_ERROR(e.getLocation() << ": Divide by zero"); return 0; } switch (e.getType()) { case Operator::EXP_OP: return pow(left, right); case Operator::MUL_OP: return left * right; case Operator::DIV_OP: return left / right; case Operator::MOD_OP: return fmod(left, right); case Operator::ADD_OP: return left + right; case Operator::SUB_OP: return left - right; case Operator::EQ_OP: return left == right; case Operator::NE_OP: return left != right; case Operator::GT_OP: return left > right; case Operator::GE_OP: return left >= right; case Operator::LT_OP: return left < right; case Operator::LE_OP: return left <= right; case Operator::AND_OP: return left && right; case Operator::OR_OP: return left || right; case Operator::XOR_OP: return (bool)left ^ (bool)right; default: THROWS(e.getLocation() << " Invalid binary operator"); } }
double Evaluator::eval(BinaryOp &e) { double left = e.getLeft()->eval(*this); double right = e.getRight()->eval(*this); if (right == 0 && (e.getType() == Operator::DIV_OP || e.getType() == Operator::MOD_OP)) { LOG_ERROR(e.getLocation() << ": Divide by zero"); return 0; } // NOTE, LinuxCNC handles floating-point equality differently. // See ``Equality and floating-point values``. switch (e.getType()) { case Operator::EXP_OP: return pow(left, right); case Operator::MUL_OP: return left * right; case Operator::DIV_OP: return left / right; case Operator::MOD_OP: return fmod(left, right); case Operator::ADD_OP: return left + right; case Operator::SUB_OP: return left - right; case Operator::EQ_OP: return left == right; case Operator::NE_OP: return left != right; case Operator::GT_OP: return left > right; case Operator::GE_OP: return left >= right; case Operator::LT_OP: return left < right; case Operator::LE_OP: return left <= right; case Operator::AND_OP: return left && right; case Operator::OR_OP: return left || right; case Operator::XOR_OP: return (bool)left ^ (bool)right; default: THROW(e.getLocation() << " Invalid binary operator"); } }