2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2014 - Scilab Enterprises - Antoine ELIAS
5 * This file must be used under the terms of the CeCILL.
6 * This source file is licensed as described in the file COPYING, which
7 * you should have received as part of this distribution. The terms
8 * are also available at
9 * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
13 //file included in runvisitor.cpp
17 void RunVisitorT<T>::visitprivate(const CallExp &e)
19 e.getName().accept(*this);
20 types::InternalType* pIT = getResult();
25 types::typed_list out;
27 types::optional_list opt;
29 int iRetCount = getExpectedSize();
30 int iSaveExpectedSize = iRetCount;
32 // manage case [a,b]=foo() where foo is defined as a=foo()
33 if (pIT->getInvokeNbOut() != -1 && pIT->getInvokeNbOut() < iRetCount)
36 std::wostringstream os;
37 os << _W("Wrong number of output arguments.\n") << std::endl;
38 throw ast::ScilabError(os.str(), 999, e.getLocation());
41 //get function arguments
42 exps_t args = e.getArgs();
45 if (arg->isAssignExp())
47 AssignExp* pAssign = static_cast<AssignExp*>(arg);
49 Exp* pL = &pAssign->getLeftExp();
50 if (!pL->isSimpleVar())
56 std::wostringstream os;
57 os << _W("left side of optional parameter must be a variable") << std::endl;
58 throw ast::ScilabError(os.str(), 999, e.getLocation());
61 SimpleVar* pVar = pL->getAs<SimpleVar>();
62 Exp* pR = &pAssign->getRightExp();
64 InternalType* pITR = getResult();
65 // IncreaseRef to protect opt argument of scope_end delete
66 // It will be deleted by clear_opt
69 if (pIT->hasInvokeOption())
71 opt.push_back(std::pair<std::wstring, InternalType*>(pVar->getSymbol().getName(), pITR));
72 //in case of macro/macrofile, we have to shift input param
73 //so add NULL item in in list to keep initial order
74 if (pIT->isMacro() || pIT->isMacroFile())
88 int iSize = getExpectedSize();
91 setExpectedSize(iSize);
93 if (getResult() == NULL)
95 //special case for empty extraction of list ( list()(:) )
99 InternalType * pITArg = getResult();
100 if (pITArg->isImplicitList())
102 types::ImplicitList* pIL = pITArg->getAs<types::ImplicitList>();
103 if (pIL->isComputable())
105 setResult(pIL->extractFullMatrix());
110 if (isSingleResult())
112 in.push_back(getResult());
113 getResult()->IncreaseRef();
118 for (int i = 0 ; i < getResultSize() ; i++)
120 InternalType * pITArg = getResult(i);
121 pITArg->IncreaseRef();
122 in.push_back(pITArg);
131 // Extraction with a List in input argument.
132 // This extraction must be a recursive extract.
134 types::List* pListArg = NULL;
135 if (pIT->isCallable() == false && in.size() == 1 && in[0]->isList())
137 pListArg = in[0]->getAs<types::List>();
138 iLoopSize = pListArg->getSize();
142 setExpectedSize(iSaveExpectedSize);
143 iRetCount = std::max(1, iRetCount);
145 for (int i = 0; i < iLoopSize; i++)
149 in[0] = pListArg->get(i);
153 if (pIT->isCallable())
155 // list used like "varargin"
156 types::List* pLFuncArgs = in[0]->getAs<types::List>();
157 types::typed_list input;
158 for (int j = 0; j < pLFuncArgs->getSize(); j++)
160 input.push_back(pLFuncArgs->get(j));
161 input.back()->IncreaseRef();
168 pListArg->DecreaseRef();
171 std::wostringstream os;
172 os << _W("Invalid index.\n");
173 throw ast::ScilabError(os.str(), 999, e.getFirstLocation());
178 in[0]->IncreaseRef();
183 if(pIT->isInvokable() == false)
186 ret = Overload::call(L"%" + pIT->getShortTypeStr() + L"_e", in, iRetCount, out, this);
190 ret = pIT->invoke(in, opt, iRetCount, out, *this, e);
191 if(ret == false && pIT->isUserType())
194 ret = Overload::call(L"%" + pIT->getShortTypeStr() + L"_e", in, iRetCount, out, this);
200 if (iSaveExpectedSize != -1 && iSaveExpectedSize > out.size())
202 std::wostringstream os;
203 os << _W("bad lhs, expected : ") << iRetCount << _W(" returned : ") << out.size() << std::endl;
204 throw ScilabError(os.str(), 999, e.getLocation());
207 setExpectedSize(iSaveExpectedSize);
212 // In case a.b(), getResult contain pIT ("b").
213 // If out == pIT, do not delete it.
214 if (getResult() != pIT)
216 // protect element of out in case where
217 // out contain elements of pIT
218 for (int i = 0; i < out.size(); i++)
220 out[i]->IncreaseRef();
226 for (int i = 0; i < out.size(); i++)
228 out[i]->DecreaseRef();
232 if (pListArg && i + 1 != iLoopSize)
241 std::wostringstream os;
242 os << _W("Invalid index.\n");
243 throw ast::ScilabError(os.str(), 999, e.getFirstLocation());
249 pListArg->DecreaseRef();
253 catch (ScilabMessage & sm)
255 setExpectedSize(iSaveExpectedSize);
256 if(pIT != getResult())
265 if (pIT->isCallable())
267 Callable *pCall = pIT->getAs<Callable>();
268 if (ConfigVariable::getLastErrorFunction() == L"")
270 ConfigVariable::setLastErrorFunction(pCall->getName());
273 if (pCall->isMacro() || pCall->isMacroFile())
275 wchar_t szError[bsiz];
276 os_swprintf(szError, bsiz, _W("at line % 5d of function %ls called by :\n").c_str(), sm.GetErrorLocation().first_line, pCall->getName().c_str());
277 throw ScilabMessage(szError);
283 catch (InternalAbort & ia)
285 setExpectedSize(iSaveExpectedSize);
286 if(pIT != getResult())
297 catch (ScilabError & se)
299 setExpectedSize(iSaveExpectedSize);
300 if(pIT != getResult())
315 void RunVisitorT<T>::visitprivate(const CellCallExp &e)
319 e.getName().accept(execMeCell);
321 if (execMeCell.getResult() != NULL)
323 //a{xxx} with a variable, extraction
324 types::InternalType *pIT = NULL;
326 pIT = execMeCell.getResult();
331 if (pIT->isCell() == false)
333 throw ast::ScilabError(_W("[error] Cell contents reference from a non-cell array object.\n"), 999, e.getFirstLocation());
335 //Create list of indexes
336 ast::exps_t exps = e.getArgs();
337 types::typed_list *pArgs = GetArgumentList(exps);
339 types::List* pList = pIT->getAs<types::Cell>()->extractCell(pArgs);
344 std::wostringstream os;
345 os << _W("inconsistent row/column dimensions\n");
346 //os << ((*e.args_get().begin())->getLocation()).getLocationString() << std::endl;
347 throw ast::ScilabError(os.str(), 999, e.getFirstLocation());
350 if (pList->getSize() == 1)
352 InternalType* ret = pList->get(0);
365 //clean pArgs return by GetArgumentList
366 for (int iArg = 0 ; iArg < (int)pArgs->size() ; iArg++)
368 (*pArgs)[iArg]->killMe();
375 //result == NULL ,variable doesn't exist :(
376 // Sould never be in this case
377 // In worst case variable pointing to function does not exists
378 // visitprivate(SimpleVar) will throw the right exception.
382 } /* namespace ast */