Improve Macro Handling with MacroIf and MacroParam()
Posted: Sat Nov 21, 2015 12:01 pm
Can we please have macros that are just a tiny little bit more intelligent? I'm talking about two simple but powerful missing features (that are processed before the macro gets expanded):
The context is that I'm desperately trying to make something like the following work (and in the past I came across variations several times). Unfortunately there are a couple of problems:
With the new features it could look like this (spoiler alert: code is compiling fine!
):
There are quite a lot of feature requests for improved macro handling going back many years. I think using this approach it could be implemented easily without you (the PB team) having to think of new syntax concepts. I can imagine that with only those two additional features the PureBasic language could be adjusted to a whole new level: Custom ternary operator (without having to use a procedure for the return value), primitive generic/template programming (very important!) and many more...
- MacroParam(): Tests if a parameter is set (see discussion here for something that doesn't work with strings).
- MacroIf: Macros are processed with highest priority so to control its behavior even CompilerIf won't work. However in certain cases this is necessary, e.g. to be able to simulate a return type (see example below).
The context is that I'm desperately trying to make something like the following work (and in the past I came across variations several times). Unfortunately there are a couple of problems:
Code: Select all
Macro _Quote
"
EndMacro
Macro _MacroParam(parameter=)
Bool(_Quote parameter _Quote <> " ") ; Not working if parameter is a string. :(
EndMacro
Macro Test(String, Character=)
CompilerIf _MacroParam(Character)
Trim(String, Character)
CompilerElse
Trim(String)
CompilerEndIf
EndMacro
result$ = Test("-abc-", "-") ; Problem with "CompilerIf". :(
result$ = Test(" abc ") ; Proper handling of optional parameter is tricky. :(

Code: Select all
Macro Test(String, Character=)
MacroIf MacroParam(Character)
Trim(String, Character)
MacroElse
Trim(String)
MacroEndIf
EndMacro
result$ = Test("-abc-", "-") ; Works fine.
result$ = Test(" abc ") ; Amazing, this truly is the future! :)