Team Fortress 2 Source Code as on 22/4/2020
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.

106 lines
3.0 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // The expression operator class - scalar math calculator
  4. // for a good list of operators and simple functions, see:
  5. // \\fileserver\user\MarcS\boxweb\aliveDistLite\v4.2.0\doc\alive\functions.txt
  6. // (although we'll want to implement elerp as the standard 3x^2 - 2x^3 with rescale)
  7. //
  8. //=============================================================================
  9. #ifndef DMEEXPRESSIONOPERATOR_H
  10. #define DMEEXPRESSIONOPERATOR_H
  11. #ifdef _WIN32
  12. #pragma once
  13. #endif
  14. #include "movieobjects/dmeoperator.h"
  15. #include "tier1/utlstack.h" // for calculator
  16. #include "tier1/utlvector.h" // for calculator
  17. //-----------------------------------------------------------------------------
  18. // Calculator Parsing class
  19. // precedence order:
  20. // unary operators: + - ! func var
  21. // * / %
  22. // + -
  23. // < > <= >=
  24. // == !=
  25. // &&
  26. // ||
  27. // ?:
  28. //-----------------------------------------------------------------------------
  29. class CExpressionCalculator
  30. {
  31. public:
  32. CExpressionCalculator( const char *expr = NULL ) : m_expr( expr ) {}
  33. void SetExpression( const char *expr )
  34. {
  35. m_expr = expr;
  36. }
  37. void SetVariable( const char *var, float value );
  38. void SetVariable( int nVariableIndex, float value );
  39. int FindVariableIndex( const char *var );
  40. bool Evaluate( float &value );
  41. // Builds a list of variable names from the expression
  42. bool BuildVariableListFromExpression( );
  43. // Iterate over variables
  44. int VariableCount();
  45. const char *VariableName( int nIndex );
  46. private:
  47. bool ParseExpr ( const char *&expr );
  48. bool ParseConditional( const char *&expr );
  49. bool ParseOr ( const char *&expr );
  50. bool ParseAnd ( const char *&expr );
  51. bool ParseEquality ( const char *&expr );
  52. bool ParseLessGreater( const char *&expr );
  53. bool ParseAddSub ( const char *&expr );
  54. bool ParseDivMul ( const char *&expr );
  55. bool ParseUnary ( const char *&expr );
  56. bool ParsePrimary ( const char *&expr );
  57. bool Parse1ArgFunc ( const char *&expr );
  58. bool Parse2ArgFunc ( const char *&expr );
  59. bool Parse3ArgFunc ( const char *&expr );
  60. // bool Parse4ArgFunc ( const char *&expr );
  61. bool Parse5ArgFunc ( const char *&expr );
  62. CUtlString m_expr;
  63. CUtlVector< CUtlString > m_varNames;
  64. CUtlVector<float> m_varValues;
  65. CUtlStack<float> m_stack;
  66. bool m_bIsBuildingArgumentList;
  67. };
  68. //-----------------------------------------------------------------------------
  69. // An operator which computes the value of expressions
  70. //-----------------------------------------------------------------------------
  71. class CDmeExpressionOperator : public CDmeOperator
  72. {
  73. DEFINE_ELEMENT( CDmeExpressionOperator, CDmeOperator );
  74. public:
  75. virtual void Operate();
  76. virtual void GetInputAttributes ( CUtlVector< CDmAttribute * > &attrs );
  77. virtual void GetOutputAttributes( CUtlVector< CDmAttribute * > &attrs );
  78. void SetSpewResult( bool state );
  79. protected:
  80. bool Parse( const char *expr );
  81. bool IsInputAttribute( CDmAttribute *pAttribute );
  82. CDmaVar< float > m_result;
  83. CDmaString m_expr;
  84. CDmaVar< bool > m_bSpewResult;
  85. };
  86. #endif // DMEEXPRESSIONOPERATOR_H