Code::Blocks
SVN r11506
plugins
codecompletion
parser
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
12
class
ExpressionNode
13
{
14
public
:
15
enum
ExpressionNodeType
16
{
17
Unknown
,
// Unknown
18
Plus
,
// +
19
Subtract
,
// -
20
Multiply
,
// *
21
Divide
,
// /
22
LParenthesis
,
// (
23
RParenthesis
,
// )
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
42
ExpressionNode
();
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
59
static
ExpressionNodeType
ParseNodeType
(
wxString
token);
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
:
68
wxString
m_Token
;
69
ExpressionNodeType
m_Type
;
70
bool
m_UnaryOperator
;
71
long
m_Priority
;
72
};
73
74
class
Expression
75
{
76
public
:
77
// constructor
78
Expression
() :
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
Expression
Definition:
expression.h:74
Expression::PostfixVector
std::vector< ExpressionNode > PostfixVector
Definition:
expression.h:96
ExpressionNode::LParenthesis
Definition:
expression.h:22
ExpressionNode::GTOrEqual
Definition:
expression.h:35
Expression::m_InfixExpression
InfixVector m_InfixExpression
Definition:
expression.h:100
ExpressionNode::ExpressionNodeType
ExpressionNodeType
Definition:
expression.h:15
ExpressionNode::IsUnaryNode
static bool IsUnaryNode(ExpressionNodeType type)
Definition:
expression.cpp:163
ExpressionNode::Plus
Definition:
expression.h:18
ExpressionNode::m_Priority
long m_Priority
Definition:
expression.h:71
ExpressionNode::RShift
Definition:
expression.h:38
string.h
ExpressionNode::BitwiseAnd
Definition:
expression.h:26
ExpressionNode::SetUnaryOperator
void SetUnaryOperator(bool unary=true)
Definition:
expression.h:48
Expression::Expression
Expression()
Definition:
expression.h:78
ExpressionNode::Equal
Definition:
expression.h:31
ExpressionNode
Definition:
expression.h:12
ExpressionNode::GetToken
wxString GetToken() const
Definition:
expression.h:51
ExpressionNode::LTOrEqual
Definition:
expression.h:36
ExpressionNode::m_Type
ExpressionNodeType m_Type
Definition:
expression.h:69
ExpressionNode::Not
Definition:
expression.h:30
ExpressionNode::Subtract
Definition:
expression.h:19
ExpressionNode::GT
Definition:
expression.h:33
ExpressionNode::Numeric
Definition:
expression.h:39
ExpressionNode::IsBinaryOperator
static bool IsBinaryOperator(wxString first, wxString second)
Definition:
expression.cpp:197
ExpressionNode::Or
Definition:
expression.h:29
ExpressionNode::LT
Definition:
expression.h:34
ExpressionNode::Mod
Definition:
expression.h:24
ExpressionNode::m_Token
wxString m_Token
Definition:
expression.h:68
ExpressionNode::ExpressionNode
ExpressionNode()
Definition:
expression.cpp:88
ExpressionNode::RParenthesis
Definition:
expression.h:23
wxString
ExpressionNode::ParseNodeType
static ExpressionNodeType ParseNodeType(wxString token)
Definition:
expression.cpp:101
ExpressionNode::Initialize
void Initialize(wxString token)
Definition:
expression.cpp:93
ExpressionNode::Multiply
Definition:
expression.h:20
Expression::GetResult
long GetResult() const
Definition:
expression.h:87
ExpressionNode::Power
Definition:
expression.h:25
ExpressionNode::GetType
ExpressionNodeType GetType() const
Definition:
expression.h:45
ExpressionNode::GetTokenValue
long GetTokenValue() const
Definition:
expression.h:52
ExpressionNode::Divide
Definition:
expression.h:21
ExpressionNode::BitwiseOr
Definition:
expression.h:27
Expression::m_Status
bool m_Status
Definition:
expression.h:101
ExpressionNode::GetPriority
long GetPriority() const
Definition:
expression.h:50
wxString::ToLong
bool ToLong(long *val, int base=10) const
ExpressionNode::Unequal
Definition:
expression.h:32
Expression::InfixVector
std::vector< wxString > InfixVector
Definition:
expression.h:97
ExpressionNode::IsUnaryOperator
bool IsUnaryOperator() const
Definition:
expression.h:47
Expression::m_PostfixExpression
PostfixVector m_PostfixExpression
Definition:
expression.h:99
ExpressionNode::m_UnaryOperator
bool m_UnaryOperator
Definition:
expression.h:70
Expression::GetStatus
bool GetStatus() const
Definition:
expression.h:88
Expression::m_Result
long m_Result
Definition:
expression.h:102
ExpressionNode::LShift
Definition:
expression.h:37
ExpressionNode::Unknown
Definition:
expression.h:17
ExpressionNode::And
Definition:
expression.h:28
ExpressionNode::GetNodeTypePriority
static long GetNodeTypePriority(ExpressionNodeType type)
Definition:
expression.cpp:132
Generated by
1.8.13