keith-packard / snek

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

If a and b are strings then a+(3*b) gives "<stdin>:4 type mismatch" #53

Closed mobluse closed 2 years ago

mobluse commented 2 years ago

This can be pasted into Snek or other pythons to demonstrate the difference:

a="mo"
b="ha"
a+(b*3)
a+(3*b)
 pi  ~  snek
Welcome to Snek version 1.7
> a="mo"
> b="ha"
> a+(b*3)
'mohahaha'
> a+(3*b)
<stdin>:4 type mismatch: 3 'ha'
> 
 pi  ~  micropython 
MicroPython v1.17-333-gcf258c898-dirty on 2022-01-14; linux version
Use Ctrl-D to exit, Ctrl-E for paste mode
>>> a="mo"
>>> b="ha"
>>> a+(b*3)
'mohahaha'
>>> a+(3*b)
'mohahaha'
>>> 
 pi  ~  python3
Python 3.7.3 (default, Jan 22 2021, 20:04:44) 
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> a="mo"
>>> b="ha"
>>> a+(b*3)
'mohahaha'
>>> a+(3*b)
'mohahaha'
>>> 
 pi  ~  python2
Python 2.7.16 (default, Oct 10 2019, 22:02:15) 
[GCC 8.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> a="mo"
>>> b="ha"
>>> a+(b*3)
'mohahaha'
>>> a+(3*b)
'mohahaha'
>>> 

Maybe to fix this requires a lot of code so that it doesn't fit into Arduino UNO.

keith-packard commented 2 years ago

Just required allowing the operands to be provided in either order. Yes, fitting this onto an UNO was a bit tricky, but I found some savings elsewhere :-)