cb67331e63fe765f4c6fa3eb081f673b5bfa3dad
[scilab.git] / scilab / modules / ast / src / cpp / ast / runvisitor.cpp
1 /*
2  *  Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  *  Copyright (C) 2014 - Scilab Enterprises - Antoine ELIAS
4  *
5  * Copyright (C) 2012 - 2016 - Scilab Enterprises
6  *
7  * This file is hereby licensed under the terms of the GNU GPL v2.0,
8  * pursuant to article 5.3.4 of the CeCILL v.2.1.
9  * This file was originally licensed under the terms of the CeCILL v2.1,
10  * and continues to be available under such terms.
11  * For more information, see the COPYING file which you should have received
12  * along with this program.
13  *
14  */
15
16 //for Visual Leak Detector in debug compilation mode
17 //#define DEBUG_VLD
18 #if defined(DEBUG_VLD) && defined(_DEBUG)
19 #include <vld.h>
20 #endif
21
22 #include <string>
23
24 #include "execvisitor.hxx"
25 #include "stepvisitor.hxx"
26 #include "timedvisitor.hxx"
27 #include "shortcutvisitor.hxx"
28 #include "printvisitor.hxx"
29 //#include "AnalysisVisitor.hxx"
30 #include "debuggervisitor.hxx"
31 #include "debugmanager.hxx"
32
33 #include "visitor_common.hxx"
34
35 #include "context.hxx"
36 #include "generic_operations.hxx"
37 #include "types_or.hxx"
38 #include "types_and.hxx"
39 #include "localization.hxx"
40
41 #include "macrofile.hxx"
42 #include "macro.hxx"
43 #include "cell.hxx"
44 #include "listinsert.hxx"
45 #include "filemanager_interface.h"
46
47 #include "runner.hxx"
48 #include "threadmanagement.hxx"
49
50 #include "coverage_instance.hxx"
51
52 extern "C"
53 {
54 #include "sciprint.h"
55 #include "os_string.h"
56 #include "elem_common.h"
57 #include "storeCommand.h"
58 #include "prompt.h"
59 #include "scilabRead.h"
60 }
61
62 namespace ast
63 {
64 template <class T>
65 void RunVisitorT<T>::visitprivate(const StringExp & e)
66 {
67     CoverageInstance::invokeAndStartChrono((void*)&e);
68     if (e.getConstant() == nullptr)
69     {
70         types::String *psz = new types::String(e.getValue().c_str());
71         (const_cast<StringExp *>(&e))->setConstant(psz);
72     }
73     setResult(e.getConstant());
74     CoverageInstance::stopChrono((void*)&e);
75 }
76
77 template <class T>
78 void RunVisitorT<T>::visitprivate(const DoubleExp & e)
79 {
80     CoverageInstance::invokeAndStartChrono((void*)&e);
81     if (e.getConstant() == nullptr)
82     {
83         types::Double *pdbl = new types::Double(e.getValue());
84         (const_cast<DoubleExp *>(&e))->setConstant(pdbl);
85     }
86     setResult(e.getConstant());
87     CoverageInstance::stopChrono((void*)&e);
88 }
89
90 template <class T>
91 void RunVisitorT<T>::visitprivate(const BoolExp & e)
92 {
93     CoverageInstance::invokeAndStartChrono((void*)&e);
94     if (e.getConstant() == nullptr)
95     {
96         types::Bool *pB = new types::Bool(e.getValue());
97         (const_cast<BoolExp *>(&e))->setConstant(pB);
98     }
99     setResult(e.getConstant());
100     CoverageInstance::stopChrono((void*)&e);
101 }
102
103 template <class T>
104 void RunVisitorT<T>::visitprivate(const NilExp & e)
105 {
106     CoverageInstance::invokeAndStartChrono((void*)&e);
107     setResult(new types::Void());
108     CoverageInstance::stopChrono((void*)&e);
109 }
110
111 template <class T>
112 void RunVisitorT<T>::visitprivate(const SimpleVar & e)
113 {
114     CoverageInstance::invokeAndStartChrono((void*)&e);
115     symbol::Context* ctx = symbol::Context::getInstance();
116     symbol::Variable* var = ((SimpleVar&)e).getStack();
117     types::InternalType *pI = ctx->get(var);
118     setResult(pI);
119     if (pI != nullptr)
120     {
121         if (e.isVerbose() && pI->isCallable() == false && ConfigVariable::isPrintOutput())
122         {
123             std::wostringstream ostr;
124             ostr << L" " << e.getSymbol().getName() << L"  = ";
125 #ifndef NDEBUG
126             ostr << L"(" << pI->getRef() << L")";
127 #endif
128             ostr << std::endl;
129             ostr << std::endl;
130             scilabWriteW(ostr.str().c_str());
131             std::wostringstream ostrName;
132             ostrName << e.getSymbol().getName();
133             VariableToString(pI, ostrName.str().c_str());
134         }
135
136         //check if var is recalled in current scope like
137         //function f()
138         //  a; //<=> a=a;
139         //  a(2) = 18;
140         //endfunction
141         if (e.getParent()->isSeqExp())
142         {
143             if (ctx->getScopeLevel() > 1 && var->empty() == false && var->top()->m_iLevel != ctx->getScopeLevel())
144             {
145                 //put var in current scope
146                 ctx->put(var, pI);
147             }
148         }
149     }
150     else
151     {
152         char pstError[bsiz];
153         wchar_t* pwstError;
154
155         char* strErr = wide_string_to_UTF8(e.getSymbol().getName().c_str());
156
157         os_sprintf(pstError, _("Undefined variable: %s\n"), strErr);
158         pwstError = to_wide_string(pstError);
159         FREE(strErr);
160         std::wstring wstError(pwstError);
161         FREE(pwstError);
162         CoverageInstance::stopChrono((void*)&e);
163         throw InternalError(wstError, 999, e.getLocation());
164         //Err, SimpleVar doesn't exist in Scilab scopes.
165     }
166     CoverageInstance::stopChrono((void*)&e);
167 }
168
169 template <class T>
170 void RunVisitorT<T>::visitprivate(const ColonVar & e)
171 {
172     CoverageInstance::invokeAndStartChrono((void*)&e);
173     types::Colon *pC = new types::Colon();
174     setResult(pC);
175     CoverageInstance::stopChrono((void*)&e);
176 }
177
178 template <class T>
179 void RunVisitorT<T>::visitprivate(const DollarVar & e)
180 {
181     CoverageInstance::invokeAndStartChrono((void*)&e);
182     setResult(types::Polynom::Dollar());
183     CoverageInstance::stopChrono((void*)&e);
184 }
185
186 template <class T>
187 void RunVisitorT<T>::visitprivate(const BreakExp & e)
188 {
189     CoverageInstance::invokeAndStartChrono((void*)&e);
190     const_cast<BreakExp*>(&e)->setBreak();
191     CoverageInstance::stopChrono((void*)&e);
192 }
193
194 template <class T>
195 void RunVisitorT<T>::visitprivate(const ContinueExp &e)
196 {
197     CoverageInstance::invokeAndStartChrono((void*)&e);
198     const_cast<ContinueExp*>(&e)->setContinue();
199     CoverageInstance::stopChrono((void*)&e);
200 }
201
202 template <class T>
203 void RunVisitorT<T>::visitprivate(const ArrayListExp & e)
204 {
205     CoverageInstance::invokeAndStartChrono((void*)&e);
206     exps_t::const_iterator it;
207     int iNbExpSize = this->getExpectedSize();
208     this->setExpectedSize(1);
209
210     types::typed_list lstIT;
211     for (it = e.getExps().begin(); it != e.getExps().end(); it++)
212     {
213         (*it)->accept(*this);
214         for (int j = 0; j < getResultSize(); j++)
215         {
216             lstIT.push_back(getResult(j));
217         }
218     }
219
220     setResult(lstIT);
221
222     this->setExpectedSize(iNbExpSize);
223     CoverageInstance::stopChrono((void*)&e);
224 }
225
226 template <class T>
227 void RunVisitorT<T>::visitprivate(const VarDec & e)
228 {
229     CoverageInstance::invokeAndStartChrono((void*)&e);
230     try
231     {
232         /*getting what to assign*/
233         e.getInit().accept(*this);
234         getResult()->IncreaseRef();
235     }
236     catch (const InternalError& error)
237     {
238         CoverageInstance::stopChrono((void*)&e);
239         throw error;
240     }
241     CoverageInstance::stopChrono((void*)&e);
242 }
243
244 template <class T>
245 void RunVisitorT<T>::visitprivate(const CellExp & e)
246 {
247     CoverageInstance::invokeAndStartChrono((void*)&e);
248
249     exps_t::const_iterator row;
250     exps_t::const_iterator col;
251     int iColMax = 0;
252
253     exps_t lines = e.getLines();
254     //check dimmension
255     for (row = lines.begin(); row != lines.end(); ++row)
256     {
257         exps_t cols = (*row)->getAs<MatrixLineExp>()->getColumns();
258         if (iColMax == 0)
259         {
260             iColMax = static_cast<int>(cols.size());
261         }
262
263         if (iColMax != static_cast<int>(cols.size()))
264         {
265             std::wostringstream os;
266             os << _W("inconsistent row/column dimensions\n");
267             //os << ((Location)(*row)->getLocation()).getLocationString() << std::endl;
268             CoverageInstance::stopChrono((void*)&e);
269             throw InternalError(os.str(), 999, (*row)->getLocation());
270         }
271     }
272
273     //alloc result cell
274     types::Cell *pC = new types::Cell(static_cast<int>(lines.size()), iColMax);
275
276     int i = 0;
277     int j = 0;
278
279     //insert items in cell
280     for (i = 0, row = lines.begin(); row != lines.end(); ++row, ++i)
281     {
282         exps_t cols = (*row)->getAs<MatrixLineExp>()->getColumns();
283         for (j = 0, col = cols.begin(); col != cols.end(); ++col, ++j)
284         {
285             try
286             {
287                 (*col)->accept(*this);
288             }
289             catch (ScilabException &)
290             {
291                 CoverageInstance::stopChrono((void*)&e);
292                 throw;
293             }
294             types::InternalType *pIT = getResult();
295             if (pIT->isImplicitList())
296             {
297                 types::InternalType * _pIT = pIT->getAs<types::ImplicitList>()->extractFullMatrix();
298                 pC->set(i, j, _pIT);
299                 _pIT->killMe();
300             }
301             else
302             {
303                 pC->set(i, j, pIT);
304             }
305             clearResult();
306         }
307     }
308
309     //return new cell
310     setResult(pC);
311
312     CoverageInstance::stopChrono((void*)&e);
313 }
314
315 template <class T>
316 void RunVisitorT<T>::visitprivate(const FieldExp &e)
317 {
318     /*
319       a.b
320       */
321
322     CoverageInstance::invokeAndStartChrono((void*)&e);
323
324     if (!e.getTail()->isSimpleVar())
325     {
326         wchar_t szError[bsiz];
327         os_swprintf(szError, bsiz, _W("/!\\ Unmanaged FieldExp.\n").c_str());
328         CoverageInstance::stopChrono((void*)&e);
329         throw InternalError(szError, 999, e.getLocation());
330     }
331
332     try
333     {
334         e.getHead()->accept(*this);
335     }
336     catch (const InternalError& error)
337     {
338         CoverageInstance::stopChrono((void*)&e);
339         throw error;
340     }
341
342     if (getResult() == NULL)
343     {
344         wchar_t szError[bsiz];
345         os_swprintf(szError, bsiz, _W("Attempt to reference field of non-structure array.\n").c_str());
346         CoverageInstance::stopChrono((void*)&e);
347         throw InternalError(szError, 999, e.getLocation());
348     }
349
350     // TODO: handle case where getSize() > 1
351     // l=list(struct("toto","coucou"),struct("toto","hello"),1,2);[a,b]=l(1:2).toto
352     //
353     if (getResultSize() > 1)
354     {
355         clearResult();
356         wchar_t szError[bsiz];
357         os_swprintf(szError, bsiz, _W("Not yet implemented in Scilab.\n").c_str());
358         CoverageInstance::stopChrono((void*)&e);
359         throw InternalError(szError, 999, e.getLocation());
360     }
361
362     SimpleVar * psvRightMember = static_cast<SimpleVar *>(const_cast<Exp *>(e.getTail()));
363     std::wstring wstField = psvRightMember->getSymbol().getName();
364     types::InternalType * pValue = getResult();
365     types::InternalType * pReturn = NULL;
366     bool ok = false;
367
368     try
369     {
370         if (pValue->isGenericType() || pValue->isUserType())
371         {
372             ok = pValue->getAs<types::GenericType>()->extract(wstField, pReturn);
373         }
374     }
375     catch (std::wstring & err)
376     {
377         CoverageInstance::stopChrono((void*)&e);
378         throw InternalError(err.c_str(), 999, e.getTail()->getLocation());
379     }
380
381     if (ok)
382     {
383         if (pReturn == NULL)
384         {
385             std::wostringstream os;
386             os << _W("Invalid index.\n");
387             CoverageInstance::stopChrono((void*)&e);
388             throw InternalError(os.str(), 999, e.getLocation());
389         }
390
391         setResult(pReturn);
392         if (pValue->isDeletable())
393         {
394             if (pValue->isContainer())
395             {
396                 // prevent delete of pReturn in case where
397                 // extract not return a clone
398                 pReturn->IncreaseRef();
399                 pValue->killMe();
400                 pReturn->DecreaseRef();
401             }
402             else
403             {
404                 pValue->killMe();
405             }
406         }
407     }
408     else if (pValue->isFieldExtractionOverloadable())
409     {
410         types::typed_list in;
411         types::typed_list out;
412
413         types::String* pS = new types::String(wstField.c_str());
414
415         //TODO: in the case where overload is a macro there is no need to incref in
416         // because args will be put in context, removed and killed if required.
417         // But if the overload is a function... it is another story...
418
419         pS->IncreaseRef();
420         pValue->IncreaseRef();
421
422         in.push_back(pS);
423         in.push_back(pValue);
424         types::Callable::ReturnValue Ret = types::Callable::Error;
425         std::wstring stType = pValue->getShortTypeStr();
426
427         try
428         {
429             Ret = Overload::call(L"%" + stType + L"_e", in, 1, out, true);
430         }
431         catch (const InternalError& ie)
432         {
433             try
434             {
435                 //to compatibility with scilab 5 code.
436                 //tlist/mlist name are truncated to 8 first character
437                 if (stType.size() > 8)
438                 {
439                     Ret = Overload::call(L"%" + stType.substr(0, 8) + L"_e", in, 1, out, true);
440                 }
441                 else
442                 {
443                     CoverageInstance::stopChrono((void*)&e);
444                     throw ie;
445                 }
446             }
447             catch (const InternalError& ie)
448             {
449                 // TList or Mlist
450                 if (pValue->isList())
451                 {
452                     Ret = Overload::call(L"%l_e", in, 1, out, true);
453                 }
454                 else
455                 {
456                     CoverageInstance::stopChrono((void*)&e);
457                     throw ie;
458                 }
459             }
460         }
461
462         if (Ret != types::Callable::OK)
463         {
464             cleanInOut(in, out);
465             setResult(NULL);
466             CoverageInstance::stopChrono((void*)&e);
467             throw InternalError(ConfigVariable::getLastErrorMessage(), ConfigVariable::getLastErrorNumber(), e.getLocation());
468         }
469
470         setResult(out);
471         cleanIn(in, out);
472     }
473     else
474     {
475         pValue->killMe();
476         wchar_t szError[bsiz];
477         os_swprintf(szError, bsiz, _W("Attempt to reference field of non-structure array.\n").c_str());
478         CoverageInstance::stopChrono((void*)&e);
479         throw InternalError(szError, 999, e.getLocation());
480     }
481
482     CoverageInstance::stopChrono((void*)&e);
483 }
484
485 template <class T>
486 void RunVisitorT<T>::visitprivate(const IfExp  &e)
487 {
488     CoverageInstance::invokeAndStartChrono((void*)&e);
489
490     //Create local exec visitor
491     ShortCutVisitor SCTest;
492     bool bTestStatus = false;
493
494     //condition
495     try
496     {
497         e.getTest().accept(SCTest);
498         e.getTest().accept(*this);
499     }
500     catch (ScilabException &)
501     {
502         CoverageInstance::stopChrono((void*)&e);
503         throw;
504     }
505
506     bTestStatus = getResult()->isTrue();
507     clearResult();
508     try
509     {
510         if (bTestStatus == true)
511         {
512             e.getThen().accept(*this);
513         }
514         else if (e.hasElse())
515         {
516             e.getElse().accept(*this);
517         }
518     }
519     catch (ScilabException &)
520     {
521         CoverageInstance::stopChrono((void*)&e);
522         throw;
523     }
524
525     bool elseIsBreak = e.hasElse() && (&e.getElse())->isBreak();
526     if (e.isBreakable() && (elseIsBreak || (&e.getThen())->isBreak()))
527     {
528         const_cast<IfExp*>(&e)->setBreak();
529         const_cast<Exp*>(&e.getThen())->resetBreak();
530         if (e.hasElse())
531         {
532             const_cast<Exp*>(&e.getElse())->resetBreak();
533         }
534     }
535
536     bool elseIsContinue = e.hasElse() && (&e.getElse())->isContinue();
537     if (e.isContinuable() && (elseIsContinue || (&e.getThen())->isContinue()))
538     {
539         const_cast<IfExp*>(&e)->setContinue();
540         const_cast<Exp*>(&e.getThen())->resetContinue();
541         if (e.hasElse())
542         {
543             const_cast<Exp*>(&e.getElse())->resetContinue();
544         }
545     }
546
547     bool elseIsReturn = e.hasElse() && (&e.getElse())->isReturn();
548     if (e.isReturnable() && (elseIsReturn || (&e.getThen())->isReturn()))
549     {
550         const_cast<IfExp*>(&e)->setReturn();
551         const_cast<Exp*>(&e.getThen())->resetReturn();
552         if (e.hasElse())
553         {
554             const_cast<Exp*>(&e.getElse())->resetReturn();
555         }
556     }
557
558     CoverageInstance::stopChrono((void*)&e);
559 }
560
561 template <class T>
562 void RunVisitorT<T>::visitprivate(const WhileExp  &e)
563 {
564     CoverageInstance::invokeAndStartChrono((void*)&e);
565
566     //Create local exec visitor
567     ShortCutVisitor SCTest;
568
569     try
570     {
571         //manage & and | like && and ||
572         e.getTest().accept(SCTest);
573         //condition
574         e.getTest().accept(*this);
575     }
576     catch (ScilabException &)
577     {
578         CoverageInstance::stopChrono((void*)&e);
579         throw;
580     }
581
582     types::InternalType* pIT = getResult();
583
584     while (pIT->isTrue())
585     {
586         pIT->killMe();
587         setResult(NULL);
588
589         try
590         {
591             e.getBody().accept(*this);
592         }
593         catch (ScilabException &)
594         {
595             CoverageInstance::stopChrono((void*)&e);
596             throw;
597         }
598
599         //clear old result value before evaluate new one
600         if (getResult() != NULL)
601         {
602             getResult()->killMe();
603         }
604
605         if (e.getBody().isBreak())
606         {
607             const_cast<Exp*>(&(e.getBody()))->resetBreak();
608             break;
609         }
610
611         if (e.getBody().isReturn())
612         {
613             const_cast<WhileExp*>(&e)->setReturn();
614             const_cast<Exp*>(&(e.getBody()))->resetReturn();
615             break;
616         }
617
618         if (e.getBody().isContinue())
619         {
620             const_cast<Exp*>(&(e.getBody()))->resetContinue();
621         }
622
623         try
624         {
625             e.getTest().accept(*this);
626         }
627         catch (ScilabException &)
628         {
629             CoverageInstance::stopChrono((void*)&e);
630             throw;
631         }
632         pIT = getResult();
633     }
634
635     //pIT->killMe();
636     //clear result of condition or result of body
637     clearResult();
638     CoverageInstance::stopChrono((void*)&e);
639 }
640
641 template <class T>
642 void RunVisitorT<T>::visitprivate(const ForExp  &e)
643 {
644     CoverageInstance::invokeAndStartChrono((void*)&e);
645     symbol::Context* ctx = symbol::Context::getInstance();
646     //vardec visit increase its result reference
647     try
648     {
649         e.getVardec().accept(*this);
650     }
651     catch (ScilabException &)
652     {
653         CoverageInstance::stopChrono((void*)&e);
654         throw;
655     }
656     types::InternalType* pIT = getResult();
657
658     if (pIT->isImplicitList())
659     {
660         //get IL
661         types::ImplicitList* pVar = pIT->getAs<types::ImplicitList>();
662         //get IL initial Type
663         types::InternalType * pIL = pVar->getInitalType();
664         //std::cout << "for IL: " << pIL << std::endl;
665         //std::cout << "  for IV: " << pIT << std::endl;
666         //get index stack
667         symbol::Variable* var = e.getVardec().getAs<VarDec>()->getStack();
668
669         if (ctx->isprotected(var))
670         {
671             std::wostringstream os;
672             os << _W("Redefining permanent variable.\n");
673             CoverageInstance::stopChrono((void*)&e);
674             throw ast::InternalError(os.str(), 999, e.getVardec().getLocation());
675         }
676
677         ctx->put(var, pIL);
678         //use ref count to lock var against clear and detect any changes
679         pIL->IncreaseRef();
680
681         int size = static_cast<int>(pVar->getSize());
682         for (int i = 0; i < size; ++i)
683         {
684             //check if loop index has changed, deleted, copy ...
685             if (pIL->getRef() != 2)
686             {
687                 switch (pIL->getRef())
688                 {
689                     case 1:
690                         //someone clear me
691                         ctx->put(var, pIL);
692                         break;
693                     default:
694                         //someone assign me to another var
695                         //a = i;
696                         //unlock me
697                         pIL->DecreaseRef();
698
699                         //no need to destroy, it already assign to another var
700                         //pIL->killMe();
701
702                         //create a new me
703                         pIL = pVar->getInitalType();
704                         //lock loop index
705                         pIL->IncreaseRef();
706                         //update me ( must decrease ref of a )
707                         if (ctx->isprotected(var))
708                         {
709                             std::wostringstream os;
710                             os << _W("Redefining permanent variable.\n");
711                             CoverageInstance::stopChrono((void*)&e);
712                             throw ast::InternalError(os.str(), 999, e.getVardec().getLocation());
713                         }
714
715                         ctx->put(var, pIL);
716                         break;
717                 }
718             }
719
720             pVar->extractValue(i, pIL);
721
722             try
723             {
724                 e.getBody().accept(*this);
725             }
726             catch (const InternalError& ie)
727             {
728                 //unlock loop index and implicit list
729                 pIL->DecreaseRef();
730                 pIL->killMe();
731                 pIT->DecreaseRef();
732                 pIT->killMe();
733
734                 setResult(NULL);
735                 CoverageInstance::stopChrono((void*)&e);
736                 throw ie;
737             }
738
739             if (e.getBody().isBreak())
740             {
741                 const_cast<Exp&>(e.getBody()).resetBreak();
742                 break;
743             }
744
745             if (e.getBody().isContinue())
746             {
747                 const_cast<Exp&>(e.getBody()).resetContinue();
748                 continue;
749             }
750
751             if (e.getBody().isReturn())
752             {
753                 const_cast<ForExp&>(e).setReturn();
754                 const_cast<Exp&>(e.getBody()).resetReturn();
755                 break;
756             }
757         }
758
759         //unlock loop index
760         pIL->DecreaseRef();
761         pIL->killMe();
762     }
763     else if (pIT->isList())
764     {
765         types::List* pL = pIT->getAs<types::List>();
766         const int size = pL->getSize();
767         symbol::Variable* var = e.getVardec().getAs<VarDec>()->getStack();
768         for (int i = 0; i < size; ++i)
769         {
770             types::InternalType* pNew = pL->get(i);
771
772             if (ctx->isprotected(var))
773             {
774                 std::wostringstream os;
775                 os << _W("Redefining permanent variable.\n");
776                 CoverageInstance::stopChrono((void*)&e);
777                 throw ast::InternalError(os.str(), 999, e.getVardec().getLocation());
778             }
779             ctx->put(var, pNew);
780
781             try
782             {
783                 e.getBody().accept(*this);
784             }
785             catch (const InternalError& ie)
786             {
787                 //implicit list
788                 pIT->DecreaseRef();
789                 pIT->killMe();
790                 setResult(NULL);
791                 CoverageInstance::stopChrono((void*)&e);
792                 throw ie;
793             }
794
795             if (e.getBody().isBreak())
796             {
797                 const_cast<Exp*>(&(e.getBody()))->resetBreak();
798                 break;
799             }
800
801             if (e.getBody().isContinue())
802             {
803                 const_cast<Exp*>(&(e.getBody()))->resetContinue();
804                 continue;
805             }
806
807             if (e.getBody().isReturn())
808             {
809                 const_cast<ForExp*>(&e)->setReturn();
810                 const_cast<Exp&>(e.getBody()).resetReturn();
811                 break;
812             }
813         }
814     }
815     else if (pIT->isGenericType())
816     {
817         //Matrix i = [1,3,2,6] or other type
818         types::GenericType* pVar = pIT->getAs<types::GenericType>();
819         if (pVar->getDims() > 2)
820         {
821             pIT->DecreaseRef();
822             pIT->killMe();
823             CoverageInstance::stopChrono((void*)&e);
824             throw InternalError(_W("for expression can only manage 1 or 2 dimensions variables\n"), 999, e.getVardec().getLocation());
825         }
826
827         symbol::Variable* var = e.getVardec().getAs<VarDec>()->getStack();
828         for (int i = 0; i < pVar->getCols(); i++)
829         {
830             types::GenericType* pNew = pVar->getColumnValues(i);
831             if (pNew == NULL)
832             {
833                 pIT->DecreaseRef();
834                 pIT->killMe();
835                 CoverageInstance::stopChrono((void*)&e);
836                 throw InternalError(_W("for expression : Wrong type for loop iterator.\n"), 999, e.getVardec().getLocation());
837             }
838
839             if (ctx->isprotected(var))
840             {
841                 std::wostringstream os;
842                 os << _W("Redefining permanent variable.\n");
843                 CoverageInstance::stopChrono((void*)&e);
844                 throw InternalError(os.str(), 999, e.getVardec().getLocation());
845             }
846             ctx->put(var, pNew);
847
848             try
849             {
850                 e.getBody().accept(*this);
851             }
852             catch (const InternalError& ie)
853             {
854                 //implicit list
855                 pIT->DecreaseRef();
856                 pIT->killMe();
857                 setResult(NULL);
858                 CoverageInstance::stopChrono((void*)&e);
859                 throw ie;
860             }
861
862             if (e.getBody().isBreak())
863             {
864                 const_cast<Exp*>(&(e.getBody()))->resetBreak();
865                 break;
866             }
867
868             if (e.getBody().isContinue())
869             {
870                 const_cast<Exp*>(&(e.getBody()))->resetContinue();
871                 continue;
872             }
873
874             if (e.getBody().isReturn())
875             {
876                 const_cast<ForExp*>(&e)->setReturn();
877                 const_cast<Exp&>(e.getBody()).resetReturn();
878                 break;
879             }
880         }
881     }
882     else
883     {
884         pIT->DecreaseRef();
885         pIT->killMe();
886         CoverageInstance::stopChrono((void*)&e);
887         throw InternalError(_W("for expression : Wrong type for loop iterator.\n"), 999, e.getVardec().getLocation());
888     }
889
890     pIT->DecreaseRef();
891     pIT->killMe();
892
893     setResult(NULL);
894     CoverageInstance::stopChrono((void*)&e);
895 }
896
897 template <class T>
898 void RunVisitorT<T>::visitprivate(const ReturnExp &e)
899 {
900     CoverageInstance::invokeAndStartChrono((void*)&e);
901     if (e.isGlobal())
902     {
903         if (ConfigVariable::getPauseLevel() != 0 && symbol::Context::getInstance()->getScopeLevel() == ConfigVariable::getActivePauseLevel())
904         {
905             if (ConfigVariable::getEnableDebug() == true)
906             {
907                 sciprint(_("%s: function is disabled in debug mode.\n"), "resume");
908                 CoverageInstance::stopChrono((void*)&e);
909                 return;
910             }
911
912             //return or resume
913             ConfigVariable::DecreasePauseLevel();
914             ConfigVariable::macroFirstLine_end();
915             CoverageInstance::stopChrono((void*)&e);
916             return;
917         }
918         else
919         {
920             const_cast<ReturnExp*>(&e)->setReturn();
921         }
922     }
923     else
924     {
925         //return(x)
926
927         if (e.getParent() == nullptr || e.getParent()->isAssignExp() == false)
928         {
929             CoverageInstance::stopChrono((void*)&e);
930             throw InternalError(_W("With input arguments, return / resume expects output arguments.\n"), 999, e.getLocation());
931         }
932         //in case of CallExp, we can return only one value
933         int iSaveExpectedSize = getExpectedSize();
934         setExpectedSize(1);
935         try
936         {
937             e.getExp().accept(*this);
938         }
939         catch (ScilabException &)
940         {
941             CoverageInstance::stopChrono((void*)&e);
942             throw;
943         }
944         setExpectedSize(iSaveExpectedSize);
945         const_cast<ReturnExp*>(&e)->setReturn();
946     }
947
948     CoverageInstance::stopChrono((void*)&e);
949 }
950
951 template <class T>
952 void RunVisitorT<T>::visitprivate(const IntSelectExp &e)
953 {
954     CoverageInstance::invokeAndStartChrono((void*)&e);
955     bool found = false;
956     //e.getSelect()->accept(*this);
957     //InternalType* pIT = getResult();
958     //setResult(nullptr);
959     //if (pIT && pIT->isDouble())
960     //{
961     //    Double * pDbl = static_cast<Double *>(pIT);
962     //    if (!pDbl->isComplex() && pDbl->getSize() == 1)
963     //    {
964     //        int64_t val;
965     //        if (analysis::tools::asInteger<int64_t>(pDbl->get(0), val))
966     //        {
967     //            Exp * exp = e.getExp(val);
968     //            found = true;
969     //            if (exp)
970     //            {
971     //                Exp * body = exp->isCaseExp() ? exp->getAs<CaseExp>()->getBody() : exp;
972     //                if (e.isBreakable())
973     //                {
974     //                    const_cast<IntSelectExp*>(&e)->resetBreak();
975     //                    body->setBreakable();
976     //                }
977
978     //                if (e.isContinuable())
979     //                {
980     //                    const_cast<IntSelectExp*>(&e)->resetContinue();
981     //                    body->setContinuable();
982     //                }
983
984     //                if (e.isReturnable())
985     //                {
986     //                    const_cast<IntSelectExp*>(&e)->resetReturn();
987     //                    body->setReturnable();
988     //                }
989
990     //                try
991     //                {
992     //                    //the good one
993     //                    body->accept(*this);
994     //                }
995     //                catch (const InternalError& ie)
996     //                {
997     //                    pIT->killMe();
998     //                    throw ie;
999     //                }
1000
1001     //                if (e.isBreakable() && body->isBreak())
1002     //                {
1003     //                    const_cast<IntSelectExp*>(&e)->setBreak();
1004     //                    body->resetBreak();
1005     //                }
1006
1007     //                if (e.isContinuable() && body->isContinue())
1008     //                {
1009     //                    const_cast<IntSelectExp*>(&e)->setContinue();
1010     //                    body->resetContinue();
1011     //                }
1012
1013     //                if (e.isReturnable() && body->isReturn())
1014     //                {
1015     //                    const_cast<IntSelectExp*>(&e)->setReturn();
1016     //                    body->resetReturn();
1017     //                }
1018     //            }
1019     //        }
1020     //    }
1021     //}
1022
1023     if (!found)
1024     {
1025         try
1026         {
1027             e.getOriginal()->accept(*this);
1028         }
1029         catch (ScilabException &)
1030         {
1031             CoverageInstance::stopChrono((void*)&e);
1032             throw;
1033         }
1034     }
1035     CoverageInstance::stopChrono((void*)&e);
1036 }
1037
1038 template <class T>
1039 void RunVisitorT<T>::visitprivate(const StringSelectExp &e)
1040 {
1041     CoverageInstance::invokeAndStartChrono((void*)&e);
1042     try
1043     {
1044         e.getSelect()->accept(*this);
1045     }
1046     catch (ScilabException &)
1047     {
1048         CoverageInstance::stopChrono((void*)&e);
1049         throw;
1050     }
1051     types::InternalType* pIT = getResult();
1052     setResult(nullptr);
1053     bool found = false;
1054     if (pIT && pIT->isString())
1055     {
1056         types::String * pStr = static_cast<types::String *>(pIT);
1057         if (pStr->getSize() == 1)
1058         {
1059             if (wchar_t * s = pStr->get(0))
1060             {
1061                 const std::wstring ws(s);
1062                 Exp * exp = e.getExp(ws);
1063                 found = true;
1064                 if (exp)
1065                 {
1066                     Exp * body = exp->isCaseExp() ? exp->getAs<CaseExp>()->getBody() : exp;
1067                     if (e.isBreakable())
1068                     {
1069                         const_cast<StringSelectExp*>(&e)->resetBreak();
1070                         body->setBreakable();
1071                     }
1072
1073                     if (e.isContinuable())
1074                     {
1075                         const_cast<StringSelectExp*>(&e)->resetContinue();
1076                         body->setContinuable();
1077                     }
1078
1079                     if (e.isReturnable())
1080                     {
1081                         const_cast<StringSelectExp*>(&e)->resetReturn();
1082                         body->setReturnable();
1083                     }
1084
1085                     try
1086                     {
1087                         //the good one
1088                         body->accept(*this);
1089                     }
1090                     catch (const InternalError& ie)
1091                     {
1092                         pIT->killMe();
1093                         CoverageInstance::stopChrono((void*)&e);
1094                         throw ie;
1095                     }
1096
1097                     if (e.isBreakable() && body->isBreak())
1098                     {
1099                         const_cast<StringSelectExp*>(&e)->setBreak();
1100                         body->resetBreak();
1101                     }
1102
1103                     if (e.isContinuable() && body->isContinue())
1104                     {
1105                         const_cast<StringSelectExp*>(&e)->setContinue();
1106                         body->resetContinue();
1107                     }
1108
1109                     if (e.isReturnable() && body->isReturn())
1110                     {
1111                         const_cast<StringSelectExp*>(&e)->setReturn();
1112                         body->resetReturn();
1113                     }
1114                 }
1115             }
1116         }
1117     }
1118
1119     if (!found)
1120     {
1121         try
1122         {
1123             e.getOriginal()->accept(*this);
1124         }
1125         catch (ScilabException &)
1126         {
1127             CoverageInstance::stopChrono((void*)&e);
1128             throw;
1129         }
1130     }
1131     CoverageInstance::stopChrono((void*)&e);
1132 }
1133
1134 template <class T>
1135 void RunVisitorT<T>::visitprivate(const SelectExp &e)
1136 {
1137     // FIXME : exec select ... case ... else ... end
1138     CoverageInstance::invokeAndStartChrono((void*)&e);
1139     try
1140     {
1141         e.getSelect()->accept(*this);
1142     }
1143     catch (ScilabException &)
1144     {
1145         CoverageInstance::stopChrono((void*)&e);
1146         throw;
1147     }
1148
1149     bool bCase = false;
1150
1151     types::InternalType* pIT = getResult();
1152     setResult(NULL);
1153     if (pIT)
1154     {
1155         // protect pIT to avoid double free when
1156         // the variable in select is override in the case
1157         pIT->IncreaseRef();
1158
1159         //find good case
1160         exps_t cases = e.getCases();
1161         for (auto exp : cases)
1162         {
1163             CaseExp * pCase = exp->getAs<CaseExp>();
1164             try
1165             {
1166                 pCase->getTest()->accept(*this);
1167             }
1168             catch (ScilabException &)
1169             {
1170                 CoverageInstance::stopChrono((void*)&e);
1171                 throw;
1172             }
1173             types::InternalType *pITCase = getResult();
1174             setResult(NULL);
1175             if (pITCase)
1176             {
1177                 if (pITCase->isContainer()) //WARNING ONLY FOR CELL
1178                 {
1179                     //check each item
1180                 }
1181                 else if (*pITCase == *pIT)
1182                 {
1183                     try
1184                     {
1185                         //the good one
1186                         pCase->getBody()->accept(*this);
1187                     }
1188                     catch (const InternalError& ie)
1189                     {
1190                         pIT->DecreaseRef();
1191                         pIT->killMe();
1192                         CoverageInstance::stopChrono((void*)&e);
1193                         throw ie;
1194                     }
1195
1196                     if (e.isBreakable() && pCase->getBody()->isBreak())
1197                     {
1198                         const_cast<SelectExp*>(&e)->setBreak();
1199                         pCase->getBody()->resetBreak();
1200                     }
1201
1202                     if (e.isContinuable() && pCase->getBody()->isContinue())
1203                     {
1204                         const_cast<SelectExp*>(&e)->setContinue();
1205                         pCase->getBody()->resetContinue();
1206                     }
1207
1208                     if (e.isReturnable() && pCase->getBody()->isReturn())
1209                     {
1210                         const_cast<SelectExp*>(&e)->setReturn();
1211                         pCase->getBody()->resetReturn();
1212                     }
1213
1214                     pITCase->killMe();
1215                     bCase = true;
1216                     break;
1217                 }
1218
1219                 pITCase->killMe();
1220             }
1221         }
1222     }
1223
1224     if (bCase == false && e.getDefaultCase() != NULL)
1225     {
1226         try
1227         {
1228             //default case
1229             e.getDefaultCase()->accept(*this);
1230         }
1231         catch (const InternalError& ie)
1232         {
1233             if (pIT)
1234             {
1235                 pIT->DecreaseRef();
1236                 pIT->killMe();
1237             }
1238             CoverageInstance::stopChrono((void*)&e);
1239             throw ie;
1240         }
1241
1242         if (e.isBreakable() && e.getDefaultCase()->isBreak())
1243         {
1244             const_cast<SelectExp*>(&e)->setBreak();
1245             e.getDefaultCase()->resetBreak();
1246         }
1247
1248         if (e.isContinuable() && e.getDefaultCase()->isContinue())
1249         {
1250             const_cast<SelectExp*>(&e)->setContinue();
1251             e.getDefaultCase()->resetContinue();
1252         }
1253
1254         if (e.isReturnable() && e.getDefaultCase()->isReturn())
1255         {
1256             const_cast<SelectExp*>(&e)->setReturn();
1257             e.getDefaultCase()->resetReturn();
1258         }
1259     }
1260
1261     clearResult();
1262
1263     if (pIT)
1264     {
1265         pIT->DecreaseRef();
1266         pIT->killMe();
1267     }
1268     CoverageInstance::stopChrono((void*)&e);
1269 }
1270
1271 template <class T>
1272 void RunVisitorT<T>::visitprivate(const NotExp &e)
1273 {
1274     CoverageInstance::invokeAndStartChrono((void*)&e);
1275     /*
1276       @ or ~ !
1277       */
1278     try
1279     {
1280         e.getExp().accept(*this);
1281     }
1282     catch (ScilabException &)
1283     {
1284         CoverageInstance::stopChrono((void*)&e);
1285         throw;
1286     }
1287
1288     types::InternalType * pValue = getResult();
1289     types::InternalType * pReturn = NULL;
1290     if (pValue->neg(pReturn))
1291     {
1292         if (pValue != pReturn)
1293         {
1294             pValue->killMe();
1295         }
1296
1297         setResult(pReturn);
1298     }
1299     else
1300     {
1301         // neg returned false so the negation is not possible so we call the overload (%foo_5)
1302         types::typed_list in;
1303         types::typed_list out;
1304
1305         pValue->IncreaseRef();
1306         in.push_back(pValue);
1307
1308         types::Callable::ReturnValue Ret = Overload::call(L"%" + pValue->getShortTypeStr() + L"_5", in, 1, out, true);
1309
1310         if (Ret != types::Callable::OK)
1311         {
1312             cleanInOut(in, out);
1313             CoverageInstance::stopChrono((void*)&e);
1314             throw InternalError(ConfigVariable::getLastErrorMessage(), ConfigVariable::getLastErrorNumber(), e.getLocation());
1315         }
1316
1317         setResult(out);
1318         cleanIn(in, out);
1319     }
1320     CoverageInstance::stopChrono((void*)&e);
1321 }
1322
1323 template <class T>
1324 void RunVisitorT<T>::visitprivate(const TransposeExp &e)
1325 {
1326     CoverageInstance::invokeAndStartChrono((void*)&e);
1327     try
1328     {
1329         e.getExp().accept(*this);
1330     }
1331     catch (ScilabException &)
1332     {
1333         CoverageInstance::stopChrono((void*)&e);
1334         throw;
1335     }
1336
1337     if (getResultSize() != 1)
1338     {
1339         clearResult();
1340         wchar_t szError[bsiz];
1341         os_swprintf(szError, bsiz, _W("%ls: Can not transpose multiple elements.\n").c_str(), L"Transpose");
1342         CoverageInstance::stopChrono((void*)&e);
1343         throw InternalError(szError, 999, e.getLocation());
1344     }
1345
1346     types::InternalType * pValue = getResult();
1347     types::InternalType * pReturn = NULL;
1348     const bool bConjug = e.getConjugate() == TransposeExp::_Conjugate_;
1349
1350     if ((bConjug && pValue->adjoint(pReturn)) || (!bConjug && pValue->transpose(pReturn)))
1351     {
1352         if (pValue != pReturn)
1353         {
1354             pValue->killMe();
1355         }
1356
1357         setResult(pReturn);
1358         CoverageInstance::stopChrono((void*)&e);
1359
1360         return;
1361     }
1362     else
1363     {
1364         // transpose returned false so the negation is not possible so we call the overload (%foo_t or %foo_0)
1365         types::typed_list in;
1366         types::typed_list out;
1367
1368         pValue->IncreaseRef();
1369         in.push_back(pValue);
1370
1371         types::Callable::ReturnValue Ret;
1372         if (bConjug)
1373         {
1374             Ret = Overload::call(L"%" + getResult()->getShortTypeStr() + L"_t", in, 1, out, true);
1375         }
1376         else
1377         {
1378             Ret = Overload::call(L"%" + getResult()->getShortTypeStr() + L"_0", in, 1, out, true);
1379         }
1380
1381         if (Ret != types::Callable::OK)
1382         {
1383             cleanInOut(in, out);
1384             CoverageInstance::stopChrono((void*)&e);
1385             throw InternalError(ConfigVariable::getLastErrorMessage(), ConfigVariable::getLastErrorNumber(), e.getLocation());
1386         }
1387
1388         setResult(out);
1389         cleanIn(in, out);
1390     }
1391
1392     CoverageInstance::stopChrono((void*)&e);
1393 }
1394
1395 template <class T>
1396 void RunVisitorT<T>::visitprivate(const FunctionDec & e)
1397 {
1398     CoverageInstance::invokeAndStartChrono((void*)&e);
1399     symbol::Context* ctx = symbol::Context::getInstance();
1400     /*
1401       function foo
1402       endfunction
1403       */
1404
1405     // funcprot(0) : do nothing
1406     // funcprot(1) && warning(on) : warning
1407     //get input parameters list
1408     std::list<symbol::Variable*> *pVarList = new std::list<symbol::Variable*>();
1409     const exps_t & vars = e.getArgs().getVars();
1410     for (const auto var : vars)
1411     {
1412         pVarList->push_back(var->getAs<SimpleVar>()->getStack());
1413     }
1414
1415     //get output parameters list
1416     std::list<symbol::Variable*> *pRetList = new std::list<symbol::Variable*>();
1417     const exps_t & rets = e.getReturns().getVars();
1418     for (const auto ret : rets)
1419     {
1420         pRetList->push_back(ret->getAs<SimpleVar>()->getStack());
1421     }
1422
1423     types::Macro *pMacro = new types::Macro(e.getSymbol().getName(), *pVarList, *pRetList,
1424                                             const_cast<SeqExp&>(static_cast<const SeqExp&>(e.getBody())), L"script");
1425     pMacro->setLines(e.getLocation().first_line, e.getLocation().last_line);
1426
1427     if (ctx->isprotected(symbol::Symbol(pMacro->getName())))
1428     {
1429         delete pMacro;
1430         std::wostringstream os;
1431         os << _W("Redefining permanent variable.\n");
1432         CoverageInstance::stopChrono((void*)&e);
1433         throw InternalError(os.str(), 999, e.getLocation());
1434     }
1435
1436     if (ctx->addMacro(pMacro) == false)
1437     {
1438         char pstError[1024];
1439         char* pstFuncName = wide_string_to_UTF8(e.getSymbol().getName().c_str());
1440         os_sprintf(pstError, _("It is not possible to redefine the %s primitive this way (see clearfun).\n"), pstFuncName);
1441         wchar_t* pwstError = to_wide_string(pstError);
1442         std::wstring wstError(pwstError);
1443         FREE(pstFuncName);
1444         FREE(pwstError);
1445         pMacro->killMe();
1446         CoverageInstance::stopChrono((void*)&e);
1447         throw InternalError(wstError, 999, e.getLocation());
1448     }
1449
1450     CoverageInstance::stopChrono((void*)&e);
1451 }
1452
1453 template <class T>
1454 void RunVisitorT<T>::visitprivate(const ListExp &e)
1455 {
1456     CoverageInstance::invokeAndStartChrono((void*)&e);
1457     try
1458     {
1459         e.getStart().accept(*this);
1460     }
1461     catch (ScilabException &)
1462     {
1463         CoverageInstance::stopChrono((void*)&e);
1464         throw;
1465     }
1466
1467     types::InternalType* pITStart = getResult();
1468     types::GenericType* pStart = static_cast<types::GenericType*>(pITStart);
1469     if (pITStart == NULL ||
1470             ((pITStart->isGenericType() == false || pStart->getSize() != 1 || (pStart->isDouble() && pStart->getAs<types::Double>()->isComplex())) &&
1471              pStart->isList() == false)) // list case => call overload
1472     {
1473         setResult(NULL);
1474         wchar_t szError[bsiz];
1475         if (pITStart && pITStart->isImplicitList())
1476         {
1477             os_swprintf(szError, bsiz, _W("%ls: Too many %ls or wrong type for argument %d: Real scalar expected.\n").c_str(), L"':'", L"':'", 1);
1478         }
1479         else
1480         {
1481             os_swprintf(szError, bsiz, _W("%ls: Wrong type for argument %d: Real scalar expected.\n").c_str(), L"':'", 1);
1482         }
1483
1484         if (pITStart)
1485         {
1486             pITStart->killMe();
1487         }
1488
1489         CoverageInstance::stopChrono((void*)&e);
1490         throw InternalError(szError, 999, e.getLocation());
1491     }
1492
1493     try
1494     {
1495         e.getStep().accept(*this);
1496     }
1497     catch (ScilabException &)
1498     {
1499         CoverageInstance::stopChrono((void*)&e);
1500         throw;
1501     }
1502
1503     types::InternalType* pITStep = getResult();
1504     types::GenericType* pStep = static_cast<types::GenericType*>(pITStep);
1505     setResult(NULL);
1506     if (pITStep == NULL ||
1507             ((pITStep->isGenericType() == false || pStep->getSize() != 1 || (pStep->isDouble() && pStep->getAs<types::Double>()->isComplex())) &&
1508              pStep->isList() == false)) // list case => call overload
1509     {
1510         pITStart->killMe();
1511         if (pITStep)
1512         {
1513             pITStep->killMe();
1514         }
1515
1516         setResult(NULL);
1517         wchar_t szError[bsiz];
1518         os_swprintf(szError, bsiz, _W("%ls: Wrong type for argument %d: Real scalar expected.\n").c_str(), L"':'", 2);
1519         CoverageInstance::stopChrono((void*)&e);
1520         throw InternalError(szError, 999, e.getLocation());
1521     }
1522
1523     try
1524     {
1525         e.getEnd().accept(*this);
1526     }
1527     catch (ScilabException &)
1528     {
1529         CoverageInstance::stopChrono((void*)&e);
1530         throw;
1531     }
1532
1533     types::InternalType* pITEnd = getResult();
1534     types::GenericType* pEnd = static_cast<types::GenericType*>(pITEnd);
1535     setResult(NULL);
1536     if (pITEnd == NULL ||
1537             ((pITEnd->isGenericType() == false || pEnd->getSize() != 1 || (pEnd->isDouble() && pEnd->getAs<types::Double>()->isComplex())) &&
1538              pEnd->isList() == false)) // list case => call overload
1539     {
1540         pITStart->killMe();
1541         pITStep->killMe();
1542         if (pITEnd)
1543         {
1544             pITEnd->killMe();
1545         }
1546
1547         setResult(NULL);
1548         wchar_t szError[bsiz];
1549         os_swprintf(szError, bsiz, _W("%ls: Wrong type for argument %d: Real scalar expected.\n").c_str(), L"':'", 2 + e.hasExplicitStep());
1550         CoverageInstance::stopChrono((void*)&e);
1551         throw InternalError(szError, 999, e.getLocation());
1552     }
1553
1554     ////check if implicitlist is 1:$ to replace by ':'
1555     //if (piStart->isDouble() && piStep->isDouble() && piEnd->isPoly())
1556     //{
1557     //    if (piStart->getAs<Double>()->get()[0] == 1 && piStep->getAs<Double>()->get()[0] == 1)
1558     //    {
1559     //        SinglePoly* end = piEnd->getAs<Polynom>()->get()[0];
1560     //        if (end->getRank() == 1 && end->get()[0] == 0 && end->get()[1] == 1)
1561     //        {
1562     //            setResult(new Colon());
1563     //            return;
1564     //        }
1565     //    }
1566     //}
1567
1568     //check compatibility
1569     // double : double : double or poly : poly : poly and mix like double : double : poly
1570     if ((pStart->isPoly() || pStart->isDouble()) &&
1571             (pStep->isPoly() || pStep->isDouble()) &&
1572             (pEnd->isPoly() || pEnd->isDouble()))
1573     {
1574         // No need to kill piStart, ... because Implicit list ctor will incref them
1575         setResult(new types::ImplicitList(pStart, pStep, pEnd));
1576         CoverageInstance::stopChrono((void*)&e);
1577         return;
1578     }
1579
1580     // int : double or int : int
1581     if (pStart->isInt() &&
1582             (pStep->isDouble() || pStep->isInt()) &&
1583             pEnd->isInt())
1584     {
1585         // check for same int type int8, int 16 ...
1586         if (pStart->getType() == pEnd->getType() &&
1587                 (pStart->getType() == pStep->getType() ||
1588                  pStep->isDouble()))
1589         {
1590             // No need to kill piStart, ... because Implicit list ctor will incref them
1591             setResult(new types::ImplicitList(pStart, pStep, pEnd));
1592             CoverageInstance::stopChrono((void*)&e);
1593             return;
1594         }
1595     }
1596
1597     // Call Overload
1598     types::Callable::ReturnValue Ret;
1599     types::typed_list in;
1600     types::typed_list out;
1601
1602     pStart->IncreaseRef();
1603     in.push_back(pStart);
1604
1605     try
1606     {
1607         if (e.hasExplicitStep())
1608         {
1609             // 1:2:4
1610             //call overload %typeStart_b_typeStep
1611             pStep->IncreaseRef();
1612             in.push_back(pStep);
1613             pEnd->IncreaseRef();
1614             in.push_back(pEnd);
1615             Ret = Overload::call(L"%" + pStart->getShortTypeStr() + L"_b_" + pStep->getShortTypeStr(), in, 1, out, true);
1616         }
1617         else
1618         {
1619             // 1:2
1620             //call overload %typeStart_b_typeEnd
1621             pStep->killMe();
1622             pEnd->IncreaseRef();
1623             in.push_back(pEnd);
1624             Ret = Overload::call(L"%" + pStart->getShortTypeStr() + L"_b_" + pEnd->getShortTypeStr(), in, 1, out, true);
1625         }
1626     }
1627     catch (const InternalError& error)
1628     {
1629         setResult(NULL);
1630         cleanInOut(in, out);
1631         CoverageInstance::stopChrono((void*)&e);
1632         throw error;
1633     }
1634
1635     if (Ret != types::Callable::OK)
1636     {
1637         setResult(NULL);
1638         cleanInOut(in, out);
1639         CoverageInstance::stopChrono((void*)&e);
1640         throw InternalError(ConfigVariable::getLastErrorMessage(), ConfigVariable::getLastErrorNumber(), e.getLocation());
1641     }
1642
1643     setResult(out);
1644     cleanIn(in, out);
1645     CoverageInstance::stopChrono((void*)&e);
1646 }
1647
1648 template <class T>
1649 void RunVisitorT<T>::visitprivate(const OptimizedExp &e)
1650 {
1651 }
1652
1653 template <class T>
1654 void RunVisitorT<T>::visitprivate(const MemfillExp &e)
1655 {
1656     CoverageInstance::invokeAndStartChrono((void*)&e);
1657     try
1658     {
1659         e.getOriginal()->accept(*this);
1660     }
1661     catch (ScilabException &)
1662     {
1663         CoverageInstance::stopChrono((void*)&e);
1664         throw;
1665     }
1666 }
1667
1668 template <class T>
1669 void RunVisitorT<T>::visitprivate(const DAXPYExp &e)
1670 {
1671     CoverageInstance::invokeAndStartChrono((void*)&e);
1672     types::InternalType* pIT = NULL;
1673     types::Double* ad = NULL;
1674     int ar = 0;
1675     int ac = 0;
1676
1677     types::Double* xd = NULL;
1678     int xr = 0;
1679     int xc = 0;
1680
1681     types::Double* yd = NULL;
1682     int yr = 0;
1683     int yc = 0;
1684
1685     //check types and dimensions
1686
1687     //y must be double
1688     const Exp &ye = e.getY();
1689     try
1690     {
1691         ye.accept(*this);
1692     }
1693     catch (ScilabException &)
1694     {
1695         CoverageInstance::stopChrono((void*)&e);
1696         throw;
1697     }
1698
1699     pIT = getResult();
1700     if (pIT->isDouble())
1701     {
1702         yd = pIT->getAs<types::Double>();
1703         if (yd->getDims() == 2 && yd->isComplex() == false)
1704         {
1705             yr = yd->getRows();
1706             yc = yd->getCols();
1707         }
1708         else
1709         {
1710             yd->killMe();
1711             try
1712             {
1713                 e.getOriginal()->accept(*this);
1714             }
1715             catch (ScilabException &)
1716             {
1717                 CoverageInstance::stopChrono((void*)&e);
1718                 throw;
1719             }
1720             CoverageInstance::stopChrono((void*)&e);
1721             return;
1722         }
1723     }
1724     else
1725     {
1726         pIT->killMe();
1727         try
1728         {
1729             e.getOriginal()->accept(*this);
1730         }
1731         catch (ScilabException &)
1732         {
1733             CoverageInstance::stopChrono((void*)&e);
1734             throw;
1735         }
1736         CoverageInstance::stopChrono((void*)&e);
1737         return;
1738     }
1739
1740     //x
1741     const Exp &xe = e.getX();
1742     try
1743     {
1744         xe.accept(*this);
1745     }
1746     catch (ScilabException &)
1747     {
1748         CoverageInstance::stopChrono((void*)&e);
1749         throw;
1750     }
1751     pIT = getResult();
1752
1753     if (pIT->isDouble())
1754     {
1755         xd = pIT->getAs<types::Double>();
1756         if (xd->isScalar() && xd->isComplex() == false)
1757         {
1758             // x become a
1759             ad = xd;
1760             ar = 1;
1761             ac = 1;
1762         }
1763         else if (xd->getDims() == 2 && xd->isComplex() == false)
1764         {
1765             xr = xd->getRows();
1766             xc = xd->getCols();
1767         }
1768         else
1769         {
1770             yd->killMe();
1771             xd->killMe();
1772             try
1773             {
1774                 e.getOriginal()->accept(*this);
1775             }
1776             catch (ScilabException &)
1777             {
1778                 CoverageInstance::stopChrono((void*)&e);
1779                 throw;
1780             }
1781             CoverageInstance::stopChrono((void*)&e);
1782             return;
1783         }
1784     }
1785     else
1786     {
1787         pIT->killMe();
1788         yd->killMe();
1789         try
1790         {
1791             e.getOriginal()->accept(*this);
1792         }
1793         catch (ScilabException &)
1794         {
1795             CoverageInstance::stopChrono((void*)&e);
1796             throw;
1797         }
1798         CoverageInstance::stopChrono((void*)&e);
1799         return;
1800     }
1801
1802     const Exp &ae = e.getA();
1803     try
1804     {
1805         ae.accept(*this);
1806     }
1807     catch (ScilabException &)
1808     {
1809         CoverageInstance::stopChrono((void*)&e);
1810         throw;
1811     }
1812     pIT = getResult();
1813
1814     if (pIT->isDouble())
1815     {
1816         if (ad)
1817         {
1818             xd = pIT->getAs<types::Double>();
1819             //X is scalar it become A
1820             //now use A as X
1821             if (xd->getDims() == 2 && xd->isComplex() == false)
1822             {
1823                 xr = xd->getRows();
1824                 xc = xd->getCols();
1825             }
1826             else
1827             {
1828                 yd->killMe();
1829                 xd->killMe();
1830                 ad->killMe();
1831                 try
1832                 {
1833                     e.getOriginal()->accept(*this);
1834                 }
1835                 catch (ScilabException &)
1836                 {
1837                     CoverageInstance::stopChrono((void*)&e);
1838                     throw;
1839                 }
1840                 CoverageInstance::stopChrono((void*)&e);
1841                 return;
1842             }
1843         }
1844         else
1845         {
1846             //a is a and it must be scalar
1847             ad = pIT->getAs<types::Double>();
1848             if (/*ad->isScalar() && */ad->isComplex() == false)
1849             {
1850                 ar = ad->getRows(); //1;
1851                 ac = ad->getCols();//1;
1852             }
1853             else
1854             {
1855                 yd->killMe();
1856                 xd->killMe();
1857                 ad->killMe();
1858                 try
1859                 {
1860                     e.getOriginal()->accept(*this);
1861                 }
1862                 catch (ScilabException &)
1863                 {
1864                     CoverageInstance::stopChrono((void*)&e);
1865                     throw;
1866                 }
1867                 throw;
1868                 return;
1869             }
1870         }
1871     }
1872     else
1873     {
1874         pIT->killMe();
1875         yd->killMe();
1876         xd->killMe();
1877         try
1878         {
1879             e.getOriginal()->accept(*this);
1880         }
1881         catch (ScilabException &)
1882         {
1883             CoverageInstance::stopChrono((void*)&e);
1884             throw;
1885         }
1886         CoverageInstance::stopChrono((void*)&e);
1887         return;
1888     }
1889
1890     // If we get here we are certain that ad, xd & yd have been set
1891     if (ac == 1 &&
1892             ar == 1 &&
1893             xr == yr &&
1894             xc == yc)
1895     {
1896         //go !
1897         int one = 1;
1898         int size = xc * xr;
1899         //Double* od = (Double*)yd->clone();
1900         C2F(daxpy)(&size, ad->get(), xd->get(), &one, yd->get(), &one);
1901         //setResult(od);
1902         //yd->killMe();
1903         xd->killMe();
1904         ad->killMe();
1905         CoverageInstance::stopChrono((void*)&e);
1906         return;
1907     }
1908     else if (ac == xr && ar == yr && xc == yc)
1909     {
1910         char n = 'n';
1911         double one = 1;
1912         C2F(dgemm)(&n, &n, &ar, &xc, &ac, &one, ad->get(), &ar, xd->get(), &ac, &one, yd->get(), &ar);
1913         xd->killMe();
1914         ad->killMe();
1915         CoverageInstance::stopChrono((void*)&e);
1916         return;
1917     }
1918
1919     yd->killMe();
1920     xd->killMe();
1921     ad->killMe();
1922
1923     try
1924     {
1925         e.getOriginal()->accept(*this);
1926     }
1927     catch (ScilabException &)
1928     {
1929         CoverageInstance::stopChrono((void*)&e);
1930         throw;
1931     }
1932     CoverageInstance::stopChrono((void*)&e);
1933
1934     return;
1935 }
1936
1937 template <class T>
1938 void RunVisitorT<T>::visitprivate(const TryCatchExp  &e)
1939 {
1940     CoverageInstance::invokeAndStartChrono((void*)&e);
1941     //save current prompt mode
1942     bool oldVal = ConfigVariable::isSilentError();
1943     int oldMode = ConfigVariable::getPromptMode();
1944     //set mode silent for errors
1945     ConfigVariable::setSilentError(true);
1946
1947     symbol::Context* pCtx = symbol::Context::getInstance();
1948     try
1949     {
1950         int scope = pCtx->getScopeLevel();
1951         int level = ConfigVariable::getRecursionLevel();
1952         try
1953         {
1954             const_cast<Exp*>(&e.getTry())->setReturnable();
1955             e.getTry().accept(*this);
1956             //restore previous prompt mode
1957             ConfigVariable::setSilentError(oldVal);
1958
1959             if (e.getTry().isReturn())
1960             {
1961                 const_cast<Exp*>(&e.getTry())->resetReturn();
1962                 const_cast<TryCatchExp*>(&e)->setReturn();
1963             }
1964         }
1965         catch (const RecursionException& /* re */)
1966         {
1967             ConfigVariable::setPromptMode(oldMode);
1968
1969             //close opened scope during try
1970             while (pCtx->getScopeLevel() > scope)
1971             {
1972                 pCtx->scope_end();
1973             }
1974
1975             //decrease recursion to init value and close where
1976             while (ConfigVariable::getRecursionLevel() > level)
1977             {
1978                 ConfigVariable::where_end();
1979                 ConfigVariable::decreaseRecursion();
1980             }
1981
1982             //print msg about recursion limit and trigger an error
1983             wchar_t sz[1024];
1984             os_swprintf(sz, 1024, _W("Recursion limit reached (%d).\n").data(), ConfigVariable::getRecursionLimit());
1985             CoverageInstance::stopChrono((void*)&e);
1986             throw ast::InternalError(sz);
1987         }
1988
1989     }
1990     catch (const InternalError& /* ie */)
1991     {
1992         //restore previous prompt mode
1993         ConfigVariable::setSilentError(oldVal);
1994         //to lock lasterror
1995         ConfigVariable::setLastErrorCall();
1996         // reset call stack filled when error occurred
1997         ConfigVariable::resetWhereError();
1998         try
1999         {
2000             const_cast<Exp*>(&e.getCatch())->setReturnable();
2001             e.getCatch().accept(*this);
2002             if (e.getCatch().isReturn())
2003             {
2004                 const_cast<Exp*>(&e.getCatch())->resetReturn();
2005                 const_cast<TryCatchExp*>(&e)->setReturn();
2006             }
2007         }
2008         catch (ScilabException &)
2009         {
2010             CoverageInstance::stopChrono((void*)&e);
2011             throw;
2012         }
2013     }
2014     CoverageInstance::stopChrono((void*)&e);
2015 }
2016
2017
2018 } /* namespace ast */
2019
2020 #include "run_SeqExp.hpp"
2021 #include "run_CallExp.hpp"
2022 #include "run_MatrixExp.hpp"
2023 #include "run_OpExp.hpp"
2024 #include "run_AssignExp.hpp"
2025
2026 template EXTERN_AST class ast::RunVisitorT<ast::ExecVisitor>;
2027 template EXTERN_AST class ast::RunVisitorT<ast::StepVisitor>;
2028 template EXTERN_AST class ast::RunVisitorT<ast::TimedVisitor>;
2029 template EXTERN_AST class ast::RunVisitorT<ast::DebuggerVisitor>;