Skip to content

Commit

Permalink
fix #489 in progress (first step ...)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nelson-numerical-software committed Aug 10, 2021
1 parent 3b63b26 commit 3b26ac3
Show file tree
Hide file tree
Showing 14 changed files with 200 additions and 32 deletions.
Expand Up @@ -181,6 +181,7 @@
<ClCompile Include="..\cpp\conjBuiltin.cpp" />
<ClCompile Include="..\cpp\dec2baseBuiltin.cpp" />
<ClCompile Include="..\cpp\dispBuiltin.cpp" />
<ClCompile Include="..\cpp\displayBuiltin.cpp" />
<ClCompile Include="..\cpp\expBuiltin.cpp" />
<ClCompile Include="..\cpp\findBuiltin.cpp" />
<ClCompile Include="..\cpp\fixBuiltin.cpp" />
Expand Down
Expand Up @@ -162,6 +162,9 @@
<ClCompile Include="..\cpp\signBuiltin.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\cpp\displayBuiltin.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<Text Include="..\..\CMakeLists.txt" />
Expand Down
3 changes: 3 additions & 0 deletions modules/elementary_functions/builtin/cpp/Gateway.cpp
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion modules/elementary_functions/builtin/cpp/dispBuiltin.cpp
Expand Up @@ -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;
}
//=============================================================================
72 changes: 72 additions & 0 deletions 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 <http://www.gnu.org/licenses/>.
// 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;
}
//=============================================================================
40 changes: 40 additions & 0 deletions 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 <http://www.gnu.org/licenses/>.
// 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
//=============================================================================
30 changes: 30 additions & 0 deletions 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 <http://www.gnu.org/licenses/>.
% 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'')')
37 changes: 9 additions & 28 deletions modules/interpreter/src/cpp/Evaluator.cpp
Expand Up @@ -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) {
Expand All @@ -1797,7 +1798,6 @@ Evaluator::assignStatement(AbstractSyntaxTreePtr t, bool printIt)
}
}
if (printIt) {
io->outputMessage(variableName + " =\n\n");
OverloadDisplay(this, b);
}
if (ticProfiling != 0) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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);
}
}
Expand Down Expand Up @@ -2484,16 +2483,15 @@ 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."));
}
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;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -3138,15 +3126,7 @@ Evaluator::functionExpression(
delete[] argTypeMap;
argTypeMap = nullptr;
}
// C
return scalarArrayOfToArrayOfVector(r);
/*
if (r.isFunctionHandle())
{
return
scalarArrayOfToArrayOfVector(r);
}
*/
}
}
}
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions modules/interpreter/src/cpp/MacroFunctionDef.cpp
Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion modules/interpreter/src/cpp/NelsonLexer.cpp
Expand Up @@ -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;
Expand Down
12 changes: 10 additions & 2 deletions modules/interpreter/src/cpp/Scope.cpp
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions modules/stream_manager/src/cpp/DisplayVariable.cpp
Expand Up @@ -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);
Expand Down
14 changes: 14 additions & 0 deletions modules/types/src/cpp/ArrayOf.cpp
Expand Up @@ -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 {
Expand Down Expand Up @@ -482,6 +495,7 @@ ArrayOf::operator=(const ArrayOf& copy)
if (this == &copy) {
return;
}
this->_name = copy._name;
deleteContents();
if (copy.dp) {
dp = copy.dp->getCopy();
Expand Down

0 comments on commit 3b26ac3

Please sign in to comment.