|
| 1 | +//============================================================================= |
| 2 | +// Copyright (c) 2016-2018 Allan CORNET (Nelson) |
| 3 | +//============================================================================= |
| 4 | +// LICENCE_BLOCK_BEGIN |
| 5 | +// This program is free software: you can redistribute it and/or modify |
| 6 | +// it under the terms of the GNU General Public License as published by |
| 7 | +// the Free Software Foundation, either version 2 of the License, or |
| 8 | +// (at your option) any later version. |
| 9 | +// |
| 10 | +// This program is distributed in the hope that it will be useful, |
| 11 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +// GNU General Public License for more details. |
| 14 | +// |
| 15 | +// You should have received a copy of the GNU General Public License |
| 16 | +// along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | +// LICENCE_BLOCK_END |
| 18 | +//============================================================================= |
| 19 | +#include "castBuiltin.hpp" |
| 20 | +#include "Error.hpp" |
| 21 | +#include "Cast.hpp" |
| 22 | +#include "StringToClass.hpp" |
| 23 | +#include "OverloadFunction.hpp" |
| 24 | +//============================================================================= |
| 25 | +using namespace Nelson; |
| 26 | +//============================================================================= |
| 27 | +ArrayOfVector Nelson::ElementaryFunctionsGateway::castBuiltin(Evaluator* eval, int nLhs, const ArrayOfVector& argIn) |
| 28 | +{ |
| 29 | + ArrayOfVector retval; |
| 30 | + if (argIn.size() < 2 || argIn.size() > 3) |
| 31 | + { |
| 32 | + Error(eval, ERROR_WRONG_NUMBERS_INPUT_ARGS); |
| 33 | + } |
| 34 | + if (nLhs > 1) |
| 35 | + { |
| 36 | + Error(eval, ERROR_WRONG_NUMBERS_OUTPUT_ARGS); |
| 37 | + } |
| 38 | + bool bSuccess = false; |
| 39 | + retval = OverloadFunction(eval, nLhs, argIn, bSuccess); |
| 40 | + if (!bSuccess) |
| 41 | + { |
| 42 | + bool isSparse = false; |
| 43 | + Class destinationClass; |
| 44 | + if (argIn.size() == 2) |
| 45 | + { |
| 46 | + ArrayOf param2 = argIn[1]; |
| 47 | + std::wstring dest = param2.getContentAsWideString(); |
| 48 | + if (eval->getOverloadState()) |
| 49 | + { |
| 50 | + Context *context = eval->getContext(); |
| 51 | + FunctionDef *funcDef = nullptr; |
| 52 | + if (context->lookupFunction(dest, funcDef)) |
| 53 | + { |
| 54 | + if ((funcDef->type() == NLS_BUILT_IN_FUNCTION) || (funcDef->type() == NLS_MACRO_FUNCTION)) |
| 55 | + { |
| 56 | + bSuccess = true; |
| 57 | + ArrayOfVector argInCopy; |
| 58 | + argInCopy.push_back(argIn[0]); |
| 59 | + return funcDef->evaluateFunction(eval, argInCopy, nLhs); |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + destinationClass = StringToClass(dest); |
| 64 | + } |
| 65 | + else |
| 66 | + { |
| 67 | + ArrayOf param2 = argIn[1]; |
| 68 | + std::wstring like = param2.getContentAsWideString(); |
| 69 | + if (like != L"like") |
| 70 | + { |
| 71 | + Error(eval, ERROR_WRONG_ARGUMENT_2_VALUE); |
| 72 | + } |
| 73 | + destinationClass = argIn[2].getDataClass(); |
| 74 | + isSparse = argIn[2].isSparse(); |
| 75 | + } |
| 76 | + retval.push_back(Cast(argIn[0], destinationClass, isSparse)); |
| 77 | + } |
| 78 | + return retval; |
| 79 | +} |
| 80 | +//============================================================================= |
0 commit comments