nesdev.com
http://forums.nesdev.com/

Odd behavior with operator precedence for ! in ca65 macros.
http://forums.nesdev.com/viewtopic.php?f=2&t=16307
Page 1 of 1

Author:  GradualGames [ Sat Aug 05, 2017 5:26 pm ]
Post subject:  Odd behavior with operator precedence for ! in ca65 macros.

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:
.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.

Author:  GradualGames [ Sat Aug 05, 2017 7:15 pm ]
Post subject:  Re: How to use the .blank function in ca65.

ca65 appears to be okay with

Code:
    .if ((.blank(rega)) || (.blank(regb)))


but not if I use boolean not operators:

Code:
    .if (!(.blank(rega)) || !(.blank(regb)))

Author:  GradualGames [ Sat Aug 05, 2017 7:16 pm ]
Post subject:  Re: How to use the .blank function in ca65.

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

Code:
    .if ((!(.blank(rega))) || (!(.blank(regb))))

Author:  tokumaru [ Sat Aug 05, 2017 7:20 pm ]
Post subject:  Re: How to use the .blank function in ca65.

This also works, weirdly:
Code:
.if !(.blank({rega}) && .blank({regb}))

Author:  thefox [ Sat Aug 05, 2017 11:01 pm ]
Post subject:  Re: How to use the .blank function in ca65.

The bug/feature seems not to be related to .blank, this also fails:
Code:
foo = 1
bar = 1
xyzzy = !foo || !bar
; Equivalent to: .not foo .or .not bar

This works, though:
Code:
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

Author:  thefox [ Sun Aug 06, 2017 6:53 am ]
Post subject:  Re: How to use the .blank function in ca65.

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)

Author:  tepples [ Sun Aug 06, 2017 8:03 am ]
Post subject:  Re: How to use the .blank function in ca65.

! 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).

Author:  tokumaru [ Sun Aug 06, 2017 10:18 am ]
Post subject:  Re: How to use the .blank function in ca65.

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.

Author:  thefox [ Sun Aug 06, 2017 10:27 am ]
Post subject:  Re: How to use the .blank function in ca65.

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...

Page 1 of 1 All times are UTC - 7 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/