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

Just starting out? Need help? Post your questions and find answers here.
acreis
Enthusiast
Enthusiast
Posts: 248
Joined: Fri Jun 01, 2012 12:20 am

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

Post 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!
User avatar
Piero
Addict
Addict
Posts: 1128
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

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

Post 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$)
acreis
Enthusiast
Enthusiast
Posts: 248
Joined: Fri Jun 01, 2012 12:20 am

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

Post 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




User avatar
STARGÅTE
Addict
Addict
Posts: 2266
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

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

Post 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".
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
User avatar
Piero
Addict
Addict
Posts: 1128
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

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

Post by Piero »

Seems we need a find/replace-before-compiling IDE tool for stuff like this :mrgreen:
acreis
Enthusiast
Enthusiast
Posts: 248
Joined: Fri Jun 01, 2012 12:20 am

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

Post 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.
User avatar
kenmo
Addict
Addict
Posts: 2069
Joined: Tue Dec 23, 2003 3:54 am

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

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