#109: added >, >=, <, <=, ==, same, != for strings

This commit is contained in:
Louis Rubet 2017-05-31 23:08:40 +02:00
parent 910dad3fd3
commit 869e3fecc7
2 changed files with 185 additions and 42 deletions

View file

@ -1,9 +1,19 @@
int cmp_strings_on_stack_top()
{
// _stack sould have 2 strings at level 1 and 2
// this function removes these 2 entries
ostring* right = (ostring*)_stack->pop_back();
ostring* left = (ostring*)_stack->pop_back();
return strncmp(left->_value, right->_value, min(left->_len, right->_len));
}
void sup(void)
{
MIN_ARGUMENTS(2);
ARG_MUST_BE_OF_TYPE(0, cmd_number);
ARG_MUST_BE_OF_TYPE(1, cmd_number);
// numbers
if (_stack->get_type(0) == cmd_number && _stack->get_type(1) == cmd_number)
{
number* right = (number*)_stack->pop_back();
number* left = (number*)_stack->back();
@ -12,13 +22,24 @@ void sup(void)
else
mpfr_set_si(left->_value.mpfr, 0, floating_t::s_mpfr_rnd);
}
// strings
else if (_stack->get_type(0) == cmd_string && _stack->get_type(1) == cmd_string)
{
int res_cmp = cmp_strings_on_stack_top();
number* res = (number*)_stack->allocate_back(number::calc_size(), cmd_number);
res->_value = (res_cmp > 0) ? 1L : 0L;
}
else
ERR_CONTEXT(ret_bad_operand_type);
}
void sup_eq(void)
{
MIN_ARGUMENTS(2);
ARG_MUST_BE_OF_TYPE(0, cmd_number);
ARG_MUST_BE_OF_TYPE(1, cmd_number);
// numbers
if (_stack->get_type(0) == cmd_number && _stack->get_type(1) == cmd_number)
{
number* right = (number*)_stack->pop_back();
number* left = (number*)_stack->back();
@ -27,13 +48,24 @@ void sup_eq(void)
else
mpfr_set_si(left->_value.mpfr, 0, floating_t::s_mpfr_rnd);
}
// strings
else if (_stack->get_type(0) == cmd_string && _stack->get_type(1) == cmd_string)
{
int res_cmp = cmp_strings_on_stack_top();
number* res = (number*)_stack->allocate_back(number::calc_size(), cmd_number);
res->_value = (res_cmp >= 0) ? 1L : 0L;
}
else
ERR_CONTEXT(ret_bad_operand_type);
}
void inf(void)
{
MIN_ARGUMENTS(2);
ARG_MUST_BE_OF_TYPE(0, cmd_number);
ARG_MUST_BE_OF_TYPE(1, cmd_number);
// numbers
if (_stack->get_type(0) == cmd_number && _stack->get_type(1) == cmd_number)
{
number* right = (number*)_stack->pop_back();
number* left = (number*)_stack->back();
@ -42,13 +74,24 @@ void inf(void)
else
mpfr_set_si(left->_value.mpfr, 0, floating_t::s_mpfr_rnd);
}
// strings
else if (_stack->get_type(0) == cmd_string && _stack->get_type(1) == cmd_string)
{
int res_cmp = cmp_strings_on_stack_top();
number* res = (number*)_stack->allocate_back(number::calc_size(), cmd_number);
res->_value = (res_cmp < 0) ? 1L : 0L;
}
else
ERR_CONTEXT(ret_bad_operand_type);
}
void inf_eq(void)
{
MIN_ARGUMENTS(2);
ARG_MUST_BE_OF_TYPE(0, cmd_number);
ARG_MUST_BE_OF_TYPE(1, cmd_number);
// numbers
if (_stack->get_type(0) == cmd_number && _stack->get_type(1) == cmd_number)
{
number* right = (number*)_stack->pop_back();
number* left = (number*)_stack->back();
@ -57,13 +100,24 @@ void inf_eq(void)
else
mpfr_set_si(left->_value.mpfr, 0, floating_t::s_mpfr_rnd);
}
// strings
else if (_stack->get_type(0) == cmd_string && _stack->get_type(1) == cmd_string)
{
int res_cmp = cmp_strings_on_stack_top();
number* res = (number*)_stack->allocate_back(number::calc_size(), cmd_number);
res->_value = (res_cmp <= 0) ? 1L : 0L;
}
else
ERR_CONTEXT(ret_bad_operand_type);
}
void diff(void)
{
MIN_ARGUMENTS(2);
ARG_MUST_BE_OF_TYPE(0, cmd_number);
ARG_MUST_BE_OF_TYPE(1, cmd_number);
// numbers
if (_stack->get_type(0) == cmd_number && _stack->get_type(1) == cmd_number)
{
number* right = (number*)_stack->pop_back();
number* left = (number*)_stack->back();
@ -72,13 +126,24 @@ void diff(void)
else
mpfr_set_si(left->_value.mpfr, 0, floating_t::s_mpfr_rnd);
}
// strings
else if (_stack->get_type(0) == cmd_string && _stack->get_type(1) == cmd_string)
{
int res_cmp = cmp_strings_on_stack_top();
number* res = (number*)_stack->allocate_back(number::calc_size(), cmd_number);
res->_value = (res_cmp != 0) ? 1L : 0L;
}
else
ERR_CONTEXT(ret_bad_operand_type);
}
void eq(void)
{
MIN_ARGUMENTS(2);
ARG_MUST_BE_OF_TYPE(0, cmd_number);
ARG_MUST_BE_OF_TYPE(1, cmd_number);
// numbers
if (_stack->get_type(0) == cmd_number && _stack->get_type(1) == cmd_number)
{
number* right = (number*)_stack->pop_back();
number* left = (number*)_stack->back();
@ -87,6 +152,16 @@ void eq(void)
else
mpfr_set_si(left->_value.mpfr, 0, floating_t::s_mpfr_rnd);
}
// strings
else if (_stack->get_type(0) == cmd_string && _stack->get_type(1) == cmd_string)
{
int res_cmp = cmp_strings_on_stack_top();
number* res = (number*)_stack->allocate_back(number::calc_size(), cmd_number);
res->_value = (res_cmp == 0) ? 1L : 0L;
}
else
ERR_CONTEXT(ret_bad_operand_type);
}
void test_and(void)
{

View file

@ -173,3 +173,71 @@ drop
1 1 same
-> stack should be 1
drop
# string >
"a" "b" >
-> stack should be 0
drop
"b" "a" >
-> stack should be 1
drop
"a" "a" >
-> stack should be 0
drop
# string >=
"a" "b" >=
-> stack should be 0
drop
"b" "a" >=
-> stack should be 1
drop
"a" "a" >=
-> stack should be 1
drop
# string <
"a" "b" <
-> stack should be 1
drop
"b" "a" <
-> stack should be 0
drop
"a" "a" <
-> stack should be 0
drop
# string <=
"a" "b" <=
-> stack should be 1
drop
"b" "a" <=
-> stack should be 0
drop
"a" "a" <=
-> stack should be 1
drop
# string !=
"a" "b" !=
-> stack should be 1
drop
"a" "a" !=
-> stack should be 0
drop
# string ==
"a" "b" ==
-> stack should be 0
drop
"a" "a" ==
-> stack should be 1
drop
# string same
"a" "b" same
-> stack should be 0
drop
"a" "a" same
-> stack should be 1
drop