* Bug 16204 fixed: for i=1:$, end crashed Scilab
[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         if (pVar->isComputable() == false)
666         {
667             std::wostringstream os;
668             os << _W("Invalid index.\n");
669             throw ast::InternalError(os.str(), 999, e.getLocation());
670         }
671         //get IL initial Type
672         types::InternalType * pIL = pVar->getInitalType();
673         //std::cout << "for IL: " << pIL << std::endl;
674         //std::cout << "  for IV: " << pIT << std::endl;
675         //get index stack
676         symbol::Variable* var = e.getVardec().getAs<VarDec>()->getStack();
677
678         if (ctx->isprotected(var))
679         {
680             std::wostringstream os;
681             os << _W("Redefining permanent variable.\n");
682             CoverageInstance::stopChrono((void*)&e);
683             throw ast::InternalError(os.str(), 999, e.getVardec().getLocation());
684         }
685
686         ctx->put(var, pIL);
687         //use ref count to lock var against clear and detect any changes
688         pIL->IncreaseRef();
689
690         int size = static_cast<int>(pVar->getSize());
691         for (int i = 0; i < size; ++i)
692         {
693             //check if loop index has changed, deleted, copy ...
694             if (pIL->getRef() != 2)
695             {
696                 switch (pIL->getRef())
697                 {
698                     case 1:
699                         //someone clear me
700                         ctx->put(var, pIL);
701                         break;
702                     default:
703                         //someone assign me to another var
704                         //a = i;
705                         //unlock me
706                         pIL->DecreaseRef();
707
708                         //no need to destroy, it already assign to another var
709                         //pIL->killMe();
710
711                         //create a new me
712                         pIL = pVar->getInitalType();
713                         //lock loop index
714                         pIL->IncreaseRef();
715                         //update me ( must decrease ref of a )
716                         if (ctx->isprotected(var))
717                         {
718                             std::wostringstream os;
719                             os << _W("Redefining permanent variable.\n");
720                             CoverageInstance::stopChrono((void*)&e);
721                             throw ast::InternalError(os.str(), 999, e.getVardec().getLocation());
722                         }
723
724                         ctx->put(var, pIL);
725                         break;
726                 }
727             }
728
729             pVar->extractValue(i, pIL);
730
731             try
732             {
733                 e.getBody().accept(*this);
734             }
735             catch (const InternalError& ie)
736             {
737                 //unlock loop index and implicit list
738                 pIL->DecreaseRef();
739                 pIL->killMe();
740                 pIT->DecreaseRef();
741                 pIT->killMe();
742
743                 setResult(NULL);
744                 CoverageInstance::stopChrono((void*)&e);
745                 throw ie;
746             }
747
748             if (e.getBody().isBreak())
749             {
750                 const_cast<Exp&>(e.getBody()).resetBreak();
751                 break;
752             }
753
754             if (e.getBody().isContinue())
755             {
756                 const_cast<Exp&>(e.getBody()).resetContinue();
757                 continue;
758             }
759
760             if (e.getBody().isReturn())
761             {
762                 const_cast<ForExp&>(e).setReturn();
763                 const_cast<Exp&>(e.getBody()).resetReturn();
764                 break;
765             }
766         }
767
768         if (size == 0)
769         {
770             ctx->put(var, types::Double::Empty());
771         }
772
773         //unlock loop index
774         pIL->DecreaseRef();
775         pIL->killMe();
776     }
777     else if (pIT->isList())
778     {
779         types::List* pL = pIT->getAs<types::List>();
780         const int size = pL->getSize();
781         symbol::Variable* var = e.getVardec().getAs<VarDec>()->getStack();
782         for (int i = 0; i < size; ++i)
783         {
784             types::InternalType* pNew = pL->get(i);
785
786             if (ctx->isprotected(var))
787             {
788                 std::wostringstream os;
789                 os << _W("Redefining permanent variable.\n");
790                 CoverageInstance::stopChrono((void*)&e);
791                 throw ast::InternalError(os.str(), 999, e.getVardec().getLocation());
792             }
793             ctx->put(var, pNew);
794
795             try
796             {
797                 e.getBody().accept(*this);
798             }
799             catch (const InternalError& ie)
800             {
801                 //implicit list
802                 pIT->DecreaseRef();
803                 pIT->killMe();
804                 setResult(NULL);
805                 CoverageInstance::stopChrono((void*)&e);
806                 throw ie;
807             }
808
809             if (e.getBody().isBreak())
810             {
811                 const_cast<Exp*>(&(e.getBody()))->resetBreak();
812                 break;
813             }
814
815             if (e.getBody().isContinue())
816             {
817                 const_cast<Exp*>(&(e.getBody()))->resetContinue();
818                 continue;
819             }
820
821             if (e.getBody().isReturn())
822             {
823                 const_cast<ForExp*>(&e)->setReturn();
824                 const_cast<Exp&>(e.getBody()).resetReturn();
825                 break;
826             }
827         }
828     }
829     else if (pIT->isGenericType())
830     {
831         //Matrix i = [1,3,2,6] or other type
832         types::GenericType* pVar = pIT->getAs<types::GenericType>();
833         if (pVar->getDims() > 2)
834         {
835             pIT->DecreaseRef();
836             pIT->killMe();
837             CoverageInstance::stopChrono((void*)&e);
838             throw InternalError(_W("for expression can only manage 1 or 2 dimensions variables\n"), 999, e.getVardec().getLocation());
839         }
840
841         symbol::Variable* var = e.getVardec().getAs<VarDec>()->getStack();
842         for (int i = 0; i < pVar->getCols(); i++)
843         {
844             types::GenericType* pNew = pVar->getColumnValues(i);
845             if (pNew == NULL)
846             {
847                 pIT->DecreaseRef();
848                 pIT->killMe();
849                 CoverageInstance::stopChrono((void*)&e);
850                 throw InternalError(_W("for expression : Wrong type for loop iterator.\n"), 999, e.getVardec().getLocation());
851             }
852
853             if (ctx->isprotected(var))
854             {
855                 std::wostringstream os;
856                 os << _W("Redefining permanent variable.\n");
857                 CoverageInstance::stopChrono((void*)&e);
858                 throw InternalError(os.str(), 999, e.getVardec().getLocation());
859             }
860             ctx->put(var, pNew);
861
862             try
863             {
864                 e.getBody().accept(*this);
865             }
866             catch (const InternalError& ie)
867             {
868                 //implicit list
869                 pIT->DecreaseRef();
870                 pIT->killMe();
871                 setResult(NULL);
872                 CoverageInstance::stopChrono((void*)&e);
873                 throw ie;
874             }
875
876             if (e.getBody().isBreak())
877             {
878                 const_cast<Exp*>(&(e.getBody()))->resetBreak();
879                 break;
880             }
881
882             if (e.getBody().isContinue())
883             {
884                 const_cast<Exp*>(&(e.getBody()))->resetContinue();
885                 continue;
886             }
887
888             if (e.getBody().isReturn())
889             {
890                 const_cast<ForExp*>(&e)->setReturn();
891                 const_cast<Exp&>(e.getBody()).resetReturn();
892                 break;
893             }
894         }
895     }
896     else
897     {
898         pIT->DecreaseRef();
899         pIT->killMe();
900         CoverageInstance::stopChrono((void*)&e);
901         throw InternalError(_W("for expression : Wrong type for loop iterator.\n"), 999, e.getVardec().getLocation());
902     }
903
904     pIT->DecreaseRef();
905     pIT->killMe();
906
907     setResult(NULL);
908     CoverageInstance::stopChrono((void*)&e);
909 }
910
911 template <class T>
912 void RunVisitorT<T>::visitprivate(const ReturnExp &e)
913 {
914     CoverageInstance::invokeAndStartChrono((void*)&e);
915     if (e.isGlobal())
916     {
917         if (ConfigVariable::getPauseLevel() != 0 && symbol::Context::getInstance()->getScopeLevel() == ConfigVariable::getActivePauseLevel())
918         {
919             //return or resume
920             ConfigVariable::DecreasePauseLevel();
921             ConfigVariable::macroFirstLine_end();
922             CoverageInstance::stopChrono((void*)&e);
923             // resume will make the execution continue
924             // event if resume is a console command, it must not release the prompt
925             // because the prompt will be release at the and of the original command
926             StaticRunner_setCommandOrigin(NONE);
927             return;
928         }
929         else
930         {
931             const_cast<ReturnExp*>(&e)->setReturn();
932         }
933     }
934     else
935     {
936         //return(x)
937
938         if (e.getParent() == nullptr || e.getParent()->isAssignExp() == false)
939         {
940             CoverageInstance::stopChrono((void*)&e);
941             throw InternalError(_W("With input arguments, return / resume expects output arguments.\n"), 999, e.getLocation());
942         }
943         //in case of CallExp, we can return only one value
944         int iSaveExpectedSize = getExpectedSize();
945         setExpectedSize(1);
946         try
947         {
948             e.getExp().accept(*this);
949         }
950         catch (ScilabException &)
951         {
952             CoverageInstance::stopChrono((void*)&e);
953             throw;
954         }
955         setExpectedSize(iSaveExpectedSize);
956         const_cast<ReturnExp*>(&e)->setReturn();
957     }
958
959     CoverageInstance::stopChrono((void*)&e);
960 }
961
962 template <class T>
963 void RunVisitorT<T>::visitprivate(const IntSelectExp &e)
964 {
965     CoverageInstance::invokeAndStartChrono((void*)&e);
966     bool found = false;
967     //e.getSelect()->accept(*this);
968     //InternalType* pIT = getResult();
969     //setResult(nullptr);
970     //if (pIT && pIT->isDouble())
971     //{
972     //    Double * pDbl = static_cast<Double *>(pIT);
973     //    if (!pDbl->isComplex() && pDbl->getSize() == 1)
974     //    {
975     //        int64_t val;
976     //        if (analysis::tools::asInteger<int64_t>(pDbl->get(0), val))
977     //        {
978     //            Exp * exp = e.getExp(val);
979     //            found = true;
980     //            if (exp)
981     //            {
982     //                Exp * body = exp->isCaseExp() ? exp->getAs<CaseExp>()->getBody() : exp;
983     //                if (e.isBreakable())
984     //                {
985     //                    const_cast<IntSelectExp*>(&e)->resetBreak();
986     //                    body->setBreakable();
987     //                }
988
989     //                if (e.isContinuable())
990     //                {
991     //                    const_cast<IntSelectExp*>(&e)->resetContinue();
992     //                    body->setContinuable();
993     //                }
994
995     //                if (e.isReturnable())
996     //                {
997     //                    const_cast<IntSelectExp*>(&e)->resetReturn();
998     //                    body->setReturnable();
999     //                }
1000
1001     //                try
1002     //                {
1003     //                    //the good one
1004     //                    body->accept(*this);
1005     //                }
1006     //                catch (const InternalError& ie)
1007     //                {
1008     //                    pIT->killMe();
1009     //                    throw ie;
1010     //                }
1011
1012     //                if (e.isBreakable() && body->isBreak())
1013     //                {
1014     //                    const_cast<IntSelectExp*>(&e)->setBreak();
1015     //                    body->resetBreak();
1016     //                }
1017
1018     //                if (e.isContinuable() && body->isContinue())
1019     //                {
1020     //                    const_cast<IntSelectExp*>(&e)->setContinue();
1021     //                    body->resetContinue();
1022     //                }
1023
1024     //                if (e.isReturnable() && body->isReturn())
1025     //                {
1026     //                    const_cast<IntSelectExp*>(&e)->setReturn();
1027     //                    body->resetReturn();
1028     //                }
1029     //            }
1030     //        }
1031     //    }
1032     //}
1033
1034     if (!found)
1035     {
1036         try
1037         {
1038             e.getOriginal()->accept(*this);
1039         }
1040         catch (ScilabException &)
1041         {
1042             CoverageInstance::stopChrono((void*)&e);
1043             throw;
1044         }
1045     }
1046     CoverageInstance::stopChrono((void*)&e);
1047 }
1048
1049 template <class T>
1050 void RunVisitorT<T>::visitprivate(const StringSelectExp &e)
1051 {
1052     CoverageInstance::invokeAndStartChrono((void*)&e);
1053     try
1054     {
1055         e.getSelect()->accept(*this);
1056     }
1057     catch (ScilabException &)
1058     {
1059         CoverageInstance::stopChrono((void*)&e);
1060         throw;
1061     }
1062     types::InternalType* pIT = getResult();
1063     setResult(nullptr);
1064     bool found = false;
1065     if (pIT && pIT->isString())
1066     {
1067         types::String * pStr = static_cast<types::String *>(pIT);
1068         if (pStr->getSize() == 1)
1069         {
1070             if (wchar_t * s = pStr->get(0))
1071             {
1072                 const std::wstring ws(s);
1073                 Exp * exp = e.getExp(ws);
1074                 found = true;
1075                 if (exp)
1076                 {
1077                     Exp * body = exp->isCaseExp() ? exp->getAs<CaseExp>()->getBody() : exp;
1078                     if (e.isBreakable())
1079                     {
1080                         const_cast<StringSelectExp*>(&e)->resetBreak();
1081                         body->setBreakable();
1082                     }
1083
1084                     if (e.isContinuable())
1085                     {
1086                         const_cast<StringSelectExp*>(&e)->resetContinue();
1087                         body->setContinuable();
1088                     }
1089
1090                     if (e.isReturnable())
1091                     {
1092                         const_cast<StringSelectExp*>(&e)->resetReturn();
1093                         body->setReturnable();
1094                     }
1095
1096                     try
1097                     {
1098                         //the good one
1099                         body->accept(*this);
1100                     }
1101                     catch (const InternalError& ie)
1102                     {
1103                         pIT->killMe();
1104                         CoverageInstance::stopChrono((void*)&e);
1105                         throw ie;
1106                     }
1107
1108                     if (e.isBreakable() && body->isBreak())
1109                     {
1110                         const_cast<StringSelectExp*>(&e)->setBreak();
1111                         body->resetBreak();
1112                     }
1113
1114                     if (e.isContinuable() && body->isContinue())
1115                     {
1116                         const_cast<StringSelectExp*>(&e)->setContinue();
1117                         body->resetContinue();
1118                     }
1119
1120                     if (e.isReturnable() && body->isReturn())
1121                     {
1122                         const_cast<StringSelectExp*>(&e)->setReturn();
1123                         body->resetReturn();
1124                     }
1125                 }
1126             }
1127         }
1128     }
1129
1130     if (!found)
1131     {
1132         try
1133         {
1134             e.getOriginal()->accept(*this);
1135         }
1136         catch (ScilabException &)
1137         {
1138             CoverageInstance::stopChrono((void*)&e);
1139             throw;
1140         }
1141     }
1142     CoverageInstance::stopChrono((void*)&e);
1143 }
1144
1145 template <class T>
1146 void RunVisitorT<T>::visitprivate(const SelectExp &e)
1147 {
1148     // FIXME : exec select ... case ... else ... end
1149     CoverageInstance::invokeAndStartChrono((void*)&e);
1150     try
1151     {
1152         e.getSelect()->accept(*this);
1153     }
1154     catch (ScilabException &)
1155     {
1156         CoverageInstance::stopChrono((void*)&e);
1157         throw;
1158     }
1159
1160     bool bCase = false;
1161
1162     types::InternalType* pIT = getResult();
1163     setResult(NULL);
1164     if (pIT)
1165     {
1166         // protect pIT to avoid double free when
1167         // the variable in select is override in the case
1168         pIT->IncreaseRef();
1169
1170         //find good case
1171         exps_t cases = e.getCases();
1172         for (auto exp : cases)
1173         {
1174             CaseExp * pCase = exp->getAs<CaseExp>();
1175             try
1176             {
1177                 pCase->getTest()->accept(*this);
1178             }
1179             catch (ScilabException &)
1180             {
1181                 CoverageInstance::stopChrono((void*)&e);
1182                 throw;
1183             }
1184             types::InternalType *pITCase = getResult();
1185             setResult(NULL);
1186             if (pITCase)
1187             {
1188                 if (pITCase->isContainer()) //WARNING ONLY FOR CELL
1189                 {
1190                     //check each item
1191                 }
1192                 else if (*pITCase == *pIT)
1193                 {
1194                     try
1195                     {
1196                         //the good one
1197                         pCase->getBody()->accept(*this);
1198                     }
1199                     catch (const InternalError& ie)
1200                     {
1201                         pIT->DecreaseRef();
1202                         pIT->killMe();
1203                         CoverageInstance::stopChrono((void*)&e);
1204                         throw ie;
1205                     }
1206
1207                     if (e.isBreakable() && pCase->getBody()->isBreak())
1208                     {
1209                         const_cast<SelectExp*>(&e)->setBreak();
1210                         pCase->getBody()->resetBreak();
1211                     }
1212
1213                     if (e.isContinuable() && pCase->getBody()->isContinue())
1214                     {
1215                         const_cast<SelectExp*>(&e)->setContinue();
1216                         pCase->getBody()->resetContinue();
1217                     }
1218
1219                     if (e.isReturnable() && pCase->getBody()->isReturn())
1220                     {
1221                         const_cast<SelectExp*>(&e)->setReturn();
1222                         pCase->getBody()->resetReturn();
1223                     }
1224
1225                     pITCase->killMe();
1226                     bCase = true;
1227                     break;
1228                 }
1229
1230                 pITCase->killMe();
1231             }
1232         }
1233     }
1234
1235     if (bCase == false && e.getDefaultCase() != NULL)
1236     {
1237         try
1238         {
1239             //default case
1240             e.getDefaultCase()->accept(*this);
1241         }
1242         catch (const InternalError& ie)
1243         {
1244             if (pIT)
1245             {
1246                 pIT->DecreaseRef();
1247                 pIT->killMe();
1248             }
1249             CoverageInstance::stopChrono((void*)&e);
1250             throw ie;
1251         }
1252
1253         if (e.isBreakable() && e.getDefaultCase()->isBreak())
1254         {
1255             const_cast<SelectExp*>(&e)->setBreak();
1256             e.getDefaultCase()->resetBreak();
1257         }
1258
1259         if (e.isContinuable() && e.getDefaultCase()->isContinue())
1260         {
1261             const_cast<SelectExp*>(&e)->setContinue();
1262             e.getDefaultCase()->resetContinue();
1263         }
1264
1265         if (e.isReturnable() && e.getDefaultCase()->isReturn())
1266         {
1267             const_cast<SelectExp*>(&e)->setReturn();
1268             e.getDefaultCase()->resetReturn();
1269         }
1270     }
1271
1272     clearResult();
1273
1274     if (pIT)
1275     {
1276         pIT->DecreaseRef();
1277         pIT->killMe();
1278     }
1279     CoverageInstance::stopChrono((void*)&e);
1280 }
1281
1282 template <class T>
1283 void RunVisitorT<T>::visitprivate(const NotExp &e)
1284 {
1285     CoverageInstance::invokeAndStartChrono((void*)&e);
1286     /*
1287       @ or ~ !
1288       */
1289     try
1290     {
1291         e.getExp().accept(*this);
1292     }
1293     catch (ScilabException &)
1294     {
1295         CoverageInstance::stopChrono((void*)&e);
1296         throw;
1297     }
1298
1299     types::InternalType * pValue = getResult();
1300     types::InternalType * pReturn = NULL;
1301     if (pValue->neg(pReturn))
1302     {
1303         if (pValue != pReturn)
1304         {
1305             pValue->killMe();
1306         }
1307
1308         setResult(pReturn);
1309     }
1310     else
1311     {
1312         // neg returned false so the negation is not possible so we call the overload (%foo_5)
1313         types::typed_list in;
1314         types::typed_list out;
1315
1316         pValue->IncreaseRef();
1317         in.push_back(pValue);
1318
1319         types::Callable::ReturnValue Ret = Overload::call(L"%" + pValue->getShortTypeStr() + L"_5", in, 1, out, true);
1320
1321         if (Ret != types::Callable::OK)
1322         {
1323             cleanInOut(in, out);
1324             CoverageInstance::stopChrono((void*)&e);
1325             throw InternalError(ConfigVariable::getLastErrorMessage(), ConfigVariable::getLastErrorNumber(), e.getLocation());
1326         }
1327
1328         setResult(out);
1329         cleanIn(in, out);
1330     }
1331     CoverageInstance::stopChrono((void*)&e);
1332 }
1333
1334 template <class T>
1335 void RunVisitorT<T>::visitprivate(const TransposeExp &e)
1336 {
1337     CoverageInstance::invokeAndStartChrono((void*)&e);
1338     try
1339     {
1340         e.getExp().accept(*this);
1341     }
1342     catch (ScilabException &)
1343     {
1344         CoverageInstance::stopChrono((void*)&e);
1345         throw;
1346     }
1347
1348     if (getResultSize() != 1)
1349     {
1350         clearResult();
1351         wchar_t szError[bsiz];
1352         os_swprintf(szError, bsiz, _W("%ls: Can not transpose multiple elements.\n").c_str(), L"Transpose");
1353         CoverageInstance::stopChrono((void*)&e);
1354         throw InternalError(szError, 999, e.getLocation());
1355     }
1356
1357     types::InternalType * pValue = getResult();
1358     types::InternalType * pReturn = NULL;
1359     const bool bConjug = e.getConjugate() == TransposeExp::_Conjugate_;
1360
1361     if ((bConjug && pValue->adjoint(pReturn)) || (!bConjug && pValue->transpose(pReturn)))
1362     {
1363         if (pValue != pReturn)
1364         {
1365             pValue->killMe();
1366         }
1367
1368         setResult(pReturn);
1369         CoverageInstance::stopChrono((void*)&e);
1370
1371         return;
1372     }
1373     else
1374     {
1375         // transpose returned false so the negation is not possible so we call the overload (%foo_t or %foo_0)
1376         types::typed_list in;
1377         types::typed_list out;
1378
1379         pValue->IncreaseRef();
1380         in.push_back(pValue);
1381
1382         types::Callable::ReturnValue Ret;
1383         if (bConjug)
1384         {
1385             Ret = Overload::call(L"%" + getResult()->getShortTypeStr() + L"_t", in, 1, out, true);
1386         }
1387         else
1388         {
1389             Ret = Overload::call(L"%" + getResult()->getShortTypeStr() + L"_0", in, 1, out, true);
1390         }
1391
1392         if (Ret != types::Callable::OK)
1393         {
1394             cleanInOut(in, out);
1395             CoverageInstance::stopChrono((void*)&e);
1396             throw InternalError(ConfigVariable::getLastErrorMessage(), ConfigVariable::getLastErrorNumber(), e.getLocation());
1397         }
1398
1399         setResult(out);
1400         cleanIn(in, out);
1401     }
1402
1403     CoverageInstance::stopChrono((void*)&e);
1404 }
1405
1406 template <class T>
1407 void RunVisitorT<T>::visitprivate(const FunctionDec & e)
1408 {
1409     CoverageInstance::invokeAndStartChrono((void*)&e);
1410     symbol::Context* ctx = symbol::Context::getInstance();
1411     /*
1412       function foo
1413       endfunction
1414       */
1415
1416     // funcprot(0) : do nothing
1417     // funcprot(1) && warning(on) : warning
1418     //get input parameters list
1419     std::list<symbol::Variable*> *pVarList = new std::list<symbol::Variable*>();
1420     const exps_t & vars = e.getArgs().getVars();
1421     for (const auto var : vars)
1422     {
1423         pVarList->push_back(var->getAs<SimpleVar>()->getStack());
1424     }
1425
1426     //get output parameters list
1427     std::list<symbol::Variable*> *pRetList = new std::list<symbol::Variable*>();
1428     const exps_t & rets = e.getReturns().getVars();
1429     for (const auto ret : rets)
1430     {
1431         pRetList->push_back(ret->getAs<SimpleVar>()->getStack());
1432     }
1433
1434     types::Macro *pMacro = new types::Macro(e.getSymbol().getName(), *pVarList, *pRetList,
1435                                             const_cast<SeqExp&>(static_cast<const SeqExp&>(e.getBody())), L"script");
1436     pMacro->setLines(e.getLocation().first_line, e.getLocation().last_line);
1437     if (e.getMacro())
1438     {
1439         pMacro->setFileName(e.getMacro()->getFileName());
1440     }
1441
1442     if (ctx->isprotected(symbol::Symbol(pMacro->getName())))
1443     {
1444         delete pMacro;
1445         std::wostringstream os;
1446         os << _W("Redefining permanent variable.\n");
1447         CoverageInstance::stopChrono((void*)&e);
1448         throw InternalError(os.str(), 999, e.getLocation());
1449     }
1450
1451     if (ctx->addMacro(pMacro) == false)
1452     {
1453         char pstError[1024];
1454         char* pstFuncName = wide_string_to_UTF8(e.getSymbol().getName().c_str());
1455         os_sprintf(pstError, _("It is not possible to redefine the %s primitive this way (see clearfun).\n"), pstFuncName);
1456         wchar_t* pwstError = to_wide_string(pstError);
1457         std::wstring wstError(pwstError);
1458         FREE(pstFuncName);
1459         FREE(pwstError);
1460         pMacro->killMe();
1461         CoverageInstance::stopChrono((void*)&e);
1462         throw InternalError(wstError, 999, e.getLocation());
1463     }
1464
1465     CoverageInstance::stopChrono((void*)&e);
1466 }
1467
1468 template <class T>
1469 void RunVisitorT<T>::visitprivate(const ListExp &e)
1470 {
1471     CoverageInstance::invokeAndStartChrono((void*)&e);
1472     try
1473     {
1474         e.getStart().accept(*this);
1475     }
1476     catch (ScilabException &)
1477     {
1478         CoverageInstance::stopChrono((void*)&e);
1479         throw;
1480     }
1481
1482     types::InternalType* pITStart = getResult();
1483     types::GenericType* pStart = static_cast<types::GenericType*>(pITStart);
1484     if (pITStart == NULL ||
1485             ((pITStart->isGenericType() == false || pStart->getSize() != 1 || (pStart->isDouble() && pStart->getAs<types::Double>()->isComplex())) &&
1486              pStart->isList() == false)) // list case => call overload
1487     {
1488         setResult(NULL);
1489         wchar_t szError[bsiz];
1490         if (pITStart && pITStart->isImplicitList())
1491         {
1492             os_swprintf(szError, bsiz, _W("%ls: Too many %ls or wrong type for argument %d: Real scalar expected.\n").c_str(), L"':'", L"':'", 1);
1493         }
1494         else
1495         {
1496             os_swprintf(szError, bsiz, _W("%ls: Wrong type for argument %d: Real scalar expected.\n").c_str(), L"':'", 1);
1497         }
1498
1499         if (pITStart)
1500         {
1501             pITStart->killMe();
1502         }
1503
1504         CoverageInstance::stopChrono((void*)&e);
1505         throw InternalError(szError, 999, e.getLocation());
1506     }
1507
1508     try
1509     {
1510         e.getStep().accept(*this);
1511     }
1512     catch (ScilabException &)
1513     {
1514         CoverageInstance::stopChrono((void*)&e);
1515         throw;
1516     }
1517
1518     types::InternalType* pITStep = getResult();
1519     types::GenericType* pStep = static_cast<types::GenericType*>(pITStep);
1520     setResult(NULL);
1521     if (pITStep == NULL ||
1522             ((pITStep->isGenericType() == false || pStep->getSize() != 1 || (pStep->isDouble() && pStep->getAs<types::Double>()->isComplex())) &&
1523              pStep->isList() == false)) // list case => call overload
1524     {
1525         pITStart->killMe();
1526         if (pITStep)
1527         {
1528             pITStep->killMe();
1529         }
1530
1531         setResult(NULL);
1532         wchar_t szError[bsiz];
1533         os_swprintf(szError, bsiz, _W("%ls: Wrong type for argument %d: Real scalar expected.\n").c_str(), L"':'", 2);
1534         CoverageInstance::stopChrono((void*)&e);
1535         throw InternalError(szError, 999, e.getLocation());
1536     }
1537
1538     try
1539     {
1540         e.getEnd().accept(*this);
1541     }
1542     catch (ScilabException &)
1543     {
1544         CoverageInstance::stopChrono((void*)&e);
1545         throw;
1546     }
1547
1548     types::InternalType* pITEnd = getResult();
1549     types::GenericType* pEnd = static_cast<types::GenericType*>(pITEnd);
1550     setResult(NULL);
1551     if (pITEnd == NULL ||
1552             ((pITEnd->isGenericType() == false || pEnd->getSize() != 1 || (pEnd->isDouble() && pEnd->getAs<types::Double>()->isComplex())) &&
1553              pEnd->isList() == false)) // list case => call overload
1554     {
1555         pITStart->killMe();
1556         pITStep->killMe();
1557         if (pITEnd)
1558         {
1559             pITEnd->killMe();
1560         }
1561
1562         setResult(NULL);
1563         wchar_t szError[bsiz];
1564         os_swprintf(szError, bsiz, _W("%ls: Wrong type for argument %d: Real scalar expected.\n").c_str(), L"':'", 2 + e.hasExplicitStep());
1565         CoverageInstance::stopChrono((void*)&e);
1566         throw InternalError(szError, 999, e.getLocation());
1567     }
1568
1569     ////check if implicitlist is 1:$ to replace by ':'
1570     //if (piStart->isDouble() && piStep->isDouble() && piEnd->isPoly())
1571     //{
1572     //    if (piStart->getAs<Double>()->get()[0] == 1 && piStep->getAs<Double>()->get()[0] == 1)
1573     //    {
1574     //        SinglePoly* end = piEnd->getAs<Polynom>()->get()[0];
1575     //        if (end->getRank() == 1 && end->get()[0] == 0 && end->get()[1] == 1)
1576     //        {
1577     //            setResult(new Colon());
1578     //            return;
1579     //        }
1580     //    }
1581     //}
1582
1583     //check compatibility
1584     // double : double : double or poly : poly : poly and mix like double : double : poly
1585     if ((pStart->isPoly() || pStart->isDouble()) &&
1586             (pStep->isPoly() || pStep->isDouble()) &&
1587             (pEnd->isPoly() || pEnd->isDouble()))
1588     {
1589         // No need to kill piStart, ... because Implicit list ctor will incref them
1590         types::ImplicitList* pIL = new types::ImplicitList(pStart, pStep, pEnd);
1591         try
1592         {
1593             pIL->compute();
1594         }
1595         catch (const InternalError& ie)
1596         {
1597             // happends when compute() of ImplicitList cannot allocate memory
1598             pIL->killMe();
1599             throw ie;
1600         }
1601
1602         setResult(pIL);
1603         CoverageInstance::stopChrono((void*)&e);
1604         return;
1605     }
1606
1607     // int : double or int : int
1608     if (pStart->isInt() &&
1609             (pStep->isDouble() || pStep->isInt()) &&
1610             pEnd->isInt())
1611     {
1612         // check for same int type int8, int 16 ...
1613         if (pStart->getType() == pEnd->getType() &&
1614                 (pStart->getType() == pStep->getType() ||
1615                  pStep->isDouble()))
1616         {
1617             // No need to kill piStart, ... because Implicit list ctor will incref them
1618             types::ImplicitList* pIL = new types::ImplicitList(pStart, pStep, pEnd);
1619             try
1620             {
1621                 pIL->compute();
1622             }
1623             catch (const InternalError& ie)
1624             {
1625                 // happends when compute() of ImplicitList cannot allocate memory
1626                 pIL->killMe();
1627                 throw ie;
1628             }
1629
1630             setResult(pIL);
1631             CoverageInstance::stopChrono((void*)&e);
1632             return;
1633         }
1634     }
1635
1636     // Call Overload
1637     types::Callable::ReturnValue Ret;
1638     types::typed_list in;
1639     types::typed_list out;
1640
1641     pStart->IncreaseRef();
1642     in.push_back(pStart);
1643
1644     try
1645     {
1646         if (e.hasExplicitStep())
1647         {
1648             // 1:2:4
1649             //call overload %typeStart_b_typeStep
1650             pStep->IncreaseRef();
1651             in.push_back(pStep);
1652             pEnd->IncreaseRef();
1653             in.push_back(pEnd);
1654             Ret = Overload::call(L"%" + pStart->getShortTypeStr() + L"_b_" + pStep->getShortTypeStr(), in, 1, out, true);
1655         }
1656         else
1657         {
1658             // 1:2
1659             //call overload %typeStart_b_typeEnd
1660             pStep->killMe();
1661             pEnd->IncreaseRef();
1662             in.push_back(pEnd);
1663             Ret = Overload::call(L"%" + pStart->getShortTypeStr() + L"_b_" + pEnd->getShortTypeStr(), in, 1, out, true);
1664         }
1665     }
1666     catch (const InternalError& error)
1667     {
1668         setResult(NULL);
1669         cleanInOut(in, out);
1670         CoverageInstance::stopChrono((void*)&e);
1671         throw error;
1672     }
1673
1674     if (Ret != types::Callable::OK)
1675     {
1676         setResult(NULL);
1677         cleanInOut(in, out);
1678         CoverageInstance::stopChrono((void*)&e);
1679         throw InternalError(ConfigVariable::getLastErrorMessage(), ConfigVariable::getLastErrorNumber(), e.getLocation());
1680     }
1681
1682     setResult(out);
1683     cleanIn(in, out);
1684     CoverageInstance::stopChrono((void*)&e);
1685 }
1686
1687 template <class T>
1688 void RunVisitorT<T>::visitprivate(const OptimizedExp &e)
1689 {
1690 }
1691
1692 template <class T>
1693 void RunVisitorT<T>::visitprivate(const MemfillExp &e)
1694 {
1695     CoverageInstance::invokeAndStartChrono((void*)&e);
1696     try
1697     {
1698         e.getOriginal()->accept(*this);
1699     }
1700     catch (ScilabException &)
1701     {
1702         CoverageInstance::stopChrono((void*)&e);
1703         throw;
1704     }
1705 }
1706
1707 template <class T>
1708 void RunVisitorT<T>::visitprivate(const DAXPYExp &e)
1709 {
1710     CoverageInstance::invokeAndStartChrono((void*)&e);
1711     types::InternalType* pIT = NULL;
1712     types::Double* ad = NULL;
1713     int ar = 0;
1714     int ac = 0;
1715
1716     types::Double* xd = NULL;
1717     int xr = 0;
1718     int xc = 0;
1719
1720     types::Double* yd = NULL;
1721     int yr = 0;
1722     int yc = 0;
1723
1724     //check types and dimensions
1725
1726     //y must be double
1727     const Exp &ye = e.getY();
1728     try
1729     {
1730         ye.accept(*this);
1731     }
1732     catch (ScilabException &)
1733     {
1734         CoverageInstance::stopChrono((void*)&e);
1735         throw;
1736     }
1737
1738     pIT = getResult();
1739     if (pIT->isDouble())
1740     {
1741         yd = pIT->getAs<types::Double>();
1742         if (yd->getDims() == 2 && yd->isComplex() == false)
1743         {
1744             yr = yd->getRows();
1745             yc = yd->getCols();
1746         }
1747         else
1748         {
1749             yd->killMe();
1750             try
1751             {
1752                 e.getOriginal()->accept(*this);
1753             }
1754             catch (ScilabException &)
1755             {
1756                 CoverageInstance::stopChrono((void*)&e);
1757                 throw;
1758             }
1759             CoverageInstance::stopChrono((void*)&e);
1760             return;
1761         }
1762     }
1763     else
1764     {
1765         pIT->killMe();
1766         try
1767         {
1768             e.getOriginal()->accept(*this);
1769         }
1770         catch (ScilabException &)
1771         {
1772             CoverageInstance::stopChrono((void*)&e);
1773             throw;
1774         }
1775         CoverageInstance::stopChrono((void*)&e);
1776         return;
1777     }
1778
1779     //x
1780     const Exp &xe = e.getX();
1781     try
1782     {
1783         xe.accept(*this);
1784     }
1785     catch (ScilabException &)
1786     {
1787         CoverageInstance::stopChrono((void*)&e);
1788         throw;
1789     }
1790     pIT = getResult();
1791
1792     if (pIT->isDouble())
1793     {
1794         xd = pIT->getAs<types::Double>();
1795         if (xd->isScalar() && xd->isComplex() == false)
1796         {
1797             // x become a
1798             ad = xd;
1799             ar = 1;
1800             ac = 1;
1801         }
1802         else if (xd->getDims() == 2 && xd->isComplex() == false)
1803         {
1804             xr = xd->getRows();
1805             xc = xd->getCols();
1806         }
1807         else
1808         {
1809             yd->killMe();
1810             xd->killMe();
1811             try
1812             {
1813                 e.getOriginal()->accept(*this);
1814             }
1815             catch (ScilabException &)
1816             {
1817                 CoverageInstance::stopChrono((void*)&e);
1818                 throw;
1819             }
1820             CoverageInstance::stopChrono((void*)&e);
1821             return;
1822         }
1823     }
1824     else
1825     {
1826         pIT->killMe();
1827         yd->killMe();
1828         try
1829         {
1830             e.getOriginal()->accept(*this);
1831         }
1832         catch (ScilabException &)
1833         {
1834             CoverageInstance::stopChrono((void*)&e);
1835             throw;
1836         }
1837         CoverageInstance::stopChrono((void*)&e);
1838         return;
1839     }
1840
1841     const Exp &ae = e.getA();
1842     try
1843     {
1844         ae.accept(*this);
1845     }
1846     catch (ScilabException &)
1847     {
1848         CoverageInstance::stopChrono((void*)&e);
1849         throw;
1850     }
1851     pIT = getResult();
1852
1853     if (pIT->isDouble())
1854     {
1855         if (ad)
1856         {
1857             xd = pIT->getAs<types::Double>();
1858             //X is scalar it become A
1859             //now use A as X
1860             if (xd->getDims() == 2 && xd->isComplex() == false)
1861             {
1862                 xr = xd->getRows();
1863                 xc = xd->getCols();
1864             }
1865             else
1866             {
1867                 yd->killMe();
1868                 xd->killMe();
1869                 ad->killMe();
1870                 try
1871                 {
1872                     e.getOriginal()->accept(*this);
1873                 }
1874                 catch (ScilabException &)
1875                 {
1876                     CoverageInstance::stopChrono((void*)&e);
1877                     throw;
1878                 }
1879                 CoverageInstance::stopChrono((void*)&e);
1880                 return;
1881             }
1882         }
1883         else
1884         {
1885             //a is a and it must be scalar
1886             ad = pIT->getAs<types::Double>();
1887             if (/*ad->isScalar() && */ad->isComplex() == false)
1888             {
1889                 ar = ad->getRows(); //1;
1890                 ac = ad->getCols();//1;
1891             }
1892             else
1893             {
1894                 yd->killMe();
1895                 xd->killMe();
1896                 ad->killMe();
1897                 try
1898                 {
1899                     e.getOriginal()->accept(*this);
1900                 }
1901                 catch (ScilabException &)
1902                 {
1903                     CoverageInstance::stopChrono((void*)&e);
1904                     throw;
1905                 }
1906                 throw;
1907                 return;
1908             }
1909         }
1910     }
1911     else
1912     {
1913         pIT->killMe();
1914         yd->killMe();
1915         xd->killMe();
1916         try
1917         {
1918             e.getOriginal()->accept(*this);
1919         }
1920         catch (ScilabException &)
1921         {
1922             CoverageInstance::stopChrono((void*)&e);
1923             throw;
1924         }
1925         CoverageInstance::stopChrono((void*)&e);
1926         return;
1927     }
1928
1929     // If we get here we are certain that ad, xd & yd have been set
1930     if (ac == 1 &&
1931             ar == 1 &&
1932             xr == yr &&
1933             xc == yc)
1934     {
1935         //go !
1936         int one = 1;
1937         int size = xc * xr;
1938         //Double* od = (Double*)yd->clone();
1939         C2F(daxpy)(&size, ad->get(), xd->get(), &one, yd->get(), &one);
1940         //setResult(od);
1941         //yd->killMe();
1942         xd->killMe();
1943         ad->killMe();
1944         CoverageInstance::stopChrono((void*)&e);
1945         return;
1946     }
1947     else if (ac == xr && ar == yr && xc == yc)
1948     {
1949         char n = 'n';
1950         double one = 1;
1951         C2F(dgemm)(&n, &n, &ar, &xc, &ac, &one, ad->get(), &ar, xd->get(), &ac, &one, yd->get(), &ar);
1952         xd->killMe();
1953         ad->killMe();
1954         CoverageInstance::stopChrono((void*)&e);
1955         return;
1956     }
1957
1958     yd->killMe();
1959     xd->killMe();
1960     ad->killMe();
1961
1962     try
1963     {
1964         e.getOriginal()->accept(*this);
1965     }
1966     catch (ScilabException &)
1967     {
1968         CoverageInstance::stopChrono((void*)&e);
1969         throw;
1970     }
1971     CoverageInstance::stopChrono((void*)&e);
1972
1973     return;
1974 }
1975
1976 template <class T>
1977 void RunVisitorT<T>::visitprivate(const TryCatchExp  &e)
1978 {
1979     CoverageInstance::invokeAndStartChrono((void*)&e);
1980     //save current prompt mode
1981     bool oldVal = ConfigVariable::isSilentError();
1982     int oldMode = ConfigVariable::getPromptMode();
1983     //set mode silent for errors
1984     ConfigVariable::setSilentError(true);
1985
1986     symbol::Context* pCtx = symbol::Context::getInstance();
1987     try
1988     {
1989         int scope = pCtx->getScopeLevel();
1990         int level = ConfigVariable::getRecursionLevel();
1991         try
1992         {
1993             const_cast<Exp*>(&e.getTry())->setReturnable();
1994             e.getTry().accept(*this);
1995             //restore previous prompt mode
1996             ConfigVariable::setSilentError(oldVal);
1997
1998             if (e.getTry().isReturn())
1999             {
2000                 const_cast<Exp*>(&e.getTry())->resetReturn();
2001                 const_cast<TryCatchExp*>(&e)->setReturn();
2002             }
2003         }
2004         catch (const RecursionException& /* re */)
2005         {
2006             ConfigVariable::setPromptMode(oldMode);
2007
2008             //close opened scope during try
2009             while (pCtx->getScopeLevel() > scope)
2010             {
2011                 pCtx->scope_end();
2012             }
2013
2014             //decrease recursion to init value and close where
2015             while (ConfigVariable::getRecursionLevel() > level)
2016             {
2017                 ConfigVariable::where_end();
2018                 ConfigVariable::decreaseRecursion();
2019             }
2020
2021             //print msg about recursion limit and trigger an error
2022             wchar_t sz[1024];
2023             os_swprintf(sz, 1024, _W("Recursion limit reached (%d).\n").data(), ConfigVariable::getRecursionLimit());
2024             CoverageInstance::stopChrono((void*)&e);
2025             throw ast::InternalError(sz);
2026         }
2027
2028     }
2029     catch (const InternalError& /* ie */)
2030     {
2031         //restore previous prompt mode
2032         ConfigVariable::setSilentError(oldVal);
2033         //to lock lasterror
2034         ConfigVariable::setLastErrorCall();
2035         // reset call stack filled when error occurred
2036         ConfigVariable::resetWhereError();
2037         try
2038         {
2039             const_cast<Exp*>(&e.getCatch())->setReturnable();
2040             e.getCatch().accept(*this);
2041             if (e.getCatch().isReturn())
2042             {
2043                 const_cast<Exp*>(&e.getCatch())->resetReturn();
2044                 const_cast<TryCatchExp*>(&e)->setReturn();
2045             }
2046         }
2047         catch (ScilabException &)
2048         {
2049             CoverageInstance::stopChrono((void*)&e);
2050             throw;
2051         }
2052     }
2053     CoverageInstance::stopChrono((void*)&e);
2054 }
2055
2056
2057 } /* namespace ast */
2058
2059 #include "run_SeqExp.hpp"
2060 #include "run_CallExp.hpp"
2061 #include "run_MatrixExp.hpp"
2062 #include "run_OpExp.hpp"
2063 #include "run_AssignExp.hpp"
2064
2065 template EXTERN_AST class ast::RunVisitorT<ast::ExecVisitor>;
2066 template EXTERN_AST class ast::RunVisitorT<ast::StepVisitor>;
2067 template EXTERN_AST class ast::RunVisitorT<ast::TimedVisitor>;
2068 template EXTERN_AST class ast::RunVisitorT<ast::DebuggerVisitor>;