Lua enhancement

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Lua enhancement

Ilja V.Levinson
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.
  [hidden email]    |             **********
-----------------------------------------------------------------

Reply | Threaded
Open this post in threaded view
|

Re: Lua enhancement (octal, hex numerics)

Mike Palmer
I made a similar change to the previous release of Lua. The particular
method I used was to append 'b' for binary numbers and 'h' for hex
numbers (I don't think I included 'o', but its addition would be 
trivial). The motivation to begin the task was somewhat different: I am
an electrical engineer, and generally want to use numeric constants
with exponents in multiples of three (mega, kilo, milli, micro, etc.).
A circuit simulator called SPICE accomodates this by adding character 
suffixes to numeric values. So, 3e-6 becomes 3u, 4.73e-3 becomes 4.73m, 
and 5e3 becomes 5k. 1e6 is handled with either the three character 'meg' 
suffix, or with an 'x' (more recent, and only used in some versions 
of SPICE). I implemented those suffixes first, then added the support 
for hexadecimal numbers.

The only caveat is that hex numbers must begin with a numeric value, 
or they are treated as variables. To enter the value FFEFh, it must
begin with a zero: 0FFEFh. The zero is used (by Lua) to flag the value 
as a numeric value, and A-F following a numeric value are treated as 
hex values if the value ends with 'h'. 

The remaining issue with either approach is printing the values. I did
not modify the output functions, so although a user may enter values as
hex, they will be printed as decimal. Similarly, values entered in 
engineering notation will be printed in scientific notation.

Last, users may think that values entered in hex or binary format are
represented internally as integers.

-- Mike --


> 
> 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
> 

-- 
---- Mike Palmer --------------- [hidden email] ----
----    The opinions expressed here are mine, and not     ----
----      those of my employer or Texas Instruments.      ----