The macro command does not correctly resolve the # character

Just starting out? Need help? Post your questions and find answers here.
User avatar
Kurzer
Enthusiast
Enthusiast
Posts: 693
Joined: Sun Jun 11, 2006 12:07 am
Location: Near Hamburg

The macro command does not correctly resolve the # character

Post by Kurzer »

Hello,

the PBs help in section "Macros" say:
"With the use of parameters it is possible to create very flexible macros. The special link character '#' can be used to create new labels or keywords by mixing macro code and parameter expressions"

This example from the documentation works very well:

Code: Select all

Macro XCase(Type, Text)
  Type#Case(Text)
EndMacro

Debug XCase(U, "Hello")
Debug XCase(L, "Hello")
But why the concatenation does not work if I want to paste an '*' charcter to transform a variable to a pointer?

Code: Select all

Macro IntRef(var)
	*#var#.integer
EndMacro

Debug IntRef(MyParam)
This will result in a compiler error, because 'MyParam' is expanded to '*#MyParam.integer'

The reason why I want to try this, I want to find an easy way to imitate the "ByRef"-parameter handling of PHP functions. In PHP you can handle a parameter variable of a function very easy "by reference" by pre-empting a & sign. Such a parameter variable is handled as pointer which points to the variable outside of the function but there is no need to change the syntax style inside or outside the function.

PHP-example:
function add(&$dollars, $value) {
$dollars = $dollars + $value;
}

$price = 10;
add($price, 50);
echo $price; // will print 60

In PureBasic we have to use another syntax at several code points if we want to use a procedure without or with "ByRef" handling.

Code: Select all

; Parameter by value
Procedure.f addval(dollars.f, value.f)
	ProcedureReturn dollars + value
EndProcedure

price.f = 10
price = addval(price, 50)
Debug price


; Parameter by reference
Procedure.f addref(*dollars.Float, value.f)
	*dollars\f + value
EndProcedure

price.f = 10
addref(@price, 50)
Debug price
My attempt is to use macros to keep the syntax for ByRef and ByVal procedures as identical as possible in PB.
Last edited by Kurzer on Sun Jul 30, 2017 2:31 pm, edited 1 time in total.
PB 6.12 x64, OS: Win 11 24H2 x64, Desktopscaling: 150%, CPU: I7 12700 H, RAM: 32 GB, GPU: Intel(R) Iris(R) Xe Graphics | NVIDIA GeForce RTX 3070, User age in 2025: 57y
"Happiness is a pet." | "Never run a changing system!"
Little John
Addict
Addict
Posts: 4812
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: The macro command does not correctly resolve the # chara

Post by Little John »

Hi,

if you do without the first # or without both #, is that resulting in what you want?

Code: Select all

Macro IntRef(var)
   *var.integer
   Deliberate sytax error To see the Macro expansion.
EndMacro

Debug IntRef(MyParam)
User avatar
Kurzer
Enthusiast
Enthusiast
Posts: 693
Joined: Sun Jun 11, 2006 12:07 am
Location: Near Hamburg

Re: The macro command does not correctly resolve the # chara

Post by Kurzer »

Little John, yes. Great, did not realize that it would work this way. Image
Thanks.
PB 6.12 x64, OS: Win 11 24H2 x64, Desktopscaling: 150%, CPU: I7 12700 H, RAM: 32 GB, GPU: Intel(R) Iris(R) Xe Graphics | NVIDIA GeForce RTX 3070, User age in 2025: 57y
"Happiness is a pet." | "Never run a changing system!"
Post Reply