Hi. I'd like to suggest adding a 'MacroReturn' command to Purebasic macros. The point of this command would be to allow macros to return a value in a similar fashion to the way that a procedure returns a value. This would allow many simple 'GetValue()' procedures used as library interfaces to be replaced by macros for more efficient code.
The premise of the MacroReturn command would be that all the statements in the macro definition that appear before the statement containing the MacroReturn command would be expanded on the line the macro is called on as statements placed before the statement where the macro call appears. All the statements in the macro definition that appear after the statement containing the MacroReturn command would be expanded on the line the macro is called on as statements placed after the statement where the macro call appears. The code in the statement where the MacroReturn command appears (excluding the MacroReturn command itself) would replace the macro identifier where the macro is called.
Here's an example (taken from my threaded mouse library):-
Macro GetLeftMouseButtonClicks()
; Returns the number of times that the left mouse button has been clicked since the last call to this function.
; A click in this case means the button being pressed after it was in the released state, rather than a press followed by a release.
LockMutex( G_pb_mouse_mutex )
MacroReturn G_pb_mouse_button_1_clicks ; The number of button clicks are stored in this global variable.
G_pb_mouse_button_1_clicks = 0
UnLockMutex( G_pb_mouse_mutex )
EndMacro
; Line with macro call unexpanded.
InputEvent3D( G_pb_mouse_x, G_pb_mouse_y, GetLeftMouseButtonClicks(), input_key.s, special_key )
; Line with macro call expanded.
LockMutex( G_pb_mouse_mutex ) : InputEvent3D( G_pb_mouse_x, G_pb_mouse_y, G_pb_mouse_button_1_clicks, input_key.s, special_key ) : G_pb_mouse_button_1_clicks = 0 : UnLockMutex( G_pb_mouse_mutex )
; ---
; This is the procedure that the macro is intended to replace.
Procedure.i GetLeftMouseButtonClicks()
; Returns the number of times that the left mouse button has been clicked since the last call to this function.
; A click in this case means the button being pressed after it was in the released state, rather than a press followed by a release.
LockMutex( G_pb_mouse_mutex )
clicks = G_pb_mouse_button_1_clicks
G_pb_mouse_button_1_clicks = 0
UnlockMutex( G_pb_mouse_mutex )
ProcedureReturn clicks
EndProcedure
Add 'MacroReturn' command for Purebasic macros
Re: Add 'MacroReturn' command for Purebasic macros
Macros are just a search/replace of code at compile time. You can already get a return value from them if you code them that way - I do it all the time. You don't need a MacroReturn to do it.
My app has dozens of lines like this, which is what you're asking:
Code: Select all
ReturnValue = FromMacro(arg1=1,arg2=2,etc)
Re: Add 'MacroReturn' command for Purebasic macros
BarryG - The code you posted only works in specific circumstances. The example code I posted shows a situation where the result of the approach you are using would result in an uncompilable code syntax (which is why I used that specific code as an example).
Re: Add 'MacroReturn' command for Purebasic macros
Your idea can't work in various situations:
Using your Macro:
In this example, your idea will generate the following expansion
The code calls more UnLockMutex than LockMutex!
Even more problematic is:
This code never calls UnLockMutex()
Please use Procedures! The way how you want to use Macros is not the idear behind marcos.
Using your Macro:
Code: Select all
Macro GetLeftMouseButtonClicks()
LockMutex( G_pb_mouse_mutex )
MacroReturn G_pb_mouse_button_1_clicks ; The number of button clicks are stored in this global variable.
G_pb_mouse_button_1_clicks = 0
UnLockMutex( G_pb_mouse_mutex )
EndMacro
Code: Select all
For I = 1 to GetLeftMouseButtonClicks()
; Any Code here
Next
Code: Select all
LockMutex( G_pb_mouse_mutex )
For I = 1 to G_pb_mouse_button_1_clicks ; The number of button clicks are stored in this global variable.
G_pb_mouse_button_1_clicks = 0
UnLockMutex( G_pb_mouse_mutex )
; Any Code here
Next
Even more problematic is:
Code: Select all
Procedure Test()
; Any Code here
ProcedureReturn GetLeftMouseButtonClicks()
EndProcedure
Code: Select all
Procedure Test()
; Any Code here
LockMutex( G_pb_mouse_mutex )
ProcedureReturn G_pb_mouse_button_1_clicks ; The number of button clicks are stored in this global variable.
G_pb_mouse_button_1_clicks = 0
UnLockMutex( G_pb_mouse_mutex )
EndProcedure
Please use Procedures! The way how you want to use Macros is not the idear behind marcos.
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 more ― Typeface - Sprite-based font include/module
Lizard - Script language for symbolic calculations and more ― Typeface - Sprite-based font include/module
Re: Add 'MacroReturn' command for Purebasic macros
Yes you would indeed have to think about how this approach would be used, much like every other bit of code you might write. I'm not suggesting a 'leave your brain at the door' solution, just one that could improve certain situations where a macro might be more desirable than a procedure.
Re: Add 'MacroReturn' command for Purebasic macros
what you want is a "inline"- Prefix for Procedures.
With the c-Backend this would be easy possible. For assembler-backend not, but since a "inline" is only a suggestion, it could easy be ignored.
A "macroreturn" would make no sense who macros work...
With the c-Backend this would be easy possible. For assembler-backend not, but since a "inline" is only a suggestion, it could easy be ignored.
A "macroreturn" would make no sense who macros work...