Hello,
I'm working on a project where I need to count the current recursion depth of a macro to abstract recursion so that given instances don't collide. The idea is to provide say 8 instances of each re-enterable macro in the library so that the individual macros can more or less stack to an arbitrary nesting depth so code for example, can be written like:
where mAdd and mMul are preprocessor macros that perform their operations and emit tokens.
what I have tried is something like:
that is the rough idea of what I have tried - as pseudo code because I haven't gotten it working yet.
- each reentrant function has an object-like 'constructor' which decorates the function like version with the current nest depth ( as prepended '_' i.e. ___ is nest depth 3)
- each reentrant function returns it's value as _m NestLevel(Emit, Result) ..at the current nest level to keep mNestLevel from resolving so that it is marked used for the next nesting level
- mNestLevel uses the fact that if a version is still resolving, it will just be emitted directly without firing. a '_' is then prepended and the first version that actually fires is the current nest level.
- mNestLevel has 2 modes, controled by it's first parameter - Count and Emit. During count, it prepends '_' antil it reaches a level that will acutally fire then it fires the Fn. Emit mode is a wrapper for the return value of the function being 'executed'
The problem is in the mNestLevel macro. I haven't been able to get it to work right. Does anyone have any ideas for a macro that can count the current nesting level? The problems I have been having are that what I've tried has always been resolving completely to nest level 0 or not at all.
Before I get a bunch of 'why do you want to do that?' spam, It is because it would allow fully recursive list processing at compile time. In an elegant syntax similar to:
I'm currently using expressions similar to that to do a variety of things like fill the gaping holes in template partial specialization. Right now, I'm having to select operations that aren't being used somewhere else in the expression, or manually select a free nest level version. If get this recursion abstraction working, any standard preprocessor can be turned into something getting fairly close to a lisp engine just by including one file.
Thanks for reading, I've been stuck on this piece for a couple days now. If anyone has a solution it would help greatly.
-
Mark
I'm working on a project where I need to count the current recursion depth of a macro to abstract recursion so that given instances don't collide. The idea is to provide say 8 instances of each re-enterable macro in the library so that the individual macros can more or less stack to an arbitrary nesting depth so code for example, can be written like:
Code:
int Var = mAdd( mMul( A, mAdd( B, mMul( C, D ) ) ) );
what I have tried is something like:
Code:
#define mMul mNestLevel(Count,mMul)) #define _mMul( A, B ) ... _mNestLevel(Emit,Result) // level 1 version #define __mMul( A, B ) ... __mNestLevel(Emit,Result) ... // level 2 version #define ___mMul( A, B ) ... __mNestLevel(Emit,Result) ... // level 3 version ... continued for a set nesting limit
- each reentrant function has an object-like 'constructor' which decorates the function like version with the current nest depth ( as prepended '_' i.e. ___ is nest depth 3)
- each reentrant function returns it's value as _m NestLevel(Emit, Result) ..at the current nest level to keep mNestLevel from resolving so that it is marked used for the next nesting level
- mNestLevel uses the fact that if a version is still resolving, it will just be emitted directly without firing. a '_' is then prepended and the first version that actually fires is the current nest level.
- mNestLevel has 2 modes, controled by it's first parameter - Count and Emit. During count, it prepends '_' antil it reaches a level that will acutally fire then it fires the Fn. Emit mode is a wrapper for the return value of the function being 'executed'
The problem is in the mNestLevel macro. I haven't been able to get it to work right. Does anyone have any ideas for a macro that can count the current nesting level? The problems I have been having are that what I've tried has always been resolving completely to nest level 0 or not at all.
Before I get a bunch of 'why do you want to do that?' spam, It is because it would allow fully recursive list processing at compile time. In an elegant syntax similar to:
Code:
mForEach( (A,B,C,D,mMoreList),
Do(
mIfElse( mIsNumeric(Elem),
mIndex(Elem, mOtherList),
Elem)
)
)
Thanks for reading, I've been stuck on this piece for a couple days now. If anyone has a solution it would help greatly.
-
Mark
Comment