1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2004 the VideoLAN team
7 * Authors: Cyril Deguet <asmax@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 #include "expr_evaluator.hpp"
27 void ExprEvaluator::parse( const string &rExpr )
31 const char *pString = rExpr.c_str();
32 list<string> opStack; // operator stack
35 // Tokenize the expression
36 int begin = 0, end = 0;
37 while( pString[begin] )
39 // Find the next significant char in the string
40 while( pString[begin] == ' ' )
45 if( pString[begin] == '(' )
47 // '(' found: push it on the stack and continue
48 opStack.push_back( "(" );
51 else if( pString[begin] == ')' )
53 // ')' found: pop the stack until a '(' is found
54 while( !opStack.empty() )
57 string lastOp = opStack.back();
63 // Push the operator on the RPN stack
64 m_stack.push_back( lastOp );
72 while( pString[end] && pString[end] != ' ' && pString[end] != ')' )
77 token = rExpr.substr( begin, end - begin );
80 // TODO compare to a set of operators
81 if( token == "not" || token == "or" || token == "and" )
83 // Pop the operator stack while the operator has a higher
84 // precedence than the top of the stack
85 while( !opStack.empty() &&
86 hasPrecedency( token, opStack.back() ) )
89 string lastOp = opStack.back();
91 m_stack.push_back( lastOp );
93 opStack.push_back( token );
97 m_stack.push_back( token );
101 // Finish popping the operator stack
102 while( !opStack.empty() )
104 string lastOp = opStack.back();
106 m_stack.push_back( lastOp );
111 string ExprEvaluator::getToken()
113 if( !m_stack.empty() )
115 string token = m_stack.front();
123 bool ExprEvaluator::hasPrecedency( const string &op1, const string &op2 ) const
132 return (op2 == "or") || (op2 == "not" );
136 return (op2 == "not" );