where : value returned was inverted.
[scilab.git] / scilab / modules / ast / includes / runvisitor.hxx
1 /*
2  *  Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  *  Copyright (C) 2010-2010 - DIGITEO - Antoine ELIAS
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_RUNVISITOR_HXX
14 #define AST_RUNVISITOR_HXX
15
16 #include <time.h>
17 #include <string>
18 #include <iostream>
19 #include <sstream>
20 #include <cstdio>
21 #include <iostream>
22
23 #include "visitor_common.hxx"
24 //#include "runvisitor.hxx"
25 //#include "execvisitor.hxx"
26 //#include "timedvisitor.hxx"
27 #include "shortcutvisitor.hxx"
28 #include "printvisitor.hxx"
29 #include "mutevisitor.hxx"
30
31 // Needed by visitprivate(const OpExp &)
32 // Needed by visitprivate(const LogicalOpExp &)
33 #include "generic_operations.hxx"
34 #include "types_or_and.hxx"
35 #include "configvariable.hxx"
36 #include "overload.hxx"
37 #include "scilabexception.hxx"
38
39 #include "matrix_transpose_int.hxx"
40
41 extern "C" {
42 #include "doublecomplex.h"
43 #include "matrix_transpose.h"
44 #include "os_swprintf.h"
45 #include "more.h"
46 #include "sciprint.h"
47 #include "MALLOC.h"
48 }
49
50 #include "timer.hxx"
51 #include "localization.h"
52
53 #include "scilabWrite.hxx"
54 #include "context.hxx"
55
56 #include "all.hxx"
57 #include "types.hxx"
58 #include "alltypes.hxx"
59
60 // FIXME : remove those using
61 using namespace types;
62
63 namespace ast
64 {
65 class RunVisitor : public ConstVisitor
66 {
67 public:
68     RunVisitor()
69     {
70         _excepted_result = -1;
71         _resultVect.push_back(NULL);
72         _result = NULL;
73         m_bSingleResult = true;
74         m_pAns = new symbol::Symbol(L"ans");
75     }
76
77     ~RunVisitor()
78     {
79         result_clear();
80     }
81
82     void result_clear()
83     {
84         if (is_single_result())
85         {
86             if (_result != NULL && _result->isDeletable() == true)
87             {
88                 //                                      std::cout << "before single delete : " << _result << std::endl;
89                 delete _result;
90                 //                                      std::cout << "after single delete" << std::endl;
91             }
92             _result = NULL;
93         }
94         else
95         {
96             for (unsigned int i = 0 ; i < _resultVect.size() ; i++)
97             {
98                 if (_resultVect[i] != NULL && _resultVect[i]->isDeletable() == true)
99                 {
100                     delete _resultVect[i];
101                 }
102                 _resultVect[i] = NULL;
103             }
104         }
105         _resultVect.clear();
106         m_bSingleResult = true;
107         _result = NULL;
108     }
109
110 public:
111     int expected_getSize(void)
112     {
113         return _excepted_result;
114     }
115
116     int result_getSize(void)
117     {
118         if (is_single_result())
119         {
120             if (_result == NULL)
121             {
122                 return 0;
123             }
124             else
125             {
126                 return 1;
127             }
128         }
129         else
130         {
131             return static_cast<int>(_resultVect.size());
132         }
133     }
134
135     void expected_setSize(int _iSize)
136     {
137         _excepted_result = _iSize;
138     }
139
140     types::InternalType* result_get(void)
141     {
142         if (is_single_result())
143         {
144             return _result;
145         }
146         else
147         {
148             return _resultVect[0];
149         }
150     }
151
152     types::InternalType* result_get(int _iPos)
153     {
154         if (is_single_result() && _iPos == 0)
155         {
156             return _result;
157         }
158
159         if (_iPos >= static_cast<int>(_resultVect.size()))
160         {
161             return NULL;
162         }
163         return _resultVect[_iPos];
164     }
165
166     vector<types::InternalType*>* result_list_get()
167     {
168         if (result_getSize() == 1)
169         {
170             vector<types::InternalType*>* pList = new vector<types::InternalType*>;
171             pList->push_back(_result);
172             return pList;
173         }
174         else
175         {
176             return &_resultVect;
177         }
178     }
179
180     void result_set(int _iPos, const types::InternalType *gtVal)
181     {
182         m_bSingleResult = false;
183         if (_iPos <  static_cast<int>(_resultVect.size()))
184         {
185             if (_resultVect[_iPos] != NULL && _resultVect[_iPos]->isDeletable())
186             {
187                 delete _resultVect[_iPos];
188             }
189         }
190
191         if (_iPos >=  static_cast<int>(_resultVect.size()))
192         {
193             _resultVect.resize(_iPos + 1, NULL);
194         }
195
196         _resultVect[_iPos] = const_cast<types::InternalType *>(gtVal);
197     }
198
199     void result_set(const types::InternalType *gtVal)
200     {
201         m_bSingleResult = true;
202         _result = const_cast<types::InternalType *>(gtVal);
203     }
204
205     bool is_single_result()
206     {
207         return m_bSingleResult;
208     }
209
210     /*-------------.
211     | Attributes.  |
212     `-------------*/
213 protected:
214     vector<types::InternalType*>        _resultVect;
215     types::InternalType*        _result;
216     bool m_bSingleResult;
217     int _excepted_result;
218     symbol::Symbol* m_pAns;
219 };
220
221 template <class T>
222 class RunVisitorT : public RunVisitor
223 {
224 public :
225     RunVisitorT() : RunVisitor()
226     {
227     }
228
229     types::typed_list* GetArgumentList(std::list<ast::Exp *>const& _plstArg)
230     {
231         types::typed_list* pArgs = new types::typed_list();
232         std::list<ast::Exp *>::const_iterator it;
233         for (it = _plstArg.begin() ; it != _plstArg.end() ; it++)
234         {
235             (*it)->accept(*this);
236             if (result_getSize() > 1)
237             {
238                 for (int i = 0 ; i < result_getSize() ; i++)
239                 {
240                     pArgs->push_back(result_get(i));
241                 }
242             }
243             else
244             {
245                 pArgs->push_back(result_get());
246             }
247         }
248         //to be sure, delete operation does not delete result
249         result_set(NULL);
250         return pArgs;
251     }
252
253 public :
254     void visitprivate(const MatrixLineExp &e)
255     {
256         /*
257         All processes are done in MatrixExp
258         */
259     }
260
261
262     void visitprivate(const CellExp &e)
263     {
264         std::list<MatrixLineExp *>::const_iterator row;
265         std::list<Exp *>::const_iterator col;
266         int iColMax = 0;
267
268         //check dimmension
269         for (row = e.lines_get().begin() ; row != e.lines_get().end() ; ++row )
270         {
271             if (iColMax == 0)
272             {
273                 iColMax = static_cast<int>((*row)->columns_get().size());
274             }
275
276             if (iColMax != static_cast<int>((*row)->columns_get().size()))
277             {
278                 std::wostringstream os;
279                 os << _W("inconsistent row/column dimensions\n");
280                 //os << ((Location)(*row)->location_get()).location_getString() << std::endl;
281                 throw ScilabError(os.str(), 999, (*row)->location_get());
282             }
283         }
284
285         //alloc result cell
286         types::Cell *pC = new types::Cell(static_cast<int>(e.lines_get().size()), iColMax);
287
288         int i = 0;
289         int j = 0;
290
291         //insert items in cell
292         for (i = 0, row = e.lines_get().begin() ; row != e.lines_get().end() ; row++, i++ )
293         {
294             for (j = 0, col = (*row)->columns_get().begin() ; col != (*row)->columns_get().end() ; col++, j++)
295             {
296                 (*col)->accept(*this);
297                 InternalType *pIT = result_get();
298                 if (pIT->isImplicitList())
299                 {
300                     pIT = pIT->getAs<ImplicitList>()->extractFullMatrix();
301                 }
302
303                 pC->set(i, j, pIT);
304                 result_set(NULL);
305             }
306         }
307
308         //return new cell
309         result_set(pC);
310     }
311
312     /** \name Visit Constant Expressions nodes.
313     ** \{ */
314
315     void visitprivate(const StringExp &e)
316     {
317         if (e.getBigString() == NULL)
318         {
319             types::String *psz = new types::String(e.value_get().c_str());
320             (const_cast<StringExp *>(&e))->setBigString(psz);
321
322         }
323         result_set(e.getBigString());
324     }
325
326
327     void visitprivate(const CommentExp &e)
328     {
329         /*
330         Nothing to do
331         */
332     }
333
334
335     void visitprivate(const IntExp  &e)
336     {
337         /*
338         Int does not exist, Int8 - 16 - 32 - 64 functions
339         */
340     }
341
342
343     void visitprivate(const FloatExp  &e)
344     {
345         /*
346         Float does not exist, float function
347         */
348     }
349
350
351     void visitprivate(const DoubleExp  &e)
352     {
353         if (e.getBigDouble() == NULL)
354         {
355             Double *pdbl = new Double(e.value_get());
356             (const_cast<DoubleExp *>(&e))->setBigDouble(pdbl);
357
358         }
359         result_set(e.getBigDouble());
360     }
361
362
363     void visitprivate(const BoolExp  &e)
364     {
365         if (e.getBigBool() == NULL)
366         {
367             Bool *pB = new Bool(e.value_get());
368             (const_cast<BoolExp *>(&e))->setBigBool(pB);
369
370         }
371         result_set(e.getBigBool());
372     }
373
374
375     void visitprivate(const NilExp &e)
376     {
377         result_set(new types::Void());
378     }
379
380
381     void visitprivate(const SimpleVar &e)
382     {
383         InternalType *pI = symbol::Context::getInstance()->get(e.name_get());
384         result_set(pI);
385         if (pI != NULL)
386         {
387             if (e.is_verbose() && pI->isCallable() == false && ConfigVariable::isPromptShow())
388             {
389                 std::wostringstream ostr;
390                 ostr << e.name_get().name_get() << L"  = " << L"(" << pI->getRef() << L")" << std::endl;
391                 ostr << std::endl;
392                 scilabWriteW(ostr.str().c_str());
393                 VariableToString(pI, e.name_get().name_get().c_str());
394             }
395         }
396         else
397         {
398             char pstError[bsiz];
399             wchar_t* pwstError;
400
401             char* strErr =  wide_string_to_UTF8(e.name_get().name_get().c_str());
402
403             sprintf(pstError, _("Undefined variable: %s\n"), strErr);
404             pwstError = to_wide_string(pstError);
405             FREE(strErr);
406             std::wstring wstError(pwstError);
407             FREE(pwstError);
408             throw ScilabError(wstError, 999, e.location_get());
409             //Err, SimpleVar doesn't exist in Scilab scopes.
410         }
411     }
412
413
414     void visitprivate(const ColonVar &e)
415     {
416         //int pRank[1] = {2};
417         //Double dblCoef(1,2);
418         //dblCoef.set(0, 0, 0);
419         //dblCoef.set(0, 1, 1);
420
421         //Polynom *pVar = new Polynom(L"$", 1, 1, pRank);
422         //SinglePoly *poPoly = pVar->get(0,0);
423         //poPoly->setCoef(&dblCoef);
424
425         //ImplicitList *pIL = new ImplicitList();
426         //pIL->setStart(new Double(1));
427         //pIL->setStep(new Double(1));
428         //pIL->setEnd(pVar);
429
430         Colon *pC = new Colon();
431         result_set(pC);
432         /*
433         : = 1:$
434         */
435     }
436
437
438     void visitprivate(const DollarVar &e)
439     {
440         //int pRank[1] = {2};
441         //Double dblCoef(1,2);
442         //dblCoef.set(0, 0, 0);
443         //dblCoef.set(0, 1, 1);
444
445         //Polynom *pVar = new Polynom(L"$", 1, 1, pRank);
446         //SinglePoly *poPoly = pVar->get(0,0);
447         //poPoly->setCoef(&dblCoef);
448
449         Dollar* pVar = new Dollar();
450         result_set(pVar);
451     }
452
453
454     void visitprivate(const ArrayListVar &e)
455     {
456         /*
457
458         */
459     }
460
461
462     void visitprivate(const FieldExp &e)
463     {
464         /*
465         a.b
466         */
467         try
468         {
469             e.head_get()->accept(*this);
470         }
471         catch (ScilabError error)
472         {
473             throw error;
474         }
475
476         SimpleVar *psvRightMember = dynamic_cast<SimpleVar *>(const_cast<Exp *>(e.tail_get()));
477         std::wstring wstField = L"";
478         if (psvRightMember != NULL)
479         {
480             wstField = psvRightMember->name_get().name_get();
481         }
482         else
483         {
484             wchar_t szError[bsiz];
485             os_swprintf(szError, bsiz, _W("/!\\ Unmanaged FieldExp.\n"));
486             throw ScilabError(szError, 999, e.location_get());
487         }
488
489         if (result_get() == NULL)
490         {
491             wchar_t szError[bsiz];
492             os_swprintf(szError, bsiz, _W("Attempt to reference field of non-structure array.\n"));
493             throw ScilabError(szError, 999, e.location_get());
494         }
495
496         if (result_get()->isStruct())
497         {
498             InternalType* pTemp = result_get();
499             result_set(NULL);
500             Struct* psValue = pTemp->getAs<Struct>();
501             if (psValue->exists(wstField))
502             {
503                 if (psValue->getSize() != 1)
504                 {
505                     std::vector<std::wstring> wstFields;
506                     wstFields.push_back(wstField);
507                     std::vector<InternalType*> result;
508                     result = psValue->extractFields(wstFields);
509                     result_set(result[0]);
510                 }
511                 else
512                 {
513                     InternalType* pIT = psValue->get(0)->get(wstField)->clone();
514                     result_set(pIT);
515                 }
516             }
517             else
518             {
519                 wchar_t szError[bsiz];
520                 os_swprintf(szError, bsiz, _W("Unknown field : %ls.\n"), wstField.c_str());
521                 throw ScilabError(szError, 999, e.tail_get()->location_get());
522             }
523         }
524         else if (result_get()->isMList() || result_get()->isTList())
525         {
526             TList* psValue = ((InternalType*)result_get())->getAs<MList>();
527
528             if (psValue->exists(wstField))
529             {
530                 //without overloading function, extract by name
531                 result_set(psValue->getField(wstField));
532             }
533             else
534             {
535                 //call %mlisttype_e
536                 std::wostringstream ostr;
537                 types::typed_list in;
538                 types::optional_list opt;
539                 types::typed_list out;
540
541                 //firt argument: index
542                 String* pS = new String(wstField.c_str());
543                 pS->IncreaseRef();
544                 in.push_back(pS);
545
546                 //second argument: me ( mlist or tlist )
547                 psValue->IncreaseRef();
548                 in.push_back(psValue);
549
550                 Callable::ReturnValue Ret = Overload::call(L"%" + psValue->getShortTypeStr() + L"_e", in, 1, out, this);
551
552                 if (Ret != Callable::OK)
553                 {
554                     throw ScilabError();
555                 }
556
557                 if (out.size() == 0)
558                 {
559                     result_set(NULL);
560                 }
561                 else if (out.size() == 1)
562                 {
563                     out[0]->DecreaseRef();
564                     result_set(out[0]);
565                 }
566                 else
567                 {
568                     for (int i = 0 ; i < static_cast<int>(out.size()) ; i++)
569                     {
570                         out[i]->DecreaseRef();
571                         result_set(i, out[i]);
572                     }
573                 }
574
575                 psValue->DecreaseRef();
576                 pS->DecreaseRef();
577             }
578         }
579         else if (result_get()->isHandle())
580         {
581             typed_list in;
582             typed_list out;
583             optional_list opt;
584
585             String* pField = new String(wstField.c_str());
586             in.push_back(pField);
587             in.push_back(result_get());
588
589             Function* pCall = (Function*)symbol::Context::getInstance()->get(symbol::Symbol(L"%h_e"));
590             Callable::ReturnValue ret =  pCall->call(in, opt, 1, out, this);
591             if (ret == Callable::OK)
592             {
593                 result_set(out[0]);
594             }
595         }
596         else if (result_get()->isLibrary())
597         {
598             Library* pLib = ((InternalType*)result_get())->getAs<Library>();
599
600             InternalType* pIT = pLib->get(wstField);
601             if (pIT == NULL)
602             {
603                 wchar_t szError[bsiz];
604                 os_swprintf(szError, bsiz, _W("Unknown macro %ls in library.\n"), wstField.c_str());
605                 throw ScilabError(szError, 999, e.tail_get()->location_get());
606             }
607
608             result_set(pIT);
609         }
610         else
611         {
612             wchar_t szError[bsiz];
613             os_swprintf(szError, bsiz, _W("Attempt to reference field of non-structure array.\n"));
614             throw ScilabError(szError, 999, e.location_get());
615         }
616     }
617
618     void visitprivate(const IfExp  &e)
619     {
620         //Create local exec visitor
621         ShortCutVisitor SCTest;
622         bool bTestStatus = false;
623
624         //condition
625         e.test_get().accept(SCTest);
626         e.test_get().accept(*this);
627
628         bTestStatus = bConditionState(result_get());
629         result_clear();
630         if (bTestStatus == true)
631         {
632             //condition == true
633             if (e.is_breakable())
634             {
635                 const_cast<IfExp*>(&e)->break_reset();
636                 const_cast<Exp*>(&e.then_get())->breakable_set();
637             }
638
639             if (e.is_continuable())
640             {
641                 const_cast<IfExp*>(&e)->continue_reset();
642                 const_cast<Exp*>(&e.then_get())->continuable_set();
643             }
644
645             if (e.is_returnable())
646             {
647                 const_cast<IfExp*>(&e)->return_reset();
648                 const_cast<Exp*>(&e.then_get())->returnable_set();
649             }
650
651             e.then_get().accept(*this);
652         }
653         else
654         {
655             //condition == false
656
657             if (e.has_else())
658             {
659                 if (e.is_breakable())
660                 {
661                     const_cast<Exp*>(&e.else_get())->breakable_set();
662                 }
663
664                 if (e.is_continuable())
665                 {
666                     const_cast<IfExp*>(&e)->continue_reset();
667                     const_cast<Exp*>(&e.else_get())->continuable_set();
668                 }
669
670                 if (e.is_returnable())
671                 {
672                     const_cast<Exp*>(&e.else_get())->returnable_set();
673                 }
674
675                 e.else_get().accept(*this);
676             }
677         }
678
679         if (e.is_breakable()
680                 && ( (&e.else_get())->is_break()
681                      || (&e.then_get())->is_break() ))
682         {
683             const_cast<IfExp*>(&e)->break_set();
684             const_cast<Exp*>(&e.else_get())->break_reset();
685             const_cast<Exp*>(&e.then_get())->break_reset();
686         }
687
688         if (e.is_continuable()
689                 && ( (&e.else_get())->is_continue()
690                      || (&e.then_get())->is_continue() ))
691         {
692             const_cast<IfExp*>(&e)->continue_set();
693             const_cast<Exp*>(&e.else_get())->continue_reset();
694             const_cast<Exp*>(&e.then_get())->continue_reset();
695         }
696
697         if (e.is_returnable()
698                 && ( (&e.else_get())->is_return()
699                      || (&e.then_get())->is_return() ))
700         {
701             const_cast<IfExp*>(&e)->return_set();
702             const_cast<Exp*>(&e.else_get())->return_reset();
703             const_cast<Exp*>(&e.then_get())->return_reset();
704         }
705     }
706
707
708     void visitprivate(const TryCatchExp  &e)
709     {
710         //save current prompt mode
711         int oldVal = ConfigVariable::getSilentError();
712         //set mode silent for errors
713         ConfigVariable::setSilentError(1);
714         try
715         {
716             e.try_get().accept(*this);
717             //restore previous prompt mode
718             ConfigVariable::setSilentError(oldVal);
719         }
720         catch (ScilabMessage sm)
721         {
722             //restore previous prompt mode
723             ConfigVariable::setSilentError(oldVal);
724             //to lock lasterror
725             ConfigVariable::setLastErrorCall();
726             e.catch_get().accept(*this);
727         }
728     }
729
730
731     void visitprivate(const WhileExp  &e)
732     {
733         //allow break and continue operations
734         const_cast<Exp*>(&e.body_get())->breakable_set();
735         const_cast<Exp*>(&e.body_get())->continuable_set();
736         //allow return operation
737         if (e.is_returnable())
738         {
739             (&e.body_get())->is_returnable();
740         }
741
742         //condition
743         e.test_get().accept(*this);
744         while (bConditionState(result_get()))
745         {
746             e.body_get().accept(*this);
747             if (e.body_get().is_break())
748             {
749                 const_cast<Exp*>(&(e.body_get()))->break_reset();
750                 break;
751             }
752
753             if (e.body_get().is_return())
754             {
755                 const_cast<WhileExp*>(&e)->return_set();
756                 const_cast<Exp*>(&(e.body_get()))->return_reset();
757                 break;
758             }
759
760             if (e.body_get().is_continue())
761             {
762                 const_cast<WhileExp*>(&e)->continue_set();
763                 const_cast<Exp*>(&(e.body_get()))->continue_reset();
764                 e.test_get().accept(*this);
765                 continue;
766             }
767
768             //clear old result value before evaluate new one
769             if (result_get() != NULL)
770             {
771                 if (result_get()->isDeletable())
772                 {
773                     delete result_get();
774                 }
775             }
776
777             e.test_get().accept(*this);
778         }
779
780         //clear result of condition or result of body
781         result_clear();
782     }
783
784
785     void visitprivate(const ForExp  &e)
786     {
787         e.vardec_get().accept(*this);
788         InternalType* pIT = result_get();
789         //allow break and continue operations
790         const_cast<Exp*>(&e.body_get())->breakable_set();
791         const_cast<Exp*>(&e.body_get())->continuable_set();
792
793         //allow return operation
794         if (e.is_returnable())
795         {
796             (&e.body_get())->is_returnable();
797         }
798
799         if (result_get()->isImplicitList())
800         {
801             ImplicitList* pVar = pIT->getAs<ImplicitList>();
802
803             InternalType *pIL = NULL;
804             pIL = pVar->extractValue(0);
805             symbol::Symbol varName = e.vardec_get().name_get();
806
807             for (int i = 0 ; i < pVar->getSize() ; i++)
808             {
809                 pIL = pVar->extractValue(i);
810                 symbol::Context::getInstance()->put(varName, *pIL);
811
812                 e.body_get().accept(*this);
813                 if (e.body_get().is_break())
814                 {
815                     const_cast<Exp*>(&(e.body_get()))->break_reset();
816                     break;
817                 }
818
819                 if (e.body_get().is_continue())
820                 {
821                     const_cast<Exp*>(&(e.body_get()))->continue_reset();
822                     continue;
823                 }
824
825                 if (e.body_get().is_return())
826                 {
827                     const_cast<ForExp*>(&e)->return_set();
828                     break;
829                 }
830             }
831         }
832         else if (result_get()->isList())
833         {
834             List* pL = pIT->getAs<List>();
835             for (int i = 0 ; i < pL->getSize() ; i++)
836             {
837                 InternalType* pNew = pL->get(i);
838                 symbol::Context::getInstance()->put(e.vardec_get().name_get(), *pNew);
839                 e.body_get().accept(*this);
840                 if (e.body_get().is_break())
841                 {
842                     const_cast<Exp*>(&(e.body_get()))->break_reset();
843                     break;
844                 }
845
846                 if (e.body_get().is_continue())
847                 {
848                     const_cast<Exp*>(&(e.body_get()))->continue_reset();
849                     continue;
850                 }
851
852                 if (e.body_get().is_return())
853                 {
854                     const_cast<ForExp*>(&e)->return_set();
855                     break;
856                 }
857             }
858         }
859         else
860         {
861             //Matrix i = [1,3,2,6] or other type
862             GenericType* pVar = pIT->getAs<GenericType>();
863             if (pVar->getDims() > 2)
864             {
865                 throw ScilabError(_W("for expression can only manage 1 or 2 dimensions variables\n"), 999, e.vardec_get().location_get());
866             }
867
868             for (int i = 0 ; i < pVar->getCols() ; i++)
869             {
870                 GenericType* pNew = pVar->getColumnValues(i);
871                 symbol::Context::getInstance()->put(e.vardec_get().name_get(), *pNew);
872                 e.body_get().accept(*this);
873                 if (e.body_get().is_break())
874                 {
875                     const_cast<Exp*>(&(e.body_get()))->break_reset();
876                     break;
877                 }
878
879                 if (e.body_get().is_continue())
880                 {
881                     const_cast<Exp*>(&(e.body_get()))->continue_reset();
882                     continue;
883                 }
884
885                 if (e.body_get().is_return())
886                 {
887                     const_cast<ForExp*>(&e)->return_set();
888                     break;
889                 }
890             }
891         }
892
893         pIT->DecreaseRef();
894         if (pIT->isDeletable())
895         {
896             delete pIT;
897         }
898
899         result_set(NULL);
900     }
901
902
903     void visitprivate(const BreakExp &e)
904     {
905         const_cast<BreakExp*>(&e)->break_set();
906     }
907
908     void visitprivate(const ContinueExp &e)
909     {
910         const_cast<ContinueExp*>(&e)->continue_set();
911     }
912
913     void visitprivate(const ReturnExp &e)
914     {
915         //
916         if (e.is_global())
917         {
918             //return or resume
919             if (ConfigVariable::getPauseLevel() != 0)
920             {
921                 ThreadId* pThreadId = ConfigVariable::getLastPausedThread();
922                 if (pThreadId == NULL)
923                 {
924                     //no paused thread, so just go leave
925                     return;
926                 }
927
928                 //force exit without prompt of current thread ( via Aborted status )
929                 ThreadId* pMe = ConfigVariable::getThread(__GetCurrentThreadKey());
930                 pMe->setStatus(ThreadId::Aborted);
931
932                 //resume previous execution thread
933                 pThreadId->resume();
934
935                 return;
936             }
937             else
938             {
939                 const_cast<ReturnExp*>(&e)->return_set();
940             }
941         }
942         else
943         {
944             //return(x)
945
946             //in case of CallExp, we can return only one values
947             int iSaveExpectedSize = expected_getSize();
948             expected_setSize(1);
949             e.exp_get().accept(*this);
950             expected_setSize(iSaveExpectedSize);
951
952             if (result_getSize() == 1)
953             {
954                 //protect variable
955                 result_get()->IncreaseRef();
956             }
957             else
958             {
959                 for (int i = 0 ; i < result_getSize() ; i++)
960                 {
961                     //protect variable
962                     result_get(i)->IncreaseRef();
963                 }
964             }
965
966             if (result_getSize() == 1)
967             {
968                 //unprotect variable
969                 result_get()->DecreaseRef();
970             }
971             else
972             {
973                 for (int i = 0 ; i < result_getSize() ; i++)
974                 {
975                     //unprotect variable
976                     result_get(i)->DecreaseRef();
977                 }
978             }
979
980             const_cast<ReturnExp*>(&e)->return_set();
981         }
982
983     }
984
985
986     void visitprivate(const SelectExp &e)
987     {
988         // FIXME : exec select ... case ... else ... end
989         e.select_get()->accept(*this);
990         bool bCase = false;
991
992
993         InternalType* pIT = result_get();
994         result_set(NULL);
995         if (pIT)
996         {
997             //find good case
998             cases_t::iterator it;
999             for (it = e.cases_get()->begin(); it != e.cases_get()->end() ; it++)
1000             {
1001                 CaseExp* pCase = *it;
1002                 pCase->test_get()->accept(*this);
1003                 InternalType *pITCase = result_get();
1004                 result_set(NULL);
1005                 if (pITCase)
1006                 {
1007                     if (pITCase->isContainer()) //WARNING ONLY FOR CELL
1008                     {
1009                         //check each item
1010                     }
1011                     else if (*pITCase == *pIT)
1012                     {
1013                         if (e.is_breakable())
1014                         {
1015                             const_cast<SelectExp*>(&e)->break_reset();
1016                             pCase->body_get()->breakable_set();
1017                         }
1018
1019                         if (e.is_continuable())
1020                         {
1021                             const_cast<SelectExp*>(&e)->continue_reset();
1022                             pCase->body_get()->continuable_set();
1023                         }
1024
1025                         if (e.is_returnable())
1026                         {
1027                             const_cast<SelectExp*>(&e)->return_reset();
1028                             pCase->body_get()->returnable_set();
1029                         }
1030
1031                         //the good one
1032                         pCase->body_get()->accept(*this);
1033
1034                         if (e.is_breakable() && pCase->body_get()->is_break())
1035                         {
1036                             const_cast<SelectExp*>(&e)->break_set();
1037                             pCase->body_get()->break_reset();
1038                         }
1039
1040                         if (e.is_continuable() && pCase->body_get()->is_continue())
1041                         {
1042                             const_cast<SelectExp*>(&e)->continue_set();
1043                             pCase->body_get()->continue_reset();
1044                         }
1045
1046                         if (e.is_returnable() && pCase->body_get()->is_return())
1047                         {
1048                             const_cast<SelectExp*>(&e)->return_set();
1049                             pCase->body_get()->return_reset();
1050                         }
1051
1052                         bCase = true;
1053                         break;
1054                     }
1055                 }
1056             }
1057         }
1058
1059         if (bCase == false && e.default_case_get() != NULL)
1060         {
1061             if (e.is_breakable())
1062             {
1063                 const_cast<SelectExp*>(&e)->break_reset();
1064                 e.default_case_get()->breakable_set();
1065             }
1066
1067             if (e.is_continuable())
1068             {
1069                 const_cast<SelectExp*>(&e)->continue_reset();
1070                 e.default_case_get()->continuable_set();
1071             }
1072
1073             if (e.is_returnable())
1074             {
1075                 const_cast<SelectExp*>(&e)->return_reset();
1076                 e.default_case_get()->returnable_set();
1077             }
1078
1079             //default case
1080             e.default_case_get()->accept(*this);
1081
1082             if (e.is_breakable() && e.default_case_get()->is_break())
1083             {
1084                 const_cast<SelectExp*>(&e)->break_set();
1085                 e.default_case_get()->break_reset();
1086             }
1087
1088             if (e.is_continuable() && e.default_case_get()->is_continue())
1089             {
1090                 const_cast<SelectExp*>(&e)->continue_set();
1091                 e.default_case_get()->continue_reset();
1092             }
1093
1094             if (e.is_returnable() && e.default_case_get()->is_return())
1095             {
1096                 const_cast<SelectExp*>(&e)->return_set();
1097                 e.default_case_get()->return_reset();
1098             }
1099         }
1100
1101         result_clear();
1102     }
1103
1104
1105     void visitprivate(const CaseExp &e)
1106     {
1107     }
1108
1109
1110     void visitprivate(const SeqExp  &e)
1111     {
1112         //T execMe;
1113         std::list<Exp *>::const_iterator        itExp;
1114
1115         for (itExp = e.exps_get().begin (); itExp != e.exps_get().end (); ++itExp)
1116         {
1117             if (e.is_breakable())
1118             {
1119                 (*itExp)->break_reset();
1120                 (*itExp)->breakable_set();
1121             }
1122
1123             if (e.is_continuable())
1124             {
1125                 (*itExp)->continue_reset();
1126                 (*itExp)->continuable_set();
1127             }
1128
1129             if (e.is_returnable())
1130             {
1131                 (*itExp)->returnable_set();
1132             }
1133
1134             try
1135             {
1136                 //reset default values
1137                 result_set(NULL);
1138                 expected_setSize(-1);
1139                 (*itExp)->accept(*this);
1140
1141                 if (result_get() != NULL)
1142                 {
1143                     bool bImplicitCall = false;
1144                     if (result_get()->isCallable()) //to manage call without ()
1145                     {
1146                         Callable *pCall = ((InternalType*)result_get())->getAs<Callable>();
1147                         typed_list out;
1148                         typed_list in;
1149                         optional_list opt;
1150
1151                         try
1152                         {
1153                             //in this case of calling, we can return only one values
1154                             int iSaveExpectedSize = expected_getSize();
1155                             expected_setSize(1);
1156                             Function::ReturnValue Ret = pCall->call(in, opt, expected_getSize(), out, this);
1157                             expected_setSize(iSaveExpectedSize);
1158
1159                             if (Ret == Callable::OK)
1160                             {
1161                                 if (out.size() == 0)
1162                                 {
1163                                     result_set(NULL);
1164                                 }
1165                                 else
1166                                 {
1167                                     result_set(out[0]);
1168                                 }
1169                                 bImplicitCall = true;
1170                             }
1171                             else if (Ret == Callable::Error)
1172                             {
1173                                 if (ConfigVariable::getLastErrorFunction() == L"")
1174                                 {
1175                                     ConfigVariable::setLastErrorFunction(pCall->getName());
1176                                     ConfigVariable::setLastErrorLine(e.location_get().first_line);
1177                                     throw ScilabError();
1178                                 }
1179
1180                                 if (pCall->isMacro() || pCall->isMacroFile())
1181                                 {
1182                                     wchar_t szError[bsiz];
1183                                     os_swprintf(szError, bsiz, _W("at line % 5d of function %ls called by :\n"), (*itExp)->location_get().first_line, pCall->getName().c_str());
1184                                     throw ScilabMessage(szError);
1185                                 }
1186                                 else
1187                                 {
1188                                     throw ScilabMessage();
1189                                 }
1190                             }
1191                         }
1192                         catch (ScilabMessage sm)
1193                         {
1194                             wostringstream os;
1195                             PrintVisitor printMe(os);
1196                             (*itExp)->accept(printMe);
1197                             //os << std::endl << std::endl;
1198                             if (ConfigVariable::getLastErrorFunction() == L"")
1199                             {
1200                                 ConfigVariable::setLastErrorFunction(pCall->getName());
1201                             }
1202
1203                             if (pCall->isMacro() || pCall->isMacroFile())
1204                             {
1205                                 wchar_t szError[bsiz];
1206                                 os_swprintf(szError, bsiz, _W("at line % 5d of function %ls called by :\n"), sm.GetErrorLocation().first_line, pCall->getName().c_str());
1207                                 throw ScilabMessage(szError + os.str());
1208                             }
1209                             else
1210                             {
1211                                 sm.SetErrorMessage(sm.GetErrorMessage() + os.str());
1212                                 throw sm;
1213                             }
1214                         }
1215                     }
1216
1217                     SimpleVar* pVar = dynamic_cast<SimpleVar*>(*itExp);
1218                     //don't output Simplevar and empty result
1219                     if (result_get() != NULL && (pVar == NULL || bImplicitCall))
1220                     {
1221                         //symbol::Context::getInstance()->put(symbol::Symbol(L"ans"), *execMe.result_get());
1222                         InternalType* pITAns = result_get();
1223                         symbol::Context::getInstance()->put(*m_pAns, *pITAns);
1224                         if ((*itExp)->is_verbose() && ConfigVariable::isPromptShow())
1225                         {
1226                             //TODO manage multiple returns
1227                             scilabWriteW(L" ans  =\n\n");
1228                             VariableToString(pITAns, L"ans");
1229                         }
1230                     }
1231                 }
1232
1233                 if ((&e)->is_breakable() && (*itExp)->is_break())
1234                 {
1235                     const_cast<SeqExp *>(&e)->break_set();
1236                     break;
1237                 }
1238
1239                 if ((&e)->is_continuable() && (*itExp)->is_continue())
1240                 {
1241                     const_cast<SeqExp *>(&e)->continue_set();
1242                     break;
1243                 }
1244
1245                 if ((&e)->is_returnable() && (*itExp)->is_return())
1246                 {
1247                     const_cast<SeqExp *>(&e)->return_set();
1248                     (*itExp)->return_reset();
1249                     break;
1250                 }
1251             }
1252             catch (ScilabMessage sm)
1253             {
1254                 scilabErrorW(sm.GetErrorMessage().c_str());
1255
1256                 CallExp* pCall = dynamic_cast<CallExp*>(*itExp);
1257                 if (pCall != NULL)
1258                 {
1259                     //to print call expression only of it is a macro
1260                     pCall->name_get().accept(*this);
1261
1262                     if (result_get() != NULL && (result_get()->isMacro() || result_get()->isMacroFile()))
1263                     {
1264                         wostringstream os;
1265                         PrintVisitor printMe(os);
1266                         pCall->accept(printMe);
1267                         //os << std::endl << std::endl;
1268                         if (ConfigVariable::getLastErrorFunction() == L"")
1269                         {
1270                             ConfigVariable::setLastErrorFunction(((InternalType*)result_get())->getAs<Callable>()->getName());
1271                         }
1272                         throw ScilabMessage(os.str(), 0, (*itExp)->location_get());
1273                     }
1274                 }
1275
1276                 throw ScilabMessage((*itExp)->location_get());
1277             }
1278             catch (ScilabError se)
1279             {
1280                 // check on error number because error message can be empty.
1281                 if (ConfigVariable::getLastErrorNumber() == 0)
1282                 {
1283                     ConfigVariable::setLastErrorMessage(se.GetErrorMessage());
1284                     ConfigVariable::setLastErrorNumber(se.GetErrorNumber());
1285                     ConfigVariable::setLastErrorLine(se.GetErrorLocation().first_line);
1286                     ConfigVariable::setLastErrorFunction(wstring(L""));
1287                 }
1288
1289                 CallExp* pCall = dynamic_cast<CallExp*>(*itExp);
1290                 if (pCall != NULL)
1291                 {
1292                     //to print call expression only of it is a macro
1293                     try
1294                     {
1295                         pCall->name_get().accept(*this);
1296                         if (result_get() != NULL && (result_get()->isMacro() || result_get()->isMacroFile()))
1297                         {
1298                             wostringstream os;
1299                             PrintVisitor printMe(os);
1300                             pCall->accept(printMe);
1301                             //os << std::endl << std::endl;
1302                             ConfigVariable::setLastErrorFunction(((InternalType*)result_get())->getAs<Callable>()->getName());
1303                             scilabErrorW(se.GetErrorMessage().c_str());
1304                             throw ScilabMessage(os.str(), 999, (*itExp)->location_get());
1305                         }
1306                     }
1307                     catch (ScilabError se2)
1308                     {
1309                         //just to catch exception, do nothing
1310                     }
1311                 }
1312
1313                 scilabErrorW(se.GetErrorMessage().c_str());
1314                 scilabErrorW(L"\n");
1315                 throw ScilabMessage((*itExp)->location_get());
1316             }
1317             result_set(NULL);
1318         }
1319     }
1320
1321
1322     void visitprivate(const ArrayListExp  &e)
1323     {
1324         std::list<Exp *>::const_iterator it;
1325         int i = 0;
1326
1327         std::list<InternalType*> lstIT;
1328         for (it = e.exps_get().begin() ; it != e.exps_get().end() ; it++)
1329         {
1330             (*it)->accept(*this);
1331             lstIT.push_back(result_get()->clone());
1332         }
1333
1334         std::list<InternalType*>::iterator itIT = lstIT.begin();
1335         for (; itIT != lstIT.end(); itIT++)
1336         {
1337             result_set(i++, *itIT);
1338         }
1339     }
1340
1341
1342     void visitprivate(const AssignListExp  &e)
1343     {
1344
1345     }
1346     /** \} */
1347
1348     /** \name Visit Single Operation nodes.
1349     ** \{ */
1350
1351     void visitprivate(const NotExp &e)
1352     {
1353         /*
1354         @ or ~ !
1355         */
1356         e.exp_get().accept(*this);
1357
1358         if (result_get()->isDouble())
1359         {
1360             InternalType* pVar  = result_get();
1361             Double *pdbl        = pVar->getAs<Double>();
1362             Bool *pReturn       = new Bool(pdbl->getDims(), pdbl->getDimsArray());
1363             double *pR              = pdbl->getReal();
1364             int *piB            = pReturn->get();
1365             for (int i = 0 ; i < pdbl->getSize() ; i++)
1366             {
1367                 piB[i] = pR[i] == 0 ? 1 : 0;
1368             }
1369
1370             if (result_get()->isDeletable())
1371             {
1372                 delete result_get();
1373             }
1374
1375             result_set(pReturn);
1376         }
1377         else if (result_get()->isBool())
1378         {
1379             InternalType* pIT   = result_get();
1380             Bool *pb            = pIT->getAs<types::Bool>();
1381             Bool *pReturn       = new Bool(pb->getDims(), pb->getDimsArray());
1382             int *piR            = pb->get();
1383             int *piB            = pReturn->get();
1384
1385             for (int i = 0 ; i < pb->getSize() ; i++)
1386             {
1387                 piB[i] = piR[i] == 1 ? 0 : 1;
1388             }
1389
1390             if (result_get()->isDeletable())
1391             {
1392                 delete result_get();
1393             }
1394
1395             result_set(pReturn);
1396         }
1397         else if (result_get()->isSparseBool())
1398         {
1399             InternalType* pIT   = result_get();
1400             SparseBool *pb            = pIT->getAs<types::SparseBool>();
1401             SparseBool *pReturn       = new SparseBool(pb->getRows(), pb->getCols());
1402
1403             for (int iRows = 0 ; iRows < pb->getRows() ; iRows++)
1404             {
1405                 for (int iCols = 0 ; iCols < pb->getCols() ; iCols++)
1406                 {
1407                     pReturn->set(iRows, iCols, !pb->get(iRows, iCols));
1408                 }
1409             }
1410
1411
1412             result_set(pReturn);
1413         }
1414         else if (result_get()->isInt())
1415         {
1416             InternalType* pReturn = NULL;
1417             InternalType* pIT = result_get();
1418             switch (result_get()->getType())
1419             {
1420                 case InternalType::RealInt8 :
1421                     pReturn = notInt< Int8, char >(pIT->getAs<Int8>());
1422                     break;
1423                 case InternalType::RealUInt8 :
1424                     pReturn = notInt<UInt8, unsigned char>(pIT->getAs<UInt8>());
1425                     break;
1426                 case InternalType::RealInt16 :
1427                     pReturn = notInt< Int16, short >(pIT->getAs<Int16>());
1428                     break;
1429                 case InternalType::RealUInt16 :
1430                     pReturn = notInt<UInt16, unsigned short>(pIT->getAs<UInt16>());
1431                     break;
1432                 case InternalType::RealInt32 :
1433                     pReturn = notInt<Int32, int>(pIT->getAs<Int32>());
1434                     break;
1435                 case InternalType::RealUInt32 :
1436                     pReturn = notInt<UInt32, unsigned int>(pIT->getAs<UInt32>());
1437                     break;
1438                 case InternalType::RealInt64 :
1439                     pReturn = notInt<Int64, long long>(pIT->getAs<Int64>());
1440                     break;
1441                 case InternalType::RealUInt64 :
1442                     pReturn = notInt<UInt64, unsigned long long>(pIT->getAs<UInt64>());
1443                     break;
1444                 default :
1445                     break;
1446             }
1447
1448             if (result_get()->isDeletable())
1449             {
1450                 delete result_get();
1451             }
1452
1453             result_set(pReturn);
1454         }
1455     }
1456
1457     template <class intT, typename Y>
1458     InternalType* notInt(intT* _pInt)
1459     {
1460         intT* pOut = new intT(_pInt->getRows(), _pInt->getCols());
1461         Y* pDataIn = _pInt->get();
1462         Y* pDataOut = pOut->get();
1463         for (int i = 0 ; i < _pInt->getSize() ; i++)
1464         {
1465             pDataOut[i] = ~pDataIn[i];
1466         }
1467
1468         return pOut;
1469     }
1470
1471     void visitprivate(const TransposeExp &e)
1472     {
1473         /*
1474         '
1475         */
1476         e.exp_get().accept(*this);
1477
1478         bool bConjug = e.conjugate_get() == TransposeExp::_Conjugate_;
1479
1480         if (result_get()->isImplicitList())
1481         {
1482             InternalType *pIT = ((InternalType*)result_get())->getAs<ImplicitList>()->extractFullMatrix();
1483             if (result_get()->isDeletable())
1484             {
1485                 delete result_get();
1486             }
1487
1488             result_set(pIT);
1489         }
1490
1491         if (result_get()->isDouble())
1492         {
1493             InternalType* pVar  = result_get();
1494             Double *pdbl                = pVar->getAs<Double>();
1495             Double *pReturn         = NULL;
1496
1497             if (pdbl->isComplex())
1498             {
1499                 pReturn         = new Double(pdbl->getCols(), pdbl->getRows(), true);
1500                 double *pInR    = pdbl->getReal();
1501                 double *pInI    = pdbl->getImg();
1502                 double *pOutR   = pReturn->getReal();
1503                 double *pOutI   = pReturn->getImg();
1504
1505                 vTransposeComplexMatrix(pInR, pInI, pdbl->getRows(), pdbl->getCols(), pOutR, pOutI, bConjug);
1506             }
1507             else
1508             {
1509                 pReturn         = new Double(pdbl->getCols(), pdbl->getRows(), false);
1510                 double *pInR    = pdbl->getReal();
1511                 double *pOutR   = pReturn->getReal();
1512
1513                 vTransposeRealMatrix(pInR, pdbl->getRows(), pdbl->getCols(), pOutR);
1514             }
1515
1516             if (result_get()->isDeletable())
1517             {
1518                 delete result_get();
1519             }
1520
1521             result_set(pReturn);
1522         }
1523         else if (result_get()->isPoly())
1524         {
1525             InternalType *pIT   = result_get();
1526             Polynom *pMP        = pIT->getAs<types::Polynom>();
1527             Polynom *pReturn    = NULL;
1528
1529             //prepare rank array
1530             int* piRank = new int[pMP->getSize()];
1531
1532             for (int i = 0 ; i < pMP->getRows() ; i++)
1533             {
1534                 for (int j = 0 ; j < pMP->getCols() ; j++)
1535                 {
1536                     piRank[i * pMP->getCols() + j] = pMP->get(i, j)->getRank();
1537                 }
1538             }
1539
1540             pReturn = new Polynom(pMP->getVariableName(), pMP->getCols(), pMP->getRows(), piRank);
1541             pReturn->setComplex(pMP->isComplex());
1542
1543             if (pMP->isComplex() && bConjug)
1544             {
1545                 for (int i = 0 ; i < pMP->getRows() ; i++)
1546                 {
1547                     for (int j = 0 ; j < pMP->getCols() ; j++)
1548                     {
1549                         pReturn->setCoef(j, i, pMP->get(i, j)->getCoef());
1550                         double *pdblImg = pReturn->get(j, i)->getCoefImg();
1551                         for (int k = 0 ; k < pReturn->get(j, i)->getRank() ; k++)
1552                         {
1553                             pdblImg[k] *= -1;
1554                         }
1555                     }
1556                 }
1557             }
1558             else
1559             {
1560                 for (int i = 0 ; i < pMP->getRows() ; i++)
1561                 {
1562                     for (int j = 0 ; j < pMP->getCols() ; j++)
1563                     {
1564                         pReturn->setCoef(j, i, pMP->get(i, j)->getCoef());
1565                     }
1566                 }
1567             }
1568
1569             if (result_get()->isDeletable())
1570             {
1571                 delete result_get();
1572             }
1573
1574             result_set(pReturn);
1575         }
1576         else if (result_get()->isString())
1577         {
1578             InternalType* pVar  = result_get();
1579             types::String *pS          = pVar->getAs<types::String>();
1580             types::String* pReturn     = new types::String(pS->getCols(), pS->getRows());
1581
1582             for (int i = 0 ; i < pS->getRows() ; i++)
1583             {
1584                 for (int j = 0 ; j < pS->getCols() ; j++)
1585                 {
1586                     pReturn->set(j, i, pS->get(i, j));
1587                 }
1588             }
1589
1590             if (result_get()->isDeletable())
1591             {
1592                 delete result_get();
1593             }
1594
1595             result_set(pReturn);
1596         }
1597         else if (result_get()->isBool())
1598         {
1599             InternalType* pVar  = result_get();
1600             types::Bool *pB = pVar->getAs<types::Bool>();
1601             types::Bool* pReturn = new types::Bool(pB->getCols(), pB->getRows());
1602
1603             for (int i = 0 ; i < pB->getRows() ; i++)
1604             {
1605                 for (int j = 0 ; j < pB->getCols() ; j++)
1606                 {
1607                     pReturn->set(j, i, pB->get(i, j));
1608                 }
1609             }
1610
1611             if (result_get()->isDeletable())
1612             {
1613                 delete result_get();
1614             }
1615
1616             result_set(pReturn);
1617         }
1618         else if (result_get()->isSparse())
1619         {
1620             types::InternalType* pIT = result_get();
1621             result_set(pIT->getAs<types::Sparse>()->newTransposed());
1622         }
1623         else if (result_get()->isSparseBool())
1624         {
1625             types::InternalType* pIT = result_get();
1626             result_set(pIT->getAs<types::SparseBool>()->newTransposed());
1627         }
1628         else if (result_get()->isInt())
1629         {
1630             InternalType* pVar      = result_get();
1631             InternalType* pReturn   = NULL;
1632
1633             switch (pVar->getType())
1634             {
1635                 case types::InternalType::RealInt8 :
1636                 {
1637                     types::Int8* pIntIn  = pVar->getAs<types::Int8>();
1638                     types::Int8* pIntOut = new types::Int8(pIntIn->getCols(), pIntIn->getRows());
1639                     char* pIn  = pIntIn->get();
1640                     char* pOut = pIntOut->get();
1641                     vTransposeIntMatrix<char>(pIn, pIntIn->getRows(), pIntIn->getCols(), pOut);
1642                     pReturn = pIntOut;
1643                     break;
1644                 }
1645                 case types::InternalType::RealUInt8 :
1646                 {
1647                     types::UInt8* pIntIn  = pVar->getAs<types::UInt8>();
1648                     types::UInt8* pIntOut = new types::UInt8(pIntIn->getCols(), pIntIn->getRows());
1649                     unsigned char* pIn  = pIntIn->get();
1650                     unsigned char* pOut = pIntOut->get();
1651                     vTransposeIntMatrix<unsigned char>(pIn, pIntIn->getRows(), pIntIn->getCols(), pOut);
1652                     pReturn = pIntOut;
1653                     break;
1654                 }
1655                 case types::InternalType::RealInt16 :
1656                 {
1657                     types::Int16* pIntIn  = pVar->getAs<types::Int16>();
1658                     types::Int16* pIntOut = new types::Int16(pIntIn->getCols(), pIntIn->getRows());
1659                     short* pIn  = pIntIn->get();
1660                     short* pOut = pIntOut->get();
1661                     vTransposeIntMatrix<short>(pIn, pIntIn->getRows(), pIntIn->getCols(), pOut);
1662                     pReturn = pIntOut;
1663                     break;
1664                 }
1665                 case types::InternalType::RealUInt16 :
1666                 {
1667                     types::UInt16* pIntIn  = pVar->getAs<types::UInt16>();
1668                     types::UInt16* pIntOut = new types::UInt16(pIntIn->getCols(), pIntIn->getRows());
1669                     unsigned short* pIn  = pIntIn->get();
1670                     unsigned short* pOut = pIntOut->get();
1671                     vTransposeIntMatrix<unsigned short>(pIn, pIntIn->getRows(), pIntIn->getCols(), pOut);
1672                     pReturn = pIntOut;
1673                     break;
1674                 }
1675                 case types::InternalType::RealInt32 :
1676                 {
1677                     types::Int32* pIntIn  = pVar->getAs<types::Int32>();
1678                     types::Int32* pIntOut = new types::Int32(pIntIn->getCols(), pIntIn->getRows());
1679                     int* pIn  = pIntIn->get();
1680                     int* pOut = pIntOut->get();
1681                     vTransposeIntMatrix<int>(pIn, pIntIn->getRows(), pIntIn->getCols(), pOut);
1682                     pReturn = pIntOut;
1683                     break;
1684                 }
1685                 case types::InternalType::RealUInt32 :
1686                 {
1687                     types::UInt32* pIntIn  = pVar->getAs<types::UInt32>();
1688                     types::UInt32* pIntOut = new types::UInt32(pIntIn->getCols(), pIntIn->getRows());
1689                     unsigned int* pIn  = pIntIn->get();
1690                     unsigned int* pOut = pIntOut->get();
1691                     vTransposeIntMatrix<unsigned int>(pIn, pIntIn->getRows(), pIntIn->getCols(), pOut);
1692                     pReturn = pIntOut;
1693                     break;
1694                 }
1695                 case types::InternalType::RealInt64 :
1696                 {
1697                     types::Int64* pIntIn  = pVar->getAs<types::Int64>();
1698                     types::Int64* pIntOut = new types::Int64(pIntIn->getCols(), pIntIn->getRows());
1699                     long long* pIn  = pIntIn->get();
1700                     long long* pOut = pIntOut->get();
1701                     vTransposeIntMatrix<long long>(pIn, pIntIn->getRows(), pIntIn->getCols(), pOut);
1702                     pReturn = pIntOut;
1703                     break;
1704                 }
1705                 case types::InternalType::RealUInt64 :
1706                 {
1707                     types::UInt64* pIntIn  = pVar->getAs<types::UInt64>();
1708                     types::UInt64* pIntOut = new types::UInt64(pIntIn->getCols(), pIntIn->getRows());
1709                     unsigned long long* pIn  = pIntIn->get();
1710                     unsigned long long* pOut = pIntOut->get();
1711                     vTransposeIntMatrix<unsigned long long>(pIn, pIntIn->getRows(), pIntIn->getCols(), pOut);
1712                     pReturn = pIntOut;
1713                     break;
1714                 }
1715                 default:
1716                     break;
1717             }
1718
1719             if (pVar->isDeletable())
1720             {
1721                 delete pVar;
1722             }
1723
1724             result_set(pReturn);
1725         }
1726     }
1727     /** \} */
1728
1729     /** \name Visit Declaration nodes.
1730     ** \{ */
1731     /** \brief Visit Var declarations. */
1732
1733     void visitprivate(const VarDec  &e)
1734     {
1735         try
1736         {
1737             /*getting what to assign*/
1738             e.init_get().accept(*this);
1739             result_get()->IncreaseRef();
1740         }
1741         catch (ScilabError error)
1742         {
1743             throw error;
1744         }
1745     }
1746
1747
1748     void visitprivate(const FunctionDec  &e)
1749     {
1750         /*
1751         function foo
1752         endfunction
1753         */
1754
1755         // funcprot(0) : do nothing
1756         // funcprot(1) && warning(on) : warning
1757         // funcprot(2) : error
1758         if (ConfigVariable::getFuncprot() == 1 && ConfigVariable::getWarningMode())
1759         {
1760             types::InternalType* pITFunc = symbol::Context::getInstance()->get(symbol::Symbol(e.name_get().name_get()));
1761
1762             if (pITFunc && pITFunc->isCallable())
1763             {
1764                 wchar_t pwstFuncName[1024];
1765                 os_swprintf(pwstFuncName, 1024, L"%-24ls", e.name_get().name_get().c_str());
1766                 char* pstFuncName = wide_string_to_UTF8(pwstFuncName);
1767
1768                 sciprint(_("Warning : redefining function: %s. Use funcprot(0) to avoid this message"), pstFuncName);
1769                 sciprint("\n");
1770                 FREE(pstFuncName);
1771             }
1772         }
1773         else if (ConfigVariable::getFuncprot() == 2)
1774         {
1775             types::InternalType* pITFunc = symbol::Context::getInstance()->get(symbol::Symbol(e.name_get().name_get()));
1776
1777             if (pITFunc && pITFunc->isCallable())
1778             {
1779                 char pstError[1024];
1780                 char* pstFuncName = wide_string_to_UTF8(e.name_get().name_get().c_str());
1781                 sprintf(pstError, _("It is not possible to redefine the %s primitive this way (see clearfun).\n"), pstFuncName);
1782                 wchar_t* pwstError = to_wide_string(pstError);
1783                 std::wstring wstError(pwstError);
1784                 FREE(pstFuncName);
1785                 FREE(pwstError);
1786                 throw ScilabError(wstError, 999, e.location_get());
1787             }
1788         }
1789
1790         std::list<ast::Var *>::const_iterator   i;
1791
1792         //get input parameters list
1793         std::list<symbol::Symbol> *pVarList = new std::list<symbol::Symbol>();
1794         const ArrayListVar *pListVar = &e.args_get();
1795         for (i = pListVar->vars_get().begin() ; i != pListVar->vars_get().end() ; i++)
1796         {
1797             pVarList->push_back(static_cast<SimpleVar*>(*i)->name_get());
1798         }
1799
1800         //get output parameters list
1801         std::list<symbol::Symbol> *pRetList = new std::list<symbol::Symbol>();
1802         const ArrayListVar *pListRet = &e.returns_get();
1803         for (i = pListRet->vars_get().begin() ; i != pListRet->vars_get().end() ; i++)
1804         {
1805             pRetList->push_back(static_cast<SimpleVar*>(*i)->name_get());
1806         }
1807
1808         //            Location* newloc = const_cast<Location*>(&location_get())->clone();
1809         Exp* exp = const_cast<Exp*>(&e.body_get())->clone();
1810
1811         //MuteVisitor mute;
1812         //exp->accept(mute);
1813
1814         //types::Macro macro(VarList, RetList, (SeqExp&)e.body_get());
1815         types::Macro *pMacro = new types::Macro(e.name_get().name_get(), *pVarList, *pRetList,
1816                                                 static_cast<SeqExp&>(*exp), L"script");
1817         pMacro->setFirstLine(e.location_get().first_line);
1818         symbol::Context::getInstance()->AddMacro(pMacro);
1819     }
1820     /** \} */
1821
1822     /** \name Visit Type dedicated Expressions related node.
1823     ** \{ */
1824
1825     void visitprivate(const ListExp &e)
1826     {
1827         try
1828         {
1829             e.start_get().accept(*this);
1830             GenericType* pITStart = static_cast<GenericType*>(result_get());
1831             if (pITStart->getRows() != 1 || pITStart->getCols() != 1)
1832             {
1833                 throw 1;
1834             }
1835             InternalType* piStart = result_get();
1836
1837             e.step_get().accept(*this);
1838             GenericType* pITStep = static_cast<GenericType*>(result_get());
1839             if (pITStep->getRows() != 1 || pITStep->getCols() != 1)
1840             {
1841                 throw 2;
1842             }
1843             InternalType* piStep = result_get();
1844
1845             e.end_get().accept(*this);
1846             GenericType* pITEnd = static_cast<GenericType*>(result_get());
1847             if (pITEnd->getRows() != 1 || pITEnd->getCols() != 1)
1848             {
1849                 throw 3;
1850             }
1851             InternalType* piEnd = result_get();
1852
1853             //check compatibility
1854
1855             if (piStart->isInt())
1856             {
1857                 //if Step or End are Int too, they must have the same precision
1858                 if (piStep->isInt())
1859                 {
1860                     if (piStep->getType() != piStart->getType())
1861                     {
1862                         throw ScilabError(_W("Undefined operation for the given operands.\n"), 999, e.step_get().location_get());
1863                     }
1864                 }
1865                 else if (piStep->isPoly())
1866                 {
1867                     throw ScilabError(_W("Undefined operation for the given operands.\n"), 999, e.step_get().location_get());
1868                 }
1869
1870
1871                 if (piEnd->isInt())
1872                 {
1873                     if (piEnd->getType() != piStart->getType())
1874                     {
1875                         throw ScilabError(_W("Undefined operation for the given operands.\n"), 999, e.end_get().location_get());
1876                     }
1877                 }
1878                 else if (piEnd->isPoly())
1879                 {
1880                     throw ScilabError(_W("Undefined operation for the given operands.\n"), 999, e.end_get().location_get());
1881                 }
1882             }
1883             else if (piStart->isPoly())
1884             {
1885                 if (piStep->isInt())
1886                 {
1887                     throw ScilabError(_W("Undefined operation for the given operands.\n"), 999, e.step_get().location_get());
1888                 }
1889
1890                 if (piEnd->isInt())
1891                 {
1892                     throw ScilabError(_W("Undefined operation for the given operands.\n"), 999, e.end_get().location_get());
1893                 }
1894             }
1895             else if (piStep->isInt())
1896             {
1897                 //if End is Int too, they must have the same precision
1898                 if (piEnd->isInt())
1899                 {
1900                     if (piEnd->getType() != piStep->getType())
1901                     {
1902                         throw ScilabError(_W("Undefined operation for the given operands.\n"), 999, e.end_get().location_get());
1903                     }
1904                 }
1905             }
1906             else if (piStep->isPoly())
1907             {
1908                 if (piEnd->isInt())
1909                 {
1910                     throw ScilabError(_W("Undefined operation for the given operands.\n"), 999, e.step_get().location_get());
1911                 }
1912             }
1913
1914             if (piStart->isDouble() && piStart->getAs<Double>()->isComplex())
1915             {
1916                 throw 1;
1917             }
1918
1919             if (piStep->isDouble() && piStep->getAs<Double>()->isComplex())
1920             {
1921                 throw 2;
1922             }
1923
1924             if (piEnd->isDouble() && piEnd->getAs<Double>()->isComplex())
1925             {
1926                 throw 3;
1927             }
1928
1929             ImplicitList *pIL = new ImplicitList(piStart, piStep, piEnd);
1930
1931             result_set(pIL);
1932
1933             if (piStart && piStart->isDeletable())
1934             {
1935                 delete piStart;
1936             }
1937
1938             if (piStep && piStep->isDeletable())
1939             {
1940                 delete piStep;
1941             }
1942
1943             if (piEnd && piEnd->isDeletable())
1944             {
1945                 delete piEnd;
1946             }
1947         }
1948         catch (int iPos)
1949         {
1950             wchar_t szError[bsiz];
1951             os_swprintf(szError, bsiz, _W("%ls: Wrong type for argument %d: Real scalar expected.\n"), L"':'", iPos);
1952             throw ScilabError(szError, 999, e.location_get());
1953         }
1954         catch (ScilabError error)
1955         {
1956             //TODO YaSp : Overloading
1957             throw error;
1958         }
1959     }
1960
1961     void VariableToString(types::InternalType* pIT, const wchar_t* wcsVarName)
1962     {
1963         std::wostringstream ostr;
1964
1965         if (pIT->isMList() || pIT->isTList() || pIT->hasToString() == false)
1966         {
1967             //call overload %type_p
1968             types::typed_list in;
1969             types::typed_list out;
1970
1971             pIT->IncreaseRef();
1972             in.push_back(pIT);
1973
1974             try
1975             {
1976                 if (Overload::call(L"%" + pIT->getAs<TList>()->getShortTypeStr() + L"_p", in, 1, out, this) != Function::OK)
1977                 {
1978                     throw ScilabError();
1979                 }
1980             }
1981             catch (ScilabError /*&e*/)
1982             {
1983                 ostr << wcsVarName;
1984                 pIT->toString(ostr);
1985                 scilabWriteW(ostr.str().c_str());
1986             }
1987
1988             pIT->DecreaseRef();
1989         }
1990         else
1991         {
1992             //to manage lines information
1993             int iLines = ConfigVariable::getConsoleLines();
1994
1995             bool bFinish = false;
1996             do
1997             {
1998                 //block by block
1999                 bFinish = pIT->toString(ostr);
2000                 scilabWriteW(ostr.str().c_str());
2001                 if (bFinish == false && iLines != 0)
2002                 {
2003                     //show message on prompt
2004                     bFinish = linesmore() == 1;
2005                 }
2006                 ostr.str(L"");
2007             }
2008             while (bFinish == false);
2009
2010             pIT->clearPrintState();
2011         }
2012     }
2013
2014     /** \} */
2015
2016 #include "run_AssignExp.hxx"
2017     // This will define
2018     // void visitprivate(const AssignExp  &e)
2019
2020 #include "run_OpExp.hxx"
2021     // This will define
2022     // void visitprivate(const OpExp &e)
2023     // void visitprivate(const LogicalOpExp &e)
2024
2025 #include "run_MatrixExp.hxx"
2026     // This will define
2027     // void visitprivate(const MatrixExp &e)
2028
2029 #include "run_CallExp.hxx"
2030     // This will define
2031     // void visitprivate(const CallExp &e)
2032 };
2033 }
2034
2035 #endif // !AST_RUNVISITOR_HXX