Page 1 of 2

Generating Macros with Macros

Posted: Thu Dec 04, 2008 12:16 pm
by technicorn
Getting PB to generate Macros with Macros is a bit tricky,
but her's one solution

Code: Select all

; First we need some macros:
Macro _EndOfMacro1
EndM
EndMacro

Macro _EndOfMacro2
acro
EndMacro

Macro EndOfMacro
:_EndOfMacro1#_EndOfMacro2
EndMacro

; Now for the test:
Macro g1(x)
  Macro x
    Debug "Hi from macro generated macro"
  EndOfMacro
EndMacro

Macro g2(x, p1)
  Macro x
    Debug p1
  EndOfMacro
EndMacro

Macro g3(x)
  Macro x(p1)
    Debug p1
  EndOfMacro
EndMacro


g1(sayhi)
g2(saythat, "A nother macro generated macro")
g3(useparameter)
sayhi
saythat
useparameter("This is my parameter text 1")
useparameter("This is my parameter text 2")
Happy coding

Posted: Thu Dec 04, 2008 7:25 pm
by Trond
Doesn't work with the beta.

Posted: Thu Dec 04, 2008 9:49 pm
by technicorn
Hi Trond,

I downloaded 430 beta 5 and cut and past my example to it
and it compiles without problems

PS. I'm using the windows version on XP
But maybe this isn't a reliably feature anyway.

Posted: Thu Dec 04, 2008 10:33 pm
by einander
It works here with PB4.30 beta 4, XP Pro Sp3.
These are really twisted macros! :shock:

Posted: Thu Dec 04, 2008 11:16 pm
by Psychophanta
Trond wrote:Doesn't work with the beta.
Same here with b5

Posted: Thu Dec 04, 2008 11:16 pm
by jack
I with Trond, keep getting "EndMacro is missing" error

Posted: Thu Dec 04, 2008 11:30 pm
by technicorn
Hi all,

maybe the ':' before the '_EndOfMacro1#_EndOfMacro2' from macro EndOfMacro got removed when you cut an past.
This is mandatory for this to work.
Seems like this is what makes the compiler re-interprete the line, so the
generated 'EndMacro' get's recognized.

Maybe Fred can say if this is something you can rely on or not,
till then, you shouldn't build a huge amount of macro generating macros ;)

Posted: Thu Dec 04, 2008 11:35 pm
by Psychophanta
technicorn wrote:Hi all,

maybe the ':' before the '_EndOfMacro1#_EndOfMacro2' from macro EndOfMacro got removed when you cut an past.
This is mandatory for this to work.
Still same.
Does this work for you?
http://www.purebasic.fr/english/viewtopic.php?t=35341

Posted: Fri Dec 05, 2008 1:15 am
by pdwyer
10 out of 10 for imagination
1 out of 10 for supportability

:)

Interesting to read how it works but I probably won't use it :P

Posted: Fri Dec 05, 2008 6:17 am
by technicorn
@Psychophanta

Your example doesn't work neither on 420 final windows nor 430 beta 5 windows
Tried it with and without parentheses on the first to macros, no success

But the error box that pops ub shows exactly the right macro name, so this really is a bug

Posted: Fri Dec 05, 2008 2:17 pm
by SFSxOI
does not work here on 4.2. I get "EndMacro is missing" error and if I add a "EndMacro" at the very bottom of code it compiles and runs but doesn't do anything. So overall its doesn't work here.

Posted: Fri Dec 05, 2008 4:09 pm
by Trond
I just get "EndMacro" missing.

Posted: Sat Dec 06, 2008 10:10 pm
by hallodri
Remove the space after "EndM " and "acro ".

Posted: Sun Dec 07, 2008 10:34 am
by Psychophanta
hallodri wrote:Remove the space after "EndM " and "acro ".
Good observation, that was the fault.
I removed every space after every line. I hate those spaces got when copy from the forum.

Posted: Sun Dec 07, 2008 10:57 pm
by ABBKlaus
adding a semicolon would solve that issue.

Code: Select all

Macro _EndOfMacro1 
EndM; 
EndMacro 

Macro _EndOfMacro2 
acro; 
EndMacro