keith-packard / snek

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

del a[:], del a[3:], del [:3], i.e. del with slices doesn't work #45

Open mobluse opened 2 years ago

mobluse commented 2 years ago

del with slices doesn't work. E.g.

Welcome to Snek version 1.7
> a=['a', 'b', 'c', 'd', 'e', 'f']
> a[:]
['a', 'b', 'c', 'd', 'e', 'f']
> del a[:]
<stdin>:4 Syntax error at "\n".
+ 
> a[:]
['a', 'b', 'c', 'd', 'e', 'f']
> del a[3:]
<stdin>:7 Syntax error at "\n".
+ 
> del a[:3]
<stdin>:9 Syntax error at "\n".
+ 
> a[:]
['a', 'b', 'c', 'd', 'e', 'f']

E.g. MicroPython handles this:

MicroPython v1.15-64-g1e2f0d280 on 2021-06-30; micro:bit v2.0.0 with nRF52833
Type "help()" for more information.
>>> a=['a', 'b', 'c', 'd', 'e', 'f']
>>> a[:]
['a', 'b', 'c', 'd', 'e', 'f']
>>> del a[:]
>>> a[:]
[]
>>> a=['a', 'b', 'c', 'd', 'e', 'f']
>>> del a[3:]
>>> a[:]
['a', 'b', 'c']
>>> a=['a', 'b', 'c', 'd', 'e', 'f']
>>> del a[:3]
>>> a[:]
['d', 'e', 'f']
>>> a
['d', 'e', 'f']

Maybe it would take up too much memory in Snek to handle slices with del, but if some code can be reused for this purpose without adding memory it would be good to have. At least, I think, it should be documented that slices don't work with del since they seem to work everywhere else in Snek.

keith-packard commented 2 years ago

Thanks! I didn't realize that they should work. I can add some code for at least some platforms :-)