lua-users home
lua-l archive

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

As an exercise, I followed the instructions (my own!) in config to create
a version of Lua that deals only with integers.
Here are the differences in the code, with my comments.

This is the first step. It should have been enough, but see below.

diff -r lua/config lua-int/config

This next change is just me being lazy.
I should have gone through the code and changed all doubles (there are a few;
they occur in lua.h, lapi.c, lauxlib.h, liolib.c, lmathlib.c).
But this is simpler, and does not affect the code, because lua.h is always
included after the standard headers and before all Lua internal headers
(this is on purpose, so that you can add/change things in lua.h and
they'll affect only Lua code).
However, if your own host code includes lua.h and uses double, then defining
double as long in lua.h you give you headaches...

diff -r lua/include/lua.h lua-int/include/lua.h
> #define double long

The instructions in config say that the changes below are optional, but this
is true only for using floats instead of doubles, not for using longs.
So, the instructions are wrong in this case, but luckily the compiler will
at least warn you (gcc does).

diff -r lua/src/lib/liolib.c lua-int/src/lib/liolib.c
<   if (fscanf(f, "%lf", &d) == 1) {
>   if (fscanf(f, "%ld", &d) == 1) {
<       status = status && fprintf(f, "%.16g", lua_tonumber(L, arg)) > 0;
>       status = status && fprintf(f, "%ld", lua_tonumber(L, arg)) > 0;

diff -r lua/src/llimits.h lua-int/src/llimits.h
< #define NUMBER_FMT	"%.16g"		/* LUA_NUMBER */
> #define NUMBER_FMT	"%ld"		/* LUA_NUMBER */
< #define lua_str2number(s,p)	strtod((s), (p))
> #define lua_str2number(s,p)	strtol((s), (p), 10)

That's it, these simple changes are all that is needed!

The only other place in the code that is marked LUA_NUMBER is in llex.c,
but I didn't need to change this because lua_str2number is used in llex.c,
and it takes care that numbers in floating-point notation are rejected.
(the lexer actually accepts them, but it fails in the last step, when the
string is converted to number.)

I hope this is useful. I'd like to hear about other people's experience with
this (and also with using floats instead).