rpn/src/rpn-general.hpp

186 lines
4.8 KiB
C++
Raw Normal View History

//
void nop()
{
2015-05-19 17:51:03 +02:00
// nop
}
void good_bye()
{
2015-05-19 17:51:03 +02:00
ERR_CONTEXT(ret_good_bye);
}
void help()
{
2017-04-20 23:06:49 +02:00
// software name
2017-05-24 14:09:52 +02:00
printf("\n" ATTR_BOLD "%s" ATTR_OFF "\n", uname);
2014-09-24 16:55:59 +02:00
2017-04-20 23:06:49 +02:00
// description
2017-05-24 14:09:52 +02:00
printf("%s\n\n", description);
2017-04-20 23:06:49 +02:00
2015-05-19 17:51:03 +02:00
// syntax
2017-05-24 14:09:52 +02:00
printf("%s\n", syntax);
2015-02-23 17:33:05 +01:00
// keywords
unsigned int i = 0;
while(s_keywords[i].type != cmd_max)
2015-07-23 13:35:02 +02:00
{
if (s_keywords[i].comment.size() != 0)
2015-07-23 13:35:02 +02:00
{
// titles in bold
if (s_keywords[i].type==cmd_undef)
2017-05-24 14:09:52 +02:00
printf(ATTR_BOLD);
2015-07-23 13:35:02 +02:00
// show title or keyword + comment
printf("%s\t%s\n", s_keywords[i].name, s_keywords[i].comment.c_str());
if (s_keywords[i].type==cmd_undef)
2017-05-24 14:09:52 +02:00
printf(ATTR_OFF);
2015-07-23 13:35:02 +02:00
}
i++;
2015-07-23 13:35:02 +02:00
}
2017-05-24 14:09:52 +02:00
printf("\n");
2015-05-19 17:51:03 +02:00
2017-05-22 17:09:15 +02:00
// show mode
2017-05-24 14:09:52 +02:00
printf("Current float mode is ");
2015-05-19 17:51:03 +02:00
switch(number::s_mode)
{
2017-05-24 14:09:52 +02:00
case number::std: printf("'std'"); break;
case number::fix: printf("'fix'"); break;
case number::sci: printf("'sci'"); break;
default: printf("unknown"); break;
2015-05-19 17:51:03 +02:00
}
2017-05-24 14:09:52 +02:00
printf(" with %d digits\n", number::s_current_precision);
2015-05-19 17:51:03 +02:00
2017-05-22 17:09:15 +02:00
// calc precision and rounding mode
// MPFR_PREC_MAX mpfr_prec_t depends on _MPFR_PREC_FORMAT macro (see mpfr.h)
// this could not exceed 63 bits max (0x7FFFFFFFFFFFFFFF)
2017-05-25 22:13:19 +02:00
double prec_min = (double)MPFR_PREC_MIN;
double prec_max = (double)MPFR_PREC_MAX;
2017-05-25 22:13:19 +02:00
printf("Current floating point precision is %d bits (min=%ld bits, max=0x%lx bits)\n", (int)floating_t::s_mpfr_prec, (int64_t)prec_min, (int64_t)prec_max);
printf("Current rounding mode is '%s'\n", floating_t::s_mpfr_rnd_str[floating_t::s_mpfr_rnd]);
2017-05-24 14:09:52 +02:00
printf("\n\n");
}
2014-09-05 09:49:39 +02:00
void std()
{
2017-05-23 14:34:11 +02:00
int precision = -1;
2015-05-19 17:51:03 +02:00
if (stack_size()>=1)
{
ARG_MUST_BE_OF_TYPE(0, cmd_number);
2017-05-23 14:34:11 +02:00
precision = int(((number*)_stack->pop_back())->_value);
2015-05-19 17:51:03 +02:00
}
2017-05-23 14:34:11 +02:00
if (precision != -1)
number::s_current_precision = precision;
2015-05-19 17:51:03 +02:00
number::s_mode = number::std;
// format for mpfr_printf
stringstream ss;
ss << number::s_current_precision;
number::s_mpfr_printf_format = string(MPFR_FORMAT_BEG) + ss.str() + string(MPFR_FORMAT_STD);
2014-09-05 09:49:39 +02:00
}
void fix()
{
2015-05-19 17:51:03 +02:00
MIN_ARGUMENTS(1);
ARG_MUST_BE_OF_TYPE(0, cmd_number);
2014-09-05 09:49:39 +02:00
int precision = int(((number*)_stack->pop_back())->_value);
number::s_current_precision = int(precision);
2015-05-19 17:51:03 +02:00
number::s_mode = number::fix;
2014-09-06 22:31:01 +02:00
// format for mpfr_printf
stringstream ss;
ss << number::s_current_precision;
number::s_mpfr_printf_format = string(MPFR_FORMAT_BEG) + ss.str() + string(MPFR_FORMAT_FIX);
2014-09-05 09:49:39 +02:00
}
void sci()
{
2015-05-19 17:51:03 +02:00
MIN_ARGUMENTS(1);
ARG_MUST_BE_OF_TYPE(0, cmd_number);
2014-09-05 09:49:39 +02:00
int precision = int(((number*)_stack->pop_back())->_value);
number::s_current_precision = int(precision);
2015-05-19 17:51:03 +02:00
number::s_mode = number::sci;
2014-09-06 22:31:01 +02:00
// format for mpfr_printf
stringstream ss;
ss << number::s_current_precision;
number::s_mpfr_printf_format = string(MPFR_FORMAT_BEG) + ss.str() + string(MPFR_FORMAT_SCI);
2014-09-05 09:49:39 +02:00
}
2014-09-24 16:55:59 +02:00
void rpn_version()
{
2017-05-02 10:47:03 +02:00
// allocate and set object
unsigned int naked_entry_len = strlen(version);
ostring* str = (ostring*)_stack->allocate_back(sizeof(ostring)+naked_entry_len+1, cmd_string);
2015-03-02 18:20:21 +01:00
str->set(version, naked_entry_len);
2014-09-24 16:55:59 +02:00
}
void rpn_uname()
{
2017-05-02 10:47:03 +02:00
// allocate and set object
unsigned int naked_entry_len = strlen(uname);
ostring* str = (ostring*)_stack->allocate_back(sizeof(ostring)+naked_entry_len+1, cmd_string);
2015-03-02 18:20:21 +01:00
str->set(uname, naked_entry_len);
2014-09-24 16:55:59 +02:00
}
2017-04-22 22:17:51 +02:00
void type()
{
MIN_ARGUMENTS(1);
int type = _stack->back()->_type;
2017-04-22 22:17:51 +02:00
if (type < 0 || type >= (int)cmd_max)
type = (int)cmd_undef;
unsigned int string_size = strlen(object::s_cmd_type_string[type]);
2017-04-22 22:17:51 +02:00
unsigned int size = sizeof(symbol)+string_size+1;
2017-05-13 12:20:07 +02:00
symbol* sym = (symbol*)_stack->allocate_back(size, cmd_symbol);
sym->set(object::s_cmd_type_string[type], string_size, false);
2017-04-22 22:17:51 +02:00
}
2017-04-23 15:02:46 +02:00
void rpn_default()
{
program::apply_default();
}
2017-05-22 14:29:55 +02:00
void precision()
{
2017-05-22 17:09:15 +02:00
MIN_ARGUMENTS(1);
ARG_MUST_BE_OF_TYPE(0, cmd_number);
2017-05-25 22:13:19 +02:00
//set precision
unsigned long prec = mpfr_get_ui(((number*)_stack->pop_back())->_value.mpfr, floating_t::s_mpfr_rnd);
if (prec>=(unsigned long)MPFR_PREC_MIN && prec<=(unsigned long)MPFR_PREC_MAX)
2017-05-22 14:29:55 +02:00
{
floating_t::s_mpfr_prec = (mpfr_prec_t)prec;
floating_t::s_mpfr_prec_bytes = mpfr_custom_get_size(prec);
2017-05-22 14:29:55 +02:00
}
else
2017-05-22 17:09:15 +02:00
ERR_CONTEXT(ret_out_of_range);
}
void round()
{
MIN_ARGUMENTS(1);
ARG_MUST_BE_OF_TYPE(0, cmd_string);
ostring* str = (ostring*)_stack->pop_back();
bool done = false;
2017-05-23 14:55:09 +02:00
for(int rnd = (int)MPFR_DEFAULT_RND; rnd <= (int)MPFR_RNDA; rnd++)
2017-05-22 14:29:55 +02:00
{
if (string(floating_t::s_mpfr_rnd_str[rnd]) == str->_value)
2017-05-22 17:09:15 +02:00
{
floating_t::s_mpfr_rnd = (mpfr_rnd_t)rnd;
2017-05-22 17:09:15 +02:00
done = true;
}
2017-05-22 14:29:55 +02:00
}
2017-05-22 17:09:15 +02:00
if (!done)
ERR_CONTEXT(ret_out_of_range);
2017-05-22 14:29:55 +02:00
}