lua-users home
lua-l archive

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


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 --------------- Mike.Palmer@tus.ssi1.com ----
----    The opinions expressed here are mine, and not     ----
----      those of my employer or Texas Instruments.      ----