lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Todor Totev wrote:

The only thing I care right now is the possibility to enter hexadecimal
numbers in my scripts and give them as parameters to C functions.
And to print them of course but Roberto already made the neccessary
changes for that :-)
Here is an updated llex.c's read_numeral from the LuaPlus for Lua 5.1 alpha that does hex numbers as 0x12345678. I'm not sure if it needs changes to work against Lua 5.1 beta. I haven't had time to revisit the code base since the beta came out. Certainly, for 64-bit numbers, numDigits needs to be increased to 16.

Josh

-------

/* LUA_NUMBER */
static void read_numeral (LexState *ls, SemInfo *seminfo) {
 int isReal = 0;
 int startsWithZero = ls->current == '0';

 while (lex_isdigit(ls->current)) {
   save_and_next(ls);
 }
 if (ls->current == '.') {
   save_and_next(ls);
   if (ls->current == '.') {
     save_and_next(ls);
     luaX_lexerror(ls,
                "ambiguous syntax (decimal point x string concatenation)",
                TK_NUMBER);
   }
   isReal = 1;
 }
 if (startsWithZero) {
   if (ls->current == 'x') {
     /* Process a hex number */
     int ch = 0;
     int c = 0;
     int i = 0;
     int numDigits = 8;
     next(ls);
     do {
       ch = tolower(ls->current);
       if (lex_isdigit(ch))
         c = 16*c + (ch-'0');
       else if (ch >= 'a' && ch <= 'f')
         c = 16*c + (ch-'a') + 10;
       next(ls);
       ch = tolower(ls->current);
} while (++i<numDigits && (lex_isdigit(ch) || (ch >= 'a' && ch <= 'f')));
     seminfo->r = c;
     return;
   }
 }
 while (lex_isdigit(ls->current)) {
   save_and_next(ls);
 }
 if (ls->current == 'e' || ls->current == 'E') {
   save_and_next(ls);  /* read `E' */
   if (ls->current == '+' || ls->current == '-')
     save_and_next(ls);  /* optional exponent sign */
   while (lex_isdigit(ls->current)) {
     save_and_next(ls);
   }
 }
 save(ls, '\0');
 if (!luaO_str2d(luaZ_buffer(ls->buff), &seminfo->r))
   luaX_lexerror(ls, "malformed number", TK_NUMBER);
}