40 #ifndef headerfilettmathparser 41 #define headerfilettmathparser 137 template<
class ValueType>
159 none,add,sub,mul,div,pow,lt,gt,let,
get,eq,neq,lor,land,shortmul
168 Type GetType()
const {
return type; }
169 int GetPriority()
const {
return priority; }
170 Assoc GetAssoc()
const {
return assoc; }
213 Error( err_internal_error );
218 MatOperator(): type(none), priority(0), assoc(non_right)
242 none, numerical_value, mat_operator, first_bracket,
243 last_bracket, variable, semicolon
253 MatOperator moperator;
265 std::string function_name;
275 Item(): type(none),
function(
false), sign(
false)
307 const int default_stack_size;
316 unsigned int stack_index;
329 const char * pstring;
358 const Objects * puser_variables;
363 const Objects * puser_functions;
366 typedef std::map<std::string, ValueType> FunctionLocalVariables;
371 const FunctionLocalVariables * pfunction_local_variables;
377 std::set<std::string> visited_variables;
383 std::set<std::string> visited_functions;
398 typedef void (
Parser<ValueType>::*pfunction)(
int pstack,
int amount_of_arg, ValueType & result);
404 typedef void (ValueType::*pfunction_var)();
414 typedef std::map<std::string, pfunction> FunctionsTable;
415 FunctionsTable functions_table;
425 typedef std::map<std::string, typename MatOperator::Type> OperatorsTable;
426 OperatorsTable operators_table;
436 typedef std::map<std::string, pfunction_var> VariablesTable;
437 VariablesTable variables_table;
449 std::string wide_to_ansi;
495 void SkipWhiteCharacters()
497 while( (*pstring==
' ' ) || (*pstring==
'\t') )
505 void RecurrenceParsingVariablesOrFunction_CheckStopCondition(
bool variable,
const std::string & name)
509 if( visited_variables.find(name) != visited_variables.end() )
510 Error( err_variable_loop );
514 if( visited_functions.find(name) != visited_functions.end() )
515 Error( err_functions_loop );
523 void RecurrenceParsingVariablesOrFunction_AddName(
bool variable,
const std::string & name)
526 visited_variables.insert( name );
528 visited_functions.insert( name );
535 void RecurrenceParsingVariablesOrFunction_DeleteName(
bool variable,
const std::string & name)
538 visited_variables.erase( name );
540 visited_functions.erase( name );
554 ValueType RecurrenceParsingVariablesOrFunction(
bool variable,
const std::string & name,
const char * new_string,
555 FunctionLocalVariables * local_variables = 0)
557 RecurrenceParsingVariablesOrFunction_CheckStopCondition(variable, name);
558 RecurrenceParsingVariablesOrFunction_AddName(variable, name);
563 NewParser.pfunction_local_variables = local_variables;
567 err = NewParser.Parse(new_string);
571 RecurrenceParsingVariablesOrFunction_DeleteName(variable, name);
576 RecurrenceParsingVariablesOrFunction_DeleteName(variable, name);
581 if( NewParser.
stack.size() != 1 )
582 Error( err_must_be_only_one_value );
584 if( NewParser.
stack[0].type != Item::numerical_value )
586 Error( err_incorrect_value );
588 return NewParser.
stack[0].value;
600 if( !puser_variables )
603 const char * string_value;
605 if( puser_variables->
GetValue(variable_name, &string_value) != err_ok )
608 result = RecurrenceParsingVariablesOrFunction(
true, variable_name, string_value);
620 if( !pfunction_local_variables )
623 typename FunctionLocalVariables::const_iterator i = pfunction_local_variables->find(variable_name);
625 if( i == pfunction_local_variables->end() )
651 typename std::map<std::string, pfunction_var>::iterator i =
652 variables_table.find(variable_name);
654 if( i == variables_table.end() )
655 Error( err_unknown_variable );
657 (result.*(i->second))();
683 ValueType ConvertAngleToRad(
const ValueType & input)
685 if( deg_rad_grad == 1 )
691 if( deg_rad_grad == 0 )
706 ValueType ConvertRadToAngle(
const ValueType & input)
708 if( deg_rad_grad == 1 )
714 if( deg_rad_grad == 0 )
726 void Gamma(
int sindex,
int amount_of_args, ValueType & result)
728 if( amount_of_args != 1 )
729 Error( err_improper_amount_of_arguments );
733 result =
ttmath::Gamma(stack[sindex].value, cgamma, &err, pstop_calculating);
744 void Factorial(
int sindex,
int amount_of_args, ValueType & result)
746 if( amount_of_args != 1 )
747 Error( err_improper_amount_of_arguments );
751 result =
ttmath::Factorial(stack[sindex].value, cgamma, &err, pstop_calculating);
758 void Abs(
int sindex,
int amount_of_args, ValueType & result)
760 if( amount_of_args != 1 )
761 Error( err_improper_amount_of_arguments );
766 void Sin(
int sindex,
int amount_of_args, ValueType & result)
768 if( amount_of_args != 1 )
769 Error( err_improper_amount_of_arguments );
772 result =
ttmath::Sin( ConvertAngleToRad(stack[sindex].value), &err );
778 void Cos(
int sindex,
int amount_of_args, ValueType & result)
780 if( amount_of_args != 1 )
781 Error( err_improper_amount_of_arguments );
784 result =
ttmath::Cos( ConvertAngleToRad(stack[sindex].value), &err );
790 void Tan(
int sindex,
int amount_of_args, ValueType & result)
792 if( amount_of_args != 1 )
793 Error( err_improper_amount_of_arguments );
796 result =
ttmath::Tan(ConvertAngleToRad(stack[sindex].value), &err);
802 void Cot(
int sindex,
int amount_of_args, ValueType & result)
804 if( amount_of_args != 1 )
805 Error( err_improper_amount_of_arguments );
808 result =
ttmath::Cot(ConvertAngleToRad(stack[sindex].value), &err);
814 void Int(
int sindex,
int amount_of_args, ValueType & result)
816 if( amount_of_args != 1 )
817 Error( err_improper_amount_of_arguments );
823 void Round(
int sindex,
int amount_of_args, ValueType & result)
825 if( amount_of_args != 1 )
826 Error( err_improper_amount_of_arguments );
828 result = stack[sindex].value;
831 Error( err_overflow );
835 void Ln(
int sindex,
int amount_of_args, ValueType & result)
837 if( amount_of_args != 1 )
838 Error( err_improper_amount_of_arguments );
841 result =
ttmath::Ln(stack[sindex].value, &err);
847 void Log(
int sindex,
int amount_of_args, ValueType & result)
849 if( amount_of_args != 2 )
850 Error( err_improper_amount_of_arguments );
853 result =
ttmath::Log(stack[sindex].value, stack[sindex+2].value, &err);
859 void Exp(
int sindex,
int amount_of_args, ValueType & result)
861 if( amount_of_args != 1 )
862 Error( err_improper_amount_of_arguments );
872 void Max(
int sindex,
int amount_of_args, ValueType & result)
874 if( amount_of_args == 0 )
881 result = stack[sindex].value;
883 for(
int i=1 ; i<amount_of_args ; ++i)
885 if( result < stack[sindex + i*2].value )
886 result = stack[sindex + i*2].value;
891 void Min(
int sindex,
int amount_of_args, ValueType & result)
893 if( amount_of_args == 0 )
900 result = stack[sindex].value;
902 for(
int i=1 ; i<amount_of_args ; ++i)
904 if( result > stack[sindex + i*2].value )
905 result = stack[sindex + i*2].value;
910 void ASin(
int sindex,
int amount_of_args, ValueType & result)
912 if( amount_of_args != 1 )
913 Error( err_improper_amount_of_arguments );
916 ValueType temp =
ttmath::ASin(stack[sindex].value, &err);
921 result = ConvertRadToAngle(temp);
925 void ACos(
int sindex,
int amount_of_args, ValueType & result)
927 if( amount_of_args != 1 )
928 Error( err_improper_amount_of_arguments );
931 ValueType temp =
ttmath::ACos(stack[sindex].value, &err);
936 result = ConvertRadToAngle(temp);
940 void ATan(
int sindex,
int amount_of_args, ValueType & result)
942 if( amount_of_args != 1 )
943 Error( err_improper_amount_of_arguments );
945 result = ConvertRadToAngle(
ttmath::ATan(stack[sindex].value));
949 void ACot(
int sindex,
int amount_of_args, ValueType & result)
951 if( amount_of_args != 1 )
952 Error( err_improper_amount_of_arguments );
954 result = ConvertRadToAngle(
ttmath::ACot(stack[sindex].value));
958 void Sgn(
int sindex,
int amount_of_args, ValueType & result)
960 if( amount_of_args != 1 )
961 Error( err_improper_amount_of_arguments );
967 void Mod(
int sindex,
int amount_of_args, ValueType & result)
969 if( amount_of_args != 2 )
970 Error( err_improper_amount_of_arguments );
972 if( stack[sindex+2].value.IsZero() )
973 Error( err_improper_argument );
975 result = stack[sindex].value;
976 uint c = result.Mod(stack[sindex+2].value);
979 Error( err_overflow );
983 void If(
int sindex,
int amount_of_args, ValueType & result)
985 if( amount_of_args != 3 )
986 Error( err_improper_amount_of_arguments );
989 if( !stack[sindex].value.IsZero() )
990 result = stack[sindex+2].value;
992 result = stack[sindex+4].value;
996 void Or(
int sindex,
int amount_of_args, ValueType & result)
998 if( amount_of_args < 2 )
999 Error( err_improper_amount_of_arguments );
1001 for(
int i=0 ; i<amount_of_args ; ++i)
1003 if( !stack[sindex+i*2].value.IsZero() )
1014 void And(
int sindex,
int amount_of_args, ValueType & result)
1016 if( amount_of_args < 2 )
1017 Error( err_improper_amount_of_arguments );
1019 for(
int i=0 ; i<amount_of_args ; ++i)
1021 if( stack[sindex+i*2].value.IsZero() )
1032 void Not(
int sindex,
int amount_of_args, ValueType & result)
1034 if( amount_of_args != 1 )
1035 Error( err_improper_amount_of_arguments );
1038 if( stack[sindex].value.IsZero() )
1045 void DegToRad(
int sindex,
int amount_of_args, ValueType & result)
1049 if( amount_of_args == 1 )
1054 if( amount_of_args == 3 )
1057 stack[sindex+4].value, &err);
1060 Error( err_improper_amount_of_arguments );
1068 void RadToDeg(
int sindex,
int amount_of_args, ValueType & result)
1072 if( amount_of_args != 1 )
1073 Error( err_improper_amount_of_arguments );
1082 void DegToDeg(
int sindex,
int amount_of_args, ValueType & result)
1084 if( amount_of_args != 3 )
1085 Error( err_improper_amount_of_arguments );
1089 stack[sindex+4].value, &err);
1096 void GradToRad(
int sindex,
int amount_of_args, ValueType & result)
1100 if( amount_of_args != 1 )
1101 Error( err_improper_amount_of_arguments );
1110 void RadToGrad(
int sindex,
int amount_of_args, ValueType & result)
1114 if( amount_of_args != 1 )
1115 Error( err_improper_amount_of_arguments );
1124 void DegToGrad(
int sindex,
int amount_of_args, ValueType & result)
1128 if( amount_of_args == 1 )
1133 if( amount_of_args == 3 )
1136 stack[sindex+4].value, &err);
1139 Error( err_improper_amount_of_arguments );
1147 void GradToDeg(
int sindex,
int amount_of_args, ValueType & result)
1151 if( amount_of_args != 1 )
1152 Error( err_improper_amount_of_arguments );
1161 void Ceil(
int sindex,
int amount_of_args, ValueType & result)
1163 if( amount_of_args != 1 )
1164 Error( err_improper_amount_of_arguments );
1174 void Floor(
int sindex,
int amount_of_args, ValueType & result)
1176 if( amount_of_args != 1 )
1177 Error( err_improper_amount_of_arguments );
1186 void Sqrt(
int sindex,
int amount_of_args, ValueType & result)
1188 if( amount_of_args != 1 )
1189 Error( err_improper_amount_of_arguments );
1199 void Sinh(
int sindex,
int amount_of_args, ValueType & result)
1201 if( amount_of_args != 1 )
1202 Error( err_improper_amount_of_arguments );
1212 void Cosh(
int sindex,
int amount_of_args, ValueType & result)
1214 if( amount_of_args != 1 )
1215 Error( err_improper_amount_of_arguments );
1225 void Tanh(
int sindex,
int amount_of_args, ValueType & result)
1227 if( amount_of_args != 1 )
1228 Error( err_improper_amount_of_arguments );
1238 void Coth(
int sindex,
int amount_of_args, ValueType & result)
1240 if( amount_of_args != 1 )
1241 Error( err_improper_amount_of_arguments );
1251 void Root(
int sindex,
int amount_of_args, ValueType & result)
1253 if( amount_of_args != 2 )
1254 Error( err_improper_amount_of_arguments );
1257 result =
ttmath::Root(stack[sindex].value, stack[sindex+2].value, &err);
1265 void ASinh(
int sindex,
int amount_of_args, ValueType & result)
1267 if( amount_of_args != 1 )
1268 Error( err_improper_amount_of_arguments );
1278 void ACosh(
int sindex,
int amount_of_args, ValueType & result)
1280 if( amount_of_args != 1 )
1281 Error( err_improper_amount_of_arguments );
1291 void ATanh(
int sindex,
int amount_of_args, ValueType & result)
1293 if( amount_of_args != 1 )
1294 Error( err_improper_amount_of_arguments );
1304 void ACoth(
int sindex,
int amount_of_args, ValueType & result)
1306 if( amount_of_args != 1 )
1307 Error( err_improper_amount_of_arguments );
1317 void BitAnd(
int sindex,
int amount_of_args, ValueType & result)
1319 if( amount_of_args != 2 )
1320 Error( err_improper_amount_of_arguments );
1323 result = stack[sindex].value;
1324 err = result.BitAnd(stack[sindex+2].value);
1329 Error( err_overflow );
1332 Error( err_improper_argument );
1337 void BitOr(
int sindex,
int amount_of_args, ValueType & result)
1339 if( amount_of_args != 2 )
1340 Error( err_improper_amount_of_arguments );
1343 result = stack[sindex].value;
1344 err = result.BitOr(stack[sindex+2].value);
1349 Error( err_overflow );
1352 Error( err_improper_argument );
1358 void BitXor(
int sindex,
int amount_of_args, ValueType & result)
1360 if( amount_of_args != 2 )
1361 Error( err_improper_amount_of_arguments );
1364 result = stack[sindex].value;
1365 err = result.BitXor(stack[sindex+2].value);
1370 Error( err_overflow );
1373 Error( err_improper_argument );
1379 void Sum(
int sindex,
int amount_of_args, ValueType & result)
1381 if( amount_of_args == 0 )
1382 Error( err_improper_amount_of_arguments );
1384 result = stack[sindex].value;
1386 for(
int i=1 ; i<amount_of_args ; ++i )
1387 if( result.Add( stack[ sindex + i*2 ].value ) )
1388 Error( err_overflow );
1391 void Avg(
int sindex,
int amount_of_args, ValueType & result)
1393 if( amount_of_args == 0 )
1394 Error( err_improper_amount_of_arguments );
1396 result = stack[sindex].value;
1398 for(
int i=1 ; i<amount_of_args ; ++i )
1399 if( result.Add( stack[ sindex + i*2 ].value ) )
1400 Error( err_overflow );
1402 if( result.Div( amount_of_args ) )
1403 Error( err_overflow );
1407 void Frac(
int sindex,
int amount_of_args, ValueType & result)
1409 if( amount_of_args != 1 )
1410 Error( err_improper_amount_of_arguments );
1412 result = stack[sindex].value;
1413 result.RemainFraction();
1422 void Sprintf(
char * buffer,
int par)
1428 #pragma warning( disable: 4996 ) 1432 sprintf(buf,
"%d", par);
1433 for(i=0 ; buf[i] != 0 ; ++i)
1439 #pragma warning( default: 4996 ) 1451 bool GetValueOfUserDefinedFunction(
const std::string & function_name,
int amount_of_args,
int sindex)
1453 if( !puser_functions )
1456 const char * string_value;
1459 if( puser_functions->
GetValueAndParam(function_name, &string_value, ¶m) != err_ok )
1462 if( param != amount_of_args )
1463 Error( err_improper_amount_of_arguments );
1466 FunctionLocalVariables local_variables;
1468 if( amount_of_args > 0 )
1475 local_variables.insert( std::make_pair(buffer, stack[sindex].value) );
1477 for(
int i=0 ; i<amount_of_args ; ++i)
1480 Sprintf(buffer+1, i+1);
1481 local_variables.insert( std::make_pair(buffer, stack[sindex + i*2].value) );
1485 stack[sindex-1].value = RecurrenceParsingVariablesOrFunction(
false, function_name, string_value, &local_variables);
1505 void CallFunction(
const std::string & function_name,
int amount_of_args,
int sindex)
1507 if( GetValueOfUserDefinedFunction(function_name, amount_of_args, sindex) )
1510 typename FunctionsTable::iterator i = functions_table.find( function_name );
1512 if( i == functions_table.end() )
1513 Error( err_unknown_function );
1518 (this->*(i->second))(sindex, amount_of_args, stack[sindex-1].value);
1532 void InsertFunctionToTable(
const char * function_name, pfunction pf)
1537 functions_table.insert( std::make_pair(str, pf) );
1549 void InsertVariableToTable(
const char * variable_name, pfunction_var pf)
1554 variables_table.insert( std::make_pair(str, pf) );
1561 void CreateFunctionsTable()
1629 void CreateVariablesTable()
1631 InsertVariableToTable(
"pi", &ValueType::SetPi);
1632 InsertVariableToTable(
"e", &ValueType::SetE);
1639 int ToLowerCase(
int c)
1641 if( c>=
'A' && c<=
'Z' )
1642 return c -
'A' +
'a';
1658 bool ReadName(std::string & result)
1664 character = *pstring;
1669 if( ! (( character>=
'a' && character<=
'z' ) || ( character>=
'A' && character<=
'Z' )) )
1670 Error( err_unknown_character );
1675 result +=
static_cast<char>( character );
1676 character = * ++pstring;
1678 while( (character>=
'a' && character<=
'z') ||
1679 (character>=
'A' && character<=
'Z') ||
1680 (character>=
'0' && character<=
'9') ||
1684 SkipWhiteCharacters();
1690 if( *pstring ==
'(' )
1705 bool TestSign(
Item & result)
1707 SkipWhiteCharacters();
1708 result.sign =
false;
1710 if( *pstring ==
'-' || *pstring ==
'+' )
1712 if( *pstring ==
'-' )
1728 bool ReadVariableOrFunction(
Item & result)
1731 bool is_it_name_of_function = ReadName(name);
1733 if( is_it_name_of_function )
1738 result.function_name = name;
1739 result.type = Item::first_bracket;
1740 result.function =
true;
1750 return is_it_name_of_function;
1759 void ReadValue(
Item & result,
int reading_base)
1761 const char * new_stack_pointer;
1765 conv.
base = reading_base;
1770 uint carry = result.value.FromString(pstring, conv, &new_stack_pointer, &value_read);
1771 pstring = new_stack_pointer;
1774 Error( err_overflow );
1777 Error( err_unknown_character );
1784 bool ValueStarts(
int character,
int character_base)
1786 if( character == comma )
1789 if( comma2!=0 && character==comma2 )
1807 int ReadValueVariableOrFunction(
Item & result)
1809 bool it_was_sign =
false;
1813 if( TestSign(result) )
1817 SkipWhiteCharacters();
1818 character = ToLowerCase( *pstring );
1821 if( character == 0 )
1825 Error( err_unexpected_end );
1831 if( character ==
'(' )
1834 result.type = Item::first_bracket;
1835 result.function =
false;
1841 if( character ==
')' )
1848 Error( err_unexpected_final_bracket );
1850 result.type = Item::last_bracket;
1858 if( character ==
'#' )
1861 SkipWhiteCharacters();
1864 if( ValueStarts(*pstring, 16) )
1865 ReadValue( result, 16 );
1867 Error( err_unknown_character );
1870 if( character ==
'&' )
1873 SkipWhiteCharacters();
1876 if( ValueStarts(*pstring, 2) )
1877 ReadValue( result, 2 );
1879 Error( err_unknown_character );
1882 if( ValueStarts(character, base) )
1884 ReadValue( result, base );
1887 if( character>=
'a' && character<=
'z' )
1889 if( ReadVariableOrFunction(result) )
1894 Error( err_unknown_character );
1902 result.type = Item::numerical_value;
1906 result.value.ChangeSign();
1907 result.sign =
false;
1915 void InsertOperatorToTable(
const char * name,
typename MatOperator::Type type)
1917 operators_table.insert( std::make_pair(std::string(name), type) );
1924 void CreateMathematicalOperatorsTable()
1926 InsertOperatorToTable(
"||", MatOperator::lor);
1927 InsertOperatorToTable(
"&&", MatOperator::land);
1928 InsertOperatorToTable(
"!=", MatOperator::neq);
1929 InsertOperatorToTable(
"==", MatOperator::eq);
1930 InsertOperatorToTable(
">=", MatOperator::get);
1931 InsertOperatorToTable(
"<=", MatOperator::let);
1932 InsertOperatorToTable(
">", MatOperator::gt);
1933 InsertOperatorToTable(
"<", MatOperator::lt);
1934 InsertOperatorToTable(
"-", MatOperator::sub);
1935 InsertOperatorToTable(
"+", MatOperator::add);
1936 InsertOperatorToTable(
"/", MatOperator::div);
1937 InsertOperatorToTable(
"*", MatOperator::mul);
1938 InsertOperatorToTable(
"^", MatOperator::pow);
1948 bool IsSubstring(
const std::string & str1,
const std::string & str2)
1950 if( str2.length() > str1.length() )
1953 for(
typename std::string::size_type i=0 ; i<str2.length() ; ++i)
1954 if( str1[i] != str2[i] )
1964 void ReadMathematicalOperator(
Item & result)
1967 typename OperatorsTable::iterator iter_old, iter_new;
1969 iter_old = operators_table.end();
1971 for( ; true ; ++pstring )
1974 iter_new = operators_table.lower_bound(oper);
1976 if( iter_new == operators_table.end() || !IsSubstring(iter_new->first, oper) )
1978 oper.erase(oper.begin() + oper.size() - 1);
1980 if( iter_old != operators_table.end() && iter_old->first == oper )
1982 result.type = Item::mat_operator;
1983 result.moperator.SetType( iter_old->second );
1987 Error( err_unknown_operator );
1990 iter_old = iter_new;
2000 void OperatorPercentage()
2002 if( stack_index < 3 ||
2003 stack[stack_index-1].type != Item::numerical_value ||
2004 stack[stack_index-2].type != Item::mat_operator ||
2005 stack[stack_index-3].type != Item::numerical_value )
2006 Error(err_percent_from);
2009 SkipWhiteCharacters();
2012 c += stack[stack_index-1].value.Div(100);
2013 c += stack[stack_index-1].value.Mul(stack[stack_index-3].value);
2016 Error(err_overflow);
2028 int ReadOperator(
Item & result)
2030 SkipWhiteCharacters();
2032 if( *pstring ==
'%' )
2033 OperatorPercentage();
2039 if( *pstring ==
')' )
2041 result.type = Item::last_bracket;
2045 if( *pstring ==
';' || (param_sep!=0 && *pstring==param_sep) )
2047 result.type = Item::semicolon;
2051 if( (*pstring>=
'a' && *pstring<=
'z') || (*pstring>=
'A' && *pstring<=
'Z') )
2055 result.type = Item::mat_operator;
2056 result.moperator.SetType( MatOperator::shortmul );
2059 ReadMathematicalOperator(result);
2072 void MakeStandardMathematicOperation(ValueType & value1,
typename MatOperator::Type mat_operator,
2073 const ValueType & value2)
2079 switch( mat_operator )
2081 case MatOperator::land:
2082 (!value1.IsZero() && !value2.IsZero()) ? value1.SetOne() : value1.SetZero();
2085 case MatOperator::lor:
2086 (!value1.IsZero() || !value2.IsZero()) ? value1.SetOne() : value1.SetZero();
2089 case MatOperator::eq:
2090 (value1 == value2) ? value1.SetOne() : value1.SetZero();
2093 case MatOperator::neq:
2094 (value1 != value2) ? value1.SetOne() : value1.SetZero();
2097 case MatOperator::lt:
2098 (value1 < value2) ? value1.SetOne() : value1.SetZero();
2101 case MatOperator::gt:
2102 (value1 > value2) ? value1.SetOne() : value1.SetZero();
2105 case MatOperator::let:
2106 (value1 <= value2) ? value1.SetOne() : value1.SetZero();
2109 case MatOperator::get:
2110 (value1 >= value2) ? value1.SetOne() : value1.SetZero();
2113 case MatOperator::sub:
2114 if( value1.Sub(value2) ) Error( err_overflow );
2117 case MatOperator::add:
2118 if( value1.Add(value2) ) Error( err_overflow );
2121 case MatOperator::mul:
2122 case MatOperator::shortmul:
2123 if( value1.Mul(value2) ) Error( err_overflow );
2126 case MatOperator::div:
2127 if( value2.IsZero() ) Error( err_division_by_zero );
2128 if( value1.Div(value2) ) Error( err_overflow );
2131 case MatOperator::pow:
2132 res = value1.Pow( value2 );
2134 if( res == 1 ) Error( err_overflow );
2136 if( res == 2 ) Error( err_improper_argument );
2145 Error( err_internal_error );
2164 void TryRollingUpStackWithOperatorPriority()
2166 while( stack_index>=4 &&
2167 stack[stack_index-4].type == Item::numerical_value &&
2168 stack[stack_index-3].type == Item::mat_operator &&
2169 stack[stack_index-2].type == Item::numerical_value &&
2170 stack[stack_index-1].type == Item::mat_operator &&
2174 stack[stack_index-3].moperator.GetPriority() > stack[stack_index-1].moperator.GetPriority()
2178 stack[stack_index-3].moperator.GetPriority() == stack[stack_index-1].moperator.GetPriority() &&
2179 stack[stack_index-3].moperator.GetAssoc() == MatOperator::non_right
2184 MakeStandardMathematicOperation(stack[stack_index-4].value,
2185 stack[stack_index-3].moperator.GetType(),
2186 stack[stack_index-2].value);
2192 stack[stack_index-3] = stack[stack_index-1];
2205 void TryRollingUpStack()
2207 while( stack_index >= 3 &&
2208 stack[stack_index-3].type == Item::numerical_value &&
2209 stack[stack_index-2].type == Item::mat_operator &&
2210 stack[stack_index-1].type == Item::numerical_value )
2212 MakeStandardMathematicOperation( stack[stack_index-3].value,
2213 stack[stack_index-2].moperator.GetType(),
2214 stack[stack_index-1].value );
2225 int ReadValueVariableOrFunctionAndPushItIntoStack(
Item & temp)
2227 int code = ReadValueVariableOrFunction( temp );
2231 if( stack_index < stack.size() )
2232 stack[stack_index] = temp;
2234 stack.push_back( temp );
2258 void HowManyParameters(
int & size,
int & index)
2261 index = stack_index;
2265 Error( err_unexpected_final_bracket );
2268 if( stack[index-1].type == Item::first_bracket )
2272 for( --index ; index>=1 ; index-=2 )
2274 if( stack[index].type != Item::numerical_value )
2280 Error( err_internal_error );
2285 if( stack[index-1].type != Item::semicolon )
2289 if( index<1 || stack[index-1].type != Item::first_bracket )
2294 Error( err_unexpected_final_bracket );
2305 void RollingUpFinalBracket()
2307 int amount_of_parameters;
2311 if( stack_index<1 ||
2312 (stack[stack_index-1].type != Item::numerical_value &&
2313 stack[stack_index-1].type != Item::first_bracket)
2315 Error( err_unexpected_final_bracket );
2318 TryRollingUpStack();
2319 HowManyParameters(amount_of_parameters, index);
2325 if( amount_of_parameters==0 && !stack[index-1].
function )
2326 Error( err_unexpected_final_bracket );
2329 bool was_sign = stack[index-1].sign;
2332 if( stack[index-1].
function )
2336 CallFunction(stack[index-1].function_name, amount_of_parameters, index);
2343 if( amount_of_parameters != 1 )
2344 Error( err_unexpected_semicolon_operator );
2350 stack[index-1] = stack[index];
2358 stack[index-1].sign =
false;
2361 stack[index-1].value.ChangeSign();
2363 stack[index-1].type = Item::numerical_value;
2369 stack_index = index;
2377 void PushOperatorIntoStack(
Item & temp)
2379 if( stack_index < stack.size() )
2380 stack[stack_index] = temp;
2382 stack.push_back( temp );
2393 int ReadOperatorAndCheckFinalBracket(
Item & temp)
2397 if( ReadOperator(temp) == 1 )
2405 if( temp.type == Item::last_bracket )
2406 RollingUpFinalBracket();
2409 while( temp.type == Item::last_bracket );
2418 void CheckIntegrityOfStack()
2420 for(
unsigned int i=0 ; i<stack_index; ++i)
2422 if( stack[i].type != Item::numerical_value &&
2423 stack[i].type != Item::semicolon)
2430 Error( err_stack_not_clear );
2448 if( pstop_calculating && pstop_calculating->WasStopSignal() )
2449 Error( err_interrupt );
2451 result_code = ReadValueVariableOrFunctionAndPushItIntoStack( item );
2453 if( result_code == 0 )
2455 if( item.type == Item::first_bracket )
2458 result_code = ReadOperatorAndCheckFinalBracket( item );
2462 if( result_code==1 || item.type==Item::semicolon )
2468 if( stack_index == 0 )
2469 Error( err_nothing_has_read );
2471 TryRollingUpStack();
2473 if( result_code == 1 )
2475 CheckIntegrityOfStack();
2482 PushOperatorIntoStack( item );
2483 TryRollingUpStackWithOperatorPriority();
2497 void NormalizeStack()
2499 if( error!=err_ok || stack_index==0 )
2511 stack.resize( stack_index );
2513 for(
uint i=stack_index-1 ; i!=
uint(-1) ; --i)
2515 if( stack[i].type != Item::numerical_value )
2516 stack.erase( stack.begin() + i );
2529 pstop_calculating = 0;
2530 puser_variables = 0;
2531 puser_functions = 0;
2532 pfunction_local_variables = 0;
2541 CreateFunctionsTable();
2542 CreateVariablesTable();
2543 CreateMathematicalOperatorsTable();
2552 pstop_calculating = p.pstop_calculating;
2553 puser_variables = p.puser_variables;
2554 puser_functions = p.puser_functions;
2555 pfunction_local_variables = 0;
2557 deg_rad_grad = p.deg_rad_grad;
2562 param_sep = p.param_sep;
2568 functions_table = p.functions_table;
2569 variables_table = p.variables_table;
2570 operators_table = p.operators_table;
2572 visited_variables = p.visited_variables;
2573 visited_functions = p.visited_functions;
2607 if( angle >= 0 && angle <= 2 )
2608 deg_rad_grad = angle;
2617 pstop_calculating = ps;
2629 puser_variables = pv;
2641 puser_functions = pf;
2689 stack.resize( default_stack_size );
2712 return Parse(str.c_str());
2716 #ifndef TTMATH_DONT_USE_WCHAR 2725 return Parse(wide_to_ansi.c_str());
2736 return Parse(str.c_str());
ValueType Sgn(ValueType x)
ValueType ACot(const ValueType &x)
ValueType ACoth(const ValueType &x, ErrorCode *err=0)
Int implements a big integer value with a sign.
ValueType Tan(const ValueType &x, ErrorCode *err=0)
ValueType Log(const ValueType &x, const ValueType &base, ErrorCode *err=0)
ValueType Sin(ValueType x, ErrorCode *err=0)
static uint CharToDigit(uint c)
ErrorCode Parse(const std::wstring &str)
void SetComma(int c, int c2=0)
std::vector< Item > stack
ValueType Exp(const ValueType &x, ErrorCode *err=0)
ErrorCode Parse(const char *str)
ValueType ACosh(const ValueType &x, ErrorCode *err=0)
ValueType RadToDeg(const ValueType &x, ErrorCode *err=0)
ValueType ASin(ValueType x, ErrorCode *err=0)
ValueType ASinh(const ValueType &x, ErrorCode *err=0)
ValueType SkipFraction(const ValueType &x)
ValueType Cos(ValueType x, ErrorCode *err=0)
void SetVariables(const Objects *pv)
ValueType ATanh(const ValueType &x, ErrorCode *err=0)
ErrorCode Parse(const wchar_t *str)
ValueType Ln(const ValueType &x, ErrorCode *err=0)
Parser< ValueType > & operator=(const Parser< ValueType > &p)
ErrorCode GetValueAndParam(const std::string &name, std::string &value, int *param) const
ValueType RadToGrad(const ValueType &x, ErrorCode *err=0)
ValueType Abs(const ValueType &x)
void SetDegRadGrad(int angle)
ValueType GradToDeg(const ValueType &x, ErrorCode *err=0)
static void AssignString(std::string &result, const char *str)
a namespace for the TTMath library
ErrorCode Parse(const std::string &str)
ValueType Root(ValueType x, const ValueType &index, ErrorCode *err=0)
ValueType DegToRad(const ValueType &x, ErrorCode *err=0)
ValueType GradToRad(const ValueType &x, ErrorCode *err=0)
void SetFunctions(const Objects *pf)
ValueType Mod(ValueType a, const ValueType &b, ErrorCode *err=0)
void SetStopObject(const volatile StopCalculating *ps)
ValueType ATan(ValueType x)
Parser(const Parser< ValueType > &p)
ValueType Coth(const ValueType &x, ErrorCode *err=0)
ValueType Factorial(const ValueType &x, CGamma< ValueType > &cgamma, ErrorCode *err=0, const volatile StopCalculating *stop=0)
ValueType Floor(const ValueType &x, ErrorCode *err=0)
ValueType Tanh(const ValueType &x, ErrorCode *err=0)
ValueType Sinh(const ValueType &x, ErrorCode *err=0)
ValueType Cot(const ValueType &x, ErrorCode *err=0)
ValueType DegToGrad(const ValueType &x, ErrorCode *err=0)
ValueType Gamma(const ValueType &n, CGamma< ValueType > &cgamma, ErrorCode *err=0, const volatile StopCalculating *stop=0)
ValueType Cosh(const ValueType &x, ErrorCode *err=0)
ErrorCode GetValue(const std::string &name, std::string &value) const
ValueType Round(const ValueType &x, ErrorCode *err=0)
ValueType DegToDeg(const ValueType &d, const ValueType &m, const ValueType &s, ErrorCode *err=0)
ValueType GetValueOfVariable(const std::string &variable_name)
bool GetValueOfFunctionLocalVariable(const std::string &variable_name, ValueType &result)
ValueType Ceil(const ValueType &x, ErrorCode *err=0)
ValueType Sqrt(ValueType x, ErrorCode *err=0)
ValueType ACos(const ValueType &x, ErrorCode *err=0)
bool GetValueOfUserDefinedVariable(const std::string &variable_name, ValueType &result)