25 #define CC_EXPRESSION_DEBUG_OUTPUT 0 27 #if defined(CC_GLOBAL_DEBUG_OUTPUT) 28 #if CC_GLOBAL_DEBUG_OUTPUT == 1 29 #undef CC_EXPRESSION_DEBUG_OUTPUT 30 #define CC_EXPRESSION_DEBUG_OUTPUT 1 31 #elif CC_GLOBAL_DEBUG_OUTPUT == 2 32 #undef CC_EXPRESSION_DEBUG_OUTPUT 33 #define CC_EXPRESSION_DEBUG_OUTPUT 2 38 #define TRACE(format, args...) \ 39 CCLogger::Get()->DebugLog(F(format, ##args)) 40 #define TRACE2(format, args...) \ 41 CCLogger::Get()->DebugLog(F(format, ##args)) 43 #if CC_EXPRESSION_DEBUG_OUTPUT == 1 44 #define TRACE(format, args...) \ 45 CCLogger::Get()->DebugLog(F(format, ##args)) 46 #define TRACE2(format, args...) 47 #elif CC_EXPRESSION_DEBUG_OUTPUT == 2 48 #define TRACE(format, args...) \ 51 if (g_EnableDebugTrace) \ 52 CCLogger::Get()->DebugLog(F(format, ##args)); \ 55 #define TRACE2(format, args...) \ 56 CCLogger::Get()->DebugLog(F(format, ##args)) 58 #define TRACE(format, args...) 59 #define TRACE2(format, args...) 95 m_UnaryOperator =
false;
97 m_Type = ParseNodeType(m_Token);
98 m_Priority = GetNodeTypePriority(m_Type);
142 case Power:
return 7;
208 wxString newOperator(first + second);
228 m_InfixExpression.clear();
229 m_PostfixExpression.clear();
237 if (!m_InfixExpression.empty())
239 wxString& lastToken = m_InfixExpression[m_InfixExpression.size() - 1];
247 m_InfixExpression.push_back(token);
252 if (!m_PostfixExpression.empty() || m_InfixExpression.empty())
258 std::stack<ExpressionNode> stackOperator;
260 for (PostfixVector::size_type i = 0; i < m_InfixExpression.size(); ++i)
268 m_PostfixExpression.push_back(expNode);
269 while (!stackOperator.empty() && stackOperator.top().IsUnaryOperator())
271 m_PostfixExpression.push_back(stackOperator.top());
278 stackOperator.push(expNode);
284 while (!stackOperator.empty())
286 node = stackOperator.top();
290 while (!stackOperator.empty() && stackOperator.top().IsUnaryOperator())
292 m_PostfixExpression.push_back(stackOperator.top());
298 m_PostfixExpression.push_back(node);
314 stackOperator.push(expNode);
316 else if (stackOperator.empty())
318 stackOperator.push(expNode);
326 m_PostfixExpression.push_back(beforeExpNode);
330 stackOperator.push(expNode);
337 while (!stackOperator.empty())
344 m_PostfixExpression.push_back(beforeExpNode);
348 #ifdef CC_PARSER_TEST 350 for (InfixVector::size_type i = 0; i < m_InfixExpression.size(); ++i)
351 infix += m_InfixExpression[i] +
_T(
" ");
352 for (PostfixVector::size_type i = 0; i < m_PostfixExpression.size(); ++i)
353 postfix += m_PostfixExpression[i].GetToken() +
_T(
" ");
354 TRACE(
_T(
"ConvertInfixToPostfix() : InfixExpression : %s"), infix.
wx_str());
355 TRACE(
_T(
"ConvertInfixToPostfix() : PostfixExpression : %s"), postfix.
wx_str());
361 std::pair<long, long> pair;
362 std::stack<long> stack;
365 for (PostfixVector::size_type i = 0; i < m_PostfixExpression.size(); ++i)
374 else if (cntNumeric == 2)
376 else if (cntNumeric == 3)
379 stack.push(pair.first);
380 TRACE(
_T(
"CalcPostfix() : stack.push(pair.first) : %ld"), pair.first);
381 pair.first = pair.second;
390 pair.first = CalculateUnary(type, pair.first);
391 else if (cntNumeric == 2)
392 pair.second = CalculateUnary(type, pair.second);
399 pair.first = Calculate(type, pair.first, pair.second);
401 else if (cntNumeric == 1)
408 pair.second = pair.first;
409 pair.first = stack.top();
410 TRACE(
_T(
"CalcPostfix() : stack.pop() : %ld"), pair.first);
412 pair.first = Calculate(type, pair.first, pair.second);
417 TRACE(
_T(
"CalcPostfix() : pair.first : %ld, pair.second : %ld"), pair.first, pair.second);
426 m_Result = pair.first;
436 return first + second;
438 return first - second;
440 return first * second;
442 if (second == 0) { m_Status =
false;
return 0; }
443 else return first / second;
445 if (second == 0) { m_Status =
false;
return 0; }
446 else return first / second;
448 return first & second;
450 return first | second;
452 return first && second;
454 return first || second;
456 return first == second;
458 return first != second;
460 return first > second;
462 return first < second;
464 return first >= second;
466 return first <= second;
468 return first << second;
470 return first >> second;
const wxString Equal(_T("=="))
const wxString Mod(_T("%"))
static bool IsUnaryNode(ExpressionNodeType type)
const wxString LParenthesis(_T("("))
const wxString Plus(_T("+"))
void SetUnaryOperator(bool unary=true)
const wxString BitwiseAnd(_T("&"))
#define TRACE(format, args...)
bool wxIsdigit(const wxUniChar &c)
const wxString GT(_T(">"))
const wxString Multiply(_T("*"))
static bool IsBinaryOperator(wxString first, wxString second)
long Calculate(ExpressionNode::ExpressionNodeType type, long first, long second)
const wxString Divide(_T("/"))
wxUSE_UNICODE_dependent wxChar
const wxString LTOrEqual(_T("<="))
const wxString RParenthesis(_T(")"))
static ExpressionNodeType ParseNodeType(wxString token)
void Initialize(wxString token)
const wxString And(_T("&&"))
const wxString Or(_T("||"))
ExpressionNodeType GetType() const
const wxString LShift(_T("<<"))
const wxString LT(_T("<"))
long GetTokenValue() const
const wxStringCharType * wx_str() const
const wxString Unequal(_T("!="))
const wxString Power(_T("^"))
void AddToInfixExpression(wxString token)
bool IsUnaryOperator() const
const wxString Subtract(_T("-"))
wxUniChar GetChar(size_t n) const
const wxString RShift(_T(">>"))
const wxString Not(_T("!"))
void ConvertInfixToPostfix()
long CalculateUnary(ExpressionNode::ExpressionNodeType type, long value)
const wxString BitwiseOr(_T("|"))
const wxString GTOrEqual(_T(">="))
static long GetNodeTypePriority(ExpressionNodeType type)