lua-users home
lua-l archive

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

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


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!