Page 1 of 1
How to Check If Macro's "Non-Value" Parameter Is Set?
Posted: Sat Sep 26, 2015 11:14 am
by c4s
One advantage of macros is that they can help at
writing actual code. So with "non-value" I mean not a regular string, number, variable or anything like that, but something that's part of e.g. a constant's name.
Being kind of new to the interesting world of PB's macro-magic I came up with this rather strange solution:
Code: Select all
Macro _Quote
"
EndMacro
Macro Test(param=)
CompilerIf _Quote param _Quote <> " " ; Check if it's set
Debug "set to " + _Quote param _Quote
CompilerElse
Debug "not set"
CompilerEndIf
EndMacro
Test() ; Not set
Test(yeah) ; Set to "yeah"
Now my question is if that can be improved? Preferably without the "_quote" workaround.
Just a suggestion: Maybe even the
Defined() functionality could be extended?! I'm thinking of something simple like
Defined(param) (for this example)...
Re: How to Check If Macro's "Non-Value" Parameter Is Set?
Posted: Sat Sep 26, 2015 1:06 pm
by Little John
Interesting question.
The following seems to work, too.
However, I think your solution is better.
Code: Select all
#MyMacroParamTestConstant = 0
Macro Test (param=)
CompilerIf Defined(MyMacroParamTestConstant#param, #PB_Constant)
Debug "not set"
CompilerElse
Debug "set"
CompilerEndIf
EndMacro
Test()
Test(yeah)
Re: How to Check If Macro's "Non-Value" Parameter Is Set?
Posted: Sat Sep 26, 2015 4:05 pm
by c4s
Thanks Little John, that's a pretty cool idea. When using a predefined constant it's even more compact. I think I prefer your way:
Code: Select all
Macro Test(param=)
CompilerIf Defined(True#param, #PB_Constant)
Debug "not set"
CompilerElse
Debug "set"
CompilerEndIf
EndMacro

Re: How to Check If Macro's "Non-Value" Parameter Is Set?
Posted: Sat Sep 26, 2015 7:31 pm
by Little John
Hi, that's a nice improvement.
I didn't like the idea myself very much, because there is a pitfall (it's the same with both my original suggestion and your modified version).
I think you are aware of it, but I want to mention it here for the sake of completeness.
Care has to be taken that something like the following will be avoided:
Code: Select all
#TrueLove = 127
Macro Test(param=)
CompilerIf Defined(True#param, #PB_Constant)
Debug "not set"
CompilerElse
Debug "set"
CompilerEndIf
EndMacro
Test()
Test(Love)
Re: How to Check If Macro's "Non-Value" Parameter Is Set?
Posted: Sat Sep 26, 2015 8:06 pm
by GPI
The original idea is not from me, i only expand it for quotes:
Code: Select all
Macro MacroColon
:
EndMacro
Macro MacroQuote
"
EndMacro
Macro MacroSingleQuote
'
EndMacro
Macro JoinMacroParts (P1, P2=, P3=, P4=, P5=, P6=, P7=, P8=) : P1#P2#P3#P4#P5#P6#P7#P8 : EndMacro
Macro CreateMacro (name,macroBody=)
JoinMacroParts (Macro name, MacroColon, macroBody, MacroColon, EndMacro) :
EndMacro
Macro CreateQuote (name)
JoinMacroParts (MacroQuote,name,MacroQuote)
EndMacro
Macro CreateSingleQuote (name)
JoinMacroParts (MacroSingleQuote,name,MacroSingleQuote)
EndMacro
Macro test (para=)
CompilerIf CreateQuote(para)=""
Debug "not set"
CompilerElse
Debug "set"
CompilerEndIf
EndMacro
You can also Create Macro inside a Macro - but it is a little big buggy. You can only define one Macro inside of a Macro, because the "EndMacro" will stop it.
Re: How to Check If Macro's "Non-Value" Parameter Is Set?
Posted: Sat Sep 26, 2015 9:31 pm
by c4s
@Little John
Yes, I'm aware of that, but thanks for pointing it out. To avoid that issue (and I'm sure you know that too

) you could simply use a more abstruse constant like
#PB_Billboard_SelfPerpendicular.
@GPI
Thanks for the different approach.
Re: How to Check If Macro's "Non-Value" Parameter Is Set?
Posted: Thu Oct 01, 2015 11:04 am
by c4s
I just realized that I need this to be more flexible (than Little John's version that I initially preferred). My first idea (or GPI's) pretty much does what I need -- except that it doesn't work with literal strings. Here is a quick example:
Code: Select all
Macro _Quote
"
EndMacro
Macro MacroParam(parameter=)
Bool(_Quote parameter _Quote <> " ")
EndMacro
; All tests work fine except the last ones containing any strings
Debug Bool(#False = MacroParam())
Debug Bool(#True = MacroParam(yeah))
Debug Bool(#True = MacroParam(123))
Debug Bool(#True = MacroParam(4e5))
Debug Bool(#True = MacroParam(6.7))
Debug Bool(#True = MacroParam('a'))
Debug Bool(#True = MacroParam(#foo))
Debug Bool(#True = MacroParam(8 / 9))
Debug Bool(#True = MacroParam(#foo + #bar))
;Debug Bool(#True = MacroParam("str")) ; <- How could this be tested? (using the same MacroParam())
;Debug Bool(#True = MacroParam(#foo + "str"))
;Debug Bool(#True = MacroParam("s1" + "s2"))
Obviously strings are a huge problem for this approach because the quote workaround for comparison doesn't work anymore. So how could this be done? Any ideas on how to detect if a macro parameter is a string?
Re: How to Check If Macro's "Non-Value" Parameter Is Set?
Posted: Thu Oct 01, 2015 11:25 am
by GPI
Re: How to Check If Macro's "Non-Value" Parameter Is Set?
Posted: Thu Oct 01, 2015 2:01 pm
by c4s
Good idea, but it doesn't work in the following cases (especially the first one):
Code: Select all
Debug Bool(#False = MacroParam())
Debug Bool(#True = MacroParam(#foo))
Debug Bool(#True = MacroParam(8 / 9))
Debug Bool(#True = MacroParam(#foo + #bar))
Debug Bool(#True = MacroParam(#foo + "str"))