Macro concatenate doesn't work with variable?

Just starting out? Need help? Post your questions and find answers here.
BarryG
Addict
Addict
Posts: 4123
Joined: Thu Apr 18, 2019 8:17 am

Macro concatenate doesn't work with variable?

Post by BarryG »

I was looking at this topic -> https://www.purebasic.fr/english/viewtopic.php?t=84189

And I wanted to try and help, but this test code doesn't work if I use a variable ("n") instead of a literal number like "1".

I assumed the macro would take the numeric value of "n" to concatenate it to the "MyLabel_" text? But it's just literally using the text "n" instead?

Code: Select all

Macro DynamicRestore(n)
  MyLabel_#n
EndMacro

n=Random(3,1)
Restore DynamicRestore(1) ; Change "1" to "n" and it doesn't work?
Read.s label$
Debug label$

End

DataSection
  MyLabel_1: : Data.s "MyLabel_1"
  MyLabel_2: : Data.s "MyLabel_2"
  MyLabel_3: : Data.s "MyLabel_3"
EndDataSection
The reason for my confusion is because in this example, the macro IS using the numeric value of "n" to double it:

Code: Select all

Macro test(n)
  n*2
EndMacro

n=50
Debug test(n) ; Outputs 100
So, what the? :shock:
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Macro concatenate doesn't work with variable?

Post by Demivec »

The macro functions by substituting the 'text' of its parameters at compile time. The text of the parameters is not interpreted in the macro but instead is interpreted by the compiler after the macro substitution occurs in the source code. The concatenation symbol '#' concatenates the text of the macro parameter with the adjacent text in the source of the macro, again before compiling and not at run time.

In the second code sample you posted the macro parameter is n and its substitution text is 'n'. So Debug test(n) is compiled as Debug n*2. At run time n=50 and the displayed result is 100.
User avatar
Michael Vogel
Addict
Addict
Posts: 2797
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: Macro concatenate doesn't work with variable?

Post by Michael Vogel »

Code: Select all


Macro DynamicRestore(n)
	MyLabel_#n
EndMacro

n=2
Restore DynamicRestore(n)
Read.s label$
Debug label$

; --------------------------------------------------------------------------------------- 

Global Dim Labels(3)
Labels(1)=?MyLabel_1
Labels(2)=?MyLabel_2
Labels(3)=?MyLabel_3

Procedure RestoreEx (*address)
	CompilerIf (#PB_Compiler_Processor = #PB_Processor_x86)
		!mov eax, [p.p_address]
		!mov [PB_DataPointer], eax
	CompilerElse
		!mov rax, [p.p_address]
		!mov [PB_DataPointer], rax
	CompilerEndIf
EndProcedure

RestoreEx(Labels(n))
Read.s label$
Debug label$

End

DataSection
	MyLabel_1: : Data.s "MyLabel_1"
	MyLabel_2: : Data.s "MyLabel_2"
	MyLabel_3: : Data.s "MyLabel_3"
	MyLabel_n: : Data.s "MyLabel_NNNNN"
EndDataSection
juergenkulow
Enthusiast
Enthusiast
Posts: 581
Joined: Wed Sep 25, 2019 10:18 am

Re: Macro concatenate doesn't work with variable?

Post by juergenkulow »

After adding MyLabel_n: in DataSection the preprocessor "-pp /tmp/a.pp" do Restore MyLabel_n:

Code: Select all

Macro DynamicRestore(n)
  MyLabel_#n
EndMacro
n=Random(3,1)
Restore MyLabel_n 
Read.s label$
Debug label$
End
DataSection
  MyLabel_n:  
  MyLabel_1:
 Data.s "MyLabel_1"
  MyLabel_2:
 Data.s "MyLabel_2"
  MyLabel_3:
 Data.s "MyLabel_3"
EndDataSection
Post Reply