Page 1 of 1
Allow Recursive Macros
Posted: Mon Mar 20, 2006 2:57 pm
by GedB
PB4 is does a great job of detecting and rejecting recursive macros.
This is great protection of the programmer who only wants nice simple macros.
However, for those of us who like to do all kinds of perverse things with Macros its a little frustrating.
Could something be added to override the recursion checking, so that those of us who want to get all LISPish can?
Posted: Mon Mar 20, 2006 2:59 pm
by Dare2
How do you get recursion in a macro without it going infinite?
Not knocking your request, btw! I am genuinely curious - would love to see an example of what you mean, and learn from it.
Posted: Mon Mar 20, 2006 3:43 pm
by GedB
Dare2,
I'd like to try something like this:
Code: Select all
Macro RecursiveCountDownToZero(i)
CompilerIf i > 0
RecursiveCountDownToZero(i - 1)
CompilerEndIf
EndMacro
Posted: Mon Mar 20, 2006 4:27 pm
by freak
The CompilerIF is only processed once the macro is fully expanded, so you get an infinite recursion here.
Posted: Mon Mar 20, 2006 4:44 pm
by Lebostein
Yes, I had a similar problem with CompilerIf and macros:
Code: Select all
Macro FunctionCall
CompilerIf #PB_Compiler_OS = #PB_OS_Windows
CallFunction
CompilerElse
CallCFunction
CompilerEndIf
EndMacro
FunctionCall(1, "test")
The solution is to create two macros:
Code: Select all
CompilerIf #PB_Compiler_OS = #PB_OS_Windows
Macro FunctionCall
CallFunction
EndMacro
CompilerElse
Macro FunctionCall
CallCFunction
EndMacro
CompilerEndIf
FunctionCall(1, "test")
Edit: oh, it's a other topic, sorry...
Posted: Mon Mar 20, 2006 4:47 pm
by GedB
Hmmm, I see.
If only it could be worked out to allow this style of programming, then Macros could become so powerful.
Posted: Mon Mar 20, 2006 4:52 pm
by Fred
The problem is it would also be much more slower..
Posted: Tue Mar 28, 2006 8:27 am
by helpy
GedB wrote:
Code: Select all
Macro RecursiveCountDownToZero(i)
CompilerIf i > 0
RecursiveCountDownToZero(i - 1)
CompilerEndIf
EndMacro
There is another problem:
Variables are not allowed in CompilerIf statement. Variables are calculated at runtime ... CompilerIf statements are parsed at compile time.
Posted: Tue Mar 28, 2006 11:22 am
by freak
Just pass a constant for i, where is the problem ?
Posted: Tue Mar 28, 2006 12:14 pm
by helpy
freak wrote:Just pass a constant for i, where is the problem ?
i was just thinking "one way" ... because the possibility to pass a variable is there.
And the "i" in the recursive macro is just working like an variable.
I assume that the replacement of all macros is just done right before compile time. So it is not possible to combine the functionality of Macros and CompileIf.
In order to get recursive macros working, there should have to be new macro-keywords ... something like MacroIf, MacroEndIf, MacroElse ...
cu,