lua-users home
lua-l archive

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


Hello,

The following code demonstrates a technique for reducing redundancy in
defining LUA_VERSION etc.:

> #include <stdio.h>
> 
> #define Stringize0( x ) #x
> #define Stringize( x ) Stringize0( x )
> 
> #define LUA_VERSION_MAJOR_NUM 5
> #define LUA_VERSION_MAJOR Stringize( LUA_VERSION_MAJOR_NUM )
> #define LUA_VERSION_MINOR_NUM 4
> #define LUA_VERSION_MINOR Stringize( LUA_VERSION_MINOR_NUM )
> #define LUA_VERSION_NUM LUA_VERSION_MAJOR_NUM * 100 + LUA_VERSION_MINOR_NUM
> #define LUA_VERSION	"Lua " LUA_VERSION_MAJOR "." LUA_VERSION_MINOR
> 
> int main( int argc, char *argv[] ) {
>   printf( "LUA_VERSION_MAJOR_NUM=%d\n", LUA_VERSION_MAJOR_NUM );
>   printf( "LUA_VERSION_MAJOR=<%s>\n", LUA_VERSION_MAJOR );
>   printf( "LUA_VERSION_MINOR_NUM=%d\n", LUA_VERSION_MINOR_NUM );
>   printf( "LUA_VERSION_MINOR=<%s>\n", LUA_VERSION_MINOR );
>   printf( "LUA_VERSION_NUM=%d\n", LUA_VERSION_NUM );
>   printf( "LUA_VERSION=<%s>\n", LUA_VERSION );
> }

Running this program produces the output:

> LUA_VERSION_MAJOR_NUM=5
> LUA_VERSION_MAJOR=<5>
> LUA_VERSION_MINOR_NUM=4
> LUA_VERSION_MINOR=<4>
> LUA_VERSION_NUM=504
> LUA_VERSION=<Lua 5.4>

Whether the added obscurity by using this technique is worth the reduced
redundancy is not for me to decide.

Best
Thorkil