Page 1 of 1

How to Use `#PB_Compiler_Procedure` in a Macro to Declare a Variable?

Posted: Sun Nov 02, 2025 8:49 am
by acreis
Hi Folks!

I'm trying to write a PureBasic macro that declares a `Protected.s` variable named after the current procedure using `#PB_Compiler_Procedure`. Here's what I attempted:

Code: Select all

Macro CreateProtectedStringVarWithProcName()
  Protected.s #PB_Compiler_Procedure
EndMacro

Procedure.s MyProcedure()
  CreateProtectedStringVarWithProcName() ; expands to 'Protected.s #PB_Compiler_Procedure' → syntax error


EndProcedure
This gives a syntax error on the macro expansion line. I expected it to declare a variable like Protected.s MyProcedure, but it seems PureBasic doesn't allow #PB_Compiler_Procedure in this context.

Interestingly, this works fine when passing `#PB_Compiler_Procedure` as a parameter:

Code: Select all

Procedure.s MyProcedure(sString$)
  ProcedureReturn sString$
EndProcedure

Procedure CallMyDebug()
  Debug MyProcedure(#PB_Compiler_Procedure) ; expands correctly
EndProcedure

Is there a way to use `#PB_Compiler_Procedure` inside a macro to declare a variable with the same name as the current procedure?**
Or is there a workaround to dynamically name a variable based on the procedure name?

Any insights or alternative approaches would be appreciated!

Re: How to Use `#PB_Compiler_Procedure` in a Macro to Declare a Variable?

Posted: Sun Nov 02, 2025 11:26 am
by Piero
Hmmm… interesting macro challenge… but at the end of the day this is nonsense (you already know the procedure name, and protected can be just XYZ$)

Re: How to Use `#PB_Compiler_Procedure` in a Macro to Declare a Variable?

Posted: Sun Nov 02, 2025 12:16 pm
by acreis
Thanks Piero,

Let's see one real case scenario:

Code: Select all

 
  #RUNONCE_ABORT   = -1
  
 Macro MacroRunOnce(_AtomicFlagInt32_, _ReturnValueOnAbort_ = #RUNONCE_ABORT, _StatementOnAbort_ =)
  
  If InterlockedCompareExchange_(@_AtomicFlagInt32_, 1, 0) = 1 ; set atomicflag to 1 if it is zero
    _StatementOnAbort_
    ProcedureReturn _ReturnValueOnAbort_
  EndIf   
  
EndMacro   

Code: Select all

;the folowing procedure will run only once

Procedure CalledManyTimes()

     Global GlobalVariable_CalledManyTimes_AtomicFlagInt32
     MacroRunOnce(GlobalVariable_CalledManyTimes_AtomicFlagInt32)

EndProcedure

If macro is able to create the variable, we have a cleaner code:

Code: Select all


Procedure CalledManyTimes()
     
     MacroRunOnce() ;GlobalVariable_CalledManyTimes_AtomicFlagInt32 will be declared by the macro

EndProcedure





Re: How to Use `#PB_Compiler_Procedure` in a Macro to Declare a Variable?

Posted: Sun Nov 02, 2025 1:01 pm
by STARGÅTE
acreis wrote: Sun Nov 02, 2025 8:49 am Is there a way to use `#PB_Compiler_Procedure` inside a macro to declare a variable with the same name as the current procedure?**
Or is there a workaround to dynamically name a variable based on the procedure name?

Any insights or alternative approaches would be appreciated!
No, it is not possible.
#PB_Compiler_Procedure is a string constant and resolves as "MyProcedure", not MyProcedure, during compilation.
Therefore it works as a procedure argument, because it is a string.
But in your context, it is expand to Protected.s "MyProcedure".

Re: How to Use `#PB_Compiler_Procedure` in a Macro to Declare a Variable?

Posted: Sun Nov 02, 2025 1:32 pm
by Piero
Seems we need a find/replace-before-compiling IDE tool for stuff like this :mrgreen:

Re: How to Use `#PB_Compiler_Procedure` in a Macro to Declare a Variable?

Posted: Tue Nov 04, 2025 12:42 pm
by acreis
Thanks STARGÅTE and Piero!

I'll think about this tool.

Maybe

Code: Select all

   #PB_Compiler_Procedure% ;tools replace by procedure name with no double quotes.

Re: How to Use `#PB_Compiler_Procedure` in a Macro to Declare a Variable?

Posted: Tue Nov 04, 2025 2:33 pm
by kenmo
acreis wrote: Sun Nov 02, 2025 12:16 pmIf macro is able to create the variable, we have a cleaner code:
Why does it need to be Global, and why does it need the procedure's name in the variable name...........

Can't you achieve this with something like Static ThisProcedure_CalledManyTimes.l var created within the Macro ?