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