Allow Recursive Macros

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
GedB
Addict
Addict
Posts: 1313
Joined: Fri May 16, 2003 3:47 pm
Location: England
Contact:

Allow Recursive Macros

Post 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?
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post 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.
@}--`--,-- A rose by any other name ..
User avatar
GedB
Addict
Addict
Posts: 1313
Joined: Fri May 16, 2003 3:47 pm
Location: England
Contact:

Post 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

freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

The CompilerIF is only processed once the macro is fully expanded, so you get an infinite recursion here.
quidquid Latine dictum sit altum videtur
Lebostein
Addict
Addict
Posts: 826
Joined: Fri Jun 11, 2004 7:07 am

Post 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...
Last edited by Lebostein on Mon Mar 20, 2006 4:50 pm, edited 1 time in total.
User avatar
GedB
Addict
Addict
Posts: 1313
Joined: Fri May 16, 2003 3:47 pm
Location: England
Contact:

Post by GedB »

Hmmm, I see.

If only it could be worked out to allow this style of programming, then Macros could become so powerful.
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

The problem is it would also be much more slower..
User avatar
helpy
Enthusiast
Enthusiast
Posts: 552
Joined: Sat Jun 28, 2003 12:01 am

Post 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.
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

Just pass a constant for i, where is the problem ?
quidquid Latine dictum sit altum videtur
User avatar
helpy
Enthusiast
Enthusiast
Posts: 552
Joined: Sat Jun 28, 2003 12:01 am

Post 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,
Post Reply