From 5bed99ad817ffe1d6b1dc000a45b4438c6d73866 Mon Sep 17 00:00:00 2001 From: Michael Uleysky Date: Fri, 2 Oct 2015 20:01:49 +1000 Subject: [PATCH] Simple converters from strings to double, int and unsigned int added to common.h --- include/common.h | 25 +++++++++++++++++++++++++ src/parser/lexical.l | 17 +++++++++++++++-- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/include/common.h b/include/common.h index 45a2c7c..372cf07 100644 --- a/include/common.h +++ b/include/common.h @@ -289,4 +289,29 @@ ObjectBase* Get(const ObjectList* input) 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 diff --git a/src/parser/lexical.l b/src/parser/lexical.l index 0adcbc8..9c8cb07 100644 --- a/src/parser/lexical.l +++ b/src/parser/lexical.l @@ -101,8 +101,21 @@ std::string str; } [a-zA-Z][a-zA-Z0-9_]* COUT(MOREDEBUG)<<"NAME("<str=new std::string(yytext); setyyllocp; yynextsym; return NAME; [+\-*/^] COUT(MOREDEBUG)<<" OPERATION("<[0-9]+ COUT(MOREDEBUG)<<" INTEGER("<i=atoll(yytext); setyyllocp; yynextsym; return INTEGER; -[0-9]+(\.[0-9]*)?([eE][+-][0-9]+)? COUT(MOREDEBUG)<<" REAL("<r=atof(yytext); setyyllocp; yynextsym; return REAL; +[0-9]+ { + COUT(MOREDEBUG)<<" INTEGER("<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; + } +[0-9]+(\.[0-9]*)?([eE][+-][0-9]+)? { + COUT(MOREDEBUG)<<" REAL("<r)) yyerrormessage(std::string("failed conversion to double of ")+yytext+" at position "+std::to_string(yyextra->state.curpos),-1); + setyyllocp; + yynextsym; + return REAL; + } [TF] COUT(MOREDEBUG)<<" BOOL("<b=(yytext[0]=='T')?true:false; setyyllocp; yynextsym; return BOOL; \( COUT(MOREDEBUG)<<" OBRACE()"; setyyllocp; yynextsym; return OBRACE; \) COUT(MOREDEBUG)<<" CBRACE()"; setyyllocp; yynextsym; return CBRACE;