Add 'MacroReturn' command for Purebasic macros

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Axeman
User
User
Posts: 89
Joined: Mon Nov 03, 2003 5:34 am

Add 'MacroReturn' command for Purebasic macros

Post by Axeman »

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
BarryG
Addict
Addict
Posts: 3292
Joined: Thu Apr 18, 2019 8:17 am

Re: Add 'MacroReturn' command for Purebasic macros

Post by BarryG »

Axeman wrote: Sat Jun 19, 2021 4:57 amHi. 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.
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)
Axeman
User
User
Posts: 89
Joined: Mon Nov 03, 2003 5:34 am

Re: Add 'MacroReturn' command for Purebasic macros

Post by Axeman »

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).
User avatar
STARGÅTE
Addict
Addict
Posts: 2067
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Add 'MacroReturn' command for Purebasic macros

Post by STARGÅTE »

Your idea can't work in various situations:
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
In this example, your idea will generate the following expansion

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
The code calls more UnLockMutex than LockMutex!

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
This code never calls UnLockMutex()

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 moreTypeface - Sprite-based font include/module
Axeman
User
User
Posts: 89
Joined: Mon Nov 03, 2003 5:34 am

Re: Add 'MacroReturn' command for Purebasic macros

Post by Axeman »

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

Re: Add 'MacroReturn' command for Purebasic macros

Post by GPI »

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...
Post Reply