mirror of
https://github.com/louisrubet/rpn
synced 2025-01-28 19:58:01 +01:00
#60: printf instead of cout for MPFR
This commit is contained in:
parent
6f3c4ffb85
commit
3063351a3f
6 changed files with 131 additions and 120 deletions
|
@ -41,5 +41,5 @@ static void dump8(unsigned char* to_dump, unsigned long offset,
|
|||
}
|
||||
|
||||
//
|
||||
#define TRACE(x) cout<<__FUNCTION__<<": "<<(x)<<endl;
|
||||
#define TRACE2(x, y) cout<<__FUNCTION__<<": "<<(x)<<(y)<<endl;
|
||||
#define TRACE(...) do { (void)printf(__FUNCTION__ ": "); (void)printf(__VA_ARGS__); } while(0)
|
||||
|
||||
|
|
|
@ -14,15 +14,13 @@ void good_bye()
|
|||
void help()
|
||||
{
|
||||
// software name
|
||||
cout<<endl;
|
||||
cout<<ATTR_BOLD<<uname<<ATTR_OFF<<endl;
|
||||
cout<<endl;
|
||||
printf("\n" ATTR_BOLD "%s" ATTR_OFF "\n", uname);
|
||||
|
||||
// description
|
||||
cout<<description<<endl<<endl;
|
||||
printf("%s\n\n", description);
|
||||
|
||||
// syntax
|
||||
cout<<syntax<<endl;
|
||||
printf("%s\n", syntax);
|
||||
|
||||
// keywords
|
||||
for(unsigned int i=0; i<sizeof(_keywords)/sizeof(_keywords[0]); i++)
|
||||
|
@ -31,30 +29,30 @@ void help()
|
|||
{
|
||||
// titles in bold
|
||||
if (_keywords[i].type==cmd_undef)
|
||||
cout<<ATTR_BOLD;
|
||||
printf(ATTR_BOLD);
|
||||
// show title or keyword + comment
|
||||
cout<<_keywords[i].name<<"\t"<<_keywords[i].comment<<endl;
|
||||
printf("%s\t%s\n", _keywords[i].name, _keywords[i].comment.c_str());
|
||||
if (_keywords[i].type==cmd_undef)
|
||||
cout<<ATTR_OFF;
|
||||
printf(ATTR_OFF);
|
||||
}
|
||||
}
|
||||
cout<<endl;
|
||||
printf("\n");
|
||||
|
||||
// show mode
|
||||
cout<<"Current float mode is ";
|
||||
printf("Current float mode is ");
|
||||
switch(number::s_mode)
|
||||
{
|
||||
case number::std: cout << "'std'"; break;
|
||||
case number::fix: cout << "'fix'"; break;
|
||||
case number::sci: cout << "'sci'"; break;
|
||||
default: cout << "unknown"; break;
|
||||
case number::std: printf("'std'"); break;
|
||||
case number::fix: printf("'fix'"); break;
|
||||
case number::sci: printf("'sci'"); break;
|
||||
default: printf("unknown"); break;
|
||||
}
|
||||
cout<<" with "<<number::s_current_precision<<" digits"<<endl;
|
||||
printf(" with %d digits\n", number::s_current_precision);
|
||||
|
||||
// calc precision and rounding mode
|
||||
cout<<"Current floating point precision is "<<(int)s_mpfr_prec<<" bits"<<endl;
|
||||
cout<<"Current rounding mode is '"<<s_mpfr_rnd_str[s_mpfr_rnd]<<"'"<<endl;
|
||||
cout<<endl<<endl;
|
||||
printf("Current floating point precision is %d bits\n", (int)s_mpfr_prec);
|
||||
printf("Current rounding mode is '%s'\n", s_mpfr_rnd_str[s_mpfr_rnd]);
|
||||
printf("\n\n");
|
||||
}
|
||||
|
||||
void std()
|
||||
|
|
|
@ -37,33 +37,28 @@ void edit(void)
|
|||
{
|
||||
MIN_ARGUMENTS(1);
|
||||
|
||||
stringstream out;
|
||||
|
||||
if (((object*)_stack->back())->_type == cmd_number)
|
||||
FILE* tmp = tmpfile();
|
||||
if (tmp != NULL)
|
||||
{
|
||||
switch(number::s_mode)
|
||||
// re-write stack objet in a stream
|
||||
((object*)_stack->pop_back())->show(tmp);
|
||||
|
||||
// edit: stuff chars using readline facility
|
||||
int len = (int)ftell(tmp);
|
||||
rewind(tmp);
|
||||
for(int i=0;i<len;i++)
|
||||
{
|
||||
case number::std:
|
||||
out.precision(number::s_current_precision);
|
||||
out.unsetf(ios_base::floatfield);
|
||||
break;
|
||||
case number::fix:
|
||||
out << setprecision(number::s_current_precision) << fixed;
|
||||
break;
|
||||
case number::sci:
|
||||
out << setprecision(number::s_current_precision) << scientific;
|
||||
break;
|
||||
char readc;
|
||||
if (fread(&readc, 1, 1, tmp) == 1)
|
||||
rl_stuff_char(readc);
|
||||
else
|
||||
{
|
||||
ERR_CONTEXT(ret_runtime_error);
|
||||
break;
|
||||
}
|
||||
}
|
||||
fclose(tmp);
|
||||
}
|
||||
|
||||
// re-write stack objet in a stream
|
||||
((object*)_stack->pop_back())->show(out);
|
||||
|
||||
// edit: stuff chars using readline facility
|
||||
string str = out.str();
|
||||
|
||||
for(int i=0;i<str.size();i++)
|
||||
rl_stuff_char(str[i]);
|
||||
}
|
||||
|
||||
// carefull : this is not a langage command
|
||||
|
@ -129,25 +124,25 @@ void vars(void)
|
|||
for (int i=0; i<(int)_global_heap->size(); i++)
|
||||
{
|
||||
(void)_global_heap->get_by_index(i, name, obj, size);
|
||||
cout<<"var "<<i+1<<": name '"<<name<<"', type "<<cmd_type_string[obj->_type]<<", value ";
|
||||
printf("var %d: name '%s', type %s, value ", i+1, name.c_str(), cmd_type_string[obj->_type]);
|
||||
obj->show();
|
||||
cout<<endl;
|
||||
printf("\n");
|
||||
}
|
||||
if (_parent_local_heap != NULL)
|
||||
{
|
||||
for (int i=0; i<(int)_parent_local_heap->size(); i++)
|
||||
{
|
||||
(void)_parent_local_heap->get_by_index(i, name, obj, size);
|
||||
cout<<"local var "<<i+1<<": name '"<<name<<"', type "<<cmd_type_string[obj->_type]<<", value ";
|
||||
printf("local var %d: name '%s', type %s, value ", i+1, name.c_str(), cmd_type_string[obj->_type]);
|
||||
obj->show();
|
||||
cout<<endl;
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
for (int i=0; i<(int)_local_heap.size(); i++)
|
||||
{
|
||||
(void)_local_heap.get_by_index(i, name, obj, size);
|
||||
cout<<"local var "<<i+1<<": name '"<<name<<"', type "<<cmd_type_string[obj->_type]<<", value ";
|
||||
printf("local var %d: name '%s', type %s, value ", i+1, name.c_str(), cmd_type_string[obj->_type]);
|
||||
obj->show();
|
||||
cout<<endl;
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,15 +6,23 @@ void instr()
|
|||
if (_stack->get_type(0) != cmd_string)
|
||||
{
|
||||
// write the object in stack(0) in a string and remove this obj
|
||||
stringstream out;
|
||||
((object*)_stack->pop_back())->show(out);
|
||||
FILE* tmp = tmpfile();
|
||||
if (tmp != NULL)
|
||||
{
|
||||
((object*)_stack->pop_back())->show(tmp);
|
||||
|
||||
// reserve the correct size in stack
|
||||
unsigned int str_size = (unsigned int)out.str().size();
|
||||
ostring* str = (ostring*)_stack->allocate_back(str_size+1+sizeof(ostring), cmd_string);
|
||||
// reserve the correct size in stack
|
||||
unsigned int str_size = (unsigned int)ftell(tmp);
|
||||
ostring* str = (ostring*)_stack->allocate_back(str_size+1+sizeof(ostring), cmd_string);
|
||||
str->_size = str_size;
|
||||
|
||||
// fill the obj
|
||||
str->set(out.str().c_str(), str_size);
|
||||
// fill the obj
|
||||
if (fread(str->_value, str_size, 1, tmp) != str_size)
|
||||
ERR_CONTEXT(ret_runtime_error);
|
||||
fclose(tmp);
|
||||
}
|
||||
else
|
||||
ERR_CONTEXT(ret_runtime_error);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -29,8 +37,6 @@ void strout()
|
|||
|
||||
// make program from string in stack level 1
|
||||
if (program::parse(entry.c_str(), prog) == ret_ok)
|
||||
{
|
||||
// run it
|
||||
prog.run(*_stack, *_global_heap, &_local_heap);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -36,22 +36,19 @@ void program::test()
|
|||
{
|
||||
getline(test_file, entry);
|
||||
if (entry.substr(0,2)=="##")
|
||||
{
|
||||
cout << entry;
|
||||
cout << endl;
|
||||
}
|
||||
printf("%s\n", entry.c_str());
|
||||
else if (entry.substr(0,2)=="# ")
|
||||
{
|
||||
// indicates the status of previous test
|
||||
if (failed == false && tests > 0)
|
||||
cout<<FG_GREEN<<" PASSED"<<COLOR_OFF<<endl;
|
||||
printf(FG_GREEN " PASSED" COLOR_OFF "\n");
|
||||
failed = false;
|
||||
|
||||
// read a test title
|
||||
test_title = entry;
|
||||
is_first_step = true;
|
||||
is_test_error_shown = false;
|
||||
cout << test_title;
|
||||
printf("%s", test_title.c_str());
|
||||
}
|
||||
// treat "-> stack size should be "
|
||||
else if (entry.find(stack_size, 0) == 0)
|
||||
|
@ -72,15 +69,15 @@ void program::test()
|
|||
// count fail test and step
|
||||
if (!is_test_error_shown)
|
||||
{
|
||||
cout<<FG_RED<<" FAIL"<<COLOR_OFF<<endl;
|
||||
printf(FG_RED " FAIL" COLOR_OFF "\n");
|
||||
tests_failed++;
|
||||
is_test_error_shown = true;
|
||||
}
|
||||
steps_failed++;
|
||||
|
||||
// show failure
|
||||
cout<<"\t"<<entry<<endl;
|
||||
cout<<"\tbut real stack size is "<<FG_RED<<stk.size()<<COLOR_OFF<<endl;
|
||||
printf("\t%s\n", entry.c_str());
|
||||
printf("\tbut real stack size is " FG_RED "%d" COLOR_OFF "\n", stk.size());
|
||||
failed = true;
|
||||
}
|
||||
is_first_step = false;
|
||||
|
@ -97,32 +94,51 @@ void program::test()
|
|||
string stack_should_be = entry.substr(stack_value.size());
|
||||
string stack_is;
|
||||
string tmp;
|
||||
for (int i = 0; i < (int)stk.size(); i++)
|
||||
|
||||
FILE* tmp_file = tmpfile();
|
||||
if (tmp_file != NULL)
|
||||
{
|
||||
stringstream os;
|
||||
if (i > 0)
|
||||
stack_is += ", ";
|
||||
((object*)stk.seq_obj(i))->show(os);
|
||||
getline(os, tmp);
|
||||
stack_is += tmp;
|
||||
}
|
||||
if (stack_is != stack_should_be)
|
||||
{
|
||||
// count fail test and step
|
||||
if (!is_test_error_shown)
|
||||
char* line;
|
||||
for (int i = 0; i < (int)stk.size(); i++)
|
||||
{
|
||||
cout<<FG_RED<<" FAIL"<<COLOR_OFF<<endl;
|
||||
tests_failed++;
|
||||
is_test_error_shown = true;
|
||||
}
|
||||
steps_failed++;
|
||||
if (i > 0)
|
||||
stack_is += ", ";
|
||||
|
||||
// show failure
|
||||
cout<<"\t"<<entry<<endl;
|
||||
cout<<"\tbut real stack is "<<FG_RED<<stack_is<<COLOR_OFF<<endl;
|
||||
failed = true;
|
||||
rewind(tmp_file);
|
||||
((object*)stk.seq_obj(i))->show(tmp_file);
|
||||
line = NULL;
|
||||
if (getline(&line, NULL, tmp_file) >=0)
|
||||
{
|
||||
stack_is += line;
|
||||
free(line);
|
||||
}
|
||||
else
|
||||
{
|
||||
ERR_CONTEXT(ret_runtime_error);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (stack_is != stack_should_be)
|
||||
{
|
||||
// count fail test and step
|
||||
if (!is_test_error_shown)
|
||||
{
|
||||
printf(FG_RED " FAIL" COLOR_OFF "\n");
|
||||
tests_failed++;
|
||||
is_test_error_shown = true;
|
||||
}
|
||||
steps_failed++;
|
||||
|
||||
// show failure
|
||||
printf("\t%s\n", entry.c_str());
|
||||
printf("\tbut real stack size is " FG_RED "%s" COLOR_OFF "\n", stack_is.c_str());
|
||||
failed = true;
|
||||
}
|
||||
is_first_step = false;
|
||||
fclose(tmp_file);
|
||||
}
|
||||
is_first_step = false;
|
||||
else
|
||||
ERR_CONTEXT(ret_runtime_error);
|
||||
}
|
||||
// treat "-> error should be "
|
||||
else if (entry.find(cmd_error, 0) == 0)
|
||||
|
@ -142,15 +158,15 @@ void program::test()
|
|||
// count fail test and step
|
||||
if (!is_test_error_shown)
|
||||
{
|
||||
cout<<FG_RED<<" FAIL"<<COLOR_OFF<<endl;
|
||||
printf(FG_RED " FAIL" COLOR_OFF "\n");
|
||||
tests_failed++;
|
||||
is_test_error_shown = true;
|
||||
}
|
||||
steps_failed++;
|
||||
|
||||
// show failure
|
||||
cout<<"\t"<<entry<<endl;
|
||||
cout<<"\tbut last error is "<<FG_RED<<last_err<<COLOR_OFF<<endl;
|
||||
printf("\t%s\n", entry.c_str());
|
||||
printf("\tbut last error is " FG_RED "%d" COLOR_OFF "\n", last_err);
|
||||
failed = true;
|
||||
}
|
||||
is_first_step = false;
|
||||
|
@ -177,27 +193,27 @@ void program::test()
|
|||
// last test
|
||||
// indicates the status of previous test
|
||||
if (failed == false && tests > 0)
|
||||
cout << FG_GREEN << " PASSED" << COLOR_OFF << endl;
|
||||
printf(FG_GREEN " PASSED" COLOR_OFF "\n");
|
||||
|
||||
// cerr back
|
||||
cerr.rdbuf(cerr_old_buffer);
|
||||
|
||||
// conclusion
|
||||
cout<<endl<<"Run "<<tests<<" tests: "<<tests-tests_failed<<" passed, ";
|
||||
printf("\nRun %d tests: %d passed, ", tests, tests-tests_failed);
|
||||
if(tests_failed>0)
|
||||
cout<<FG_RED;
|
||||
cout<<tests_failed<<" failed";
|
||||
printf(FG_RED);
|
||||
printf("%d failed", tests_failed);
|
||||
if(tests_failed>0)
|
||||
cout<<COLOR_OFF;
|
||||
printf(COLOR_OFF);
|
||||
|
||||
cout<<" ("<<steps<<" steps: "<<steps-steps_failed<<" passed, ";
|
||||
printf(" (%d steps: %d passed, ", steps, steps-steps_failed);
|
||||
if(steps_failed>0)
|
||||
cout<<FG_RED;
|
||||
cout<<steps_failed<<" failed";
|
||||
printf(FG_RED);
|
||||
printf("%d failed", steps_failed);
|
||||
if(steps_failed>0)
|
||||
cout<<COLOR_OFF;
|
||||
cout<<")"<<endl;
|
||||
printf(COLOR_OFF);
|
||||
printf(")\n");
|
||||
}
|
||||
else
|
||||
cerr << "test file '"<<test_filename<<"' not found" << endl;
|
||||
fprintf(stderr, "test file '%s' not found\n", test_filename.c_str());
|
||||
}
|
||||
|
|
30
src/rpn.cpp
30
src/rpn.cpp
|
@ -143,7 +143,7 @@ struct object
|
|||
|
||||
//
|
||||
unsigned int size() { return _size; }
|
||||
void show(ostream& stream = cout);
|
||||
void show(FILE* stream = stdout);
|
||||
};
|
||||
|
||||
struct number : public object
|
||||
|
@ -344,42 +344,38 @@ struct branch : public object
|
|||
char _value[0];
|
||||
};
|
||||
|
||||
void object::show(ostream& stream)
|
||||
void object::show(FILE* stream)
|
||||
{
|
||||
//char buffer[512];
|
||||
|
||||
switch(_type)
|
||||
{
|
||||
case cmd_number:
|
||||
switch(((number*)this)->_representation)
|
||||
{
|
||||
case number::dec:
|
||||
(void)mpfr_printf(s_mpfr_printf_format.c_str(), ((number*)this)->_value.mpfr);
|
||||
//stream<<buffer;
|
||||
mpfr_fprintf(stream, s_mpfr_printf_format.c_str(), ((number*)this)->_value.mpfr);
|
||||
break;
|
||||
case number::hex:
|
||||
(void)mpfr_printf(string(MPFR_FORMAT_HEX).c_str(), ((number*)this)->_value.mpfr);
|
||||
//stream<<buffer;
|
||||
mpfr_fprintf(stream, string(MPFR_FORMAT_HEX).c_str(), ((number*)this)->_value.mpfr);
|
||||
break;
|
||||
case number::bin:
|
||||
cout<<"<binary representation TODO>";
|
||||
fprintf(stream, "<binary representation TODO>\n");
|
||||
}
|
||||
break;
|
||||
case cmd_string:
|
||||
stream << "\"" << ((ostring*)this)->_value << "\"";
|
||||
fprintf(stream, "\"%s\"", ((ostring*)this)->_value);
|
||||
break;
|
||||
case cmd_program:
|
||||
stream << "<<" << ((oprogram*)this)->_value << ">>";
|
||||
fprintf(stream, "<<%s>>", ((oprogram*)this)->_value);
|
||||
break;
|
||||
case cmd_symbol:
|
||||
stream << "'" << ((symbol*)this)->_value << "'";
|
||||
fprintf(stream, "'%s'", ((oprogram*)this)->_value);
|
||||
break;
|
||||
case cmd_keyword:
|
||||
case cmd_branch:
|
||||
stream << ((keyword*)this)->_value;
|
||||
fprintf(stream, "%s", ((keyword*)this)->_value);
|
||||
break;
|
||||
default:
|
||||
stream << "< unknown object representation >";
|
||||
fprintf(stream, "< unknown object representation >");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -749,7 +745,7 @@ public:
|
|||
if (st.size() == 1)
|
||||
{
|
||||
((object*)st.back())->show();
|
||||
cout<<endl;
|
||||
printf("\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -757,9 +753,9 @@ public:
|
|||
for (int i = st.size()-1; i>=0; i--)
|
||||
{
|
||||
if (show_sep)
|
||||
cout<<i+1<<separator;
|
||||
printf("%d%s", i+1, separator.c_str());
|
||||
((object*)st[i])->show();
|
||||
cout<<endl;
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue