[PB v.5.62 - x86 & x64] Macro 'Parameter' Bug!

Just starting out? Need help? Post your questions and find answers here.
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

[PB v.5.62 - x86 & x64] Macro 'Parameter' Bug!

Post 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
#NULL
Addict
Addict
Posts: 1440
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: [PB v.5.62 - x86 & x64] Macro 'Parameter' Bug!

Post 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(?)
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: [PB v.5.62 - x86 & x64] Macro 'Parameter' Bug!

Post 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
User avatar
STARGÅTE
Addict
Addict
Posts: 2067
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: [PB v.5.62 - x86 & x64] Macro 'Parameter' Bug!

Post 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
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
Sicro
Enthusiast
Enthusiast
Posts: 538
Joined: Wed Jun 25, 2014 5:25 pm
Location: Germany
Contact:

Re: [PB v.5.62 - x86 & x64] Macro 'Parameter' Bug!

Post 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.
Image
Why OpenSource should have a license :: PB-CodeArchiv-Rebirth :: Pleasant-Dark (syntax color scheme) :: RegEx-Engine (compiles RegExes to NFA/DFA)
Manjaro Xfce x64 (Main system) :: Windows 10 Home (VirtualBox) :: Newest PureBasic version
Post Reply