Allow to define macros everywhere in source

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Allow to define macros everywhere in source

Post by Psychophanta »

Not only before to be used.
Like that:

Code: Select all

ff
Macro ff
  Debug "hi!"
EndMacro
Or allow the 'Declare' statement also for macros.
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
thamarok
Enthusiast
Enthusiast
Posts: 282
Joined: Wed Sep 06, 2006 1:37 pm

Re: Allow to define macros everywhere in source

Post by thamarok »

Psychophanta wrote:Not only before to be used.
Like that:

Code: Select all

ff
Macro ff
  Debug "hi!"
EndMacro
Or allow the 'Declare' statement also for macros.
Yes would be nice indeed.

This for example:

Code: Select all

DeclareMacro My_Macro

a=My_Macro+1*My_Macro

Macro My_Macro
5
EndMacro
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

That would require a two-pass compiler or backpatching so I don't think it will get implemented.
thamarok
Enthusiast
Enthusiast
Posts: 282
Joined: Wed Sep 06, 2006 1:37 pm

Post by thamarok »

Trond wrote:That would require a two-pass compiler or backpatching so I don't think it will get implemented.
Yes, I know. But it still would be cool :P
But you can always write your own pre-processor :roll:
c4s
Addict
Addict
Posts: 1981
Joined: Thu Nov 01, 2007 5:37 pm
Location: Germany

Re: Allow to define macros everywhere in source

Post by c4s »

9 years? Pshh... I'll just leave my +1 here because I just came across this issue.

Why? You know, I simply want to order macro definitions independent of the compiler's quirks (of course same goes for procedures etc.). After all higher-level programming languages are here to make programming as straightforward as possible and save us from having to type in binary... 01011001011000010111100100100001!

By the way, this reminds me on another very similar feature request: Declare for Arrays, Lists, Variables, Structures
If any of you native English speakers have any suggestions for the above text, please let me know (via PM). Thanks!
HanPBF
Enthusiast
Enthusiast
Posts: 570
Joined: Fri Feb 19, 2010 3:42 am

Re: Allow to define macros everywhere in source

Post by HanPBF »

PB compiler must stay as single pass compiler; either it won't be fast enough anymore.

Then, PB is a compiler for easily build up exe programs; it's not an IDE to support the whole software development process.

The best way to deal with OOP, etc. is using a precompiler.
But then, additional editor with auto complete or ctags for UltraEdit, etc.!


Many languages are precompiled (Scala -> JAVA, Eiffel -> C/C++, etc.)


But, declaration should always come before using things; that's from PASCAL and I think it's correct.
c4s
Addict
Addict
Posts: 1981
Joined: Thu Nov 01, 2007 5:37 pm
Location: Germany

Re: Allow to define macros everywhere in source

Post by c4s »

HanPBF wrote:But, declaration should always come before using things; that's from PASCAL and I think it's correct.
Of course! But what about this quick example:

Code: Select all

Procedure Test1()
  Test2()
  ; Do something
EndProcedure

Macro Test2()
  ; Do something else
EndMacro
If I could simply declare it at the top the compiler wouldn't complain (as with procedures). Please note that this is a very simplified example so "just declare the macros first" is not the proper solution but more of an ugly workaround.
If any of you native English speakers have any suggestions for the above text, please let me know (via PM). Thanks!
User avatar
Shield
Addict
Addict
Posts: 1021
Joined: Fri Jan 21, 2011 8:25 am
Location: 'stralia!
Contact:

Re: Allow to define macros everywhere in source

Post by Shield »

You can declare procedures because for a procedure call the function signature is the only thing that matters.
In the case with macros, you can't do that because with macros the entire content of the macro matters.
Image
Blog: Why Does It Suck? (http://whydoesitsuck.com/)
"You can disagree with me as much as you want, but during this talk, by definition, anybody who disagrees is stupid and ugly."
- Linus Torvalds
c4s
Addict
Addict
Posts: 1981
Joined: Thu Nov 01, 2007 5:37 pm
Location: Germany

Re: Allow to define macros everywhere in source

Post by c4s »

Shield wrote:You can declare procedures because for a procedure call the function signature is the only thing that matters.
In the case with macros, you can't do that because with macros the entire content of the macro matters.
This might be true on a compiler perspective. However as a programmer we're still addressing a macro by just its name, right?!
I get the problem though. I'm mostly using macros as simple procedures without their overhead: Small calculations, quick calls to other procedures with different arguments etc. So no fancy replacement magic that probably wasn't intended by the PB team in the first place.

Anyway, regardless of whether it's currently possible to implement or not (everything is! :) ), I'd like to have it because code is much easier to read if you're allowed to structure it as you like/need/want it.
If any of you native English speakers have any suggestions for the above text, please let me know (via PM). Thanks!
User avatar
Shield
Addict
Addict
Posts: 1021
Joined: Fri Jan 21, 2011 8:25 am
Location: 'stralia!
Contact:

Re: Allow to define macros everywhere in source

Post by Shield »

That's another issue then. The compiler should be able to effectively inline function calls automatically anyways,
unfortunately it doesn't do that at the moment.
Image
Blog: Why Does It Suck? (http://whydoesitsuck.com/)
"You can disagree with me as much as you want, but during this talk, by definition, anybody who disagrees is stupid and ugly."
- Linus Torvalds
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Re: Allow to define macros everywhere in source

Post by freak »

Currently, you can use macros to replace/extend an internal PB command while still calling the original command (this is done quite a lot by some people):

Code: Select all

Procedure MyOpenWindow(WindowID, x, y, ...)
  Result = OpenWindow(WindowID, x, y, ...) ; This is the real OpenWindow() from PB
  If Result
    ; Do awesome stuff here
  EndIf
  ProcedureReturn Result
EndProcedure

Macro OpenWindow(WindowID, x, y, ...)
  MyOpenWindow(WindowID, x, y, ...)
EndMacro

If OpenWindow(...) ; this now calls the MyOpenWindow() procedure
With your proposed change this will not work anymore, because the first OpenWindow() call will now also refer to the macro and not the PB one.

For more on why the order of things is important:
http://www.purebasic.fr/english/viewtop ... 25#p307825
quidquid Latine dictum sit altum videtur
c4s
Addict
Addict
Posts: 1981
Joined: Thu Nov 01, 2007 5:37 pm
Location: Germany

Re: Allow to define macros everywhere in source

Post by c4s »

@freak
Yeah, you're right about that. Actually I'm using that too sometimes. I guess it's best as it is and I just have to use a real procedure instead of a macro (to be able to use "declare") whenever the code structure is more important than some little speed gain...
If any of you native English speakers have any suggestions for the above text, please let me know (via PM). Thanks!
Post Reply