It is currently Wed Jul 17, 2019 5:32 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: [PB v.5.62 - x86 & x64] Macro 'Parameter' Bug!
PostPosted: Sat Dec 08, 2018 2:04 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sun Sep 11, 2016 2:17 pm
Posts: 440
Macro 'Parameter' Bug

Im not sure how to describe it but the macro is not properly resolving it 'parameters'.

Example:
Code:
EnableExplicit

Structure INFO_STRUCT
  Title.s{256}
EndStructure

Structure TASK_STRUCT
  Info.INFO_STRUCT
EndStructure

Global *Task.TASK_STRUCT
Global *Info.INFO_STRUCT

Macro CopyTask(task,info);-> macro confuses *Task\Info & "info" so *Task\Info becomes *Task\*Info which is wrong!
  CopyStructure(task\Info,info,INFO_STRUCT);-> rename "info" -> "infoX" and all works as expected!
EndMacro

*Task = AllocateStructure(TASK_STRUCT)
If *Task
  *Task\Info\Title = "Test!"
  *Info = AllocateStructure(INFO_STRUCT)
  If *Info
    CopyTask(*Task,*Info)
    FreeStructure(*Info)
  EndIf
  FreeStructure(*Task)
EndIf

End


Top
 Profile  
Reply with quote  
 Post subject: Re: [PB v.5.62 - x86 & x64] Macro 'Parameter' Bug!
PostPosted: Sat Dec 08, 2018 3:20 pm 
Offline
Addict
Addict

Joined: Thu Aug 30, 2007 11:54 pm
Posts: 1006
Location: right here
Not a bug. How should the compiler decide depending on the context (like '\..') if you want it to replace or not? In other cases this might be exactly the opposite: access the field that was passed as macro parameter but don't touch a local variable 'info'. You simply have to choose macro parameter names that don't collide unwantedly with other symbols, which is not a problem in general since the macro body is just there right next to the parameters. I don't know if it might become less obvious though if you call other macros within the body that evaluate beforehand(?)


Top
 Profile  
Reply with quote  
 Post subject: Re: [PB v.5.62 - x86 & x64] Macro 'Parameter' Bug!
PostPosted: Sat Dec 08, 2018 6:20 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sun Sep 11, 2016 2:17 pm
Posts: 440
- 'info' -> should equal: variable 123
- 'task' -> should equal: variable *Task\Info
- 'info' != 'task'

It cant be the same...

Code:

EnableExplicit

Structure INFO_STRUCT
  Title.s{256}
EndStructure

Structure TASK_STRUCT
  Info.INFO_STRUCT
EndStructure

Global *Task.TASK_STRUCT
Global *Info.INFO_STRUCT

Macro CopyTask(task,info)
  CopyStructure(task\Info,info,INFO_STRUCT)
EndMacro

Macro SetInfo(task,info)
  task\Info = info
EndMacro

*Task = AllocateStructure(TASK_STRUCT)
If *Task
  *Task\Info\Title = "Test!"
  *Info = AllocateStructure(INFO_STRUCT)
  If *Info
    SetInfo(*Task,123)
    ;CopyTask(*Task,*Info)
    FreeStructure(*Info)
  EndIf
  FreeStructure(*Task)
EndIf

End


Top
 Profile  
Reply with quote  
 Post subject: Re: [PB v.5.62 - x86 & x64] Macro 'Parameter' Bug!
PostPosted: Sat Dec 08, 2018 6:44 pm 
Offline
Addict
Addict
User avatar

Joined: Thu Jan 10, 2008 1:30 pm
Posts: 1205
Location: Germany, Glienicke
Not a bug, it is a nice feature to replace each name in each context. 'Info' = 'info'
I use this feature very often to create multiply procedures.

Macro parameter are not case sensitive.
If you use "info" as parameter name, "info" is replace whenever info is written:
In function names, in constant names, in structure fields, in variables, in structure names
Code:
Macro Test(info)
   info()
   info\info
   #info
   info.info
EndMacro

Test(whatever)

Code:
whatever()
whatever\whatever
#whatever
whatever.whatever

_________________
ImageImage


Top
 Profile  
Reply with quote  
 Post subject: Re: [PB v.5.62 - x86 & x64] Macro 'Parameter' Bug!
PostPosted: Sat Dec 08, 2018 7:00 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Wed Jun 25, 2014 5:25 pm
Posts: 339
Location: Germany
It looks like you don't fully understand how macros work.
They don't resolve the parameters, but insert them into the code exactly as you pass them to the macro. That's why something like this works, too:
Code:
Macro Exec(x)
  x
EndMacro

Exec(MessageRequester("Test", "This is a test"))
The PB compiler does something like this:
Code:
macroCode$ = ReplaceString(macroCode$, "x", parameter_x$)
Unlike it is with procedures, the macro code is inserted each time where the macro is called.
Macros are like constants (#Constant), but unlike normal constants they can be multiline and can have parameters.

_________________
Image
Why OpenSource should have a license
PureBasic-CodeArchiv-Rebirth: Git-Repository / Download -- Any help is welcome!
Manjaro Xfce x64 (Main system) :: WindowsXP/Xubuntu x86 (VirtualBox) :: PureBasic (Linux: x86/x64, Windows: x86) :: All are up to date


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: Majestic-12 [Bot] and 13 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye