Skip to content

Commit

Permalink
extended numbers syntax about inspired by Rust (in progress)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nelson-numerical-software committed Oct 23, 2023
1 parent 86c5f62 commit 08ab300
Show file tree
Hide file tree
Showing 17 changed files with 439 additions and 119 deletions.
8 changes: 4 additions & 4 deletions modules/elementary_functions/tests/test_swapbytes.m
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@
assert_isequal(swapbytes(int64(-1)),int64(-1));
%=============================================================================
assert_isequal(swapbytes(uint64(12)), uint64(864691128455135232));
assert_isequal(swapbytes(uint64(5648002115885334530u)),uint64(144115188088201550u));
assert_isequal(swapbytes(5648002115885334530u64), 144115188088201550u64);
%=============================================================================
R = swapbytes([1u, 2u, 3u; 4u, 5u, 6u]);
REF = [72057594037927936u, 144115188075855872u, 216172782113783808u;
288230376151711744u, 360287970189639680u, 432345564227567616u];
R = swapbytes([1u64, 2u64, 3u64; 4u64, 5u64, 6u64]);
REF = [72057594037927936u64, 144115188075855872u64, 216172782113783808u64;
288230376151711744u64, 360287970189639680u64, 432345564227567616u64];
assert_isequal(R, REF);
%=============================================================================
msg = sprintf(_('Check for incorrect argument data type or missing argument in call to function ''%s''.'), 'swapbytes');
Expand Down
2 changes: 1 addition & 1 deletion modules/integer/tests/test_integer_minus.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
% LICENCE_BLOCK_END
%=============================================================================
R = (uint64(2^53) - 1);
REF = 9007199254740991u;
REF = 9007199254740991u64;
assert_isequal(R, REF);
%=============================================================================
R = (uint64(2^53) - 1) - (uint64(2^53) - 1000);
Expand Down
12 changes: 6 additions & 6 deletions modules/integer/tests/test_integer_plus.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,27 @@
% LICENCE_BLOCK_END
%=============================================================================
R = (uint64(2^53) + 1) + (uint64(2^53) + 1000);
REF = 18014398509482985u;
REF = 18014398509482985u64;
assert_isequal(R, REF);
%=============================================================================
R = (uint64(2^54) + 1) + (uint64(2^54) + 1000);
REF = 36028797018964969u;
REF = 36028797018964969u64;
assert_isequal(R, REF);
%=============================================================================
R = (uint64(2^57) + 1) + (uint64(2^57) + 1000);
REF = 288230376151712745u;
REF = 288230376151712745u64;
assert_isequal(R, REF);
%=============================================================================
R = (uint64(2^59) + 1) + (uint64(2^59) + 1000);
REF = 1152921504606847977u;
REF = 1152921504606847977u64;
assert_isequal(R, REF);
%=============================================================================
R = (int64(2^53) + 1) + (int64(2^53) + 1000);
REF = int64(18014398509482985u);
REF = int64(18014398509482985u64);
assert_isequal(R, REF);
%=============================================================================
R = (int64(2^54) + 1) + (int64(2^54) + 1000);
REF = int64(36028797018964969u);
REF = 36028797018964969i64;
assert_isequal(R, REF);
%=============================================================================
R = int8(127) + int8(127);
Expand Down
4 changes: 2 additions & 2 deletions modules/integer/tests/test_intmax.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
assert_isequal(intmax('int16'), int16(32767));
assert_isequal(intmax('uint16'), uint16(65535));
assert_isequal(intmax('uint32'), uint32(4294967295));
assert_isequal(intmax('int64'), int64(9223372036854775807U));
assert_isequal(intmax('uint64'), uint64(18446744073709551615U));
assert_isequal(intmax('int64'), int64(9223372036854775807i64));
assert_isequal(intmax('uint64'), uint64(18446744073709551615u64));
%=============================================================================
assert_checkerror('intmax(''uint64'', 3)', _('Wrong number of input arguments.'));
assert_checkerror('intmax(3)', _('Wrong type for argument #1: string expected.'));
Expand Down
4 changes: 2 additions & 2 deletions modules/integer/tests/test_uint64.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
% SPDX-License-Identifier: LGPL-3.0-or-later
% LICENCE_BLOCK_END
%=============================================================================
as_uint64 = 18446744073709551615U;
as_uint64 = 18446744073709551615u64;
max_uint64 = intmax('uint64');
assert_isequal(as_uint64, max_uint64);
%=============================================================================
A = 1;
B = 1U;
B = 1u64;
C = uint64(A);
assert_isequal(class(B), class(C));
assert_isequal(class(B), 'uint64');
Expand Down
4 changes: 2 additions & 2 deletions modules/interpreter/src/c/nlsInterpreter.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@
<ClCompile Include="..\cpp\AbstractSyntaxTreeHelpers.cpp" />
<ClCompile Include="..\cpp\ActionMenu.cpp" />
<ClCompile Include="..\cpp\AnonymousMacroFunctionDef.cpp" />
<ClCompile Include="..\cpp\AsciiToDouble.cpp" />
<ClCompile Include="..\cpp\TextToNumber.cpp" />
<ClCompile Include="..\cpp\AbstractSyntaxTree.cpp" />
<ClCompile Include="..\cpp\BuiltInFunctionDef.cpp" />
<ClCompile Include="..\cpp\BuiltInFunctionDefManager.cpp" />
Expand Down Expand Up @@ -346,7 +346,7 @@
<ResourceCompile Include="resources.rc" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\cpp\AsciiToDouble.hpp" />
<ClInclude Include="..\cpp\TextToNumber.hpp" />
<ClInclude Include="..\cpp\CallMexBuiltin.hpp" />
<ClInclude Include="..\cpp\operators\RelationOperatorsHelpers.hpp" />
<ClInclude Include="..\grammar\NelSonParser.h" />
Expand Down
4 changes: 2 additions & 2 deletions modules/interpreter/src/c/nlsInterpreter.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
<ClCompile Include="..\cpp\NelsonReadyNamedMutex.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\cpp\AsciiToDouble.cpp">
<ClCompile Include="..\cpp\TextToNumber.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\grammar\NelsonParserHelpers.cpp">
Expand Down Expand Up @@ -355,7 +355,7 @@
<ClInclude Include="..\include\CallStack.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\cpp\AsciiToDouble.hpp">
<ClInclude Include="..\cpp\TextToNumber.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\grammar\NelsonParserHelpers.hpp">
Expand Down
4 changes: 2 additions & 2 deletions modules/interpreter/src/cpp/AbstractSyntaxTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ AbstractSyntaxTree::getReferences()
//=============================================================================

AbstractSyntaxTreePtr
AbstractSyntaxTree::createNode(NODE_TYPE ntype, const char* name, int context)
AbstractSyntaxTree::createNode(NODE_TYPE ntype, const std::string& name, int context)
{
AbstractSyntaxTreePtr p;
try {
Expand Down Expand Up @@ -126,7 +126,7 @@ AbstractSyntaxTree::AbstractSyntaxTree()
opNum = OP_NULL;
}
//=============================================================================
AbstractSyntaxTree::AbstractSyntaxTree(NODE_TYPE ntype, const char* name, int context)
AbstractSyntaxTree::AbstractSyntaxTree(NODE_TYPE ntype, const std::string& name, int context)
: type(ntype), text(name), m_context(context)
{
tokenNumber = 0;
Expand Down
7 changes: 7 additions & 0 deletions modules/interpreter/src/cpp/AbstractSyntaxTreeHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ expression(AbstractSyntaxTreePtr expr)
} break;
case const_int_node:
case const_uint64_node:
case const_uint32_node:
case const_uint16_node:
case const_uint8_node:
case const_int64_node:
case const_int32_node:
case const_int16_node:
case const_int8_node:
case const_float_node:
case const_double_node: {
res = expr->text;
Expand Down
27 changes: 0 additions & 27 deletions modules/interpreter/src/cpp/AsciiToDouble.cpp

This file was deleted.

21 changes: 0 additions & 21 deletions modules/interpreter/src/cpp/AsciiToDouble.hpp

This file was deleted.

71 changes: 58 additions & 13 deletions modules/interpreter/src/cpp/Evaluator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
#include "ClassToString.hpp"
#include "IsValidVariableName.hpp"
#include "NelsonReadyNamedMutex.hpp"
#include "AsciiToDouble.hpp"
#include "TextToNumber.hpp"
#include "MException.hpp"
#include "FileSystemWrapper.hpp"
#include "PredefinedErrorMessages.hpp"
Expand Down Expand Up @@ -555,15 +555,59 @@ Evaluator::expression(AbstractSyntaxTreePtr t)
{
ArrayOf retval;
switch (t->type) {
case const_double_node:
case const_int_node: {
case const_double_node: {
callstack.pushID((size_t)t->getContext());
retval = ArrayOf::doubleConstructor(asciiToDouble(t->text));
retval = ArrayOf::doubleConstructor(textToDouble(t->text));
callstack.popID();
} break;
case const_float_node: {
callstack.pushID((size_t)t->getContext());
retval = ArrayOf::singleConstructor(((float)asciiToDouble(t->text)));
retval = ArrayOf::singleConstructor((textToSingle(t->text)));
callstack.popID();
} break;
case const_uint8_node: {
callstack.pushID((size_t)t->getContext());
retval = ArrayOf::uint8Constructor(textToUint8(t->text));
callstack.popID();
} break;
case const_int8_node: {
callstack.pushID((size_t)t->getContext());
retval = ArrayOf::int8Constructor(textToInt8(t->text));
callstack.popID();
} break;
case const_uint16_node: {
callstack.pushID((size_t)t->getContext());
retval = ArrayOf::uint16Constructor(textToUint16(t->text));
callstack.popID();
} break;
case const_int16_node: {
callstack.pushID((size_t)t->getContext());
retval = ArrayOf::int16Constructor(textToInt16(t->text));
callstack.popID();
} break;
case const_uint32_node: {
callstack.pushID((size_t)t->getContext());
retval = ArrayOf::uint32Constructor(textToUint32(t->text));
callstack.popID();
} break;
case const_int32_node: {
callstack.pushID((size_t)t->getContext());
retval = ArrayOf::int32Constructor(textToInt32(t->text));
callstack.popID();
} break;
case const_int64_node: {
callstack.pushID((size_t)t->getContext());
retval = ArrayOf::int64Constructor(textToInt64(t->text));
callstack.popID();
} break;
case const_uint64_node: {
callstack.pushID((size_t)t->getContext());
retval = ArrayOf::uint64Constructor(textToUint64(t->text));
callstack.popID();
} break;
case const_int_node: {
callstack.pushID((size_t)t->getContext());
retval = ArrayOf::doubleConstructor(textToDouble(t->text));
callstack.popID();
} break;
case const_character_array_node: {
Expand All @@ -576,23 +620,24 @@ Evaluator::expression(AbstractSyntaxTreePtr t)
retval = ArrayOf::stringArrayConstructor(t->text);
callstack.popID();
} break;
case const_dcomplex_node:
case const_complex_node: {
case const_dcomplex_node: {
callstack.pushID((size_t)t->getContext());
double val = asciiToDouble(t->text);
double val = textToDouble(t->text);
if (approximatelyEqual(val, 0, std::numeric_limits<double>::epsilon())) {
retval = ArrayOf::doubleConstructor(0.);
} else {
retval = ArrayOf::dcomplexConstructor(0, val);
}
callstack.popID();
} break;
case const_uint64_node: {
case const_complex_node: {
callstack.pushID((size_t)t->getContext());
char* endptr = nullptr;
unsigned long long int v = strtoull(t->text.c_str(), &endptr, 10);
auto r = static_cast<uint64>(v);
retval = ArrayOf::uint64Constructor(r);
single val = textToSingle(t->text);
if (approximatelyEqual(val, 0, std::numeric_limits<single>::epsilon())) {
retval = ArrayOf::singleConstructor(0.);
} else {
retval = ArrayOf::complexConstructor(0, val);
}
callstack.popID();
} break;
case reserved_node: {
Expand Down
Loading

0 comments on commit 08ab300

Please sign in to comment.