// ================== ExprResult AddExpression::Add // ================== ( ExprResult& left, ExprResult& right ) const { // Create a result, and initialise its status as undefined. ExprResult result( m_strPrecision ); result.SetStatus( ExprResult::UNDEF_STATUS ); // Convert any possible UNSPECIFIED operand. left.ConvertFromUnspecified(); right.ConvertFromUnspecified(); // Both operands must now have arithmetic types; check if either // operand has the type FLOAT. if ( left.IsFloat() || right.IsFloat() ) { // Convert both to FLOAT. left.ConvertToFloat(); right.ConvertToFloat(); } // Get the numerical values of the operands. double* pNumValLeft = left.GetNumValue(); double* pNumValRight = right.GetNumValue(); // Check if the numerical values exist; i.e. check if no // null pointers were returned. if ( pNumValLeft != 0 && pNumValRight != 0 ) { // Calculate the numerical value of the result by adding // the numerical values of the operands. double dlNumVal = (*pNumValLeft) + (*pNumValRight); // Since both operands now have the same type, check the type // of the left operand to check if both have type INT. if ( left.IsInt() ) { // Take as value of the result the conversion of its // numerical value to a string; not using exponential // notation, and with a precision of 0. Then, set the // result type to INT. result.SetValue( DoubleAsIntToStr( dlNumVal ) ); result.SetType( ExprResult::INT ); } else { // Take as value of the result the conversion of its // numerical value to a string, using a precision of // ADDPRECISION. Then, set the result type to FLOAT. result.SetValue( DoubleToStr( dlNumVal, ADDPRECISION ) ); result.SetType( ExprResult::FLOAT ); } // Set the result status to OK. result.SetStatus( ExprResult::OK ); } else { // Although the operands were of arithmetic type, no mumerical // value could be determined; internal error. result.SetStatus( ExprResult::INTERNAL_ERROR ); } return result; }