]> git.sesse.net Git - vlc/blob - modules/gui/skins2/parser/expr_evaluator.cpp
lua: fix fonction prototype (should return a boolean).
[vlc] / modules / gui / skins2 / parser / expr_evaluator.cpp
1 /*****************************************************************************
2  * expr_evaluator.cpp
3  *****************************************************************************
4  * Copyright (C) 2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Cyril Deguet     <asmax@via.ecp.fr>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 #include "expr_evaluator.hpp"
25
26
27 void ExprEvaluator::parse( const string &rExpr )
28 {
29     m_stack.clear();
30
31     const char *pString = rExpr.c_str();
32     list<string> opStack;   // operator stack
33     string token;
34
35     // Tokenize the expression
36     int begin = 0, end = 0;
37     while( pString[begin] )
38     {
39         // Find the next significant char in the string
40         while( pString[begin] == ' ' )
41         {
42             begin++;
43         }
44
45         if( pString[begin] == '(' )
46         {
47             // '(' found: push it on the stack and continue
48             opStack.push_back( "(" );
49             begin++;
50         }
51         else if( pString[begin] == ')' )
52         {
53             // ')' found: pop the stack until a '(' is found
54             while( !opStack.empty() )
55             {
56                 // Pop the stack
57                 string lastOp = opStack.back();
58                 opStack.pop_back();
59                 if( lastOp == "(" )
60                 {
61                     break;
62                 }
63                 // Push the operator on the RPN stack
64                 m_stack.push_back( lastOp );
65             }
66             begin++;
67         }
68         else
69         {
70             // Skip white spaces
71             end = begin;
72             while( pString[end] && pString[end] != ' ' && pString[end] != ')' )
73             {
74                 end++;
75             }
76             // Get the next token
77             token = rExpr.substr( begin, end - begin );
78             begin = end;
79
80             // TODO compare to a set of operators
81             if( token == "not" || token == "or" || token == "and" )
82             {
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() ) )
87                 {
88                     // Pop the stack
89                     string lastOp = opStack.back();
90                     opStack.pop_back();
91                     m_stack.push_back( lastOp );
92                 }
93                 opStack.push_back( token );
94             }
95             else
96             {
97                 m_stack.push_back( token );
98             }
99         }
100     }
101     // Finish popping the operator stack
102     while( !opStack.empty() )
103     {
104         string lastOp = opStack.back();
105         opStack.pop_back();
106         m_stack.push_back( lastOp );
107     }
108 }
109
110
111 string ExprEvaluator::getToken()
112 {
113     if( !m_stack.empty() )
114     {
115         string token = m_stack.front();
116         m_stack.pop_front();
117         return token;
118     }
119     return "";
120 }
121
122
123 bool ExprEvaluator::hasPrecedency( const string &op1, const string &op2 ) const
124 {
125     // FIXME
126     if( op1 == "(" )
127     {
128         return true;
129     }
130     if( op1 == "and" )
131     {
132         return (op2 == "or") || (op2 == "not" );
133     }
134     if( op1 == "or" )
135     {
136         return (op2 == "not" );
137     }
138     return false;
139 }
140