lua-users home
lua-l archive

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


Some time ago, I added support for several different additional
formats in Lua. It's been a while, and I think I added them in 
version 2.X (so it was some time ago), but it wasn't that 
difficult to do, and I suspect that it would be fairly easy
to rip out my modifications and port them into current and future
versions of Lua. I don't remember if I ever changed the way 
values printed - as I recall I didn't - but it appears from 
recent postings that this isn't a significant problem.

My modifications were different than what you're proposing, but 
they still used what I considered to be a reasonable syntax. I'm
an electrical engineer, and one of the common notations that EE
software uses is suffixes that denote various powers of 10. For 
example, a 'u' suffix denotes 'micro' (1e-6), 'k' denotes 'kilo' 
(1e3) and so on. The full list is:

     f      femto   10^-15       example: 1.23f or 1.23F
     p      pico    10^-12
     n      nano    10^-9
     u      micro   10^-6
     m      milli   10^-3
     k      kilo    10^3
     meg    mega    10^6    (use meg to avoid confusion with milli)
     x      mega    10^6    (alternative for meg - used in some EE
     g      giga    10^9
     h      hex     base 16
     d      decimal base 10
     b      binary  base 2
In addition, once the suffix has been parsed far enough to 
guarantee uniqueness, additional characters are skipped up to
the end of the suffix. That allows me to write more descriptive
suffixes (like 'mhenry' for millihenry, 'pF' for picofarad, 
and 'kohm' for kiloohm).

There might be some confusion in some circles regarding the use
of 'x' for mega, since it would also (because of C) make a good
'hex' flag, but that could easily be changed. The only caveat I
found was that hex values had to begin with a digit (0-9), or 
the value would be interpreted as a variable name. Again, that 
seems like a minor issue.

If anyone is interested, I can send you my modified lex.c file.
Remember, though, it's from an earlier version of lua, so you
shouldn't use it directly.

Is there enough interest that something like this could be 
added to a general release of Lua? 

You can e-mail me at

-- Mike --

>  Roberto and lhf:
>  Thanks for the reply. I knew of the solutions you proposed.
> With due respect, I thing they are a bit cumbersome.
> Why not add support for the "\x0123EFC" notation in lua 3.2?
>  I'm sure people who use lua to configure and/or test system or
> communications software will be happy. They (we) think in hexa. 
> Since lua 3.1 strings can contain arbitrary data, why not also
> make the notation for specifying that data more "arbitrary"?