Macros defined inside procedures should be valid therin only

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
uwekel
Enthusiast
Enthusiast
Posts: 740
Joined: Sat Dec 03, 2011 5:54 pm
Location: Oldenburg (Germany)

Macros defined inside procedures should be valid therin only

Post 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
PB 5.70 LTS (x64) - Debian Testing, Gnome 3.30.2
User avatar
TI-994A
Addict
Addict
Posts: 2512
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Macros defined inside procedures should be valid therin

Post 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. :)
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
User avatar
skywalk
Addict
Addict
Posts: 3996
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Macros defined inside procedures should be valid therin

Post 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.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
c4s
Addict
Addict
Posts: 1981
Joined: Thu Nov 01, 2007 5:37 pm
Location: Germany

Re: Macros defined inside procedures should be valid therin

Post 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.
If any of you native English speakers have any suggestions for the above text, please let me know (via PM). Thanks!
Post Reply