nurpax / c64jasm

C64 6502 assembler in TypeScript
51 stars 14 forks source link

[improvement] Support for backward for loop like in python #70

Open shazz opened 4 years ago

shazz commented 4 years ago

As a workaround, a js plugin works well but as you implemented the python style for loop style using range(), could be nice to also support backwards loop like

for i in range(10, 0, -1)

Here is my javascript plugin:

module.exports = {
    reverse: ({}, val) => {
        return val.reverse();
    }
}

ex:

!for i in syn.reverse(range(SPRITES_NB)) {
...     
nurpax commented 3 years ago

BTW, the python range is IMO a bit funky:

>>> list(range(10, 0, -1))
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

This is not the same as just looping the list in reverse:

>>> list(reversed(range(0, 10)))
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

Looks like a great source of off-by-one errors.

Alternative would be to add a built-in reversed() function like in Python. This would be the same as your plugin, except built-in.

Do we want to pollute the symbol namespace with many functions like this? Or maybe something like:

!for i in range(10).reverse() {
}

This type of array prototype calling convention is not currently supportd in c64jasm.

PS:

module.exports = {
    reverse: ({}, val) => {
        return val.reverse();
    }
}

Whoops! JavaScript reverse() is in-place. You should not mutate the plugin input arguments. (I should document this!)