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