How to Check If Macro's "Non-Value" Parameter Is Set?

Just starting out? Need help? Post your questions and find answers here.
c4s
Addict
Addict
Posts: 1981
Joined: Thu Nov 01, 2007 5:37 pm
Location: Germany

How to Check If Macro's "Non-Value" Parameter Is Set?

Post 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)...
If any of you native English speakers have any suggestions for the above text, please let me know (via PM). Thanks!
Little John
Addict
Addict
Posts: 4779
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: How to Check If Macro's "Non-Value" Parameter Is Set?

Post 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)
c4s
Addict
Addict
Posts: 1981
Joined: Thu Nov 01, 2007 5:37 pm
Location: Germany

Re: How to Check If Macro's "Non-Value" Parameter Is Set?

Post 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
8)
If any of you native English speakers have any suggestions for the above text, please let me know (via PM). Thanks!
Little John
Addict
Addict
Posts: 4779
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: How to Check If Macro's "Non-Value" Parameter Is Set?

Post 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)
GPI
PureBasic Expert
PureBasic Expert
Posts: 1394
Joined: Fri Apr 25, 2003 6:41 pm

Re: How to Check If Macro's "Non-Value" Parameter Is Set?

Post 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.
c4s
Addict
Addict
Posts: 1981
Joined: Thu Nov 01, 2007 5:37 pm
Location: Germany

Re: How to Check If Macro's "Non-Value" Parameter Is Set?

Post 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.
If any of you native English speakers have any suggestions for the above text, please let me know (via PM). Thanks!
c4s
Addict
Addict
Posts: 1981
Joined: Thu Nov 01, 2007 5:37 pm
Location: Germany

Re: How to Check If Macro's "Non-Value" Parameter Is Set?

Post 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?
If any of you native English speakers have any suggestions for the above text, please let me know (via PM). Thanks!
GPI
PureBasic Expert
PureBasic Expert
Posts: 1394
Joined: Fri Apr 25, 2003 6:41 pm

Re: How to Check If Macro's "Non-Value" Parameter Is Set?

Post by GPI »

hmm... maybe:

Code: Select all

CompilerIf ""+param <>""
c4s
Addict
Addict
Posts: 1981
Joined: Thu Nov 01, 2007 5:37 pm
Location: Germany

Re: How to Check If Macro's "Non-Value" Parameter Is Set?

Post 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"))
If any of you native English speakers have any suggestions for the above text, please let me know (via PM). Thanks!
Post Reply