diff --git a/version2.0/nasal_misc.h b/version2.0/nasal_misc.h index 632a671..71a7291 100644 --- a/version2.0/nasal_misc.h +++ b/version2.0/nasal_misc.h @@ -12,52 +12,85 @@ '0xdeadbeef' '0xDEADBEEF' '0o71230' + '1e23' + '1E-123' + '1.34E10' */ +inline bool check_hex_string(std::string str) +{ + int len=str.length(); + for(int i=2;i'7') + return false; + return true; +} +inline bool check_dec_string(std::string str) +{ + int len=str.length(); + int dot_cnt=0; + int i=0; + for(;i'9') + return false; + } + if(str[i]=='e' || str[i]=='E') + { + ++i; + if(i==len) + return false; + if(str[i]=='-') + { + ++i; + if(i==len) + return false; + } + for(;i='9') + return false; + } + if(dot_cnt>1 || str[0]=='.' || (!dot_cnt && str[0]=='0')) + return false; + return true; +} + bool check_numerable_string(std::string str) { - if(str.length()>1 && str[0]=='-') + if(!str.length()) + return false; + if(str[0]=='-' && str.length()>1) { - // in lexer this statements are useless - // because lexer judge a number that begins with 0~9 (or 0x for hex & 0o for oct) - std::string temp=""; - int str_len=str.length(); - for(int i=1;i2 && str[0]=='0' && str[1]=='x') - { - int str_len=str.length(); - for(int i=2;i2 && str[0]=='0' && str[1]=='o') - { - int str_len=str.length(); - for(int i=2;i1) || (str[0]=='.') || (!float_dot_cnt && str[0]=='0')) - return false; - return true; - } + return check_oct_string(str); + else if('0'<=str[0] && str[0]<='9') + return check_dec_string(str); return false; } @@ -65,75 +98,102 @@ bool check_numerable_string(std::string str) trans_string_to_number: convert string to number */ +inline double hex_to_double(std::string str) +{ + double ret=0; + int len=str.length(); + double num_pow=1; + for(int i=len-1;i>1;--i) + { + if('0'<=str[i] && str[i]<='9') + ret+=num_pow*(str[i]-'0'); + else if('a'<=str[i] && str[i]<='f') + ret+=num_pow*(str[i]-'a'+10); + else if('A'<=str[i] && str[i]<='F') + ret+=num_pow*(str[i]-'A'+10); + num_pow*=16; + } + return ret; +} +inline double oct_to_double(std::string str) +{ + double ret=0; + int len=str.length(); + double num_pow=1; + for(int i=len-1;i>1;--i) + { + ret+=num_pow*(str[i]-'0'); + num_pow*=8; + } + return ret; +} +inline double dec_to_double(std::string str) +{ + double ret=0; + int len=str.length(); + int i=0; + for(;i1 && str[0]=='-') + double ret_num=0; + if(!str.length()) + return 0; + if(str[0]=='-' && str.length()>1) { - // in parse this statements are useless - // because lexer recognizes a number that begins with a '0'~'9' char - std::string temp=""; - int str_len=str.length(); - for(int i=1;i1;--i) - { - if('0'<=str[i] && str[i]<='9') - hex_number+=bit_pow*(str[i]-'0'); - else if('a'<=str[i] && str[i]<='f') - hex_number+=bit_pow*(10+str[i]-'a'); - else - hex_number+=bit_pow*(10+str[i]-'A'); - bit_pow<<=4; - } - trans_str_number=(double)hex_number; - } - else if(str[0]=='0' && str[1]=='o') - { - long long int oct_number=0,bit_pow=1; - for(int i=str.length()-1;i>1;--i) - { - oct_number+=bit_pow*(str[i]-'0'); - bit_pow<<=3; - } - trans_str_number=(double)oct_number; - } - else - { - int float_dot_place=str.length(); - int str_len=str.length(); - double pow; - for(int i=0;i=0;--i) - { - trans_str_number+=pow*(double(str[i]-'0')); - pow*=10; - } - } - if(is_negative) - trans_str_number*=-1; - return trans_str_number; + else if(str[0]=='-' && str.length()==1) + return 0; + if(str.length()==1 && '0'<=str[0] && str[0]<='9') + ret_num=(double)(str[0]-'0'); + else if(str.length()>2 && str[0]=='0' && str[1]=='x') + ret_num=hex_to_double(str); + else if(str.length()>2 && str[0]=='0' && str[1]=='o') + ret_num=oct_to_double(str); + else if('0'<=str[0] && str[0]<='9') + ret_num=dec_to_double(str); + return is_negative?-ret_num:ret_num; } /*