Announcement

Collapse
No announcement yet.

Macro Glitch?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Macro Glitch?




    I found this last night. It was a pretty ugly glitch to track! one of my conditions got so big that i had to use line continuations.

    Normally this is not an issue, but since the condition was so big, i put it in a MACRO to make the statement smaller and clearer.

    I got distracted and went to do something else, when i came back the code was complaining about false errors. I had to look for it
    since at fist it was an inocuous change in the code. But turned out to be very hard to track... Im sharing so others do not have to
    deal with this...


    Code:
    #COMPILE EXE
    
    ' This macro is short and dumb for simplicity of this error demo code,
    ' In real world code, the condition makes sense and it is big enough to require
    ' line continuations.
    MACRO MACRO_TEST(i) = (1 = 1) OR _
                          (2 = 2)
    
    FUNCTION PBMAIN AS LONG
    
        ' NORMALLY, CONDITIONS WITH LINE CONTINUATION WORK AS EXPECTED.
        IF (1 = 1) OR _
           (2 = 2) THEN
           MSGBOX "THE REQUIREMENTS ARE FILLED."
        END IF
    
        ' WHEN IN A MACRO, IT BREAKS IF/END IF CONTINUITY.
        IF MACRO_TEST(10) THEN
            MSGBOX "THE REQUIREMENTS ARE FILLED."
        END IF
    
    END FUNCTION
    www.pluribasic.com

  • #2
    Help: Macro:

    The first line of a MACRO definition is termed the macro prototype, and this line may not be split into multiple logical lines with the underscore (_) line continuation character.

    and

    MACRO prototypes (those beginning with the MACRO keyword) and END MACRO = returnexpression lines must be constructed on a single line of source code. That is, they may not be split across multiple lines of source code with line continuation characters, since these interfere with the text substitution process.

    Comment


    • #3
      Do you mean like this? Same result.

      Code:
      #COMPILE EXE
      
      ' This macro is short for simplicity of this error demo code,
      ' In real world code, the condition is big enough to require
      ' line continuations.
      MACRO MACRO_TEST(i) = (1 = 1) OR _
      
      FUNCTION PBMAIN AS LONG
      
      ' CONDITIONS WITH LINE CONTINUATION WORK AS EXPECTED.
      IF (1 = 1) OR _
      (2 = 2) THEN
      MSGBOX "THIS IS THE FIRST ELEM"
      END IF
      
      ' WHEN IN A MACRO, IT BREAKS IF/END IF CONTINUITY.
      IF MACRO_TEST(10) (2 = 2) THEN
      MSGBOX("THIS IS THE FIRST ELEM")
      END IF
      
      END FUNCTION
      I am not plitting the lines in the macro, I am using the underscore to split them later.

      Perhaps what the help file meant was... not using underscore at all.
      www.pluribasic.com

      Comment


      • #4
        The line continuation in the MACRO definition is the problem

        Stuart gave you the quotes from Help saying so and the reason.
        Code:
        #compile exe
        #dim all
        
        
        ' This macro is short and dumb for simplicity of this error demo code,
        ' In real world code, the condition makes sense and it is big enough to require
        ' line continuations.
        macro MACRO_TEST(i) = (1 = 1) or (2 = 2) 'the underscore after OR was problem
        
        function pbmain as long
        
            ' NORMALLY, CONDITIONS WITH LINE CONTINUATION WORK AS EXPECTED.
            if (1 = 1) or _
               (2 = 2) then
               msgbox "THE REQUIREMENTS ARE FILLED. 1"
            end if
        
            ' WHEN IN A MACRO, IT BREAKS IF/END IF CONTINUITY. <=failed BY DESIGN see Help,
            if MACRO_TEST(10) then                           '    now works.
                msgbox "THE REQUIREMENTS ARE FILLED. 2"
            end if
        
        end function
        It does not matter when (before compile time) the "_" is added! A MACRO line can't be continued.
        Dale

        Comment


        • #5
          Fwiw
          error 594 - macro ends with continuation '_'- macro body text may not end with an underscore continuation character.
          However, the compiler does not report the error, it reports some other error. When I tried it, it reported :
          Error 457 in C:\PBWin10\Programs\ame.bas(85324:000): END SELECT expected
          Rod
          "To every unsung hero in the universe
          To those who roam the skies and those who roam the earth
          To all good men of reason may they never thirst " - from "Heaven Help the Devil" by G. Lightfoot

          Comment


          • #6
            Instead of MACRO, how about NewVar = long condition
            Then -
            IF NewVar blah, blah

            would make the IF line more readable.

            Cheers,
            Dale

            Comment


            • #7
              Originally posted by Brian Alvarez View Post
              Do you mean like this? Same result.

              I am not plitting the lines in the macro, I am using the underscore to split them later.

              Perhaps what the help file meant was... not using underscore at all.
              How do you expect the compiler to know whether the line continuation is intended to "split the lines in the macro" or to "split them later"?

              Comment


              • #8
                Originally posted by Rodney Hicks View Post
                Fwiw

                However, the compiler does not report the error, it reports some other error. When I tried it, it reported :
                Error 457 in C:\PBWin10\Programs\ame.bas(85324:000): END SELECT expected
                Not unexpected. The compiler is not trying to compile the macro, so it can't report an error there. It reports an error where it tries to substitute the text and then compile the ill formed result

                > error 594 - macro ends with continuation '_'- macro body text may not end with an underscore continuation character.
                That's a different error. In this case the "body text" doesn't end with an underscore, the definition line does.

                Comment


                • #9
                  That's a different error. In this case the "body text" doesn't end with an underscore, the definition line does.
                  No, it is not. Check the macro in post #3.
                  This is with the understanding that 'body text' refers to text to the right of the "=" in a single line macro.

                  Not unexpected. The compiler is not trying to compile the macro, so it can't report an error there. It reports an error where it tries to substitute the text and then compile the ill formed result
                  I don't think so. I affixed an underscore to a macro as so:
                  Code:
                  MACRO PI =ATN(1)*4 _
                  In the test program, that macro is used at least 70 times.
                  And the error is reported at line 85324 when there are only 2299 lines in the program. As well, the cursor was not placed in the editor.

                  And in the docs on MACRO/END MACRO, immediately after what was quoted in post # 2 is the following:
                  A Macro also cannot end with a line continuation character.
                  The error I quoted previously is the only error about underscores in a macro but there appears to be no way to report the error as it is not reporting the error when it tries to implement/substitute the macro as was suggested earlier in post 8.

                  I recall this same sort of discussion about 10 years ago, where Bob Zale had something to say, but I can't find it.
                  .
                  Rod
                  "To every unsung hero in the universe
                  To those who roam the skies and those who roam the earth
                  To all good men of reason may they never thirst " - from "Heaven Help the Devil" by G. Lightfoot

                  Comment


                  • #10

                    A Macro also cannot end with a line continuation character.
                    Well, technically my first macro doesnt end with a continuation character. It continued in the following row and ended with a bracket. I think the correct help should be:
                    A single line macro cannot contain line continuation characters, it must be as the name states, a single line.
                    Also, i think whatever thing that is not reported correctly is a glitch. If a macro cannot contain continuation of lines, it should complain about that when it finds one, instead of breaking block continuity. It should be handled more graciously.
                    www.pluribasic.com

                    Comment


                    • #11
                      Also, i think whatever thing that is not reported correctly is a glitch. If a macro cannot contain continuation of lines, it should complain about that when it finds one, instead of breaking block continuity. It should be handled more graciously.
                      You're right about that, but I think Stuart, in post #8, is close, if not bang on, to why it doesn't report the error properly.
                      Your first macro breaks this condition:
                      The first line of a MACRO definition is termed the macro prototype, and this line may not be split into multiple logical lines with the underscore (_) line continuation character.
                      Rod
                      "To every unsung hero in the universe
                      To those who roam the skies and those who roam the earth
                      To all good men of reason may they never thirst " - from "Heaven Help the Devil" by G. Lightfoot

                      Comment

                      Working...
                      X