Just starting out? Need help? Post your questions and find answers here.
Little John
Addict
Posts: 4527 Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany
Post
by Little John » Wed May 11, 2011 12:10 am
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
STARGÅTE
Addict
Posts: 2084 Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:
Post
by STARGÅTE » Wed May 11, 2011 8:47 am
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)
;[...]
Fred
Administrator
Posts: 16680 Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:
Post
by Fred » Wed May 11, 2011 8:48 am
That's it.
Little John
Addict
Posts: 4527 Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany
Post
by Little John » Wed May 11, 2011 3:38 pm
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
Posts: 5929 Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany
Post
by freak » Wed May 11, 2011 8:13 pm
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
Posts: 4527 Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany
Post
by Little John » Thu May 12, 2011 5:40 am
That's a good idea, thank you!
Regards, Little John
jacdelad
Addict
Posts: 1474 Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:
Post
by jacdelad » Fri Jun 23, 2023 9:05 am
Or the compiler could store the macro names in a map or list, so even if the are replaced it remembers they were there.
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
Fred
Administrator
Posts: 16680 Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:
Post
by Fred » Fri Jun 23, 2023 11:05 am
The whole line is evaluated before being parsed, so it wouldn't solve the issue.
Olli
Addict
Posts: 1071 Joined: Wed May 27, 2020 12:26 pm
Post
by Olli » Sat Jun 24, 2023 9:06 pm
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()