Defining Macros in one line

Just starting out? Need help? Post your questions and find answers here.
User avatar
manu
User
User
Posts: 31
Joined: Tue May 13, 2003 12:40 pm
Location: Germany
Contact:

Defining Macros in one line

Post 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?
Mindphazer
Enthusiast
Enthusiast
Posts: 340
Joined: Mon Sep 10, 2012 10:41 am
Location: Savoie

Re: Defining Macros in one line

Post 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$
MacBook Pro 14" M1 Pro - 16 Gb - MacOS 14 - Iphone 15 Pro Max - iPad at home
...and unfortunately... Windows at work...
User avatar
manu
User
User
Posts: 31
Joined: Tue May 13, 2003 12:40 pm
Location: Germany
Contact:

Re: Defining Macros in one line

Post 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.
User avatar
manu
User
User
Posts: 31
Joined: Tue May 13, 2003 12:40 pm
Location: Germany
Contact:

Re: Defining Macros in one line

Post 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?
User avatar
Demivec
Addict
Addict
Posts: 4086
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Defining Macros in one line

Post 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.
User avatar
NicTheQuick
Addict
Addict
Posts: 1224
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: Defining Macros in one line

Post 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. :|
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
Quin
Enthusiast
Enthusiast
Posts: 282
Joined: Thu Mar 31, 2022 7:03 pm
Location: United States
Contact:

Re: Defining Macros in one line

Post 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
PB v5.40/6.10, Windows 10 64-bit.
16-core AMD Ryzen 9 5950X, 128 GB DDR5.
Olli
Addict
Addict
Posts: 1071
Joined: Wed May 27, 2020 12:26 pm

Re: Defining Macros in one line

Post by Olli »

Same observation, a few days apart...
https://www.purebasic.fr/english/viewtopic.php?t=79196
Post Reply