9c0c0b689a4dc83229dc223dbbe55d5d502cad15
[scilab.git] / scilab / modules / abstractSyntaxTree / includes / execvisitor.hxx
1 /*
2 *  Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 *  Copyright (C) 2008-2008 - DIGITEO - Bruno JOFRET
4 *
5 *  This file must be used under the terms of the CeCILL.
6 *  This source file is licensed as described in the file COPYING, which
7 *  you should have received as part of this distribution.  The terms
8 *  are also available at
9 *  http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
10 *
11 */
12
13 #ifndef AST_EXECVISITOR_HXX
14 # define AST_EXECVISITOR_HXX
15
16 #include <time.h>
17 #include <string>
18 #include <iostream>
19 #include <sstream>
20
21 #include "visitor.hxx"
22 #include "allexp.hxx"
23 #include "allvar.hxx"
24 #include "alldec.hxx"
25 #include "alltypes.hxx"
26 #include "context.hxx"
27
28 using namespace types;
29
30 int GetIndexList(std::list<ast::Exp *>const& _plstArg, int** _piIndexSeq, int** _piMaxDim, InternalType *_pRefVar, int *_iDimSize);
31 void ExpandList(int ** _piList, int *_piListSize, int _iListSizeSize, int *_piResultList);
32 int GetVarMaxDim(types::InternalType *_pIT, int _iCurrentDim, int _iMaxDim);
33
34 namespace ast
35 {
36         class EXTERN_AST ExecVisitor : public GenVisitor<const_kind>
37         {
38         public:
39                 ExecVisitor()
40                 {
41                         _excepted_result = -1;
42                         _resultVect.push_back(NULL);
43                         _result = NULL;
44                         m_bSingleResult = true;
45                 }
46
47                 ~ExecVisitor()
48                 {
49                         result_clear();
50                 }
51
52                 void result_clear()
53                 {
54                         if(is_single_result())
55                         {
56                                 if(_result != NULL && _result->isDeletable() == true)
57                                 {
58 //                                      std::cout << "before single delete : " << _result << std::endl;
59                                         delete _result;
60 //                                      std::cout << "after single delete" << std::endl;
61                                 }
62                         }
63                         else
64                         {
65                                 for(int i = 0 ; i < _resultVect.size() ; i++)
66                                 {
67                                         if(_resultVect[i] != NULL && _resultVect[i]->isDeletable() == true)
68                                         {
69                                                 delete _resultVect[i];
70                                         }
71                                 }
72                         }
73
74                 }
75
76                 /** \name Visit Matrix Expressions nodes.
77                 ** \{ */
78         public :
79                 virtual void visit (const MatrixExp &e);
80                 virtual void visit (const MatrixLineExp &e);
81                 /** \} */
82
83                 /** \name Visit Cell Expressions nodes.
84                 ** \{ */
85         public :
86                 virtual void visit (const CellExp &e);
87                 /** \} */
88
89                 /** \name Visit Constant Expressions nodes.
90                 ** \{ */
91         public :
92                 virtual void visit (const StringExp &e);
93                 virtual void visit (const CommentExp &e);
94                 virtual void visit (const IntExp  &e);
95                 virtual void visit (const FloatExp  &e);
96                 virtual void visit (const DoubleExp  &e);
97                 virtual void visit (const BoolExp  &e);
98                 virtual void visit (const NilExp &e);
99                 /** \} */
100
101                 /** \name Visit Variable related nodes.
102                 ** \{ */
103                 virtual void visit (const SimpleVar &e);
104                 virtual void visit (const ColonVar &e);
105                 virtual void visit (const DollarVar &e);
106                 virtual void visit (const ArrayListVar &e);
107                 /** \} */
108
109                 /** \name Visit Control Expressions or Instructions nodes.
110                 ** \{ */
111                 virtual void visit (const FieldExp &e);
112                 virtual void visit (const OpExp &e);
113                 virtual void visit (const LogicalOpExp &e);
114                 virtual void visit (const AssignExp  &e);
115                 virtual void visit (const CallExp &e);
116                 virtual void visit (const IfExp  &e);
117                 virtual void visit (const TryCatchExp  &e);
118                 virtual void visit (const WhileExp  &e);
119                 virtual void visit (const ForExp  &e);
120                 virtual void visit (const BreakExp &e);
121                 virtual void visit (const ReturnExp &e);
122                 virtual void visit (const SelectExp &e);
123                 virtual void visit (const CaseExp &e);
124                 virtual void visit (const SeqExp  &e);
125                 virtual void visit (const ArrayListExp  &e);
126                 virtual void visit (const AssignListExp  &e);
127                 /** \} */
128
129                 /** \name Visit Single Operation nodes.
130                 ** \{ */
131                 virtual void visit (const NotExp &e);
132                 virtual void visit (const TransposeExp &e);
133                 /** \} */
134
135                 /** \name Visit Declaration nodes.
136                 ** \{ */
137                 /** \brief Visit Var declarations. */
138                 virtual void visit (const VarDec  &e);
139                 virtual void visit (const FunctionDec  &e);
140                 /** \} */
141
142                 /** \name Visit Type dedicated Expressions related node.
143                 ** \{ */
144         public:
145                 virtual void visit(const ListExp &e);
146                 /** \} */
147
148
149
150                 int expected_size_get(void)
151                 {
152                         return _excepted_result;
153                 }
154
155                 int result_size_get(void)
156                 {
157                         if(is_single_result())
158                         {
159                                 if(_result == NULL)
160                                 {
161                                         return 0;
162                                 }
163                                 else
164                                 {
165                                         return 1;
166                                 }
167                         }
168                         else
169                         {
170                                 return (int)_resultVect.size();
171                         }
172                 }
173
174                 void expected_size_set(int _iSize)
175                 {
176                         _excepted_result = _iSize;
177                 }
178
179                 InternalType* result_get(void)
180                 {
181                         if(is_single_result())
182                         {
183                                 return _result;
184                         }
185                         else
186                         {
187                                 return _resultVect[0];
188                         }
189                 }
190
191                 types::InternalType* result_get(int _iPos)
192                 {
193                         if(_iPos >= _resultVect.size())
194                         {
195                                 return NULL;
196                         }
197                         return _resultVect[_iPos];
198                 }
199
200                 vector<types::InternalType*>* result_list_get()
201                 {
202                         return &_resultVect;
203                 }
204
205                 void result_set(int _iPos, const types::InternalType *gtVal)
206                 {
207                         m_bSingleResult = false;
208                         if(_iPos < _resultVect.size())
209                         {
210                                 if(_resultVect[_iPos] != NULL && _resultVect[_iPos]->isDeletable())
211                                 {
212                                         delete _resultVect[_iPos];
213                                 }
214                         }
215
216                         if(_iPos >= _resultVect.size())
217                         {
218                                 _resultVect.resize(_iPos + 1, NULL);
219                         }
220
221                         _resultVect[_iPos] = (InternalType *)gtVal;
222                 }
223
224                 void result_set(const InternalType *gtVal)
225                 {
226                         m_bSingleResult = true;
227                         _result = const_cast<InternalType *>(gtVal);
228                 }
229
230                 bool is_single_result()
231                 {
232                         return m_bSingleResult;
233                 }
234
235
236                 /*
237                 int result_size_get(void);
238                 int expected_size_get(void);
239                 void expected_size_set(int _iSize);
240                 types::InternalType*            result_get(void);
241                 types::InternalType*            result_get(int _iPos);
242                 vector<types::InternalType*>* result_list_get();
243                 void    result_set(const types::InternalType *gtVal);
244                 void    result_set(int _iPos, const types::InternalType *gtVal);
245                 bool    is_single_result();
246                 */
247
248                 /*-------------.
249                 | Attributes.  |
250                 `-------------*/
251         protected:
252                 vector<types::InternalType*>    _resultVect;
253                 types::InternalType*    _result;
254                 bool m_bSingleResult;
255                 int _excepted_result;
256         private :
257                 ExecVisitor(ExecVisitor const& e){}
258
259         public : 
260                 static ExecVisitor* m_defaultVisitor;
261                 static ExecVisitor& getDefaultVisitor(){return *m_defaultVisitor;}
262                 static void setDefaultVisitor(ExecVisitor& visitor){m_defaultVisitor = &visitor;}
263         };
264 }
265 #endif // !AST_EXECVISITOR_HXX