Make obligatory the '#' postfix for Macro input params

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
Psychophanta
Addict
Addict
Posts: 4969
Joined: Wed Jun 11, 2003 9:33 pm
Location: Lípetsk, Russian Federation
Contact:

Re: Make obligatory the '#' postfix for Macro input params

Post by Psychophanta »

This is still a big but fixable issue:
Compiler is not able to differenciate external variables neither constants from a macro local ones.

I suggested a way to fix it:
Make obligatory the '#' postfix for Macro input params
Will you fix it?
http://www.zeitgeistmovie.com

While world=business:world+mafia:Wend
Will never leave this forum until the absolute bugfree PB :mrgreen:
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Make obligatory the '#' postfix for Macro input params

Post by Little John »

Psychophanta wrote:I suggested a way to fix it:
Make obligatory the '#' postfix for Macro input params
That's no good idea, and it has been told you years ago.
Psychophanta wrote:Will you fix it?
It seems that you really like to beat dead horses ...
User avatar
Psychophanta
Addict
Addict
Posts: 4969
Joined: Wed Jun 11, 2003 9:33 pm
Location: Lípetsk, Russian Federation
Contact:

Re: Make obligatory the '#' postfix for Macro input params

Post by Psychophanta »

Little John wrote: That's no good idea, and it has been told you years ago.
Sorry to have to correct that:
The only things has been told are workarounds to solve it, but not a solution to differenciate external variables and the input parameters inside a macro.
http://www.zeitgeistmovie.com

While world=business:world+mafia:Wend
Will never leave this forum until the absolute bugfree PB :mrgreen:
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Make obligatory the '#' postfix for Macro input params

Post by Little John »

Psychophanta wrote:Sorry to have to correct that:
There's no need to discuss that further: Everyone who is interested can read her/himself what has been written in this thread more than 6-7 years ago.
User avatar
Psychophanta
Addict
Addict
Posts: 4969
Joined: Wed Jun 11, 2003 9:33 pm
Location: Lípetsk, Russian Federation
Contact:

Re: Make obligatory the '#' postfix for Macro input params

Post by Psychophanta »

Little John wrote:
Psychophanta wrote:Sorry to have to correct that:
There's no need to discuss that further: Everyone who is interested can read her/himself what has been written in this thread more than 6-7 years ago.
Agree, but repeat with the fact, and don't try to escape from reality, just like german people is in use to do again and again:
no solution to differenciate external variable names from input parameters names for macros.
:wink:
http://www.zeitgeistmovie.com

While world=business:world+mafia:Wend
Will never leave this forum until the absolute bugfree PB :mrgreen:
User avatar
Demivec
Addict
Addict
Posts: 4086
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Make obligatory the '#' postfix for Macro input params

Post by Demivec »

Psychophanta wrote:...no solution to differenciate external variable names from input parameters names for macros.
:wink:
Image

Use a different name. :wink:
User avatar
Tenaja
Addict
Addict
Posts: 1948
Joined: Tue Nov 09, 2010 10:15 pm

Re: Make obligatory the '#' postfix for Macro input params

Post by Tenaja »

Demivec wrote:
Psychophanta wrote:...no solution to differenciate external variable names from input parameters names for macros.
:wink:
Use a different name. :wink:
Exactly. There is no confusion with this:

Code: Select all

    Define .f
    v=1.0
    Macro ma(Macro_ma_Parameter_v=2.0)
      Debug Pow(Macro_ma_Parameter_v,v)
    EndMacro

    ma()
:mrgreen: <---note: indicates friendly sarcasm towards both Psycho's request and Demivec's suggestion.

IMO, the compiler should parse variable names with precedence from inside out. That is, automatically search the scope starting with the most recent construct. So, any v references should be forced to be the macro parameter v if you are within a macro, and local v if within a procedure, and last, the global var. This would make all other references to v essentially invisible if you are doubling-up on your variable name use. (I have not tested to see if PB does this or not.)

I know this does not help find frustrating bugs within large code, but neither does repetitive re-use of variable names.

...and yes, I see the value within the request, as well. I think it would be handy, and would certainly give it a +1 regardless of how "ugly" it is. However, I just work around it as Demivec suggests.
User avatar
Tenaja
Addict
Addict
Posts: 1948
Joined: Tue Nov 09, 2010 10:15 pm

Re: Make obligatory the '#' postfix for Macro input params

Post by Tenaja »

Demivec wrote:Use a different name. :wink:
A simple technique is to habitually prefix an underscore (or "InsertYourFavoritePrefixHere") to all macro parms:

Code: Select all

        Define .f
        v=1.0
        Macro ma(_v=2.0)
          Debug Pow(_v,v)
        EndMacro

        ma()
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Make obligatory the '#' postfix for Macro input params

Post by Little John »

Psychophanta wrote:don't try to escape from reality, just like german people is in use to do again and again:
You really made my day. :lol: I'm so sorry that I can't confirm your prejudices.
You better shouldn't project your character onto others. :lol: :lol:
Psychophanta wrote:no solution to differenciate external variable names from input parameters names for macros.
Huh, really? Oh what a big problem!
Image
Tenaja wrote:
Demivec wrote:Use a different name. :wink:
A simple technique is to habitually prefix an underscore (or "InsertYourFavoritePrefixHere") to all macro parms:
I've suggested the same thing to him more than 6 years ago.
This is probably too far away from his ... erm ... "reality". :lol:
User avatar
Psychophanta
Addict
Addict
Posts: 4969
Joined: Wed Jun 11, 2003 9:33 pm
Location: Lípetsk, Russian Federation
Contact:

Re: Make obligatory the '#' postfix for Macro input params

Post by Psychophanta »

No matters if big or small problem Litle John, just a problem.
A problem which a good and elegant compiler like PB is, should solve.

Thanks!
http://www.zeitgeistmovie.com

While world=business:world+mafia:Wend
Will never leave this forum until the absolute bugfree PB :mrgreen:
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Make obligatory the '#' postfix for Macro input params

Post by Little John »

Psychophanta wrote:A problem which a good and elegant compiler like PB is, should solve.
You did actually not understand the replies that you got 6-7 years ago, among others from Fred and Freak.
Sorry, I can't help you.
User avatar
Psychophanta
Addict
Addict
Posts: 4969
Joined: Wed Jun 11, 2003 9:33 pm
Location: Lípetsk, Russian Federation
Contact:

Re: Make obligatory the '#' postfix for Macro input params

Post by Psychophanta »

Little John wrote:
Psychophanta wrote:A problem which a good and elegant compiler like PB is, should solve.
You did actually not understand the replies that you got 6-7 years ago, among others from Fred and Freak.
Sorry, I can't help you.
Mmm... allow me to remember....
Yes.

Just look:
freak wrote:You are the only one i have seen putting a # behind every macro parameter.
I think it is a very weird thing to do (as the # is the concatination operator for macros, not a parameter postfix)

I don't think this is a good idea.
Never mind if i are the only one who do that (I am not really), it is irrelevant.
very weird thing to do?
Not good idea, well, I think it is not.
Fred wrote:It's way too ugly ! Don't mess with global var in your macro..
ugly ! ¿?
Fred wrote:# is already used in the macros to join 2 parameters together.
We know it, thanks for remember it anyway.
freak wrote:I don't see the problem really. It is just like with procedures.

In procedures, if you reuse a global variable (not as a parameter and without protected),
you are referencing the global variable. Same thing happens with macros.

In a procedure, if you reuse the global variable name inside a procedure as a parameter
or with protected, it will not use the global one, and you have actually no more a way
to access the global variable from inside this procedure (as the name represents the local variable then)
The same thing is with macro parameters that have the same name as a variable used elsewhere. makes perfect sense.

You always have to make sure you do not reuse the same name for different purposes.
That is a problem that exists in every language, not just in PB macros.
You can't just name all your global variables "var" and expect to get no conflict.
Choosing descriptive names solves this problem easily.
Yeah, just like procedures. Makes perfect sense... o_o :

Code: Select all

Structure stru
  var1.f
  var2.l
EndStructure
  
Global v.stru
v\var2=4
var2.l=5

Procedure ma(var2=6)
  Debug v.stru\var2
EndProcedure

ma()
and:

Code: Select all

Structure stru
  var1.f
  var2.l
EndStructure
  
Global v.stru
v\var2=4
var2.l=5

Macro ma(var2=6)
  Debug v.stru\var2
EndMacro

ma()
Patience....

EDIT:
NOTICE: I don't say to use '#' as postfix if for you is ugly, ok, right, but just suggest to improve another good idea from your part to avoid this issue.
http://www.zeitgeistmovie.com

While world=business:world+mafia:Wend
Will never leave this forum until the absolute bugfree PB :mrgreen:
User avatar
Psychophanta
Addict
Addict
Posts: 4969
Joined: Wed Jun 11, 2003 9:33 pm
Location: Lípetsk, Russian Federation
Contact:

Re: Make obligatory the '#' postfix for Macro input params

Post by Psychophanta »

About 4 hours with a powerful pain in brain because of this topic. :|
Finally I got to isolate the problem, and here it is:
Look at this one:

Code: Select all

#Colorhilo2=$AAEE90

Global .f a=12,b=12,c=11,e=3

Global fasehilo1.u=0,fasehilo2.u

Macro seno(f=0,desv=0)
  Sin(f#+e*x/c)
EndMacro

Macro dibuja2(h,c)
  For x.u=0 To 2
    y.f = Sin(fasehilo#h#+e*x/c)
    Debug y
    y.f = seno(fasehilo#h#)
    Debug y
    Debug "-----------------"
  Next
EndMacro

dibuja2(2,#Colorhilo2)
http://www.zeitgeistmovie.com

While world=business:world+mafia:Wend
Will never leave this forum until the absolute bugfree PB :mrgreen:
BorisTheOld
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Apr 24, 2012 5:08 pm
Location: Ontario, Canada

Re: Make obligatory the '#' postfix for Macro input params

Post by BorisTheOld »

@Psychophanta

You might save yourself a lot time and trouble if you didn't use single-letter parameters, constants, and variables. Here are a few of our macros that might show you a better way of organizing your code.

Rod

Code: Select all

;-----------------------------------------------
;
;  Macro 1 : QQ                             double quote character used in macro creation
;
;
Macro QQ
  "
EndMacro
;
;-----------------------------------------------
;
;  Macro 2 : QQColon                        colon character used in macro creation
;
;
Macro QQColon
  :
EndMacro
;
;-----------------------------------------------
;
;  Macro 3 : QQJoin                         join parameter strings for macro creation
;
;  bvsP1                                    16725 : PB macro parameter:  1
;  bvsP2                                    16726 : PB macro parameter:  2
;  bvsP3                                    16727 : PB macro parameter:  3
;  bvsP4                                    16728 : PB macro parameter:  4
;  bvsP5                                    16729 : PB macro parameter:  5
;  bvsP6                                    16730 : PB macro parameter:  6
;  bvsP7                                    16731 : PB macro parameter:  7
;  bvsP8                                    16732 : PB macro parameter:  8
;
Macro QQJoin (bvsP1, bvsP2=, bvsP3=, bvsP4=, bvsP5=, bvsP6=, bvsP7=, bvsP8=)
  bvsP1#bvsP2#bvsP3#bvsP4#bvsP5#bvsP6#bvsP7#bvsP8
EndMacro
;
;-----------------------------------------------
;
;  Macro 4 : CreateBlockName                create the QQBlock macro
;
;  bvsBlockName                             16663 : the name of a class or module
;
Macro CreateBlockName (bvsBlockName)
  QQJoin (Macro QQBlock, QQColon, bvsBlockName, QQColon, EndMacro)
EndMacro
;
;-----------------------------------------------
;
;  Macro 5 : DestroyBlockName               destroy the QQBlock macro
;
;
Macro DestroyBlockName
  QQJoin (UndefineMacro Q, QBlock)
EndMacro
;
;-----------------------------------------------
;
;  Macro 6 : CreateLibraryName              create the QQLib macro
;
;  bvsLibName                               16505 : the name of a dynamic library
;
Macro CreateLibraryName (bvsLibName)
  QQJoin (Macro QQLib, QQColon, bvsLibName, QQColon, EndMacro)
EndMacro
;
;-----------------------------------------------
;
;  Macro 7 : DestroyLibraryName             destroy the QQLib macro
;
;
Macro DestroyLibraryName
  QQJoin (UndefineMacro Q, QLib)
EndMacro
;
;-----------------------------------------------
;
;  Macro 8 : AttachDynamicLibrary           attach a dynamic library to the program
;
;  bvsLibName                               16505 : the name of a dynamic library
;
Macro AttachDynamicLibrary (bvsLibName)
  Global lib#bvsLibName.typInt32
  CompilerSelect #PB_Compiler_OS
    CompilerCase #PB_OS_Linux
      lib#bvsLibName = OpenLibrary(#PB_Any, QQ./lib#bvsLibName.so#QQ)
      If IsFalse(lib#bvsLibName)
        MessageRequester("Critical Error", QQ#Dynamic Library not found -- lib#bvsLibName.so#QQ, #PB_MessageRequester_Ok)
        End
      EndIf
    CompilerCase #PB_OS_Windows
      lib#bvsLibName = OpenLibrary(#PB_Any, QQ#bvsLibName.dll#QQ)
      If IsFalse(lib#bvsLibName)
        MessageRequester("Critical Error", QQ#Dynamic Library not found -- bvsLibName.dll#QQ, #PB_MessageRequester_Ok)
        End
      EndIf
    CompilerCase #PB_OS_MacOS
      ????
    CompilerCase #PB_OS_AmigaOS
      ????
  CompilerEndSelect
EndMacro
;
;-----------------------------------------------
;
;  Macro 9 : DetachDynamicLibrary           detach a dynamic library from the program
;
;  bvsLibName                               16505 : the name of a dynamic library
;
Macro DetachDynamicLibrary (bvsLibName)
  CloseLibrary(lib#bvsLibName)
EndMacro
;
;-----------------------------------------------
;
;  Macro 10 : BeginLibraryDeclare           link the program to dynamic library procedures
;
;  bvsLibName                               16505 : the name of a dynamic library
;
Macro BeginLibraryDeclare (bvsLibName)
  CreateLibraryName(bvsLibName)
EndMacro
;
;-----------------------------------------------
;
;  Macro 11 : EndLibraryDeclare             end declares for a dynamic library
;
;
Macro EndLibraryDeclare
  DestroyLibraryName
EndMacro
;
;-----------------------------------------------
;
;  Macro 12 : FunctionPrototype             dynamic library prototype:  function
;
;  bvsProcName                              15157 : name of the proc being generated
;  bvsDataParmType                          15080 : parameter data type
;
Macro FunctionPrototype (bvsProcName, bvsDataParmType)
  Prototype.bvsDataParmType QQLib#bvsProcName
EndMacro
;
;-----------------------------------------------
;
;  Macro 13 : SubroutinePrototype           dynamic library prototype:  subroutine
;
;  bvsProcName                              15157 : name of the proc being generated
;
Macro SubroutinePrototype (bvsProcName)
  Prototype QQLib#bvsProcName
EndMacro
;
;-----------------------------------------------
;
;  Macro 14 : CreateFunctionReference       create a reference to a library function
;
;  bvsProcName                              15157 : name of the proc being generated
;
Macro CreateFunctionReference (bvsProcName)
  Global lib#QQLib#_fun#bvsProcName.QQLib#bvsProcName = GetFunction(lib#QQLib, QQ#QQLib#bvsProcName#QQ)
EndMacro
;
;-----------------------------------------------
;
;  Macro 15 : CreateSubroutineReference     create a reference to a library subroutine
;
;  bvsProcName                              15157 : name of the proc being generated
;
Macro CreateSubroutineReference (bvsProcName)
  Global lib#QQLib#_sub#bvsProcName.QQLib#bvsProcName = GetFunction(lib#QQLib, QQ#QQLib#bvsProcName#QQ)
EndMacro
;
;-----------------------------------------------
;
;  Macro 16 : LibCall                       call a dynamic library subroutine
;
;  bvsLibName                               16505 : the name of a dynamic library
;  bvsProcName                              15157 : name of the proc being generated
;
Macro LibCall (bvsLibName, bvsProcName)
  lib#bvsLibName#_sub#bvsProcName
EndMacro
;
;-----------------------------------------------
;
;  Macro 17 : LibReturn                     return a value from a dynamic library function
;
;  bvsLibName                               16505 : the name of a dynamic library
;  bvsProcName                              15157 : name of the proc being generated
;
Macro LibReturn (bvsLibName, bvsProcName)
  lib#bvsLibName#_fun#bvsProcName
EndMacro
;
For ten years Caesar ruled with an iron hand, then with a wooden foot, and finally with a piece of string.
~ Spike Milligan
Post Reply