pikasTech/PikaPython

Support built-in lists and dicts

Closed this issue · 10 comments

Same setup as in #96

>>> myList = [1,2,3]
NameError: name '[1,2,__get__' is not defined
   NUM 3                (#1)
 -> RUN [1,2,__get__            (#3)
>>> myList = ["123"]
NameError: name '[__get__' is not defined
   STR 123              (#1)
 -> RUN [__get__                (#5)

Really weird error messages too.

Hm I get the feeling this is because I don't import the PikaStdData.py which declares the List class? But if the shell is running, how do I import this file? I can't even define a function due to syntax errors.

>>> class GPIO(TinyObj):
...    def __init__(self):
...       pass
...    def append(self, arg: any):
...       pass
... 
[error]: Syntax error.

import in runtime is not supported because there is no file system.

Import is done by preCompiler. Just import it in main.py and after running the preCompiler, you can use it in the shell.

Hm I get the feeling this is because I don't import the PikaStdData.py which declares the List class? But if the shell is running, how do I import this file? I can't even define a function due to syntax errors.

>>> class GPIO(TinyObj):
...    def __init__(self):
...       pass
...    def append(self, arg: any):
...       pass
... 
[error]: Syntax error.

Syntax error because of 4 spaces, the same as #96.

And also,
Class and function definition is only supported in main.py, and the feature of shell is added to the readme
https://github.com/pikastech/pikascript/#3syntax

The List() is only supplied by the PikaStdData.List() class.
And the document is here.

https://pikadoc.readthedocs.io/en/latest/PikaStdData%20%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84.html#class-list

Right so after maxgerhardt/pikascript-pio-bluepill@82c7e4b, I can do

>>> x = PikaStdData.List()
>>> x.len()
0
>>> x.append(1)
>>> x.append(2)
>>> x
>>>
>>> for elem in x:
...     print(elem)
...
1
2

Which works well here (thanks!), but importing it into the global namespace seems to not work :/

>>> from PikaStdData import List
>>> x = List()
NameError: name 'List' is not defined
 -> RUN List            (#1)
>>> from PikaStdData import *
>>> x = List()
NameError: name 'List' is not defined
 -> RUN List            (#1)

And shouldn't list(), dict(), bytearray() and string() be a built-in type and always there? Even after import, native lists still throw the weird error

>>> x = [1,2,3]
NameError: name '[1,2,__get__' is not defined
   NUM 3                (#1)
 -> RUN [1,2,__get__            (#3)

Right so after maxgerhardt/pikascript-pio-bluepill@82c7e4b, I can do

>>> x = PikaStdData.List()
>>> x.len()
0
>>> x.append(1)
>>> x.append(2)
>>> x
>>>
>>> for elem in x:
...     print(elem)
...
1
2

Which works well here (thanks!), but importing it into the global namespace seems to not work :/

>>> from PikaStdData import List
>>> x = List()
NameError: name 'List' is not defined
 -> RUN List            (#1)
>>> from PikaStdData import *
>>> x = List()
NameError: name 'List' is not defined
 -> RUN List            (#1)

And shouldn't list(), dict(), bytearray() and string() be a built-in type and always there? Even after import, native lists still throw the weird error

>>> x = [1,2,3]
NameError: name '[1,2,__get__' is not defined
   NUM 3                (#1)
 -> RUN [1,2,__get__            (#3)

Right so after maxgerhardt/pikascript-pio-bluepill@82c7e4b, I can do

>>> x = PikaStdData.List()
>>> x.len()
0
>>> x.append(1)
>>> x.append(2)
>>> x
>>>
>>> for elem in x:
...     print(elem)
...
1
2

Which works well here (thanks!), but importing it into the global namespace seems to not work :/

>>> from PikaStdData import List
>>> x = List()
NameError: name 'List' is not defined
 -> RUN List            (#1)
>>> from PikaStdData import *
>>> x = List()
NameError: name 'List' is not defined
 -> RUN List            (#1)

And shouldn't list(), dict(), bytearray() and string() be a built-in type and always there? Even after import, native lists still throw the weird error

>>> x = [1,2,3]
NameError: name '[1,2,__get__' is not defined
   NUM 3                (#1)
 -> RUN [1,2,__get__            (#3)

This is for trimmability reasons, in some applications list() and dict() are not used, then not introducing them can help reduce the firmware size.

And from *import* is not supported now. Maybe it will be available in the future.

https://pikadoc.readthedocs.io/en/latest/Pikascript%20%E6%8B%93%E5%B1%95%E6%A8%A1%E5%9D%97%E5%BC%80%E5%8F%91%E6%B5%81%E7%A8%8B.html#new-module-interface

Maybe the core should have a #define / macro option to enable built in lists, dictionaries, etc. as a selectable featureset. I think it's with regards to Python programming that things like x = [1,2,3] work built-in, also with e.g. len(x) then, which with the List class hes to be done with x.len().

Maybe the core should have a #define / macro option to enable built in lists, dictionaries, etc. as a selectable featureset. I think it's with regards to Python programming that things like x = [1,2,3] work built-in, also with e.g. len(x) then, which with the List class hes to be done with x.len().

Okay, the build-in mode for list() dict() shoud be a TODO issue.

Now (f6f38bd) you can use the PIKA_BUILTIN_LIST_ENABLE and PIKA_BUILTIN_DICT_ENABLE to enable built-in list and dict.
And now you can do that with list:

>>> x = [1,2,3]
>>> for item in x:
...     item
... 
1
2
3
>>> 

For dict, you can only use x = dict() to create a dict.

About how to config pikascript:

https://pikadoc.readthedocs.io/en/latest/%E4%BC%98%E5%8C%96%E5%86%85%E5%AD%98%E5%8D%A0%E7%94%A8%E3%80%81%E9%85%8D%E7%BD%AE%20libc.html

@maxgerhardt there is a typo about the macro and now fixed. f6f38bd