Jump to content
IGNORED

MACROS


Recommended Posts

According to the documentation...

DEF FN func = RAND % 10
  DEF FN screen_off(row, col) = (row * 20 + col)
  DEF FN resetsprite(number) = SPRITE number,0

    Allows to define functions with any number of arguments.

    Notice that these are processed like macros: The arguments are replaced
    as-is and the text of the function is inserted as-is after argument
    replacement. (because of that are included the extra parenthesis in the
    screen_off expression)

    For example, the two functions screen_off and resetsprite could be called as:

          A = func
          PRINT AT screen_off(4,8),"HELLO!"
          resetsprite(0)

    Please note that although it's possible to reuse internal function names,
    when called the internal functions have priority over DEF FN (DEF FN is
    ignored)

It does not described what type the arguments can be.

 

For instance, I would like to pass an array.  I presume this is not supported since I get an error when calling the MACRO when compiling the code.

 

Link to comment
Share on other sites

14 minutes ago, SpaceHunter said:

According to the documentation...


DEF FN func = RAND % 10
  DEF FN screen_off(row, col) = (row * 20 + col)
  DEF FN resetsprite(number) = SPRITE number,0

    Allows to define functions with any number of arguments.

    Notice that these are processed like macros: The arguments are replaced
    as-is and the text of the function is inserted as-is after argument
    replacement. (because of that are included the extra parenthesis in the
    screen_off expression)

    For example, the two functions screen_off and resetsprite could be called as:

          A = func
          PRINT AT screen_off(4,8),"HELLO!"
          resetsprite(0)

    Please note that although it's possible to reuse internal function names,
    when called the internal functions have priority over DEF FN (DEF FN is
    ignored)

It does not described what type the arguments can be.

 

For instance, I would like to pass an array.  I presume this is not supported since I get an error when calling the MACRO when compiling the code.

 

The arguments can be anything at all -- any IntyBASIC expression.  The compiler takes the arguments, as is, and interpolates them in-line into the macro.  Then the entire thing -- the statement with the composed macro -- is evaluated as any other statement.


If the array argument is causing an error, it could be that an array is not valid in the resulting expression.  Perhaps a syntax error.

 

Can you offer an example of what you are trying?

 

    dZ.

Edited by DZ-Jay
Link to comment
Share on other sites

I have a simple sort macro...

DEF FN swap(arr, xp, yp) = \
	temp = arr(xp)\
    arr(xp) = arr(yp)\
    arr(yp) = temp
 
DEF FN sort(arr, n) = \
	FOR i = 0 TO n - 1\
      FOR j = 0 TO n - i - 1\
        IF arr(j) > arr(j+1) THEN\
	        swap(arr, j, j+1)\
        END IF\
	  NEXT j\
	NEXT i

This compiles fine, no errors or warnings.  However, when I call it with an array with integers...

sort(myArray, length)

' ...rest of code

I get a strange error complaining about the following statement thereafter, after the macro has been invoked.

 

If I comment out the sort call, I'm back with a clean compilation.

 

Link to comment
Share on other sites

31 minutes ago, SpaceHunter said:

I have a simple sort macro...


DEF FN swap(arr, xp, yp) = \
	temp = arr(xp)\
    arr(xp) = arr(yp)\
    arr(yp) = temp
 
DEF FN sort(arr, n) = \
	FOR i = 0 TO n - 1\
      FOR j = 0 TO n - i - 1\
        IF arr(j) > arr(j+1) THEN\
	        swap(arr, j, j+1)\
        END IF\
	  NEXT j\
	NEXT i

This compiles fine, no errors or warnings.  However, when I call it with an array with integers...


sort(myArray, length)

' ...rest of code

I get a strange error complaining about the following statement thereafter, after the macro has been invoked.

 

If I comment out the sort call, I'm back with a clean compilation.

 

Your problem is that you are using "\" to concatenate all statements together.  It's like if you've written:

FOR i = 0 TO n - 1 FOR j = 0 TO n - i - 1 IF arr(j) > arr(j+1) THEN ...

which is obviously not valid syntax.

 

Since the macro forces you to define everything on a single line, you need ":" in between statements to separate the commands.

 

   dZ.

  • Like 1
Link to comment
Share on other sites

1 hour ago, DZ-Jay said:

which is obviously not valid syntax.

Sure would be nice to have the compiler flag it then.

 

Quote

Since the macro forces you to define everything on a single line, you need ":" in between statements to separate the commands.

Ah yes, I forgot the colons, however, you can define a macro on multiple lines using backslash.

Edited by SpaceHunter
Link to comment
Share on other sites

51 minutes ago, SpaceHunter said:

Sure would be nice to have the compiler flag it then.

 

Ah yes, I forgot the colons, however, you can define a macro on multiple lines using backslash.

Yes, but all the backslash does is escape the newline -- that is, it ignores the carriage-return and concatenates the two lines.  It does not separate the statements.  In a macro, you only use the newlines to format your commands as multiple lines, for legibility.  You still need the colons in between separate statements.

 

Put another way:  A macro supports exactly one line of code.  That line of code may contain multiple statements separated by colons.  That line of code may further be formatted into multiple lines, for the sake of legibility, using the backslash.

 

     -dZ.

Edited by DZ-Jay
  • Like 1
  • Thanks 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...