Odd behavior with operator precedence for ! in ca65 macros.

Discuss technical or other issues relating to programming the Nintendo Entertainment System, Famicom, or compatible systems. See the NESdev wiki for more information.

Moderator: Moderators

Post Reply
User avatar
GradualGames
Posts: 1106
Joined: Sun Nov 09, 2008 9:18 pm
Location: Pennsylvania, USA
Contact:

Odd behavior with operator precedence for ! in ca65 macros.

Post by GradualGames »

I'm attempting to use .blank instead of .ifnblank, so I can use it in an || expression to test if one or another macro argument is present:

Code: Select all

.macro my_macro source, dest, rega, regb

    .if (!.blank({rega}) || !.blank({regb}))

    ...
It doesn't seem to like this. I've attempted to use .blank in the past unsuccessfully; not sure what I'm doing wrong. So far I've been able to work around this by using .ifnblank which works right away for me. I can just repeat code and use .ifnblank twice to simulate the || expression but it'd be nicer if I could get this to work.
Last edited by GradualGames on Sun Aug 06, 2017 8:07 am, edited 1 time in total.
User avatar
GradualGames
Posts: 1106
Joined: Sun Nov 09, 2008 9:18 pm
Location: Pennsylvania, USA
Contact:

Re: How to use the .blank function in ca65.

Post by GradualGames »

ca65 appears to be okay with

Code: Select all

    .if ((.blank(rega)) || (.blank(regb)))
but not if I use boolean not operators:

Code: Select all

    .if (!(.blank(rega)) || !(.blank(regb)))
User avatar
GradualGames
Posts: 1106
Joined: Sun Nov 09, 2008 9:18 pm
Location: Pennsylvania, USA
Contact:

Re: How to use the .blank function in ca65.

Post by GradualGames »

But apparently if you use enough parentheses it's happy?

Code: Select all

    .if ((!(.blank(rega))) || (!(.blank(regb))))
User avatar
tokumaru
Posts: 12427
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: How to use the .blank function in ca65.

Post by tokumaru »

This also works, weirdly:

Code: Select all

.if !(.blank({rega}) && .blank({regb}))
User avatar
thefox
Posts: 3134
Joined: Mon Jan 03, 2005 10:36 am
Location: 🇫🇮
Contact:

Re: How to use the .blank function in ca65.

Post by thefox »

The bug/feature seems not to be related to .blank, this also fails:

Code: Select all

foo = 1
bar = 1
xyzzy = !foo || !bar
; Equivalent to: .not foo .or .not bar
This works, though:

Code: Select all

foo = 1
bar = 1
xyzzy = !foo || (!bar)
; Equivalent to: .not foo .or (.not bar)
I think in the first case it might be thinking that only the ".not" is the argument to ".or", and then gets confused when it's not a proper expression. Surprising to see such a trivial expression fail.

EDIT: I've reported this in the cc65 issue tracker: https://github.com/cc65/cc65/issues/474
Download STREEMERZ for NES from fauxgame.com! — Some other stuff I've done: fo.aspekt.fi
User avatar
thefox
Posts: 3134
Joined: Mon Jan 03, 2005 10:36 am
Location: 🇫🇮
Contact:

Re: How to use the .blank function in ca65.

Post by thefox »

Looks like this extremely unintuitive behavior is by design (explained in the issue).

Can't help but wonder why ! was given the lowest possible precedence. (Compare to, e.g., C, where ! and ~ have equal precedence.)

The moral of the story, I guess, is to always use parenthesis with ! and .not. Otherwise you end up with such fun behavior like !foo && bar being equal to !(foo && bar)
Download STREEMERZ for NES from fauxgame.com! — Some other stuff I've done: fo.aspekt.fi
tepples
Posts: 22708
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: How to use the .blank function in ca65.

Post by tepples »

! in ca65 is even looser than not in Python, where not is just tighter than and and or. But either one is a huge jump from ! in C++, which is as tight as all the other prefix unary operators (such as - ++ * & (int) sizeof).
User avatar
tokumaru
Posts: 12427
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: How to use the .blank function in ca65.

Post by tokumaru »

thefox wrote:Looks like this extremely unintuitive behavior is by design
That seems somewhat common for ca65. Just one more weirdness we have to watch out for, then.
User avatar
thefox
Posts: 3134
Joined: Mon Jan 03, 2005 10:36 am
Location: 🇫🇮
Contact:

Re: How to use the .blank function in ca65.

Post by thefox »

tokumaru wrote:
thefox wrote:Looks like this extremely unintuitive behavior is by design
That seems somewhat common for ca65. Just one more weirdness we have to watch out for, then.
Yeah, I can tolerate many of the quirks (more so the ones where I know there's a good bit of implementation complexity), but there are quite a few quirks now that really annoy me, mainly the ones which just seem like bad design decisions. This is one of them. Another is the one where zeropage variables may get absolute addressing depending on whether they are scoped or not. Macro variables not being local to the macro by default is a third one. I'm sure there were a few others that I can't remember right now as well.

Makes forking all that more tempting...
Download STREEMERZ for NES from fauxgame.com! — Some other stuff I've done: fo.aspekt.fi
Post Reply