From 3b26ac3827b97b04531ad0871c54dd75ff88d1fc Mon Sep 17 00:00:00 2001 From: Allan CORNET Date: Tue, 10 Aug 2021 21:20:12 +0200 Subject: [PATCH] fix #489 in progress (first step ...) --- .../c/nlsElementary_functions_builtin.vcxproj | 1 + ...ementary_functions_builtin.vcxproj.filters | 3 + .../builtin/cpp/Gateway.cpp | 3 + .../builtin/cpp/dispBuiltin.cpp | 4 +- .../builtin/cpp/displayBuiltin.cpp | 72 +++++++++++++++++++ .../builtin/include/displayBuiltin.hpp | 40 +++++++++++ .../elementary_functions/tests/test_display.m | 30 ++++++++ modules/interpreter/src/cpp/Evaluator.cpp | 37 +++------- .../interpreter/src/cpp/MacroFunctionDef.cpp | 1 + modules/interpreter/src/cpp/NelsonLexer.cpp | 3 +- modules/interpreter/src/cpp/Scope.cpp | 12 +++- .../src/cpp/DisplayVariable.cpp | 6 ++ modules/types/src/cpp/ArrayOf.cpp | 14 ++++ modules/types/src/include/ArrayOf.hpp | 6 ++ 14 files changed, 200 insertions(+), 32 deletions(-) create mode 100644 modules/elementary_functions/builtin/cpp/displayBuiltin.cpp create mode 100644 modules/elementary_functions/builtin/include/displayBuiltin.hpp create mode 100644 modules/elementary_functions/tests/test_display.m diff --git a/modules/elementary_functions/builtin/c/nlsElementary_functions_builtin.vcxproj b/modules/elementary_functions/builtin/c/nlsElementary_functions_builtin.vcxproj index a917d01ec9..14ae646e1a 100644 --- a/modules/elementary_functions/builtin/c/nlsElementary_functions_builtin.vcxproj +++ b/modules/elementary_functions/builtin/c/nlsElementary_functions_builtin.vcxproj @@ -181,6 +181,7 @@ + diff --git a/modules/elementary_functions/builtin/c/nlsElementary_functions_builtin.vcxproj.filters b/modules/elementary_functions/builtin/c/nlsElementary_functions_builtin.vcxproj.filters index c91bc24409..d72df2ccc8 100644 --- a/modules/elementary_functions/builtin/c/nlsElementary_functions_builtin.vcxproj.filters +++ b/modules/elementary_functions/builtin/c/nlsElementary_functions_builtin.vcxproj.filters @@ -162,6 +162,9 @@ Source Files + + Source Files + diff --git a/modules/elementary_functions/builtin/cpp/Gateway.cpp b/modules/elementary_functions/builtin/cpp/Gateway.cpp index 1281b331ff..89f18159b6 100644 --- a/modules/elementary_functions/builtin/cpp/Gateway.cpp +++ b/modules/elementary_functions/builtin/cpp/Gateway.cpp @@ -30,6 +30,7 @@ #include "complexBuiltin.hpp" #include "conjBuiltin.hpp" #include "dispBuiltin.hpp" +#include "displayBuiltin.hpp" #include "fixBuiltin.hpp" #include "floorBuiltin.hpp" #include "imagBuiltin.hpp" @@ -79,6 +80,8 @@ const std::wstring gatewayName = L"elementary_functions"; static const nlsGateway gateway[] = { { "disp", (void*)Nelson::ElementaryFunctionsGateway::dispBuiltin, 0, 1, CPP_BUILTIN_WITH_EVALUATOR }, + { "display", (void*)Nelson::ElementaryFunctionsGateway::displayBuiltin, 0, 2, + CPP_BUILTIN_WITH_EVALUATOR }, { "size", (void*)Nelson::ElementaryFunctionsGateway::sizeBuiltin, -1, 2, CPP_BUILTIN_WITH_EVALUATOR }, { "length", (void*)Nelson::ElementaryFunctionsGateway::lengthBuiltin, 1, 1, diff --git a/modules/elementary_functions/builtin/cpp/dispBuiltin.cpp b/modules/elementary_functions/builtin/cpp/dispBuiltin.cpp index dbe4b16941..b61b84ed9a 100644 --- a/modules/elementary_functions/builtin/cpp/dispBuiltin.cpp +++ b/modules/elementary_functions/builtin/cpp/dispBuiltin.cpp @@ -36,7 +36,9 @@ Nelson::ElementaryFunctionsGateway::dispBuiltin( ArrayOfVector retval; nargincheck(argIn, 1, 1); nargoutcheck(nLhs, 0, 0); - OverloadDisplay(eval, argIn[0], true); + ArrayOf variable = argIn[0]; + variable.name(""); + OverloadDisplay(eval, variable, true); return retval; } //============================================================================= diff --git a/modules/elementary_functions/builtin/cpp/displayBuiltin.cpp b/modules/elementary_functions/builtin/cpp/displayBuiltin.cpp new file mode 100644 index 0000000000..f0de76eded --- /dev/null +++ b/modules/elementary_functions/builtin/cpp/displayBuiltin.cpp @@ -0,0 +1,72 @@ +//============================================================================= +// Copyright (c) 2016-present Allan CORNET (Nelson) +//============================================================================= +// This file is part of the Nelson. +//============================================================================= +// LICENCE_BLOCK_BEGIN +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// Alternatively, you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation; either version 2 of +// the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this program. If not, see . +// LICENCE_BLOCK_END +//============================================================================= +#include "displayBuiltin.hpp" +#include "Error.hpp" +#include "OverloadDisplay.hpp" +//============================================================================= +using namespace Nelson; +//============================================================================= +ArrayOfVector +Nelson::ElementaryFunctionsGateway::displayBuiltin( + Evaluator* eval, int nLhs, const ArrayOfVector& argIn) +{ + ArrayOfVector retval; + nargincheck(argIn, 1, 2); + nargoutcheck(nLhs, 0, 0); + + ArrayOf variable = argIn[0]; + std::string variableName = variable.name(); + if (argIn.size() == 2) { + variableName = argIn[1].getContentAsCString(); + variable.name(variableName); + } + OverloadDisplay(eval, variable, true); + + /* + std::wstring variableName; + ArrayOf variableValue; + Interface* io = eval->getInterface(); + switch (argIn.size()) { + case 1: { + variableName = argIn[0].name(); + io->outputMessage(variableName + " = \n\n"); + OverloadDisplay(eval, argIn[0], true); + } break; + case 2: { + std::wstring variableName = argIn[1].getContentAsWideString(); + io->outputMessage(variableName + L" =\n\n"); + OverloadDisplay(eval, argIn[0], true); + } + + } break; + } + if (!variableName.empty()) { + io->outputMessage(variableName + L" =\n\n"); + } + */ + return retval; +} +//============================================================================= diff --git a/modules/elementary_functions/builtin/include/displayBuiltin.hpp b/modules/elementary_functions/builtin/include/displayBuiltin.hpp new file mode 100644 index 0000000000..cb4c178cae --- /dev/null +++ b/modules/elementary_functions/builtin/include/displayBuiltin.hpp @@ -0,0 +1,40 @@ +//============================================================================= +// Copyright (c) 2016-present Allan CORNET (Nelson) +//============================================================================= +// This file is part of the Nelson. +//============================================================================= +// LICENCE_BLOCK_BEGIN +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// Alternatively, you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation; either version 2 of +// the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this program. If not, see . +// LICENCE_BLOCK_END +//============================================================================= +#pragma once +//============================================================================= +#include "ArrayOf.hpp" +#include "Evaluator.hpp" +//============================================================================= +namespace Nelson { +namespace ElementaryFunctionsGateway { + //============================================================================= + ArrayOfVector + displayBuiltin(Evaluator* eval, int nLhs, const ArrayOfVector& argIn); + //============================================================================= +} +//============================================================================= +} // namespace Nelson +//============================================================================= diff --git a/modules/elementary_functions/tests/test_display.m b/modules/elementary_functions/tests/test_display.m new file mode 100644 index 0000000000..5e4af9226b --- /dev/null +++ b/modules/elementary_functions/tests/test_display.m @@ -0,0 +1,30 @@ +%============================================================================= +% Copyright (c) 2016-present Allan CORNET (Nelson) +%============================================================================= +% This file is part of the Nelson. +%============================================================================= +% LICENCE_BLOCK_BEGIN +% This program is free software; you can redistribute it and/or +% modify it under the terms of the GNU Lesser General Public +% License as published by the Free Software Foundation; either +% version 2.1 of the License, or (at your option) any later version. +% +% Alternatively, you can redistribute it and/or +% modify it under the terms of the GNU General Public License as +% published by the Free Software Foundation; either version 2 of +% the License, or (at your option) any later version. +% +% This program is distributed in the hope that it will be useful, +% but WITHOUT ANY WARRANTY; without even the implied warranty of +% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +% GNU Lesser General Public License for more details. +% +% You should have received a copy of the GNU Lesser General Public +% License along with this program. If not, see . +% LICENCE_BLOCK_END +%============================================================================= +assert_isequal(nargin('display'), 2) +assert_isequal(nargout('display'), 0) +%============================================================================= +X = 'Alice will be 12 this year.'; +R = evalc('display(X, ''YYY'')') \ No newline at end of file diff --git a/modules/interpreter/src/cpp/Evaluator.cpp b/modules/interpreter/src/cpp/Evaluator.cpp index 5bd086013b..07be360dda 100644 --- a/modules/interpreter/src/cpp/Evaluator.cpp +++ b/modules/interpreter/src/cpp/Evaluator.cpp @@ -1773,11 +1773,12 @@ Evaluator::assignStatement(AbstractSyntaxTreePtr t, bool printIt) uint64 ticProfiling = Profiler::getInstance()->tic(); bool isHandle = false; ArrayOf b = expression(t->right); + std::string variableName = t->text; + b.name(variableName); if (t->down != nullptr) { b = assignExpression(t, b); isHandle = b.isHandle(); } - std::string variableName = t->text; if (!isHandle) { ArrayOf* var = context->lookupVariable(variableName); if (var == nullptr) { @@ -1797,7 +1798,6 @@ Evaluator::assignStatement(AbstractSyntaxTreePtr t, bool printIt) } } if (printIt) { - io->outputMessage(variableName + " =\n\n"); OverloadDisplay(this, b); } if (ticProfiling != 0) { @@ -1836,9 +1836,9 @@ Evaluator::statementType(AbstractSyntaxTreePtr t, bool printIt) } else if (t->opNum == (OP_SCALL)) { ArrayOfVector m = specialFunctionCall(t->down, printIt); if (m.size() > 0) { - io->outputMessage(L"\nans =\n\n"); - OverloadDisplay(this, m[0]); + m[0].name("ans"); context->insertVariable("ans", m[0]); + OverloadDisplay(this, m[0]); } } else if (t->type == reserved_node) { switch (t->tokenNumber) { @@ -1912,7 +1912,7 @@ Evaluator::statementType(AbstractSyntaxTreePtr t, bool printIt) bUpdateAns = false; } if (printIt && (m.size() > 0) && (state < NLS_STATE_QUIT)) { - io->outputMessage(L"\nans =\n\n"); + b.name("ans"); OverloadDisplay(this, b); } } else if (t->opNum == OP_RHS) { @@ -1922,7 +1922,6 @@ Evaluator::statementType(AbstractSyntaxTreePtr t, bool printIt) } else { b = m[0]; if (printIt && (state < NLS_STATE_QUIT)) { - // io->outputMessage(L"ans =\n\n"); io->outputMessage("\n"); for (size_t j = 0; j < m.size(); j++) { if (m.size() > 1) { @@ -1938,7 +1937,7 @@ Evaluator::statementType(AbstractSyntaxTreePtr t, bool printIt) } else { b = expression(t); if (printIt && (state < NLS_STATE_QUIT)) { - io->outputMessage(L"\nans =\n\n"); + b.name("ans"); OverloadDisplay(this, b); } } @@ -2484,6 +2483,7 @@ Evaluator::multiFunctionCall(AbstractSyntaxTreePtr t, bool printIt) s = saveLHS; while ((s != nullptr) && (m.size() > 0)) { ArrayOf c(assignExpression(s->down, m)); + c.name(s->down->text); if (!context->insertVariable(s->down->text, c)) { if (IsValidVariableName(s->down->text, true)) { Error(_W("Redefining permanent variable.")); @@ -2491,9 +2491,7 @@ Evaluator::multiFunctionCall(AbstractSyntaxTreePtr t, bool printIt) Error(_W("Valid variable name expected.")); } if (printIt) { - io->outputMessage(s->down->text); - io->outputMessage(L" =\n\n"); - OverloadDisplay(this, c); + OverloadDisplay(this, c, false); } s = s->right; } @@ -3097,16 +3095,6 @@ Evaluator::functionExpression( } else { m = ArrayOfVector(); } - // int nRhs = (int)m.size(); - // int nLhs = funcDef->outputArgCount(); - /* - if ((funcDef->inputArgCount() >= 0) && - ((int)(m.size()) > funcDef->inputArgCount())) - Error(std::string("Too many inputs to function ")+t->text); - if ((funcDef->outputArgCount() >= 0) && - (narg_out > funcDef->outputArgCount() && !outputOptional)) - Error(std::string("Too many outputs to function ")+t->text); - */ CLIFlagsave = InCLI; InCLI = false; ArrayOf r; @@ -3138,15 +3126,7 @@ Evaluator::functionExpression( delete[] argTypeMap; argTypeMap = nullptr; } - // C return scalarArrayOfToArrayOfVector(r); - /* - if (r.isFunctionHandle()) - { - return - scalarArrayOfToArrayOfVector(r); - } - */ } } } @@ -3225,6 +3205,7 @@ Evaluator::functionExpression( if (ptrVar != nullptr) { ptrVar->setValue(c); } else { + c.name(variableName); if (!context->insertVariable(variableName, c)) { if (argTypeMap != nullptr) { delete[] argTypeMap; diff --git a/modules/interpreter/src/cpp/MacroFunctionDef.cpp b/modules/interpreter/src/cpp/MacroFunctionDef.cpp index 30d2c052c6..18a3358180 100644 --- a/modules/interpreter/src/cpp/MacroFunctionDef.cpp +++ b/modules/interpreter/src/cpp/MacroFunctionDef.cpp @@ -193,6 +193,7 @@ MacroFunctionDef::evaluateMFunction(Evaluator* eval, const ArrayOfVector& inputs for (i = 0; i < inputCount; i++) { dp[i] = inputs[i + minCount]; } + varg.name("varargin"); context->insertVariableLocally("varargin", varg); } context->getCurrentScope()->setNargOut(nargout); diff --git a/modules/interpreter/src/cpp/NelsonLexer.cpp b/modules/interpreter/src/cpp/NelsonLexer.cpp index 83eb9cb791..340763ab47 100644 --- a/modules/interpreter/src/cpp/NelsonLexer.cpp +++ b/modules/interpreter/src/cpp/NelsonLexer.cpp @@ -175,7 +175,8 @@ testSpecialFuncs() return false; } bool isHardcodedShorcut = isPathCommandShortCut(L"ls", wline) - || isPathCommandShortCut(L"cd", wline) || isPathCommandShortCut(L"dir", wline); + || isPathCommandShortCut(L"display", wline) || isPathCommandShortCut(L"cd", wline) + || isPathCommandShortCut(L"dir", wline); if (isHardcodedShorcut) { return true; diff --git a/modules/interpreter/src/cpp/Scope.cpp b/modules/interpreter/src/cpp/Scope.cpp index aaa87d70fe..097a78c271 100644 --- a/modules/interpreter/src/cpp/Scope.cpp +++ b/modules/interpreter/src/cpp/Scope.cpp @@ -80,13 +80,21 @@ Scope::lookupFunction(const std::string& funcName, FunctionDefPtr& val) ArrayOf* Scope::lookupVariable(const std::string& varName) { - return variablesTab.findVariable(varName); + ArrayOf* var = variablesTab.findVariable(varName); + if (var) { + var->name(varName); + } + return var; } //============================================================================= bool Scope::lookupVariable(const std::string& varName, ArrayOf& val) { - return variablesTab.findVariable(varName, val); + bool found = variablesTab.findVariable(varName, val); + if (found) { + val.name(varName); + } + return found; } //============================================================================= std::string diff --git a/modules/stream_manager/src/cpp/DisplayVariable.cpp b/modules/stream_manager/src/cpp/DisplayVariable.cpp index 875f45c5f1..3e0d4f7d46 100644 --- a/modules/stream_manager/src/cpp/DisplayVariable.cpp +++ b/modules/stream_manager/src/cpp/DisplayVariable.cpp @@ -46,6 +46,12 @@ DisplayVariable(Interface* io, const ArrayOf& A, bool fromDispBuiltin, bool& nee needToOverload = true; return; } + std::string variableName = A.name(); + if (!variableName.empty()) { + if (variableName == "ans") + io->outputMessage("\n"); + io->outputMessage(variableName + " =\n\n"); + } switch (A.getDataClass()) { case NLS_CELL_ARRAY: { DisplayCell(io, A, fromDispBuiltin, needToOverload); diff --git a/modules/types/src/cpp/ArrayOf.cpp b/modules/types/src/cpp/ArrayOf.cpp index ba324d52e2..0634dd0dd0 100644 --- a/modules/types/src/cpp/ArrayOf.cpp +++ b/modules/types/src/cpp/ArrayOf.cpp @@ -84,9 +84,22 @@ scalarArrayOfToArrayOfVector(ArrayOf a) return retval; } //============================================================================= +std::string +ArrayOf::name() const +{ + return _name; +} +//============================================================================= +void +ArrayOf::name(const std::string& name) +{ + _name = name; +} +//============================================================================= void ArrayOf::copyObject(const ArrayOf& copy) { + this->_name = copy._name; if (copy.dp) { dp = copy.dp->getCopy(); } else { @@ -482,6 +495,7 @@ ArrayOf::operator=(const ArrayOf& copy) if (this == ©) { return; } + this->_name = copy._name; deleteContents(); if (copy.dp) { dp = copy.dp->getCopy(); diff --git a/modules/types/src/include/ArrayOf.hpp b/modules/types/src/include/ArrayOf.hpp index 5f58c832e2..3660a0cb9e 100644 --- a/modules/types/src/include/ArrayOf.hpp +++ b/modules/types/src/include/ArrayOf.hpp @@ -93,6 +93,7 @@ class Data; class NLSTYPES_IMPEXP ArrayOf { private: + std::string _name; /** * This is a pointer to our data object - which is shared between * different ArrayOf objects. It is essentially the memory block @@ -138,6 +139,11 @@ class NLSTYPES_IMPEXP ArrayOf haveUniqueFieldNames(const stringVector& fieldnames); public: + std::string + name() const; + + void name(const std::string &name); + /** * Allocate an array. */