PureBasic Forum
http://forums.purebasic.com/english/

[PB v.5.62 - x86 & x64] Macro 'Parameter' Bug!
http://forums.purebasic.com/english/viewtopic.php?f=13&t=71892
Page 1 of 1

Author:  Mijikai [ Sat Dec 08, 2018 2:04 pm ]
Post subject:  [PB v.5.62 - x86 & x64] Macro 'Parameter' Bug!

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

Author:  #NULL [ Sat Dec 08, 2018 3:20 pm ]
Post subject:  Re: [PB v.5.62 - x86 & x64] Macro 'Parameter' Bug!

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(?)

Author:  Mijikai [ Sat Dec 08, 2018 6:20 pm ]
Post subject:  Re: [PB v.5.62 - x86 & x64] Macro 'Parameter' Bug!

- '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

Author:  STARGĂ…TE [ Sat Dec 08, 2018 6:44 pm ]
Post subject:  Re: [PB v.5.62 - x86 & x64] Macro 'Parameter' Bug!

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

Author:  Sicro [ Sat Dec 08, 2018 7:00 pm ]
Post subject:  Re: [PB v.5.62 - x86 & x64] Macro 'Parameter' Bug!

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.

Page 1 of 1 All times are UTC + 1 hour
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/