lua-users home
lua-l archive

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


On Friday 04 February 2005 12.37, Andre Carregal wrote:
[...]
> #define LERR_PRE "(#"
> #define LERR_POS ")"
> 
> #define LERR01_TEXT = "stack overflow"
> #define LERR01_CODE = "01"
> ...
> 
> #define LUA_MSG (code) = ...
> 
> Is it possible to use code as a part of a macro name? If so, the 
LUA_MSG macro would receive code as a parameter and depending on the 
message mode would result in one of
> 
> LERR(code)_TEXT
> LERR(code)_TEXT LERR_PRE LERR(code)_CODE LERR_POS
> LERR(code)_CODE
> 
> I'm afraid this is not C, that's why I'm asking if it is doable by 
macros... :o)

Macro mayhem... *hehe*

Well, you can indeed pass arguments. I have these all over the place 
in my code (at least alpha and beta code):

----------
#define DBGX(x) x       // Enabled
----------
#define DBGX(x)         // Disabled
----------
        DBGX(printf("Some debug message.\n");)
----------

That way, code can be conditionally compiled without #ifdef..#endif 
goo all over the place.


You're going to have to take it a bit further for this, though. The 
macro preprocessor is actually quite powerful, so it's definitely 
doable.

(Check out the VM instruction dispatcher of EEL if you want to see 
some serious macro magic. ;-)
 http://eel.olofson.net )


Hmm... Something like this, perhaps: (Untested!)
-------------------------------------------
/* Configuration */
#define LERR_USE_TEXT
#define LERR_USE_CODE
-------------------------------------------
/* Error text and code defs */
#define LERR01 "stack overflow"
#define LERR_STACK_OVERFLOW 01
#define LERR02 ...
    etc...
-------------------------------------------
/* Define what LERR(x) evaluates into */
#if defined(LERR_USE_CODE) && defined(LERR_USE_TEXT)
#define LERR(x) LERR##x "(" #x ")"
#elif defined(LERR_USE_TEXT)
#define LERR(x) LERR##x
#elif defined(LERR_USE_CODE)
#define LERR(x) "(" #x ")"
#else
#define LERR(x) ""
#endif
-------------------------------------------
...
    if (L->size_ci > LUA_MAXCALLS)
      luaG_runerror(L, LERR(LERR_STACK_OVERFLOW));
...
-------------------------------------------

(Hope I didn't get any GNUisms in there... I try to avoid them, but 
macro programming is always a mess and it's easy to accidentally rely 
on non-standard features of whatever tool you're using.)


//David Olofson - Programmer, Composer, Open Source Advocate

.- Audiality -----------------------------------------------.
|  Free/Open Source audio engine for games and multimedia.  |
| MIDI, modular synthesis, real time effects, scripting,... |
`-----------------------------------> http://audiality.org -'
   --- http://olofson.net --- http://www.reologica.se ---