This code:

x+=x<0 and -100 or 100 |

doesn't work the way I'd expect; it sets x to either -100 or 100

I assume this happens because that line gets preprocessed to this:

x=x+x<0 and -100 or 100 |

which gets interpreted like this:

x=(x+x<0) and -100 or 100 |

but I wish it would be preprocessed to this instead:

x=x+(x<0 and -100 or 100) |

Here's a test cart; it currently (0.2.2c) fails the tests:

You can use a feature of the parser to find out what the final/unrolled code is:

> print([=[x+=x<0 and -100 or 100]=]) x = x + (x) <0 and -100 or 100 |

And yeah, this clearly doesn't unroll correctly, since it obviously needs to be this:

x = x + (x<0 and -100 or 100) |

It looks like the regex that @zep uses to search/replace the expression on the right side of the assignment is simply missing two of the comparison operators: "<" and ">".

Surprisingly, it *does* work correctly with other comparison operators:

> print([=[x+=x<=0 and -100 or 100]=]) x = x + (x<=0 and -100 or 100) > print([=[x+=x==0 and -100 or 100]=]) x = x + (x==0 and -100 or 100) > print([=[x+=x>=0 and -100 or 100]=]) x = x + (x>=0 and -100 or 100) |

**So yes, there's a bug here.**

As a workaround, you could enclose the expression manually:

> print([=[x+=(x<0 and -100 or 100)]=]) x = x + ((x<0 and -100 or 100 )) |

You could also flip the condition to use one of the working operators:

> print([=[x+=x>=0 and 100 or -100]=]) x = x + (x>=0 and 100 or -100) |

Or you could use the built-in **sgn(x)** function, which also compares x<0 and returns -1 or 1:

x=sgn(x)*100 |

(Note: Unlike C/C++'s **sign(x)**, the PICO-8 **sgn(x)** does *not* return 0 when x==0. 0 is considered positive, so it returns 1.)

@StinkerB06

Ah, someone else told me. ~~I forget, might have been Fred or maybe thisismypassword, maybe someone else. (I has swiss-cheese memory; I apologize to whomever deserves credit!)~~ Edit: It was sam!

[Please log in to post a comment]