Generating Macros with Macros

Share your advanced PureBasic knowledge/code with the community.
technicorn
Enthusiast
Enthusiast
Posts: 105
Joined: Wed Jan 18, 2006 7:40 pm
Location: Hamburg

Generating Macros with Macros

Post 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
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Doesn't work with the beta.
technicorn
Enthusiast
Enthusiast
Posts: 105
Joined: Wed Jan 18, 2006 7:40 pm
Location: Hamburg

Post 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.
User avatar
einander
Enthusiast
Enthusiast
Posts: 744
Joined: Thu Jun 26, 2003 2:09 am
Location: Spain (Galicia)

Post by einander »

It works here with PB4.30 beta 4, XP Pro Sp3.
These are really twisted macros! :shock:
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

Trond wrote:Doesn't work with the beta.
Same here with b5
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
jack
Addict
Addict
Posts: 1358
Joined: Fri Apr 25, 2003 11:10 pm

Post by jack »

I with Trond, keep getting "EndMacro is missing" error
technicorn
Enthusiast
Enthusiast
Posts: 105
Joined: Wed Jan 18, 2006 7:40 pm
Location: Hamburg

Post 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 ;)
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post 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
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
User avatar
pdwyer
Addict
Addict
Posts: 2813
Joined: Tue May 08, 2007 1:27 pm
Location: Chiba, Japan

Post 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
Paul Dwyer

“In nature, it’s not the strongest nor the most intelligent who survives. It’s the most adaptable to change” - Charles Darwin
“If you can't explain it to a six-year old you really don't understand it yourself.” - Albert Einstein
technicorn
Enthusiast
Enthusiast
Posts: 105
Joined: Wed Jan 18, 2006 7:40 pm
Location: Hamburg

Post 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
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Post 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.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

I just get "EndMacro" missing.
User avatar
hallodri
Enthusiast
Enthusiast
Posts: 208
Joined: Tue Nov 08, 2005 7:59 am
Location: Germany
Contact:

Post by hallodri »

Remove the space after "EndM " and "acro ".
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post 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.
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
ABBKlaus
Addict
Addict
Posts: 1143
Joined: Sat Apr 10, 2004 1:20 pm
Location: Germany

Post by ABBKlaus »

adding a semicolon would solve that issue.

Code: Select all

Macro _EndOfMacro1 
EndM; 
EndMacro 

Macro _EndOfMacro2 
acro; 
EndMacro 
Post Reply