Page 1 of 1

Macros defined inside procedures should be valid therin only

Posted: Sun Apr 19, 2015 7:11 am
by uwekel
Hi,

if a macro is defined inside of a procedure, it should be valid therein only, without the need to use UndefineMacro before the next definition.

Greatings
Uwe

Re: Macros defined inside procedures should be valid therin

Posted: Sun Apr 19, 2015 3:10 pm
by TI-994A
uwekel wrote:if a macro is defined inside of a procedure, it should be valid therein only...
Hi uwekel. Unlike the code compilation process, PureBasic's macro system simply scans the source code top down, and expands the macro placeholders wherever they're found. Only parameter-based macros are validated, and the rest is left to the compiler. So, in its current form, it would not be able to recognise code scopes. To do so would require a revamp of the current macro system. IMHO.

What you're suggesting can already be done with FASM's macro system, and can be utilised with inline Assembly. Here's an example:

Code: Select all

; tested with PureBasic v5.31 x64 on Windows 8.1

msg.s

;*** global FASM macro *****************
!macro globalMacro
!{
  MessageRequester("Global Macro:", msg)
!}
;***************************************

Procedure myProcedure(msg.s)
  
  ;*** local FASM macro ***************** 
  !macro localMacro
  !{
    MessageRequester("Local Macro:", msg)
  !}
  ;**************************************  
  
  !localMacro    ;calls local macro
  !globalMacro   ;calls global macro
  
EndProcedure

msg = "...being called from procedure" 
myProcedure(msg)

msg = "...being called from main program"
!globalMacro   ;calls global macro
;!localMacro   ;local procedure macros cannot be used
This is based on code found on this forum, although I'm not sure if it is safe or advisable for use.

Just some thoughts. :)

Re: Macros defined inside procedures should be valid therin

Posted: Sun Apr 02, 2017 8:48 pm
by skywalk
Just came across this behavior as I needed a Macro within a Procedure to save typing.

Please add a reference to the manual...
Add to Help wrote:Macros can be defined within Procedures and it is recommended to 'Undefine Macro' prior to ProcedureReturn.

Re: Macros defined inside procedures should be valid therin

Posted: Mon Apr 03, 2017 9:40 am
by c4s
skywalk, thanks for pointing this out. I wasn't aware of it either and really think it should be noted in the help file.