Page 1 of 1

Defining Macros in one line

Posted: Wed May 11, 2022 1:17 pm
by manu
Hi!

I came across a strange behavior. What exactly is the difference between defining a macro one line (with ":") and in mutilple lines?
Is it even allowed to define a macro in one line?

Example:

Code: Select all

Macro DoubleQuote
  "
EndMacro

Macro Test1(x):Debug DoubleQuote#x#DoubleQuote:EndMacro

Macro Test2(x)
  Debug DoubleQuote#x#DoubleQuote
EndMacro

Test1(Apple)
Test2(Banana)
The DoubleQuote macro cannot be defined in one line.
The Test1 and Test2 are the same macros, just written diffently, one could think.
But only the Test2 macro behaves as expected, the Test1 macro outputs just "x", the parameter is not expanded.

Can someone explain this? Feature (unknown to me) or bug?

BTW: The DoubleQuote feels like a hack. It would be better to have a special character as prefix or postfix to the parameter name and the macro processor would automatically stringify it, like in this example Debug x$ or $x or #x or ##x or whatever. But I don't think this exists, right?

Re: Defining Macros in one line

Posted: Wed May 11, 2022 1:25 pm
by Mindphazer
manu wrote: Wed May 11, 2022 1:17 pm BTW: The DoubleQuote feels like a hack. It would be better to have a special character as prefix or postfix to the parameter name and the macro processor would automatically stringify it, like in this example Debug x$ or $x or #x or ##x or whatever. But I don't think this exists, right?
There is a constant for this : #DQUOTE$

Re: Defining Macros in one line

Posted: Wed May 11, 2022 2:23 pm
by manu
Mindphazer wrote: Wed May 11, 2022 1:25 pm
manu wrote: Wed May 11, 2022 1:17 pm BTW: The DoubleQuote feels like a hack. It would be better to have a special character as prefix or postfix to the parameter name and the macro processor would automatically stringify it, like in this example Debug x$ or $x or #x or ##x or whatever. But I don't think this exists, right?
There is a constant for this : #DQUOTE$
You can't use #DQUOTE$ here, it doesn't work with the # concatenation operator.

Re: Defining Macros in one line

Posted: Thu May 19, 2022 11:57 am
by manu
If nobody can explain why this happens, I call it a bug and will repost it in the bugs forum, or can someone move this topic?

Re: Defining Macros in one line

Posted: Thu May 19, 2022 12:48 pm
by Demivec
manu wrote: Thu May 19, 2022 11:57 am If nobody can explain why this happens, I call it a bug and will repost it in the bugs forum, or can someone move this topic?
I say it's a bug.

Rewriting the macro different ways shows a pattern for errors. When the Macro is written with the command concatenation character ':' the macro fails if the command 'Macro' is not written on a line by itself and it fails when the command 'Macro' is combined with any other lines of the macro's contents but excluding 'EndMacro'.

Code: Select all

Macro DoubleQuote
  "
EndMacro

;works, Test1(Apple) produces 'Debug "Apple"'
Macro Test1(x)
  Debug DoubleQuote#x#DoubleQuote
EndMacro
Test1(Apple)

;fails, Test2(Apple) produces 'Debug "x"'
Macro Test2(x):Debug DoubleQuote#x#DoubleQUOTE:EndMacro
Test2(Apple)

;fails, Test3(Apple) produces 'Debug "x"'
Macro Test3(x):Debug DoubleQuote#x#DoubleQUOTE
EndMacro
Test3(Apple)

;works, Test4(Apple) produces 'Debug "Apple"'
Macro Test4(x)
  Debug DoubleQuote#x#DoubleQUOTE:EndMacro
Test4(Apple)
There is of course also the separate issue of not being able to use '#DQUOTE'$' in certain situations with the concatenation character '#' but I did not test that completely.

Re: Defining Macros in one line

Posted: Thu May 19, 2022 1:00 pm
by NicTheQuick
I guess the problem has something to with how Purebasic compiles the file. When "Macro name(arguments..)" are in the same line as the parameter the replacing mechanism for arguments is skipped. I guess replacing macros or its argument from the current scope happens before the line goes to the actual parser and compiler. The macro handling is a completely different step, even before commands were separated by a colon.

Example:

Code: Select all

Macro DoubleQuote
  "
EndMacro

Macro Test1(x):Debug DoubleQuote#x#DoubleQuote
Debug DoubleQuote#x#DoubleQuote:EndMacro

Test1(Apple)
On the other hand, this seems to work:

Code: Select all

Macro DoubleQuote
  "
EndMacro

Macro Test1(x):Debug x#_var
Debug x:EndMacro

Apple_var = 2
Apple = 1
Test1(Apple)
Well, I don't know anymore. :|

Re: Defining Macros in one line

Posted: Thu May 19, 2022 2:34 pm
by Quin
This is more than likely a bug. I tend to define Macros in one line, and have never had any such issues. For example, this works:

Macro k(key): #PB_Key_#key: EndMacro

Re: Defining Macros in one line

Posted: Fri May 20, 2022 1:44 pm
by Olli
Same observation, a few days apart...
https://www.purebasic.fr/english/viewtopic.php?t=79196