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