Code::Blocks  SVN r11506
expression.h
Go to the documentation of this file.
1 /*
2  * This file is part of the Code::Blocks IDE and licensed under the GNU General Public License, version 3
3  * http://www.gnu.org/licenses/gpl-3.0.html
4  */
5 
6 #ifndef EXPRESSION_H
7 #define EXPRESSION_H
8 
9 #include <wx/string.h>
10 #include <vector>
11 
13 {
14 public:
16  {
17  Unknown, // Unknown
18  Plus, // +
19  Subtract, // -
20  Multiply, // *
21  Divide, // /
24  Mod, // %
25  Power, // ^
26  BitwiseAnd, // &
27  BitwiseOr, // |
28  And, // &&
29  Or, // ||
30  Not, // !
31  Equal, // ==
32  Unequal, // !=
33  GT, // >
34  LT, // <
35  GTOrEqual, // >=
36  LTOrEqual, // <=
37  LShift, // <<
38  RShift, // >>
39  Numeric // Numeric
40  };
41 
43  void Initialize(wxString token);
44 
45  ExpressionNodeType GetType() const { return m_Type; }
46 
47  bool IsUnaryOperator() const { return m_UnaryOperator; }
48  void SetUnaryOperator(bool unary = true) { m_UnaryOperator = unary; }
49 
50  long GetPriority() const { return m_Priority; }
51  wxString GetToken() const { return m_Token; }
52  long GetTokenValue() const
53  {
54  long r;
55  m_Token.ToLong(&r);
56  return r;
57  }
58 
60  static long GetNodeTypePriority(ExpressionNodeType type);
61  static bool IsUnaryNode(ExpressionNodeType type);
62  // check to see the two char token can be merged to create a single operator
63  // e.g. if the first is '&' and the second is '&', then the function will return true since
64  // "&&" is a valid operator.
65  static bool IsBinaryOperator(wxString first, wxString second);
66 
67 private:
71  long m_Priority;
72 };
73 
75 {
76 public:
77  // constructor
79  m_Status(false),
80  m_Result(0)
81  {
82  };
83 
84  void AddToInfixExpression(wxString token);
85  void ConvertInfixToPostfix();
86  bool CalcPostfix();
87  long GetResult() const { return m_Result; }
88  bool GetStatus() const { return m_Status; }
89  void Clear();
90 
91 private:
92  long Calculate(ExpressionNode::ExpressionNodeType type, long first, long second);
93  long CalculateUnary(ExpressionNode::ExpressionNodeType type, long value);
94 
95 private:
96  typedef std::vector<ExpressionNode> PostfixVector;
97  typedef std::vector<wxString> InfixVector;
98 
99  PostfixVector m_PostfixExpression;
100  InfixVector m_InfixExpression;
101  bool m_Status;
102  long m_Result;
103 };
104 
105 #endif // EXPRESSION_H
std::vector< ExpressionNode > PostfixVector
Definition: expression.h:96
InfixVector m_InfixExpression
Definition: expression.h:100
static bool IsUnaryNode(ExpressionNodeType type)
Definition: expression.cpp:163
void SetUnaryOperator(bool unary=true)
Definition: expression.h:48
wxString GetToken() const
Definition: expression.h:51
ExpressionNodeType m_Type
Definition: expression.h:69
static bool IsBinaryOperator(wxString first, wxString second)
Definition: expression.cpp:197
wxString m_Token
Definition: expression.h:68
static ExpressionNodeType ParseNodeType(wxString token)
Definition: expression.cpp:101
void Initialize(wxString token)
Definition: expression.cpp:93
long GetResult() const
Definition: expression.h:87
ExpressionNodeType GetType() const
Definition: expression.h:45
long GetTokenValue() const
Definition: expression.h:52
bool m_Status
Definition: expression.h:101
long GetPriority() const
Definition: expression.h:50
bool ToLong(long *val, int base=10) const
std::vector< wxString > InfixVector
Definition: expression.h:97
bool IsUnaryOperator() const
Definition: expression.h:47
PostfixVector m_PostfixExpression
Definition: expression.h:99
bool m_UnaryOperator
Definition: expression.h:70
bool GetStatus() const
Definition: expression.h:88
long m_Result
Definition: expression.h:102
static long GetNodeTypePriority(ExpressionNodeType type)
Definition: expression.cpp:132