Erlang-style numbers in Lua...

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

Erlang-style numbers in Lua...

Joseph Stewart
I hacked the first part of supporting "Erlang-style" numbers in Lua. To explain what I mean, Erlang support numeric constant in the form:

BASE#NUMBER

such that you could specify the binary constant for 255 as 2#11111111, or the base-36 constant for 46655 as 36#zzz.

I did a "search-and-destroy" method to find where to make the changes. Looks like llex.c::read_numeral() (which looks for the '#' in the right place to tag something as a TK_NUMBER) and lobject.c::lua_str2number () (to decide whether to call strtod() or strtoul()) need to be changed for this purpose. I'd considered adding another lua datatype for integers, but decided that required too many plumbing changes.

I don't know that my work could be considered elegant or even safe, but if anyone is curious, I'll throw together a quick patch for lua-5.0.2. I haven't looked at the structure of lua-5.1 yet, so I don't know how similar this hack will be for it.

The second part of my hack is to look at coercing string.format() to print numbers in these arbitrary bases... I'm not sure when I'll get around to this... keep posted!

(

To be quite honest, I really only want to enter hex, decimal, and binary numbers in my code, so this might be overkill for this purpose, considering the suggestions others have made to use this idiom:

function x(s) return tonumber(s,16) end -- for hex and...
function b(s) return tonumber(s,2) end -- for binary

and to be truly useful, a bit manipulation library need to be added (such as lhf's lbase)

)

I'm done ranting for a while!

-joe
Reply | Threaded
Open this post in threaded view
|

Re: Erlang-style numbers in Lua...

Joseph Stewart


On 2/9/06, Joseph Stewart <[hidden email]> wrote:
I hacked the first part of supporting "Erlang-style" numbers in Lua. To explain what I mean, Erlang support numeric constant in the form:

BASE#NUMBER

such that you could specify the binary constant for 255 as 2#11111111, or the base-36 constant for 46655 as 36#zzz.

I did a "search-and-destroy" method to find where to make the changes. Looks like llex.c::read_numeral() (which looks for the '#' in the right place to tag something as a TK_NUMBER) and lobject.c::lua_str2number () (to decide whether to call strtod() or strtoul()) need to be changed for this purpose. I'd considered adding another lua datatype for integers, but decided that required too many plumbing changes.

I don't know that my work could be considered elegant or even safe, but if anyone is curious, I'll throw together a quick patch for lua-5.0.2. I haven't looked at the structure of lua-5.1 yet, so I don't know how similar this hack will be for it.

The second part of my hack is to look at coercing string.format() to print numbers in these arbitrary bases... I'm not sure when I'll get around to this... keep posted!

(

To be quite honest, I really only want to enter hex, decimal, and binary numbers in my code, so this might be overkill for this purpose, considering the suggestions others have made to use this idiom:

function x(s) return tonumber(s,16) end -- for hex and...
function b(s) return tonumber(s,2) end -- for binary

and to be truly useful, a bit manipulation library need to be added (such as lhf's lbase)

Whoops, there is no "lbase". To give proper credit... I meant to refer to Reuben Thomas' "bitlib" (http://rrt.sc3d.org/Software/Lua/). Thank's for the great work, Reuben!

)

I'm done ranting for a while!

-joe