Page 1 of 1
[PB v.5.62 - x86 & x64] Macro 'Parameter' Bug!
Posted: Sat Dec 08, 2018 2:04 pm
by Mijikai
Macro 'Parameter' Bug
Im not sure how to describe it but the macro is not properly resolving it 'parameters'.
Example:
Code: Select all
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
Re: [PB v.5.62 - x86 & x64] Macro 'Parameter' Bug!
Posted: Sat Dec 08, 2018 3:20 pm
by #NULL
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(?)
Re: [PB v.5.62 - x86 & x64] Macro 'Parameter' Bug!
Posted: Sat Dec 08, 2018 6:20 pm
by Mijikai
- 'info' -> should equal: variable 123
- 'task' -> should equal: variable *Task\Info
- 'info' != 'task'
It cant be the same...
Code: Select all
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
Re: [PB v.5.62 - x86 & x64] Macro 'Parameter' Bug!
Posted: Sat Dec 08, 2018 6:44 pm
by STARGĂ…TE
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: Select all
Macro Test(info)
info()
info\info
#info
info.info
EndMacro
Test(whatever)
Code: Select all
whatever()
whatever\whatever
#whatever
whatever.whatever
Re: [PB v.5.62 - x86 & x64] Macro 'Parameter' Bug!
Posted: Sat Dec 08, 2018 7:00 pm
by Sicro
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: Select all
Macro Exec(x)
x
EndMacro
Exec(MessageRequester("Test", "This is a test"))
The PB compiler does something like this:
Code: Select all
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.