lua-users home
lua-l archive

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


Hi,
At this moment Lua lexer accepts only decimal numeric constants. I think,
that using octal/hexadecimal digits would be more  comfortable for some
users. So, I wrote a simple patch to lex.c to implement this feature. It
seems, there is only one bug - a token '0x' is interpereted as number '0'.

I also think about to implement binary arithmetic commands like '&' - binary
AND, '|' - binary OR and '^' - binary XOR. I have a look into sources - this
will be a non-trivial work.  '^' symbol also will conflict with pow()
function. May be, a '^^' or '**' (Ada-like) would be more suitable for pow()
?

Any suggestions?

Regards,
  Ilja

---------------------------------------------------------------
All changes are between #ifdef _OSK/#endif
------------------------ cut here -----------------------------
*** orig/lex.c	Mon Jul 28 14:29:00 1997
--- lex.c	Mon Jul 28 15:19:00 1997
***************
*** 20,26 ****
--- 20,30 ----
  
  
  #define next() (current = zgetc(lex_z))
+ #ifdef _OSK   /* possible a bug too -  yytext[] is not zeroed
automatically */
+ #define save(x) (yytext[tokensize++] = (x), yytext[tokensize] = '\0')
+ #else
  #define save(x) (yytext[tokensize++] = (x))
+ #endif
  #define save_and_next()  (save(current), next())
  
  
***************
*** 272,277 ****
--- 276,284 ----
  {
    static int linelasttoken = 0;
    double a;
+ #ifdef _OSK
+   double base;
+ #endif
    int buffsize = MINBUFF;
    char *yytext = luaI_buffer(buffsize);
    yytext[1] = yytext[2] = yytext[3] = 0;
***************
*** 288,294 ****
--- 295,305 ----
          linelasttoken = lua_linenumber;
          continue;
  
+ #ifdef _OSK  /* only OS-9 related, ignore this */
+       case ' ': case '\t': case '\012':  /* CR: to avoid problems with DOS
*/
+ #else
        case ' ': case '\t': case '\r':  /* CR: to avoid problems with DOS
*/
+ #endif
          next();
          continue;
  
***************
*** 412,423 ****
--- 423,471 ----
        case '0': case '1': case '2': case '3': case '4':
        case '5': case '6': case '7': case '8': case '9':
  	a=0.0;
+ #ifdef _OSK  /* The main code */
+ 		base = 10.0;
+ 		if(current == '0') {
+ 			save_and_next();
+ 			if(current == 'x' || current == 'X') {
+ 				base = 16.0;
+ 				save_and_next();
+ 			}
+ 			else {
+ 				base = 8.0;
+ 			}
+ 		}
+ 
+ 		while (isxdigit((unsigned char)current)) {
+ 		  int digit;
+ 		  
+ 		  if(isdigit((unsigned char)current))
+ 		  	digit = current - '0';
+ 		  else if(current >= 'a')
+ 		  	digit = current -'a' + 10;
+ 		  else
+ 		  	digit = current -'A' + 10;
+ 
+ 		  if(digit >= base)
+ 			luaI_syntaxerror(
+ 			"numeric constant contains digit beyond the radix");
+           a = base*a+digit;
+           save_and_next();
+         } 
+ #else
          do {
            a=10.0*a+(current-'0');
            save_and_next();
          } while (isdigit((unsigned char)current));
+ #endif
+ 
          if (current == '.') {
            save_and_next();
+ #ifdef _OSK
+           if(base != 10.0)
+ 			luaI_syntaxerror(
+ 				"floating constant must be in radix 10 only");
+ #endif
            if (current == '.')
              luaI_syntaxerror(
                "ambiguous syntax (decimal point x string concatenation)");
---------------------------------- cut here -----------------------

--
-----------------------------------------------------------------
    Ilja V.Levinson      | If fifty million people say a foolish
 Yekaterinburg, Russia   | thing, it is still a foolish thing.
  lev@odusv.oduurl.ru    |             **********
-----------------------------------------------------------------