#60: printf instead of cout for MPFR

This commit is contained in:
Louis Rubet 2017-05-24 14:09:52 +02:00
parent 0e1c7a7c45
commit 4d7ee13e08
6 changed files with 131 additions and 120 deletions

View file

@ -41,5 +41,5 @@ static void dump8(unsigned char* to_dump, unsigned long offset,
} }
// //
#define TRACE(x) cout<<__FUNCTION__<<": "<<(x)<<endl; #define TRACE(...) do { (void)printf(__FUNCTION__ ": "); (void)printf(__VA_ARGS__); } while(0)
#define TRACE2(x, y) cout<<__FUNCTION__<<": "<<(x)<<(y)<<endl;

View file

@ -14,15 +14,13 @@ void good_bye()
void help() void help()
{ {
// software name // software name
cout<<endl; printf("\n" ATTR_BOLD "%s" ATTR_OFF "\n", uname);
cout<<ATTR_BOLD<<uname<<ATTR_OFF<<endl;
cout<<endl;
// description // description
cout<<description<<endl<<endl; printf("%s\n\n", description);
// syntax // syntax
cout<<syntax<<endl; printf("%s\n", syntax);
// keywords // keywords
for(unsigned int i=0; i<sizeof(_keywords)/sizeof(_keywords[0]); i++) for(unsigned int i=0; i<sizeof(_keywords)/sizeof(_keywords[0]); i++)
@ -31,30 +29,30 @@ void help()
{ {
// titles in bold // titles in bold
if (_keywords[i].type==cmd_undef) if (_keywords[i].type==cmd_undef)
cout<<ATTR_BOLD; printf(ATTR_BOLD);
// show title or keyword + comment // 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) if (_keywords[i].type==cmd_undef)
cout<<ATTR_OFF; printf(ATTR_OFF);
} }
} }
cout<<endl; printf("\n");
// show mode // show mode
cout<<"Current float mode is "; printf("Current float mode is ");
switch(number::s_mode) switch(number::s_mode)
{ {
case number::std: cout << "'std'"; break; case number::std: printf("'std'"); break;
case number::fix: cout << "'fix'"; break; case number::fix: printf("'fix'"); break;
case number::sci: cout << "'sci'"; break; case number::sci: printf("'sci'"); break;
default: cout << "unknown"; 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 // calc precision and rounding mode
cout<<"Current floating point precision is "<<(int)s_mpfr_prec<<" bits"<<endl; printf("Current floating point precision is %d bits\n", (int)s_mpfr_prec);
cout<<"Current rounding mode is '"<<s_mpfr_rnd_str[s_mpfr_rnd]<<"'"<<endl; printf("Current rounding mode is '%s'\n", s_mpfr_rnd_str[s_mpfr_rnd]);
cout<<endl<<endl; printf("\n\n");
} }
void std() void std()

View file

@ -37,33 +37,28 @@ void edit(void)
{ {
MIN_ARGUMENTS(1); MIN_ARGUMENTS(1);
stringstream out; FILE* tmp = tmpfile();
if (tmp != NULL)
if (((object*)_stack->back())->_type == cmd_number)
{ {
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: char readc;
out.precision(number::s_current_precision); if (fread(&readc, 1, 1, tmp) == 1)
out.unsetf(ios_base::floatfield); rl_stuff_char(readc);
break; else
case number::fix: {
out << setprecision(number::s_current_precision) << fixed; ERR_CONTEXT(ret_runtime_error);
break; break;
case number::sci: }
out << setprecision(number::s_current_precision) << scientific;
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 // carefull : this is not a langage command
@ -129,25 +124,25 @@ void vars(void)
for (int i=0; i<(int)_global_heap->size(); i++) for (int i=0; i<(int)_global_heap->size(); i++)
{ {
(void)_global_heap->get_by_index(i, name, obj, size); (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(); obj->show();
cout<<endl; printf("\n");
} }
if (_parent_local_heap != NULL) if (_parent_local_heap != NULL)
{ {
for (int i=0; i<(int)_parent_local_heap->size(); i++) for (int i=0; i<(int)_parent_local_heap->size(); i++)
{ {
(void)_parent_local_heap->get_by_index(i, name, obj, size); (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(); obj->show();
cout<<endl; printf("\n");
} }
} }
for (int i=0; i<(int)_local_heap.size(); i++) for (int i=0; i<(int)_local_heap.size(); i++)
{ {
(void)_local_heap.get_by_index(i, name, obj, size); (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(); obj->show();
cout<<endl; printf("\n");
} }
} }

View file

@ -6,15 +6,23 @@ void instr()
if (_stack->get_type(0) != cmd_string) if (_stack->get_type(0) != cmd_string)
{ {
// write the object in stack(0) in a string and remove this obj // write the object in stack(0) in a string and remove this obj
stringstream out; FILE* tmp = tmpfile();
((object*)_stack->pop_back())->show(out); if (tmp != NULL)
{
((object*)_stack->pop_back())->show(tmp);
// reserve the correct size in stack // reserve the correct size in stack
unsigned int str_size = (unsigned int)out.str().size(); unsigned int str_size = (unsigned int)ftell(tmp);
ostring* str = (ostring*)_stack->allocate_back(str_size+1+sizeof(ostring), cmd_string); ostring* str = (ostring*)_stack->allocate_back(str_size+1+sizeof(ostring), cmd_string);
str->_size = str_size;
// fill the obj // fill the obj
str->set(out.str().c_str(), str_size); 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 // make program from string in stack level 1
if (program::parse(entry.c_str(), prog) == ret_ok) if (program::parse(entry.c_str(), prog) == ret_ok)
{
// run it // run it
prog.run(*_stack, *_global_heap, &_local_heap); prog.run(*_stack, *_global_heap, &_local_heap);
}
} }

View file

@ -36,22 +36,19 @@ void program::test()
{ {
getline(test_file, entry); getline(test_file, entry);
if (entry.substr(0,2)=="##") if (entry.substr(0,2)=="##")
{ printf("%s\n", entry.c_str());
cout << entry;
cout << endl;
}
else if (entry.substr(0,2)=="# ") else if (entry.substr(0,2)=="# ")
{ {
// indicates the status of previous test // indicates the status of previous test
if (failed == false && tests > 0) if (failed == false && tests > 0)
cout<<FG_GREEN<<" PASSED"<<COLOR_OFF<<endl; printf(FG_GREEN " PASSED" COLOR_OFF "\n");
failed = false; failed = false;
// read a test title // read a test title
test_title = entry; test_title = entry;
is_first_step = true; is_first_step = true;
is_test_error_shown = false; is_test_error_shown = false;
cout << test_title; printf("%s", test_title.c_str());
} }
// treat "-> stack size should be " // treat "-> stack size should be "
else if (entry.find(stack_size, 0) == 0) else if (entry.find(stack_size, 0) == 0)
@ -72,15 +69,15 @@ void program::test()
// count fail test and step // count fail test and step
if (!is_test_error_shown) if (!is_test_error_shown)
{ {
cout<<FG_RED<<" FAIL"<<COLOR_OFF<<endl; printf(FG_RED " FAIL" COLOR_OFF "\n");
tests_failed++; tests_failed++;
is_test_error_shown = true; is_test_error_shown = true;
} }
steps_failed++; steps_failed++;
// show failure // show failure
cout<<"\t"<<entry<<endl; printf("\t%s\n", entry.c_str());
cout<<"\tbut real stack size is "<<FG_RED<<stk.size()<<COLOR_OFF<<endl; printf("\tbut real stack size is " FG_RED "%d" COLOR_OFF "\n", stk.size());
failed = true; failed = true;
} }
is_first_step = false; is_first_step = false;
@ -97,32 +94,51 @@ void program::test()
string stack_should_be = entry.substr(stack_value.size()); string stack_should_be = entry.substr(stack_value.size());
string stack_is; string stack_is;
string tmp; string tmp;
for (int i = 0; i < (int)stk.size(); i++)
FILE* tmp_file = tmpfile();
if (tmp_file != NULL)
{ {
stringstream os; char* line;
if (i > 0) for (int i = 0; i < (int)stk.size(); i++)
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)
{ {
cout<<FG_RED<<" FAIL"<<COLOR_OFF<<endl; if (i > 0)
tests_failed++; stack_is += ", ";
is_test_error_shown = true;
}
steps_failed++;
// show failure rewind(tmp_file);
cout<<"\t"<<entry<<endl; ((object*)stk.seq_obj(i))->show(tmp_file);
cout<<"\tbut real stack is "<<FG_RED<<stack_is<<COLOR_OFF<<endl; line = NULL;
failed = true; 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 " // treat "-> error should be "
else if (entry.find(cmd_error, 0) == 0) else if (entry.find(cmd_error, 0) == 0)
@ -142,15 +158,15 @@ void program::test()
// count fail test and step // count fail test and step
if (!is_test_error_shown) if (!is_test_error_shown)
{ {
cout<<FG_RED<<" FAIL"<<COLOR_OFF<<endl; printf(FG_RED " FAIL" COLOR_OFF "\n");
tests_failed++; tests_failed++;
is_test_error_shown = true; is_test_error_shown = true;
} }
steps_failed++; steps_failed++;
// show failure // show failure
cout<<"\t"<<entry<<endl; printf("\t%s\n", entry.c_str());
cout<<"\tbut last error is "<<FG_RED<<last_err<<COLOR_OFF<<endl; printf("\tbut last error is " FG_RED "%d" COLOR_OFF "\n", last_err);
failed = true; failed = true;
} }
is_first_step = false; is_first_step = false;
@ -177,27 +193,27 @@ void program::test()
// last test // last test
// indicates the status of previous test // indicates the status of previous test
if (failed == false && tests > 0) if (failed == false && tests > 0)
cout << FG_GREEN << " PASSED" << COLOR_OFF << endl; printf(FG_GREEN " PASSED" COLOR_OFF "\n");
// cerr back // cerr back
cerr.rdbuf(cerr_old_buffer); cerr.rdbuf(cerr_old_buffer);
// conclusion // conclusion
cout<<endl<<"Run "<<tests<<" tests: "<<tests-tests_failed<<" passed, "; printf("\nRun %d tests: %d passed, ", tests, tests-tests_failed);
if(tests_failed>0) if(tests_failed>0)
cout<<FG_RED; printf(FG_RED);
cout<<tests_failed<<" failed"; printf("%d failed", tests_failed);
if(tests_failed>0) 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) if(steps_failed>0)
cout<<FG_RED; printf(FG_RED);
cout<<steps_failed<<" failed"; printf("%d failed", steps_failed);
if(steps_failed>0) if(steps_failed>0)
cout<<COLOR_OFF; printf(COLOR_OFF);
cout<<")"<<endl; printf(")\n");
} }
else else
cerr << "test file '"<<test_filename<<"' not found" << endl; fprintf(stderr, "test file '%s' not found\n", test_filename.c_str());
} }

View file

@ -143,7 +143,7 @@ struct object
// //
unsigned int size() { return _size; } unsigned int size() { return _size; }
void show(ostream& stream = cout); void show(FILE* stream = stdout);
}; };
struct number : public object struct number : public object
@ -344,42 +344,38 @@ struct branch : public object
char _value[0]; char _value[0];
}; };
void object::show(ostream& stream) void object::show(FILE* stream)
{ {
//char buffer[512];
switch(_type) switch(_type)
{ {
case cmd_number: case cmd_number:
switch(((number*)this)->_representation) switch(((number*)this)->_representation)
{ {
case number::dec: case number::dec:
(void)mpfr_printf(s_mpfr_printf_format.c_str(), ((number*)this)->_value.mpfr); mpfr_fprintf(stream, s_mpfr_printf_format.c_str(), ((number*)this)->_value.mpfr);
//stream<<buffer;
break; break;
case number::hex: case number::hex:
(void)mpfr_printf(string(MPFR_FORMAT_HEX).c_str(), ((number*)this)->_value.mpfr); mpfr_fprintf(stream, string(MPFR_FORMAT_HEX).c_str(), ((number*)this)->_value.mpfr);
//stream<<buffer;
break; break;
case number::bin: case number::bin:
cout<<"<binary representation TODO>"; fprintf(stream, "<binary representation TODO>\n");
} }
break; break;
case cmd_string: case cmd_string:
stream << "\"" << ((ostring*)this)->_value << "\""; fprintf(stream, "\"%s\"", ((ostring*)this)->_value);
break; break;
case cmd_program: case cmd_program:
stream << "<<" << ((oprogram*)this)->_value << ">>"; fprintf(stream, "<<%s>>", ((oprogram*)this)->_value);
break; break;
case cmd_symbol: case cmd_symbol:
stream << "'" << ((symbol*)this)->_value << "'"; fprintf(stream, "'%s'", ((oprogram*)this)->_value);
break; break;
case cmd_keyword: case cmd_keyword:
case cmd_branch: case cmd_branch:
stream << ((keyword*)this)->_value; fprintf(stream, "%s", ((keyword*)this)->_value);
break; break;
default: default:
stream << "< unknown object representation >"; fprintf(stream, "< unknown object representation >");
break; break;
} }
} }
@ -749,7 +745,7 @@ public:
if (st.size() == 1) if (st.size() == 1)
{ {
((object*)st.back())->show(); ((object*)st.back())->show();
cout<<endl; printf("\n");
} }
else else
{ {
@ -757,9 +753,9 @@ public:
for (int i = st.size()-1; i>=0; i--) for (int i = st.size()-1; i>=0; i--)
{ {
if (show_sep) if (show_sep)
cout<<i+1<<separator; printf("%d%s", i+1, separator.c_str());
((object*)st[i])->show(); ((object*)st[i])->show();
cout<<endl; printf("\n");
} }
} }
} }