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.

74 lines
2.3 KiB

  1. //===== Copyright � 1996-2006, Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose: ExprSimplifier builds a binary tree from an infix expression (in the
  4. // form of a character array).
  5. //
  6. //===========================================================================//
  7. #ifndef EXPREVALUATOR_H
  8. #define EXPREVALUATOR_H
  9. #if defined( _WIN32 )
  10. #pragma once
  11. #endif
  12. static const char OR_OP = '|';
  13. static const char AND_OP = '&';
  14. static const char NOT_OP = '!';
  15. #define MAX_IDENTIFIER_LEN 128
  16. enum Kind {CONDITIONAL, NOT, LITERAL};
  17. struct ExprNode
  18. {
  19. ExprNode *left; // left sub-expression
  20. ExprNode *right; // right sub-expression
  21. Kind kind; // kind of node this is
  22. union
  23. {
  24. char cond; // the conditional
  25. bool value; // the value
  26. } data;
  27. };
  28. typedef ExprNode *ExprTree;
  29. // callback to evaluate a $<symbol> during evaluation, return true or false
  30. typedef bool (*GetSymbolProc_t)( const char *pKey );
  31. typedef void (*SyntaxErrorProc_t)( const char *pReason );
  32. class CExpressionEvaluator
  33. {
  34. public:
  35. CExpressionEvaluator();
  36. ~CExpressionEvaluator();
  37. bool Evaluate( bool &result, const char *pInfixExpression, GetSymbolProc_t pGetSymbolProc = 0, SyntaxErrorProc_t pSyntaxErrorProc = 0 );
  38. private:
  39. CExpressionEvaluator( CExpressionEvaluator& ); // prevent copy constructor being used
  40. char GetNextToken( void );
  41. void FreeNode( ExprNode *pNode );
  42. ExprNode *AllocateNode( void );
  43. void FreeTree( ExprTree &node );
  44. bool IsConditional( bool &bCondition, const char token );
  45. bool IsNotOp( const char token );
  46. bool IsIdentifierOrConstant( const char token );
  47. bool MakeExprNode( ExprTree &tree, char token, Kind kind, ExprTree left, ExprTree right );
  48. bool MakeFactor( ExprTree &tree );
  49. bool MakeTerm( ExprTree &tree );
  50. bool MakeExpression( ExprTree &tree );
  51. bool BuildExpression( void );
  52. bool SimplifyNode( ExprTree &node );
  53. ExprTree m_ExprTree; // Tree representation of the expression
  54. char m_CurToken; // Current token read from the input expression
  55. const char *m_pExpression; // Array of the expression characters
  56. int m_CurPosition; // Current position in the input expression
  57. char m_Identifier[MAX_IDENTIFIER_LEN]; // Stores the identifier string
  58. GetSymbolProc_t m_pGetSymbolProc;
  59. SyntaxErrorProc_t m_pSyntaxErrorProc;
  60. bool m_bSetup;
  61. };
  62. #endif