absyntax_utils/absyntax_utils.cc
changeset 576 8368ec909825
parent 571 97b85630e496
child 587 1ecf916cc397
equal deleted inserted replaced
575:a1b63f776535 576:8368ec909825
   137 }
   137 }
   138 
   138 
   139 
   139 
   140 /* extract the value of a real from an real_c object !! */
   140 /* extract the value of a real from an real_c object !! */
   141 /* NOTE: it must ignore underscores! */
   141 /* NOTE: it must ignore underscores! */
   142 real64_t extract_real_value(symbol_c *sym) {
   142 /* From iec_bison.yy
       
   143  *  real:
       
   144  *   real_token		{$$ = new real_c($1, locloc(@$));}
       
   145  * | fixed_point_token	{$$ = new real_c($1, locloc(@$));}
       
   146  *
       
   147  * From iec_flex.ll
       
   148  * {real}			{yylval.ID=strdup(yytext); return real_token;}
       
   149  * {fixed_point}		{yylval.ID=strdup(yytext); return fixed_point_token;}
       
   150  *
       
   151  * real		{integer}\.{integer}{exponent}
       
   152  * fixed_point		{integer}\.{integer}
       
   153  * exponent        [Ee]([+-]?){integer}
       
   154  * integer         {digit}((_?{digit})*)
       
   155  */
       
   156 real64_t extract_real_value(symbol_c *sym, bool *overflow = NULL) {
   143   std::string str = "";
   157   std::string str = "";
   144   char *endptr;
   158   char *endptr;
   145   real_c * real_sym;
   159   real_c * real_sym;
   146   uint64_t ret;
   160   real64_t ret;
   147 
   161 
   148   if ((real_sym = dynamic_cast<real_c *>(sym)) == NULL) ERROR;
   162   if ((real_sym = dynamic_cast<real_c *>(sym)) == NULL) ERROR;
   149   for(unsigned int i = 3; i < strlen(real_sym->value); i++)
   163   for(unsigned int i = 0; i < strlen(real_sym->value); i++)
   150     if (real_sym->value[i] != '_') str += real_sym->value[i];
   164     if (real_sym->value[i] != '_') str += real_sym->value[i];
   151     
   165     
   152   errno = 0; // since strtoXX() may legally return 0, we must set errno to 0 to detect errors correctly!
   166   errno = 0; // since strtoXX() may legally return 0, we must set errno to 0 to detect errors correctly!
   153   #if    (real64_t  == float)
   167   #if    (real64_t  == float)
   154     ret = strtof(str.c_str(), NULL);
   168     ret = strtof(str.c_str(), NULL);
   157   #elif  (real64_t  == long_double)
   171   #elif  (real64_t  == long_double)
   158     ret = strtold(str.c_str(), NULL);
   172     ret = strtold(str.c_str(), NULL);
   159   #else 
   173   #else 
   160     #error Could not determine which data type is being used for real64_t (defined in absyntax.hh). Aborting!
   174     #error Could not determine which data type is being used for real64_t (defined in absyntax.hh). Aborting!
   161   #endif
   175   #endif
   162   if (errno != 0) ERROR;
   176   if (overflow != NULL)
       
   177     *overflow = (errno == ERANGE);
       
   178   if ((errno != 0) && (errno != ERANGE)) 
       
   179     ERROR;
   163 
   180 
   164   return ret;
   181   return ret;
   165 }
   182 }
   166 
   183 
   167 
   184