2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2014 - Scilab Enterprises - Antoine ELIAS
5 * Copyright (C) 2012 - 2016 - Scilab Enterprises
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.
16 //for Visual Leak Detector in debug compilation mode
18 #if defined(DEBUG_VLD) && defined(_DEBUG)
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"
33 #include "visitor_common.hxx"
35 #include "context.hxx"
36 #include "generic_operations.hxx"
37 #include "types_or.hxx"
38 #include "types_and.hxx"
39 #include "localization.hxx"
41 #include "macrofile.hxx"
44 #include "filemanager_interface.h"
47 #include "threadmanagement.hxx"
49 #include "coverage_instance.hxx"
54 #include "os_string.h"
55 #include "elem_common.h"
56 #include "storeCommand.h"
58 #include "scilabRead.h"
64 void RunVisitorT<T>::visitprivate(const StringExp & e)
66 CoverageInstance::invokeAndStartChrono((void*)&e);
67 if (e.getConstant() == nullptr)
69 types::String *psz = new types::String(e.getValue().c_str());
70 (const_cast<StringExp *>(&e))->setConstant(psz);
72 setResult(e.getConstant());
73 CoverageInstance::stopChrono((void*)&e);
77 void RunVisitorT<T>::visitprivate(const DoubleExp & e)
79 CoverageInstance::invokeAndStartChrono((void*)&e);
80 if (e.getConstant() == nullptr)
82 types::Double *pdbl = new types::Double(e.getValue());
83 (const_cast<DoubleExp *>(&e))->setConstant(pdbl);
85 setResult(e.getConstant());
86 CoverageInstance::stopChrono((void*)&e);
90 void RunVisitorT<T>::visitprivate(const BoolExp & e)
92 CoverageInstance::invokeAndStartChrono((void*)&e);
93 if (e.getConstant() == nullptr)
95 types::Bool *pB = new types::Bool(e.getValue());
96 (const_cast<BoolExp *>(&e))->setConstant(pB);
98 setResult(e.getConstant());
99 CoverageInstance::stopChrono((void*)&e);
103 void RunVisitorT<T>::visitprivate(const NilExp & e)
105 CoverageInstance::invokeAndStartChrono((void*)&e);
106 setResult(new types::Void());
107 CoverageInstance::stopChrono((void*)&e);
111 void RunVisitorT<T>::visitprivate(const SimpleVar & e)
113 CoverageInstance::invokeAndStartChrono((void*)&e);
114 symbol::Context* ctx = symbol::Context::getInstance();
115 symbol::Variable* var = ((SimpleVar&)e).getStack();
116 types::InternalType *pI = ctx->get(var);
120 if (e.isVerbose() && pI->isCallable() == false && ConfigVariable::isPrintOutput())
122 std::wostringstream ostr;
123 ostr << L" " << e.getSymbol().getName() << L" = ";
125 ostr << L"(" << pI->getRef() << L")";
129 scilabWriteW(ostr.str().c_str());
130 std::wostringstream ostrName;
131 ostrName << e.getSymbol().getName();
132 VariableToString(pI, ostrName.str().c_str());
135 //check if var is recalled in current scope like
140 if (e.getParent()->isSeqExp())
142 if (ctx->getScopeLevel() > 1 && var->empty() == false && var->top()->m_iLevel != ctx->getScopeLevel())
144 //put var in current scope
154 char* strErr = wide_string_to_UTF8(e.getSymbol().getName().c_str());
156 os_sprintf(pstError, _("Undefined variable: %s\n"), strErr);
157 pwstError = to_wide_string(pstError);
159 std::wstring wstError(pwstError);
161 CoverageInstance::stopChrono((void*)&e);
162 throw InternalError(wstError, 999, e.getLocation());
163 //Err, SimpleVar doesn't exist in Scilab scopes.
165 CoverageInstance::stopChrono((void*)&e);
169 void RunVisitorT<T>::visitprivate(const ColonVar & e)
171 CoverageInstance::invokeAndStartChrono((void*)&e);
172 types::Colon *pC = new types::Colon();
174 CoverageInstance::stopChrono((void*)&e);
178 void RunVisitorT<T>::visitprivate(const DollarVar & e)
180 CoverageInstance::invokeAndStartChrono((void*)&e);
181 setResult(types::Polynom::Dollar());
182 CoverageInstance::stopChrono((void*)&e);
186 void RunVisitorT<T>::visitprivate(const BreakExp & e)
188 CoverageInstance::invokeAndStartChrono((void*)&e);
189 const_cast<BreakExp*>(&e)->setBreak();
190 CoverageInstance::stopChrono((void*)&e);
194 void RunVisitorT<T>::visitprivate(const ContinueExp &e)
196 CoverageInstance::invokeAndStartChrono((void*)&e);
197 const_cast<ContinueExp*>(&e)->setContinue();
198 CoverageInstance::stopChrono((void*)&e);
202 void RunVisitorT<T>::visitprivate(const ArrayListExp & e)
204 CoverageInstance::invokeAndStartChrono((void*)&e);
205 exps_t::const_iterator it;
206 int iNbExpSize = this->getExpectedSize();
207 this->setExpectedSize(1);
209 types::typed_list lstIT;
210 for (it = e.getExps().begin(); it != e.getExps().end(); it++)
212 (*it)->accept(*this);
213 for (int j = 0; j < getResultSize(); j++)
215 lstIT.push_back(getResult(j));
221 this->setExpectedSize(iNbExpSize);
222 CoverageInstance::stopChrono((void*)&e);
226 void RunVisitorT<T>::visitprivate(const VarDec & e)
228 CoverageInstance::invokeAndStartChrono((void*)&e);
231 /*getting what to assign*/
232 e.getInit().accept(*this);
233 getResult()->IncreaseRef();
235 catch (const InternalError& error)
237 CoverageInstance::stopChrono((void*)&e);
240 CoverageInstance::stopChrono((void*)&e);
244 void RunVisitorT<T>::visitprivate(const CellExp & e)
246 CoverageInstance::invokeAndStartChrono((void*)&e);
248 exps_t::const_iterator row;
249 exps_t::const_iterator col;
252 exps_t lines = e.getLines();
254 for (row = lines.begin(); row != lines.end(); ++row)
256 exps_t cols = (*row)->getAs<MatrixLineExp>()->getColumns();
259 iColMax = static_cast<int>(cols.size());
262 if (iColMax != static_cast<int>(cols.size()))
264 std::wostringstream os;
265 os << _W("inconsistent row/column dimensions\n");
266 //os << ((Location)(*row)->getLocation()).getLocationString() << std::endl;
267 CoverageInstance::stopChrono((void*)&e);
268 throw InternalError(os.str(), 999, (*row)->getLocation());
273 types::Cell *pC = new types::Cell(static_cast<int>(lines.size()), iColMax);
278 //insert items in cell
279 for (i = 0, row = lines.begin(); row != lines.end(); ++row, ++i)
281 exps_t cols = (*row)->getAs<MatrixLineExp>()->getColumns();
282 for (j = 0, col = cols.begin(); col != cols.end(); ++col, ++j)
286 (*col)->accept(*this);
288 catch (ScilabException &)
290 CoverageInstance::stopChrono((void*)&e);
293 types::InternalType *pIT = getResult();
294 if (pIT->isImplicitList())
296 types::InternalType * _pIT = pIT->getAs<types::ImplicitList>()->extractFullMatrix();
311 CoverageInstance::stopChrono((void*)&e);
315 void RunVisitorT<T>::visitprivate(const FieldExp &e)
321 CoverageInstance::invokeAndStartChrono((void*)&e);
323 if (!e.getTail()->isSimpleVar())
325 wchar_t szError[bsiz];
326 os_swprintf(szError, bsiz, _W("/!\\ Unmanaged FieldExp.\n").c_str());
327 CoverageInstance::stopChrono((void*)&e);
328 throw InternalError(szError, 999, e.getLocation());
333 e.getHead()->accept(*this);
335 catch (const InternalError& error)
337 CoverageInstance::stopChrono((void*)&e);
341 if (getResult() == NULL)
343 wchar_t szError[bsiz];
344 os_swprintf(szError, bsiz, _W("Attempt to reference field of non-structure array.\n").c_str());
345 CoverageInstance::stopChrono((void*)&e);
346 throw InternalError(szError, 999, e.getLocation());
349 // TODO: handle case where getSize() > 1
350 // l=list(struct("toto","coucou"),struct("toto","hello"),1,2);[a,b]=l(1:2).toto
352 if (getResultSize() > 1)
355 wchar_t szError[bsiz];
356 os_swprintf(szError, bsiz, _W("Not yet implemented in Scilab.\n").c_str());
357 CoverageInstance::stopChrono((void*)&e);
358 throw InternalError(szError, 999, e.getLocation());
361 SimpleVar * psvRightMember = static_cast<SimpleVar *>(const_cast<Exp *>(e.getTail()));
362 std::wstring wstField = psvRightMember->getSymbol().getName();
363 types::InternalType * pValue = getResult();
364 types::InternalType * pReturn = NULL;
369 if (pValue->isGenericType() || pValue->isUserType())
371 ok = pValue->getAs<types::GenericType>()->extract(wstField, pReturn);
374 catch (std::wstring & err)
376 CoverageInstance::stopChrono((void*)&e);
377 throw InternalError(err.c_str(), 999, e.getTail()->getLocation());
384 std::wostringstream os;
385 os << _W("Invalid index.\n");
386 CoverageInstance::stopChrono((void*)&e);
387 throw InternalError(os.str(), 999, e.getLocation());
391 if (pValue->isDeletable())
393 if (pValue->isContainer())
395 // prevent delete of pReturn in case where
396 // extract not return a clone
397 pReturn->IncreaseRef();
399 pReturn->DecreaseRef();
407 else if (pValue->isFieldExtractionOverloadable())
409 types::typed_list in;
410 types::typed_list out;
412 types::String* pS = new types::String(wstField.c_str());
414 //TODO: in the case where overload is a macro there is no need to incref in
415 // because args will be put in context, removed and killed if required.
416 // But if the overload is a function... it is another story...
419 pValue->IncreaseRef();
422 in.push_back(pValue);
423 types::Callable::ReturnValue Ret = types::Callable::Error;
424 std::wstring stType = pValue->getShortTypeStr();
428 Ret = Overload::call(L"%" + stType + L"_e", in, 1, out, this);
430 catch (const InternalError& ie)
434 //to compatibility with scilab 5 code.
435 //tlist/mlist name are truncated to 8 first character
436 if (stType.size() > 8)
438 Ret = Overload::call(L"%" + stType.substr(0, 8) + L"_e", in, 1, out, this);
442 CoverageInstance::stopChrono((void*)&e);
446 catch (const InternalError& ie)
449 if (pValue->isList())
451 Ret = Overload::call(L"%l_e", in, 1, out, this);
455 CoverageInstance::stopChrono((void*)&e);
461 if (Ret != types::Callable::OK)
465 CoverageInstance::stopChrono((void*)&e);
466 throw InternalError(ConfigVariable::getLastErrorMessage(), ConfigVariable::getLastErrorNumber(), e.getLocation());
475 wchar_t szError[bsiz];
476 os_swprintf(szError, bsiz, _W("Attempt to reference field of non-structure array.\n").c_str());
477 CoverageInstance::stopChrono((void*)&e);
478 throw InternalError(szError, 999, e.getLocation());
481 CoverageInstance::stopChrono((void*)&e);
485 void RunVisitorT<T>::visitprivate(const IfExp &e)
487 CoverageInstance::invokeAndStartChrono((void*)&e);
489 //Create local exec visitor
490 ShortCutVisitor SCTest;
491 bool bTestStatus = false;
496 e.getTest().accept(SCTest);
497 e.getTest().accept(*this);
499 catch (ScilabException &)
501 CoverageInstance::stopChrono((void*)&e);
505 bTestStatus = getResult()->isTrue();
509 if (bTestStatus == true)
511 e.getThen().accept(*this);
513 else if (e.hasElse())
515 const ast::Exp & _else = e.getElse();
516 if (_else.isCommentExp())
518 CoverageInstance::invoke(_else);
522 e.getElse().accept(*this);
526 catch (ScilabException &)
528 CoverageInstance::stopChrono((void*)&e);
533 && ((&e.getElse())->isBreak()
534 || (&e.getThen())->isBreak()))
536 const_cast<IfExp*>(&e)->setBreak();
537 const_cast<Exp*>(&e.getElse())->resetBreak();
538 const_cast<Exp*>(&e.getThen())->resetBreak();
541 if (e.isContinuable()
542 && ((&e.getElse())->isContinue()
543 || (&e.getThen())->isContinue()))
545 const_cast<IfExp*>(&e)->setContinue();
546 const_cast<Exp*>(&e.getElse())->resetContinue();
547 const_cast<Exp*>(&e.getThen())->resetContinue();
551 && ((&e.getElse())->isReturn()
552 || (&e.getThen())->isReturn()))
554 const_cast<IfExp*>(&e)->setReturn();
555 const_cast<Exp*>(&e.getElse())->resetReturn();
556 const_cast<Exp*>(&e.getThen())->resetReturn();
559 CoverageInstance::stopChrono((void*)&e);
563 void RunVisitorT<T>::visitprivate(const WhileExp &e)
565 CoverageInstance::invokeAndStartChrono((void*)&e);
567 //Create local exec visitor
568 ShortCutVisitor SCTest;
572 //manage & and | like && and ||
573 e.getTest().accept(SCTest);
575 e.getTest().accept(*this);
577 catch (ScilabException &)
579 CoverageInstance::stopChrono((void*)&e);
583 types::InternalType* pIT = getResult();
585 while (pIT->isTrue())
590 e.getBody().accept(*this);
592 catch (ScilabException &)
594 CoverageInstance::stopChrono((void*)&e);
598 //clear old result value before evaluate new one
599 if (getResult() != NULL)
601 getResult()->killMe();
604 if (e.getBody().isBreak())
606 const_cast<Exp*>(&(e.getBody()))->resetBreak();
610 if (e.getBody().isReturn())
612 const_cast<WhileExp*>(&e)->setReturn();
613 const_cast<Exp*>(&(e.getBody()))->resetReturn();
617 if (e.getBody().isContinue())
619 const_cast<Exp*>(&(e.getBody()))->resetContinue();
624 e.getTest().accept(*this);
626 catch (ScilabException &)
628 CoverageInstance::stopChrono((void*)&e);
635 //clear result of condition or result of body
637 CoverageInstance::stopChrono((void*)&e);
641 void RunVisitorT<T>::visitprivate(const ForExp &e)
643 CoverageInstance::invokeAndStartChrono((void*)&e);
644 symbol::Context* ctx = symbol::Context::getInstance();
645 //vardec visit increase its result reference
648 e.getVardec().accept(*this);
650 catch (ScilabException &)
652 CoverageInstance::stopChrono((void*)&e);
655 types::InternalType* pIT = getResult();
657 if (pIT->isImplicitList())
660 types::ImplicitList* pVar = pIT->getAs<types::ImplicitList>();
661 //get IL initial Type
662 types::InternalType * pIL = pVar->getInitalType();
663 //std::cout << "for IL: " << pIL << std::endl;
664 //std::cout << " for IV: " << pIT << std::endl;
666 symbol::Variable* var = e.getVardec().getAs<VarDec>()->getStack();
668 if (ctx->isprotected(var))
670 std::wostringstream os;
671 os << _W("Redefining permanent variable.\n");
672 CoverageInstance::stopChrono((void*)&e);
673 throw ast::InternalError(os.str(), 999, e.getVardec().getLocation());
677 //use ref count to lock var against clear and detect any changes
680 int size = static_cast<int>(pVar->getSize());
681 for (int i = 0; i < size; ++i)
683 //check if loop index has changed, deleted, copy ...
684 if (pIL->getRef() != 2)
686 switch (pIL->getRef())
693 //someone assign me to another var
698 //no need to destroy, it already assign to another var
702 pIL = pVar->getInitalType();
705 //update me ( must decrease ref of a )
706 if (ctx->isprotected(var))
708 std::wostringstream os;
709 os << _W("Redefining permanent variable.\n");
710 CoverageInstance::stopChrono((void*)&e);
711 throw ast::InternalError(os.str(), 999, e.getVardec().getLocation());
719 pVar->extractValue(i, pIL);
723 e.getBody().accept(*this);
725 catch (const InternalError& ie)
727 //unlock loop index and implicit list
734 CoverageInstance::stopChrono((void*)&e);
738 if (e.getBody().isBreak())
740 const_cast<Exp&>(e.getBody()).resetBreak();
744 if (e.getBody().isContinue())
746 const_cast<Exp&>(e.getBody()).resetContinue();
750 if (e.getBody().isReturn())
752 const_cast<ForExp&>(e).setReturn();
753 const_cast<Exp&>(e.getBody()).resetReturn();
762 else if (pIT->isList())
764 types::List* pL = pIT->getAs<types::List>();
765 const int size = pL->getSize();
766 symbol::Variable* var = e.getVardec().getAs<VarDec>()->getStack();
767 for (int i = 0; i < size; ++i)
769 types::InternalType* pNew = pL->get(i);
771 if (ctx->isprotected(var))
773 std::wostringstream os;
774 os << _W("Redefining permanent variable.\n");
775 CoverageInstance::stopChrono((void*)&e);
776 throw ast::InternalError(os.str(), 999, e.getVardec().getLocation());
782 e.getBody().accept(*this);
784 catch (const InternalError& ie)
790 CoverageInstance::stopChrono((void*)&e);
794 if (e.getBody().isBreak())
796 const_cast<Exp*>(&(e.getBody()))->resetBreak();
800 if (e.getBody().isContinue())
802 const_cast<Exp*>(&(e.getBody()))->resetContinue();
806 if (e.getBody().isReturn())
808 const_cast<ForExp*>(&e)->setReturn();
809 const_cast<Exp&>(e.getBody()).resetReturn();
814 else if (pIT->isGenericType())
816 //Matrix i = [1,3,2,6] or other type
817 types::GenericType* pVar = pIT->getAs<types::GenericType>();
818 if (pVar->getDims() > 2)
822 CoverageInstance::stopChrono((void*)&e);
823 throw InternalError(_W("for expression can only manage 1 or 2 dimensions variables\n"), 999, e.getVardec().getLocation());
826 symbol::Variable* var = e.getVardec().getAs<VarDec>()->getStack();
827 for (int i = 0; i < pVar->getCols(); i++)
829 types::GenericType* pNew = pVar->getColumnValues(i);
834 CoverageInstance::stopChrono((void*)&e);
835 throw InternalError(_W("for expression : Wrong type for loop iterator.\n"), 999, e.getVardec().getLocation());
838 if (ctx->isprotected(var))
840 std::wostringstream os;
841 os << _W("Redefining permanent variable.\n");
842 CoverageInstance::stopChrono((void*)&e);
843 throw InternalError(os.str(), 999, e.getVardec().getLocation());
849 e.getBody().accept(*this);
851 catch (const InternalError& ie)
857 CoverageInstance::stopChrono((void*)&e);
861 if (e.getBody().isBreak())
863 const_cast<Exp*>(&(e.getBody()))->resetBreak();
867 if (e.getBody().isContinue())
869 const_cast<Exp*>(&(e.getBody()))->resetContinue();
873 if (e.getBody().isReturn())
875 const_cast<ForExp*>(&e)->setReturn();
876 const_cast<Exp&>(e.getBody()).resetReturn();
885 CoverageInstance::stopChrono((void*)&e);
886 throw InternalError(_W("for expression : Wrong type for loop iterator.\n"), 999, e.getVardec().getLocation());
893 CoverageInstance::stopChrono((void*)&e);
897 void RunVisitorT<T>::visitprivate(const ReturnExp &e)
899 CoverageInstance::invokeAndStartChrono((void*)&e);
902 if (ConfigVariable::getPauseLevel() != 0 && symbol::Context::getInstance()->getScopeLevel() == ConfigVariable::getActivePauseLevel())
904 if (ConfigVariable::getEnableDebug() == true)
906 sciprint(_("%s: function is disabled in debug mode.\n"), "resume");
907 CoverageInstance::stopChrono((void*)&e);
912 ConfigVariable::DecreasePauseLevel();
913 CoverageInstance::stopChrono((void*)&e);
918 const_cast<ReturnExp*>(&e)->setReturn();
925 if (e.getParent() == nullptr || e.getParent()->isAssignExp() == false)
927 CoverageInstance::stopChrono((void*)&e);
928 throw InternalError(_W("With input arguments, return / resume expects output arguments.\n"), 999, e.getLocation());
930 //in case of CallExp, we can return only one values
931 int iSaveExpectedSize = getExpectedSize();
935 e.getExp().accept(*this);
937 catch (ScilabException &)
939 CoverageInstance::stopChrono((void*)&e);
942 setExpectedSize(iSaveExpectedSize);
943 const_cast<ReturnExp*>(&e)->setReturn();
946 CoverageInstance::stopChrono((void*)&e);
950 void RunVisitorT<T>::visitprivate(const IntSelectExp &e)
952 CoverageInstance::invokeAndStartChrono((void*)&e);
954 //e.getSelect()->accept(*this);
955 //InternalType* pIT = getResult();
956 //setResult(nullptr);
957 //if (pIT && pIT->isDouble())
959 // Double * pDbl = static_cast<Double *>(pIT);
960 // if (!pDbl->isComplex() && pDbl->getSize() == 1)
963 // if (analysis::tools::asInteger<int64_t>(pDbl->get(0), val))
965 // Exp * exp = e.getExp(val);
969 // Exp * body = exp->isCaseExp() ? exp->getAs<CaseExp>()->getBody() : exp;
970 // if (e.isBreakable())
972 // const_cast<IntSelectExp*>(&e)->resetBreak();
973 // body->setBreakable();
976 // if (e.isContinuable())
978 // const_cast<IntSelectExp*>(&e)->resetContinue();
979 // body->setContinuable();
982 // if (e.isReturnable())
984 // const_cast<IntSelectExp*>(&e)->resetReturn();
985 // body->setReturnable();
991 // body->accept(*this);
993 // catch (const InternalError& ie)
999 // if (e.isBreakable() && body->isBreak())
1001 // const_cast<IntSelectExp*>(&e)->setBreak();
1002 // body->resetBreak();
1005 // if (e.isContinuable() && body->isContinue())
1007 // const_cast<IntSelectExp*>(&e)->setContinue();
1008 // body->resetContinue();
1011 // if (e.isReturnable() && body->isReturn())
1013 // const_cast<IntSelectExp*>(&e)->setReturn();
1014 // body->resetReturn();
1025 e.getOriginal()->accept(*this);
1027 catch (ScilabException &)
1029 CoverageInstance::stopChrono((void*)&e);
1033 CoverageInstance::stopChrono((void*)&e);
1037 void RunVisitorT<T>::visitprivate(const StringSelectExp &e)
1039 CoverageInstance::invokeAndStartChrono((void*)&e);
1042 e.getSelect()->accept(*this);
1044 catch (ScilabException &)
1046 CoverageInstance::stopChrono((void*)&e);
1049 types::InternalType* pIT = getResult();
1052 if (pIT && pIT->isString())
1054 types::String * pStr = static_cast<types::String *>(pIT);
1055 if (pStr->getSize() == 1)
1057 if (wchar_t * s = pStr->get(0))
1059 const std::wstring ws(s);
1060 Exp * exp = e.getExp(ws);
1064 Exp * body = exp->isCaseExp() ? exp->getAs<CaseExp>()->getBody() : exp;
1065 if (e.isBreakable())
1067 const_cast<StringSelectExp*>(&e)->resetBreak();
1068 body->setBreakable();
1071 if (e.isContinuable())
1073 const_cast<StringSelectExp*>(&e)->resetContinue();
1074 body->setContinuable();
1077 if (e.isReturnable())
1079 const_cast<StringSelectExp*>(&e)->resetReturn();
1080 body->setReturnable();
1086 body->accept(*this);
1088 catch (const InternalError& ie)
1091 CoverageInstance::stopChrono((void*)&e);
1095 if (e.isBreakable() && body->isBreak())
1097 const_cast<StringSelectExp*>(&e)->setBreak();
1101 if (e.isContinuable() && body->isContinue())
1103 const_cast<StringSelectExp*>(&e)->setContinue();
1104 body->resetContinue();
1107 if (e.isReturnable() && body->isReturn())
1109 const_cast<StringSelectExp*>(&e)->setReturn();
1110 body->resetReturn();
1121 e.getOriginal()->accept(*this);
1123 catch (ScilabException &)
1125 CoverageInstance::stopChrono((void*)&e);
1129 CoverageInstance::stopChrono((void*)&e);
1133 void RunVisitorT<T>::visitprivate(const SelectExp &e)
1135 // FIXME : exec select ... case ... else ... end
1136 CoverageInstance::invokeAndStartChrono((void*)&e);
1139 e.getSelect()->accept(*this);
1141 catch (ScilabException &)
1143 CoverageInstance::stopChrono((void*)&e);
1149 types::InternalType* pIT = getResult();
1153 // protect pIT to avoid double free when
1154 // the variable in select is override in the case
1158 exps_t cases = e.getCases();
1159 for (auto exp : cases)
1161 CaseExp * pCase = exp->getAs<CaseExp>();
1164 pCase->getTest()->accept(*this);
1166 catch (ScilabException &)
1168 CoverageInstance::stopChrono((void*)&e);
1171 types::InternalType *pITCase = getResult();
1175 if (pITCase->isContainer()) //WARNING ONLY FOR CELL
1179 else if (*pITCase == *pIT)
1184 pCase->getBody()->accept(*this);
1186 catch (const InternalError& ie)
1190 CoverageInstance::stopChrono((void*)&e);
1194 if (e.isBreakable() && pCase->getBody()->isBreak())
1196 const_cast<SelectExp*>(&e)->setBreak();
1197 pCase->getBody()->resetBreak();
1200 if (e.isContinuable() && pCase->getBody()->isContinue())
1202 const_cast<SelectExp*>(&e)->setContinue();
1203 pCase->getBody()->resetContinue();
1206 if (e.isReturnable() && pCase->getBody()->isReturn())
1208 const_cast<SelectExp*>(&e)->setReturn();
1209 pCase->getBody()->resetReturn();
1222 if (bCase == false && e.getDefaultCase() != NULL)
1227 e.getDefaultCase()->accept(*this);
1229 catch (const InternalError& ie)
1236 CoverageInstance::stopChrono((void*)&e);
1240 if (e.isBreakable() && e.getDefaultCase()->isBreak())
1242 const_cast<SelectExp*>(&e)->setBreak();
1243 e.getDefaultCase()->resetBreak();
1246 if (e.isContinuable() && e.getDefaultCase()->isContinue())
1248 const_cast<SelectExp*>(&e)->setContinue();
1249 e.getDefaultCase()->resetContinue();
1252 if (e.isReturnable() && e.getDefaultCase()->isReturn())
1254 const_cast<SelectExp*>(&e)->setReturn();
1255 e.getDefaultCase()->resetReturn();
1266 CoverageInstance::stopChrono((void*)&e);
1270 void RunVisitorT<T>::visitprivate(const NotExp &e)
1272 CoverageInstance::invokeAndStartChrono((void*)&e);
1278 e.getExp().accept(*this);
1280 catch (ScilabException &)
1282 CoverageInstance::stopChrono((void*)&e);
1286 types::InternalType * pValue = getResult();
1287 types::InternalType * pReturn = NULL;
1288 if (pValue->neg(pReturn))
1290 if (pValue != pReturn)
1299 // neg returned false so the negation is not possible so we call the overload (%foo_5)
1300 types::typed_list in;
1301 types::typed_list out;
1303 pValue->IncreaseRef();
1304 in.push_back(pValue);
1306 types::Callable::ReturnValue Ret = Overload::call(L"%" + pValue->getShortTypeStr() + L"_5", in, 1, out, this);
1308 if (Ret != types::Callable::OK)
1310 cleanInOut(in, out);
1311 CoverageInstance::stopChrono((void*)&e);
1312 throw InternalError(ConfigVariable::getLastErrorMessage(), ConfigVariable::getLastErrorNumber(), e.getLocation());
1318 CoverageInstance::stopChrono((void*)&e);
1322 void RunVisitorT<T>::visitprivate(const TransposeExp &e)
1324 CoverageInstance::invokeAndStartChrono((void*)&e);
1327 e.getExp().accept(*this);
1329 catch (ScilabException &)
1331 CoverageInstance::stopChrono((void*)&e);
1335 if (getResultSize() != 1)
1338 wchar_t szError[bsiz];
1339 os_swprintf(szError, bsiz, _W("%ls: Can not transpose multiple elements.\n").c_str(), L"Transpose");
1340 CoverageInstance::stopChrono((void*)&e);
1341 throw InternalError(szError, 999, e.getLocation());
1344 types::InternalType * pValue = getResult();
1345 types::InternalType * pReturn = NULL;
1346 const bool bConjug = e.getConjugate() == TransposeExp::_Conjugate_;
1348 if ((bConjug && pValue->adjoint(pReturn)) || (!bConjug && pValue->transpose(pReturn)))
1350 if (pValue != pReturn)
1356 CoverageInstance::stopChrono((void*)&e);
1362 // transpose returned false so the negation is not possible so we call the overload (%foo_t or %foo_0)
1363 types::typed_list in;
1364 types::typed_list out;
1366 pValue->IncreaseRef();
1367 in.push_back(pValue);
1369 types::Callable::ReturnValue Ret;
1372 Ret = Overload::call(L"%" + getResult()->getShortTypeStr() + L"_t", in, 1, out, this);
1376 Ret = Overload::call(L"%" + getResult()->getShortTypeStr() + L"_0", in, 1, out, this);
1379 if (Ret != types::Callable::OK)
1381 cleanInOut(in, out);
1382 CoverageInstance::stopChrono((void*)&e);
1383 throw InternalError(ConfigVariable::getLastErrorMessage(), ConfigVariable::getLastErrorNumber(), e.getLocation());
1390 CoverageInstance::stopChrono((void*)&e);
1394 void RunVisitorT<T>::visitprivate(const FunctionDec & e)
1396 CoverageInstance::invokeAndStartChrono((void*)&e);
1397 symbol::Context* ctx = symbol::Context::getInstance();
1403 // funcprot(0) : do nothing
1404 // funcprot(1) && warning(on) : warning
1405 //get input parameters list
1406 std::list<symbol::Variable*> *pVarList = new std::list<symbol::Variable*>();
1407 const exps_t & vars = e.getArgs().getVars();
1408 for (const auto var : vars)
1410 pVarList->push_back(var->getAs<SimpleVar>()->getStack());
1413 //get output parameters list
1414 std::list<symbol::Variable*> *pRetList = new std::list<symbol::Variable*>();
1415 const exps_t & rets = e.getReturns().getVars();
1416 for (const auto ret : rets)
1418 pRetList->push_back(ret->getAs<SimpleVar>()->getStack());
1421 types::Macro *pMacro = new types::Macro(e.getSymbol().getName(), *pVarList, *pRetList,
1422 const_cast<SeqExp&>(static_cast<const SeqExp&>(e.getBody())), L"script");
1423 pMacro->setLines(e.getLocation().first_line, e.getLocation().last_line);
1425 if (ctx->isprotected(symbol::Symbol(pMacro->getName())))
1428 std::wostringstream os;
1429 os << _W("Redefining permanent variable.\n");
1430 CoverageInstance::stopChrono((void*)&e);
1431 throw InternalError(os.str(), 999, e.getLocation());
1434 if (ctx->addMacro(pMacro) == false)
1436 char pstError[1024];
1437 char* pstFuncName = wide_string_to_UTF8(e.getSymbol().getName().c_str());
1438 os_sprintf(pstError, _("It is not possible to redefine the %s primitive this way (see clearfun).\n"), pstFuncName);
1439 wchar_t* pwstError = to_wide_string(pstError);
1440 std::wstring wstError(pwstError);
1444 CoverageInstance::stopChrono((void*)&e);
1445 throw InternalError(wstError, 999, e.getLocation());
1448 CoverageInstance::stopChrono((void*)&e);
1452 void RunVisitorT<T>::visitprivate(const ListExp &e)
1454 CoverageInstance::invokeAndStartChrono((void*)&e);
1457 e.getStart().accept(*this);
1459 catch (ScilabException &)
1461 CoverageInstance::stopChrono((void*)&e);
1464 types::GenericType* pITStart = static_cast<types::GenericType*>(getResult());
1465 if ((pITStart->getSize() != 1 || (pITStart->isDouble() && pITStart->getAs<types::Double>()->isComplex())) &&
1466 pITStart->isList() == false) // list case => call overload
1470 wchar_t szError[bsiz];
1471 os_swprintf(szError, bsiz, _W("%ls: Wrong type for argument %d: Real scalar expected.\n").c_str(), L"':'", 1);
1472 CoverageInstance::stopChrono((void*)&e);
1473 throw InternalError(szError, 999, e.getLocation());
1475 types::InternalType * piStart = pITStart;
1479 e.getStep().accept(*this);
1481 catch (ScilabException &)
1483 CoverageInstance::stopChrono((void*)&e);
1486 types::GenericType* pITStep = static_cast<types::GenericType*>(getResult());
1488 if ((pITStep->getSize() != 1 || (pITStep->isDouble() && pITStep->getAs<types::Double>()->isComplex())) &&
1489 pITStep->isList() == false) // list case => call overload
1494 wchar_t szError[bsiz];
1495 os_swprintf(szError, bsiz, _W("%ls: Wrong type for argument %d: Real scalar expected.\n").c_str(), L"':'", 2);
1496 CoverageInstance::stopChrono((void*)&e);
1497 throw InternalError(szError, 999, e.getLocation());
1499 types::InternalType* piStep = pITStep;
1503 e.getEnd().accept(*this);
1505 catch (ScilabException &)
1507 CoverageInstance::stopChrono((void*)&e);
1511 types::GenericType* pITEnd = static_cast<types::GenericType*>(getResult());
1513 if ((pITEnd->getSize() != 1 || (pITEnd->isDouble() && pITEnd->getAs<types::Double>()->isComplex())) &&
1514 pITEnd->isList() == false) // list case => call overload
1520 wchar_t szError[bsiz];
1521 os_swprintf(szError, bsiz, _W("%ls: Wrong type for argument %d: Real scalar expected.\n").c_str(), L"':'", 3);
1522 CoverageInstance::stopChrono((void*)&e);
1523 throw InternalError(szError, 999, e.getLocation());
1525 types::InternalType* piEnd = pITEnd;
1527 ////check if implicitlist is 1:$ to replace by ':'
1528 //if (piStart->isDouble() && piStep->isDouble() && piEnd->isPoly())
1530 // if (piStart->getAs<Double>()->get()[0] == 1 && piStep->getAs<Double>()->get()[0] == 1)
1532 // SinglePoly* end = piEnd->getAs<Polynom>()->get()[0];
1533 // if (end->getRank() == 1 && end->get()[0] == 0 && end->get()[1] == 1)
1535 // setResult(new Colon());
1541 //check compatibility
1542 // double : double : double or poly : poly : poly and mix like double : double : poly
1543 if ((piStart->isPoly() || piStart->isDouble()) &&
1544 (piStep->isPoly() || piStep->isDouble()) &&
1545 (piEnd->isPoly() || piEnd->isDouble()))
1547 // No need to kill piStart, ... because Implicit list ctor will incref them
1548 setResult(new types::ImplicitList(piStart, piStep, piEnd));
1549 CoverageInstance::stopChrono((void*)&e);
1553 // int : double or int : int
1554 if (piStart->isInt() &&
1555 (piStep->isDouble() || piStep->isInt()) &&
1558 // check for same int type int8, int 16 ...
1559 if (piStart->getType() == piEnd->getType() &&
1560 (piStart->getType() == piStep->getType() ||
1561 piStep->isDouble()))
1563 // No need to kill piStart, ... because Implicit list ctor will incref them
1564 setResult(new types::ImplicitList(piStart, piStep, piEnd));
1565 CoverageInstance::stopChrono((void*)&e);
1571 types::Callable::ReturnValue Ret;
1572 types::typed_list in;
1573 types::typed_list out;
1575 piStart->IncreaseRef();
1576 in.push_back(piStart);
1580 if (e.hasExplicitStep())
1583 //call overload %typeStart_b_typeStep
1584 piStep->IncreaseRef();
1585 in.push_back(piStep);
1586 piEnd->IncreaseRef();
1587 in.push_back(piEnd);
1588 Ret = Overload::call(L"%" + piStart->getShortTypeStr() + L"_b_" + piStep->getShortTypeStr(), in, 1, out, true);
1593 //call overload %typeStart_b_typeEnd
1595 piEnd->IncreaseRef();
1596 in.push_back(piEnd);
1597 Ret = Overload::call(L"%" + piStart->getShortTypeStr() + L"_b_" + piEnd->getShortTypeStr(), in, 1, out, true);
1600 catch (const InternalError& error)
1603 cleanInOut(in, out);
1604 CoverageInstance::stopChrono((void*)&e);
1608 if (Ret != types::Callable::OK)
1611 cleanInOut(in, out);
1612 CoverageInstance::stopChrono((void*)&e);
1613 throw InternalError(ConfigVariable::getLastErrorMessage(), ConfigVariable::getLastErrorNumber(), e.getLocation());
1618 CoverageInstance::stopChrono((void*)&e);
1622 void RunVisitorT<T>::visitprivate(const OptimizedExp &e)
1627 void RunVisitorT<T>::visitprivate(const MemfillExp &e)
1629 CoverageInstance::invokeAndStartChrono((void*)&e);
1632 e.getOriginal()->accept(*this);
1634 catch (ScilabException &)
1636 CoverageInstance::stopChrono((void*)&e);
1642 void RunVisitorT<T>::visitprivate(const DAXPYExp &e)
1644 CoverageInstance::invokeAndStartChrono((void*)&e);
1645 types::InternalType* pIT = NULL;
1646 types::Double* ad = NULL;
1650 types::Double* xd = NULL;
1654 types::Double* yd = NULL;
1658 //check types and dimensions
1661 const Exp &ye = e.getY();
1666 catch (ScilabException &)
1668 CoverageInstance::stopChrono((void*)&e);
1673 if (pIT->isDouble())
1675 yd = pIT->getAs<types::Double>();
1676 if (yd->getDims() == 2 && yd->isComplex() == false)
1686 e.getOriginal()->accept(*this);
1688 catch (ScilabException &)
1690 CoverageInstance::stopChrono((void*)&e);
1693 CoverageInstance::stopChrono((void*)&e);
1702 e.getOriginal()->accept(*this);
1704 catch (ScilabException &)
1706 CoverageInstance::stopChrono((void*)&e);
1709 CoverageInstance::stopChrono((void*)&e);
1714 const Exp &xe = e.getX();
1719 catch (ScilabException &)
1721 CoverageInstance::stopChrono((void*)&e);
1726 if (pIT->isDouble())
1728 xd = pIT->getAs<types::Double>();
1729 if (xd->isScalar() && xd->isComplex() == false)
1736 else if (xd->getDims() == 2 && xd->isComplex() == false)
1747 e.getOriginal()->accept(*this);
1749 catch (ScilabException &)
1751 CoverageInstance::stopChrono((void*)&e);
1754 CoverageInstance::stopChrono((void*)&e);
1764 e.getOriginal()->accept(*this);
1766 catch (ScilabException &)
1768 CoverageInstance::stopChrono((void*)&e);
1771 CoverageInstance::stopChrono((void*)&e);
1775 const Exp &ae = e.getA();
1780 catch (ScilabException &)
1782 CoverageInstance::stopChrono((void*)&e);
1787 if (pIT->isDouble())
1791 xd = pIT->getAs<types::Double>();
1792 //X is scalar it become A
1794 if (xd->getDims() == 2 && xd->isComplex() == false)
1806 e.getOriginal()->accept(*this);
1808 catch (ScilabException &)
1810 CoverageInstance::stopChrono((void*)&e);
1813 CoverageInstance::stopChrono((void*)&e);
1819 //a is a and it must be scalar
1820 ad = pIT->getAs<types::Double>();
1821 if (/*ad->isScalar() && */ad->isComplex() == false)
1823 ar = ad->getRows(); //1;
1824 ac = ad->getCols();//1;
1833 e.getOriginal()->accept(*this);
1835 catch (ScilabException &)
1837 CoverageInstance::stopChrono((void*)&e);
1852 e.getOriginal()->accept(*this);
1854 catch (ScilabException &)
1856 CoverageInstance::stopChrono((void*)&e);
1859 CoverageInstance::stopChrono((void*)&e);
1873 //Double* od = (Double*)yd->clone();
1874 C2F(daxpy)(&size, ad->get(), xd->get(), &one, yd->get(), &one);
1879 CoverageInstance::stopChrono((void*)&e);
1882 else if (ac == xr && ar == yr && xc == yc)
1886 C2F(dgemm)(&n, &n, &ar, &xc, &ac, &one, ad->get(), &ar, xd->get(), &ac, &one, yd->get(), &ar);
1889 CoverageInstance::stopChrono((void*)&e);
1911 e.getOriginal()->accept(*this);
1913 catch (ScilabException &)
1915 CoverageInstance::stopChrono((void*)&e);
1918 CoverageInstance::stopChrono((void*)&e);
1924 void RunVisitorT<T>::visitprivate(const TryCatchExp &e)
1926 CoverageInstance::invokeAndStartChrono((void*)&e);
1927 //save current prompt mode
1928 bool oldVal = ConfigVariable::isSilentError();
1929 int oldMode = ConfigVariable::getPromptMode();
1930 //set mode silent for errors
1931 ConfigVariable::setSilentError(true);
1933 symbol::Context* pCtx = symbol::Context::getInstance();
1936 int scope = pCtx->getScopeLevel();
1937 int level = ConfigVariable::getRecursionLevel();
1940 e.getTry().accept(*this);
1941 //restore previous prompt mode
1942 ConfigVariable::setSilentError(oldVal);
1944 catch (const RecursionException& /* re */)
1946 ConfigVariable::setPromptMode(oldMode);
1948 //close opened scope during try
1949 while (pCtx->getScopeLevel() > scope)
1954 //decrease recursion to init value and close where
1955 while (ConfigVariable::getRecursionLevel() > level)
1957 ConfigVariable::where_end();
1958 ConfigVariable::decreaseRecursion();
1961 //print msg about recursion limit and trigger an error
1963 os_swprintf(sz, 1024, _W("Recursion limit reached (%d).\n").data(), ConfigVariable::getRecursionLimit());
1964 CoverageInstance::stopChrono((void*)&e);
1965 throw ast::InternalError(sz);
1969 catch (const InternalError& /* ie */)
1971 //restore previous prompt mode
1972 ConfigVariable::setSilentError(oldVal);
1974 ConfigVariable::setLastErrorCall();
1975 // reset call stack filled when error occured
1976 ConfigVariable::resetWhereError();
1979 e.getCatch().accept(*this);
1981 catch (ScilabException &)
1983 CoverageInstance::stopChrono((void*)&e);
1987 CoverageInstance::stopChrono((void*)&e);
1991 } /* namespace ast */
1993 #include "run_SeqExp.hpp"
1994 #include "run_CallExp.hpp"
1995 #include "run_MatrixExp.hpp"
1996 #include "run_OpExp.hpp"
1997 #include "run_AssignExp.hpp"
1999 template EXTERN_AST class ast::RunVisitorT<ast::ExecVisitor>;
2000 template EXTERN_AST class ast::RunVisitorT<ast::StepVisitor>;
2001 template EXTERN_AST class ast::RunVisitorT<ast::TimedVisitor>;
2002 template EXTERN_AST class ast::RunVisitorT<ast::DebuggerVisitor>;