The macro command does not correctly resolve the # character
Posted: Sun Jul 30, 2017 12:56 pm
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:
But why the concatenation does not work if I want to paste an '*' charcter to transform a variable to a pointer?
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.
My attempt is to use macros to keep the syntax for ByRef and ByVal procedures as identical as possible in PB.
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")
Code: Select all
Macro IntRef(var)
*#var#.integer
EndMacro
Debug IntRef(MyParam)
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
