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 ea9c195
Show file tree
Hide file tree
Showing 13 changed files with 254 additions and 60 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/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
46 changes: 43 additions & 3 deletions modules/interpreter/src/cpp/AsciiToDouble.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <string>
#include <algorithm>
#include <cstdlib>
#include "fast_float/fast_float.h"
#include <fast_float/fast_float.h>
#include "AsciiToDouble.hpp"
//=============================================================================
namespace Nelson {
Expand All @@ -19,9 +19,49 @@ double
asciiToDouble(const std::string& str)
{
double value;
auto answer = fast_float::from_chars(str.data(), str.data() + str.size(), value);
return value;
auto answer = fast_float::from_chars<double>(str.data(), str.data() + str.size(), value);
return abs(value);
}
//=============================================================================
int32
asciiToInt32(const std::string& str)
{
int32 res = static_cast<int32>(0);
int64 valuei64 = asciiToInt64(str);
if (valuei64 >= static_cast<int64>(std::numeric_limits<int32>::max())) {
res = std::numeric_limits<int32>::max();
} else if (valuei64 <= static_cast<int64>(std::numeric_limits<int32>::min())) {
res = std::numeric_limits<int32>::min();
} else {
res = abs(static_cast<int32>(valuei64));
}
return res;
}
//=============================================================================
int64
asciiToInt64(const std::string& str)
{
int64 res = static_cast<int64>(0);
char* endptr = nullptr;
if (str[0] == '-') {
std::string withNeg = str.substr(1);
unsigned long long int v = strtoull(withNeg.c_str(), &endptr, 10);
if (v > -std::numeric_limits<int64>::min()) {
res = std::numeric_limits<int64>::min();
} else {
res = static_cast<int64>(v);
}
} else {
unsigned long long int v = strtoull(str.c_str(), &endptr, 10);
if (v > std::numeric_limits<int64>::max()) {
res = std::numeric_limits<int64>::max();
} else {
res = static_cast<int64>(v);
}
}
return res;
}
//=============================================================================

}; // namespace Nelson
//=============================================================================
8 changes: 8 additions & 0 deletions modules/interpreter/src/cpp/AsciiToDouble.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,19 @@
//=============================================================================
#include <string>
#include "nlsInterpreter_exports.h"
#include "Types.hpp"
//=============================================================================
namespace Nelson {
//=============================================================================
NLSINTERPRETER_IMPEXP double
asciiToDouble(const std::string& str);

NLSINTERPRETER_IMPEXP int32
asciiToInt32(const std::string& str);

NLSINTERPRETER_IMPEXP int64
asciiToInt64(const std::string& str);

//=============================================================================
}; // namespace Nelson
//=============================================================================
29 changes: 26 additions & 3 deletions modules/interpreter/src/cpp/Evaluator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,21 @@ Evaluator::expression(AbstractSyntaxTreePtr t)
{
ArrayOf retval;
switch (t->type) {
case const_double_node:
case const_double_node: {
callstack.pushID((size_t)t->getContext());
retval = ArrayOf::doubleConstructor(asciiToDouble(t->text));
callstack.popID();
} break;
case const_int32_node: {
callstack.pushID((size_t)t->getContext());
retval = ArrayOf::int32Constructor(asciiToInt32(t->text));
callstack.popID();
} break;
case const_int64_node: {
callstack.pushID((size_t)t->getContext());
retval = ArrayOf::int64Constructor(asciiToInt64(t->text));
callstack.popID();
} break;
case const_int_node: {
callstack.pushID((size_t)t->getContext());
retval = ArrayOf::doubleConstructor(asciiToDouble(t->text));
Expand All @@ -576,8 +590,7 @@ 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);
if (approximatelyEqual(val, 0, std::numeric_limits<double>::epsilon())) {
Expand All @@ -587,6 +600,16 @@ Evaluator::expression(AbstractSyntaxTreePtr t)
}
callstack.popID();
} break;
case const_complex_node: {
callstack.pushID((size_t)t->getContext());
single val = asciiToDouble(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 const_uint64_node: {
callstack.pushID((size_t)t->getContext());
char* endptr = nullptr;
Expand Down
Loading

0 comments on commit ea9c195

Please sign in to comment.