Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
2.3 KiB

  1. //====== Copyright c 1996-2008, Valve Corporation, All rights reserved. =======
  2. #ifndef MATHLIB_EXPRESSION_CALCULATOR_H
  3. #define MATHLIB_EXPRESSION_CALCULATOR_H
  4. #ifdef _WIN32
  5. #pragma once
  6. #endif
  7. #include "tier1/utlstring.h"
  8. #include "tier1/utlstack.h"
  9. #include "tier1/utlvector.h"
  10. //-----------------------------------------------------------------------------
  11. // Calculator Parsing class
  12. // precedence order:
  13. // unary operators: + - ! func var
  14. // * / %
  15. // + -
  16. // < > <= >=
  17. // == !=
  18. // &&
  19. // ||
  20. // ?:
  21. //-----------------------------------------------------------------------------
  22. class CExpressionCalculator
  23. {
  24. public:
  25. CExpressionCalculator( const char *expr = NULL ) : m_expr( expr ) {}
  26. CExpressionCalculator( const CExpressionCalculator& x );
  27. CExpressionCalculator& operator=( const CExpressionCalculator& x );
  28. public:
  29. void SetExpression( const char *expr )
  30. {
  31. m_expr = expr;
  32. }
  33. void SetVariable( const char *var, float value );
  34. void SetVariable( int nVariableIndex, float value );
  35. void ModifyVariable( const char *var, float value );
  36. int FindVariableIndex( const char *var );
  37. bool Evaluate( float &value );
  38. // Builds a list of variable names from the expression
  39. bool BuildVariableListFromExpression( );
  40. // Iterate over variables
  41. int VariableCount();
  42. const char *VariableName( int nIndex );
  43. private:
  44. bool ParseExpr ( const char *&expr );
  45. bool ParseConditional( const char *&expr );
  46. bool ParseOr ( const char *&expr );
  47. bool ParseAnd ( const char *&expr );
  48. bool ParseEquality ( const char *&expr );
  49. bool ParseLessGreater( const char *&expr );
  50. bool ParseAddSub ( const char *&expr );
  51. bool ParseDivMul ( const char *&expr );
  52. bool ParseUnary ( const char *&expr );
  53. bool ParsePrimary ( const char *&expr );
  54. bool Parse1ArgFunc ( const char *&expr );
  55. bool Parse2ArgFunc ( const char *&expr );
  56. bool Parse3ArgFunc ( const char *&expr );
  57. // bool Parse4ArgFunc ( const char *&expr );
  58. bool Parse5ArgFunc ( const char *&expr );
  59. CUtlString m_expr;
  60. CUtlVector< CUtlString > m_varNames;
  61. CUtlVector<float> m_varValues;
  62. CUtlStack<float> m_stack;
  63. bool m_bIsBuildingArgumentList;
  64. };
  65. // simple warppers for using cExpressionCalculator
  66. float EvaluateExpression( char const *pExprString, float flValueToReturnIfFailure );
  67. #endif // MATHLIB_EXPRESSION_CALCULATOR_H