keith-packard / snek

Snek programming language for tiny systems
GNU General Public License v3.0
292 stars 30 forks source link

Correct stack usage during chained comparison operations #51

Closed keith-packard closed 2 years ago

keith-packard commented 2 years ago

Chained comparison operators (a < b < c) use a "short-circuit" mechanism much like the boolean operators 'and' and 'or'. This requires some fancy hacks in snek and there were a few mistakes:

  1. The left-hand operand ('a') was left on the stack

  2. The second operator (b < c) would set the 'push' bit, which is on the first comparison operator so that 'b' would be on the stack. If the first comparison was false, that push wouldn't be appropriate. So, we reset the 'push' flag when branching.

  3. We need an instruction to land on after branching that can get the 'push' bit set in case the following operations need it. So we insert a 'nop' there, just like the short-circuited 'and'/'or' does.

Also add some tests which detect these mistakes.

Signed-off-by: Keith Packard keithp@keithp.com

Closes #50