Defined(Foo, #PB_Macro) ?

Just starting out? Need help? Post your questions and find answers here.
Little John
Addict
Addict
Posts: 4779
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Defined(Foo, #PB_Macro) ?

Post by Little John »

Hi all,

I wanted to check, whether a macro is defined:

Code: Select all

CompilerIf Defined(Foo, #PB_Macro)
   Debug "Macro 'Foo' is already defined."
CompilerEndIf
But this does not work (tesed with PB 4.51 and PB 4.60 Beta 3).
Why is there no cnstant #PB_Macro for this purpose?
Is there any other possibility to check, whether a particular macro is defined?

Regards, Little John
User avatar
STARGÅTE
Addict
Addict
Posts: 2227
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Defined(Foo, #PB_Macro) ?

Post by STARGÅTE »

You can not test in this way because the macro (the name) will replace already during the test.

Code: Select all

Macro Foo
 Debug 1
EndMacro

CompilerIf Defined(Foo, #PB_Macro)

CompilerEndIf
will be:

Code: Select all

Macro Foo
 Debug 1
EndMacro

CompilerIf Defined(Debug 1, #PB_Macro)
;[...]
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Defined(Foo, #PB_Macro) ?

Post by Fred »

That's it.
Little John
Addict
Addict
Posts: 4779
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Defined(Foo, #PB_Macro) ?

Post by Little John »

Hi, thank you for the explanation!

Maybe it is possible (and desirable) to change the compiler a little, so that it makes and exception in this case:
If a macro name is an argument of Defined(), then the compiler will not expand the macro.

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

Re: Defined(Foo, #PB_Macro) ?

Post by freak »

You can use a separate constant to guard against the double definition of a macro. Its not perfect, but it does the job for me:

Code: Select all

CompilerIf Defined(macro_test_defined, #PB_Constant) = 0
  #macro_test_defined = 1
  
  Macro test()
    ; something
  EndMacro
CompilerEndIf
quidquid Latine dictum sit altum videtur
Little John
Addict
Addict
Posts: 4779
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Defined(Foo, #PB_Macro) ?

Post by Little John »

That's a good idea, thank you!

Regards, Little John
User avatar
jacdelad
Addict
Addict
Posts: 1993
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: Defined(Foo, #PB_Macro) ?

Post by jacdelad »

Or the compiler could store the macro names in a map or list, so even if the are replaced it remembers they were there.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Defined(Foo, #PB_Macro) ?

Post by Fred »

The whole line is evaluated before being parsed, so it wouldn't solve the issue.
Olli
Addict
Addict
Posts: 1202
Joined: Wed May 27, 2020 12:26 pm

Re: Defined(Foo, #PB_Macro) ?

Post by Olli »

Solve --> A homemade IDE tool set as pre-process, feature available since long time.

That is not very complex to do. Just the homemade feature would not be native nor public...

Just a remark : what's so bad with this code below ?

Code: Select all

Macro aa()
 Debug 11
EndMacro

UndefineMacro aa

Macro aa()
 Debug 13
EndMacro

aa()
Post Reply