Evaluate Macro parameter before macro execution?

Just starting out? Need help? Post your questions and find answers here.
Ramses800
User
User
Posts: 27
Joined: Wed Nov 05, 2014 3:12 pm
Location: Sweden

Evaluate Macro parameter before macro execution?

Post by Ramses800 »

I'm trying to figure out how to feed a variable to a macro and use it to assemble it into another variable name inside the macro. The ting is , I would like call the macro inside a loop like below but the parameter used in the concantenated variable name in the macro is the literal value of the variable not its content even if it would seem that it should work? Is there any way to do this?

Code: Select all

Macro TestD(number)
  Debug "Macro input parameter:" + number
  Debug "Variable value :" + Str(i#number)
EndMacro

i1=2
i2=4
i3=6
; Works
TestD(1)
TestD(2)
TestD(3)
; Does not work
For i=1 To 3
  Testd(i)
Next
results in

Code: Select all

[20:55:59] Waiting for executable to start...
[20:55:59] Executable type: Windows - x86  (32bit, Thread)
[20:55:59] Executable started.
[20:55:59] [Debug] Macro input parameter:1
[20:55:59] [Debug] Variable value :2
[20:55:59] [Debug] Macro input parameter:2
[20:55:59] [Debug] Variable value :4
[20:55:59] [Debug] Macro input parameter:3
[20:55:59] [Debug] Variable value :6
[20:55:59] [Debug] Macro input parameter:1
[20:55:59] [Debug] Variable value :0
[20:55:59] [Debug] Macro input parameter:2
[20:55:59] [Debug] Variable value :0
[20:55:59] [Debug] Macro input parameter:3
[20:55:59] [Debug] Variable value :0
[20:55:59] The Program execution has finished.
Former VB6 developer adventuring in a brave, new Purebasic world!
User avatar
skywalk
Addict
Addict
Posts: 4242
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Evaluate Macro parameter before macro execution?

Post by skywalk »

Macros are expanded at compile time so only constants are known.
Your variable i = 0 at this time.
Use a Procedure(). :wink:
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
Demivec
Addict
Addict
Posts: 4282
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Evaluate Macro parameter before macro execution?

Post by Demivec »

You are trying to reference a variable with a dynamic value (one that is determine at runtime). Macros only function at compile time.

Another option is to use an array instead that you store your values in and index as you need them. If you need more diverse names to be accessed you can declare those variables with 'Runtime' and use the appropriate functions to access them from the Runtime library.
Last edited by Demivec on Sun Jan 31, 2016 11:35 pm, edited 1 time in total.
User avatar
STARGÅTE
Addict
Addict
Posts: 2261
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Evaluate Macro parameter before macro execution?

Post by STARGÅTE »

A macro is a compiler function.
Your For-loop is a run time function, so the macro cannot know the value of "i" and the macro is translate to:
Debug "Variable value :" + Str(ii)
because, the "i" is insert for "#number"

There is no chance to to get i1, i2 and i3 from a macro which called only one time (on the compiler!) in your loop.
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
Ramses800
User
User
Posts: 27
Joined: Wed Nov 05, 2014 3:12 pm
Location: Sweden

Re: Evaluate Macro parameter before macro execution?

Post by Ramses800 »

Thanks for the replies!
But the thing that is confusing me, seeing the outputted results, it seems that the macro indeed can use the incremented value in this part

Code: Select all

Debug "Macro input parameter:" + number
but not in this part

Code: Select all

Debug "Variable value :" + Str(i#number)
Former VB6 developer adventuring in a brave, new Purebasic world!
User avatar
STARGÅTE
Addict
Addict
Posts: 2261
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Evaluate Macro parameter before macro execution?

Post by STARGÅTE »

Your macro:

Code: Select all

Macro TestD(number)
  Debug "Macro input parameter:" + number
  Debug "Variable value :" + Str(i#number)
EndMacro
and this code:

Code: Select all

For i=1 To 3
  Testd(i)
Next
is translated to this result:

Code: Select all

For i=1 To 3
  Debug "Macro input parameter:" + i
  Debug "Variable value :" + Str(ii)
Next
Note the "ii" in the second line, this variable name doesn't exist.
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
Ramses800
User
User
Posts: 27
Joined: Wed Nov 05, 2014 3:12 pm
Location: Sweden

Re: Evaluate Macro parameter before macro execution?

Post by Ramses800 »

Thanks stargate, that clearifies it to me!
Former VB6 developer adventuring in a brave, new Purebasic world!
Post Reply