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, true);
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, true);
442 CoverageInstance::stopChrono((void*)&e);
446 catch (const InternalError& ie)
449 if (pValue->isList())
451 Ret = Overload::call(L"%l_e", in, 1, out, true);
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())
592 e.getBody().accept(*this);
594 catch (ScilabException &)
596 CoverageInstance::stopChrono((void*)&e);
600 //clear old result value before evaluate new one
601 if (getResult() != NULL)
603 getResult()->killMe();
606 if (e.getBody().isBreak())
608 const_cast<Exp*>(&(e.getBody()))->resetBreak();
612 if (e.getBody().isReturn())
614 const_cast<WhileExp*>(&e)->setReturn();
615 const_cast<Exp*>(&(e.getBody()))->resetReturn();
619 if (e.getBody().isContinue())
621 const_cast<Exp*>(&(e.getBody()))->resetContinue();
626 e.getTest().accept(*this);
628 catch (ScilabException &)
630 CoverageInstance::stopChrono((void*)&e);
637 //clear result of condition or result of body
639 CoverageInstance::stopChrono((void*)&e);
643 void RunVisitorT<T>::visitprivate(const ForExp &e)
645 CoverageInstance::invokeAndStartChrono((void*)&e);
646 symbol::Context* ctx = symbol::Context::getInstance();
647 //vardec visit increase its result reference
650 e.getVardec().accept(*this);
652 catch (ScilabException &)
654 CoverageInstance::stopChrono((void*)&e);
657 types::InternalType* pIT = getResult();
659 if (pIT->isImplicitList())
662 types::ImplicitList* pVar = pIT->getAs<types::ImplicitList>();
663 //get IL initial Type
664 types::InternalType * pIL = pVar->getInitalType();
665 //std::cout << "for IL: " << pIL << std::endl;
666 //std::cout << " for IV: " << pIT << std::endl;
668 symbol::Variable* var = e.getVardec().getAs<VarDec>()->getStack();
670 if (ctx->isprotected(var))
672 std::wostringstream os;
673 os << _W("Redefining permanent variable.\n");
674 CoverageInstance::stopChrono((void*)&e);
675 throw ast::InternalError(os.str(), 999, e.getVardec().getLocation());
679 //use ref count to lock var against clear and detect any changes
682 int size = static_cast<int>(pVar->getSize());
683 for (int i = 0; i < size; ++i)
685 //check if loop index has changed, deleted, copy ...
686 if (pIL->getRef() != 2)
688 switch (pIL->getRef())
695 //someone assign me to another var
700 //no need to destroy, it already assign to another var
704 pIL = pVar->getInitalType();
707 //update me ( must decrease ref of a )
708 if (ctx->isprotected(var))
710 std::wostringstream os;
711 os << _W("Redefining permanent variable.\n");
712 CoverageInstance::stopChrono((void*)&e);
713 throw ast::InternalError(os.str(), 999, e.getVardec().getLocation());
721 pVar->extractValue(i, pIL);
725 e.getBody().accept(*this);
727 catch (const InternalError& ie)
729 //unlock loop index and implicit list
736 CoverageInstance::stopChrono((void*)&e);
740 if (e.getBody().isBreak())
742 const_cast<Exp&>(e.getBody()).resetBreak();
746 if (e.getBody().isContinue())
748 const_cast<Exp&>(e.getBody()).resetContinue();
752 if (e.getBody().isReturn())
754 const_cast<ForExp&>(e).setReturn();
755 const_cast<Exp&>(e.getBody()).resetReturn();
764 else if (pIT->isList())
766 types::List* pL = pIT->getAs<types::List>();
767 const int size = pL->getSize();
768 symbol::Variable* var = e.getVardec().getAs<VarDec>()->getStack();
769 for (int i = 0; i < size; ++i)
771 types::InternalType* pNew = pL->get(i);
773 if (ctx->isprotected(var))
775 std::wostringstream os;
776 os << _W("Redefining permanent variable.\n");
777 CoverageInstance::stopChrono((void*)&e);
778 throw ast::InternalError(os.str(), 999, e.getVardec().getLocation());
784 e.getBody().accept(*this);
786 catch (const InternalError& ie)
792 CoverageInstance::stopChrono((void*)&e);
796 if (e.getBody().isBreak())
798 const_cast<Exp*>(&(e.getBody()))->resetBreak();
802 if (e.getBody().isContinue())
804 const_cast<Exp*>(&(e.getBody()))->resetContinue();
808 if (e.getBody().isReturn())
810 const_cast<ForExp*>(&e)->setReturn();
811 const_cast<Exp&>(e.getBody()).resetReturn();
816 else if (pIT->isGenericType())
818 //Matrix i = [1,3,2,6] or other type
819 types::GenericType* pVar = pIT->getAs<types::GenericType>();
820 if (pVar->getDims() > 2)
824 CoverageInstance::stopChrono((void*)&e);
825 throw InternalError(_W("for expression can only manage 1 or 2 dimensions variables\n"), 999, e.getVardec().getLocation());
828 symbol::Variable* var = e.getVardec().getAs<VarDec>()->getStack();
829 for (int i = 0; i < pVar->getCols(); i++)
831 types::GenericType* pNew = pVar->getColumnValues(i);
836 CoverageInstance::stopChrono((void*)&e);
837 throw InternalError(_W("for expression : Wrong type for loop iterator.\n"), 999, e.getVardec().getLocation());
840 if (ctx->isprotected(var))
842 std::wostringstream os;
843 os << _W("Redefining permanent variable.\n");
844 CoverageInstance::stopChrono((void*)&e);
845 throw InternalError(os.str(), 999, e.getVardec().getLocation());
851 e.getBody().accept(*this);
853 catch (const InternalError& ie)
859 CoverageInstance::stopChrono((void*)&e);
863 if (e.getBody().isBreak())
865 const_cast<Exp*>(&(e.getBody()))->resetBreak();
869 if (e.getBody().isContinue())
871 const_cast<Exp*>(&(e.getBody()))->resetContinue();
875 if (e.getBody().isReturn())
877 const_cast<ForExp*>(&e)->setReturn();
878 const_cast<Exp&>(e.getBody()).resetReturn();
887 CoverageInstance::stopChrono((void*)&e);
888 throw InternalError(_W("for expression : Wrong type for loop iterator.\n"), 999, e.getVardec().getLocation());
895 CoverageInstance::stopChrono((void*)&e);
899 void RunVisitorT<T>::visitprivate(const ReturnExp &e)
901 CoverageInstance::invokeAndStartChrono((void*)&e);
904 if (ConfigVariable::getPauseLevel() != 0 && symbol::Context::getInstance()->getScopeLevel() == ConfigVariable::getActivePauseLevel())
906 if (ConfigVariable::getEnableDebug() == true)
908 sciprint(_("%s: function is disabled in debug mode.\n"), "resume");
909 CoverageInstance::stopChrono((void*)&e);
914 ConfigVariable::DecreasePauseLevel();
915 ConfigVariable::macroFirstLine_end();
916 CoverageInstance::stopChrono((void*)&e);
921 const_cast<ReturnExp*>(&e)->setReturn();
928 if (e.getParent() == nullptr || e.getParent()->isAssignExp() == false)
930 CoverageInstance::stopChrono((void*)&e);
931 throw InternalError(_W("With input arguments, return / resume expects output arguments.\n"), 999, e.getLocation());
933 //in case of CallExp, we can return only one value
934 int iSaveExpectedSize = getExpectedSize();
938 e.getExp().accept(*this);
940 catch (ScilabException &)
942 CoverageInstance::stopChrono((void*)&e);
945 setExpectedSize(iSaveExpectedSize);
946 const_cast<ReturnExp*>(&e)->setReturn();
949 CoverageInstance::stopChrono((void*)&e);
953 void RunVisitorT<T>::visitprivate(const IntSelectExp &e)
955 CoverageInstance::invokeAndStartChrono((void*)&e);
957 //e.getSelect()->accept(*this);
958 //InternalType* pIT = getResult();
959 //setResult(nullptr);
960 //if (pIT && pIT->isDouble())
962 // Double * pDbl = static_cast<Double *>(pIT);
963 // if (!pDbl->isComplex() && pDbl->getSize() == 1)
966 // if (analysis::tools::asInteger<int64_t>(pDbl->get(0), val))
968 // Exp * exp = e.getExp(val);
972 // Exp * body = exp->isCaseExp() ? exp->getAs<CaseExp>()->getBody() : exp;
973 // if (e.isBreakable())
975 // const_cast<IntSelectExp*>(&e)->resetBreak();
976 // body->setBreakable();
979 // if (e.isContinuable())
981 // const_cast<IntSelectExp*>(&e)->resetContinue();
982 // body->setContinuable();
985 // if (e.isReturnable())
987 // const_cast<IntSelectExp*>(&e)->resetReturn();
988 // body->setReturnable();
994 // body->accept(*this);
996 // catch (const InternalError& ie)
1002 // if (e.isBreakable() && body->isBreak())
1004 // const_cast<IntSelectExp*>(&e)->setBreak();
1005 // body->resetBreak();
1008 // if (e.isContinuable() && body->isContinue())
1010 // const_cast<IntSelectExp*>(&e)->setContinue();
1011 // body->resetContinue();
1014 // if (e.isReturnable() && body->isReturn())
1016 // const_cast<IntSelectExp*>(&e)->setReturn();
1017 // body->resetReturn();
1028 e.getOriginal()->accept(*this);
1030 catch (ScilabException &)
1032 CoverageInstance::stopChrono((void*)&e);
1036 CoverageInstance::stopChrono((void*)&e);
1040 void RunVisitorT<T>::visitprivate(const StringSelectExp &e)
1042 CoverageInstance::invokeAndStartChrono((void*)&e);
1045 e.getSelect()->accept(*this);
1047 catch (ScilabException &)
1049 CoverageInstance::stopChrono((void*)&e);
1052 types::InternalType* pIT = getResult();
1055 if (pIT && pIT->isString())
1057 types::String * pStr = static_cast<types::String *>(pIT);
1058 if (pStr->getSize() == 1)
1060 if (wchar_t * s = pStr->get(0))
1062 const std::wstring ws(s);
1063 Exp * exp = e.getExp(ws);
1067 Exp * body = exp->isCaseExp() ? exp->getAs<CaseExp>()->getBody() : exp;
1068 if (e.isBreakable())
1070 const_cast<StringSelectExp*>(&e)->resetBreak();
1071 body->setBreakable();
1074 if (e.isContinuable())
1076 const_cast<StringSelectExp*>(&e)->resetContinue();
1077 body->setContinuable();
1080 if (e.isReturnable())
1082 const_cast<StringSelectExp*>(&e)->resetReturn();
1083 body->setReturnable();
1089 body->accept(*this);
1091 catch (const InternalError& ie)
1094 CoverageInstance::stopChrono((void*)&e);
1098 if (e.isBreakable() && body->isBreak())
1100 const_cast<StringSelectExp*>(&e)->setBreak();
1104 if (e.isContinuable() && body->isContinue())
1106 const_cast<StringSelectExp*>(&e)->setContinue();
1107 body->resetContinue();
1110 if (e.isReturnable() && body->isReturn())
1112 const_cast<StringSelectExp*>(&e)->setReturn();
1113 body->resetReturn();
1124 e.getOriginal()->accept(*this);
1126 catch (ScilabException &)
1128 CoverageInstance::stopChrono((void*)&e);
1132 CoverageInstance::stopChrono((void*)&e);
1136 void RunVisitorT<T>::visitprivate(const SelectExp &e)
1138 // FIXME : exec select ... case ... else ... end
1139 CoverageInstance::invokeAndStartChrono((void*)&e);
1142 e.getSelect()->accept(*this);
1144 catch (ScilabException &)
1146 CoverageInstance::stopChrono((void*)&e);
1152 types::InternalType* pIT = getResult();
1156 // protect pIT to avoid double free when
1157 // the variable in select is override in the case
1161 exps_t cases = e.getCases();
1162 for (auto exp : cases)
1164 CaseExp * pCase = exp->getAs<CaseExp>();
1167 pCase->getTest()->accept(*this);
1169 catch (ScilabException &)
1171 CoverageInstance::stopChrono((void*)&e);
1174 types::InternalType *pITCase = getResult();
1178 if (pITCase->isContainer()) //WARNING ONLY FOR CELL
1182 else if (*pITCase == *pIT)
1187 pCase->getBody()->accept(*this);
1189 catch (const InternalError& ie)
1193 CoverageInstance::stopChrono((void*)&e);
1197 if (e.isBreakable() && pCase->getBody()->isBreak())
1199 const_cast<SelectExp*>(&e)->setBreak();
1200 pCase->getBody()->resetBreak();
1203 if (e.isContinuable() && pCase->getBody()->isContinue())
1205 const_cast<SelectExp*>(&e)->setContinue();
1206 pCase->getBody()->resetContinue();
1209 if (e.isReturnable() && pCase->getBody()->isReturn())
1211 const_cast<SelectExp*>(&e)->setReturn();
1212 pCase->getBody()->resetReturn();
1225 if (bCase == false && e.getDefaultCase() != NULL)
1230 e.getDefaultCase()->accept(*this);
1232 catch (const InternalError& ie)
1239 CoverageInstance::stopChrono((void*)&e);
1243 if (e.isBreakable() && e.getDefaultCase()->isBreak())
1245 const_cast<SelectExp*>(&e)->setBreak();
1246 e.getDefaultCase()->resetBreak();
1249 if (e.isContinuable() && e.getDefaultCase()->isContinue())
1251 const_cast<SelectExp*>(&e)->setContinue();
1252 e.getDefaultCase()->resetContinue();
1255 if (e.isReturnable() && e.getDefaultCase()->isReturn())
1257 const_cast<SelectExp*>(&e)->setReturn();
1258 e.getDefaultCase()->resetReturn();
1269 CoverageInstance::stopChrono((void*)&e);
1273 void RunVisitorT<T>::visitprivate(const NotExp &e)
1275 CoverageInstance::invokeAndStartChrono((void*)&e);
1281 e.getExp().accept(*this);
1283 catch (ScilabException &)
1285 CoverageInstance::stopChrono((void*)&e);
1289 types::InternalType * pValue = getResult();
1290 types::InternalType * pReturn = NULL;
1291 if (pValue->neg(pReturn))
1293 if (pValue != pReturn)
1302 // neg returned false so the negation is not possible so we call the overload (%foo_5)
1303 types::typed_list in;
1304 types::typed_list out;
1306 pValue->IncreaseRef();
1307 in.push_back(pValue);
1309 types::Callable::ReturnValue Ret = Overload::call(L"%" + pValue->getShortTypeStr() + L"_5", in, 1, out, true);
1311 if (Ret != types::Callable::OK)
1313 cleanInOut(in, out);
1314 CoverageInstance::stopChrono((void*)&e);
1315 throw InternalError(ConfigVariable::getLastErrorMessage(), ConfigVariable::getLastErrorNumber(), e.getLocation());
1321 CoverageInstance::stopChrono((void*)&e);
1325 void RunVisitorT<T>::visitprivate(const TransposeExp &e)
1327 CoverageInstance::invokeAndStartChrono((void*)&e);
1330 e.getExp().accept(*this);
1332 catch (ScilabException &)
1334 CoverageInstance::stopChrono((void*)&e);
1338 if (getResultSize() != 1)
1341 wchar_t szError[bsiz];
1342 os_swprintf(szError, bsiz, _W("%ls: Can not transpose multiple elements.\n").c_str(), L"Transpose");
1343 CoverageInstance::stopChrono((void*)&e);
1344 throw InternalError(szError, 999, e.getLocation());
1347 types::InternalType * pValue = getResult();
1348 types::InternalType * pReturn = NULL;
1349 const bool bConjug = e.getConjugate() == TransposeExp::_Conjugate_;
1351 if ((bConjug && pValue->adjoint(pReturn)) || (!bConjug && pValue->transpose(pReturn)))
1353 if (pValue != pReturn)
1359 CoverageInstance::stopChrono((void*)&e);
1365 // transpose returned false so the negation is not possible so we call the overload (%foo_t or %foo_0)
1366 types::typed_list in;
1367 types::typed_list out;
1369 pValue->IncreaseRef();
1370 in.push_back(pValue);
1372 types::Callable::ReturnValue Ret;
1375 Ret = Overload::call(L"%" + getResult()->getShortTypeStr() + L"_t", in, 1, out, true);
1379 Ret = Overload::call(L"%" + getResult()->getShortTypeStr() + L"_0", in, 1, out, true);
1382 if (Ret != types::Callable::OK)
1384 cleanInOut(in, out);
1385 CoverageInstance::stopChrono((void*)&e);
1386 throw InternalError(ConfigVariable::getLastErrorMessage(), ConfigVariable::getLastErrorNumber(), e.getLocation());
1393 CoverageInstance::stopChrono((void*)&e);
1397 void RunVisitorT<T>::visitprivate(const FunctionDec & e)
1399 CoverageInstance::invokeAndStartChrono((void*)&e);
1400 symbol::Context* ctx = symbol::Context::getInstance();
1406 // funcprot(0) : do nothing
1407 // funcprot(1) && warning(on) : warning
1408 //get input parameters list
1409 std::list<symbol::Variable*> *pVarList = new std::list<symbol::Variable*>();
1410 const exps_t & vars = e.getArgs().getVars();
1411 for (const auto var : vars)
1413 pVarList->push_back(var->getAs<SimpleVar>()->getStack());
1416 //get output parameters list
1417 std::list<symbol::Variable*> *pRetList = new std::list<symbol::Variable*>();
1418 const exps_t & rets = e.getReturns().getVars();
1419 for (const auto ret : rets)
1421 pRetList->push_back(ret->getAs<SimpleVar>()->getStack());
1424 types::Macro *pMacro = new types::Macro(e.getSymbol().getName(), *pVarList, *pRetList,
1425 const_cast<SeqExp&>(static_cast<const SeqExp&>(e.getBody())), L"script");
1426 pMacro->setLines(e.getLocation().first_line, e.getLocation().last_line);
1428 if (ctx->isprotected(symbol::Symbol(pMacro->getName())))
1431 std::wostringstream os;
1432 os << _W("Redefining permanent variable.\n");
1433 CoverageInstance::stopChrono((void*)&e);
1434 throw InternalError(os.str(), 999, e.getLocation());
1437 if (ctx->addMacro(pMacro) == false)
1439 char pstError[1024];
1440 char* pstFuncName = wide_string_to_UTF8(e.getSymbol().getName().c_str());
1441 os_sprintf(pstError, _("It is not possible to redefine the %s primitive this way (see clearfun).\n"), pstFuncName);
1442 wchar_t* pwstError = to_wide_string(pstError);
1443 std::wstring wstError(pwstError);
1447 CoverageInstance::stopChrono((void*)&e);
1448 throw InternalError(wstError, 999, e.getLocation());
1451 CoverageInstance::stopChrono((void*)&e);
1455 void RunVisitorT<T>::visitprivate(const ListExp &e)
1457 CoverageInstance::invokeAndStartChrono((void*)&e);
1460 e.getStart().accept(*this);
1462 catch (ScilabException &)
1464 CoverageInstance::stopChrono((void*)&e);
1468 types::GenericType* pITStart = static_cast<types::GenericType*>(getResult());
1469 if (pITStart == NULL ||
1470 ((pITStart->getSize() != 1 || (pITStart->isDouble() && pITStart->getAs<types::Double>()->isComplex())) &&
1471 pITStart->isList() == false)) // list case => call overload
1479 wchar_t szError[bsiz];
1480 os_swprintf(szError, bsiz, _W("%ls: Wrong type for argument %d: Real scalar expected.\n").c_str(), L"':'", 1);
1481 CoverageInstance::stopChrono((void*)&e);
1482 throw InternalError(szError, 999, e.getLocation());
1484 types::InternalType * piStart = pITStart;
1488 e.getStep().accept(*this);
1490 catch (ScilabException &)
1492 CoverageInstance::stopChrono((void*)&e);
1495 types::GenericType* pITStep = static_cast<types::GenericType*>(getResult());
1497 if (pITStep == NULL ||
1498 ((pITStep->getSize() != 1 || (pITStep->isDouble() && pITStep->getAs<types::Double>()->isComplex())) &&
1499 pITStep->isList() == false)) // list case => call overload
1508 wchar_t szError[bsiz];
1509 os_swprintf(szError, bsiz, _W("%ls: Wrong type for argument %d: Real scalar expected.\n").c_str(), L"':'", 2);
1510 CoverageInstance::stopChrono((void*)&e);
1511 throw InternalError(szError, 999, e.getLocation());
1513 types::InternalType* piStep = pITStep;
1517 e.getEnd().accept(*this);
1519 catch (ScilabException &)
1521 CoverageInstance::stopChrono((void*)&e);
1525 types::GenericType* pITEnd = static_cast<types::GenericType*>(getResult());
1527 if (pITEnd == NULL ||
1528 ((pITEnd->getSize() != 1 || (pITEnd->isDouble() && pITEnd->getAs<types::Double>()->isComplex())) &&
1529 pITEnd->isList() == false)) // list case => call overload
1539 wchar_t szError[bsiz];
1540 os_swprintf(szError, bsiz, _W("%ls: Wrong type for argument %d: Real scalar expected.\n").c_str(), L"':'", 3);
1541 CoverageInstance::stopChrono((void*)&e);
1542 throw InternalError(szError, 999, e.getLocation());
1544 types::InternalType* piEnd = pITEnd;
1546 ////check if implicitlist is 1:$ to replace by ':'
1547 //if (piStart->isDouble() && piStep->isDouble() && piEnd->isPoly())
1549 // if (piStart->getAs<Double>()->get()[0] == 1 && piStep->getAs<Double>()->get()[0] == 1)
1551 // SinglePoly* end = piEnd->getAs<Polynom>()->get()[0];
1552 // if (end->getRank() == 1 && end->get()[0] == 0 && end->get()[1] == 1)
1554 // setResult(new Colon());
1560 //check compatibility
1561 // double : double : double or poly : poly : poly and mix like double : double : poly
1562 if ((piStart->isPoly() || piStart->isDouble()) &&
1563 (piStep->isPoly() || piStep->isDouble()) &&
1564 (piEnd->isPoly() || piEnd->isDouble()))
1566 // No need to kill piStart, ... because Implicit list ctor will incref them
1567 setResult(new types::ImplicitList(piStart, piStep, piEnd));
1568 CoverageInstance::stopChrono((void*)&e);
1572 // int : double or int : int
1573 if (piStart->isInt() &&
1574 (piStep->isDouble() || piStep->isInt()) &&
1577 // check for same int type int8, int 16 ...
1578 if (piStart->getType() == piEnd->getType() &&
1579 (piStart->getType() == piStep->getType() ||
1580 piStep->isDouble()))
1582 // No need to kill piStart, ... because Implicit list ctor will incref them
1583 setResult(new types::ImplicitList(piStart, piStep, piEnd));
1584 CoverageInstance::stopChrono((void*)&e);
1590 types::Callable::ReturnValue Ret;
1591 types::typed_list in;
1592 types::typed_list out;
1594 piStart->IncreaseRef();
1595 in.push_back(piStart);
1599 if (e.hasExplicitStep())
1602 //call overload %typeStart_b_typeStep
1603 piStep->IncreaseRef();
1604 in.push_back(piStep);
1605 piEnd->IncreaseRef();
1606 in.push_back(piEnd);
1607 Ret = Overload::call(L"%" + piStart->getShortTypeStr() + L"_b_" + piStep->getShortTypeStr(), in, 1, out, true);
1612 //call overload %typeStart_b_typeEnd
1614 piEnd->IncreaseRef();
1615 in.push_back(piEnd);
1616 Ret = Overload::call(L"%" + piStart->getShortTypeStr() + L"_b_" + piEnd->getShortTypeStr(), in, 1, out, true);
1619 catch (const InternalError& error)
1622 cleanInOut(in, out);
1623 CoverageInstance::stopChrono((void*)&e);
1627 if (Ret != types::Callable::OK)
1630 cleanInOut(in, out);
1631 CoverageInstance::stopChrono((void*)&e);
1632 throw InternalError(ConfigVariable::getLastErrorMessage(), ConfigVariable::getLastErrorNumber(), e.getLocation());
1637 CoverageInstance::stopChrono((void*)&e);
1641 void RunVisitorT<T>::visitprivate(const OptimizedExp &e)
1646 void RunVisitorT<T>::visitprivate(const MemfillExp &e)
1648 CoverageInstance::invokeAndStartChrono((void*)&e);
1651 e.getOriginal()->accept(*this);
1653 catch (ScilabException &)
1655 CoverageInstance::stopChrono((void*)&e);
1661 void RunVisitorT<T>::visitprivate(const DAXPYExp &e)
1663 CoverageInstance::invokeAndStartChrono((void*)&e);
1664 types::InternalType* pIT = NULL;
1665 types::Double* ad = NULL;
1669 types::Double* xd = NULL;
1673 types::Double* yd = NULL;
1677 //check types and dimensions
1680 const Exp &ye = e.getY();
1685 catch (ScilabException &)
1687 CoverageInstance::stopChrono((void*)&e);
1692 if (pIT->isDouble())
1694 yd = pIT->getAs<types::Double>();
1695 if (yd->getDims() == 2 && yd->isComplex() == false)
1705 e.getOriginal()->accept(*this);
1707 catch (ScilabException &)
1709 CoverageInstance::stopChrono((void*)&e);
1712 CoverageInstance::stopChrono((void*)&e);
1721 e.getOriginal()->accept(*this);
1723 catch (ScilabException &)
1725 CoverageInstance::stopChrono((void*)&e);
1728 CoverageInstance::stopChrono((void*)&e);
1733 const Exp &xe = e.getX();
1738 catch (ScilabException &)
1740 CoverageInstance::stopChrono((void*)&e);
1745 if (pIT->isDouble())
1747 xd = pIT->getAs<types::Double>();
1748 if (xd->isScalar() && xd->isComplex() == false)
1755 else if (xd->getDims() == 2 && xd->isComplex() == false)
1766 e.getOriginal()->accept(*this);
1768 catch (ScilabException &)
1770 CoverageInstance::stopChrono((void*)&e);
1773 CoverageInstance::stopChrono((void*)&e);
1783 e.getOriginal()->accept(*this);
1785 catch (ScilabException &)
1787 CoverageInstance::stopChrono((void*)&e);
1790 CoverageInstance::stopChrono((void*)&e);
1794 const Exp &ae = e.getA();
1799 catch (ScilabException &)
1801 CoverageInstance::stopChrono((void*)&e);
1806 if (pIT->isDouble())
1810 xd = pIT->getAs<types::Double>();
1811 //X is scalar it become A
1813 if (xd->getDims() == 2 && xd->isComplex() == false)
1825 e.getOriginal()->accept(*this);
1827 catch (ScilabException &)
1829 CoverageInstance::stopChrono((void*)&e);
1832 CoverageInstance::stopChrono((void*)&e);
1838 //a is a and it must be scalar
1839 ad = pIT->getAs<types::Double>();
1840 if (/*ad->isScalar() && */ad->isComplex() == false)
1842 ar = ad->getRows(); //1;
1843 ac = ad->getCols();//1;
1852 e.getOriginal()->accept(*this);
1854 catch (ScilabException &)
1856 CoverageInstance::stopChrono((void*)&e);
1871 e.getOriginal()->accept(*this);
1873 catch (ScilabException &)
1875 CoverageInstance::stopChrono((void*)&e);
1878 CoverageInstance::stopChrono((void*)&e);
1882 // If we get here we are certain that ad, xd & yd have been set
1891 //Double* od = (Double*)yd->clone();
1892 C2F(daxpy)(&size, ad->get(), xd->get(), &one, yd->get(), &one);
1897 CoverageInstance::stopChrono((void*)&e);
1900 else if (ac == xr && ar == yr && xc == yc)
1904 C2F(dgemm)(&n, &n, &ar, &xc, &ac, &one, ad->get(), &ar, xd->get(), &ac, &one, yd->get(), &ar);
1907 CoverageInstance::stopChrono((void*)&e);
1917 e.getOriginal()->accept(*this);
1919 catch (ScilabException &)
1921 CoverageInstance::stopChrono((void*)&e);
1924 CoverageInstance::stopChrono((void*)&e);
1930 void RunVisitorT<T>::visitprivate(const TryCatchExp &e)
1932 CoverageInstance::invokeAndStartChrono((void*)&e);
1933 //save current prompt mode
1934 bool oldVal = ConfigVariable::isSilentError();
1935 int oldMode = ConfigVariable::getPromptMode();
1936 //set mode silent for errors
1937 ConfigVariable::setSilentError(true);
1939 symbol::Context* pCtx = symbol::Context::getInstance();
1942 int scope = pCtx->getScopeLevel();
1943 int level = ConfigVariable::getRecursionLevel();
1946 const_cast<Exp*>(&e.getTry())->setReturnable();
1947 e.getTry().accept(*this);
1948 //restore previous prompt mode
1949 ConfigVariable::setSilentError(oldVal);
1951 if (e.getTry().isReturn())
1953 const_cast<Exp*>(&e.getTry())->resetReturn();
1954 const_cast<TryCatchExp*>(&e)->setReturn();
1957 catch (const RecursionException& /* re */)
1959 ConfigVariable::setPromptMode(oldMode);
1961 //close opened scope during try
1962 while (pCtx->getScopeLevel() > scope)
1967 //decrease recursion to init value and close where
1968 while (ConfigVariable::getRecursionLevel() > level)
1970 ConfigVariable::where_end();
1971 ConfigVariable::decreaseRecursion();
1974 //print msg about recursion limit and trigger an error
1976 os_swprintf(sz, 1024, _W("Recursion limit reached (%d).\n").data(), ConfigVariable::getRecursionLimit());
1977 CoverageInstance::stopChrono((void*)&e);
1978 throw ast::InternalError(sz);
1982 catch (const InternalError& /* ie */)
1984 //restore previous prompt mode
1985 ConfigVariable::setSilentError(oldVal);
1987 ConfigVariable::setLastErrorCall();
1988 // reset call stack filled when error occured
1989 ConfigVariable::resetWhereError();
1992 const_cast<Exp*>(&e.getCatch())->setReturnable();
1993 e.getCatch().accept(*this);
1994 if (e.getCatch().isReturn())
1996 const_cast<Exp*>(&e.getCatch())->resetReturn();
1997 const_cast<TryCatchExp*>(&e)->setReturn();
2000 catch (ScilabException &)
2002 CoverageInstance::stopChrono((void*)&e);
2006 CoverageInstance::stopChrono((void*)&e);
2010 } /* namespace ast */
2012 #include "run_SeqExp.hpp"
2013 #include "run_CallExp.hpp"
2014 #include "run_MatrixExp.hpp"
2015 #include "run_OpExp.hpp"
2016 #include "run_AssignExp.hpp"
2018 template EXTERN_AST class ast::RunVisitorT<ast::ExecVisitor>;
2019 template EXTERN_AST class ast::RunVisitorT<ast::StepVisitor>;
2020 template EXTERN_AST class ast::RunVisitorT<ast::TimedVisitor>;
2021 template EXTERN_AST class ast::RunVisitorT<ast::DebuggerVisitor>;