Page 1 of 1

[Macro] Not sure is this a bug or feature

Posted: Sun Jan 18, 2015 4:31 am
by UncleBen
simple macro:
to replace [...something...] to [...otherthing...]
":Endif" replace "Endif"

Code: Select all

Macro Endif
    :Endif
EndMacro
This trying to macro some "Keywords", this give compiler error.


ok, below this is try to change the a "Traditional Remark" to "PB Remark"
Replace "REM" with ";"

Code: Select all

Macro REM
  ;
EndMacro
REM This is comments

For i= 1 To 100
  Debug i
Next
or even put in singleline

Code: Select all

Macro REM :    ; : EndMacro
or
Change "{" to ":"

Code: Select all

Macro {
   :
EndMacro
It didn't work, any one mind to tell the work around ? thanks anyway

Re: [Macro] Not sure is this a bug or feature

Posted: Sun Jan 18, 2015 9:02 am
by Little John
UncleBen wrote:simple macro:
to replace [...something...] to [...otherthing...]
":Endif" replace "Endif"

Code: Select all

Macro Endif
    :Endif
EndMacro
This trying to macro some "Keywords", this give compiler error.
... and the compiler also tells you the reason why:
Error message wrote:A macro can't have the same name as a keyword: EndIf.
UncleBen wrote:Change "{" to ":"

Code: Select all

Macro {
   :
EndMacro
It didn't work, any one mind to tell the work around ? thanks anyway
I don't know where this is officially documented, but I think macro names (like any other identifiers in PB) can only contain digits 0-9 (not at the beginning), characters A-Z, a-z, and "_".

Re: [Macro] Not sure is this a bug or feature

Posted: Sun Jan 18, 2015 12:21 pm
by UncleBen
Little John wrote:
UncleBen wrote: A macro can't have the same name as a keyword: EndIf.

I don't know where this is officially documented, but I think macro names (like any other identifiers in PB) can only contain digits 0-9 (not at the beginning), characters A-Z, a-z, and "_".
There was so much fun if the macro can replace anything given to, including keywords , it should work that way isn't it ?

See this :

Code: Select all

Macro REM
  ;
EndMacro

Macro THEN
  :
EndMacro

Macro EndIf
  :EndIf
EndMacro

Macro {
  :
EndMacro

Macro }
  Wend
EndMacro

REM *** This example try to illustrate the power and flexibility of Macro preprocessor
REM *** unfortunately , ONLY the second Macro works
  
  
REM single Line If ...THEN ....  
  
If a=b THEN c=d+e EndIf


While a=b 
  {
  c=d+e
  dosomething()
  }
  
However ,below is working beautifully:

Code: Select all

  Macro SUB
      Procedure
  EndMacro
  
  Macro ENDSUB
      EndProcedure
  EndMacro  

SUB dosomething()
   a=b+2
   c=a+d
   Procedurereturn c
ENDSUB

G=dosomething()


I think you probably figure out what i want to do with macro, yes , to extend the language to my comfortable .

Re: [Macro] Not sure is this a bug or feature

Posted: Sun Jan 18, 2015 1:14 pm
by Little John
You posted this in the "Bugs - Documentation" subforum.
So what exactly is the bug in the documentation that you want to report?

Re: [Macro] Not sure is this a bug or feature

Posted: Sun Jan 18, 2015 1:35 pm
by UncleBen
Little John wrote:You posted this in the "Bugs - Documentation" subforum.
So what exactly is the bug in the documentation that you want to report?

I'm not sure , that's why i ask,
[Macro] Not sure is this a bug or feature
for the MACRO , i suppose understand :

Macro something_to_be_replace
something_actual
EndMacro

So... that something is it anything ? if so , i would be very happy :)

sorry to bother you

Re: [Macro] Not sure is this a bug or feature

Posted: Sun Jan 18, 2015 2:17 pm
by luis
FYI: when you are not sure you can just post your question (because this is a question) in "Coding questions" first.
You can post it again in the appropriate section once you've made your mind about it. :wink:

About your question: documentation could be more strict but a simple DO BY YOURSELF test seems to suggest a macro name has to follow the same rules used for identifiers, so no strange names like (, ~, {, etc.

See the the rules to be observed when naming a variable in the manual.

So you are out of luck in replacing those.

About your REM example, I think that outcome may be debatable:

Code: Select all

Macro DQ
 "
EndMacro

Macro MYCHAR_1
 $ 
EndMacro

Macro MYCHAR_2
 ;
EndMacro

a$ = DQ#MYCHAR_1#DQ
b$ = DQ#MYCHAR_2#DQ

Debug a$ ; $
Debug b$ ; empty string
By logic, the second example should work too, but it seems PB just throw away any comment when expanding a macro (so yours doesn't work too).
That is bad in this specific case (and yours), but by doing so it lets you to write comments inside a macro without affecting the macro.

For example:

Code: Select all

Macro MYCHAR_1
 $; this is a dollar sign
EndMacro
still works the same

Since in a macro you can write the most absurd stuff (when it works), and what written inside a macro doesn't have to be syntactically valid (only when expanded in the final context need to make sense) I would be more inclined in not treating the comments differently and expanding them too like any other text inside the macro.
For example now we have a compiler switch which gives as output the post-processed source file with macros expanded, and one could want to output comments in specific places by using macros, maybe for textual markers to be used by a precompiler or something like that.
Also removing comments from the macro contrasts with the idea that no evaluation of the contents is done at this stage. The macro should be simply expanded and the resulting line processed by the compiler and the comments stripped only then.
May be debatable, it's possible there is something else I'm not considering right now.

Re: [Macro] Not sure is this a bug or feature

Posted: Mon Jan 19, 2015 2:36 am
by UncleBen
From [PB5.30]Documentation of MACRO
Macros


Syntax
Macro <name> [(Parameter [, ...])]
...
EndMacro

Description

Macros are a very powerful feature, mainly useful for advanced programmers. A macro is a placeholder for some code (one keyword, one line or even many lines), which will be directly inserted in the source code at the place where a macro is used. In this, it differs from procedures, as the procedures doesn't duplicate the code when they are called.

The Macro : EndMacro declaration must be done before the macro will be called for the first time. Because macros will be completely replaced by their related code at compile time, they are not local to a procedure.

A macro can not have a return type nor typed parameters. When a macro has some parameters, they are replaced in the macro code by the literal expression which is passed to the called macro. No evaluation is done as this stage, which is very important to understand: the evaluation of a line is started once all the macros found on this line are expanded.

The macros are divided into two categories: simple (without parameters) and complex (with parameters, needs the parentheses when calling it). When using no parameters, it's possible to replace any word with another word (or any expression). The macros can be used recursively, but if the parameter passed contain the concatenation character '#', it won't be expanded.
It doesn't state clearly what is valid and invalid.

suppose a simple macro without parameters will replace whatever giving to it directly ? IMHO

OR
use this one

Code: Select all

;replace whatever given to new
MacroReplace whatever
   Newtext;including this comment,this line doesn't check syntax ,this is waht macro do 
EndMacro

;with parameter
Macro newfunction(a,b)
      syntax(a)
      syntax(b)
      syntax(a,b)
EndMacro


Re: [Macro] Not sure is this a bug or feature

Posted: Mon Jan 19, 2015 10:21 am
by Danilo
PB Style:

Code: Select all

Macro Then(_stmt1_=,_stmt2_=,_stmt3_=,_stmt4_=,_stmt5_=,_stmt6_=,_stmt7_=,_stmt8_=,_stmt9_=,_stmt10_=)
  : _stmt1_ : _stmt2_ : _stmt3_ : _stmt4_ : _stmt5_ : _stmt6_ : _stmt7_ : _stmt8_ : _stmt9_ : _stmt10_ : EndIf
EndMacro

Macro Do(_stmt1_=,_stmt2_=,_stmt3_=,_stmt4_=,_stmt5_=,_stmt6_=,_stmt7_=,_stmt8_=,_stmt9_=,_stmt10_=)
  : _stmt1_ : _stmt2_ : _stmt3_ : _stmt4_ : _stmt5_ : _stmt6_ : _stmt7_ : _stmt8_ : _stmt9_ : _stmt10_ : Wend
EndMacro

Macro Comment(_text_) : : : EndMacro
Macro REM(_text_)     : : : EndMacro

;--------------------------------------------------

REM( single Line If ...THEN .... )

If a=b Then( c=d+e )

If a=0
    Then( Debug "a=0", a+1 )

Debug "---"

While a < 10 Do( a+1, Debug a)

Debug "---"

While a > 0
    Do( a-1, Debug a)


Debug "---"


Re: [Macro] Not sure is this a bug or feature

Posted: Mon Jan 19, 2015 12:04 pm
by UncleBen
Danilo wrote:PB Style:

Code: Select all

Macro Then(_stmt1_=,_stmt2_=,_stmt3_=,_stmt4_=,_stmt5_=,_stmt6_=,_stmt7_=,_stmt8_=,_stmt9_=,_stmt10_=)
  : _stmt1_ : _stmt2_ : _stmt3_ : _stmt4_ : _stmt5_ : _stmt6_ : _stmt7_ : _stmt8_ : _stmt9_ : _stmt10_ : EndIf
EndMacro

Macro Do(_stmt1_=,_stmt2_=,_stmt3_=,_stmt4_=,_stmt5_=,_stmt6_=,_stmt7_=,_stmt8_=,_stmt9_=,_stmt10_=)
  : _stmt1_ : _stmt2_ : _stmt3_ : _stmt4_ : _stmt5_ : _stmt6_ : _stmt7_ : _stmt8_ : _stmt9_ : _stmt10_ : Wend
EndMacro

Macro Comment(_text_) : : : EndMacro
Macro REM(_text_)     : : : EndMacro

;--------------------------------------------------

REM( single Line If ...THEN .... )

If a=b Then( c=d+e )

If a=0
    Then( Debug "a=0", a+1 )

Debug "---"

While a < 10 Do( a+1, Debug a)

Debug "---"

While a > 0
    Do( a-1, Debug a)


Debug "---"

I must say , I LOVE U !
:)
What a beautiful !

Re: [Macro] Not sure is this a bug or feature

Posted: Mon Jan 19, 2015 12:17 pm
by luis
Do you like that ? :shock:

Even using REM like a function with text as its param ? Looks pretty ugly yo me.

It's not exactly replacing ";" with "REM"

Or multiple lines which would go inside an IF/ENDIF block now on a single line separated by commas ?

I thought you were not looking for something like that. Why do you want to hurt yourself and add limitations not present in the language ?

But .... "de gustibus non est disputandum", so ...OK !

You'll have to expand the macros if you'll ever have to post a question in the forum because that's not PB syntax anymore. I think it would be worth the effort to learn to use PB as it is.

But it's just my opinion.

Re: [Macro] Not sure is this a bug or feature

Posted: Mon Jan 19, 2015 2:00 pm
by UncleBen
luis wrote:Do you like that ? :shock:

Even using REM like a function with text as its param ? Looks pretty ugly yo me.

It's not exactly replacing ";" with "REM"

Or multiple lines which would go inside an IF/ENDIF block now on a single line separated by commas ?

I thought you were not looking for something like that. Why do you want to hurt yourself and add limitations not present in the language ?

But .... "de gustibus non est disputandum", so ...OK !

You'll have to expand the macros if you'll ever have to post a question in the forum because that's not PB syntax anymore. I think it would be worth the effort to learn to use PB as it is.

But it's just my opinion.
I don't like that , really ! :oops:
it's ugly too , sorry to say that. :|

This isn't anything that i look for.

But , i love his effort and brave to code this idea !



P/S: one way to satisfy myself may be is to custom a pre-proccesor , but not really want that path.
This is not a syntax or coding question , i just need the direct replacement without parsing, i thought macro will do the job. but it doesn't (for now)

Re: [Macro] Not sure is this a bug or feature

Posted: Mon Jan 19, 2015 2:22 pm
by luis
UncleBen wrote: But , i love his effort and brave to code this idea !
Oh sure, I didn't mean to criticize Danilo, there is not much you can do, I was just surprised you were happy with that. Looked like a downgrade to the language to me :)
UncleBen wrote: P/S: one way to satisfy myself may be is to custom a pre-proccesor , but not really want that path.
I understand, but I think it's the only way to get a reasonably decedent result, even if it has some shortcomings too. Some people here wrote preprocessors to add OOP to PB and they seemed happy with them.
UncleBen wrote: i just need the direct replacement without parsing, i thought macro will do the job. but it doesn't (for now)
Unfortunately no, and realistically I don't think they will ever be able to do what you wish. :|