Page 1 of 1

The macro command does not correctly resolve the # character

Posted: Sun Jul 30, 2017 12:56 pm
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.

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

Posted: Sun Jul 30, 2017 1:09 pm
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)

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

Posted: Sun Jul 30, 2017 1:16 pm
by Kurzer
Little John, yes. Great, did not realize that it would work this way. Image
Thanks.