diff --git a/src/debug.h b/src/debug.h index 295c665..f7752fa 100644 --- a/src/debug.h +++ b/src/debug.h @@ -41,5 +41,5 @@ static void dump8(unsigned char* to_dump, unsigned long offset, } // -#define TRACE(x) cout<<__FUNCTION__<<": "<<(x)<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;ipop_back())->show(out); - - // edit: stuff chars using readline facility - string str = out.str(); - - for(int i=0;isize(); i++) { (void)_global_heap->get_by_index(i, name, obj, size); - cout<<"var "<_type]<<", value "; + printf("var %d: name '%s', type %s, value ", i+1, name.c_str(), cmd_type_string[obj->_type]); obj->show(); - cout<size(); i++) { (void)_parent_local_heap->get_by_index(i, name, obj, size); - cout<<"local var "<_type]<<", value "; + printf("local var %d: name '%s', type %s, value ", i+1, name.c_str(), cmd_type_string[obj->_type]); obj->show(); - cout<_type]<<", value "; + printf("local var %d: name '%s', type %s, value ", i+1, name.c_str(), cmd_type_string[obj->_type]); obj->show(); - cout<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); - } } diff --git a/src/rpn-test-core.h b/src/rpn-test-core.h index 3643b9b..0282edf 100644 --- a/src/rpn-test-core.h +++ b/src/rpn-test-core.h @@ -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< 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< 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< 0) + stack_is += ", "; - // show failure - cout<<"\t"<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< 0) - cout << FG_GREEN << " PASSED" << COLOR_OFF << endl; + printf(FG_GREEN " PASSED" COLOR_OFF "\n"); // cerr back cerr.rdbuf(cerr_old_buffer); // conclusion - cout<0) - cout<0) - cout<0) - cout<0) - cout<_representation) { case number::dec: - (void)mpfr_printf(s_mpfr_printf_format.c_str(), ((number*)this)->_value.mpfr); - //stream<_value.mpfr); break; case number::hex: - (void)mpfr_printf(string(MPFR_FORMAT_HEX).c_str(), ((number*)this)->_value.mpfr); - //stream<_value.mpfr); break; case number::bin: - cout<<""; + fprintf(stream, "\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<=0; i--) { if (show_sep) - cout<show(); - cout<