compile error using a macro name

Coding and general discussion relating to the compiler

Moderators: David Barker, Jerry Messina

Post Reply
Jerry Messina
Swordfish Developer
Posts: 1469
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

compile error using a macro name

Post by Jerry Messina » Thu Sep 14, 2017 10:51 am

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!

User avatar
David Barker
Swordfish Developer
Posts: 1214
Joined: Tue Oct 03, 2006 7:01 pm
Location: Saltburn by the Sea, UK
Contact:

Re: compile error using a macro name

Post by David Barker » Sun Sep 17, 2017 8:59 am

Wow, that is a good one - I'll try and take a look when I get some free time...

Jerry Messina
Swordfish Developer
Posts: 1469
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: compile error using a macro name

Post by Jerry Messina » Sun Sep 17, 2017 10:27 am

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.

Post Reply