Page 1 of 1

compile error using a macro name

Posted: Thu Sep 14, 2017 10:51 am
by Jerry Messina
I can't believe I haven't run across this before, but it appears that if you happen to use a macro name inside a quoted text string the compiler generates an error.

Code: Select all

macro test(b)
    if (b = 0) then
        PORTD.3 = 0
    endif
end macro

// using a macro name inside a quoted string generates 
// "Error: Unable to locate a macro with these number of arguments" or
// "Error: Invalid expression",  depending on the macro

// all of these fail
const cstr = "this is a test"

dim s as string
s = "test"
s = "this is a test"
I use macros all over the place but it seems I haven't run afoul of this until now!

Re: compile error using a macro name

Posted: Sun Sep 17, 2017 8:59 am
by David Barker
Wow, that is a good one - I'll try and take a look when I get some free time...

Re: compile error using a macro name

Posted: Sun Sep 17, 2017 10:27 am
by Jerry Messina
Thanks. That one can be a hard one to figure out what's actually wrong.

Perhaps on a related note (?) there also seems to be some sort of scoping/visibility issue w/macros when you have
a sub and a macro with the same name... the macro seems to override other definitions.

file1.bas:

Code: Select all

module file1

public macro test(b)
    if (b = 0) then
        PORTD.3 = 0
    endif
end macro

end module
file2.bas:

Code: Select all

module file2

// just including the file w/the macro blows the definition of sub 'test' here away,
// like the macro name has "super scope"
include "file1.bas"     // contains macro 'test'

public sub test(b as byte)
    dim i as byte
    
    i = 0
    i = b + 1
end sub

end module
main program:

Code: Select all

include "file1.bas"     // contains macro 'test'
include "file2.bas"     // contains sub 'test'

// macro ok
file1.test(0)

// should get sub test(), but get macro file1.test instead
file2.test(0)
Once you know it's happening this one's easy enough to code around (just rename the macro), but you don't get any indication it's happening. Just figured I'd mention it.