Browse Source

Using ToString instead of std::to_string with different format conversion for double

ObjPtr
Michael Uleysky 9 years ago
parent
commit
5703808781
  1. 13
      include/common.h
  2. 6
      src/parser/lexical.l

13
include/common.h

@ -1,6 +1,7 @@
#ifndef COMMON_H #ifndef COMMON_H
#define COMMON_H #define COMMON_H
#include <cctype> #include <cctype>
#include <cfloat>
#include <iostream> #include <iostream>
#include <list> #include <list>
#include <memory> #include <memory>
@ -413,4 +414,16 @@ inline WordList Split(const char* str, const std::string& delims) {return Split(
inline WordList Split(const std::string& str, const char* delims) {return Split(str,std::string(delims),false);} inline WordList Split(const std::string& str, const char* delims) {return Split(str,std::string(delims),false);}
inline WordList Split(const char* str, const char* delims) {return Split(std::string(str),std::string(delims),false);} inline WordList Split(const char* str, const char* delims) {return Split(std::string(str),std::string(delims),false);}
// We use different format for conversion of double to string
template<class T>
inline std::string ToString(T n) { return std::to_string(n); }
template<>
inline std::string ToString<double>(double n)
{
char buffer[32];
int i;
i=snprintf(buffer,32,"%.*G",DBL_DIG,n);
return std::string(buffer,i);
}
#endif #endif

6
src/parser/lexical.l

@ -104,7 +104,7 @@ std::string str;
<PARSE>[+\-*/^] COUT(MOREDEBUG)<<" OPERATION("<<yytext<<")"; setyyllocp; yynextsym; return yytext[0]; <PARSE>[+\-*/^] COUT(MOREDEBUG)<<" OPERATION("<<yytext<<")"; setyyllocp; yynextsym; return yytext[0];
<PARSE>[0-9]+ { <PARSE>[0-9]+ {
COUT(MOREDEBUG)<<" INTEGER("<<yytext<<")"; COUT(MOREDEBUG)<<" INTEGER("<<yytext<<")";
if(!str2int(yytext,&yylval_param->i)) yyerrormessage(std::string("failed conversion to integer of ")+yytext+" at position "+std::to_string(yyextra->state.curpos),-1); if(!str2int(yytext,&yylval_param->i)) yyerrormessage(std::string("failed conversion to integer of ")+yytext+" at position "+ToString(yyextra->state.curpos),-1);
yylval_param->i=atoll(yytext); yylval_param->i=atoll(yytext);
setyyllocp; setyyllocp;
yynextsym; yynextsym;
@ -112,7 +112,7 @@ std::string str;
} }
<PARSE>[0-9]+(\.[0-9]*)?([eE][+-][0-9]+)? { <PARSE>[0-9]+(\.[0-9]*)?([eE][+-][0-9]+)? {
COUT(MOREDEBUG)<<" REAL("<<yytext<<")"; COUT(MOREDEBUG)<<" REAL("<<yytext<<")";
if(!str2double(yytext,&yylval_param->r)) yyerrormessage(std::string("failed conversion to double of ")+yytext+" at position "+std::to_string(yyextra->state.curpos),-1); if(!str2double(yytext,&yylval_param->r)) yyerrormessage(std::string("failed conversion to double of ")+yytext+" at position "+ToString(yyextra->state.curpos),-1);
setyyllocp; setyyllocp;
yynextsym; yynextsym;
return REAL; return REAL;
@ -130,7 +130,7 @@ std::string str;
<PREPROC>\n BEGIN(0); yynextline; <PREPROC>\n BEGIN(0); yynextline;
<PARSE,INITIAL>\#.* yynextsym; <PARSE,INITIAL>\#.* yynextsym;
<PARSE,PREPROC>\" yy_push_state(PSTRING,yyscanner); str.erase(); yyextra->states.push(yyextra->state); yynextsym; <PARSE,PREPROC>\" yy_push_state(PSTRING,yyscanner); str.erase(); yyextra->states.push(yyextra->state); yynextsym;
<PARSE,PREPROC,INITIAL>. yyerrormessage(std::string("unknown symbol ")+yytext+" at position "+std::to_string(yyextra->state.curpos),-1); <PARSE,PREPROC,INITIAL>. yyerrormessage(std::string("unknown symbol ")+yytext+" at position "+ToString(yyextra->state.curpos),-1);
<PSTRING,INCLUDE>\\\\ str+='\\'; yynextsym; <PSTRING,INCLUDE>\\\\ str+='\\'; yynextsym;
<PSTRING,INCLUDE>\\\" str+='\"'; yynextsym; <PSTRING,INCLUDE>\\\" str+='\"'; yynextsym;
<PSTRING>\n str+=yytext[0]; yynextline; <PSTRING>\n str+=yytext[0]; yynextline;

Loading…
Cancel
Save