vsariola / pakettic

TIC-80 cartridge packer
MIT License
19 stars 4 forks source link

Evaluate binary operators #10

Closed koorogi closed 1 year ago

koorogi commented 1 year ago

This PR allows pakettic to evaluate binary operators where both operands are numbers, and where the result of the evaluation is an integer.

I've found this useful because I'm using the C preprocessor together with my TIC-80 code. This lets me use macros as a sort of "inline function". If I use literal numbers as arguments to the macros, often times the inlined code has sub-expressions where all operands are numbers, and the whole thing become smaller if we evaluate those sub-expressions at packing time.

vsariola commented 1 year ago

Cool! One minor thing: I think the rounding rules of Python are different from LUA; e.g. //a with positive a rounds towards negative infinity, and with negative a it rounds towards positive infinity. I'll try to figure out if there's a way to make these mutations testable, and make unit tests for them, to make sure we don't get this one wrong.

But overall I like it and will merge it once I have tested it & convinced myself no bad things will happen.

If there's a risk of these evaluations changing the math somehow, we can make a command line toggles to turn off/turn on individual mutations. I think it should be fine, but with floating point math you never know.

vsariola commented 1 year ago

Never mind, it's easy fix: the Python // operator works identical to LUA's