Browse Source

Simple converters from strings to double, int and unsigned int added to common.h

ObjPtr
Michael Uleysky 9 years ago
parent
commit
5bed99ad81
  1. 25
      include/common.h
  2. 17
      src/parser/lexical.l

25
include/common.h

@ -289,4 +289,29 @@ ObjectBase* Get(const ObjectList* input)
return ob->Get(name->Value()); return ob->Get(name->Value());
} }
// Simple conversion functions
inline bool str2double(const char* str, double* res)
{
char* pos;
*res=strtod(str,&pos);
if('\0'!=*pos) return false; else return true;
}
inline bool str2int(const char* str, int64_t* res)
{
char* pos;
*res=strtoll(str,&pos,0);
if('\0'!=*pos) return false; else return true;
}
inline bool str2uint(const char* str, uint64_t* res)
{
char* pos;
*res=strtoull(str,&pos,0);
if('\0'!=*pos) return false; else return true;
}
inline bool str2double(const std::string& str, double* res) {return str2double(str.c_str(),res);}
inline bool str2int(const std::string& str, int64_t* res) {return str2int(str.c_str(),res);}
inline bool str2uint(const std::string& str, int64_t* res) {return str2uint(str.c_str(),res);}
#endif #endif

17
src/parser/lexical.l

@ -101,8 +101,21 @@ std::string str;
} }
[a-zA-Z][a-zA-Z0-9_]* COUT(MOREDEBUG)<<"NAME("<<yytext<<")"; BEGIN(PARSE); yylval_param->str=new std::string(yytext); setyyllocp; yynextsym; return NAME; [a-zA-Z][a-zA-Z0-9_]* COUT(MOREDEBUG)<<"NAME("<<yytext<<")"; BEGIN(PARSE); yylval_param->str=new std::string(yytext); setyyllocp; yynextsym; return NAME;
<PARSE>[+\-*/^] COUT(MOREDEBUG)<<" OPERATION("<<yytext<<")"; setyyllocp; yynextsym; return yytext[0]; <PARSE>[+\-*/^] COUT(MOREDEBUG)<<" OPERATION("<<yytext<<")"; setyyllocp; yynextsym; return yytext[0];
<PARSE>[0-9]+ COUT(MOREDEBUG)<<" INTEGER("<<yytext<<")"; yylval_param->i=atoll(yytext); setyyllocp; yynextsym; return INTEGER; <PARSE>[0-9]+ {
<PARSE>[0-9]+(\.[0-9]*)?([eE][+-][0-9]+)? COUT(MOREDEBUG)<<" REAL("<<yytext<<")"; yylval_param->r=atof(yytext); setyyllocp; yynextsym; return REAL; 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);
yylval_param->i=atoll(yytext);
setyyllocp;
yynextsym;
return INTEGER;
}
<PARSE>[0-9]+(\.[0-9]*)?([eE][+-][0-9]+)? {
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);
setyyllocp;
yynextsym;
return REAL;
}
<PARSE>[TF] COUT(MOREDEBUG)<<" BOOL("<<yytext<<")"; yylval_param->b=(yytext[0]=='T')?true:false; setyyllocp; yynextsym; return BOOL; <PARSE>[TF] COUT(MOREDEBUG)<<" BOOL("<<yytext<<")"; yylval_param->b=(yytext[0]=='T')?true:false; setyyllocp; yynextsym; return BOOL;
<PARSE>\( COUT(MOREDEBUG)<<" OBRACE()"; setyyllocp; yynextsym; return OBRACE; <PARSE>\( COUT(MOREDEBUG)<<" OBRACE()"; setyyllocp; yynextsym; return OBRACE;
<PARSE>\) COUT(MOREDEBUG)<<" CBRACE()"; setyyllocp; yynextsym; return CBRACE; <PARSE>\) COUT(MOREDEBUG)<<" CBRACE()"; setyyllocp; yynextsym; return CBRACE;

Loading…
Cancel
Save