mirror of
https://github.com/louisrubet/rpn
synced 2025-01-30 20:34:30 +01:00
#109: added >, >=, <, <=, ==, same, != for strings
This commit is contained in:
parent
910dad3fd3
commit
869e3fecc7
2 changed files with 185 additions and 42 deletions
159
src/rpn-test.hpp
159
src/rpn-test.hpp
|
@ -1,91 +1,166 @@
|
|||
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);
|
||||
|
||||
number* right = (number*)_stack->pop_back();
|
||||
number* left = (number*)_stack->back();
|
||||
// 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();
|
||||
|
||||
if (mpfr_cmp(left->_value.mpfr, right->_value.mpfr) > 0)
|
||||
mpfr_set_si(left->_value.mpfr, 1, floating_t::s_mpfr_rnd);
|
||||
if (mpfr_cmp(left->_value.mpfr, right->_value.mpfr) > 0)
|
||||
mpfr_set_si(left->_value.mpfr, 1, floating_t::s_mpfr_rnd);
|
||||
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
|
||||
mpfr_set_si(left->_value.mpfr, 0, floating_t::s_mpfr_rnd);
|
||||
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);
|
||||
|
||||
number* right = (number*)_stack->pop_back();
|
||||
number* left = (number*)_stack->back();
|
||||
// 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();
|
||||
|
||||
if (mpfr_cmp(left->_value.mpfr, right->_value.mpfr) >= 0)
|
||||
mpfr_set_si(left->_value.mpfr, 1, floating_t::s_mpfr_rnd);
|
||||
if (mpfr_cmp(left->_value.mpfr, right->_value.mpfr) >= 0)
|
||||
mpfr_set_si(left->_value.mpfr, 1, floating_t::s_mpfr_rnd);
|
||||
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
|
||||
mpfr_set_si(left->_value.mpfr, 0, floating_t::s_mpfr_rnd);
|
||||
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);
|
||||
|
||||
number* right = (number*)_stack->pop_back();
|
||||
number* left = (number*)_stack->back();
|
||||
// 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();
|
||||
|
||||
if (mpfr_cmp(left->_value.mpfr, right->_value.mpfr) < 0)
|
||||
mpfr_set_si(left->_value.mpfr, 1, floating_t::s_mpfr_rnd);
|
||||
if (mpfr_cmp(left->_value.mpfr, right->_value.mpfr) < 0)
|
||||
mpfr_set_si(left->_value.mpfr, 1, floating_t::s_mpfr_rnd);
|
||||
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
|
||||
mpfr_set_si(left->_value.mpfr, 0, floating_t::s_mpfr_rnd);
|
||||
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);
|
||||
|
||||
number* right = (number*)_stack->pop_back();
|
||||
number* left = (number*)_stack->back();
|
||||
// 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();
|
||||
|
||||
if (mpfr_cmp(left->_value.mpfr, right->_value.mpfr) <= 0)
|
||||
mpfr_set_si(left->_value.mpfr, 1, floating_t::s_mpfr_rnd);
|
||||
if (mpfr_cmp(left->_value.mpfr, right->_value.mpfr) <= 0)
|
||||
mpfr_set_si(left->_value.mpfr, 1, floating_t::s_mpfr_rnd);
|
||||
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
|
||||
mpfr_set_si(left->_value.mpfr, 0, floating_t::s_mpfr_rnd);
|
||||
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);
|
||||
|
||||
number* right = (number*)_stack->pop_back();
|
||||
number* left = (number*)_stack->back();
|
||||
// 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();
|
||||
|
||||
if (mpfr_cmp(left->_value.mpfr, right->_value.mpfr) != 0)
|
||||
mpfr_set_si(left->_value.mpfr, 1, floating_t::s_mpfr_rnd);
|
||||
if (mpfr_cmp(left->_value.mpfr, right->_value.mpfr) != 0)
|
||||
mpfr_set_si(left->_value.mpfr, 1, floating_t::s_mpfr_rnd);
|
||||
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
|
||||
mpfr_set_si(left->_value.mpfr, 0, floating_t::s_mpfr_rnd);
|
||||
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);
|
||||
|
||||
number* right = (number*)_stack->pop_back();
|
||||
number* left = (number*)_stack->back();
|
||||
// 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();
|
||||
|
||||
if (mpfr_cmp(left->_value.mpfr, right->_value.mpfr) == 0)
|
||||
mpfr_set_si(left->_value.mpfr, 1, floating_t::s_mpfr_rnd);
|
||||
if (mpfr_cmp(left->_value.mpfr, right->_value.mpfr) == 0)
|
||||
mpfr_set_si(left->_value.mpfr, 1, floating_t::s_mpfr_rnd);
|
||||
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
|
||||
mpfr_set_si(left->_value.mpfr, 0, floating_t::s_mpfr_rnd);
|
||||
ERR_CONTEXT(ret_bad_operand_type);
|
||||
}
|
||||
|
||||
void test_and(void)
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Reference in a new issue