Zezombye / overpy

High-level language for the Overwatch Workshop with support for compilation and decompilation.
GNU General Public License v3.0
180 stars 28 forks source link

Incorrect code generation for mapped and filtered arrays #302

Open Iluvatar opened 2 years ago

Iluvatar commented 2 years ago

When using list comprehensions that are both mapping and filtering, the generated code first does one then the other, leading to inconsistencies.

Example:

A = [2, 2, 4, 4]
T = [i for e, i in A if e == 4]

You'd expect T to be [2, 3]. However, it compiles to

Set Global Variable(T, Mapped Array(Filtered Array(Global.A, Compare(Current Array Element, ==, 4)), Current Array Index));

which first filters, then maps, leading to [2, 2, 4, 4] => [4, 4] => T = [0, 1].

It seems the workshop doesn't allow mapping and filtering at the same time. A rather heavy handed solution might be to first map elements to [elem, index], then filter, then map back out. Or just include a warning or something.

Zezombye commented 2 years ago

Indeed, that's a corner case that I didn't realize occurred when they added the current array index. I think I'll add a warning, though I've pretty much stopped working on overpy until I see what they're doing with the workshop in ow2. Thanks for reporting :)