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 theList
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.
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 2Which 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()
andstring()
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 2Which 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()
andstring()
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.
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 likex = [1,2,3]
work built-in, also with e.g.len(x)
then, which with theList
class hes to be done withx.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:
@maxgerhardt there is a typo about the macro and now fixed. f6f38bd