diff --git a/src/rpn-binary.h b/src/rpn-binary.h index 1f10593..0e7fa23 100644 --- a/src/rpn-binary.h +++ b/src/rpn-binary.h @@ -20,20 +20,30 @@ void bin() void rtob() { - //TODO - //MIN_ARGUMENTS(1); - //ARG_MUST_BE_OF_TYPE(0, cmd_number); + MIN_ARGUMENTS(1); + ARG_MUST_BE_OF_TYPE(0, cmd_number); - //binary bin; - //bin.set(((integer_t)getf())); - //_stack->push_back(&bin, bin.size(), cmd_binary); + number* left = (number*)_stack->back(); + _stack->pop_back(); + + cout<<"TODO"<_value.mpfr, s_mpfr_rnd); + _stack->push_back(&bin, bin.size(), cmd_binary); } void btor() { - // TODO - //MIN_ARGUMENTS(1); - //ARG_MUST_BE_OF_TYPE(0, cmd_binary); + MIN_ARGUMENTS(1); + ARG_MUST_BE_OF_TYPE(0, cmd_binary); - //putf((floating_t)getb()); + integer_t bin = getb(); + + cout<<"TODO"<push_back(NULL, sizeof(number), cmd_number, true); + number* left = (number*)_stack->back(); + left->_value = bin; + left->ensure_significand(); } diff --git a/src/rpn-test.h b/src/rpn-test.h index 41c8bc2..afd90c2 100644 --- a/src/rpn-test.h +++ b/src/rpn-test.h @@ -1,142 +1,171 @@ +//TODO must work with binary, ostring (others ?) + void sup(void) { - //TODO - //MIN_ARGUMENTS(2); - //ARG_MUST_BE_OF_TYPE(0, cmd_number); - //ARG_MUST_BE_OF_TYPE(1, cmd_number); + MIN_ARGUMENTS(2); + ARG_MUST_BE_OF_TYPE(0, cmd_number); + ARG_MUST_BE_OF_TYPE(1, cmd_number); - //floating_t first = getf(); - //putf(getf() > first); + number* right = (number*)_stack->back(); + _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, s_mpfr_rnd); + else + mpfr_set_si(&left->_value.mpfr, 0, s_mpfr_rnd); } void sup_eq(void) { - //TODO - //MIN_ARGUMENTS(2); - //ARG_MUST_BE_OF_TYPE(0, cmd_number); - //ARG_MUST_BE_OF_TYPE(1, cmd_number); + MIN_ARGUMENTS(2); + ARG_MUST_BE_OF_TYPE(0, cmd_number); + ARG_MUST_BE_OF_TYPE(1, cmd_number); - //floating_t first = getf(); - //putf(getf() >= first); + number* right = (number*)_stack->back(); + _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, s_mpfr_rnd); + else + mpfr_set_si(&left->_value.mpfr, 0, s_mpfr_rnd); } void inf(void) { - //TODO - //MIN_ARGUMENTS(2); - //ARG_MUST_BE_OF_TYPE(0, cmd_number); - //ARG_MUST_BE_OF_TYPE(1, cmd_number); + MIN_ARGUMENTS(2); + ARG_MUST_BE_OF_TYPE(0, cmd_number); + ARG_MUST_BE_OF_TYPE(1, cmd_number); - //floating_t first = getf(); - //putf(getf() < first); + number* right = (number*)_stack->back(); + _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, s_mpfr_rnd); + else + mpfr_set_si(&left->_value.mpfr, 0, s_mpfr_rnd); } void inf_eq(void) { - //TODO - //MIN_ARGUMENTS(2); - //ARG_MUST_BE_OF_TYPE(0, cmd_number); - //ARG_MUST_BE_OF_TYPE(1, cmd_number); + MIN_ARGUMENTS(2); + ARG_MUST_BE_OF_TYPE(0, cmd_number); + ARG_MUST_BE_OF_TYPE(1, cmd_number); - //floating_t first = getf(); - //putf(getf() <= first); + number* right = (number*)_stack->back(); + _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, s_mpfr_rnd); + else + mpfr_set_si(&left->_value.mpfr, 0, s_mpfr_rnd); } void diff(void) { - //TODO - //MIN_ARGUMENTS(2); - //ARG_MUST_BE_OF_TYPE(0, cmd_number); - //ARG_MUST_BE_OF_TYPE(1, cmd_number); + MIN_ARGUMENTS(2); + ARG_MUST_BE_OF_TYPE(0, cmd_number); + ARG_MUST_BE_OF_TYPE(1, cmd_number); - //floating_t first = getf(); - //putf(getf() != first); + number* right = (number*)_stack->back(); + _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, s_mpfr_rnd); + else + mpfr_set_si(&left->_value.mpfr, 0, s_mpfr_rnd); } void eq(void) { - //TODO - //MIN_ARGUMENTS(2); - //int type = _stack->get_type(0); + MIN_ARGUMENTS(2); + ARG_MUST_BE_OF_TYPE(0, cmd_number); + ARG_MUST_BE_OF_TYPE(1, cmd_number); - //if (_stack->get_type(1) == type) - //{ - // switch(type) - // { - // case cmd_number: - // { - // floating_t first = getf(); - // putf(getf() == first); - // } - // break; - //TODO -#if 0 - case cmd_symbol: - { - string first = getn(); - putf(getn() == first); - } -#endif - // break; - // default: - // _stack->pop_back(); - // _stack->pop_back(); - // putf(0); - // break; - // } - //} - //else - //{ - // _stack->pop_back(); - // _stack->pop_back(); - // putf(0); - //} + number* right = (number*)_stack->back(); + _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, s_mpfr_rnd); + else + mpfr_set_si(&left->_value.mpfr, 0, s_mpfr_rnd); } void test_and(void) { - //TODO - //MIN_ARGUMENTS(2); - //ARG_MUST_BE_OF_TYPE(0, cmd_number); - //ARG_MUST_BE_OF_TYPE(1, cmd_number); + MIN_ARGUMENTS(2); + ARG_MUST_BE_OF_TYPE(0, cmd_number); + ARG_MUST_BE_OF_TYPE(1, cmd_number); - //floating_t first = getf(); - //floating_t second = getf(); - //putf((first != 0) && (second != 0)); + number* right = (number*)_stack->back(); + _stack->pop_back(); + number* left = (number*)_stack->back(); + + if ((mpfr_cmp_si(&left->_value.mpfr, 0) == 0) + && (mpfr_cmp_si(&right->_value.mpfr, 0) == 0)) + mpfr_set_si(&left->_value.mpfr, 1, s_mpfr_rnd); + else + mpfr_set_si(&left->_value.mpfr, 0, s_mpfr_rnd); } void test_or(void) { - //TODO - //MIN_ARGUMENTS(2); - //ARG_MUST_BE_OF_TYPE(0, cmd_number); - //ARG_MUST_BE_OF_TYPE(1, cmd_number); + MIN_ARGUMENTS(2); + ARG_MUST_BE_OF_TYPE(0, cmd_number); + ARG_MUST_BE_OF_TYPE(1, cmd_number); - //floating_t first = getf(); - //floating_t second = getf(); - //putf((first != 0) || (second != 0)); + number* right = (number*)_stack->back(); + _stack->pop_back(); + number* left = (number*)_stack->back(); + + if ((mpfr_cmp_si(&left->_value.mpfr, 0) == 0) + || (mpfr_cmp_si(&right->_value.mpfr, 0) == 0)) + mpfr_set_si(&left->_value.mpfr, 1, s_mpfr_rnd); + else + mpfr_set_si(&left->_value.mpfr, 0, s_mpfr_rnd); } void test_xor(void) { - //TODO - //MIN_ARGUMENTS(2); - //ARG_MUST_BE_OF_TYPE(0, cmd_number); - //ARG_MUST_BE_OF_TYPE(1, cmd_number); + MIN_ARGUMENTS(2); + ARG_MUST_BE_OF_TYPE(0, cmd_number); + ARG_MUST_BE_OF_TYPE(1, cmd_number); - //floating_t first = getf(); - //floating_t second = getf(); - //putf(((first == 0) && (second != 0)) || ((first != 0) && (second == 0))); + number* right = (number*)_stack->back(); + _stack->pop_back(); + number* left = (number*)_stack->back(); + + if (mpfr_cmp_si(&left->_value.mpfr, 0) == 0) + { + if (mpfr_cmp_si(&right->_value.mpfr, 0) != 0) + mpfr_set_si(&left->_value.mpfr, 1, s_mpfr_rnd); + else + mpfr_set_si(&left->_value.mpfr, 0, s_mpfr_rnd); + } + else + { + if (mpfr_cmp_si(&right->_value.mpfr, 0) == 0) + mpfr_set_si(&left->_value.mpfr, 1, s_mpfr_rnd); + else + mpfr_set_si(&left->_value.mpfr, 0, s_mpfr_rnd); + } } void test_not(void) { - //TODO - //MIN_ARGUMENTS(1); - //ARG_MUST_BE_OF_TYPE(0, cmd_number); + MIN_ARGUMENTS(1); + ARG_MUST_BE_OF_TYPE(0, cmd_number); - //floating_t first = getf(); - //putf((first == 0) ? 1 : 0); + number* left = (number*)_stack->back(); + if (mpfr_cmp_si(&left->_value.mpfr, 0) == 0) + mpfr_set_si(&left->_value.mpfr, 1, s_mpfr_rnd); + else + mpfr_set_si(&left->_value.mpfr, 0, s_mpfr_rnd); } void same(void) diff --git a/src/rpn.cpp b/src/rpn.cpp index 3ce4c64..cf70dbe 100644 --- a/src/rpn.cpp +++ b/src/rpn.cpp @@ -21,6 +21,7 @@ * */ #include +#include //for intmax_t #include #include @@ -99,6 +100,8 @@ const char* cmd_type_string[cmd_max] = { "undef", "number", "binary", "string", "symbol", "program", "keyword", "keyword" }; +typedef long long integer_t; + // MPFR object struct floating_t { @@ -111,13 +114,20 @@ struct floating_t mpfr_custom_init_set(&mpfr, MPFR_NAN_KIND, 0, s_mpfr_prec, significand); } - floating_t operator=(const long int val) + floating_t& operator=(const long int val) { mpfr_custom_init(significand, MPFR_128BITS_STORING_LENGTH); mpfr_custom_init_set(&mpfr, MPFR_ZERO_KIND, 0, s_mpfr_prec, significand); mpfr_set_si(&mpfr, val, s_mpfr_rnd); } + floating_t& operator=(const integer_t val) + { + mpfr_custom_init(significand, MPFR_128BITS_STORING_LENGTH); + mpfr_custom_init_set(&mpfr, MPFR_ZERO_KIND, 0, s_mpfr_prec, significand); + mpfr_set_sj(&mpfr, val, s_mpfr_rnd); + } + operator int() { return (int)mpfr_get_si(&mpfr, s_mpfr_rnd); @@ -139,8 +149,6 @@ struct floating_t } }; -typedef long long integer_t; - class program; class object; class branch; @@ -344,8 +352,8 @@ struct branch : public object arg1 = -1; arg2 = -1; arg3 = -1; - farg1 = 0; - farg2 = 0; + farg1 = 0L; + farg2 = 0L; arg_bool = 0; if (value != NULL) {