diff --git a/port/cmsis-pack/PikaTech.PikaScript.pdsc b/port/cmsis-pack/PikaTech.PikaScript.pdsc index c72875277..0c7303f70 100644 --- a/port/cmsis-pack/PikaTech.PikaScript.pdsc +++ b/port/cmsis-pack/PikaTech.PikaScript.pdsc @@ -243,6 +243,7 @@ + diff --git a/port/cmsis-pack/pikascript/PikaObj.pyi b/port/cmsis-pack/pikascript/PikaObj.pyi index 9304eabcf..9a85df17b 100644 --- a/port/cmsis-pack/pikascript/PikaObj.pyi +++ b/port/cmsis-pack/pikascript/PikaObj.pyi @@ -14,6 +14,10 @@ class any: ... +class int64: + ... + + def printNoEnd(val: any): ... def abstractmethod(fn): ... def PIKA_C_MACRO_IF(fn): ... diff --git a/port/cmsis-pack/pikascript/PikaStdData.pyi b/port/cmsis-pack/pikascript/PikaStdData.pyi index fde1201ef..ab29840ef 100644 --- a/port/cmsis-pack/pikascript/PikaStdData.pyi +++ b/port/cmsis-pack/pikascript/PikaStdData.pyi @@ -20,6 +20,9 @@ class Tuple: def __str__(self) -> str: ... def __len__(self) -> int: ... + def __contains__(self, val: any) -> int: + """ support val in list """ + class List(Tuple): def __init__(self): ... @@ -59,8 +62,12 @@ class Dict: def __del__(self): ... def __str__(self) -> str: ... def keys(self) -> dict_keys: ... + def items(self) -> dict_items: ... def __len__(self) -> int: ... + def __contains__(self, val: any) -> int: + """ support val in list """ + class dict_keys: def __iter__(self) -> any: ... @@ -68,6 +75,11 @@ class dict_keys: def __str__(self) -> str: ... def __len__(self) -> int: ... +class dict_items: + def __iter__(self) -> any: ... + def __next__(self) -> any: ... + def __str__(self) -> str: ... + def __len__(self) -> int: ... class String: def __init__(self, s: str): ... @@ -82,7 +94,7 @@ class String: # support str() def __str__(self) -> str: ... def __len__(self) -> int: ... - def encode(self) -> bytes: ... + def encode(self, *encoding) -> bytes: ... def startswith(self, prefix: str) -> int: ... def endswith(self, suffix: str) -> int: ... def isdigit(self) -> int: ... diff --git a/port/cmsis-pack/pikascript/PikaStdLib.pyi b/port/cmsis-pack/pikascript/PikaStdLib.pyi index bf21965df..22b5bcb69 100644 --- a/port/cmsis-pack/pikascript/PikaStdLib.pyi +++ b/port/cmsis-pack/pikascript/PikaStdLib.pyi @@ -1,5 +1,6 @@ from PikaObj import * + class MemChecker: def max(self): ... def now(self): ... @@ -15,9 +16,6 @@ class MemChecker: class SysObj: - @staticmethod - def type(arg: any) -> any: ... - @staticmethod def int(arg: any) -> int: ... @@ -31,14 +29,10 @@ class SysObj: def iter(arg: any) -> any: ... @staticmethod - def range(a1: int, a2: int) -> any: ... + def range(*ax) -> any: ... @staticmethod - def print(*val): ... - - @staticmethod - @PIKA_C_MACRO_IF("!PIKA_NANO_ENABLE") - def printNoEnd(val: any): ... + def print(*val, **ops): ... @staticmethod def __setitem__(obj: any, key: any, val: any) -> any: ... @@ -46,6 +40,10 @@ class SysObj: @staticmethod def __getitem__(obj: any, key: any) -> any: ... + @staticmethod + @PIKA_C_MACRO_IF("!PIKA_NANO_ENABLE") + def type(arg: any) -> any: ... + @staticmethod @PIKA_C_MACRO_IF("PIKA_BUILTIN_STRUCT_ENABLE") def __slice__(obj: any, start: any, end: any, step: int) -> any: ... @@ -105,6 +103,10 @@ class SysObj: @PIKA_C_MACRO_IF("!PIKA_NANO_ENABLE") def setattr(obj: object, name: str, val: any): ... + @staticmethod + @PIKA_C_MACRO_IF("!PIKA_NANO_ENABLE") + def exit(): ... + @PIKA_C_MACRO_IF("0") class RangeObj: @@ -114,4 +116,3 @@ class RangeObj: @PIKA_C_MACRO_IF("0") class StringObj: def __next__(self) -> any: ... - diff --git a/port/cmsis-pack/pikascript/PikaTech.PikaScript.pdsc b/port/cmsis-pack/pikascript/PikaTech.PikaScript.pdsc index c72875277..0c7303f70 100644 --- a/port/cmsis-pack/pikascript/PikaTech.PikaScript.pdsc +++ b/port/cmsis-pack/pikascript/PikaTech.PikaScript.pdsc @@ -243,6 +243,7 @@ + diff --git a/port/cmsis-pack/pikascript/clean.bat b/port/cmsis-pack/pikascript/clean.bat index 447f4368c..e09e4dd4c 100644 --- a/port/cmsis-pack/pikascript/clean.bat +++ b/port/cmsis-pack/pikascript/clean.bat @@ -8,6 +8,7 @@ @del "pikascript-api\PikaStdData.h" @del "pikascript-api\PikaStdData_ByteArray.h" @del "pikascript-api\PikaStdData_Dict.h" +@del "pikascript-api\PikaStdData_dict_items.h" @del "pikascript-api\PikaStdData_dict_keys.h" @del "pikascript-api\PikaStdData_FILEIO.h" @del "pikascript-api\PikaStdData_List.h" diff --git a/port/cmsis-pack/pikascript/pikascript-api/PikaStdData.h b/port/cmsis-pack/pikascript/pikascript-api/PikaStdData.h index 5339023b3..637de8e90 100644 --- a/port/cmsis-pack/pikascript/pikascript-api/PikaStdData.h +++ b/port/cmsis-pack/pikascript/pikascript-api/PikaStdData.h @@ -24,6 +24,7 @@ Arg* PikaStdData_List(PikaObj *self); Arg* PikaStdData_String(PikaObj *self); Arg* PikaStdData_Tuple(PikaObj *self); Arg* PikaStdData_Utils(PikaObj *self); +Arg* PikaStdData_dict_items(PikaObj *self); Arg* PikaStdData_dict_keys(PikaObj *self); #endif diff --git a/port/cmsis-pack/pikascript/pikascript-api/PikaStdData_Dict.h b/port/cmsis-pack/pikascript/pikascript-api/PikaStdData_Dict.h index 172cef596..943674602 100644 --- a/port/cmsis-pack/pikascript/pikascript-api/PikaStdData_Dict.h +++ b/port/cmsis-pack/pikascript/pikascript-api/PikaStdData_Dict.h @@ -17,6 +17,7 @@ PikaObj *New_PikaStdData_Dict(Args *args); +int PikaStdData_Dict___contains__(PikaObj *self, Arg* val); void PikaStdData_Dict___del__(PikaObj *self); Arg* PikaStdData_Dict___getitem__(PikaObj *self, Arg* __key); void PikaStdData_Dict___init__(PikaObj *self); @@ -26,6 +27,7 @@ Arg* PikaStdData_Dict___next__(PikaObj *self); void PikaStdData_Dict___setitem__(PikaObj *self, Arg* __key, Arg* __val); char* PikaStdData_Dict___str__(PikaObj *self); Arg* PikaStdData_Dict_get(PikaObj *self, char* key); +PikaObj* PikaStdData_Dict_items(PikaObj *self); PikaObj* PikaStdData_Dict_keys(PikaObj *self); void PikaStdData_Dict_remove(PikaObj *self, char* key); void PikaStdData_Dict_set(PikaObj *self, char* key, Arg* arg); diff --git a/port/cmsis-pack/pikascript/pikascript-api/PikaStdData_String.h b/port/cmsis-pack/pikascript/pikascript-api/PikaStdData_String.h index b0f1962f6..8f2bbffa7 100644 --- a/port/cmsis-pack/pikascript/pikascript-api/PikaStdData_String.h +++ b/port/cmsis-pack/pikascript/pikascript-api/PikaStdData_String.h @@ -24,7 +24,7 @@ int PikaStdData_String___len__(PikaObj *self); Arg* PikaStdData_String___next__(PikaObj *self); void PikaStdData_String___setitem__(PikaObj *self, Arg* __key, Arg* __val); char* PikaStdData_String___str__(PikaObj *self); -Arg* PikaStdData_String_encode(PikaObj *self); +Arg* PikaStdData_String_encode(PikaObj *self, PikaTuple* encoding); int PikaStdData_String_endswith(PikaObj *self, char* suffix); char* PikaStdData_String_get(PikaObj *self); int PikaStdData_String_isalnum(PikaObj *self); diff --git a/port/cmsis-pack/pikascript/pikascript-api/PikaStdData_Tuple.h b/port/cmsis-pack/pikascript/pikascript-api/PikaStdData_Tuple.h index c8097ac2a..c09be1620 100644 --- a/port/cmsis-pack/pikascript/pikascript-api/PikaStdData_Tuple.h +++ b/port/cmsis-pack/pikascript/pikascript-api/PikaStdData_Tuple.h @@ -17,6 +17,7 @@ PikaObj *New_PikaStdData_Tuple(Args *args); +int PikaStdData_Tuple___contains__(PikaObj *self, Arg* val); void PikaStdData_Tuple___del__(PikaObj *self); Arg* PikaStdData_Tuple___getitem__(PikaObj *self, Arg* __key); void PikaStdData_Tuple___init__(PikaObj *self); diff --git a/port/cmsis-pack/pikascript/pikascript-api/PikaStdData_dict_items.h b/port/cmsis-pack/pikascript/pikascript-api/PikaStdData_dict_items.h new file mode 100644 index 000000000..35640ad7b --- /dev/null +++ b/port/cmsis-pack/pikascript/pikascript-api/PikaStdData_dict_items.h @@ -0,0 +1,25 @@ +/* + * [Warning!] This file is auto-generated by pika compiler. + * Do not edit it manually. + * The source code is *.pyi file. + * More details: + * English Doc: + * https://pikadoc.readthedocs.io/en/latest/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html + * Chinese Doc: + * https://pikadoc.readthedocs.io/zh/latest/PikaScript%20%E6%A8%A1%E5%9D%97%E6%A6%82%E8%BF%B0.html + */ + +#ifndef __PikaStdData_dict_items__H +#define __PikaStdData_dict_items__H +#include +#include +#include "PikaObj.h" + +PikaObj *New_PikaStdData_dict_items(Args *args); + +Arg* PikaStdData_dict_items___iter__(PikaObj *self); +int PikaStdData_dict_items___len__(PikaObj *self); +Arg* PikaStdData_dict_items___next__(PikaObj *self); +char* PikaStdData_dict_items___str__(PikaObj *self); + +#endif diff --git a/port/cmsis-pack/pikascript/pikascript-api/PikaStdLib_SysObj.h b/port/cmsis-pack/pikascript/pikascript-api/PikaStdLib_SysObj.h index 04c13775c..288bb7433 100644 --- a/port/cmsis-pack/pikascript/pikascript-api/PikaStdLib_SysObj.h +++ b/port/cmsis-pack/pikascript/pikascript-api/PikaStdLib_SysObj.h @@ -26,6 +26,7 @@ char* PikaStdLib_SysObj_chr(PikaObj *self, int val); Arg* PikaStdLib_SysObj_dict(PikaObj *self); PikaObj* PikaStdLib_SysObj_dir(PikaObj *self, PikaObj* obj); void PikaStdLib_SysObj_exec(PikaObj *self, char* code); +void PikaStdLib_SysObj_exit(PikaObj *self); double PikaStdLib_SysObj_float(PikaObj *self, Arg* arg); Arg* PikaStdLib_SysObj_getattr(PikaObj *self, PikaObj* obj, char* name); char* PikaStdLib_SysObj_hex(PikaObj *self, int val); @@ -36,9 +37,8 @@ int PikaStdLib_SysObj_len(PikaObj *self, Arg* arg); Arg* PikaStdLib_SysObj_list(PikaObj *self); PikaObj* PikaStdLib_SysObj_open(PikaObj *self, char* path, char* mode); int PikaStdLib_SysObj_ord(PikaObj *self, char* val); -void PikaStdLib_SysObj_print(PikaObj *self, PikaTuple* val); -void PikaStdLib_SysObj_printNoEnd(PikaObj *self, Arg* val); -Arg* PikaStdLib_SysObj_range(PikaObj *self, int a1, int a2); +void PikaStdLib_SysObj_print(PikaObj *self, PikaTuple* val, PikaDict* ops); +Arg* PikaStdLib_SysObj_range(PikaObj *self, PikaTuple* ax); void PikaStdLib_SysObj_setattr(PikaObj *self, PikaObj* obj, char* name, Arg* val); char* PikaStdLib_SysObj_str(PikaObj *self, Arg* arg); Arg* PikaStdLib_SysObj_type(PikaObj *self, Arg* arg); diff --git a/port/cmsis-pack/pikascript/pikascript-api/__pikaBinding.c b/port/cmsis-pack/pikascript/pikascript-api/__pikaBinding.c index 3e50ec47b..298cfe066 100644 --- a/port/cmsis-pack/pikascript/pikascript-api/__pikaBinding.c +++ b/port/cmsis-pack/pikascript/pikascript-api/__pikaBinding.c @@ -35,6 +35,8 @@ #include "TinyObj.h" #include "PikaStdData_Utils.h" #include "TinyObj.h" +#include "PikaStdData_dict_items.h" +#include "TinyObj.h" #include "PikaStdData_dict_keys.h" #include "TinyObj.h" #include "PikaStdLib.h" @@ -111,7 +113,7 @@ void PikaDebug_DebugerMethod(PikaObj *self, Args *args){ PikaObj *New_PikaDebug(Args *args){ PikaObj *self = New_TinyObj(args); - class_defineConstructor(self, "Debuger()->any", PikaDebug_DebugerMethod); + class_defineConstructor(self, "Debuger()", PikaDebug_DebugerMethod); return self; } #endif @@ -185,6 +187,11 @@ void PikaStdData_UtilsMethod(PikaObj *self, Args *args){ method_returnArg(args, res); } +void PikaStdData_dict_itemsMethod(PikaObj *self, Args *args){ + Arg* res = PikaStdData_dict_items(self); + method_returnArg(args, res); +} + void PikaStdData_dict_keysMethod(PikaObj *self, Args *args){ Arg* res = PikaStdData_dict_keys(self); method_returnArg(args, res); @@ -192,14 +199,15 @@ void PikaStdData_dict_keysMethod(PikaObj *self, Args *args){ PikaObj *New_PikaStdData(Args *args){ PikaObj *self = New_TinyObj(args); - class_defineConstructor(self, "ByteArray()->any", PikaStdData_ByteArrayMethod); - class_defineConstructor(self, "Dict()->any", PikaStdData_DictMethod); - class_defineConstructor(self, "FILEIO()->any", PikaStdData_FILEIOMethod); - class_defineConstructor(self, "List()->any", PikaStdData_ListMethod); - class_defineConstructor(self, "String()->any", PikaStdData_StringMethod); - class_defineConstructor(self, "Tuple()->any", PikaStdData_TupleMethod); - class_defineConstructor(self, "Utils()->any", PikaStdData_UtilsMethod); - class_defineConstructor(self, "dict_keys()->any", PikaStdData_dict_keysMethod); + class_defineConstructor(self, "ByteArray()", PikaStdData_ByteArrayMethod); + class_defineConstructor(self, "Dict()", PikaStdData_DictMethod); + class_defineConstructor(self, "FILEIO()", PikaStdData_FILEIOMethod); + class_defineConstructor(self, "List()", PikaStdData_ListMethod); + class_defineConstructor(self, "String()", PikaStdData_StringMethod); + class_defineConstructor(self, "Tuple()", PikaStdData_TupleMethod); + class_defineConstructor(self, "Utils()", PikaStdData_UtilsMethod); + class_defineConstructor(self, "dict_items()", PikaStdData_dict_itemsMethod); + class_defineConstructor(self, "dict_keys()", PikaStdData_dict_keysMethod); return self; } #endif @@ -244,13 +252,13 @@ void PikaStdData_ByteArray_decodeMethod(PikaObj *self, Args *args){ PikaObj *New_PikaStdData_ByteArray(Args *args){ PikaObj *self = New_TinyObj(args); - class_defineMethod(self, "__getitem__(__key:int)->int", PikaStdData_ByteArray___getitem__Method); - class_defineMethod(self, "__init__(bytes:any)", PikaStdData_ByteArray___init__Method); - class_defineMethod(self, "__iter__()->any", PikaStdData_ByteArray___iter__Method); - class_defineMethod(self, "__next__()->any", PikaStdData_ByteArray___next__Method); - class_defineMethod(self, "__setitem__(__key:int,__val:int)", PikaStdData_ByteArray___setitem__Method); - class_defineMethod(self, "__str__()->str", PikaStdData_ByteArray___str__Method); - class_defineMethod(self, "decode()->str", PikaStdData_ByteArray_decodeMethod); + class_defineMethod(self, "__getitem__(__key)", PikaStdData_ByteArray___getitem__Method); + class_defineMethod(self, "__init__(bytes)", PikaStdData_ByteArray___init__Method); + class_defineMethod(self, "__iter__()", PikaStdData_ByteArray___iter__Method); + class_defineMethod(self, "__next__()", PikaStdData_ByteArray___next__Method); + class_defineMethod(self, "__setitem__(__key,__val)", PikaStdData_ByteArray___setitem__Method); + class_defineMethod(self, "__str__()", PikaStdData_ByteArray___str__Method); + class_defineMethod(self, "decode()", PikaStdData_ByteArray_decodeMethod); return self; } @@ -260,6 +268,12 @@ Arg *PikaStdData_ByteArray(PikaObj *self){ #endif #ifndef PIKA_MODULE_PIKASTDDATA_DISABLE +void PikaStdData_Dict___contains__Method(PikaObj *self, Args *args){ + Arg* val = args_getArg(args, "val"); + int res = PikaStdData_Dict___contains__(self, val); + method_returnInt(args, res); +} + void PikaStdData_Dict___del__Method(PikaObj *self, Args *args){ PikaStdData_Dict___del__(self); } @@ -306,6 +320,11 @@ void PikaStdData_Dict_getMethod(PikaObj *self, Args *args){ method_returnArg(args, res); } +void PikaStdData_Dict_itemsMethod(PikaObj *self, Args *args){ + PikaObj* res = PikaStdData_Dict_items(self); + method_returnObj(args, res); +} + void PikaStdData_Dict_keysMethod(PikaObj *self, Args *args){ PikaObj* res = PikaStdData_Dict_keys(self); method_returnObj(args, res); @@ -324,18 +343,20 @@ void PikaStdData_Dict_setMethod(PikaObj *self, Args *args){ PikaObj *New_PikaStdData_Dict(Args *args){ PikaObj *self = New_TinyObj(args); + class_defineMethod(self, "__contains__(val)", PikaStdData_Dict___contains__Method); class_defineMethod(self, "__del__()", PikaStdData_Dict___del__Method); - class_defineMethod(self, "__getitem__(__key:any)->any", PikaStdData_Dict___getitem__Method); + class_defineMethod(self, "__getitem__(__key)", PikaStdData_Dict___getitem__Method); class_defineMethod(self, "__init__()", PikaStdData_Dict___init__Method); - class_defineMethod(self, "__iter__()->any", PikaStdData_Dict___iter__Method); - class_defineMethod(self, "__len__()->int", PikaStdData_Dict___len__Method); - class_defineMethod(self, "__next__()->any", PikaStdData_Dict___next__Method); - class_defineMethod(self, "__setitem__(__key:any,__val:any)", PikaStdData_Dict___setitem__Method); - class_defineMethod(self, "__str__()->str", PikaStdData_Dict___str__Method); - class_defineMethod(self, "get(key:str)->any", PikaStdData_Dict_getMethod); - class_defineMethod(self, "keys()->dict_keys", PikaStdData_Dict_keysMethod); - class_defineMethod(self, "remove(key:str)", PikaStdData_Dict_removeMethod); - class_defineMethod(self, "set(key:str,arg:any)", PikaStdData_Dict_setMethod); + class_defineMethod(self, "__iter__()", PikaStdData_Dict___iter__Method); + class_defineMethod(self, "__len__()", PikaStdData_Dict___len__Method); + class_defineMethod(self, "__next__()", PikaStdData_Dict___next__Method); + class_defineMethod(self, "__setitem__(__key,__val)", PikaStdData_Dict___setitem__Method); + class_defineMethod(self, "__str__()", PikaStdData_Dict___str__Method); + class_defineMethod(self, "get(key)", PikaStdData_Dict_getMethod); + class_defineMethod(self, "items()", PikaStdData_Dict_itemsMethod); + class_defineMethod(self, "keys()", PikaStdData_Dict_keysMethod); + class_defineMethod(self, "remove(key)", PikaStdData_Dict_removeMethod); + class_defineMethod(self, "set(key,arg)", PikaStdData_Dict_setMethod); return self; } @@ -398,14 +419,14 @@ void PikaStdData_FILEIO_writelinesMethod(PikaObj *self, Args *args){ PikaObj *New_PikaStdData_FILEIO(Args *args){ PikaObj *self = New_TinyObj(args); class_defineMethod(self, "close()", PikaStdData_FILEIO_closeMethod); - class_defineMethod(self, "init(path:str,mode:str)->int", PikaStdData_FILEIO_initMethod); - class_defineMethod(self, "read(size:int)->any", PikaStdData_FILEIO_readMethod); - class_defineMethod(self, "readline()->str", PikaStdData_FILEIO_readlineMethod); - class_defineMethod(self, "readlines()->List", PikaStdData_FILEIO_readlinesMethod); - class_defineMethod(self, "seek(offset:int,*fromwhere)->int", PikaStdData_FILEIO_seekMethod); - class_defineMethod(self, "tell()->int", PikaStdData_FILEIO_tellMethod); - class_defineMethod(self, "write(s:any)->int", PikaStdData_FILEIO_writeMethod); - class_defineMethod(self, "writelines(lines:List)", PikaStdData_FILEIO_writelinesMethod); + class_defineMethod(self, "init(path,mode)", PikaStdData_FILEIO_initMethod); + class_defineMethod(self, "read(size)", PikaStdData_FILEIO_readMethod); + class_defineMethod(self, "readline()", PikaStdData_FILEIO_readlineMethod); + class_defineMethod(self, "readlines()", PikaStdData_FILEIO_readlinesMethod); + class_defineMethod(self, "seek(offset,*fromwhere)", PikaStdData_FILEIO_seekMethod); + class_defineMethod(self, "tell()", PikaStdData_FILEIO_tellMethod); + class_defineMethod(self, "write(s)", PikaStdData_FILEIO_writeMethod); + class_defineMethod(self, "writelines(lines)", PikaStdData_FILEIO_writelinesMethod); return self; } @@ -453,13 +474,13 @@ void PikaStdData_List_setMethod(PikaObj *self, Args *args){ PikaObj *New_PikaStdData_List(Args *args){ PikaObj *self = New_PikaStdData_Tuple(args); - class_defineMethod(self, "__add__(others:List)->List", PikaStdData_List___add__Method); + class_defineMethod(self, "__add__(others)", PikaStdData_List___add__Method); class_defineMethod(self, "__init__()", PikaStdData_List___init__Method); - class_defineMethod(self, "__setitem__(__key:any,__val:any)", PikaStdData_List___setitem__Method); - class_defineMethod(self, "__str__()->str", PikaStdData_List___str__Method); - class_defineMethod(self, "append(arg:any)", PikaStdData_List_appendMethod); + class_defineMethod(self, "__setitem__(__key,__val)", PikaStdData_List___setitem__Method); + class_defineMethod(self, "__str__()", PikaStdData_List___str__Method); + class_defineMethod(self, "append(arg)", PikaStdData_List_appendMethod); class_defineMethod(self, "reverse()", PikaStdData_List_reverseMethod); - class_defineMethod(self, "set(i:int,arg:any)", PikaStdData_List_setMethod); + class_defineMethod(self, "set(i,arg)", PikaStdData_List_setMethod); return self; } @@ -507,7 +528,8 @@ void PikaStdData_String___str__Method(PikaObj *self, Args *args){ } void PikaStdData_String_encodeMethod(PikaObj *self, Args *args){ - Arg* res = PikaStdData_String_encode(self); + PikaTuple* encoding = args_getTuple(args, "encoding"); + Arg* res = PikaStdData_String_encode(self, encoding); method_returnArg(args, res); } @@ -578,26 +600,26 @@ void PikaStdData_String_stripMethod(PikaObj *self, Args *args){ PikaObj *New_PikaStdData_String(Args *args){ PikaObj *self = New_TinyObj(args); - class_defineMethod(self, "__getitem__(__key:any)->any", PikaStdData_String___getitem__Method); - class_defineMethod(self, "__init__(s:str)", PikaStdData_String___init__Method); - class_defineMethod(self, "__iter__()->any", PikaStdData_String___iter__Method); - class_defineMethod(self, "__len__()->int", PikaStdData_String___len__Method); - class_defineMethod(self, "__next__()->any", PikaStdData_String___next__Method); - class_defineMethod(self, "__setitem__(__key:any,__val:any)", PikaStdData_String___setitem__Method); - class_defineMethod(self, "__str__()->str", PikaStdData_String___str__Method); - class_defineMethod(self, "encode()->bytes", PikaStdData_String_encodeMethod); - class_defineMethod(self, "endswith(suffix:str)->int", PikaStdData_String_endswithMethod); - class_defineMethod(self, "get()->str", PikaStdData_String_getMethod); - class_defineMethod(self, "isalnum()->int", PikaStdData_String_isalnumMethod); - class_defineMethod(self, "isalpha()->int", PikaStdData_String_isalphaMethod); - class_defineMethod(self, "isdigit()->int", PikaStdData_String_isdigitMethod); - class_defineMethod(self, "islower()->int", PikaStdData_String_islowerMethod); - class_defineMethod(self, "isspace()->int", PikaStdData_String_isspaceMethod); - class_defineMethod(self, "replace(old:str,new:str)->str", PikaStdData_String_replaceMethod); - class_defineMethod(self, "set(s:str)", PikaStdData_String_setMethod); - class_defineMethod(self, "split(s:str)->List", PikaStdData_String_splitMethod); - class_defineMethod(self, "startswith(prefix:str)->int", PikaStdData_String_startswithMethod); - class_defineMethod(self, "strip()->str", PikaStdData_String_stripMethod); + class_defineMethod(self, "__getitem__(__key)", PikaStdData_String___getitem__Method); + class_defineMethod(self, "__init__(s)", PikaStdData_String___init__Method); + class_defineMethod(self, "__iter__()", PikaStdData_String___iter__Method); + class_defineMethod(self, "__len__()", PikaStdData_String___len__Method); + class_defineMethod(self, "__next__()", PikaStdData_String___next__Method); + class_defineMethod(self, "__setitem__(__key,__val)", PikaStdData_String___setitem__Method); + class_defineMethod(self, "__str__()", PikaStdData_String___str__Method); + class_defineMethod(self, "encode(*encoding)", PikaStdData_String_encodeMethod); + class_defineMethod(self, "endswith(suffix)", PikaStdData_String_endswithMethod); + class_defineMethod(self, "get()", PikaStdData_String_getMethod); + class_defineMethod(self, "isalnum()", PikaStdData_String_isalnumMethod); + class_defineMethod(self, "isalpha()", PikaStdData_String_isalphaMethod); + class_defineMethod(self, "isdigit()", PikaStdData_String_isdigitMethod); + class_defineMethod(self, "islower()", PikaStdData_String_islowerMethod); + class_defineMethod(self, "isspace()", PikaStdData_String_isspaceMethod); + class_defineMethod(self, "replace(old,new)", PikaStdData_String_replaceMethod); + class_defineMethod(self, "set(s)", PikaStdData_String_setMethod); + class_defineMethod(self, "split(s)", PikaStdData_String_splitMethod); + class_defineMethod(self, "startswith(prefix)", PikaStdData_String_startswithMethod); + class_defineMethod(self, "strip()", PikaStdData_String_stripMethod); return self; } @@ -607,6 +629,12 @@ Arg *PikaStdData_String(PikaObj *self){ #endif #ifndef PIKA_MODULE_PIKASTDDATA_DISABLE +void PikaStdData_Tuple___contains__Method(PikaObj *self, Args *args){ + Arg* val = args_getArg(args, "val"); + int res = PikaStdData_Tuple___contains__(self, val); + method_returnInt(args, res); +} + void PikaStdData_Tuple___del__Method(PikaObj *self, Args *args){ PikaStdData_Tuple___del__(self); } @@ -654,15 +682,16 @@ void PikaStdData_Tuple_lenMethod(PikaObj *self, Args *args){ PikaObj *New_PikaStdData_Tuple(Args *args){ PikaObj *self = New_TinyObj(args); + class_defineMethod(self, "__contains__(val)", PikaStdData_Tuple___contains__Method); class_defineMethod(self, "__del__()", PikaStdData_Tuple___del__Method); - class_defineMethod(self, "__getitem__(__key:any)->any", PikaStdData_Tuple___getitem__Method); + class_defineMethod(self, "__getitem__(__key)", PikaStdData_Tuple___getitem__Method); class_defineMethod(self, "__init__()", PikaStdData_Tuple___init__Method); - class_defineMethod(self, "__iter__()->any", PikaStdData_Tuple___iter__Method); - class_defineMethod(self, "__len__()->int", PikaStdData_Tuple___len__Method); - class_defineMethod(self, "__next__()->any", PikaStdData_Tuple___next__Method); - class_defineMethod(self, "__str__()->str", PikaStdData_Tuple___str__Method); - class_defineMethod(self, "get(i:int)->any", PikaStdData_Tuple_getMethod); - class_defineMethod(self, "len()->int", PikaStdData_Tuple_lenMethod); + class_defineMethod(self, "__iter__()", PikaStdData_Tuple___iter__Method); + class_defineMethod(self, "__len__()", PikaStdData_Tuple___len__Method); + class_defineMethod(self, "__next__()", PikaStdData_Tuple___next__Method); + class_defineMethod(self, "__str__()", PikaStdData_Tuple___str__Method); + class_defineMethod(self, "get(i)", PikaStdData_Tuple_getMethod); + class_defineMethod(self, "len()", PikaStdData_Tuple_lenMethod); return self; } @@ -680,7 +709,7 @@ void PikaStdData_Utils_int_to_bytesMethod(PikaObj *self, Args *args){ PikaObj *New_PikaStdData_Utils(Args *args){ PikaObj *self = New_TinyObj(args); - class_defineMethod(self, "int_to_bytes(val:int)->bytes", PikaStdData_Utils_int_to_bytesMethod); + class_defineMethod(self, "int_to_bytes(val)", PikaStdData_Utils_int_to_bytesMethod); return self; } @@ -689,6 +718,41 @@ Arg *PikaStdData_Utils(PikaObj *self){ } #endif +#ifndef PIKA_MODULE_PIKASTDDATA_DISABLE +void PikaStdData_dict_items___iter__Method(PikaObj *self, Args *args){ + Arg* res = PikaStdData_dict_items___iter__(self); + method_returnArg(args, res); +} + +void PikaStdData_dict_items___len__Method(PikaObj *self, Args *args){ + int res = PikaStdData_dict_items___len__(self); + method_returnInt(args, res); +} + +void PikaStdData_dict_items___next__Method(PikaObj *self, Args *args){ + Arg* res = PikaStdData_dict_items___next__(self); + method_returnArg(args, res); +} + +void PikaStdData_dict_items___str__Method(PikaObj *self, Args *args){ + char* res = PikaStdData_dict_items___str__(self); + method_returnStr(args, res); +} + +PikaObj *New_PikaStdData_dict_items(Args *args){ + PikaObj *self = New_TinyObj(args); + class_defineMethod(self, "__iter__()", PikaStdData_dict_items___iter__Method); + class_defineMethod(self, "__len__()", PikaStdData_dict_items___len__Method); + class_defineMethod(self, "__next__()", PikaStdData_dict_items___next__Method); + class_defineMethod(self, "__str__()", PikaStdData_dict_items___str__Method); + return self; +} + +Arg *PikaStdData_dict_items(PikaObj *self){ + return obj_newObjInPackage(New_PikaStdData_dict_items); +} +#endif + #ifndef PIKA_MODULE_PIKASTDDATA_DISABLE void PikaStdData_dict_keys___iter__Method(PikaObj *self, Args *args){ Arg* res = PikaStdData_dict_keys___iter__(self); @@ -712,10 +776,10 @@ void PikaStdData_dict_keys___str__Method(PikaObj *self, Args *args){ PikaObj *New_PikaStdData_dict_keys(Args *args){ PikaObj *self = New_TinyObj(args); - class_defineMethod(self, "__iter__()->any", PikaStdData_dict_keys___iter__Method); - class_defineMethod(self, "__len__()->int", PikaStdData_dict_keys___len__Method); - class_defineMethod(self, "__next__()->any", PikaStdData_dict_keys___next__Method); - class_defineMethod(self, "__str__()->str", PikaStdData_dict_keys___str__Method); + class_defineMethod(self, "__iter__()", PikaStdData_dict_keys___iter__Method); + class_defineMethod(self, "__len__()", PikaStdData_dict_keys___len__Method); + class_defineMethod(self, "__next__()", PikaStdData_dict_keys___next__Method); + class_defineMethod(self, "__str__()", PikaStdData_dict_keys___str__Method); return self; } @@ -747,14 +811,14 @@ void PikaStdLib_SysObjMethod(PikaObj *self, Args *args){ PikaObj *New_PikaStdLib(Args *args){ PikaObj *self = New_TinyObj(args); - class_defineConstructor(self, "MemChecker()->any", PikaStdLib_MemCheckerMethod); + class_defineConstructor(self, "MemChecker()", PikaStdLib_MemCheckerMethod); #if 0 - class_defineConstructor(self, "RangeObj()->any", PikaStdLib_RangeObjMethod); + class_defineConstructor(self, "RangeObj()", PikaStdLib_RangeObjMethod); #endif #if 0 - class_defineConstructor(self, "StringObj()->any", PikaStdLib_StringObjMethod); + class_defineConstructor(self, "StringObj()", PikaStdLib_StringObjMethod); #endif - class_defineConstructor(self, "SysObj()->any", PikaStdLib_SysObjMethod); + class_defineConstructor(self, "SysObj()", PikaStdLib_SysObjMethod); return self; } #endif @@ -785,10 +849,10 @@ void PikaStdLib_MemChecker_resetMaxMethod(PikaObj *self, Args *args){ PikaObj *New_PikaStdLib_MemChecker(Args *args){ PikaObj *self = New_TinyObj(args); #if !PIKA_NANO_ENABLE - class_defineMethod(self, "getMax()->float", PikaStdLib_MemChecker_getMaxMethod); + class_defineMethod(self, "getMax()", PikaStdLib_MemChecker_getMaxMethod); #endif #if !PIKA_NANO_ENABLE - class_defineMethod(self, "getNow()->float", PikaStdLib_MemChecker_getNowMethod); + class_defineMethod(self, "getNow()", PikaStdLib_MemChecker_getNowMethod); #endif class_defineMethod(self, "max()", PikaStdLib_MemChecker_maxMethod); class_defineMethod(self, "now()", PikaStdLib_MemChecker_nowMethod); @@ -811,7 +875,7 @@ void PikaStdLib_RangeObj___next__Method(PikaObj *self, Args *args){ PikaObj *New_PikaStdLib_RangeObj(Args *args){ PikaObj *self = New_TinyObj(args); - class_defineMethod(self, "__next__()->any", PikaStdLib_RangeObj___next__Method); + class_defineMethod(self, "__next__()", PikaStdLib_RangeObj___next__Method); return self; } @@ -828,7 +892,7 @@ void PikaStdLib_StringObj___next__Method(PikaObj *self, Args *args){ PikaObj *New_PikaStdLib_StringObj(Args *args){ PikaObj *self = New_TinyObj(args); - class_defineMethod(self, "__next__()->any", PikaStdLib_StringObj___next__Method); + class_defineMethod(self, "__next__()", PikaStdLib_StringObj___next__Method); return self; } @@ -897,6 +961,10 @@ void PikaStdLib_SysObj_execMethod(PikaObj *self, Args *args){ PikaStdLib_SysObj_exec(self, code); } +void PikaStdLib_SysObj_exitMethod(PikaObj *self, Args *args){ + PikaStdLib_SysObj_exit(self); +} + void PikaStdLib_SysObj_floatMethod(PikaObj *self, Args *args){ Arg* arg = args_getArg(args, "arg"); double res = PikaStdLib_SysObj_float(self, arg); @@ -960,18 +1028,13 @@ void PikaStdLib_SysObj_ordMethod(PikaObj *self, Args *args){ void PikaStdLib_SysObj_printMethod(PikaObj *self, Args *args){ PikaTuple* val = args_getTuple(args, "val"); - PikaStdLib_SysObj_print(self, val); -} - -void PikaStdLib_SysObj_printNoEndMethod(PikaObj *self, Args *args){ - Arg* val = args_getArg(args, "val"); - PikaStdLib_SysObj_printNoEnd(self, val); + PikaDict* ops = args_getDict(args, "ops"); + PikaStdLib_SysObj_print(self, val, ops); } void PikaStdLib_SysObj_rangeMethod(PikaObj *self, Args *args){ - int a1 = args_getInt(args, "a1"); - int a2 = args_getInt(args, "a2"); - Arg* res = PikaStdLib_SysObj_range(self, a1, a2); + PikaTuple* ax = args_getTuple(args, "ax"); + Arg* res = PikaStdLib_SysObj_range(self, ax); method_returnArg(args, res); } @@ -996,61 +1059,63 @@ void PikaStdLib_SysObj_typeMethod(PikaObj *self, Args *args){ PikaObj *New_PikaStdLib_SysObj(Args *args){ PikaObj *self = New_TinyObj(args); - class_defineMethod(self, "__getitem__(obj:any,key:any)->any", PikaStdLib_SysObj___getitem__Method); - class_defineMethod(self, "__setitem__(obj:any,key:any,val:any)->any", PikaStdLib_SysObj___setitem__Method); + class_defineMethod(self, "__getitem__(obj,key)", PikaStdLib_SysObj___getitem__Method); + class_defineMethod(self, "__setitem__(obj,key,val)", PikaStdLib_SysObj___setitem__Method); #if PIKA_BUILTIN_STRUCT_ENABLE - class_defineMethod(self, "__slice__(obj:any,start:any,end:any,step:int)->any", PikaStdLib_SysObj___slice__Method); + class_defineMethod(self, "__slice__(obj,start,end,step)", PikaStdLib_SysObj___slice__Method); #endif #if !PIKA_NANO_ENABLE - class_defineMethod(self, "bytes(val:any)->bytes", PikaStdLib_SysObj_bytesMethod); + class_defineMethod(self, "bytes(val)", PikaStdLib_SysObj_bytesMethod); #endif #if PIKA_SYNTAX_FORMAT_ENABLE - class_defineMethod(self, "cformat(fmt:str,*var)->str", PikaStdLib_SysObj_cformatMethod); + class_defineMethod(self, "cformat(fmt,*var)", PikaStdLib_SysObj_cformatMethod); #endif #if !PIKA_NANO_ENABLE - class_defineMethod(self, "chr(val:int)->str", PikaStdLib_SysObj_chrMethod); + class_defineMethod(self, "chr(val)", PikaStdLib_SysObj_chrMethod); #endif #if PIKA_BUILTIN_STRUCT_ENABLE - class_defineMethod(self, "dict()->any", PikaStdLib_SysObj_dictMethod); + class_defineMethod(self, "dict()", PikaStdLib_SysObj_dictMethod); #endif #if !PIKA_NANO_ENABLE - class_defineMethod(self, "dir(obj:object)->list", PikaStdLib_SysObj_dirMethod); + class_defineMethod(self, "dir(obj)", PikaStdLib_SysObj_dirMethod); #endif #if PIKA_EXEC_ENABLE - class_defineMethod(self, "exec(code:str)", PikaStdLib_SysObj_execMethod); -#endif - class_defineMethod(self, "float(arg:any)->float", PikaStdLib_SysObj_floatMethod); -#if !PIKA_NANO_ENABLE - class_defineMethod(self, "getattr(obj:object,name:str)->any", PikaStdLib_SysObj_getattrMethod); + class_defineMethod(self, "exec(code)", PikaStdLib_SysObj_execMethod); #endif #if !PIKA_NANO_ENABLE - class_defineMethod(self, "hex(val:int)->str", PikaStdLib_SysObj_hexMethod); + class_defineMethod(self, "exit()", PikaStdLib_SysObj_exitMethod); +#endif + class_defineMethod(self, "float(arg)", PikaStdLib_SysObj_floatMethod); +#if !PIKA_NANO_ENABLE + class_defineMethod(self, "getattr(obj,name)", PikaStdLib_SysObj_getattrMethod); #endif #if !PIKA_NANO_ENABLE - class_defineMethod(self, "id(obj:any)->int", PikaStdLib_SysObj_idMethod); + class_defineMethod(self, "hex(val)", PikaStdLib_SysObj_hexMethod); #endif - class_defineMethod(self, "int(arg:any)->int", PikaStdLib_SysObj_intMethod); - class_defineMethod(self, "iter(arg:any)->any", PikaStdLib_SysObj_iterMethod); - class_defineMethod(self, "len(arg:any)->int", PikaStdLib_SysObj_lenMethod); +#if !PIKA_NANO_ENABLE + class_defineMethod(self, "id(obj)", PikaStdLib_SysObj_idMethod); +#endif + class_defineMethod(self, "int(arg)", PikaStdLib_SysObj_intMethod); + class_defineMethod(self, "iter(arg)", PikaStdLib_SysObj_iterMethod); + class_defineMethod(self, "len(arg)", PikaStdLib_SysObj_lenMethod); #if PIKA_BUILTIN_STRUCT_ENABLE - class_defineMethod(self, "list()->any", PikaStdLib_SysObj_listMethod); + class_defineMethod(self, "list()", PikaStdLib_SysObj_listMethod); #endif #if PIKA_FILEIO_ENABLE - class_defineMethod(self, "open(path:str,mode:str)->object", PikaStdLib_SysObj_openMethod); + class_defineMethod(self, "open(path,mode)", PikaStdLib_SysObj_openMethod); #endif #if !PIKA_NANO_ENABLE - class_defineMethod(self, "ord(val:str)->int", PikaStdLib_SysObj_ordMethod); + class_defineMethod(self, "ord(val)", PikaStdLib_SysObj_ordMethod); #endif - class_defineMethod(self, "print(*val)", PikaStdLib_SysObj_printMethod); + class_defineMethod(self, "print(*val,**ops)", PikaStdLib_SysObj_printMethod); + class_defineMethod(self, "range(*ax)", PikaStdLib_SysObj_rangeMethod); #if !PIKA_NANO_ENABLE - class_defineMethod(self, "printNoEnd(val:any)", PikaStdLib_SysObj_printNoEndMethod); + class_defineMethod(self, "setattr(obj,name,val)", PikaStdLib_SysObj_setattrMethod); #endif - class_defineMethod(self, "range(a1:int,a2:int)->any", PikaStdLib_SysObj_rangeMethod); + class_defineMethod(self, "str(arg)", PikaStdLib_SysObj_strMethod); #if !PIKA_NANO_ENABLE - class_defineMethod(self, "setattr(obj:object,name:str,val:any)", PikaStdLib_SysObj_setattrMethod); + class_defineMethod(self, "type(arg)", PikaStdLib_SysObj_typeMethod); #endif - class_defineMethod(self, "str(arg:any)->str", PikaStdLib_SysObj_strMethod); - class_defineMethod(self, "type(arg:any)->any", PikaStdLib_SysObj_typeMethod); return self; } @@ -1067,7 +1132,7 @@ void PikaStdTask_TaskMethod(PikaObj *self, Args *args){ PikaObj *New_PikaStdTask(Args *args){ PikaObj *self = New_TinyObj(args); - class_defineConstructor(self, "Task()->any", PikaStdTask_TaskMethod); + class_defineConstructor(self, "Task()", PikaStdTask_TaskMethod); return self; } #endif @@ -1115,13 +1180,13 @@ PikaObj *New_PikaStdTask_Task(Args *args){ PikaObj *self = New_PikaStdLib_SysObj(args); obj_newObj(self, "calls", "PikaStdData_List", New_PikaStdData_List); class_defineMethod(self, "__init__()", PikaStdTask_Task___init__Method); - class_defineMethod(self, "call_always(fun_todo:any)", PikaStdTask_Task_call_alwaysMethod); - class_defineMethod(self, "call_period_ms(fun_todo:any,period_ms:int)", PikaStdTask_Task_call_period_msMethod); - class_defineMethod(self, "call_when(fun_todo:any,fun_when:any)", PikaStdTask_Task_call_whenMethod); + class_defineMethod(self, "call_always(fun_todo)", PikaStdTask_Task_call_alwaysMethod); + class_defineMethod(self, "call_period_ms(fun_todo,period_ms)", PikaStdTask_Task_call_period_msMethod); + class_defineMethod(self, "call_when(fun_todo,fun_when)", PikaStdTask_Task_call_whenMethod); class_defineMethod(self, "platformGetTick()", PikaStdTask_Task_platformGetTickMethod); class_defineMethod(self, "run_forever()", PikaStdTask_Task_run_foreverMethod); class_defineMethod(self, "run_once()", PikaStdTask_Task_run_onceMethod); - class_defineMethod(self, "run_until_ms(until_ms:int)", PikaStdTask_Task_run_until_msMethod); + class_defineMethod(self, "run_until_ms(until_ms)", PikaStdTask_Task_run_until_msMethod); return self; } @@ -1291,37 +1356,37 @@ void pika_lvgl_timer_create_basicMethod(PikaObj *self, Args *args){ PikaObj *New_pika_lvgl(Args *args){ PikaObj *self = New_TinyObj(args); - class_defineConstructor(self, "ALIGN()->any", pika_lvgl_ALIGNMethod); - class_defineConstructor(self, "ANIM()->any", pika_lvgl_ANIMMethod); - class_defineConstructor(self, "EVENT()->any", pika_lvgl_EVENTMethod); - class_defineConstructor(self, "OPA()->any", pika_lvgl_OPAMethod); - class_defineConstructor(self, "PALETTE()->any", pika_lvgl_PALETTEMethod); - class_defineConstructor(self, "STATE()->any", pika_lvgl_STATEMethod); + class_defineConstructor(self, "ALIGN()", pika_lvgl_ALIGNMethod); + class_defineConstructor(self, "ANIM()", pika_lvgl_ANIMMethod); + class_defineConstructor(self, "EVENT()", pika_lvgl_EVENTMethod); + class_defineConstructor(self, "OPA()", pika_lvgl_OPAMethod); + class_defineConstructor(self, "PALETTE()", pika_lvgl_PALETTEMethod); + class_defineConstructor(self, "STATE()", pika_lvgl_STATEMethod); class_defineMethod(self, "__init__()", pika_lvgl___init__Method); - class_defineConstructor(self, "arc()->any", pika_lvgl_arcMethod); - class_defineConstructor(self, "bar()->any", pika_lvgl_barMethod); - class_defineConstructor(self, "btn()->any", pika_lvgl_btnMethod); - class_defineConstructor(self, "checkbox()->any", pika_lvgl_checkboxMethod); - class_defineConstructor(self, "dropdown()->any", pika_lvgl_dropdownMethod); - class_defineMethod(self, "indev_get_act()->indev_t", pika_lvgl_indev_get_actMethod); - class_defineConstructor(self, "indev_t()->any", pika_lvgl_indev_tMethod); - class_defineConstructor(self, "label()->any", pika_lvgl_labelMethod); - class_defineConstructor(self, "lv_color_t()->any", pika_lvgl_lv_color_tMethod); - class_defineConstructor(self, "lv_event()->any", pika_lvgl_lv_eventMethod); - class_defineConstructor(self, "lv_obj()->any", pika_lvgl_lv_objMethod); - class_defineConstructor(self, "lv_timer_t()->any", pika_lvgl_lv_timer_tMethod); - class_defineMethod(self, "obj(parent:lv_obj)->lv_obj", pika_lvgl_objMethod); - class_defineMethod(self, "palette_lighten(p:int,lvl:int)->lv_color_t", pika_lvgl_palette_lightenMethod); - class_defineMethod(self, "palette_main(p:int)->lv_color_t", pika_lvgl_palette_mainMethod); - class_defineConstructor(self, "point_t()->any", pika_lvgl_point_tMethod); - class_defineConstructor(self, "roller()->any", pika_lvgl_rollerMethod); - class_defineMethod(self, "scr_act()->lv_obj", pika_lvgl_scr_actMethod); - class_defineConstructor(self, "slider()->any", pika_lvgl_sliderMethod); - class_defineConstructor(self, "style_t()->any", pika_lvgl_style_tMethod); - class_defineConstructor(self, "switch()->any", pika_lvgl_switchMethod); - class_defineConstructor(self, "table()->any", pika_lvgl_tableMethod); - class_defineConstructor(self, "textarea()->any", pika_lvgl_textareaMethod); - class_defineMethod(self, "timer_create_basic()->lv_timer_t", pika_lvgl_timer_create_basicMethod); + class_defineConstructor(self, "arc()", pika_lvgl_arcMethod); + class_defineConstructor(self, "bar()", pika_lvgl_barMethod); + class_defineConstructor(self, "btn()", pika_lvgl_btnMethod); + class_defineConstructor(self, "checkbox()", pika_lvgl_checkboxMethod); + class_defineConstructor(self, "dropdown()", pika_lvgl_dropdownMethod); + class_defineMethod(self, "indev_get_act()", pika_lvgl_indev_get_actMethod); + class_defineConstructor(self, "indev_t()", pika_lvgl_indev_tMethod); + class_defineConstructor(self, "label()", pika_lvgl_labelMethod); + class_defineConstructor(self, "lv_color_t()", pika_lvgl_lv_color_tMethod); + class_defineConstructor(self, "lv_event()", pika_lvgl_lv_eventMethod); + class_defineConstructor(self, "lv_obj()", pika_lvgl_lv_objMethod); + class_defineConstructor(self, "lv_timer_t()", pika_lvgl_lv_timer_tMethod); + class_defineMethod(self, "obj(parent)", pika_lvgl_objMethod); + class_defineMethod(self, "palette_lighten(p,lvl)", pika_lvgl_palette_lightenMethod); + class_defineMethod(self, "palette_main(p)", pika_lvgl_palette_mainMethod); + class_defineConstructor(self, "point_t()", pika_lvgl_point_tMethod); + class_defineConstructor(self, "roller()", pika_lvgl_rollerMethod); + class_defineMethod(self, "scr_act()", pika_lvgl_scr_actMethod); + class_defineConstructor(self, "slider()", pika_lvgl_sliderMethod); + class_defineConstructor(self, "style_t()", pika_lvgl_style_tMethod); + class_defineConstructor(self, "switch()", pika_lvgl_switchMethod); + class_defineConstructor(self, "table()", pika_lvgl_tableMethod); + class_defineConstructor(self, "textarea()", pika_lvgl_textareaMethod); + class_defineMethod(self, "timer_create_basic()", pika_lvgl_timer_create_basicMethod); return self; } #endif @@ -1528,26 +1593,26 @@ void pika_lvgl_arc_set_valueMethod(PikaObj *self, Args *args){ PikaObj *New_pika_lvgl_arc(Args *args){ PikaObj *self = New_pika_lvgl_lv_obj(args); - class_defineMethod(self, "__init__(parent:lv_obj)", pika_lvgl_arc___init__Method); - class_defineMethod(self, "get_angle_end()->int", pika_lvgl_arc_get_angle_endMethod); - class_defineMethod(self, "get_angle_start()->int", pika_lvgl_arc_get_angle_startMethod); - class_defineMethod(self, "get_bg_angle_end()->int", pika_lvgl_arc_get_bg_angle_endMethod); - class_defineMethod(self, "get_bg_angle_start()->int", pika_lvgl_arc_get_bg_angle_startMethod); - class_defineMethod(self, "get_max_value()->int", pika_lvgl_arc_get_max_valueMethod); - class_defineMethod(self, "get_min_value()->int", pika_lvgl_arc_get_min_valueMethod); - class_defineMethod(self, "get_mode()->int", pika_lvgl_arc_get_modeMethod); - class_defineMethod(self, "get_value()->int", pika_lvgl_arc_get_valueMethod); - class_defineMethod(self, "set_angles(start:int,end:int)", pika_lvgl_arc_set_anglesMethod); - class_defineMethod(self, "set_bg_angles(start:int,end:int)", pika_lvgl_arc_set_bg_anglesMethod); - class_defineMethod(self, "set_bg_end_angle(angle:int)", pika_lvgl_arc_set_bg_end_angleMethod); - class_defineMethod(self, "set_bg_start_angle(start:int)", pika_lvgl_arc_set_bg_start_angleMethod); - class_defineMethod(self, "set_change_rate(rate:int)", pika_lvgl_arc_set_change_rateMethod); - class_defineMethod(self, "set_end_angle(angle:int)", pika_lvgl_arc_set_end_angleMethod); - class_defineMethod(self, "set_mode(mode:int)", pika_lvgl_arc_set_modeMethod); - class_defineMethod(self, "set_range(min:int,max:int)", pika_lvgl_arc_set_rangeMethod); - class_defineMethod(self, "set_rotation(rotation:int)", pika_lvgl_arc_set_rotationMethod); - class_defineMethod(self, "set_start_angle(start:int)", pika_lvgl_arc_set_start_angleMethod); - class_defineMethod(self, "set_value(value:int)", pika_lvgl_arc_set_valueMethod); + class_defineMethod(self, "__init__(parent)", pika_lvgl_arc___init__Method); + class_defineMethod(self, "get_angle_end()", pika_lvgl_arc_get_angle_endMethod); + class_defineMethod(self, "get_angle_start()", pika_lvgl_arc_get_angle_startMethod); + class_defineMethod(self, "get_bg_angle_end()", pika_lvgl_arc_get_bg_angle_endMethod); + class_defineMethod(self, "get_bg_angle_start()", pika_lvgl_arc_get_bg_angle_startMethod); + class_defineMethod(self, "get_max_value()", pika_lvgl_arc_get_max_valueMethod); + class_defineMethod(self, "get_min_value()", pika_lvgl_arc_get_min_valueMethod); + class_defineMethod(self, "get_mode()", pika_lvgl_arc_get_modeMethod); + class_defineMethod(self, "get_value()", pika_lvgl_arc_get_valueMethod); + class_defineMethod(self, "set_angles(start,end)", pika_lvgl_arc_set_anglesMethod); + class_defineMethod(self, "set_bg_angles(start,end)", pika_lvgl_arc_set_bg_anglesMethod); + class_defineMethod(self, "set_bg_end_angle(angle)", pika_lvgl_arc_set_bg_end_angleMethod); + class_defineMethod(self, "set_bg_start_angle(start)", pika_lvgl_arc_set_bg_start_angleMethod); + class_defineMethod(self, "set_change_rate(rate)", pika_lvgl_arc_set_change_rateMethod); + class_defineMethod(self, "set_end_angle(angle)", pika_lvgl_arc_set_end_angleMethod); + class_defineMethod(self, "set_mode(mode)", pika_lvgl_arc_set_modeMethod); + class_defineMethod(self, "set_range(min,max)", pika_lvgl_arc_set_rangeMethod); + class_defineMethod(self, "set_rotation(rotation)", pika_lvgl_arc_set_rotationMethod); + class_defineMethod(self, "set_start_angle(start)", pika_lvgl_arc_set_start_angleMethod); + class_defineMethod(self, "set_value(value)", pika_lvgl_arc_set_valueMethod); return self; } @@ -1612,16 +1677,16 @@ void pika_lvgl_bar_set_valueMethod(PikaObj *self, Args *args){ PikaObj *New_pika_lvgl_bar(Args *args){ PikaObj *self = New_pika_lvgl_lv_obj(args); - class_defineMethod(self, "__init__(parent:lv_obj)", pika_lvgl_bar___init__Method); - class_defineMethod(self, "get_max_value()->int", pika_lvgl_bar_get_max_valueMethod); - class_defineMethod(self, "get_min_value()->int", pika_lvgl_bar_get_min_valueMethod); - class_defineMethod(self, "get_mode()->int", pika_lvgl_bar_get_modeMethod); - class_defineMethod(self, "get_start_value()->int", pika_lvgl_bar_get_start_valueMethod); - class_defineMethod(self, "get_value()->int", pika_lvgl_bar_get_valueMethod); - class_defineMethod(self, "set_mode(mode:int)", pika_lvgl_bar_set_modeMethod); - class_defineMethod(self, "set_range(min:int,max:int)", pika_lvgl_bar_set_rangeMethod); - class_defineMethod(self, "set_start_value(start_value:int,anim:int)", pika_lvgl_bar_set_start_valueMethod); - class_defineMethod(self, "set_value(value:int,anim:int)", pika_lvgl_bar_set_valueMethod); + class_defineMethod(self, "__init__(parent)", pika_lvgl_bar___init__Method); + class_defineMethod(self, "get_max_value()", pika_lvgl_bar_get_max_valueMethod); + class_defineMethod(self, "get_min_value()", pika_lvgl_bar_get_min_valueMethod); + class_defineMethod(self, "get_mode()", pika_lvgl_bar_get_modeMethod); + class_defineMethod(self, "get_start_value()", pika_lvgl_bar_get_start_valueMethod); + class_defineMethod(self, "get_value()", pika_lvgl_bar_get_valueMethod); + class_defineMethod(self, "set_mode(mode)", pika_lvgl_bar_set_modeMethod); + class_defineMethod(self, "set_range(min,max)", pika_lvgl_bar_set_rangeMethod); + class_defineMethod(self, "set_start_value(start_value,anim)", pika_lvgl_bar_set_start_valueMethod); + class_defineMethod(self, "set_value(value,anim)", pika_lvgl_bar_set_valueMethod); return self; } @@ -1638,7 +1703,7 @@ void pika_lvgl_btn___init__Method(PikaObj *self, Args *args){ PikaObj *New_pika_lvgl_btn(Args *args){ PikaObj *self = New_pika_lvgl_lv_obj(args); - class_defineMethod(self, "__init__(parent:lv_obj)", pika_lvgl_btn___init__Method); + class_defineMethod(self, "__init__(parent)", pika_lvgl_btn___init__Method); return self; } @@ -1670,10 +1735,10 @@ void pika_lvgl_checkbox_set_text_staticMethod(PikaObj *self, Args *args){ PikaObj *New_pika_lvgl_checkbox(Args *args){ PikaObj *self = New_pika_lvgl_lv_obj(args); - class_defineMethod(self, "__init__(parent:lv_obj)", pika_lvgl_checkbox___init__Method); - class_defineMethod(self, "get_text()->str", pika_lvgl_checkbox_get_textMethod); - class_defineMethod(self, "set_text(txt:str)", pika_lvgl_checkbox_set_textMethod); - class_defineMethod(self, "set_text_static(txt:str)", pika_lvgl_checkbox_set_text_staticMethod); + class_defineMethod(self, "__init__(parent)", pika_lvgl_checkbox___init__Method); + class_defineMethod(self, "get_text()", pika_lvgl_checkbox_get_textMethod); + class_defineMethod(self, "set_text(txt)", pika_lvgl_checkbox_set_textMethod); + class_defineMethod(self, "set_text_static(txt)", pika_lvgl_checkbox_set_text_staticMethod); return self; } @@ -1789,27 +1854,27 @@ void pika_lvgl_dropdown_set_textMethod(PikaObj *self, Args *args){ PikaObj *New_pika_lvgl_dropdown(Args *args){ PikaObj *self = New_pika_lvgl_lv_obj(args); - class_defineMethod(self, "__init__(parent:lv_obj)", pika_lvgl_dropdown___init__Method); - class_defineMethod(self, "add_option(option:str,pos:int)", pika_lvgl_dropdown_add_optionMethod); + class_defineMethod(self, "__init__(parent)", pika_lvgl_dropdown___init__Method); + class_defineMethod(self, "add_option(option,pos)", pika_lvgl_dropdown_add_optionMethod); class_defineMethod(self, "clear_options()", pika_lvgl_dropdown_clear_optionsMethod); class_defineMethod(self, "close()", pika_lvgl_dropdown_closeMethod); - class_defineMethod(self, "get_dir()->int", pika_lvgl_dropdown_get_dirMethod); - class_defineMethod(self, "get_option_cnt()->int", pika_lvgl_dropdown_get_option_cntMethod); - class_defineMethod(self, "get_option_index(option:str)->int", pika_lvgl_dropdown_get_option_indexMethod); - class_defineMethod(self, "get_options()->str", pika_lvgl_dropdown_get_optionsMethod); - class_defineMethod(self, "get_selected()->int", pika_lvgl_dropdown_get_selectedMethod); - class_defineMethod(self, "get_selected_highlight()->int", pika_lvgl_dropdown_get_selected_highlightMethod); - class_defineMethod(self, "get_selected_str()->str", pika_lvgl_dropdown_get_selected_strMethod); - class_defineMethod(self, "get_symbol()->int", pika_lvgl_dropdown_get_symbolMethod); - class_defineMethod(self, "get_text()->str", pika_lvgl_dropdown_get_textMethod); - class_defineMethod(self, "is_open()->int", pika_lvgl_dropdown_is_openMethod); + class_defineMethod(self, "get_dir()", pika_lvgl_dropdown_get_dirMethod); + class_defineMethod(self, "get_option_cnt()", pika_lvgl_dropdown_get_option_cntMethod); + class_defineMethod(self, "get_option_index(option)", pika_lvgl_dropdown_get_option_indexMethod); + class_defineMethod(self, "get_options()", pika_lvgl_dropdown_get_optionsMethod); + class_defineMethod(self, "get_selected()", pika_lvgl_dropdown_get_selectedMethod); + class_defineMethod(self, "get_selected_highlight()", pika_lvgl_dropdown_get_selected_highlightMethod); + class_defineMethod(self, "get_selected_str()", pika_lvgl_dropdown_get_selected_strMethod); + class_defineMethod(self, "get_symbol()", pika_lvgl_dropdown_get_symbolMethod); + class_defineMethod(self, "get_text()", pika_lvgl_dropdown_get_textMethod); + class_defineMethod(self, "is_open()", pika_lvgl_dropdown_is_openMethod); class_defineMethod(self, "open()", pika_lvgl_dropdown_openMethod); - class_defineMethod(self, "set_dir(dir:int)", pika_lvgl_dropdown_set_dirMethod); - class_defineMethod(self, "set_options(options:str)", pika_lvgl_dropdown_set_optionsMethod); - class_defineMethod(self, "set_selected(sel_opt:int)", pika_lvgl_dropdown_set_selectedMethod); - class_defineMethod(self, "set_selected_hightlight(en:int)", pika_lvgl_dropdown_set_selected_hightlightMethod); - class_defineMethod(self, "set_symbol(symbol:str)", pika_lvgl_dropdown_set_symbolMethod); - class_defineMethod(self, "set_text(txt:str)", pika_lvgl_dropdown_set_textMethod); + class_defineMethod(self, "set_dir(dir)", pika_lvgl_dropdown_set_dirMethod); + class_defineMethod(self, "set_options(options)", pika_lvgl_dropdown_set_optionsMethod); + class_defineMethod(self, "set_selected(sel_opt)", pika_lvgl_dropdown_set_selectedMethod); + class_defineMethod(self, "set_selected_hightlight(en)", pika_lvgl_dropdown_set_selected_hightlightMethod); + class_defineMethod(self, "set_symbol(symbol)", pika_lvgl_dropdown_set_symbolMethod); + class_defineMethod(self, "set_text(txt)", pika_lvgl_dropdown_set_textMethod); return self; } @@ -1826,7 +1891,7 @@ void pika_lvgl_indev_t_get_vectMethod(PikaObj *self, Args *args){ PikaObj *New_pika_lvgl_indev_t(Args *args){ PikaObj *self = New_TinyObj(args); - class_defineMethod(self, "get_vect(point:point_t)", pika_lvgl_indev_t_get_vectMethod); + class_defineMethod(self, "get_vect(point)", pika_lvgl_indev_t_get_vectMethod); return self; } @@ -1864,11 +1929,11 @@ void pika_lvgl_label_set_textMethod(PikaObj *self, Args *args){ PikaObj *New_pika_lvgl_label(Args *args){ PikaObj *self = New_pika_lvgl_lv_obj(args); - class_defineMethod(self, "__init__(parent:lv_obj)", pika_lvgl_label___init__Method); - class_defineMethod(self, "set_long_mode(mode:int)", pika_lvgl_label_set_long_modeMethod); - class_defineMethod(self, "set_recolor(en:int)", pika_lvgl_label_set_recolorMethod); - class_defineMethod(self, "set_style_text_align(value:int,selector:int)", pika_lvgl_label_set_style_text_alignMethod); - class_defineMethod(self, "set_text(txt:str)", pika_lvgl_label_set_textMethod); + class_defineMethod(self, "__init__(parent)", pika_lvgl_label___init__Method); + class_defineMethod(self, "set_long_mode(mode)", pika_lvgl_label_set_long_modeMethod); + class_defineMethod(self, "set_recolor(en)", pika_lvgl_label_set_recolorMethod); + class_defineMethod(self, "set_style_text_align(value,selector)", pika_lvgl_label_set_style_text_alignMethod); + class_defineMethod(self, "set_text(txt)", pika_lvgl_label_set_textMethod); return self; } @@ -1901,8 +1966,8 @@ void pika_lvgl_lv_event_get_targetMethod(PikaObj *self, Args *args){ PikaObj *New_pika_lvgl_lv_event(Args *args){ PikaObj *self = New_TinyObj(args); - class_defineMethod(self, "get_code()->int", pika_lvgl_lv_event_get_codeMethod); - class_defineMethod(self, "get_target()->lv_obj", pika_lvgl_lv_event_get_targetMethod); + class_defineMethod(self, "get_code()", pika_lvgl_lv_event_get_codeMethod); + class_defineMethod(self, "get_target()", pika_lvgl_lv_event_get_targetMethod); return self; } @@ -1979,17 +2044,17 @@ void pika_lvgl_lv_obj_update_layoutMethod(PikaObj *self, Args *args){ PikaObj *New_pika_lvgl_lv_obj(Args *args){ PikaObj *self = New_TinyObj(args); - class_defineMethod(self, "add_event_cb(event_cb:any,filter:int,user_data:pointer)", pika_lvgl_lv_obj_add_event_cbMethod); - class_defineMethod(self, "add_state(state:int)", pika_lvgl_lv_obj_add_stateMethod); - class_defineMethod(self, "add_style(style:style_t,selector:int)", pika_lvgl_lv_obj_add_styleMethod); - class_defineMethod(self, "align(align:int,x_ofs:int,y_ofs:int)", pika_lvgl_lv_obj_alignMethod); + class_defineMethod(self, "add_event_cb(event_cb,filter,user_data)", pika_lvgl_lv_obj_add_event_cbMethod); + class_defineMethod(self, "add_state(state)", pika_lvgl_lv_obj_add_stateMethod); + class_defineMethod(self, "add_style(style,selector)", pika_lvgl_lv_obj_add_styleMethod); + class_defineMethod(self, "align(align,x_ofs,y_ofs)", pika_lvgl_lv_obj_alignMethod); class_defineMethod(self, "center()", pika_lvgl_lv_obj_centerMethod); - class_defineMethod(self, "get_x()->int", pika_lvgl_lv_obj_get_xMethod); - class_defineMethod(self, "get_y()->int", pika_lvgl_lv_obj_get_yMethod); - class_defineMethod(self, "set_hight(h:int)", pika_lvgl_lv_obj_set_hightMethod); - class_defineMethod(self, "set_pos(x:int,y:int)", pika_lvgl_lv_obj_set_posMethod); - class_defineMethod(self, "set_size(size_x:int,size_y:int)", pika_lvgl_lv_obj_set_sizeMethod); - class_defineMethod(self, "set_width(w:int)", pika_lvgl_lv_obj_set_widthMethod); + class_defineMethod(self, "get_x()", pika_lvgl_lv_obj_get_xMethod); + class_defineMethod(self, "get_y()", pika_lvgl_lv_obj_get_yMethod); + class_defineMethod(self, "set_hight(h)", pika_lvgl_lv_obj_set_hightMethod); + class_defineMethod(self, "set_pos(x,y)", pika_lvgl_lv_obj_set_posMethod); + class_defineMethod(self, "set_size(size_x,size_y)", pika_lvgl_lv_obj_set_sizeMethod); + class_defineMethod(self, "set_width(w)", pika_lvgl_lv_obj_set_widthMethod); class_defineMethod(self, "update_layout()", pika_lvgl_lv_obj_update_layoutMethod); return self; } @@ -2017,8 +2082,8 @@ void pika_lvgl_lv_timer_t_set_periodMethod(PikaObj *self, Args *args){ PikaObj *New_pika_lvgl_lv_timer_t(Args *args){ PikaObj *self = New_TinyObj(args); class_defineMethod(self, "_del()", pika_lvgl_lv_timer_t__delMethod); - class_defineMethod(self, "set_cb(cb:any)", pika_lvgl_lv_timer_t_set_cbMethod); - class_defineMethod(self, "set_period(period:int)", pika_lvgl_lv_timer_t_set_periodMethod); + class_defineMethod(self, "set_cb(cb)", pika_lvgl_lv_timer_t_set_cbMethod); + class_defineMethod(self, "set_period(period)", pika_lvgl_lv_timer_t_set_periodMethod); return self; } @@ -2062,9 +2127,9 @@ void pika_lvgl_roller_set_visible_row_countMethod(PikaObj *self, Args *args){ PikaObj *New_pika_lvgl_roller(Args *args){ PikaObj *self = New_pika_lvgl_lv_obj(args); - class_defineMethod(self, "__init__(parent:lv_obj)", pika_lvgl_roller___init__Method); - class_defineMethod(self, "set_options(options:str,mode:int)", pika_lvgl_roller_set_optionsMethod); - class_defineMethod(self, "set_visible_row_count(row_cnt:int)", pika_lvgl_roller_set_visible_row_countMethod); + class_defineMethod(self, "__init__(parent)", pika_lvgl_roller___init__Method); + class_defineMethod(self, "set_options(options,mode)", pika_lvgl_roller_set_optionsMethod); + class_defineMethod(self, "set_visible_row_count(row_cnt)", pika_lvgl_roller_set_visible_row_countMethod); return self; } @@ -2081,7 +2146,7 @@ void pika_lvgl_slider___init__Method(PikaObj *self, Args *args){ PikaObj *New_pika_lvgl_slider(Args *args){ PikaObj *self = New_pika_lvgl_lv_obj(args); - class_defineMethod(self, "__init__(parent:lv_obj)", pika_lvgl_slider___init__Method); + class_defineMethod(self, "__init__(parent)", pika_lvgl_slider___init__Method); return self; } @@ -2148,15 +2213,15 @@ PikaObj *New_pika_lvgl_style_t(Args *args){ PikaObj *self = New_TinyObj(args); class_defineMethod(self, "__init__()", pika_lvgl_style_t___init__Method); class_defineMethod(self, "init()", pika_lvgl_style_t_initMethod); - class_defineMethod(self, "set_bg_color(color:lv_color_t)", pika_lvgl_style_t_set_bg_colorMethod); - class_defineMethod(self, "set_bg_opa(opa:int)", pika_lvgl_style_t_set_bg_opaMethod); - class_defineMethod(self, "set_outline_color(color:lv_color_t)", pika_lvgl_style_t_set_outline_colorMethod); - class_defineMethod(self, "set_outline_pad(pad:int)", pika_lvgl_style_t_set_outline_padMethod); - class_defineMethod(self, "set_outline_width(w:int)", pika_lvgl_style_t_set_outline_widthMethod); - class_defineMethod(self, "set_radius(radius:int)", pika_lvgl_style_t_set_radiusMethod); - class_defineMethod(self, "set_shadow_color(color:lv_color_t)", pika_lvgl_style_t_set_shadow_colorMethod); - class_defineMethod(self, "set_shadow_spread(s:int)", pika_lvgl_style_t_set_shadow_spreadMethod); - class_defineMethod(self, "set_shadow_width(w:int)", pika_lvgl_style_t_set_shadow_widthMethod); + class_defineMethod(self, "set_bg_color(color)", pika_lvgl_style_t_set_bg_colorMethod); + class_defineMethod(self, "set_bg_opa(opa)", pika_lvgl_style_t_set_bg_opaMethod); + class_defineMethod(self, "set_outline_color(color)", pika_lvgl_style_t_set_outline_colorMethod); + class_defineMethod(self, "set_outline_pad(pad)", pika_lvgl_style_t_set_outline_padMethod); + class_defineMethod(self, "set_outline_width(w)", pika_lvgl_style_t_set_outline_widthMethod); + class_defineMethod(self, "set_radius(radius)", pika_lvgl_style_t_set_radiusMethod); + class_defineMethod(self, "set_shadow_color(color)", pika_lvgl_style_t_set_shadow_colorMethod); + class_defineMethod(self, "set_shadow_spread(s)", pika_lvgl_style_t_set_shadow_spreadMethod); + class_defineMethod(self, "set_shadow_width(w)", pika_lvgl_style_t_set_shadow_widthMethod); return self; } @@ -2173,7 +2238,7 @@ void pika_lvgl_switch___init__Method(PikaObj *self, Args *args){ PikaObj *New_pika_lvgl_switch(Args *args){ PikaObj *self = New_pika_lvgl_lv_obj(args); - class_defineMethod(self, "__init__(parent:lv_obj)", pika_lvgl_switch___init__Method); + class_defineMethod(self, "__init__(parent)", pika_lvgl_switch___init__Method); return self; } @@ -2197,8 +2262,8 @@ void pika_lvgl_table_set_cell_valueMethod(PikaObj *self, Args *args){ PikaObj *New_pika_lvgl_table(Args *args){ PikaObj *self = New_pika_lvgl_lv_obj(args); - class_defineMethod(self, "__init__(parent:lv_obj)", pika_lvgl_table___init__Method); - class_defineMethod(self, "set_cell_value(row:int,col:int,txt:str)", pika_lvgl_table_set_cell_valueMethod); + class_defineMethod(self, "__init__(parent)", pika_lvgl_table___init__Method); + class_defineMethod(self, "set_cell_value(row,col,txt)", pika_lvgl_table_set_cell_valueMethod); return self; } @@ -2220,8 +2285,8 @@ void pika_lvgl_textarea_set_one_lineMethod(PikaObj *self, Args *args){ PikaObj *New_pika_lvgl_textarea(Args *args){ PikaObj *self = New_pika_lvgl_lv_obj(args); - class_defineMethod(self, "__init__(parent:lv_obj)", pika_lvgl_textarea___init__Method); - class_defineMethod(self, "set_one_line(en:int)", pika_lvgl_textarea_set_one_lineMethod); + class_defineMethod(self, "__init__(parent)", pika_lvgl_textarea___init__Method); + class_defineMethod(self, "set_one_line(en)", pika_lvgl_textarea_set_one_lineMethod); return self; } diff --git a/port/cmsis-pack/pikascript/pikascript-api/pikaScript.c b/port/cmsis-pack/pikascript/pikascript-api/pikaScript.c index 25a51f4ce..e420c03d7 100644 --- a/port/cmsis-pack/pikascript/pikascript-api/pikaScript.c +++ b/port/cmsis-pack/pikascript/pikascript-api/pikaScript.c @@ -17,7 +17,7 @@ PikaObj *__pikaMain; PikaObj *pikaScriptInit(void){ __platform_printf("======[pikascript packages installed]======\r\n"); pks_printVersion(); - __platform_printf("PikaStdLib==v1.10.6\r\n"); + __platform_printf("PikaStdLib==v1.11.0\r\n"); __platform_printf("pika_lvgl==v0.2.0\r\n"); __platform_printf("===========================================\r\n"); __pikaMain = newRootObj("pikaMain", New_PikaMain); diff --git a/port/cmsis-pack/pikascript/pikascript-core/PikaObj.c b/port/cmsis-pack/pikascript/pikascript-core/PikaObj.c index d8259368e..7848cbbce 100644 --- a/port/cmsis-pack/pikascript/pikascript-core/PikaObj.c +++ b/port/cmsis-pack/pikascript/pikascript-core/PikaObj.c @@ -47,12 +47,12 @@ static const uint64_t __talbe_fast_atoi[][10] = { {0, 1e9, 2e9, 3e9, 4e9, 5e9, 6e9, 7e9, 8e9, 9e9}, }; -int fast_atoi(char* src) { +int64_t fast_atoi(char* src) { const char* p = src; uint16_t size = strGetSize(src); p = p + size - 1; if (*p) { - int s = 0; + int64_t s = 0; const uint64_t* n = __talbe_fast_atoi[0]; while (p != src) { s += n[(*p - '0')]; @@ -192,13 +192,13 @@ int64_t obj_getInt(PikaObj* self, char* argPath) { return -999999999; } char* argName = strPointToLastToken(argPath, '.'); - int res = args_getInt(obj->list, argName); + int64_t res = args_getInt(obj->list, argName); return res; } Arg* obj_getArg(PikaObj* self, char* argPath) { PIKA_BOOL isClass = PIKA_FALSE; - PikaObj* obj = obj_getHostObjWithIsClass(self, argPath, &isClass); + PikaObj* obj = obj_getHostObjWithIsTemp(self, argPath, &isClass); if (NULL == obj) { return NULL; } @@ -434,10 +434,33 @@ exit: return res; } +PikaObj* _arg_to_obj(Arg* self, PIKA_BOOL* pIsTemp) { + if (argType_isObject(arg_getType(self))) { + return arg_getPtr(self); + } +#if !PIKA_NANO_ENABLE + if (arg_getType(self) == ARG_TYPE_STRING) { + PikaObj* New_PikaStdData_String(Args * args); + PikaObj* obj = newNormalObj(New_PikaStdData_String); + obj_setStr(obj, "str", arg_getStr(self)); + *pIsTemp = PIKA_TRUE; + return obj; + } + if (arg_getType(self) == ARG_TYPE_BYTES) { + PikaObj* New_PikaStdData_ByteArray(Args * args); + PikaObj* obj = newNormalObj(New_PikaStdData_ByteArray); + obj_setArg(obj, "raw", self); + *pIsTemp = PIKA_TRUE; + return obj; + } +#endif + return NULL; +} + static PikaObj* __obj_getObjDirect(PikaObj* self, char* name, - PIKA_BOOL* pIsClass) { - *pIsClass = PIKA_FALSE; + PIKA_BOOL* pIsTemp) { + *pIsTemp = PIKA_FALSE; if (NULL == self) { return NULL; } @@ -451,10 +474,11 @@ static PikaObj* __obj_getObjDirect(PikaObj* self, if (argType_isObject(type)) { return args_getPtr(self->list, name); } +#if !PIKA_NANO_ENABLE /* found class */ if (type == ARG_TYPE_METHOD_NATIVE_CONSTRUCTOR || type == ARG_TYPE_METHOD_CONSTRUCTOR) { - *pIsClass = PIKA_TRUE; + *pIsTemp = PIKA_TRUE; PikaObj* method_args_obj = New_TinyObj(NULL); Arg* cls_obj_arg = obj_runMethodArg(self, method_args_obj, args_getArg(self->list, name)); @@ -466,12 +490,13 @@ static PikaObj* __obj_getObjDirect(PikaObj* self, arg_deinit(cls_obj_arg); return res; } - return NULL; +#endif + return _arg_to_obj(args_getArg(self->list, name), pIsTemp); } static PikaObj* __obj_getObjWithKeepDeepth(PikaObj* self, char* objPath, - PIKA_BOOL* pIsClass, + PIKA_BOOL* pIsTemp, int32_t keepDeepth) { char objPath_buff[PIKA_PATH_BUFF_SIZE]; __platform_memcpy(objPath_buff, objPath, strGetSize(objPath) + 1); @@ -480,7 +505,7 @@ static PikaObj* __obj_getObjWithKeepDeepth(PikaObj* self, PikaObj* obj = self; for (int32_t i = 0; i < token_num - keepDeepth; i++) { char* token = strPopToken(token_buff, objPath_buff, '.'); - obj = __obj_getObjDirect(obj, token, pIsClass); + obj = __obj_getObjDirect(obj, token, pIsTemp); if (obj == NULL) { goto exit; } @@ -500,10 +525,10 @@ PikaObj* obj_getHostObj(PikaObj* self, char* objPath) { return __obj_getObjWithKeepDeepth(self, objPath, &isClass, 1); } -PikaObj* obj_getHostObjWithIsClass(PikaObj* self, - char* objPath, - PIKA_BOOL* pIsClass) { - return __obj_getObjWithKeepDeepth(self, objPath, pIsClass, 1); +PikaObj* obj_getHostObjWithIsTemp(PikaObj* self, + char* objPath, + PIKA_BOOL* pIsTemp) { + return __obj_getObjWithKeepDeepth(self, objPath, pIsTemp, 1); } Method methodArg_getPtr(Arg* method_arg) { @@ -588,18 +613,18 @@ static void obj_saveMethodInfo(PikaObj* self, MethodInfo* method_info) { } static int32_t __class_defineMethodWithType(PikaObj* self, - char* declearation, + char* declareation, Method method_ptr, ArgType method_type, PikaObj* def_context, ByteCodeFrame* bytecode_frame) { - int32_t size = strGetSize(declearation); + int32_t size = strGetSize(declareation); int32_t res = 0; Args buffs = {0}; - char* clean_declearation = - strDeleteChar(args_getBuff(&buffs, size), declearation, ' '); + char* clean_declareation = + strDeleteChar(args_getBuff(&buffs, size), declareation, ' '); char* method_path = - strGetFirstToken(args_getBuff(&buffs, size), clean_declearation, '('); + strGetFirstToken(args_getBuff(&buffs, size), clean_declareation, '('); PikaObj* method_host = obj_getHostObj(self, method_path); MethodInfo method_info = {0}; @@ -610,7 +635,7 @@ static int32_t __class_defineMethodWithType(PikaObj* self, goto exit; } method_name = strPointToLastToken(method_path, '.'); - method_info.dec = clean_declearation; + method_info.dec = clean_declareation; method_info.name = method_name; method_info.ptr = (void*)method_ptr; method_info.type = method_type; @@ -626,50 +651,50 @@ exit: /* define a constructor method */ int32_t class_defineConstructor(PikaObj* self, - char* declearation, + char* declareation, Method methodPtr) { - return __class_defineMethodWithType(self, declearation, methodPtr, + return __class_defineMethodWithType(self, declareation, methodPtr, ARG_TYPE_METHOD_NATIVE_CONSTRUCTOR, NULL, NULL); } /* define a native method as default */ int32_t class_defineMethod(PikaObj* self, - char* declearation, + char* declareation, Method methodPtr) { - return __class_defineMethodWithType(self, declearation, methodPtr, + return __class_defineMethodWithType(self, declareation, methodPtr, ARG_TYPE_METHOD_NATIVE, NULL, NULL); } /* define object method, object method is which startwith (self) */ int32_t class_defineRunTimeConstructor(PikaObj* self, - char* declearation, + char* declareation, Method methodPtr, PikaObj* def_context, ByteCodeFrame* bytecode_frame) { - return __class_defineMethodWithType(self, declearation, methodPtr, + return __class_defineMethodWithType(self, declareation, methodPtr, ARG_TYPE_METHOD_CONSTRUCTOR, def_context, bytecode_frame); } /* define object method, object method is which startwith (self) */ int32_t class_defineObjectMethod(PikaObj* self, - char* declearation, + char* declareation, Method methodPtr, PikaObj* def_context, ByteCodeFrame* bytecode_frame) { - return __class_defineMethodWithType(self, declearation, methodPtr, + return __class_defineMethodWithType(self, declareation, methodPtr, ARG_TYPE_METHOD_OBJECT, def_context, bytecode_frame); } /* define a static method as default */ int32_t class_defineStaticMethod(PikaObj* self, - char* declearation, + char* declareation, Method methodPtr, PikaObj* def_context, ByteCodeFrame* bytecode_frame) { - return __class_defineMethodWithType(self, declearation, methodPtr, + return __class_defineMethodWithType(self, declareation, methodPtr, ARG_TYPE_METHOD_STATIC, def_context, bytecode_frame); } @@ -930,7 +955,7 @@ void method_returnStr(Args* args, char* val) { args_setStr(args, "return", val); } -void method_returnInt(Args* args, int32_t val) { +void method_returnInt(Args* args, int64_t val) { args_setInt(args, "return", val); } @@ -954,7 +979,7 @@ void method_returnArg(Args* args, Arg* arg) { args_setArg(args, arg); } -int32_t method_getInt(Args* args, char* argName) { +int64_t method_getInt(Args* args, char* argName) { return args_getInt(args, argName); } @@ -975,11 +1000,16 @@ PikaObj* New_PikaObj(void) { return self; } -Arg* arg_setRef(Arg* self, char* name, PikaObj* obj) { - obj_refcntInc(obj); +Arg* arg_setObj(Arg* self, char* name, PikaObj* obj) { return arg_setPtr(self, name, ARG_TYPE_OBJECT, obj); } +Arg* arg_setRef(Arg* self, char* name, PikaObj* obj) { + pika_assert(NULL != obj); + obj_refcntInc(obj); + return arg_setObj(self, name, obj); +} + int32_t obj_newDirectObj(PikaObj* self, char* objName, NewFun newFunPtr) { Arg* new_obj = arg_newDirectObj(newFunPtr); new_obj = arg_setName(new_obj, objName); @@ -1181,9 +1211,9 @@ void pks_printVersion(void) { PIKA_VERSION_MINOR, PIKA_VERSION_MICRO, PIKA_EDIT_TIME); } -void pks_getVersion(char *buff) -{ - __platform_sprintf(buff, "%d.%d.%d", PIKA_VERSION_MAJOR, PIKA_VERSION_MINOR, PIKA_VERSION_MICRO); +void pks_getVersion(char* buff) { + __platform_sprintf(buff, "%d.%d.%d", PIKA_VERSION_MAJOR, PIKA_VERSION_MINOR, + PIKA_VERSION_MICRO); } void* obj_getStruct(PikaObj* self, char* name) { diff --git a/port/cmsis-pack/pikascript/pikascript-core/PikaObj.h b/port/cmsis-pack/pikascript/pikascript-core/PikaObj.h index 1e03e284a..a852f55a3 100644 --- a/port/cmsis-pack/pikascript/pikascript-core/PikaObj.h +++ b/port/cmsis-pack/pikascript/pikascript-core/PikaObj.h @@ -127,7 +127,7 @@ int32_t obj_load(PikaObj* self, Args* args, char* name); int32_t obj_addOther(PikaObj* self, char* subObjectName, void* new_projcetFun); PikaObj* obj_getObj(PikaObj* self, char* objPath); PikaObj* obj_getHostObj(PikaObj* self, char* objPath); -PikaObj* obj_getHostObjWithIsClass(PikaObj* self, +PikaObj* obj_getHostObjWithIsTemp(PikaObj* self, char* objPath, PIKA_BOOL* pIsClass); @@ -135,26 +135,26 @@ PikaObj* obj_getHostObjWithIsClass(PikaObj* self, int32_t obj_freeObj(PikaObj* self, char* subObjectName); /* method */ -int32_t class_defineMethod(PikaObj* self, char* declearation, Method methodPtr); +int32_t class_defineMethod(PikaObj* self, char* declareation, Method methodPtr); int32_t class_defineObjectMethod(PikaObj* self, - char* declearation, + char* declareation, Method methodPtr, PikaObj* def_context, ByteCodeFrame* bytecode_frame); int32_t class_defineStaticMethod(PikaObj* self, - char* declearation, + char* declareation, Method methodPtr, PikaObj* def_context, ByteCodeFrame* bytecode_frame); int32_t class_defineConstructor(PikaObj* self, - char* declearation, + char* declareation, Method methodPtr); int32_t class_defineRunTimeConstructor(PikaObj* self, - char* declearation, + char* declareation, Method methodPtr, PikaObj* def_context, ByteCodeFrame* bytecode_frame); @@ -182,11 +182,11 @@ uint8_t obj_getAnyArg(PikaObj* self, Args* targetArgs); void method_returnStr(Args* args, char* val); -void method_returnInt(Args* args, int32_t val); +void method_returnInt(Args* args, int64_t val); void method_returnFloat(Args* args, double val); void method_returnPtr(Args* args, void* val); void method_returnObj(Args* args, void* val); -int32_t method_getInt(Args* args, char* argName); +int64_t method_getInt(Args* args, char* argName); double method_getFloat(Args* args, char* argName); char* method_getStr(Args* args, char* argName); void method_returnArg(Args* args, Arg* arg); @@ -202,7 +202,7 @@ VMParameters* obj_runDirect(PikaObj* self, char* cmd); PikaObj* New_PikaObj(void); /* tools */ -int fast_atoi(char* src); +int64_t fast_atoi(char* src); char* fast_itoa(char* buf, uint32_t val); /* shell */ @@ -231,10 +231,10 @@ Arg* obj_newObjInPackage(NewFun newObjFun); PikaObj* newNormalObj(NewFun newObjFun); Arg* arg_setRef(Arg* self, char* name, PikaObj* obj); -Arg* arg_setWeakRef(Arg* self, char* name, PikaObj* obj); +Arg* arg_setObj(Arg* self, char* name, PikaObj* obj); +#define arg_newObj(obj) arg_setObj(NULL, "", (obj)) #define arg_newRef(obj) arg_setRef(NULL, "", (obj)) -#define arg_newWeakRef(obj) arg_setWeakRef(NULL, "", (obj)) PikaObj* obj_importModuleWithByteCodeFrame(PikaObj* self, char* name, @@ -307,6 +307,8 @@ void* obj_getStruct(PikaObj* self, char* name); while (1) char* obj_cacheStr(PikaObj* self, char* str); +PikaObj* _arg_to_obj(Arg* self, PIKA_BOOL* pIsTemp); +char* __printBytes(PikaObj* self, Arg* arg); #define PIKASCRIPT_VERSION_TO_NUM(majer, minor, micro) \ majer * 100 * 100 + minor * 100 + micro diff --git a/port/cmsis-pack/pikascript/pikascript-core/PikaParser.c b/port/cmsis-pack/pikascript/pikascript-core/PikaParser.c index e902b4f2a..953fb86d2 100644 --- a/port/cmsis-pack/pikascript/pikascript-core/PikaParser.c +++ b/port/cmsis-pack/pikascript/pikascript-core/PikaParser.c @@ -35,19 +35,20 @@ /* local head */ typedef QueueObj AST; -char* AST_toPikaASM(AST* ast, Args* outBuffs); +char* AST_genAsm(AST* ast, Args* outBuffs); char* Lexer_parseLine(Args* outBuffs, char* stmt); int32_t AST_deinit(AST* ast); char* Parser_linesToAsm(Args* outBuffs, char* multiLine); uint8_t Parser_isContainToken(char* tokens, enum TokenType token_type, char* pyload); -void Cursor_init(struct Cursor* ps); -void Cursor_parse(struct Cursor* ps, char* stmt); -void Cursor_deinit(struct Cursor* ps); -void Cursor_beforeIter(struct Cursor* ps); -void Cursor_iterStart(struct Cursor* ps); -void Cursor_iterEnd(struct Cursor* ps); +void Cursor_init(struct Cursor* cs); +void Cursor_parse(struct Cursor* cs, char* stmt); +void Cursor_deinit(struct Cursor* cs); +void Cursor_beforeIter(struct Cursor* cs); +void Cursor_iterStart(struct Cursor* cs); +void Cursor_iterEnd(struct Cursor* cs); +char* Cursor_popToken(Args* buffs, char** tokens, char* devide); char* Parser_popToken(Args* buffs_p, char* tokens); uint16_t Tokens_getSize(char* tokens) { @@ -65,27 +66,27 @@ char* strsPopTokenWithSkip_byStr(Args* outBuffs, uint8_t divider_index = 0; Arg* keeped_arg = arg_newStr(""); Arg* poped_arg = arg_newStr(""); - Cursor_forEachToken(ps, stmts) { - Cursor_iterStart(&ps); - if (ps.branket_deepth == 0) { - if (strEqu(str, ps.token1.pyload)) { - divider_index = ps.iter_index; + Cursor_forEachToken(cs, stmts) { + Cursor_iterStart(&cs); + if (cs.branket_deepth == 0) { + if (strEqu(str, cs.token1.pyload)) { + divider_index = cs.iter_index; } } - Cursor_iterEnd(&ps); + Cursor_iterEnd(&cs); } - Cursor_deinit(&ps); - Cursor_forEachTokenExistPs(ps, stmts) { - Cursor_iterStart(&ps); - if (ps.iter_index < divider_index) { - poped_arg = arg_strAppend(poped_arg, ps.token1.pyload); + Cursor_deinit(&cs); + Cursor_forEachTokenExistPs(cs, stmts) { + Cursor_iterStart(&cs); + if (cs.iter_index < divider_index) { + poped_arg = arg_strAppend(poped_arg, cs.token1.pyload); } - if (ps.iter_index > divider_index) { - keeped_arg = arg_strAppend(keeped_arg, ps.token1.pyload); + if (cs.iter_index > divider_index) { + keeped_arg = arg_strAppend(keeped_arg, cs.token1.pyload); } - Cursor_iterEnd(&ps); + Cursor_iterEnd(&cs); } - Cursor_deinit(&ps); + Cursor_deinit(&cs); char* keeped = arg_getStr(keeped_arg); char* poped = strsCopy(outBuffs, arg_getStr(poped_arg)); __platform_memcpy(stmts, keeped, strGetSize(keeped) + 1); @@ -100,15 +101,15 @@ char* strsGetCleanCmd(Args* outBuffs, char* cmd) { /* lexer may generate more chars than input */ char* strOut = args_getBuff(outBuffs, size * 2); int32_t iOut = 0; - Cursor_forEachToken(ps, cmd) { - Cursor_iterStart(&ps); - for (uint16_t k = 0; k < strGetSize(ps.token1.pyload); k++) { - strOut[iOut] = ps.token1.pyload[k]; + Cursor_forEachToken(cs, cmd) { + Cursor_iterStart(&cs); + for (uint16_t k = 0; k < strGetSize(cs.token1.pyload); k++) { + strOut[iOut] = cs.token1.pyload[k]; iOut++; } - Cursor_iterEnd(&ps); + Cursor_iterEnd(&cs); } - Cursor_deinit(&ps); + Cursor_deinit(&cs); /* add \0 */ strOut[iOut] = 0; return strOut; @@ -156,23 +157,23 @@ static char* __removeTokensBetween(Args* outBuffs, Args buffs = {0}; uint8_t block_deepth = 0; char* output = ""; - Cursor_forEachToken(ps, input) { - Cursor_iterStart(&ps); - if (strEqu(token_pyload1, ps.token1.pyload)) { + Cursor_forEachToken(cs, input) { + Cursor_iterStart(&cs); + if (strEqu(token_pyload1, cs.token1.pyload)) { if (block_deepth == 0) { - output = strsAppend(&buffs, output, ps.token1.pyload); + output = strsAppend(&buffs, output, cs.token1.pyload); } block_deepth++; } - if (strEqu(token_pyload2, ps.token1.pyload)) { + if (strEqu(token_pyload2, cs.token1.pyload)) { block_deepth--; } if (block_deepth == 0) { - output = strsAppend(&buffs, output, ps.token1.pyload); + output = strsAppend(&buffs, output, cs.token1.pyload); } - Cursor_iterEnd(&ps); + Cursor_iterEnd(&cs); } - Cursor_deinit(&ps); + Cursor_deinit(&cs); output = strsCopy(outBuffs, output); strsDeinit(&buffs); return output; @@ -198,59 +199,64 @@ static enum StmtType Lexer_matchStmtType(char* right) { PIKA_BOOL is_get_dict = PIKA_FALSE; PIKA_BOOL is_get_import = PIKA_FALSE; PIKA_BOOL is_get_chain = PIKA_FALSE; - Cursor_forEachToken(ps, rightWithoutSubStmt) { - Cursor_iterStart(&ps); + Cursor_forEachToken(cs, rightWithoutSubStmt) { + Cursor_iterStart(&cs); /* collect type */ - if (strEqu(ps.token1.pyload, " import ")) { + if (strEqu(cs.token1.pyload, " import ")) { is_get_import = PIKA_TRUE; goto iter_continue; } - if (strEqu(ps.token2.pyload, "[")) { + if (strEqu(cs.token2.pyload, "[")) { /* (symble | iteral | <]> | <)>) + <[> */ - if (TOKEN_symbol == ps.token1.type || - TOKEN_literal == ps.token1.type || - strEqu(ps.token1.pyload, "]") || - strEqu(ps.token1.pyload, ")")) { + if (TOKEN_symbol == cs.token1.type || + TOKEN_literal == cs.token1.type || + strEqu(cs.token1.pyload, "]") || + strEqu(cs.token1.pyload, ")")) { is_get_slice = PIKA_TRUE; goto iter_continue; } /* ( <,> | <=> ) + <[> */ is_get_list = PIKA_TRUE; } - if (strEqu(ps.token1.pyload, "[") && ps.iter_index == 1) { + if (strEqu(cs.token1.pyload, "[") && cs.iter_index == 1) { /* VOID + <[> */ is_get_list = PIKA_TRUE; goto iter_continue; } - if (strEqu(ps.token1.pyload, "...")) { + if (strEqu(cs.token1.pyload, "...")) { goto iter_continue; } - if (strIsStartWith(ps.token1.pyload, ".")) { - if (ps.iter_index != 1) { + + if (strEqu(cs.token1.pyload, "pass")) { + goto iter_continue; + } + + if (strIsStartWith(cs.token1.pyload, ".")) { + if (cs.iter_index != 1) { is_get_chain = PIKA_TRUE; goto iter_continue; } } - if (strEqu(ps.token1.pyload, "{")) { + if (strEqu(cs.token1.pyload, "{")) { is_get_dict = PIKA_TRUE; goto iter_continue; } - if (ps.token1.type == TOKEN_operator) { + if (cs.token1.type == TOKEN_operator) { is_get_operator = PIKA_TRUE; goto iter_continue; } /* <(> */ - if (strEqu(ps.token1.pyload, "(")) { + if (strEqu(cs.token1.pyload, "(")) { is_get_method = PIKA_TRUE; goto iter_continue; } - if (ps.token1.type == TOKEN_literal) { - if (ps.token1.pyload[0] == '\'' || ps.token1.pyload[0] == '"') { + if (cs.token1.type == TOKEN_literal) { + if (cs.token1.pyload[0] == '\'' || cs.token1.pyload[0] == '"') { is_get_string = PIKA_TRUE; goto iter_continue; } - if (ps.token1.pyload[1] == '\'' || ps.token1.pyload[1] == '"') { - if (ps.token1.pyload[0] == 'b') { + if (cs.token1.pyload[1] == '\'' || cs.token1.pyload[1] == '"') { + if (cs.token1.pyload[0] == 'b') { is_get_bytes = PIKA_TRUE; goto iter_continue; } @@ -258,12 +264,12 @@ static enum StmtType Lexer_matchStmtType(char* right) { is_get_number = PIKA_TRUE; goto iter_continue; } - if (ps.token1.type == TOKEN_symbol) { + if (cs.token1.type == TOKEN_symbol) { is_get_symbol = PIKA_TRUE; goto iter_continue; } iter_continue: - Cursor_iterEnd(&ps); + Cursor_iterEnd(&cs); } if (is_get_import) { stmtType = STMT_import; @@ -310,7 +316,7 @@ static enum StmtType Lexer_matchStmtType(char* right) { goto exit; } exit: - Cursor_deinit(&ps); + Cursor_deinit(&cs); strsDeinit(&buffs); return stmtType; } @@ -775,49 +781,49 @@ char* Lexer_getOperator(Args* outBuffs, char* stmt) { // use parse state foreach to get operator for (uint32_t i = 0; i < sizeof(operators) / 9; i++) { - Cursor_forEachToken(ps, tokens) { - Cursor_iterStart(&ps); + Cursor_forEachToken(cs, tokens) { + Cursor_iterStart(&cs); // get operator - if (strEqu(ps.token2.pyload, (char*)operators[i])) { + if (strEqu(cs.token2.pyload, (char*)operators[i])) { // solve the iuuse of "~-1" operator= strsCopy(&buffs, (char*)operators[i]); - Cursor_iterEnd(&ps); + Cursor_iterEnd(&cs); break; } - Cursor_iterEnd(&ps); + Cursor_iterEnd(&cs); }; - Cursor_deinit(&ps); + Cursor_deinit(&cs); } /* solve the iuuse of "~-1" */ if (strEqu(operator, "-")) { - Cursor_forEachToken(ps, stmt) { - Cursor_iterStart(&ps); - if (strEqu(ps.token2.pyload, "-")) { - if (ps.token1.type == TOKEN_operator) { - operator= strsCopy(&buffs, ps.token1.pyload); - Cursor_iterEnd(&ps); + Cursor_forEachToken(cs, stmt) { + Cursor_iterStart(&cs); + if (strEqu(cs.token2.pyload, "-")) { + if (cs.token1.type == TOKEN_operator) { + operator= strsCopy(&buffs, cs.token1.pyload); + Cursor_iterEnd(&cs); break; } } - Cursor_iterEnd(&ps); + Cursor_iterEnd(&cs); }; - Cursor_deinit(&ps); + Cursor_deinit(&cs); } /* match the last operator in equal level */ if ((strEqu(operator, "+")) || (strEqu(operator, "-"))) { - Cursor_forEachToken(ps, stmt) { - Cursor_iterStart(&ps); - if (strEqu(ps.token1.pyload, "+")) { + Cursor_forEachToken(cs, stmt) { + Cursor_iterStart(&cs); + if (strEqu(cs.token1.pyload, "+")) { operator= strsCopy(&buffs, "+"); } - if (strEqu(ps.token1.pyload, "-")) { + if (strEqu(cs.token1.pyload, "-")) { operator= strsCopy(&buffs, "-"); } - Cursor_iterEnd(&ps); + Cursor_iterEnd(&cs); } - Cursor_deinit(&ps); + Cursor_deinit(&cs); } /* out put */ if (NULL == operator) { @@ -839,36 +845,36 @@ void LexToken_update(struct LexToken* lex_token) { } } -void Cursor_iterStart(struct Cursor* ps) { - ps->iter_index++; - ps->iter_buffs = New_strBuff(); +void Cursor_iterStart(struct Cursor* cs) { + cs->iter_index++; + cs->iter_buffs = New_strBuff(); /* token1 is the last token */ - ps->token1.token = strsCopy(ps->iter_buffs, arg_getStr(ps->last_token)); + cs->token1.token = strsCopy(cs->iter_buffs, arg_getStr(cs->last_token)); /* token2 is the next token */ - ps->token2.token = Parser_popToken(ps->iter_buffs, ps->tokens); + cs->token2.token = Parser_popToken(cs->iter_buffs, cs->tokens); /* store last token */ - arg_deinit(ps->last_token); - ps->last_token = arg_newStr(ps->token2.token); + arg_deinit(cs->last_token); + cs->last_token = arg_newStr(cs->token2.token); - LexToken_update(&ps->token1); - LexToken_update(&ps->token2); - if (strEqu(ps->token1.pyload, "(")) { - ps->branket_deepth++; + LexToken_update(&cs->token1); + LexToken_update(&cs->token2); + if (strEqu(cs->token1.pyload, "(")) { + cs->branket_deepth++; } - if (strEqu(ps->token1.pyload, ")")) { - ps->branket_deepth--; + if (strEqu(cs->token1.pyload, ")")) { + cs->branket_deepth--; } - if (strEqu(ps->token1.pyload, "[")) { - ps->branket_deepth++; + if (strEqu(cs->token1.pyload, "[")) { + cs->branket_deepth++; } - if (strEqu(ps->token1.pyload, "]")) { - ps->branket_deepth--; + if (strEqu(cs->token1.pyload, "]")) { + cs->branket_deepth--; } - if (strEqu(ps->token1.pyload, "{")) { - ps->branket_deepth++; + if (strEqu(cs->token1.pyload, "{")) { + cs->branket_deepth++; } - if (strEqu(ps->token1.pyload, "}")) { - ps->branket_deepth--; + if (strEqu(cs->token1.pyload, "}")) { + cs->branket_deepth--; } } @@ -878,50 +884,81 @@ void LexToken_init(struct LexToken* lt) { lt->type = TOKEN_strEnd; } -void Cursor_init(struct Cursor* ps) { - ps->tokens = NULL; - ps->length = 0; - ps->iter_index = 0; - ps->branket_deepth = 0; - ps->last_token = NULL; - ps->iter_buffs = NULL; - ps->buffs_p = New_strBuff(); - ps->result = PIKA_RES_OK; - LexToken_init(&ps->token1); - LexToken_init(&ps->token2); +void Cursor_init(struct Cursor* cs) { + cs->tokens = NULL; + cs->length = 0; + cs->iter_index = 0; + cs->branket_deepth = 0; + cs->last_token = NULL; + cs->iter_buffs = NULL; + cs->buffs_p = New_strBuff(); + cs->result = PIKA_RES_OK; + LexToken_init(&cs->token1); + LexToken_init(&cs->token2); } -void Cursor_iterEnd(struct Cursor* ps) { - args_deinit(ps->iter_buffs); +void Cursor_iterEnd(struct Cursor* cs) { + args_deinit(cs->iter_buffs); } -void Cursor_deinit(struct Cursor* ps) { - if (NULL != ps->last_token) { - arg_deinit(ps->last_token); +void Cursor_deinit(struct Cursor* cs) { + if (NULL != cs->last_token) { + arg_deinit(cs->last_token); } - args_deinit(ps->buffs_p); + args_deinit(cs->buffs_p); } -void Cursor_parse(struct Cursor* ps, char* stmt) { +void Cursor_parse(struct Cursor* cs, char* stmt) { if (NULL == stmt) { - ps->result = PIKA_RES_ERR_SYNTAX_ERROR; + cs->result = PIKA_RES_ERR_SYNTAX_ERROR; return; } - ps->tokens = Lexer_parseLine(ps->buffs_p, stmt); - if (NULL == ps->tokens) { - ps->result = PIKA_RES_ERR_SYNTAX_ERROR; + cs->tokens = Lexer_parseLine(cs->buffs_p, stmt); + if (NULL == cs->tokens) { + cs->result = PIKA_RES_ERR_SYNTAX_ERROR; return; } - ps->length = Tokens_getSize(ps->tokens); + cs->length = Tokens_getSize(cs->tokens); } -void Cursor_beforeIter(struct Cursor* ps) { +void Cursor_beforeIter(struct Cursor* cs) { /* clear first token */ - if (ps->result != PIKA_RES_OK) { + if (cs->result != PIKA_RES_OK) { return; } - Parser_popToken(ps->buffs_p, ps->tokens); - ps->last_token = arg_newStr(Parser_popToken(ps->buffs_p, ps->tokens)); + Parser_popToken(cs->buffs_p, cs->tokens); + cs->last_token = arg_newStr(Parser_popToken(cs->buffs_p, cs->tokens)); +} + +char* Cursor_popToken(Args* buffs, char** tokens, char* devide) { + Arg* out_item = arg_newStr(""); + Arg* tokens_after = arg_newStr(""); + PIKA_BOOL is_find_devide = PIKA_FALSE; + Cursor_forEachToken(cs, *tokens) { + Cursor_iterStart(&cs); + if ((cs.branket_deepth == 0 && strEqu(cs.token1.pyload, devide)) || + cs.iter_index == cs.length) { + is_find_devide = PIKA_TRUE; + Cursor_iterEnd(&cs); + continue; + } + if (!is_find_devide) { + out_item = arg_strAppend(out_item, cs.token1.pyload); + } else { + tokens_after = arg_strAppend(tokens_after, cs.token1.pyload); + } + Cursor_iterEnd(&cs); + } + Cursor_deinit(&cs); + /* cache out item */ + char* out_item_str = strsCopy(buffs, arg_getStr(out_item)); + arg_deinit(out_item); + /* cache tokens after */ + char* token_after_str = strsCopy(buffs, arg_getStr(tokens_after)); + arg_deinit(tokens_after); + /* update tokens */ + *tokens = token_after_str; + return out_item_str; } static void Slice_getPars(Args* outBuffs, @@ -936,25 +973,25 @@ static void Slice_getPars(Args* outBuffs, /* slice */ uint8_t colon_i = 0; - Cursor_forEachToken(ps, inner) { - Cursor_iterStart(&ps); - if (strEqu(ps.token1.pyload, ":") && ps.branket_deepth == 0) { + Cursor_forEachToken(cs, inner) { + Cursor_iterStart(&cs); + if (strEqu(cs.token1.pyload, ":") && cs.branket_deepth == 0) { colon_i++; goto iter_continue1; } if (colon_i == 0) { - *pStart = strsAppend(&buffs, *pStart, ps.token1.pyload); + *pStart = strsAppend(&buffs, *pStart, cs.token1.pyload); } if (colon_i == 1) { - *pEnd = strsAppend(&buffs, *pEnd, ps.token1.pyload); + *pEnd = strsAppend(&buffs, *pEnd, cs.token1.pyload); } if (colon_i == 2) { - *pStep = strsAppend(&buffs, *pStep, ps.token1.pyload); + *pStep = strsAppend(&buffs, *pStep, cs.token1.pyload); } iter_continue1: - Cursor_iterEnd(&ps); + Cursor_iterEnd(&cs); } - Cursor_deinit(&ps); + Cursor_deinit(&cs); if (colon_i == 1) { *pStep = "1"; if (strEqu(*pStart, "")) { @@ -979,7 +1016,7 @@ static void Slice_getPars(Args* outBuffs, strsDeinit(&buffs); } -char* Suger_solveLeftSlice(Args* outBuffs, char* right, char** left_p) { +char* Suger_leftSlice(Args* outBuffs, char* right, char** left_p) { #if !PIKA_SYNTAX_SLICE_ENABLE return right; #endif @@ -1000,19 +1037,19 @@ char* Suger_solveLeftSlice(Args* outBuffs, char* right, char** left_p) { /* exit when not match (symble|iteral)'[' */ - Cursor_forEachToken(ps, left) { - Cursor_iterStart(&ps); - if (strEqu(ps.token2.pyload, "[")) { - if (TOKEN_symbol == ps.token1.type || - TOKEN_literal == ps.token1.type) { + Cursor_forEachToken(cs, left) { + Cursor_iterStart(&cs); + if (strEqu(cs.token2.pyload, "[")) { + if (TOKEN_symbol == cs.token1.type || + TOKEN_literal == cs.token1.type) { matched = 1; - Cursor_iterEnd(&ps); + Cursor_iterEnd(&cs); break; } } - Cursor_iterEnd(&ps); + Cursor_iterEnd(&cs); } - Cursor_deinit(&ps); + Cursor_deinit(&cs); if (!matched) { /* not contain '[', return origin */ arg_deinit(right_arg); @@ -1021,21 +1058,21 @@ char* Suger_solveLeftSlice(Args* outBuffs, char* right, char** left_p) { } /* matched [] */ - Cursor_forEachTokenExistPs(ps, left) { - Cursor_iterStart(&ps); + Cursor_forEachTokenExistPs(cs, left) { + Cursor_iterStart(&cs); /* found '[' */ - if ((TOKEN_devider == ps.token2.type) && - (strEqu(ps.token2.pyload, "["))) { + if ((TOKEN_devider == cs.token2.type) && + (strEqu(cs.token2.pyload, "["))) { /* get 'obj' from obj[] */ - args_setStr(&buffs, "obj", ps.token1.pyload); + args_setStr(&buffs, "obj", cs.token1.pyload); is_in_brancket = 1; /* fond ']' */ - } else if ((TOKEN_devider == ps.token2.type) && - (strEqu(ps.token2.pyload, "]"))) { + } else if ((TOKEN_devider == cs.token2.type) && + (strEqu(cs.token2.pyload, "]"))) { is_in_brancket = 0; char* inner = args_getStr(&buffs, "inner"); Arg* inner_arg = arg_newStr(inner); - inner_arg = arg_strAppend(inner_arg, ps.token1.pyload); + inner_arg = arg_strAppend(inner_arg, cs.token1.pyload); args_setStr(&buffs, "inner", arg_getStr(inner_arg)); arg_deinit(inner_arg); /* update inner pointer */ @@ -1055,21 +1092,21 @@ char* Suger_solveLeftSlice(Args* outBuffs, char* right, char** left_p) { /* clean the inner */ args_setStr(&buffs, "inner", ""); /* in brancket and found '[' */ - } else if (is_in_brancket && (!strEqu(ps.token1.pyload, "["))) { + } else if (is_in_brancket && (!strEqu(cs.token1.pyload, "["))) { char* inner = args_getStr(&buffs, "inner"); Arg* index_arg = arg_newStr(inner); - index_arg = arg_strAppend(index_arg, ps.token1.pyload); + index_arg = arg_strAppend(index_arg, cs.token1.pyload); args_setStr(&buffs, "inner", arg_getStr(index_arg)); arg_deinit(index_arg); /* out of brancket and not found ']' */ - } else if (!is_in_brancket && (!strEqu(ps.token1.pyload, "]"))) { - if (TOKEN_strEnd != ps.token1.type) { - right_arg = arg_strAppend(right_arg, ps.token1.pyload); + } else if (!is_in_brancket && (!strEqu(cs.token1.pyload, "]"))) { + if (TOKEN_strEnd != cs.token1.type) { + right_arg = arg_strAppend(right_arg, cs.token1.pyload); } } - Cursor_iterEnd(&ps); + Cursor_iterEnd(&cs); } - Cursor_deinit(&ps); + Cursor_deinit(&cs); /* clean the left */ for (size_t i = 0; i < strGetSize(left); i++) { if (left[i] == '[') { @@ -1085,7 +1122,7 @@ exit: return right_res; } -char* Suger_solveFormat(Args* outBuffs, char* right) { +char* Suger_format(Args* outBuffs, char* right) { #if !PIKA_SYNTAX_FORMAT_ENABLE return right; #endif @@ -1115,45 +1152,45 @@ char* Suger_solveFormat(Args* outBuffs, char* right) { PIKA_BOOL is_out_vars = PIKA_FALSE; Args buffs = {0}; char* fmt = NULL; - Cursor_forEachToken(ps, right) { + Cursor_forEachToken(cs, right) { char* item = ""; - Cursor_iterStart(&ps); + Cursor_iterStart(&cs); if (PIKA_FALSE == is_in_format) { - if (ps.token1.type != TOKEN_literal) { - item = ps.token1.pyload; + if (cs.token1.type != TOKEN_literal) { + item = cs.token1.pyload; goto iter_continue; } - if (ps.token1.pyload[0] != '\'' && ps.token1.pyload[0] != '"') { - item = ps.token1.pyload; + if (cs.token1.pyload[0] != '\'' && cs.token1.pyload[0] != '"') { + item = cs.token1.pyload; goto iter_continue; } - if (!strEqu(ps.token2.pyload, "%")) { - item = ps.token1.pyload; + if (!strEqu(cs.token2.pyload, "%")) { + item = cs.token1.pyload; goto iter_continue; } /* found the format stmt */ is_in_format = PIKA_TRUE; - fmt = strsCopy(&buffs, ps.token1.pyload); + fmt = strsCopy(&buffs, cs.token1.pyload); goto iter_continue; } if (PIKA_TRUE == is_in_format) { /* check the format vars */ - if (strEqu(ps.token1.pyload, "%")) { + if (strEqu(cs.token1.pyload, "%")) { /* is a tuple */ - if (strEqu(ps.token2.pyload, "(")) { + if (strEqu(cs.token2.pyload, "(")) { is_tuple = PIKA_TRUE; } else { - var_buf = arg_strAppend(var_buf, ps.token2.pyload); + var_buf = arg_strAppend(var_buf, cs.token2.pyload); } goto iter_continue; } /* found the end of tuple */ - if (ps.iter_index == ps.length) { + if (cs.iter_index == cs.length) { is_out_vars = PIKA_TRUE; is_in_format = PIKA_FALSE; } else { /* push the vars inner the tuple */ - var_buf = arg_strAppend(var_buf, ps.token2.pyload); + var_buf = arg_strAppend(var_buf, cs.token2.pyload); } if (is_out_vars) { if (is_tuple) { @@ -1174,9 +1211,9 @@ char* Suger_solveFormat(Args* outBuffs, char* right) { if (!is_in_format) { str_buf = arg_strAppend(str_buf, item); } - Cursor_iterEnd(&ps); + Cursor_iterEnd(&cs); } - Cursor_deinit(&ps); + Cursor_deinit(&cs); res = strsCopy(outBuffs, arg_getStr(str_buf)); arg_deinit(str_buf); @@ -1185,10 +1222,10 @@ char* Suger_solveFormat(Args* outBuffs, char* right) { return res; } -uint8_t Parser_solveSelfOperator(Args* outbuffs, - char* stmt, - char** right_p, - char** left_p) { +uint8_t Suger_selfOperator(Args* outbuffs, + char* stmt, + char** right_p, + char** left_p) { char* left_new = NULL; char* right_new = NULL; Arg* left_arg = arg_newStr(""); @@ -1219,24 +1256,24 @@ uint8_t Parser_solveSelfOperator(Args* outbuffs, } /* found self operator */ is_left_exist = 1; - Cursor_forEachToken(ps, stmt) { - Cursor_iterStart(&ps); - if ((strEqu(ps.token1.pyload, "*=")) || - (strEqu(ps.token1.pyload, "/=")) || - (strEqu(ps.token1.pyload, "+=")) || - (strEqu(ps.token1.pyload, "-="))) { + Cursor_forEachToken(cs, stmt) { + Cursor_iterStart(&cs); + if ((strEqu(cs.token1.pyload, "*=")) || + (strEqu(cs.token1.pyload, "/=")) || + (strEqu(cs.token1.pyload, "+=")) || + (strEqu(cs.token1.pyload, "-="))) { is_right = 1; goto iter_continue; } if (!is_right) { - left_arg = arg_strAppend(left_arg, ps.token1.pyload); + left_arg = arg_strAppend(left_arg, cs.token1.pyload); } else { - right_arg = arg_strAppend(right_arg, ps.token1.pyload); + right_arg = arg_strAppend(right_arg, cs.token1.pyload); } iter_continue: - Cursor_iterEnd(&ps); + Cursor_iterEnd(&cs); } - Cursor_deinit(&ps); + Cursor_deinit(&cs); /* connect right */ right_arg_new = arg_strAppend(right_arg_new, arg_getStr(left_arg)); right_arg_new = arg_strAppend(right_arg_new, operator); @@ -1263,8 +1300,20 @@ exit: return is_left_exist; } -PIKA_RES AST_setThisNode(AST* ast, char* node_type, char* node_content) { - return obj_setStr(ast, node_type, node_content); +PIKA_RES AST_setNodeAttr(AST* ast, char* attr_name, char* attr_val) { + return obj_setStr(ast, attr_name, attr_val); +} + +char* AST_getNodeAttr(AST* ast, char* attr_name) { + return obj_getStr(ast, attr_name); +} + +PIKA_RES AST_setNodeBlock(AST* ast, char* node_content) { + return AST_setNodeAttr(ast, "block", node_content); +} + +char* AST_getThisBlock(AST* ast) { + return obj_getStr(ast, "block"); } AST* AST_parseStmt(AST* ast, char* stmt); @@ -1280,31 +1329,31 @@ char* Parser_popSubStmt(Args* outbuffs, char** stmt_p, char* delimiter) { char* stmt = *stmt_p; PIKA_BOOL is_get_substmt = PIKA_FALSE; Args buffs = {0}; - Cursor_forEachToken(ps, stmt) { - Cursor_iterStart(&ps); + Cursor_forEachToken(cs, stmt) { + Cursor_iterStart(&cs); if (is_get_substmt) { /* get new stmt */ - newstmt_arg = arg_strAppend(newstmt_arg, ps.token1.pyload); - Cursor_iterEnd(&ps); + newstmt_arg = arg_strAppend(newstmt_arg, cs.token1.pyload); + Cursor_iterEnd(&cs); continue; } - if (ps.branket_deepth > 0) { + if (cs.branket_deepth > 0) { /* ignore */ - substmt_arg = arg_strAppend(substmt_arg, ps.token1.pyload); - Cursor_iterEnd(&ps); + substmt_arg = arg_strAppend(substmt_arg, cs.token1.pyload); + Cursor_iterEnd(&cs); continue; } - if (strEqu(ps.token1.pyload, delimiter)) { + if (strEqu(cs.token1.pyload, delimiter)) { /* found delimiter */ is_get_substmt = PIKA_TRUE; - Cursor_iterEnd(&ps); + Cursor_iterEnd(&cs); continue; } /* collect substmt */ - substmt_arg = arg_strAppend(substmt_arg, ps.token1.pyload); - Cursor_iterEnd(&ps); + substmt_arg = arg_strAppend(substmt_arg, cs.token1.pyload); + Cursor_iterEnd(&cs); } - Cursor_deinit(&ps); + Cursor_deinit(&cs); strsDeinit(&buffs); @@ -1317,43 +1366,43 @@ char* Parser_popSubStmt(Args* outbuffs, char** stmt_p, char* delimiter) { char* Parser_popLastSubStmt(Args* outbuffs, char** stmt_p, char* delimiter) { uint8_t last_stmt_i = 0; char* stmt = *stmt_p; - Cursor_forEachToken(ps, stmt) { - Cursor_iterStart(&ps); - if (strIsStartWith(ps.token1.pyload, delimiter)) { + Cursor_forEachToken(cs, stmt) { + Cursor_iterStart(&cs); + if (strIsStartWith(cs.token1.pyload, delimiter)) { /* found delimiter */ - if (!strEqu(delimiter, "[") && ps.branket_deepth > 0) { + if (!strEqu(delimiter, "[") && cs.branket_deepth > 0) { /* ignore */ - Cursor_iterEnd(&ps); + Cursor_iterEnd(&cs); continue; } /* for "[" */ - if (ps.branket_deepth > 1) { + if (cs.branket_deepth > 1) { /* ignore */ - Cursor_iterEnd(&ps); + Cursor_iterEnd(&cs); continue; } - last_stmt_i = ps.iter_index; + last_stmt_i = cs.iter_index; } - Cursor_iterEnd(&ps); + Cursor_iterEnd(&cs); } - Cursor_deinit(&ps); + Cursor_deinit(&cs); Arg* mainStmt = arg_newStr(""); Arg* lastStmt = arg_newStr(""); { - Cursor_forEachToken(ps, stmt) { - Cursor_iterStart(&ps); - if (ps.iter_index < last_stmt_i) { - mainStmt = arg_strAppend(mainStmt, ps.token1.pyload); + Cursor_forEachToken(cs, stmt) { + Cursor_iterStart(&cs); + if (cs.iter_index < last_stmt_i) { + mainStmt = arg_strAppend(mainStmt, cs.token1.pyload); } - if (ps.iter_index >= last_stmt_i) { - lastStmt = arg_strAppend(lastStmt, ps.token1.pyload); + if (cs.iter_index >= last_stmt_i) { + lastStmt = arg_strAppend(lastStmt, cs.token1.pyload); } - Cursor_iterEnd(&ps); + Cursor_iterEnd(&cs); } - Cursor_deinit(&ps); + Cursor_deinit(&cs); } *stmt_p = strsCacheArg(outbuffs, mainStmt); @@ -1364,7 +1413,7 @@ static void _AST_parse_list(AST* ast, Args* buffs, char* stmt) { #if !PIKA_BUILTIN_STRUCT_ENABLE return; #endif - AST_setThisNode(ast, (char*)"list", "list"); + AST_setNodeAttr(ast, (char*)"list", "list"); char* subStmts = strsCut(buffs, stmt, '[', ']'); subStmts = strsAppend(buffs, subStmts, ","); while (1) { @@ -1381,7 +1430,7 @@ static void _AST_parse_dict(AST* ast, Args* buffs, char* stmt) { #if !PIKA_BUILTIN_STRUCT_ENABLE return; #endif - AST_setThisNode(ast, (char*)"dict", "dict"); + AST_setNodeAttr(ast, (char*)"dict", "dict"); char* subStmts = strsCut(buffs, stmt, '{', '}'); subStmts = strsAppend(buffs, subStmts, ","); while (1) { @@ -1400,7 +1449,7 @@ static void _AST_parse_slice(AST* ast, Args* buffs, char* stmt) { #if !PIKA_SYNTAX_SLICE_ENABLE return; #endif - AST_setThisNode(ast, (char*)"slice", "slice"); + AST_setNodeAttr(ast, (char*)"slice", "slice"); stmt = strsCopy(buffs, stmt); char* laststmt = Parser_popLastSubStmt(buffs, &stmt, "["); AST_parseSubStmt(ast, stmt); @@ -1444,36 +1493,36 @@ AST* AST_parseStmt(AST* ast, char* stmt) { left = strsCopy(&buffs, ""); right = strsCopy(&buffs, ""); uint8_t is_meet_equ = 0; - Cursor_forEachToken(ps, stmt) { - Cursor_iterStart(&ps); - if (strEqu(ps.token1.pyload, "=") && - ps.token1.type == TOKEN_operator) { + Cursor_forEachToken(cs, stmt) { + Cursor_iterStart(&cs); + if (strEqu(cs.token1.pyload, "=") && + cs.token1.type == TOKEN_operator) { is_meet_equ = 1; - Cursor_iterEnd(&ps); + Cursor_iterEnd(&cs); continue; } if (0 == is_meet_equ) { - left = strsAppend(&buffs, left, ps.token1.pyload); + left = strsAppend(&buffs, left, cs.token1.pyload); } if (1 == is_meet_equ) { - right = strsAppend(&buffs, right, ps.token1.pyload); + right = strsAppend(&buffs, right, cs.token1.pyload); } - Cursor_iterEnd(&ps); + Cursor_iterEnd(&cs); } - Cursor_deinit(&ps); + Cursor_deinit(&cs); } /* solve the += -= /= *= stmt */ if (!isLeftExist) { - isLeftExist = Parser_solveSelfOperator(&buffs, stmt, &right, &left); + isLeftExist = Suger_selfOperator(&buffs, stmt, &right, &left); } /* solve the [] stmt */ - right = Suger_solveLeftSlice(&buffs, right, &left); - right = Suger_solveFormat(&buffs, right); + right = Suger_leftSlice(&buffs, right, &left); + right = Suger_format(&buffs, right); /* set left */ if (isLeftExist) { - AST_setThisNode(ast, (char*)"left", left); + AST_setNodeAttr(ast, (char*)"left", left); } /* match statment type */ enum StmtType stmtType = Lexer_matchStmtType(right); @@ -1485,7 +1534,7 @@ AST* AST_parseStmt(AST* ast, char* stmt) { result = PIKA_RES_ERR_SYNTAX_ERROR; goto exit; } - AST_setThisNode(ast, (char*)"operator", operator); + AST_setNodeAttr(ast, (char*)"operator", operator); char* rightBuff = strsCopy(&buffs, right); char* subStmt1 = strsPopTokenWithSkip_byStr(&buffs, rightBuff, operator, '(', ')'); @@ -1525,7 +1574,7 @@ AST* AST_parseStmt(AST* ast, char* stmt) { /* solve method stmt */ if (STMT_method == stmtType) { method = strsGetFirstToken(&buffs, right, '('); - AST_setThisNode(ast, (char*)"method", method); + AST_setNodeAttr(ast, (char*)"method", method); char* subStmts = strsCut(&buffs, right, '(', ')'); pika_assert(NULL != subStmts); /* add ',' at the end */ @@ -1542,13 +1591,13 @@ AST* AST_parseStmt(AST* ast, char* stmt) { /* solve reference stmt */ if (STMT_reference == stmtType) { ref = right; - AST_setThisNode(ast, (char*)"ref", ref); + AST_setNodeAttr(ast, (char*)"ref", ref); goto exit; } /* solve import stmt */ if (STMT_import == stmtType) { import = strsGetLastToken(&buffs, right, ' '); - AST_setThisNode(ast, (char*)"import", import); + AST_setNodeAttr(ast, (char*)"import", import); goto exit; } /* solve str stmt */ @@ -1563,7 +1612,7 @@ AST* AST_parseStmt(AST* ast, char* stmt) { str = strsReplace(&buffs, str, "\\\"", "\""); str = strsReplace(&buffs, str, "\\'", "'"); } - AST_setThisNode(ast, (char*)"string", str); + AST_setNodeAttr(ast, (char*)"string", str); goto exit; } /* solve bytes stmt */ @@ -1571,13 +1620,13 @@ AST* AST_parseStmt(AST* ast, char* stmt) { str = right + 1; str = strsDeleteChar(&buffs, str, '\''); str = strsDeleteChar(&buffs, str, '\"'); - AST_setThisNode(ast, (char*)"bytes", str); + AST_setNodeAttr(ast, (char*)"bytes", str); goto exit; } /* solve number stmt */ if (STMT_number == stmtType) { num = right; - AST_setThisNode(ast, (char*)"num", num); + AST_setNodeAttr(ast, (char*)"num", num); goto exit; } exit: @@ -1642,13 +1691,71 @@ char* Parser_removeAnnotation(char* line) { return line; } +char* _defGetDefault(Args* outBuffs, char** dec_out) { + Args buffs = {0}; + char* dec_str = strsCopy(&buffs, *dec_out); + char* fn_name = strsGetFirstToken(&buffs, dec_str, '('); + Arg* dec_arg = arg_strAppend(arg_newStr(fn_name), "("); + Arg* default_arg = arg_newStr(""); + char* arg_list = strsCut(&buffs, dec_str, '(', ')'); + int arg_num = strCountSign(arg_list, ',') + 1; + for (int i = 0; i < arg_num; i++) { + char* arg_str = strsPopToken(&buffs, arg_list, ','); + int is_default = 0; + if (strIsContain(arg_str, '=')) { + default_arg = arg_strAppend(default_arg, arg_str); + default_arg = arg_strAppend(default_arg, ","); + arg_str = strsPopToken(&buffs, arg_str, '='); + is_default = 1; + } + dec_arg = arg_strAppend(dec_arg, arg_str); + if (is_default) { + dec_arg = arg_strAppend(dec_arg, "="); + } + dec_arg = arg_strAppend(dec_arg, ","); + } + strPopLastToken(arg_getStr(dec_arg), ','); + dec_arg = arg_strAppend(dec_arg, ")"); + *dec_out = strsCopy(outBuffs, arg_getStr(dec_arg)); + char* default_out = strsCopy(outBuffs, arg_getStr(default_arg)); + strPopLastToken(default_out, ','); + arg_deinit(dec_arg); + arg_deinit(default_arg); + strsDeinit(&buffs); + return default_out; +} + +static char* Suger_multiReturn(Args* out_buffs, char* line) { +#if PIKA_NANO_ENABLE + return line; +#endif + Cursor_forEachToken(cs, line) { + Cursor_iterStart(&cs); + if (cs.branket_deepth == 0 && strEqu(cs.token1.pyload, ",")) { + line = strsFormat(out_buffs, strGetSize(line) + 3, "(%s)", line); + Cursor_iterEnd(&cs); + break; + } + Cursor_iterEnd(&cs); + } + Cursor_deinit(&cs); + return line; +} + /* match block start keywords */ const char control_keywords[][9] = {"break", "continue"}; /* normal keyward */ const char normal_keywords[][7] = {"while", "if", "elif"}; -AST* AST_parseLine(char* line, Stack* block_stack) { +AST* AST_parseLine_withBlockStack_withBlockDeepth(char* line, + Stack* block_stack, + int block_deepth) { + Stack s; + stack_init(&s); + if (block_stack == NULL) { + block_stack = &s; + } /* line is not exist */ if (line == NULL) { return NULL; @@ -1669,28 +1776,27 @@ AST* AST_parseLine(char* line, Stack* block_stack) { ast = NULL; goto exit; } + block_deepth_now += block_deepth; obj_setInt(ast, "blockDeepth", block_deepth_now); /* check if exit block */ - if (NULL != block_stack) { - block_deepth_last = stack_getTop(block_stack); - /* exit each block */ - for (int i = 0; i < block_deepth_last - block_deepth_now; i++) { - QueueObj* exit_block_queue = obj_getObj(ast, "exitBlock"); - /* create an exit_block queue */ - if (NULL == exit_block_queue) { - obj_newObj(ast, "exitBlock", "", New_TinyObj); - exit_block_queue = obj_getObj(ast, "exitBlock"); - queueObj_init(exit_block_queue); - } - char buff[10] = {0}; - char* block_type = stack_popStr(block_stack, buff); - /* push exit block type to exit_block queue */ - queueObj_pushStr(exit_block_queue, block_type); + block_deepth_last = stack_getTop(block_stack) + block_deepth; + /* exit each block */ + for (int i = 0; i < block_deepth_last - block_deepth_now; i++) { + QueueObj* exit_block_queue = obj_getObj(ast, "exitBlock"); + /* create an exit_block queue */ + if (NULL == exit_block_queue) { + obj_newObj(ast, "exitBlock", "", New_TinyObj); + exit_block_queue = obj_getObj(ast, "exitBlock"); + queueObj_init(exit_block_queue); } + char buff[10] = {0}; + char* block_type = stack_popStr(block_stack, buff); + /* push exit block type to exit_block queue */ + queueObj_pushStr(exit_block_queue, block_type); } - line_start = line + block_deepth_now * 4; + line_start = line + (block_deepth_now - block_deepth) * 4; stmt = line_start; // "while" "if" "elif" @@ -1700,10 +1806,8 @@ AST* AST_parseLine(char* line, Stack* block_stack) { if (strIsStartWith(line_start, keyword) && (line_start[keyword_len] == ' ')) { stmt = strsCut(&buffs, line_start, ' ', ':'); - AST_setThisNode(ast, "block", keyword); - if (NULL != block_stack) { - stack_pushStr(block_stack, keyword); - } + AST_setNodeBlock(ast, keyword); + stack_pushStr(block_stack, keyword); goto block_matched; } } @@ -1716,7 +1820,7 @@ AST* AST_parseLine(char* line, Stack* block_stack) { if ((strIsStartWith(line_start, keyward)) && ((line_start[keyward_size] == ' ') || (line_start[keyward_size] == 0))) { - AST_setThisNode(ast, keyward, ""); + AST_setNodeAttr(ast, keyward, ""); stmt = ""; goto block_matched; } @@ -1726,6 +1830,7 @@ AST* AST_parseLine(char* line, Stack* block_stack) { if (strIsStartWith(line_start, "for ")) { Args* list_buffs = New_strBuff(); char* line_buff = strsCopy(list_buffs, line_start + 4); + line_buff = strsGetCleanCmd(list_buffs, line_buff); if (strCountSign(line_buff, ':') < 1) { args_deinit(list_buffs); obj_deinit(ast); @@ -1733,18 +1838,16 @@ AST* AST_parseLine(char* line, Stack* block_stack) { goto exit; } char* arg_in = strsPopToken(list_buffs, line_buff, ' '); - AST_setThisNode(ast, "arg_in", arg_in); + AST_setNodeAttr(ast, "arg_in", arg_in); strsPopToken(list_buffs, line_buff, ' '); char* list_in = strsPopToken(list_buffs, line_buff, ':'); list_in = strsAppend(list_buffs, "iter(", list_in); list_in = strsAppend(list_buffs, list_in, ")"); list_in = strsCopy(&buffs, list_in); args_deinit(list_buffs); - AST_setThisNode(ast, "block", "for"); - AST_setThisNode(ast, "list_in", list_in); - if (NULL != block_stack) { - stack_pushStr(block_stack, "for"); - } + AST_setNodeBlock(ast, "for"); + AST_setNodeAttr(ast, "list_in", list_in); + stack_pushStr(block_stack, "for"); stmt = list_in; goto block_matched; } @@ -1753,10 +1856,8 @@ AST* AST_parseLine(char* line, Stack* block_stack) { if (strIsStartWith(line_start, "else")) { if ((line_start[4] == ' ') || (line_start[4] == ':')) { stmt = ""; - AST_setThisNode(ast, "block", "else"); - if (NULL != block_stack) { - stack_pushStr(block_stack, "else"); - } + AST_setNodeBlock(ast, "else"); + stack_pushStr(block_stack, "else"); } goto block_matched; } @@ -1766,10 +1867,8 @@ AST* AST_parseLine(char* line, Stack* block_stack) { if (strIsStartWith(line_start, "try")) { if ((line_start[3] == ' ') || (line_start[3] == ':')) { stmt = ""; - AST_setThisNode(ast, "block", "try"); - if (NULL != block_stack) { - stack_pushStr(block_stack, "try"); - } + AST_setNodeBlock(ast, "try"); + stack_pushStr(block_stack, "try"); } goto block_matched; } @@ -1778,17 +1877,15 @@ AST* AST_parseLine(char* line, Stack* block_stack) { if (strIsStartWith(line_start, "except")) { if ((line_start[6] == ' ') || (line_start[6] == ':')) { stmt = ""; - AST_setThisNode(ast, "block", "except"); - if (NULL != block_stack) { - stack_pushStr(block_stack, "except"); - } + AST_setNodeBlock(ast, "except"); + stack_pushStr(block_stack, "except"); } goto block_matched; } #endif if (strEqu(line_start, "return")) { - AST_setThisNode(ast, "return", ""); + AST_setNodeAttr(ast, "return", ""); stmt = ""; goto block_matched; } @@ -1796,18 +1893,19 @@ AST* AST_parseLine(char* line, Stack* block_stack) { char* lineBuff = strsCopy(&buffs, line_start); strsPopToken(&buffs, lineBuff, ' '); stmt = lineBuff; - AST_setThisNode(ast, "return", ""); + stmt = Suger_multiReturn(&buffs, stmt); + AST_setNodeAttr(ast, "return", ""); goto block_matched; } #if PIKA_SYNTAX_EXCEPTION_ENABLE if (strEqu(line_start, "raise")) { - AST_setThisNode(ast, "raise", ""); + AST_setNodeAttr(ast, "raise", ""); stmt = "RuntimeError"; goto block_matched; } if (strIsStartWith(line_start, "raise ")) { - AST_setThisNode(ast, "raise", ""); + AST_setNodeAttr(ast, "raise", ""); char* lineBuff = strsCopy(&buffs, line_start); strsPopToken(&buffs, lineBuff, ' '); stmt = lineBuff; @@ -1819,7 +1917,7 @@ AST* AST_parseLine(char* line, Stack* block_stack) { /* assert */ if (strIsStartWith(line_start, "assert ")) { stmt = ""; - AST_setThisNode(ast, "assert", ""); + AST_setNodeAttr(ast, "assert", ""); char* lineBuff = strsCopy(&buffs, line_start + 7); /* assert expr [, msg] */ while (1) { @@ -1837,46 +1935,46 @@ AST* AST_parseLine(char* line, Stack* block_stack) { stmt = ""; char* global_list = line_start + 7; global_list = strsGetCleanCmd(&buffs, global_list); - AST_setThisNode(ast, "global", global_list); + AST_setNodeAttr(ast, "global", global_list); goto block_matched; } if (strIsStartWith(line_start, "del ")) { stmt = ""; char* del_dir = line_start + sizeof("del ") - 1; del_dir = strsGetCleanCmd(&buffs, del_dir); - AST_setThisNode(ast, "del", del_dir); + AST_setNodeAttr(ast, "del", del_dir); goto block_matched; } if (strIsStartWith(line_start, (char*)"def ")) { stmt = ""; - char* declear = strsCut(&buffs, line_start, ' ', ':'); - if (NULL == declear) { + char* declare = strsCut(&buffs, line_start, ' ', ':'); + if (NULL == declare) { obj_deinit(ast); ast = NULL; goto exit; } - declear = strsGetCleanCmd(&buffs, declear); - AST_setThisNode(ast, "block", "def"); - AST_setThisNode(ast, "declear", declear); - if (NULL != block_stack) { - stack_pushStr(block_stack, "def"); + declare = strsGetCleanCmd(&buffs, declare); + char* defaultStmt = _defGetDefault(&buffs, &declare); + AST_setNodeBlock(ast, "def"); + AST_setNodeAttr(ast, "declare", declare); + if (defaultStmt[0] != '\0') { + AST_setNodeAttr(ast, "default", defaultStmt); } + stack_pushStr(block_stack, "def"); goto block_matched; } if (strIsStartWith(line_start, (char*)"class ")) { stmt = ""; - char* declear = strsCut(&buffs, line_start, ' ', ':'); - if (NULL == declear) { + char* declare = strsCut(&buffs, line_start, ' ', ':'); + if (NULL == declare) { obj_deinit(ast); ast = NULL; goto exit; } - declear = strsGetCleanCmd(&buffs, declear); - AST_setThisNode(ast, "block", "class"); - AST_setThisNode(ast, "declear", declear); - if (NULL != block_stack) { - stack_pushStr(block_stack, "class"); - } + declare = strsGetCleanCmd(&buffs, declare); + AST_setNodeBlock(ast, "class"); + AST_setNodeAttr(ast, "declare", declare); + stack_pushStr(block_stack, "class"); goto block_matched; } @@ -1890,10 +1988,28 @@ block_matched: ast = AST_parseStmt(ast, stmt); goto exit; exit: + stack_deinit(&s); strsDeinit(&buffs); return ast; } +static AST* AST_parseLine_withBlockStack(char* line, Stack* block_stack) { + return AST_parseLine_withBlockStack_withBlockDeepth(line, block_stack, 0); +} + +static AST* AST_parseLine_withBlockDeepth(char* line, int block_deepth) { + return AST_parseLine_withBlockStack_withBlockDeepth(line, NULL, + block_deepth); +} + +int AST_getBlockDeepthNow(AST* ast) { + return obj_getInt(ast, "blockDeepth"); +} + +AST* AST_parseLine(char* line) { + return AST_parseLine_withBlockStack(line, NULL); +} + static char* Suger_import(Args* buffs_p, char* line) { #if !PIKA_SYNTAX_IMPORT_EX_ENABLE return line; @@ -1908,21 +2024,21 @@ static char* Suger_import(Args* buffs_p, char* line) { goto exit; } - Cursor_forEachToken(ps, stmt) { - Cursor_iterStart(&ps); + Cursor_forEachToken(cs, stmt) { + Cursor_iterStart(&cs); /* defaut set the 'origin' as the first token */ - if (ps.iter_index == 1) { - origin = strsCopy(&buffs, ps.token1.pyload); + if (cs.iter_index == 1) { + origin = strsCopy(&buffs, cs.token1.pyload); } - if (strEqu(ps.token2.pyload, " as ")) { - origin = strsCopy(&buffs, ps.token1.pyload); + if (strEqu(cs.token2.pyload, " as ")) { + origin = strsCopy(&buffs, cs.token1.pyload); } - if (strEqu(ps.token1.pyload, " as ")) { - alias = strsCopy(&buffs, ps.token2.pyload); + if (strEqu(cs.token1.pyload, " as ")) { + alias = strsCopy(&buffs, cs.token2.pyload); } - Cursor_iterEnd(&ps); + Cursor_iterEnd(&cs); } - Cursor_deinit(&ps); + Cursor_deinit(&cs); /* only import, not 'as' */ if (NULL == alias) { @@ -1940,6 +2056,86 @@ exit: return line_out; } +static PIKA_BOOL _check_is_multi_assign(char* arg_list) { + PIKA_BOOL res = PIKA_FALSE; + Cursor_forEachToken(cs, arg_list) { + Cursor_iterStart(&cs); + if ((cs.branket_deepth == 0 && strEqu(cs.token1.pyload, ","))) { + res = PIKA_TRUE; + } + Cursor_iterEnd(&cs); + } + Cursor_deinit(&cs); + return res; +} + +static char* Suger_multiAssign(Args* out_buffs, char* line) { +#if PIKA_NANO_ENABLE + return line; +#endif + Args buffs = {0}; + char* line_out = line; + PIKA_BOOL is_assign = PIKA_FALSE; + Arg* stmt = arg_newStr(""); + Arg* out_list = arg_newStr(""); + Arg* out_item = arg_newStr(""); + Arg* line_out_arg = arg_newStr(""); + char* line_item = NULL; + char* out_list_str = NULL; + int out_num = 0; + Cursor_forEachToken(cs, line) { + Cursor_iterStart(&cs); + if (cs.branket_deepth == 0 && strEqu(cs.token1.pyload, "=")) { + is_assign = PIKA_TRUE; + Cursor_iterEnd(&cs); + continue; + } + if (is_assign) { + stmt = arg_strAppend(stmt, cs.token1.pyload); + } + if (!is_assign) { + out_list = arg_strAppend(out_list, cs.token1.pyload); + } + Cursor_iterEnd(&cs); + } + Cursor_deinit(&cs); + if (!is_assign) { + line_out = line; + goto exit; + } + + if (!_check_is_multi_assign(arg_getStr(out_list))) { + line_out = line; + goto exit; + } + + line_item = + strsFormat(&buffs, PIKA_LINE_BUFF_SIZE, "$tmp= %s\n", arg_getStr(stmt)); + line_out_arg = arg_strAppend(line_out_arg, line_item); + + out_list_str = arg_getStr(out_list); + while (1) { + char* item = Cursor_popToken(&buffs, &out_list_str, ","); + if (item[0] == '\0') { + break; + } + char* line_item = strsFormat(&buffs, PIKA_LINE_BUFF_SIZE, + "%s = $tmp[%d]\n", item, out_num); + line_out_arg = arg_strAppend(line_out_arg, line_item); + out_num++; + } + line_out_arg = arg_strAppend(line_out_arg, "del $tmp"); + + line_out = strsCopy(out_buffs, arg_getStr(line_out_arg)); +exit: + arg_deinit(stmt); + arg_deinit(out_list); + arg_deinit(out_item); + arg_deinit(line_out_arg); + strsDeinit(&buffs); + return line_out; +} + static char* Suger_from(Args* buffs_p, char* line) { #if !PIKA_SYNTAX_IMPORT_EX_ENABLE return line; @@ -1955,20 +2151,20 @@ static char* Suger_from(Args* buffs_p, char* line) { goto exit; } - Cursor_forEachToken(ps, stmt) { - Cursor_iterStart(&ps); - if (strEqu(ps.token2.pyload, " import ")) { - module = strsCopy(&buffs, ps.token1.pyload); + Cursor_forEachToken(cs, stmt) { + Cursor_iterStart(&cs); + if (strEqu(cs.token2.pyload, " import ")) { + module = strsCopy(&buffs, cs.token1.pyload); } - if (strEqu(ps.token1.pyload, " import ")) { - class = strsCopy(&buffs, ps.token2.pyload); + if (strEqu(cs.token1.pyload, " import ")) { + class = strsCopy(&buffs, cs.token2.pyload); } - if (strEqu(ps.token1.pyload, " as ")) { - alias = strsCopy(&buffs, ps.token2.pyload); + if (strEqu(cs.token1.pyload, " as ")) { + alias = strsCopy(&buffs, cs.token2.pyload); } - Cursor_iterEnd(&ps); + Cursor_iterEnd(&cs); } - Cursor_deinit(&ps); + Cursor_deinit(&cs); if (NULL == module) { line_out = strsCopy(buffs_p, ""); @@ -2009,6 +2205,7 @@ static char* Parser_linePreProcess(Args* buffs_p, char* line) { line = strsDeleteChar(buffs_p, line, '\r'); line = Suger_import(buffs_p, line); line = Suger_from(buffs_p, line); + line = Suger_multiAssign(buffs_p, line); exit: return line; } @@ -2035,12 +2232,12 @@ char* Parser_LineToAsm(Args* buffs_p, char* line, Stack* blockStack) { for (int i = 0; i < line_num; i++) { char* single_line = strsPopToken(buffs_p, line, '\n'); /* parse tokens to AST */ - ast = AST_parseLine(single_line, blockStack); + ast = AST_parseLine_withBlockStack(single_line, blockStack); /* gen ASM from AST */ if (ASM == NULL) { - ASM = AST_toPikaASM(ast, buffs_p); + ASM = AST_genAsm(ast, buffs_p); } else { - ASM = strsAppend(buffs_p, ASM, AST_toPikaASM(ast, buffs_p)); + ASM = strsAppend(buffs_p, ASM, AST_genAsm(ast, buffs_p)); } if (NULL != ast) { AST_deinit(ast); @@ -2261,7 +2458,7 @@ char* Parser_fileToAsm(Args* outBuffs, char* filename) { return res; } -char* AST_appandPikaASM(AST* ast, AST* subAst, Args* outBuffs, char* pikaAsm) { +char* AST_genAsm_sub(AST* ast, AST* subAst, Args* outBuffs, char* pikaAsm) { int deepth = obj_getInt(ast, "deepth"); Args buffs = {0}; /* append each queue item */ @@ -2271,7 +2468,7 @@ char* AST_appandPikaASM(AST* ast, AST* subAst, Args* outBuffs, char* pikaAsm) { break; } obj_setInt(ast, "deepth", deepth + 1); - pikaAsm = AST_appandPikaASM(ast, subStmt, &buffs, pikaAsm); + pikaAsm = AST_genAsm_sub(ast, subStmt, &buffs, pikaAsm); } /* Byte code generate rules */ @@ -2321,9 +2518,9 @@ char* ASM_addBlockDeepth(AST* ast, uint8_t deepthOffset) { pikaAsm = strsAppend(buffs_p, pikaAsm, (char*)"B"); char buff[11]; - pikaAsm = strsAppend( - buffs_p, pikaAsm, - fast_itoa(buff, obj_getInt(ast, "blockDeepth") + deepthOffset)); + pikaAsm = + strsAppend(buffs_p, pikaAsm, + fast_itoa(buff, AST_getBlockDeepthNow(ast) + deepthOffset)); pikaAsm = strsAppend(buffs_p, pikaAsm, (char*)"\n"); return pikaAsm; } @@ -2335,7 +2532,7 @@ char* GenRule_toAsm(GenRule rule, int deepth) { char* buff = args_getBuff(buffs, PIKA_SPRINTF_BUFF_SIZE); /* parse stmt ast */ - pikaAsm = AST_appandPikaASM(ast, ast, buffs, pikaAsm); + pikaAsm = AST_genAsm_sub(ast, ast, buffs, pikaAsm); /* e.g. "0 CTN \n" */ __platform_sprintf(buff, "%d %s ", deepth, rule.ins); Arg* abuff = arg_newStr(buff); @@ -2351,7 +2548,25 @@ char* GenRule_toAsm(GenRule rule, return pikaAsm; } -char* AST_toPikaASM(AST* ast, Args* outBuffs) { +char* AST_genAsm(AST* ast, Args* outBuffs) { + const GenRule rules_topAst[] = { + {.ins = "CTN", .type = VAL_NONEVAL, .ast = "continue"}, + {.ins = "BRK", .type = VAL_NONEVAL, .ast = "break"}, + {.ins = "DEL", .type = VAL_DYNAMIC, .ast = "del"}, + {.ins = "GLB", .type = VAL_DYNAMIC, .ast = "global"}, + {.ins = "RIS", .type = VAL_DYNAMIC, .ast = "raise"}, + {.ins = "ASS", .type = VAL_NONEVAL, .ast = "assert"}, + {.ins = "RET", .type = VAL_NONEVAL, .ast = "return"}}; + + /* generate code for block ast */ + const GenRule rules_block[] = { + {.ins = "TRY", .type = VAL_NONEVAL, .ast = "try"}, + {.ins = "EXP", .type = VAL_NONEVAL, .ast = "except"}, + {.ins = "NEL", .type = VAL_STATIC_, .ast = "else", .val = "1"}, + {.ins = "JEZ", .type = VAL_STATIC_, .ast = "if", .val = "1"}, + {.ins = "JEZ", .type = VAL_STATIC_, .ast = "while", .val = "2"}, + }; + Args buffs = {0}; char* pikaAsm = strsCopy(&buffs, ""); QueueObj* exitBlock; @@ -2398,9 +2613,9 @@ char* AST_toPikaASM(AST* ast, Args* outBuffs) { /* garbage collect for the list */ pikaAsm = ASM_addBlockDeepth(ast, outBuffs, pikaAsm, block_type_num); - char _l_x[] = "_lx"; + char _l_x[] = "$lx"; char block_deepth_char = - obj_getInt(ast, "blockDeepth") + block_type_num + '0'; + AST_getBlockDeepthNow(ast) + block_type_num + '0'; _l_x[sizeof(_l_x) - 2] = block_deepth_char; pikaAsm = strsAppend(outBuffs, pikaAsm, (char*)"0 DEL "); pikaAsm = strsAppend(outBuffs, pikaAsm, (char*)_l_x); @@ -2433,17 +2648,18 @@ char* AST_toPikaASM(AST* ast, Args* outBuffs) { /* match block */ is_block_matched = 0; - if (strEqu(obj_getStr(ast, "block"), "for")) { + if (strEqu(AST_getThisBlock(ast), "for")) { /* for "for" iter */ char* arg_in = obj_getStr(ast, "arg_in"); + char* arg_in_kv = NULL; Arg* newAsm_arg = arg_newStr(""); - char _l_x[] = "_lx"; + char _l_x[] = "$lx"; char block_deepth_char = '0'; - block_deepth_char += obj_getInt(ast, "blockDeepth"); + block_deepth_char += AST_getBlockDeepthNow(ast); _l_x[sizeof(_l_x) - 2] = block_deepth_char; /* init iter */ /* get the iter(_l) */ - pikaAsm = AST_appandPikaASM(ast, ast, &buffs, pikaAsm); + pikaAsm = AST_genAsm_sub(ast, ast, &buffs, pikaAsm); newAsm_arg = arg_strAppend(newAsm_arg, "0 OUT "); newAsm_arg = arg_strAppend(newAsm_arg, _l_x); newAsm_arg = arg_strAppend(newAsm_arg, "\n"); @@ -2453,6 +2669,18 @@ char* AST_toPikaASM(AST* ast, Args* outBuffs) { /* get next */ /* run next(_l) */ /* check item is exist */ + /* + $n = $lx.next() + EST $n + k, v = $n + DEL $n + */ + + if (_check_is_multi_assign(arg_in)) { + arg_in_kv = arg_in; + arg_in = "$tmp"; + } + pikaAsm = ASM_addBlockDeepth(ast, outBuffs, pikaAsm, 0); newAsm_arg = arg_strAppend(newAsm_arg, "0 RUN "); newAsm_arg = arg_strAppend(newAsm_arg, _l_x); @@ -2467,38 +2695,80 @@ char* AST_toPikaASM(AST* ast, Args* outBuffs) { newAsm_arg = arg_strAppend(newAsm_arg, "\n0 JEZ 2\n"); pikaAsm = strsAppend(&buffs, pikaAsm, arg_getStr(newAsm_arg)); arg_deinit(newAsm_arg); + + if (NULL != arg_in_kv) { + int out_num = 0; + while (1) { + char* item = Cursor_popToken(&buffs, &arg_in_kv, ","); + if (item[0] == '\0') { + break; + } + char* stmt = strsFormat(&buffs, PIKA_LINE_BUFF_SIZE, + "%s = $tmp[%d]\n", item, out_num); + + AST* ast_this = AST_parseLine_withBlockDeepth( + stmt, AST_getBlockDeepthNow(ast) + 1); + pikaAsm = + strsAppend(&buffs, pikaAsm, AST_genAsm(ast_this, &buffs)); + AST_deinit(ast_this); + out_num++; + } + pikaAsm = ASM_addBlockDeepth(ast, outBuffs, pikaAsm, 1); + pikaAsm = strsAppend(&buffs, pikaAsm, "0 DEL $tmp\n"); + } + is_block_matched = 1; goto exit; } - if (strEqu(obj_getStr(ast, "block"), "elif")) { + if (strEqu(AST_getThisBlock(ast), "elif")) { /* skip if __else is 0 */ pikaAsm = strsAppend(&buffs, pikaAsm, "0 NEL 1\n"); /* parse stmt ast */ - pikaAsm = AST_appandPikaASM(ast, ast, &buffs, pikaAsm); + pikaAsm = AST_genAsm_sub(ast, ast, &buffs, pikaAsm); /* skip if stmt is 0 */ pikaAsm = strsAppend(&buffs, pikaAsm, "0 JEZ 1\n"); is_block_matched = 1; goto exit; } - if (strEqu(obj_getStr(ast, "block"), "def")) { + if (strEqu(AST_getThisBlock(ast), "def")) { + char* defaultStmts = AST_getNodeAttr(ast, "default"); pikaAsm = strsAppend(&buffs, pikaAsm, "0 DEF "); - pikaAsm = strsAppend(&buffs, pikaAsm, obj_getStr(ast, "declear")); + pikaAsm = strsAppend(&buffs, pikaAsm, AST_getNodeAttr(ast, "declare")); pikaAsm = strsAppend(&buffs, pikaAsm, "\n" "0 JMP 1\n"); + + if (NULL != defaultStmts) { + int stmt_num = strGetTokenNum(defaultStmts, ','); + for (int i = 0; i < stmt_num; i++) { + char* stmt = strsPopToken(&buffs, defaultStmts, ','); + char* arg_name = strsGetFirstToken(&buffs, stmt, '='); + pikaAsm = ASM_addBlockDeepth(ast, &buffs, pikaAsm, 1); + pikaAsm = strsAppend(&buffs, pikaAsm, "0 EST "); + pikaAsm = strsAppend(&buffs, pikaAsm, arg_name); + pikaAsm = strsAppend(&buffs, pikaAsm, "\n"); + pikaAsm = strsAppend(&buffs, pikaAsm, "0 JNZ 2\n"); + AST* ast_this = AST_parseLine_withBlockDeepth( + stmt, AST_getBlockDeepthNow(ast) + 1); + pikaAsm = + strsAppend(&buffs, pikaAsm, AST_genAsm(ast_this, &buffs)); + AST_deinit(ast_this); + } + } + is_block_matched = 1; goto exit; } - if (strEqu(obj_getStr(ast, "block"), "class")) { - char* declear = obj_getStr(ast, "declear"); + if (strEqu(AST_getThisBlock(ast), "class")) { + char* declare = obj_getStr(ast, "declare"); char* thisClass = NULL; char* superClass = NULL; - if (strIsContain(declear, '(')) { - thisClass = strsGetFirstToken(&buffs, declear, '('); - superClass = strsCut(&buffs, declear, '(', ')'); + if (strIsContain(declare, '(')) { + thisClass = strsGetFirstToken(&buffs, declare, '('); + superClass = strsCut(&buffs, declare, '(', ')'); } else { - thisClass = declear; + thisClass = declare; superClass = ""; } if (strEqu("", superClass)) { @@ -2516,7 +2786,7 @@ char* AST_toPikaASM(AST* ast, Args* outBuffs) { "0 JMP 1\n")); char block_deepth_str[] = "B0\n"; /* goto deeper block */ - block_deepth_str[1] += obj_getInt(ast, "blockDeepth") + 1; + block_deepth_str[1] += AST_getBlockDeepthNow(ast) + 1; pikaAsm = strsAppend(&buffs, pikaAsm, block_deepth_str); pikaAsm = strsAppend(&buffs, pikaAsm, "0 RUN "); pikaAsm = strsAppend(&buffs, pikaAsm, superClass); @@ -2527,33 +2797,16 @@ char* AST_toPikaASM(AST* ast, Args* outBuffs) { is_block_matched = 1; goto exit; } - /* generate code for block ast */ - const GenRule rules_block[] = { - {.ins = "TRY", .type = VAL_NONEVAL, .ast = "try"}, - {.ins = "EXP", .type = VAL_NONEVAL, .ast = "except"}, - {.ins = "NEL", .type = VAL_STATIC_, .ast = "else", .val = "1"}, - {.ins = "JEZ", .type = VAL_STATIC_, .ast = "if", .val = "1"}, - {.ins = "JEZ", .type = VAL_STATIC_, .ast = "while", .val = "2"}, - }; for (size_t i = 0; i < sizeof(rules_block) / sizeof(GenRule); i++) { GenRule rule = rules_block[i]; - if (strEqu(obj_getStr(ast, "block"), rule.ast)) { + if (strEqu(AST_getThisBlock(ast), rule.ast)) { pikaAsm = GenRule_toAsm(rule, &buffs, ast, pikaAsm, 0); is_block_matched = 1; goto exit; } } - const GenRule rules_topAst[] = { - {.ins = "CTN", .type = VAL_NONEVAL, .ast = "continue"}, - {.ins = "BRK", .type = VAL_NONEVAL, .ast = "break"}, - {.ins = "DEL", .type = VAL_DYNAMIC, .ast = "del"}, - {.ins = "GLB", .type = VAL_DYNAMIC, .ast = "global"}, - {.ins = "RIS", .type = VAL_DYNAMIC, .ast = "raise"}, - {.ins = "ASS", .type = VAL_NONEVAL, .ast = "assert"}, - {.ins = "RET", .type = VAL_NONEVAL, .ast = "return"}}; - /* generate code for top level ast */ for (size_t i = 0; i < sizeof(rules_topAst) / sizeof(rules_topAst[0]); i++) { @@ -2571,7 +2824,7 @@ exit: } if (!is_block_matched) { /* parse stmt ast */ - pikaAsm = AST_appandPikaASM(ast, ast, &buffs, pikaAsm); + pikaAsm = AST_genAsm_sub(ast, ast, &buffs, pikaAsm); } /* output pikaAsm */ diff --git a/port/cmsis-pack/pikascript/pikascript-core/PikaPlatform.c b/port/cmsis-pack/pikascript/pikascript-core/PikaPlatform.c index 3f013eb12..c70989545 100644 --- a/port/cmsis-pack/pikascript/pikascript-core/PikaPlatform.c +++ b/port/cmsis-pack/pikascript/pikascript-core/PikaPlatform.c @@ -110,6 +110,14 @@ PIKA_WEAK void* __platform_memcpy(void* dir, const void* src, size_t size) { return memcpy(dir, src, size); } +PIKA_WEAK int __platform_memcmp(const void* s1, const void* s2, size_t n) { + return memcmp(s1, s2, n); +} + +PIKA_WEAK void* __platform_memmove(void* s1, void* s2, size_t n) { + return memmove(s1, s2, n); +} + PIKA_WEAK char __platform_getchar(void) { #if defined(__linux) || defined(_WIN32) return getchar(); diff --git a/port/cmsis-pack/pikascript/pikascript-core/PikaPlatform.h b/port/cmsis-pack/pikascript/pikascript-core/PikaPlatform.h index 12129061b..b4e6a7efa 100644 --- a/port/cmsis-pack/pikascript/pikascript-core/PikaPlatform.h +++ b/port/cmsis-pack/pikascript/pikascript-core/PikaPlatform.h @@ -137,6 +137,8 @@ void* __platform_malloc(size_t size); void __platform_free(void* ptr); void* __platform_memset(void* mem, int ch, size_t size); void* __platform_memcpy(void* dir, const void* src, size_t size); +int __platform_memcmp(const void* s1, const void* s2, size_t n); +void* __platform_memmove(void* s1, void* s2, size_t n); void* __user_malloc(size_t size); void __user_free(void* ptr, size_t size); diff --git a/port/cmsis-pack/pikascript/pikascript-core/PikaVM.c b/port/cmsis-pack/pikascript/pikascript-core/PikaVM.c index 958f89a0f..ea427fa7e 100644 --- a/port/cmsis-pack/pikascript/pikascript-core/PikaVM.c +++ b/port/cmsis-pack/pikascript/pikascript-core/PikaVM.c @@ -36,7 +36,24 @@ #include #endif -/* head declear start */ +static volatile VMSignal PikaVMSignal = { + .signal_ctrl = VM_SIGNAL_CTRL_NONE, + .vm_cnt = 0, +}; + +VM_SIGNAL_CTRL VMSignal_getCtrl(void) { + return PikaVMSignal.signal_ctrl; +} + +void pks_vm_exit(void) { + PikaVMSignal.signal_ctrl = VM_SIGNAL_CTRL_EXIT; +} + +void pks_vmSignal_setCtrlElear(void) { + PikaVMSignal.signal_ctrl = VM_SIGNAL_CTRL_NONE; +} + +/* head declare start */ static uint8_t VMState_getInputArgNum(VMState* vm); static VMParameters* __pikaVM_runByteCodeFrameWithState( PikaObj* self, @@ -44,9 +61,9 @@ static VMParameters* __pikaVM_runByteCodeFrameWithState( VMParameters* globals, ByteCodeFrame* bytecode_frame, uint16_t pc, - TryInfo* try_info); + RunState* run_state); -/* head declear end */ +/* head declare end */ static InstructUnit* VMState_getInstructNow(VMState* vm) { return instructArray_getByOffset(&(vm->bytecode_frame->instruct_array), @@ -57,12 +74,18 @@ static void VMState_setErrorCode(VMState* vm, uint8_t error_code) { vm->error_code = error_code; } -static InstructUnit* VMState_getInstructWithOffset(VMState* vm, - int32_t offset) { +static InstructUnit* VMState_getInstructUnitWithOffset(VMState* vm, + int32_t offset) { return instructArray_getByOffset(&(vm->bytecode_frame->instruct_array), vm->pc + offset); } +static enum Instruct VMstate_getInstructWithOffset(VMState* vm, + int32_t offset) { + return instructUnit_getInstruct( + VMState_getInstructUnitWithOffset(vm, offset)); +} + static int VMState_getBlockDeepthNow(VMState* vm) { /* support run byteCode */ InstructUnit* ins_unit = VMState_getInstructNow(vm); @@ -75,6 +98,13 @@ static char* VMState_getConstWithInstructUnit(VMState* vm, instructUnit_getConstPoolIndex(ins_unit)); } +#if !PIKA_NANO_ENABLE +static char* VMState_getConstWithOffset(VMState* vm, int32_t offset) { + return VMState_getConstWithInstructUnit( + vm, VMState_getInstructUnitWithOffset(vm, offset)); +} +#endif + static int VMState_getInvokeDeepthNow(VMState* vm) { /* support run byteCode */ InstructUnit* ins_unit = VMState_getInstructNow(vm); @@ -88,7 +118,8 @@ static int32_t VMState_getAddrOffsetOfJmpBack(VMState* vm) { /* find loop deepth */ while (1) { offset -= instructUnit_getSize(ins_unit_now); - InstructUnit* ins_unit_now = VMState_getInstructWithOffset(vm, offset); + InstructUnit* ins_unit_now = + VMState_getInstructUnitWithOffset(vm, offset); uint16_t invoke_deepth = instructUnit_getInvokeDeepth(ins_unit_now); enum Instruct ins = instructUnit_getInstruct(ins_unit_now); char* data = VMState_getConstWithInstructUnit(vm, ins_unit_now); @@ -101,7 +132,8 @@ static int32_t VMState_getAddrOffsetOfJmpBack(VMState* vm) { offset = 0; while (1) { offset += instructUnit_getSize(ins_unit_now); - InstructUnit* ins_unit_now = VMState_getInstructWithOffset(vm, offset); + InstructUnit* ins_unit_now = + VMState_getInstructUnitWithOffset(vm, offset); enum Instruct ins = instructUnit_getInstruct(ins_unit_now); char* data = VMState_getConstWithInstructUnit(vm, ins_unit_now); int block_deepth_now = instructUnit_getBlockDeepth(ins_unit_now); @@ -131,7 +163,7 @@ static int32_t VMState_getAddrOffsetFromJmp(VMState* vm) { if (vm->pc + offset >= (int)VMState_getInstructArraySize(vm)) { break; } - this_ins_unit = VMState_getInstructWithOffset(vm, offset); + this_ins_unit = VMState_getInstructUnitWithOffset(vm, offset); if (instructUnit_getIsNewLine(this_ins_unit)) { uint8_t blockDeepth = instructUnit_getBlockDeepth(this_ins_unit); @@ -147,7 +179,7 @@ static int32_t VMState_getAddrOffsetFromJmp(VMState* vm) { if (vm->jmp < 0) { while (1) { offset -= instructUnit_getSize(); - this_ins_unit = VMState_getInstructWithOffset(vm, offset); + this_ins_unit = VMState_getInstructUnitWithOffset(vm, offset); if (instructUnit_getIsNewLine(this_ins_unit)) { uint8_t blockDeepth = instructUnit_getBlockDeepth(this_ins_unit); @@ -170,6 +202,7 @@ static int32_t VMState_getAddrOffsetOfBreak(VMState* vm) { return offset; } +#if !PIKA_NANO_ENABLE static int32_t VMState_getAddrOffsetOfRaise(VMState* vm) { int offset = 0; InstructUnit* ins_unit_now = VMState_getInstructNow(vm); @@ -178,7 +211,7 @@ static int32_t VMState_getAddrOffsetOfRaise(VMState* vm) { if (vm->pc + offset >= (int)VMState_getInstructArraySize(vm)) { return 0; } - ins_unit_now = VMState_getInstructWithOffset(vm, offset); + ins_unit_now = VMState_getInstructUnitWithOffset(vm, offset); enum Instruct ins = instructUnit_getInstruct(ins_unit_now); if ((NTR == ins)) { return offset; @@ -189,6 +222,7 @@ static int32_t VMState_getAddrOffsetOfRaise(VMState* vm) { } } } +#endif static int32_t VMState_getAddrOffsetOfContinue(VMState* vm) { int32_t offset = VMState_getAddrOffsetOfJmpBack(vm); @@ -210,12 +244,12 @@ static void VMState_delLReg(VMState* vm, uint8_t index) { static void VMState_initReg(VMState* vm) { for (uint8_t i = 0; i < PIKA_REGIST_SIZE; i++) { vm->lreg[i] = NULL; - vm->ireg[i] = 0; + vm->ireg[i] = PIKA_FALSE; } } static PIKA_BOOL _checkLReg(char* data) { - if ((data[0] == '_') && (data[1] == 'l') && (data[2] >= '0') && + if ((data[0] == '$') && (data[1] == 'l') && (data[2] >= '0') && (data[2] <= '9')) { return PIKA_TRUE; } @@ -244,12 +278,18 @@ static Arg* VM_instruction_handler_NON(PikaObj* self, } Arg* __vm_get(PikaObj* self, Arg* key, Arg* obj) { - ArgType obj_type = arg_getType(obj); + ArgType type = arg_getType(obj); + Arg* obj_new = NULL; int index = 0; if (ARG_TYPE_INT == arg_getType(key)) { index = arg_getInt(key); } - if (ARG_TYPE_STRING == obj_type) { + if (ARG_TYPE_STRING == type) { +#if PIKA_STRING_UTF8_ENABLE + PIKA_BOOL is_temp = PIKA_FALSE; + obj_new = arg_newObj(_arg_to_obj(obj, &is_temp)); + type = arg_getType(obj_new); +#else char* str_pyload = arg_getStr(obj); char char_buff[] = " "; if (index < 0) { @@ -257,8 +297,9 @@ Arg* __vm_get(PikaObj* self, Arg* key, Arg* obj) { } char_buff[0] = str_pyload[index]; return arg_newStr(char_buff); +#endif } - if (ARG_TYPE_BYTES == obj_type) { + if (ARG_TYPE_BYTES == type) { uint8_t* bytes_pyload = arg_getBytes(obj); uint8_t byte_buff[] = " "; if (index < 0) { @@ -267,8 +308,13 @@ Arg* __vm_get(PikaObj* self, Arg* key, Arg* obj) { byte_buff[0] = bytes_pyload[index]; return arg_newBytes(byte_buff, 1); } - if (argType_isObject(obj_type)) { - PikaObj* arg_obj = arg_getPtr(obj); + if (argType_isObject(type)) { + PikaObj* arg_obj = NULL; + if (obj_new != NULL) { + arg_obj = arg_getPtr(obj_new); + } else { + arg_obj = arg_getPtr(obj); + } obj_setArg(arg_obj, "__key", key); /* clang-format off */ PIKA_PYTHON( @@ -287,7 +333,18 @@ Arg* __vm_get(PikaObj* self, Arg* key, Arg* obj) { /* const pool */ }; pikaVM_runByteCode(arg_obj, (uint8_t*)bytes); - return arg_copy(args_getArg(arg_obj->list, "__res")); + Arg* __res = args_getArg(arg_obj->list, "__res"); + Arg* res = NULL; + if (NULL != __res) { + res = arg_copy(__res); + } + if (NULL != obj_new) { + arg_deinit(obj_new); + } + if (NULL == res) { + return arg_newNull(); + } + return res; } return arg_newNull(); } @@ -353,6 +410,25 @@ Arg* __vm_slice(PikaObj* self, Arg* end, Arg* obj, Arg* start, int step) { } return sliced_arg; } + + if (argType_isObject(arg_getType(obj))) { + PikaObj* arg_obj = arg_getPtr(obj); + PikaObj* New_PikaStdData_List(Args * args); + PikaObj* New_PikaStdData_Tuple(Args * args); + if (arg_obj->constructor == New_PikaStdData_List || + arg_obj->constructor == New_PikaStdData_Tuple) { + PikaObj* sliced_obj = newNormalObj((NewFun)arg_obj->constructor); + __vm_List___init__(sliced_obj); + for (int i = start_i; i < end_i; i++) { + Arg* i_arg = arg_newInt(i); + Arg* item_arg = __vm_get(self, i_arg, obj); + __vm_List_append(sliced_obj, item_arg); + arg_deinit(item_arg); + arg_deinit(i_arg); + } + return arg_newObj(sliced_obj); + } + } return arg_newNull(); #else return __vm_get(self, start, obj); @@ -401,8 +477,8 @@ static Arg* VM_instruction_handler_TRY(PikaObj* self, VMState* vm, char* data, Arg* arg_ret_reg) { - pika_assert(NULL != vm->try_info); - vm->try_info->try_state = TRY_STATE_INNER; + pika_assert(NULL != vm->run_state); + vm->run_state->try_state = TRY_STATE_INNER; return NULL; } @@ -417,7 +493,7 @@ static Arg* VM_instruction_handler_NTR(PikaObj* self, VMState* vm, char* data, Arg* arg_ret_reg) { - vm->try_info->try_state = TRY_STATE_NONE; + vm->run_state->try_state = TRY_STATE_NONE; return NULL; } @@ -485,20 +561,12 @@ static Arg* VM_instruction_handler_GER(PikaObj* self, return err_arg; } -static int32_t __foreach_handler_deinitTuple(Arg* argEach, Args* context) { - if (arg_getType(argEach) == ARG_TYPE_TUPLE) { - PikaTuple* tuple = arg_getPtr(argEach); - tuple_deinit(tuple); - } - return PIKA_RES_OK; -} - Arg* _obj_runMethodArgWithState(PikaObj* self, - PikaObj* method_args_obj, + PikaObj* locals, Arg* method_arg, - TryInfo* try_state, + RunState* run_state, Arg* ret_arg_reg) { - pika_assert(NULL != try_state); + pika_assert(NULL != run_state); Arg* return_arg = NULL; /* get method Ptr */ Method method_ptr = methodArg_getPtr(method_arg); @@ -519,62 +587,79 @@ Arg* _obj_runMethodArgWithState(PikaObj* self, /* run method */ if (method_type == ARG_TYPE_METHOD_NATIVE) { /* native method */ - method_ptr(self, method_args_obj->list); + method_ptr(self, locals->list); /* get method return */ return_arg = arg_copy_noalloc( - args_getArg(method_args_obj->list, (char*)"return"), ret_arg_reg); + args_getArg(locals->list, (char*)"return"), ret_arg_reg); } else if (method_type == ARG_TYPE_METHOD_NATIVE_CONSTRUCTOR) { /* native method */ - method_ptr(self, method_args_obj->list); + method_ptr(self, locals->list); /* get method return */ return_arg = arg_copy_noalloc( - args_getArg(method_args_obj->list, (char*)"return"), ret_arg_reg); + args_getArg(locals->list, (char*)"return"), ret_arg_reg); } else { /* static method and object method */ /* byteCode */ uintptr_t insturctArray_start = (uintptr_t)instructArray_getByOffset( &(method_bytecodeFrame->instruct_array), 0); uint16_t pc = (uintptr_t)method_ptr - insturctArray_start; - method_args_obj = __pikaVM_runByteCodeFrameWithState( - self, method_args_obj, self, method_bytecodeFrame, pc, try_state); + locals = __pikaVM_runByteCodeFrameWithState( + self, locals, self, method_bytecodeFrame, pc, run_state); /* get method return */ return_arg = arg_copy_noalloc( - args_getArg(method_args_obj->list, (char*)"return"), ret_arg_reg); + args_getArg(locals->list, (char*)"return"), ret_arg_reg); } - args_foreach(method_args_obj->list, __foreach_handler_deinitTuple, NULL); return return_arg; } Arg* obj_runMethodArgWithState(PikaObj* self, PikaObj* method_args_obj, Arg* method_arg, - TryInfo* try_state) { + RunState* try_state) { return _obj_runMethodArgWithState(self, method_args_obj, method_arg, try_state, NULL); } Arg* obj_runMethodArgWithState_noalloc(PikaObj* self, - PikaObj* method_args_obj, + PikaObj* locals, Arg* method_arg, - TryInfo* try_state, + RunState* run_state, Arg* ret_arg_reg) { - return _obj_runMethodArgWithState(self, method_args_obj, method_arg, - try_state, ret_arg_reg); + return _obj_runMethodArgWithState(self, locals, method_arg, run_state, + ret_arg_reg); } Arg* obj_runMethodArg(PikaObj* self, PikaObj* method_args_obj, Arg* method_arg) { - TryInfo try_info = {.try_state = TRY_STATE_NONE, - .try_result = TRY_RESULT_NONE}; + RunState run_state = {.try_state = TRY_STATE_NONE, + .try_result = TRY_RESULT_NONE}; return obj_runMethodArgWithState(self, method_args_obj, method_arg, - &try_info); + &run_state); +} + +char* _loadDefaultArgs(char* type_list, + char* arg_name, + PikaDict* dict, + Args* locals) { + while (strIsContain(arg_name, '=')) { + strPopLastToken(arg_name, '='); + /* load default arg from dict */ + if (dict != NULL) { + Arg* default_arg = dict_getArg(dict, arg_name); + if (default_arg != NULL) { + args_setArg(locals, arg_copy(default_arg)); + } + } + arg_name = strPopLastToken(type_list, ','); + } + return arg_name; } static int VMState_loadArgsFromMethodArg(VMState* vm, PikaObj* method_host_obj, - Args* args, + Args* locals, Arg* method_arg, char* method_name, int arg_num_used) { @@ -583,13 +668,14 @@ static int VMState_loadArgsFromMethodArg(VMState* vm, char _buffs2[PIKA_LINE_BUFF_SIZE / 2] = {0}; char* buffs2 = (char*)_buffs2; uint8_t arg_num_dec = 0; - PIKA_BOOL is_variable = PIKA_FALSE; - PIKA_BOOL is_get_variable_arg = PIKA_FALSE; + PIKA_BOOL vars_or_keys_or_default = PIKA_FALSE; uint8_t arg_num = 0; ArgType method_type = ARG_TYPE_UNDEF; uint8_t arg_num_input = 0; PikaTuple* tuple = NULL; + PikaDict* dict = NULL; char* variable_tuple_name = NULL; + char* keyword_dict_name = NULL; char* type_list_buff = NULL; int variable_arg_start = 0; /* get method type list */ @@ -604,11 +690,6 @@ static int VMState_loadArgsFromMethodArg(VMState* vm, } method_type = arg_getType(method_arg); - /* check variable */ - if (strIsContain(type_list, '*')) { - is_variable = PIKA_TRUE; - } - /* get arg_num_dec */ if (strEqu("", type_list)) { arg_num_dec = 0; @@ -621,93 +702,128 @@ static int VMState_loadArgsFromMethodArg(VMState* vm, } arg_num_input = VMState_getInputArgNum(vm); + /* check variable */ + if (strIsContain(type_list, '*') || strIsContain(type_list, '=')) { + vars_or_keys_or_default = PIKA_TRUE; + } + /* check arg num */ if (method_type == ARG_TYPE_METHOD_NATIVE_CONSTRUCTOR || method_type == ARG_TYPE_METHOD_CONSTRUCTOR || - is_variable == PIKA_TRUE) { + vars_or_keys_or_default == PIKA_TRUE || arg_num_used != 0) { /* skip for constrctor */ /* skip for variable args */ + /* arg_num_used != 0 means it is a factory method */ } else { - /* check arg num decleard and input */ + /* check arg num declared and input */ if (arg_num_dec != arg_num_input - arg_num_used) { VMState_setErrorCode(vm, PIKA_RES_ERR_INVALID_PARAM); __platform_printf( "TypeError: %s() takes %d positional argument but %d were " "given\r\n", - method_name, arg_num_dec, arg_num_input); + method_name, arg_num_dec, arg_num_input - arg_num_used); goto exit; } } - if (PIKA_TRUE == is_variable) { + if (PIKA_TRUE == vars_or_keys_or_default) { arg_num = arg_num_input; } else { arg_num = arg_num_dec; } - if (is_variable) { - /* get variable tuple name */ + if (vars_or_keys_or_default) { type_list_buff = strCopy(buffs2, type_list); + int default_num = strCountSign(type_list_buff, '='); variable_arg_start = 0; for (int i = 0; i < arg_num_dec; i++) { char* arg_def = strPopLastToken(type_list_buff, ','); - if (strIsStartWith(arg_def, "*")) { + if (arg_def[0] == '*' && arg_def[1] != '*') { + /* get variable tuple name */ /* skip the '*' */ variable_tuple_name = arg_def + 1; - variable_arg_start = arg_num_dec - i - 1; - is_get_variable_arg = PIKA_TRUE; - break; + variable_arg_start = arg_num_dec - i - 1 - default_num; + /* create tuple */ + if (NULL == tuple) { + tuple = New_tuple(); + /* remove the format arg */ + strPopLastToken(type_list, ','); + } + continue; + } + if (arg_def[0] == '*' && arg_def[1] == '*') { + /* get keyword dict name */ + keyword_dict_name = arg_def + 2; + dict = New_dict(); + /* remove the format arg */ + strPopLastToken(type_list, ','); + continue; } } } - /* found variable arg */ - if (PIKA_TRUE == is_get_variable_arg) { - tuple = New_tuple(); - strPopLastToken(type_list, ','); - } - /* load pars */ for (int i = 0; i < arg_num; i++) { - char* arg_name = NULL; - if (arg_num - i <= variable_arg_start) { - is_get_variable_arg = PIKA_FALSE; - } - if (PIKA_FALSE == is_get_variable_arg) { - char* arg_def = strPopLastToken(type_list, ','); - strPopLastToken(arg_def, ':'); - arg_name = arg_def; - } else { - /* clear the variable arg name */ - arg_name = ""; - } Arg* call_arg = stack_popArg_alloc(&(vm->stack)); - call_arg = arg_setName(call_arg, arg_name); + /* load the keyword arg */ + if (call_arg != NULL && arg_getIsKeyword(call_arg)) { + if (NULL == dict) { + dict = New_dict(); + } + arg_setIsKeyword(call_arg, PIKA_FALSE); + dict_setArg(dict, call_arg); + continue; + } + /* load the variable arg */ - if (PIKA_TRUE == is_get_variable_arg) { + if (tuple != NULL && (arg_num - i > variable_arg_start)) { list_append(&tuple->super, call_arg); /* the append would copy the arg */ arg_deinit(call_arg); - } else { - /* load normal arg */ - args_setArg(args, call_arg); + continue; } + + char* arg_name = strPopLastToken(type_list, ','); + + arg_name = _loadDefaultArgs(type_list, arg_name, dict, locals); + + /* skip type hint */ + strPopLastToken(arg_name, ':'); + /* load normal arg */ + args_setArg(locals, arg_setName(call_arg, arg_name)); } - if (PIKA_TRUE == is_variable) { + if (strIsContain(type_list, '=')) { + char* arg_name = strPopLastToken(type_list, ','); + _loadDefaultArgs(type_list, arg_name, dict, locals); + } + + if (tuple != NULL) { list_reverse(&tuple->super); /* load variable tuple */ PikaObj* New_PikaStdData_Tuple(Args * args); PikaObj* tuple_obj = newNormalObj(New_PikaStdData_Tuple); obj_setPtr(tuple_obj, "list", tuple); - args_setPtrWithType(args, variable_tuple_name, ARG_TYPE_OBJECT, + args_setPtrWithType(locals, variable_tuple_name, ARG_TYPE_OBJECT, tuple_obj); } + if (dict != NULL) { + if (NULL == keyword_dict_name) { + keyword_dict_name = "__kwargs"; + } + /* load keyword dict */ + PikaObj* New_PikaStdData_Dict(Args * args); + PikaObj* dict_obj = newNormalObj(New_PikaStdData_Dict); + obj_setPtr(dict_obj, "dict", dict); + args_setPtrWithType(locals, keyword_dict_name, ARG_TYPE_OBJECT, + dict_obj); + } + /* load 'self' as the first arg when call object method */ if (method_type == ARG_TYPE_METHOD_OBJECT) { Arg* call_arg = arg_setRef(NULL, "self", method_host_obj); - args_setArg(args, call_arg); + args_setArg(locals, call_arg); } exit: return arg_num; @@ -753,7 +869,7 @@ static Arg* _vm_create_list_or_tuple(PikaObj* self, arg_deinit(arg); } stack_deinit(&stack); - return arg_newPtr(ARG_TYPE_OBJECT, list); + return arg_newObj(list); #else return VM_instruction_handler_NON(self, vm, "", NULL); #endif @@ -767,6 +883,9 @@ static Arg* VM_instruction_handler_LST(PikaObj* self, } void __vm_Dict___init__(PikaObj* self) { + if (obj_isArgExist(self, "dict")) { + return; + } PikaDict* dict = New_dict(); PikaDict* keys = New_dict(); obj_setPtr(self, "dict", dict); @@ -810,7 +929,7 @@ static Arg* VM_instruction_handler_DCT(PikaObj* self, arg_deinit(val_arg); } stack_deinit(&stack); - return arg_newPtr(ARG_TYPE_OBJECT, dict); + return arg_newObj(dict); #else return VM_instruction_handler_NON(self, vm, data, arg_ret_reg); #endif @@ -827,29 +946,94 @@ static Arg* VM_instruction_handler_RET(PikaObj* self, return NULL; } +#if !PIKA_NANO_ENABLE +static char* _find_super_class_name(VMState* vm) { + /* find super class */ + int offset = 0; + char* super_class_name = NULL; + while (1) { + offset -= instructUnit_getSize(); + if (vm->pc + offset >= (int)VMState_getInstructArraySize(vm)) { + return 0; + } + if ((CLS == VMstate_getInstructWithOffset(vm, offset))) { + break; + } + } + + while (1) { + offset += instructUnit_getSize(); + if (vm->pc + offset >= (int)VMState_getInstructArraySize(vm)) { + return 0; + } + if ((RUN == instructUnit_getInstruct( + VMState_getInstructUnitWithOffset(vm, offset)))) { + super_class_name = VMState_getConstWithOffset(vm, offset); + return super_class_name; + } + } +} +#endif + +#if !PIKA_NANO_ENABLE +static char* _find_self_name(VMState* vm) { + /* find super class */ + int offset = 0; + char* self_name = NULL; + while (1) { + offset -= instructUnit_getSize(); + if (vm->pc + offset >= (int)VMState_getInstructArraySize(vm)) { + return 0; + } + if ((CLS == VMstate_getInstructWithOffset(vm, offset))) { + break; + } + } + + while (1) { + offset += instructUnit_getSize(); + if (vm->pc + offset >= (int)VMState_getInstructArraySize(vm)) { + return 0; + } + if ((OUT == instructUnit_getInstruct( + VMState_getInstructUnitWithOffset(vm, offset)))) { + self_name = VMState_getConstWithOffset(vm, offset); + return self_name; + } + } +} +#endif + static Arg* VM_instruction_handler_RUN(PikaObj* self, VMState* vm, char* data, Arg* arg_ret_reg) { Arg* return_arg = NULL; VMParameters* sub_locals = NULL; - char* methodPath = data; - PikaObj* method_host_obj = NULL; - Arg* method_arg = NULL; + char* run_path = data; + PikaObj* method_host = NULL; + PikaObj* obj_this = NULL; + Arg* method = NULL; Arg* host_arg = NULL; - PIKA_BOOL isClass = PIKA_FALSE; + PIKA_BOOL is_temp = PIKA_FALSE; + PIKA_BOOL skip_init = PIKA_FALSE; char* sys_out; int arg_num_used = 0; arg_newReg(arg_reg1, 64); - TryInfo sub_try_info = {.try_state = TRY_STATE_NONE, - .try_result = TRY_RESULT_NONE}; - pika_assert(NULL != vm->try_info); + RunState sub_run_state = {.try_state = vm->run_state->try_state, + .try_result = TRY_RESULT_NONE}; + pika_assert(NULL != vm->run_state); - /* transfer try_state */ - sub_try_info.try_state = vm->try_info->try_state; + /* inhert */ + if (vm->pc - 2 * (int)instructUnit_getSize() >= 0) { + if (CLS == VMstate_getInstructWithOffset( + vm, -2 * (int)instructUnit_getSize())) { + skip_init = PIKA_TRUE; + } + } /* tuple or single arg */ - if (data[0] == 0) { + if (run_path[0] == 0) { if (VMState_getInputArgNum(vm) < 2) { /* return arg directly */ Arg* arg1 = stack_popArg(&(vm->stack), &arg_reg1); @@ -862,20 +1046,37 @@ static Arg* VM_instruction_handler_RUN(PikaObj* self, goto exit; } +#if !PIKA_NANO_ENABLE + /* support for super() */ + if (strEqu(run_path, "super")) { + run_path = _find_super_class_name(vm); + vm->in_super = PIKA_TRUE; + skip_init = PIKA_TRUE; + } +#endif + /* return tiny obj */ - if (strEqu(data, "TinyObj")) { + if (strEqu(run_path, "TinyObj")) { return_arg = arg_newMetaObj(New_TinyObj); goto exit; } +#if !PIKA_NANO_ENABLE + if (!skip_init && vm->in_super) { + vm->in_super = PIKA_FALSE; + obj_this = obj_getPtr(vm->locals, _find_self_name(vm)); + } +#endif + /* get method host obj from reg */ - if (NULL == method_host_obj && _checkLReg(data)) { - uint8_t reg_index = _getLRegIndex(data); - method_host_obj = vm->lreg[reg_index]; + if (NULL == method_host && _checkLReg(run_path)) { + uint8_t reg_index = _getLRegIndex(run_path); + method_host = vm->lreg[reg_index]; } +#if !PIKA_NANO_ENABLE /* get method host obj from stack */ - if (NULL == method_host_obj && methodPath[0] == '.') { + if (NULL == method_host && run_path[0] == '.') { /* get method host obj from stack */ Arg* stack_tmp[PIKA_ARG_NUM_MAX] = {0}; int arg_num = VMState_getInputArgNum(vm); @@ -890,8 +1091,8 @@ static Arg* VM_instruction_handler_RUN(PikaObj* self, stack_tmp[i] = stack_popArg_alloc(&(vm->stack)); } host_arg = stack_tmp[arg_num - 1]; - if (argType_isObject(arg_getType(host_arg))) { - method_host_obj = arg_getPtr(host_arg); + method_host = _arg_to_obj(host_arg, &is_temp); + if (NULL != method_host) { arg_num_used++; } /* push back other args to stack */ @@ -899,47 +1100,51 @@ static Arg* VM_instruction_handler_RUN(PikaObj* self, stack_pushArg(&(vm->stack), stack_tmp[i]); } } +#endif /* get method host obj from self */ - if (NULL == method_host_obj) { - method_host_obj = obj_getHostObjWithIsClass(self, methodPath, &isClass); + if (NULL == method_host) { + method_host = obj_getHostObjWithIsTemp(self, run_path, &is_temp); } /* get method host obj from local scope */ - if (NULL == method_host_obj) { - method_host_obj = - obj_getHostObjWithIsClass(vm->locals, methodPath, &isClass); + if (NULL == method_host) { + method_host = obj_getHostObjWithIsTemp(vm->locals, run_path, &is_temp); } /* method host obj is not found */ - if (NULL == method_host_obj) { + if (NULL == method_host) { /* error, not found object */ VMState_setErrorCode(vm, PIKA_RES_ERR_ARG_NO_FOUND); - __platform_printf("Error: method '%s' no found.\r\n", data); + __platform_printf("Error: method '%s' no found.\r\n", run_path); goto exit; } - /* get method in local */ - method_arg = - obj_getMethodArg_noalloc(method_host_obj, methodPath, &arg_reg1); - - if (NULL == method_arg) { - /* get method in locals */ - method_arg = - obj_getMethodArg_noalloc(vm->locals, methodPath, &arg_reg1); + /* get object this */ + if (NULL == obj_this) { + obj_this = method_host; } - if (NULL == method_arg) { - /* get method in global */ - method_arg = - obj_getMethodArg_noalloc(vm->globals, methodPath, &arg_reg1); + /* get method in object */ + if (NULL == method) { + method = obj_getMethodArg_noalloc(method_host, run_path, &arg_reg1); + } + + /* get method in locals */ + if (NULL == method) { + method = obj_getMethodArg_noalloc(vm->locals, run_path, &arg_reg1); + } + + /* get method in global */ + if (NULL == method) { + method = obj_getMethodArg_noalloc(vm->globals, run_path, &arg_reg1); } /* assert method type */ - if (NULL == method_arg || ARG_TYPE_NONE == arg_getType(method_arg)) { + if (NULL == method || ARG_TYPE_NONE == arg_getType(method)) { /* error, method no found */ VMState_setErrorCode(vm, PIKA_RES_ERR_ARG_NO_FOUND); - __platform_printf("NameError: name '%s' is not defined\r\n", data); + __platform_printf("NameError: name '%s' is not defined\r\n", run_path); goto exit; } @@ -948,7 +1153,7 @@ static Arg* VM_instruction_handler_RUN(PikaObj* self, /* load args from vmState to sub_local->list */ arg_num_used += VMState_loadArgsFromMethodArg( - vm, method_host_obj, sub_locals->list, method_arg, data, arg_num_used); + vm, obj_this, sub_locals->list, method, run_path, arg_num_used); /* load args faild */ if (vm->error_code != 0) { @@ -956,12 +1161,17 @@ static Arg* VM_instruction_handler_RUN(PikaObj* self, } /* run method arg */ - return_arg = obj_runMethodArgWithState_noalloc( - method_host_obj, sub_locals, method_arg, &sub_try_info, arg_ret_reg); + return_arg = obj_runMethodArgWithState_noalloc(obj_this, sub_locals, method, + &sub_run_state, arg_ret_reg); + if (skip_init) { + if (arg_getType(return_arg) == ARG_TYPE_OBJECT_NEW) { + arg_setType(return_arg, ARG_TYPE_OBJECT); + } + } - if (sub_try_info.try_result != TRY_RESULT_NONE) { + if (sub_run_state.try_result != TRY_RESULT_NONE) { /* try result */ - vm->error_code = sub_try_info.try_result; + vm->error_code = sub_run_state.try_result; } /* __init__() */ @@ -983,7 +1193,7 @@ static Arg* VM_instruction_handler_RUN(PikaObj* self, goto init_exit; } return_arg_init = obj_runMethodArgWithState(new_obj, sub_locals, - method_arg, &sub_try_info); + method_arg, &sub_run_state); init_exit: if (NULL != return_arg_init) { arg_deinit(return_arg_init); @@ -993,21 +1203,21 @@ static Arg* VM_instruction_handler_RUN(PikaObj* self, } /* transfer sysOut */ - sys_out = obj_getSysOut(method_host_obj); + sys_out = obj_getSysOut(obj_this); if (NULL != sys_out) { args_setSysOut(vm->locals->list, sys_out); } /* transfer errCode */ - if (0 != obj_getErrorCode(method_host_obj)) { + if (0 != obj_getErrorCode(obj_this)) { /* method error */ VMState_setErrorCode(vm, PIKA_RES_ERR_RUNTIME_ERROR); } goto exit; exit: - if (NULL != method_arg) { - arg_deinit(method_arg); + if (NULL != method) { + arg_deinit(method); } if (NULL != sub_locals) { obj_deinit(sub_locals); @@ -1015,9 +1225,9 @@ exit: if (NULL != host_arg) { arg_deinit(host_arg); } - if (NULL != method_host_obj && isClass) { + if (NULL != method_host && is_temp) { /* class method */ - obj_deinit(method_host_obj); + obj_deinit(method_host); } return return_arg; @@ -1201,20 +1411,14 @@ static Arg* VM_instruction_handler_SER(PikaObj* self, return NULL; } -static Arg* VM_instruction_handler_JEZ(PikaObj* self, - VMState* vm, - char* data, - Arg* arg_ret_reg) { +static Arg* _VM_JEZ(PikaObj* self, + VMState* vm, + char* data, + Arg* arg_ret_reg, + int pika_assert) { int thisBlockDeepth = VMState_getBlockDeepthNow(vm); int jmp_expect = fast_atoi(data); - arg_newReg(pika_assertArg_reg, PIKA_ARG_BUFF_SIZE); - Arg* pika_assertArg = stack_popArg(&(vm->stack), &pika_assertArg_reg); - int pika_assert = 0; - if (NULL != pika_assertArg) { - pika_assert = arg_getInt(pika_assertArg); - } - arg_deinit(pika_assertArg); - vm->ireg[thisBlockDeepth] = !pika_assert; + vm->ireg[thisBlockDeepth] = (PIKA_BOOL)!pika_assert; if (0 == pika_assert) { /* jump */ @@ -1230,6 +1434,34 @@ static Arg* VM_instruction_handler_JEZ(PikaObj* self, return NULL; } +static Arg* VM_instruction_handler_JEZ(PikaObj* self, + VMState* vm, + char* data, + Arg* arg_ret_reg) { + arg_newReg(pika_assertArg_reg, PIKA_ARG_BUFF_SIZE); + Arg* pika_assertArg = stack_popArg(&(vm->stack), &pika_assertArg_reg); + int pika_assert = 0; + if (NULL != pika_assertArg) { + pika_assert = arg_getInt(pika_assertArg); + } + arg_deinit(pika_assertArg); + return _VM_JEZ(self, vm, data, arg_ret_reg, pika_assert); +} + +static Arg* VM_instruction_handler_JNZ(PikaObj* self, + VMState* vm, + char* data, + Arg* arg_ret_reg) { + arg_newReg(pika_assertArg_reg, PIKA_ARG_BUFF_SIZE); + Arg* pika_assertArg = stack_popArg(&(vm->stack), &pika_assertArg_reg); + int pika_assert = 0; + if (NULL != pika_assertArg) { + pika_assert = arg_getInt(pika_assertArg); + } + arg_deinit(pika_assertArg); + return _VM_JEZ(self, vm, data, arg_ret_reg, !pika_assert); +} + static uint8_t VMState_getInputArgNum(VMState* vm) { InstructUnit* ins_unit_now = VMState_getInstructNow(vm); uint8_t invoke_deepth_this = instructUnit_getInvokeDeepth(ins_unit_now); @@ -1529,6 +1761,34 @@ static Arg* VM_instruction_handler_OPT(PikaObj* self, } goto exit; } + if (argType_isObject(op.t2)) { + PikaObj* obj2 = arg_getPtr(op.a2); + Arg* __contains__ = obj_getMethodArg(obj2, "__contains__"); + if (NULL != __contains__) { + arg_deinit(__contains__); + /* clang-format off */ + PIKA_PYTHON( + __res = __contains__(__others) + ) + obj_setArg(obj2, "__others", op.a1); + /* clang-format on */ + const uint8_t bytes[] = { + 0x0c, 0x00, /* instruct array size */ + 0x10, 0x81, 0x01, 0x00, 0x00, 0x02, 0x0a, 0x00, 0x00, 0x04, + 0x17, 0x00, + /* instruct array */ + 0x1d, 0x00, /* const pool size */ + 0x00, 0x5f, 0x5f, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x73, 0x00, + 0x5f, 0x5f, 0x63, 0x6f, 0x6e, 0x74, 0x61, 0x69, 0x6e, 0x73, + 0x5f, 0x5f, 0x00, 0x5f, 0x5f, 0x72, 0x65, 0x73, + 0x00, /* const pool */ + }; + pikaVM_runByteCode(obj2, (uint8_t*)bytes); + op.res = arg_setInt(op.res, "", obj_getInt(obj2, "__res")); + goto exit; + } + } + VMState_setErrorCode(vm, PIKA_RES_ERR_OPERATION_FAILED); args_setSysOut(vm->locals->list, "Operation 'in' is not supported for this " @@ -1708,7 +1968,8 @@ static Arg* __VM_instruction_handler_DEF(PikaObj* self, int offset = 0; /* byteCode */ while (1) { - InstructUnit* ins_unit_now = VMState_getInstructWithOffset(vm, offset); + InstructUnit* ins_unit_now = + VMState_getInstructUnitWithOffset(vm, offset); if (!instructUnit_getIsNewLine(ins_unit_now)) { offset += instructUnit_getSize(); continue; @@ -1754,6 +2015,9 @@ static Arg* VM_instruction_handler_RIS(PikaObj* self, VMState* vm, char* data, Arg* arg_ret_reg) { +#if PIKA_NANO_ENABLE + return NULL; +#endif Arg* err_arg = stack_popArg_alloc(&(vm->stack)); PIKA_RES err = (PIKA_RES)arg_getInt(err_arg); VMState_setErrorCode(vm, err); @@ -1765,6 +2029,9 @@ static Arg* VM_instruction_handler_ASS(PikaObj* self, VMState* vm, char* data, Arg* arg_ret_reg) { +#if PIKA_NANO_ENABLE + return NULL; +#endif arg_newReg(reg1, PIKA_ARG_BUFF_SIZE); arg_newReg(reg2, PIKA_ARG_BUFF_SIZE); Arg* arg1 = NULL; @@ -1782,7 +2049,7 @@ static Arg* VM_instruction_handler_ASS(PikaObj* self, if (arg_getType(arg1) == ARG_TYPE_INT && arg_getInt(arg1) == 0) { stack_pushArg(&vm->stack, arg_newInt(PIKA_RES_ERR_ASSERT)); res = VM_instruction_handler_RIS(self, vm, data, arg_ret_reg); - if (vm->try_info->try_state == TRY_STATE_NONE) { + if (vm->run_state->try_state == TRY_STATE_NONE) { if (arg_num == 1) { __platform_printf("AssertionError\n", data); } @@ -1932,14 +2199,17 @@ static int pikaVM_runInstructUnit(PikaObj* self, int32_t pc_next = vm->pc + instructUnit_getSize(); char* data = VMState_getConstWithInstructUnit(vm, ins_unit); /* run instruct */ - pika_assert(NULL != vm->try_info); + pika_assert(NULL != vm->run_state); return_arg = VM_instruct_handler_table[instruct](self, vm, data, &ret_reg); if (vm->error_code != PIKA_RES_OK) { /* raise jmp */ - if (vm->try_info->try_state == TRY_STATE_INNER) { + if (vm->run_state->try_state == TRY_STATE_INNER) { vm->jmp = VM_JMP_RAISE; - }; + } else { + /* exit */ + vm->jmp = VM_JMP_EXIT; + } } if (NULL != return_arg) { @@ -1962,18 +2232,20 @@ nextLine: pc_next = vm->pc + VMState_getAddrOffsetOfContinue(vm); goto exit; } - /* raise */ +/* raise */ +#if !PIKA_NANO_ENABLE if (VM_JMP_RAISE == vm->jmp) { int offset = VMState_getAddrOffsetOfRaise(vm); if (0 == offset) { /* can not found end of try, return */ pc_next = VM_PC_EXIT; - vm->try_info->try_result = TRY_RESULT_RAISE; + vm->run_state->try_result = TRY_RESULT_RAISE; goto exit; } pc_next = vm->pc + offset; goto exit; } +#endif /* static jmp */ if (vm->jmp != 0) { pc_next = vm->pc + VMState_getAddrOffsetFromJmp(vm); @@ -2049,12 +2321,12 @@ static VMParameters* __pikaVM_runPyLines_or_byteCode(PikaObj* self, /* save 'def' and 'class' to heap */ if ((strIsStartWith(py_lines, "def ")) || (strIsStartWith(py_lines, "class "))) { - char* declear_name = strsGetFirstToken(&buffs, py_lines, ':'); + char* declare_name = strsGetFirstToken(&buffs, py_lines, ':'); /* load bytecode to heap */ - args_setHeapStruct(self->list, declear_name, bytecode_frame_stack, + args_setHeapStruct(self->list, declare_name, bytecode_frame_stack, byteCodeFrame_deinit); /* get bytecode_ptr from heap */ - bytecode_frame_p = args_getHeapStruct(self->list, declear_name); + bytecode_frame_p = args_getHeapStruct(self->list, declare_name); } else { /* get bytecode_ptr from stack */ bytecode_frame_p = &bytecode_frame_stack; @@ -2413,8 +2685,8 @@ static VMParameters* __pikaVM_runByteCodeFrameWithState( VMParameters* globals, ByteCodeFrame* bytecode_frame, uint16_t pc, - TryInfo* try_info) { - pika_assert(NULL != try_info); + RunState* run_state) { + pika_assert(NULL != run_state); int size = bytecode_frame->instruct_array.size; /* locals is the local scope */ VMState vm = { @@ -2427,12 +2699,20 @@ static VMParameters* __pikaVM_runByteCodeFrameWithState( .error_code = PIKA_RES_OK, .line_error_code = PIKA_RES_OK, .try_error_code = PIKA_RES_OK, - .try_info = try_info, + .run_state = run_state, .ins_cnt = 0, + .in_super = PIKA_FALSE, }; stack_init(&(vm.stack)); VMState_initReg(&vm); + if (PikaVMSignal.vm_cnt == 0) { + pks_vmSignal_setCtrlElear(); + } + PikaVMSignal.vm_cnt++; while (vm.pc < size) { + if (VMSignal_getCtrl() == VM_SIGNAL_CTRL_EXIT) { + vm.pc = VM_PC_EXIT; + } if (vm.pc == VM_PC_EXIT) { break; } @@ -2460,11 +2740,11 @@ static VMParameters* __pikaVM_runByteCodeFrameWithState( } head_ins_unit--; } - if (vm.try_info->try_state) { + if (vm.run_state->try_state) { vm.try_error_code = vm.error_code; } /* print inses of a line */ - if (!vm.try_info->try_state) { + if (!vm.run_state->try_state) { while (1) { if (head_ins_unit != this_ins_unit) { __platform_printf(" "); @@ -2485,16 +2765,17 @@ static VMParameters* __pikaVM_runByteCodeFrameWithState( } VMState_solveUnusedStack(&vm); stack_deinit(&(vm.stack)); + PikaVMSignal.vm_cnt--; return locals; } VMParameters* pikaVM_runByteCodeFrame(PikaObj* self, ByteCodeFrame* byteCode_frame) { - TryInfo try_info = {.try_state = TRY_STATE_NONE, - .try_result = TRY_RESULT_NONE}; - try_info.try_state = TRY_STATE_NONE; + RunState run_state = {.try_state = TRY_STATE_NONE, + .try_result = TRY_RESULT_NONE}; + run_state.try_state = TRY_STATE_NONE; return __pikaVM_runByteCodeFrameWithState(self, self, self, byteCode_frame, - 0, &try_info); + 0, &run_state); } InstructUnit* instructArray_getByOffset(InstructArray* self, int32_t offset) { diff --git a/port/cmsis-pack/pikascript/pikascript-core/PikaVM.h b/port/cmsis-pack/pikascript/pikascript-core/PikaVM.h index 9fe0d4869..429ab18fb 100644 --- a/port/cmsis-pack/pikascript/pikascript-core/PikaVM.h +++ b/port/cmsis-pack/pikascript/pikascript-core/PikaVM.h @@ -57,8 +57,8 @@ typedef enum { TRY_RESULT_RAISE, } TRY_RESULT; -typedef struct TryInfo TryInfo; -struct TryInfo { +typedef struct RunState RunState; +struct RunState { TRY_STATE try_state; TRY_RESULT try_result; }; @@ -76,9 +76,10 @@ struct VMState { uint8_t line_error_code; uint8_t try_error_code; uint32_t ins_cnt; + PIKA_BOOL in_super; PikaObj* lreg[PIKA_REGIST_SIZE]; PIKA_BOOL ireg[PIKA_REGIST_SIZE]; - TryInfo* try_info; + RunState* run_state; }; typedef struct OperatorInfo OperatorInfo; @@ -97,6 +98,17 @@ struct OperatorInfo { VMState* vm; }; +typedef enum VM_SIGNAL_CTRL { + VM_SIGNAL_CTRL_NONE = 0, + VM_SIGNAL_CTRL_EXIT, +} VM_SIGNAL_CTRL; + +typedef struct VMSignal VMSignal; +struct VMSignal { + VM_SIGNAL_CTRL signal_ctrl; + int vm_cnt; +}; + VMParameters* pikaVM_run(PikaObj* self, char* pyLine); VMParameters* pikaVM_runAsm(PikaObj* self, char* pikaAsm); VMParameters* pikaVM_runByteCodeFrame(PikaObj* self, @@ -192,5 +204,8 @@ void __vm_List_append(PikaObj* self, Arg* arg); void __vm_List___init__(PikaObj* self); void __vm_Dict_set(PikaObj* self, Arg* arg, char* key); void __vm_Dict___init__(PikaObj* self); +VM_SIGNAL_CTRL VMSignal_getCtrl(void); +void pks_vm_exit(void); +void pks_vmSignal_setCtrlElear(void); #endif diff --git a/port/cmsis-pack/pikascript/pikascript-core/PikaVersion.h b/port/cmsis-pack/pikascript/pikascript-core/PikaVersion.h index 506a70601..e829a8341 100644 --- a/port/cmsis-pack/pikascript/pikascript-core/PikaVersion.h +++ b/port/cmsis-pack/pikascript/pikascript-core/PikaVersion.h @@ -1,5 +1,5 @@ #define PIKA_VERSION_MAJOR 1 -#define PIKA_VERSION_MINOR 10 -#define PIKA_VERSION_MICRO 6 +#define PIKA_VERSION_MINOR 11 +#define PIKA_VERSION_MICRO 0 -#define PIKA_EDIT_TIME "2022/08/30 18:40:40" +#define PIKA_EDIT_TIME "2022/09/16 15:33:53" diff --git a/port/cmsis-pack/pikascript/pikascript-core/__instruction_table.cfg b/port/cmsis-pack/pikascript/pikascript-core/__instruction_table.cfg index b4dbc8155..e69baf288 100644 --- a/port/cmsis-pack/pikascript/pikascript-core/__instruction_table.cfg +++ b/port/cmsis-pack/pikascript/pikascript-core/__instruction_table.cfg @@ -94,3 +94,5 @@ def_ins(SLC) def_ins(ASS) /* expect */ def_ins(EXP) +/* jump no zero */ +def_ins(JNZ) diff --git a/port/cmsis-pack/pikascript/pikascript-core/dataArg.c b/port/cmsis-pack/pikascript/pikascript-core/dataArg.c index b0dc62841..d545fd776 100644 --- a/port/cmsis-pack/pikascript/pikascript-core/dataArg.c +++ b/port/cmsis-pack/pikascript/pikascript-core/dataArg.c @@ -57,6 +57,7 @@ static Arg* arg_init_hash(Hash nameHash, self->size = size; self->name_hash = nameHash; self->type = type; + self->flag = 0; arg_setSerialized(self, PIKA_TRUE); arg_setIsKeyword(self, PIKA_FALSE); __platform_memset(arg_getContent(self), 0, @@ -64,7 +65,7 @@ static Arg* arg_init_hash(Hash nameHash, if (NULL != content) { __platform_memcpy(arg_getContent(self), content, size); } - + pika_assert(self->flag < ARG_FLAG_MAX); return self; } @@ -82,6 +83,7 @@ void arg_init_stack(Arg* self, uint8_t* buffer, uint32_t size) { self->size = size; self->type = ARG_TYPE_UNDEF; self->name_hash = 0; + self->flag = 0; arg_setSerialized(self, PIKA_FALSE); arg_setIsKeyword(self, PIKA_FALSE); } @@ -129,6 +131,7 @@ Arg* arg_setNameHash(Arg* self, Hash nameHash) { } Arg* arg_setName(Arg* self, char* name) { + pika_assert(NULL != name); return arg_setNameHash(self, hash_time33(name)); } @@ -157,6 +160,7 @@ Arg* arg_setBytes(Arg* self, char* name, uint8_t* src, size_t size) { if (NULL != src) { __platform_memcpy((void*)((uintptr_t)dir + sizeof(size_t)), src, size); } + pika_assert(self->flag < ARG_FLAG_MAX); return self; } @@ -170,14 +174,26 @@ uint8_t* arg_getBytes(Arg* self) { return arg_getContent(self) + sizeof(size_t); } -void arg_printBytes(Arg* self) { - size_t bytes_size = arg_getBytesSize(self); - uint8_t* bytes = arg_getBytes(self); - __platform_printf("b\'"); +char* __printBytes(PikaObj* self, Arg* arg) { + Args buffs = {0}; + size_t bytes_size = arg_getBytesSize(arg); + uint8_t* bytes = arg_getBytes(arg); + Arg* str_arg = arg_newStr("b\'"); for (size_t i = 0; i < bytes_size; i++) { - __platform_printf("\\x%02x", bytes[i]); + char* str_item = strsFormat(&buffs, 16, "\\x%02x", bytes[i]); + str_arg = arg_strAppend(str_arg, str_item); } - __platform_printf("\'\r\n"); + str_arg = arg_strAppend(str_arg, "\'"); + char* str_res = obj_cacheStr(self, arg_getStr(str_arg)); + strsDeinit(&buffs); + arg_deinit(str_arg); + return str_res; +} + +void arg_printBytes(Arg* self) { + PikaObj* obj = New_PikaObj(); + __platform_printf("%s\r\n", __printBytes(obj, self)); + obj_deinit(obj); } size_t arg_getBytesSize(Arg* self) { @@ -279,6 +295,9 @@ int64_t arg_getInt(Arg* self) { } void* arg_getPtr(Arg* self) { + if (arg_getType(self) == ARG_TYPE_NONE) { + return NULL; + } if (NULL == arg_getContent(self)) { return NULL; } @@ -314,16 +333,18 @@ Arg* arg_copy(Arg* arg_src) { if (NULL == arg_src) { return NULL; } + pika_assert(arg_src->flag < ARG_FLAG_MAX); ArgType arg_type = arg_getType(arg_src); if (ARG_TYPE_OBJECT == arg_type) { obj_refcntInc((PikaObj*)arg_getPtr(arg_src)); } - Arg* argCopied = New_arg(NULL); - argCopied = arg_setContent(argCopied, arg_getContent(arg_src), - arg_getContentSize(arg_src)); - argCopied = arg_setNameHash(argCopied, arg_getNameHash(arg_src)); - argCopied = arg_setType(argCopied, arg_getType(arg_src)); - return argCopied; + Arg* arg_dict = New_arg(NULL); + arg_dict = arg_setContent(arg_dict, arg_getContent(arg_src), + arg_getContentSize(arg_src)); + arg_dict = arg_setNameHash(arg_dict, arg_getNameHash(arg_src)); + arg_dict = arg_setType(arg_dict, arg_getType(arg_src)); + arg_setIsKeyword(arg_dict, arg_getIsKeyword(arg_src)); + return arg_dict; } Arg* arg_copy_noalloc(Arg* arg_src, Arg* arg_dict) { @@ -346,6 +367,7 @@ Arg* arg_copy_noalloc(Arg* arg_src, Arg* arg_dict) { arg_getContentSize(arg_src)); arg_dict = arg_setNameHash(arg_dict, arg_getNameHash(arg_src)); arg_dict = arg_setType(arg_dict, arg_getType(arg_src)); + arg_setIsKeyword(arg_dict, arg_getIsKeyword(arg_src)); return arg_dict; } @@ -431,3 +453,27 @@ void arg_deinit(Arg* self) { /* free the ref */ arg_freeContent(self); } + +PIKA_BOOL arg_isEqual(Arg* self, Arg* other) { + if (NULL == self || NULL == other) { + return PIKA_FALSE; + } + if (arg_getType(self) != arg_getType(other)) { + return PIKA_FALSE; + } + if (arg_getType(self) == ARG_TYPE_OBJECT) { + if (arg_getPtr(self) != arg_getPtr(other)) { + return PIKA_FALSE; + } + } + if (arg_getType(self) == ARG_TYPE_STRING) { + if (strEqu(arg_getStr(self), arg_getStr(other))) { + return PIKA_TRUE; + } + } + if (0 != __platform_memcmp(arg_getContent(self), arg_getContent(other), + arg_getContentSize(self))) { + return PIKA_FALSE; + } + return PIKA_TRUE; +} diff --git a/port/cmsis-pack/pikascript/pikascript-core/dataArg.h b/port/cmsis-pack/pikascript/pikascript-core/dataArg.h index 7b22374a8..750c56cc2 100644 --- a/port/cmsis-pack/pikascript/pikascript-core/dataArg.h +++ b/port/cmsis-pack/pikascript/pikascript-core/dataArg.h @@ -65,8 +65,8 @@ typedef union { struct Arg { _arg_union _; uint32_t size; - uint8_t type; - PIKA_BOOL flag; + ArgType type; + uint8_t flag; Hash name_hash; uint8_t content[]; }; @@ -133,6 +133,7 @@ uint8_t argType_isObject(ArgType type); #define ARG_FLAG_MASK_SERIALIZED 0x01 #define ARG_FLAG_MASK_IsKeyword 0x02 +#define ARG_FLAG_MAX 0x04 #define arg_getNext(self) ((self)->_.next) #define arg_getSize(self) ((self)->size) @@ -170,3 +171,4 @@ uint8_t argType_isObject(ArgType type); arg_init_stack(&__name, __##__name##_buff, __size) void arg_init_stack(Arg* self, uint8_t* buffer, uint32_t size); +PIKA_BOOL arg_isEqual(Arg* self, Arg* other); diff --git a/port/cmsis-pack/pikascript/pikascript-core/dataArgs.c b/port/cmsis-pack/pikascript/pikascript-core/dataArgs.c index 01aaf86e6..c3056538d 100644 --- a/port/cmsis-pack/pikascript/pikascript-core/dataArgs.c +++ b/port/cmsis-pack/pikascript/pikascript-core/dataArgs.c @@ -34,6 +34,7 @@ #include "dataStrs.h" void args_deinit(Args* self) { + pika_assert(self != NULL); link_deinit(self); } @@ -87,6 +88,9 @@ PIKA_RES args_setStr(Args* self, char* name, char* strIn) { } PIKA_RES args_pushArg(Args* self, Arg* arg) { + if (NULL == arg) { + return PIKA_RES_ERR_ARG_NO_FOUND; + } Arg* new_arg = NULL; if (!arg_getSerialized(arg)) { new_arg = arg_copy(arg); @@ -368,6 +372,7 @@ Arg* args_getArg_hash(Args* self, Hash nameHash) { } Arg* args_getArg(Args* self, char* name) { + pika_assert(NULL != self); LinkNode* node = args_getNode(self, name); if (NULL == node) { return NULL; @@ -582,6 +587,9 @@ void* list_getPtr(PikaList* self, int index) { } PIKA_RES list_append(PikaList* self, Arg* arg) { + if (NULL == arg) { + return PIKA_RES_ERR_ARG_NO_FOUND; + } int top = args_getInt(&self->super, "top"); char buff[11]; char* topStr = fast_itoa(buff, top); @@ -593,10 +601,12 @@ PIKA_RES list_append(PikaList* self, Arg* arg) { } size_t list_getSize(PikaList* self) { + pika_assert(NULL != self); return args_getInt(&self->super, "top"); } void list_reverse(PikaList* self) { + pika_assert(NULL != self); int top = list_getSize(self); for (int i = 0; i < top / 2; i++) { Arg* arg_i = arg_copy(list_getArg(self, i)); @@ -695,12 +705,19 @@ exit: return res; } +/* tuple */ PikaTuple* args_getTuple(Args* self, char* name) { PikaObj* tuple_obj = args_getPtr(self, name); return obj_getPtr(tuple_obj, "list"); } -char* args_cacheStr(Args* self, char* str){ +/* dict */ +PikaDict* args_getDict(Args* self, char* name) { + PikaObj* tuple_obj = args_getPtr(self, name); + return obj_getPtr(tuple_obj, "dict"); +} + +char* args_cacheStr(Args* self, char* str) { args_setStr(self, "__str_cache", str); return args_getStr(self, "__str_cache"); } diff --git a/port/cmsis-pack/pikascript/pikascript-core/dataArgs.h b/port/cmsis-pack/pikascript/pikascript-core/dataArgs.h index a91ab2866..ae1446be7 100644 --- a/port/cmsis-pack/pikascript/pikascript-core/dataArgs.h +++ b/port/cmsis-pack/pikascript/pikascript-core/dataArgs.h @@ -185,6 +185,7 @@ char* strsFormatArg(Args* out_buffs, char* fmt, Arg* arg); PikaList* New_list(void); PikaTuple* New_tuple(void); PikaTuple* args_getTuple(Args* self, char* name); +PikaDict* args_getDict(Args* self, char* name); char* strsFormatList(Args* out_buffs, char* fmt, PikaList* list); char* args_cacheStr(Args* self, char* str); diff --git a/port/cmsis-pack/pikascript/pikascript-core/dataLink.c b/port/cmsis-pack/pikascript/pikascript-core/dataLink.c index f933216e9..52e9d3e9a 100644 --- a/port/cmsis-pack/pikascript/pikascript-core/dataLink.c +++ b/port/cmsis-pack/pikascript/pikascript-core/dataLink.c @@ -41,6 +41,7 @@ void __link_deinit_pyload(Link* self) { } void link_deinit(Link* self) { + pika_assert(self != NULL); __link_deinit_pyload(self); pikaFree(self, sizeof(Link)); } diff --git a/port/cmsis-pack/pikascript/pikascript-core/dataMemory.c b/port/cmsis-pack/pikascript/pikascript-core/dataMemory.c index 9a178bb2b..6a527eff9 100644 --- a/port/cmsis-pack/pikascript/pikascript-core/dataMemory.c +++ b/port/cmsis-pack/pikascript/pikascript-core/dataMemory.c @@ -29,7 +29,7 @@ #include "dataMemory.h" #include "PikaPlatform.h" -PikaMemInfo pikaMemInfo = {0}; +volatile PikaMemInfo pikaMemInfo = {0}; void* pikaMalloc(uint32_t size) { /* pika memory lock */ diff --git a/port/cmsis-pack/pikascript/pikascript-core/dataStack.c b/port/cmsis-pack/pikascript/pikascript-core/dataStack.c index 8c6ec876e..0f09a739a 100644 --- a/port/cmsis-pack/pikascript/pikascript-core/dataStack.c +++ b/port/cmsis-pack/pikascript/pikascript-core/dataStack.c @@ -60,7 +60,7 @@ int32_t stack_deinit(Stack* stack) { return 0; } -void stack_pushPyload(Stack* stack, Arg* content, size_t size) { +void stack_pushPyload(Stack* stack, Arg* in, size_t size) { size_t stack_size_after_push = size + (stack->sp - arg_getContent(stack->stack_pyload)); if (stack_size_after_push > stack->stack_totle_size) { @@ -74,14 +74,14 @@ void stack_pushPyload(Stack* stack, Arg* content, size_t size) { while (1) { } } - if (arg_getSerialized(content)) { - __platform_memcpy(stack->sp, content, size); + Arg* top = (Arg*)stack->sp; + if (arg_getSerialized(in)) { + __platform_memcpy(top, in, size); } else { - __platform_memcpy(stack->sp, content, sizeof(Arg)); - __platform_memcpy(stack->sp + sizeof(Arg), content->_.buffer, - size - sizeof(Arg)); + __platform_memcpy(top, in, sizeof(Arg)); + __platform_memcpy(top->content, in->_.buffer, size - sizeof(Arg)); /* transfer to serialized form */ - arg_setSerialized((Arg*)stack->sp, PIKA_TRUE); + arg_setSerialized(top, PIKA_TRUE); } stack->sp += size; } @@ -144,6 +144,7 @@ Arg* _stack_popArg(Stack* stack, Arg* arg_dict, PIKA_BOOL is_alloc) { if (argType_isObject(type)) { obj_refcntDec((PikaObj*)arg_getPtr(arg)); } + pika_assert(arg->flag < ARG_FLAG_MAX); return arg; } diff --git a/port/cmsis-pack/pikascript/pikascript-core/pika_config_valid.h b/port/cmsis-pack/pikascript/pikascript-core/pika_config_valid.h index 43e14a2be..dfc00ca72 100644 --- a/port/cmsis-pack/pikascript/pikascript-core/pika_config_valid.h +++ b/port/cmsis-pack/pikascript/pikascript-core/pika_config_valid.h @@ -64,6 +64,12 @@ #ifndef PIKA_SYNTAX_LEVEL #define PIKA_SYNTAX_LEVEL PIKA_SYNTAX_LEVEL_MINIMAL #endif + + + #ifndef PIKA_STRING_UTF8_ENABLE + #define PIKA_STRING_UTF8_ENABLE 0 + #endif + #endif /* default optimize */ @@ -290,6 +296,11 @@ #define PIKA_EXEC_ENABLE 1 #endif + /* support for UTF-8 in PikaStdData_String */ + #ifndef PIKA_STRING_UTF8_ENABLE + #define PIKA_STRING_UTF8_ENABLE 1 + #endif + /* configuration validation */ #endif diff --git a/port/cmsis-pack/pikascript/pikascript-lib/PikaStdLib/PikaStdData_Dict.c b/port/cmsis-pack/pikascript/pikascript-lib/PikaStdLib/PikaStdData_Dict.c index 12209138a..55a526a54 100644 --- a/port/cmsis-pack/pikascript/pikascript-lib/PikaStdLib/PikaStdData_Dict.c +++ b/port/cmsis-pack/pikascript/pikascript-lib/PikaStdLib/PikaStdData_Dict.c @@ -1,6 +1,8 @@ #include "PikaStdData_Dict.h" #include "BaseObj.h" #include "PikaObj.h" +#include "PikaStdData_Tuple.h" +#include "PikaStdData_dict_items.h" #include "PikaStdData_dict_keys.h" #include "PikaStdLib_SysObj.h" #include "dataStrs.h" @@ -59,7 +61,9 @@ void PikaStdData_Dict___del__(PikaObj* self) { PikaDict* dict = obj_getPtr(self, "dict"); PikaDict* keys = obj_getPtr(self, "_keys"); dict_deinit(dict); - dict_deinit(keys); + if (NULL != keys) { + dict_deinit(keys); + } } void PikaStdData_dict_keys___init__(PikaObj* self, PikaObj* dict) { @@ -72,6 +76,12 @@ PikaObj* PikaStdData_Dict_keys(PikaObj* self) { return dict_keys; } +PikaObj* PikaStdData_Dict_items(PikaObj* self) { + PikaObj* dict_items = newNormalObj(New_PikaStdData_dict_items); + PikaStdData_dict_keys___init__(dict_items, self); + return dict_items; +} + Arg* PikaStdData_dict_keys___iter__(PikaObj* self) { obj_setInt(self, "__iter_i", 0); return arg_newRef(self); @@ -170,3 +180,77 @@ int PikaStdData_dict_keys___len__(PikaObj* self) { PikaDict* keys = obj_getPtr(dictptr, "_keys"); return args_getSize(&keys->super); } + +int dict_contains(PikaDict* dict, Arg* key) { + int i = 0; + while (PIKA_TRUE) { + Arg* item = args_getArgByidex(&dict->super, i); + if (NULL == item) { + break; + } + if (arg_isEqual(item, key)) { + return PIKA_TRUE; + } + i++; + } + return PIKA_FALSE; +} + +int PikaStdData_Dict___contains__(PikaObj* self, Arg* val) { + PikaDict* dict = obj_getPtr(self, "_keys"); + return dict_contains(dict, val); +} + +Arg* PikaStdData_dict_items___iter__(PikaObj* self) { + obj_setInt(self, "__iter_i", 0); + return arg_newRef(self); +} + +int PikaStdData_dict_items___len__(PikaObj* self) { + PikaObj* dictptr = obj_getPtr(self, "dictptr"); + PikaDict* keys = obj_getPtr(dictptr, "_keys"); + return args_getSize(&keys->super); +} + +Arg* PikaStdData_dict_items___next__(PikaObj* self) { + int __iter_i = args_getInt(self->list, "__iter_i"); + PikaObj* dictptr = obj_getPtr(self, "dictptr"); + PikaDict* keys = obj_getPtr(dictptr, "_keys"); + PikaDict* dict = obj_getPtr(dictptr, "dict"); + Arg* key = args_getArgByidex(&keys->super, __iter_i); + Arg* val = args_getArgByidex(&dict->super, __iter_i); + if (NULL == key) { + return arg_newNull(); + } + PikaObj* tuple = newNormalObj(New_PikaStdData_Tuple); + PikaStdData_Tuple___init__(tuple); + PikaList* list = obj_getPtr(tuple, "list"); + list_append(list, key); + list_append(list, val); + args_setInt(self->list, "__iter_i", __iter_i + 1); + return arg_newObj(tuple); +} + +char* PikaStdData_dict_items___str__(PikaObj* self) { + Arg* str_arg = arg_newStr("dict_items(["); + int i = 0; + obj_setInt(self, "__iter_i", 0); + while (PIKA_TRUE) { + Arg* item = PikaStdData_dict_items___next__(self); + if (arg_getType(item) == ARG_TYPE_NONE) { + arg_deinit(item); + break; + } + if (i != 0) { + str_arg = arg_strAppend(str_arg, ", "); + } + char* item_str = PikaStdLib_SysObj_str(self, item); + str_arg = arg_strAppend(str_arg, item_str); + i++; + arg_deinit(item); + } + str_arg = arg_strAppend(str_arg, "])"); + obj_setStr(self, "_buf", arg_getStr(str_arg)); + arg_deinit(str_arg); + return obj_getStr(self, "_buf"); +} diff --git a/port/cmsis-pack/pikascript/pikascript-lib/PikaStdLib/PikaStdData_String.c b/port/cmsis-pack/pikascript/pikascript-lib/PikaStdLib/PikaStdData_String.c index c4f9fe635..3d6692b80 100644 --- a/port/cmsis-pack/pikascript/pikascript-lib/PikaStdLib/PikaStdData_String.c +++ b/port/cmsis-pack/pikascript/pikascript-lib/PikaStdLib/PikaStdData_String.c @@ -3,74 +3,168 @@ #include "PikaStdData_String_Util.h" #include "dataStrs.h" -Arg* PikaStdData_String___iter__(PikaObj* self) { +char* _strlwr(char *str); + +Arg *PikaStdData_String___iter__(PikaObj *self) +{ obj_setInt(self, "__iter_i", 0); return arg_newRef(self); } -void PikaStdData_String_set(PikaObj* self, char* s) { +void PikaStdData_String_set(PikaObj *self, char *s) +{ +#if PIKA_STRING_UTF8_ENABLE + int r = _valid_utf8(s, -1); + if (r >= 0) + { + obj_setErrorCode(self, __LINE__); + __platform_printf("Error invaliad character %x\r\n", s[r]); + return; + } +#endif obj_setStr(self, "str", s); } -void PikaStdData_String___init__(PikaObj* self, char* s) { +void PikaStdData_String___init__(PikaObj *self, char *s) +{ +#if PIKA_STRING_UTF8_ENABLE + int r = _valid_utf8(s, -1); + if (r >= 0) + { + obj_setErrorCode(self, __LINE__); + __platform_printf("Error invaliad character %x\r\n", s[r]); + return; + } +#endif PikaStdData_String_set(self, s); } -char* PikaStdData_String_get(PikaObj* self) { +char *PikaStdData_String_get(PikaObj *self) +{ return obj_getStr(self, "str"); } -Arg* PikaStdData_String___next__(PikaObj* self) { +Arg *PikaStdData_String___next__(PikaObj *self) +{ int __iter_i = args_getInt(self->list, "__iter_i"); - char* str = obj_getStr(self, "str"); + char *str = obj_getStr(self, "str"); uint16_t len = strGetSize(str); - Arg* res = NULL; +#if PIKA_STRING_UTF8_ENABLE + char char_buff[5]; + int r = _utf8_get(str, len, __iter_i, char_buff); + if (r < 0) + { + return arg_newNull(); + } + args_setInt(self->list, "__iter_i", __iter_i + 1); + return arg_newStr((char *)char_buff); +#else + Arg *res = NULL; char char_buff[] = " "; - if (__iter_i < len) { + if (__iter_i < len) + { char_buff[0] = str[__iter_i]; - res = arg_newStr((char*)char_buff); - } else { + res = arg_newStr((char *)char_buff); + } + else + { return arg_newNull(); } args_setInt(self->list, "__iter_i", __iter_i + 1); return res; +#endif } -Arg* PikaStdData_String___getitem__(PikaObj* self, Arg* __key) { +Arg *PikaStdData_String___getitem__(PikaObj *self, Arg *__key) +{ int key_i = arg_getInt(__key); - char* str = obj_getStr(self, "str"); + if (key_i < 0){ + key_i = PikaStdData_String___len__(self) + key_i; + } + char *str = obj_getStr(self, "str"); uint16_t len = strGetSize(str); - char char_buff[] = " "; - if (key_i < len) { - char_buff[0] = str[key_i]; - return arg_newStr((char*)char_buff); - } else { +#if PIKA_STRING_UTF8_ENABLE + char char_buff[5]; + int r = _utf8_get(str, len, key_i, char_buff); + if (r < 0) + { return arg_newNull(); } + return arg_newStr((char *)char_buff); +#else + char char_buff[] = " "; + if (key_i < len) + { + char_buff[0] = str[key_i]; + return arg_newStr((char *)char_buff); + } + else + { + return arg_newNull(); + } +#endif } -void PikaStdData_String___setitem__(PikaObj* self, Arg* __key, Arg* __val) { +void PikaStdData_String___setitem__(PikaObj *self, Arg *__key, Arg *__val) +{ int key_i = arg_getInt(__key); - char* str = obj_getStr(self, "str"); - char* val = arg_getStr(__val); + char *str = obj_getStr(self, "str"); + char *val = arg_getStr(__val); uint16_t len = strGetSize(str); - if (key_i >= len) { +#if PIKA_STRING_UTF8_ENABLE + int len2 = strlen(val); + int is_invalid = _valid_utf8(val, len2); + if (is_invalid >= 0) + { + obj_setErrorCode(self, __LINE__); + __platform_printf("Error String invalid\r\n"); + return; + } + int ulen_val = _utf8_strlen(val, len2); + if (ulen_val != 1) + { + obj_setErrorCode(self, __LINE__); + __platform_printf("Error String invalid char\r\n"); + return; + } + int char_len; + int repl_at = _utf8_get_offset(str, len, key_i, &char_len); + if (repl_at < 0) + { + obj_setErrorCode(self, __LINE__); + __platform_printf("Error String Overflow\r\n"); + return; + } + int ok = __str_repl(self, str, len, repl_at, char_len, val, len2); + if (ok < 0) + { + obj_setErrorCode(self, __LINE__); + __platform_printf("Error. Internal error(-%d)\r\n", __LINE__); + return; + } +#else + if (key_i >= len) + { obj_setErrorCode(self, 1); __platform_printf("Error String Overflow\r\n"); return; } str[key_i] = val[0]; +#endif } -char* PikaStdData_String___str__(PikaObj* self) { +char *PikaStdData_String___str__(PikaObj *self) +{ return obj_getStr(self, "str"); } -int PikaStdData_String_startswith(PikaObj* self, char* prefix) { - char* str = obj_getStr(self, "str"); - char* p = prefix; +int PikaStdData_String_startswith(PikaObj *self, char *prefix) +{ + char *str = obj_getStr(self, "str"); + char *p = prefix; int i = 0; - while (*p != '\0') { + while (*p != '\0') + { if (*p != str[i]) return 0; p++; @@ -79,11 +173,13 @@ int PikaStdData_String_startswith(PikaObj* self, char* prefix) { return 1; } -int PikaStdData_String_endswith(PikaObj* self, char* suffix) { - char* str = obj_getStr(self, "str"); +int PikaStdData_String_endswith(PikaObj *self, char *suffix) +{ + char *str = obj_getStr(self, "str"); int len1 = strlen(str); int len2 = strlen(suffix); - while (len2 >= 1) { + while (len2 >= 1) + { if (suffix[len2 - 1] != str[len1 - 1]) return 0; len2--; @@ -92,10 +188,12 @@ int PikaStdData_String_endswith(PikaObj* self, char* suffix) { return 1; } -int PikaStdData_String_isdigit(PikaObj* self) { - char* str = obj_getStr(self, "str"); +int PikaStdData_String_isdigit(PikaObj *self) +{ + char *str = obj_getStr(self, "str"); int i = 0; - while (str[i] != '\0') { + while (str[i] != '\0') + { if (!isdigit((int)str[i])) return 0; i++; @@ -103,10 +201,12 @@ int PikaStdData_String_isdigit(PikaObj* self) { return 1; } -int PikaStdData_String_islower(PikaObj* self) { - char* str = obj_getStr(self, "str"); +int PikaStdData_String_islower(PikaObj *self) +{ + char *str = obj_getStr(self, "str"); int i = 0; - while (str[i] != '\0') { + while (str[i] != '\0') + { if (!islower((int)str[i])) return 0; i++; @@ -114,10 +214,12 @@ int PikaStdData_String_islower(PikaObj* self) { return 1; } -int PikaStdData_String_isalnum(PikaObj* self) { - char* str = obj_getStr(self, "str"); +int PikaStdData_String_isalnum(PikaObj *self) +{ + char *str = obj_getStr(self, "str"); int i = 0; - while (str[i] != '\0') { + while (str[i] != '\0') + { if (!isalnum((int)str[i])) return 0; i++; @@ -125,10 +227,12 @@ int PikaStdData_String_isalnum(PikaObj* self) { return 1; } -int PikaStdData_String_isalpha(PikaObj* self) { - char* str = obj_getStr(self, "str"); +int PikaStdData_String_isalpha(PikaObj *self) +{ + char *str = obj_getStr(self, "str"); int i = 0; - while (str[i] != '\0') { + while (str[i] != '\0') + { if (!isalpha((int)str[i])) return 0; i++; @@ -136,10 +240,12 @@ int PikaStdData_String_isalpha(PikaObj* self) { return 1; } -int PikaStdData_String_isspace(PikaObj* self) { - char* str = obj_getStr(self, "str"); +int PikaStdData_String_isspace(PikaObj *self) +{ + char *str = obj_getStr(self, "str"); int i = 0; - while (str[i] != '\0') { + while (str[i] != '\0') + { if (!isspace((int)str[i])) return 0; i++; @@ -147,22 +253,24 @@ int PikaStdData_String_isspace(PikaObj* self) { return 1; } -PikaObj* PikaStdData_String_split(PikaObj* self, char* s) { +PikaObj *PikaStdData_String_split(PikaObj *self, char *s) +{ /* 创建 list 对象 */ - PikaObj* list = newNormalObj(New_PikaStdData_List); + PikaObj *list = newNormalObj(New_PikaStdData_List); /* 初始化 list */ PikaStdData_List___init__(list); Args buffs = {0}; - char* str = strsCopy(&buffs, obj_getStr(self, "str")); + char *str = strsCopy(&buffs, obj_getStr(self, "str")); char sign = s[0]; int token_num = strCountSign(str, sign) + 1; - for (int i = 0; i < token_num; i++) { - char* token = strsPopToken(&buffs, str, sign); + for (int i = 0; i < token_num; i++) + { + char *token = strsPopToken(&buffs, str, sign); /* 用 arg_set 的 api 创建 arg */ - Arg* token_arg = arg_newStr(token); + Arg *token_arg = arg_newStr(token); /* 添加到 list 对象 */ PikaStdData_List_append(list, token_arg); /* 销毁 arg */ @@ -173,25 +281,42 @@ PikaObj* PikaStdData_String_split(PikaObj* self, char* s) { return list; } -int PikaStdData_String___len__(PikaObj* self) { - char* str = obj_getStr(self, "str"); +int PikaStdData_String___len__(PikaObj *self) +{ + char *str = obj_getStr(self, "str"); +#if PIKA_STRING_UTF8_ENABLE + int n = _utf8_strlen(str, -1); + if (n < 0) + { + obj_setErrorCode(self, __LINE__); + __platform_printf("Error. Internal error(%d)\r\n", __LINE__); + return n; + } + return n; +#else return strGetSize(str); +#endif } -char* PikaStdData_String_strip(PikaObj* self) { +char *PikaStdData_String_strip(PikaObj *self) +{ Args buffs = {0}; - char* str = strsCopy(&buffs, obj_getStr(self, "str")); + char *str = strsCopy(&buffs, obj_getStr(self, "str")); /* strip */ - char* str_start = str; - for (size_t i = 0; i < strGetSize(str); i++) { - if (str[i] != ' ') { - str_start = (char*)(str + i); + char *str_start = str; + for (size_t i = 0; i < strGetSize(str); i++) + { + if (str[i] != ' ') + { + str_start = (char *)(str + i); break; } } - for (int i = strGetSize(str) - 1; i >= 0; i--) { - if (str[i] != ' ') { + for (int i = strGetSize(str) - 1; i >= 0; i--) + { + if (str[i] != ' ') + { str[i + 1] = '\0'; break; } @@ -202,17 +327,553 @@ char* PikaStdData_String_strip(PikaObj* self) { return obj_getStr(self, "_buf"); } -char* PikaStdData_String_replace(PikaObj* self, char* old, char* new) { +char *PikaStdData_String_replace(PikaObj *self, char *old, char *new) +{ Args buffs = {0}; - char* str = strsCopy(&buffs, obj_getStr(self, "str")); + char *str = strsCopy(&buffs, obj_getStr(self, "str")); str = strsReplace(&buffs, str, old, new); obj_setStr(self, "_buf", str); strsDeinit(&buffs); return obj_getStr(self, "_buf"); } -Arg* PikaStdData_String_encode(PikaObj* self) { - char* str = obj_getStr(self, "str"); - Arg* arg = arg_newBytes((uint8_t*)str, strGetSize(str)); - return arg; + +Arg *PikaStdData_String_encode(PikaObj *self, PikaTuple *encoding) +{ + char *str = obj_getStr(self, "str"); + +#if PIKA_STRING_UTF8_ENABLE + char *to_code = NULL; + int argn = tuple_getSize(encoding); + if (argn < 1) + { + return arg_newBytes((uint8_t *)str, strGetSize(str)); + } + Arg *arg_i = tuple_getArg(encoding, 0); + if (arg_getType(arg_i) != ARG_TYPE_STRING) + { + obj_setErrorCode(self, __LINE__); + __platform_printf("Error invaliad arguments\r\n"); + return NULL; + } + to_code = arg_getStr(arg_i); + _strlwr(to_code); + Arg *res = _str_encode(str, to_code); + if (!res) + { + obj_setErrorCode(self, __LINE__); + __platform_printf("Error internal error\r\n"); + return NULL; + } + return res; +#else + return arg_newBytes((uint8_t *)str, strGetSize(str)); +#endif } + +#if PIKA_STRING_UTF8_ENABLE + +static const uint8_t _pcre_utf8_table4[] = { + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5}; + +const char mask1 = 0x80; +const char mask2 = 0xc0; +const char mask3 = 0xe0; +const char mask4 = 0xf0; + +const char nmask1 = 0x3f; +const char nmask2 = 0x1f; +const char nmask3 = 0x0f; +const char nmask4 = 0x07; + +int _valid_utf8(const char *string, int length) +{ + const uint8_t *p; + if (length < 0) + { + length = strlen(string); + } + + for (p = (const uint8_t *)string; length-- > 0; p++) + { + int ab; + int c = *p; + if (!(c & 0x80)) + continue; + if (c < 0xc0) + return (uintptr_t)p - (uintptr_t)string; + ab = _pcre_utf8_table4[c & 0x3f]; + if (length < ab || ab > 3) + return (uintptr_t)p - (uintptr_t)string; + length -= ab; + if ((*(++p) & 0xc0) != 0x80) + return (uintptr_t)p - (uintptr_t)string; + switch (ab) + { + case 1: + if ((c & 0x3e) == 0) + return (uintptr_t)p - (uintptr_t)string; + continue; + case 2: + if ((c == 0xe0 && (*p & 0x20) == 0) || + (c == 0xed && *p >= 0xa0)) + return (uintptr_t)p - (uintptr_t)string; + break; + case 3: + if ((c == 0xf0 && (*p & 0x30) == 0) || + (c > 0xf4) || + (c == 0xf4 && *p > 0x8f)) + return (uintptr_t)p - (uintptr_t)string; + break; + } + while (--ab > 0) + { + if ((*(++p) & 0xc0) != 0x80) + return (uintptr_t)p - (uintptr_t)string; + } + } + return -1; +} +int _utf8_get(const char *string, int length, int at, char *out_buf) +{ + const uint8_t *p; + int ab, c; + if (length < 0) + { + length = strlen(string); + } + if (at < 0 || at >= length) + return -1; + + for (p = (const uint8_t *)string; length > 0 && at; p++, at--) + { + c = *p; + if (!(c & 0x80)) + { + length--; + continue; + } + ab = _pcre_utf8_table4[c & 0x3f]; + p += ab++; + length -= ab; + } + if (at || length <= 0) + return -2; + c = *p; + if (!(c & 0x80)) + { + *out_buf = c; + out_buf[1] = 0; + return 1; + }; + ab = _pcre_utf8_table4[c & 0x3f] + 1; + __platform_memcpy(out_buf, p, ab); + out_buf[ab] = '\0'; + return ab; +} +int _utf8_get_offset(const char *string, int length, int at, int *out_char_len) +{ + const uint8_t *p; + int ab, c; + if (length < 0) + { + length = strlen(string); + } + if (at < 0 || at >= length) + return -1; + + for (p = (const uint8_t *)string; length > 0 && at; p++, at--) + { + c = *p; + if (!(c & 0x80)) + { + length--; + continue; + } + ab = _pcre_utf8_table4[c & 0x3f]; + p += ab++; + length -= ab; + } + if (at) + return -2; + c = *p; + if (!(c & 0x80)) + { + if (out_char_len) + *out_char_len = 1; + return (uintptr_t)p - (uintptr_t)string; + }; + ab = _pcre_utf8_table4[c & 0x3f] + 1; + if (out_char_len) + *out_char_len = ab; + return (uintptr_t)p - (uintptr_t)string; +} +int _utf8_strlen(const char *string, int length) +{ + const uint8_t *p; + int i, ab, c; + if (length < 0) + { + length = strlen(string); + } + + for (i = 0, p = (const uint8_t *)string; length > 0; i++, p++) + { + c = *p; + if (!(c & 0x80)) + { + length--; + continue; + } + ab = _pcre_utf8_table4[c & 0x3f]; + p += ab++; + length -= ab; + } + if (length < 0) + return -1; + return i; +} + +int __str_repl(PikaObj *self, char *str, int str_len, int repl_at, int repl_len, char *val, int val_len) +{ + if (val_len > repl_len) + { + str[repl_at] = 0; + Arg *s_new = arg_newStr(str); + if (!s_new) + return -1; + s_new = arg_strAppend(s_new, val); + s_new = arg_strAppend(s_new, str + repl_at + repl_len); + obj_removeArg(self, "str"); + int rs = obj_setArg(self, "str", s_new); + arg_deinit(s_new); + if (rs) + return -rs; + return 0; + } + char *s = str + repl_at; + __platform_memcpy(s, val, val_len); + __platform_memmove(s + val_len, s + repl_len, str_len - repl_at - repl_len + 1); + return 0; +} + +int __utf8_to_utf32_char_LE(const char *utf8, char *out_buf) +{ + char c = *utf8; + if (!(c & mask1)) + { + *out_buf = c; + out_buf[1] = 0; + out_buf[2] = 0; + out_buf[3] = 0; + return 1; + } + int left_length = _pcre_utf8_table4[c & 0x3f]; + char a, b, d; + switch (left_length) + { + case 1: + a = c & nmask2; + b = utf8[1] & nmask1; + out_buf[0] = b | a << 6; + out_buf[1] = a >> 2; + out_buf[2] = 0; + out_buf[3] = 0; + return 2; + case 2: + a = c & nmask3; + b = utf8[1] & nmask1; + c = utf8[2] & nmask1; + out_buf[0] = c | b << 6; + out_buf[1] = b >> 2 | a << 4; + out_buf[2] = 0; + out_buf[3] = 0; + return 3; + case 3: + a = c & nmask4; + b = utf8[1] & nmask1; + c = utf8[2] & nmask1; + d = utf8[3] & nmask1; + out_buf[0] = d | c << 6; + out_buf[1] = c >> 2 | b << 4; + out_buf[2] = b >> 4 | a << 2; + out_buf[3] = 0; + return 4; + default: + return 0; + } +} +int __utf8_to_utf32_LE_noBOM_get_size(const char *utf8, int len) +{ + char *p = (char*) utf8; + char buf[4]; + int space_sum = 0; + while (len > 0) + { + int size = __utf8_to_utf32_char_LE(p, buf); + if (!size) + return -1; + p += size; + len -= size; + space_sum++; + } + return space_sum * 4; +} +int __utf8_to_utf32_LE_noBOM(const char *utf8, int len, char *out_buf) +{ + char *q = out_buf; + char *p = (char*) utf8; + while (len > 0) + { + int size = __utf8_to_utf32_char_LE(p, q); + if (!size) + return -1; + p += size; + len -= size; + q += 4; + } + return q - out_buf; +} +int __utf8_to_utf32_LE_withBOM(const char *utf8, int len, char *out_buf) +{ + int size = __utf8_to_utf32_LE_noBOM(utf8, len, out_buf + 4); + if (size < 0) + { + return size; + } + out_buf[0] = '\xff'; + out_buf[1] = '\xfe'; + out_buf[2] = 0; + out_buf[3] = 0; + return size + 4; +} + +int32_t __utf8_decode(const char *utf8, int left_length) +{ + int ucode = -1; + char c = *utf8; + if (!(c & mask1)) + { + return c; + } + char a, b, d; + switch (left_length) + { + case 1: + a = c & nmask2; + b = utf8[1] & nmask1; + ucode = b | (a & 0x03) << 6; + ucode |= (a >> 2) << 8; + break; + case 2: + a = c & nmask3; + b = utf8[1] & nmask1; + c = utf8[2] & nmask1; + ucode = c | (b & 0x03) << 6; + ucode |= (b >> 2 | a << 4) << 8; + break; + case 3: + a = c & nmask4; + b = utf8[1] & nmask1; + c = utf8[2] & nmask1; + d = utf8[3] & nmask1; + ucode = d | (c & 0x03) << 6; + ucode |= (c >> 2 | (b & 0x0f) << 4) << 8; + ucode |= (b >> 4 | a << 2) << 16; + break; + default: + return -1; + } + return ucode; +} + +int __unicode_to_utf16_char_LE(int32_t u, char *out_buf) +{ + if (!(u & 0xffff0000)) + { + out_buf[0] = u & 0xff; + out_buf[1] = (u & 0xff00) >> 8; + return 2; + } + int32_t d = u - 0x10000; + int32_t L = d & 0x3ff; + int32_t U = d >> 10; + L = L | 0xdc00; + U = U | 0xd800; + out_buf[0] = U & 0xff; + out_buf[1] = (U & 0xff00) >> 8; + out_buf[2] = L & 0xff; + out_buf[3] = (L & 0xff00) >> 8; + return 4; +} +int __utf8_to_utf16_LE_noBOM(const char *utf8, int len, char *out_buf) +{ + char *q = out_buf; + char *p = (char*) utf8; + while (len > 0) + { + char c = *p; + int32_t ucode; + if (!(c & mask1)) + { + ucode = c; + p++; + len--; + } + else + { + int left_size = _pcre_utf8_table4[c & 0x3f]; + ucode = __utf8_decode(p, left_size++); + if (ucode < 0) + return ucode; + p += left_size; + len -= left_size; + } + int size = __unicode_to_utf16_char_LE(ucode, q); + q += size; + } + return q - out_buf; +} +int __utf8_to_utf16_LE_noBOM_get_size(const char *utf8, int len) +{ + char out_buf[4]; + char *p = (char*) utf8; + int need_space = 0; + while (len > 0) + { + char c = *p; + int32_t ucode; + if (!(c & mask1)) + { + ucode = c; + p++; + len--; + } + else + { + int left_size = _pcre_utf8_table4[c & 0x3f]; + ucode = __utf8_decode(p, left_size++); + if (ucode < 0) + return ucode; + p += left_size; + len -= left_size; + } + int size = __unicode_to_utf16_char_LE(ucode, out_buf); + need_space += size; + } + return need_space; +} +int __utf8_to_utf16_LE_withBOM(const char *utf8, int len, char *out_buf) +{ + int size = __utf8_to_utf16_LE_noBOM(utf8, len, out_buf + 2); + if (size < 0) + { + return size; + } + out_buf[0] = '\xff'; + out_buf[1] = '\xfe'; + return size + 2; +} +Arg *_str_encode(char *str, char *encoding) +{ + if (strEqu(encoding, "utf-8")) + { + return arg_newBytes((uint8_t *)str, strGetSize(str)); + } + int len = strlen(str); + + if (strEqu(encoding, "ascii")) + { + int ulen = _utf8_strlen(str, len); + if (ulen == len) + { + return arg_newBytes((uint8_t *)str, strGetSize(str)); + } + __platform_printf("Warning there is non-ascii characters\r\n"); + char *b = (char *)pikaMalloc(len + 1); + if (!b) + { + return NULL; + } + char *p = str; + char *q = b; + char c = *p++; + while (c) + { + if (!(c & 0x80)) + { + *q++ = c; + } + c = *p++; + } + *q = 0; + Arg *arg = arg_newBytes((uint8_t *)b, strGetSize(b)); + pikaFree(b, len + 1); + return arg; + } + + if (strEqu(encoding, "utf-16")) + { + int size_needed = __utf8_to_utf16_LE_noBOM_get_size(str, len); + if (size_needed <= 0) + { + return NULL; + } + size_needed += 2; + char *b = (char *)pikaMalloc(size_needed); + if (!b) + { + return NULL; + } + int ok = __utf8_to_utf16_LE_withBOM(str, len, b); + if (ok < 0) + { + pikaFree(b, size_needed); + return NULL; + } + Arg *arg = arg_newBytes((uint8_t *)b, size_needed); + pikaFree(b, size_needed); + return arg; + } + + if (strEqu(encoding, "utf-32")) + { + int size_needed = __utf8_to_utf32_LE_noBOM_get_size(str, len); + if (size_needed <= 0) + { + return NULL; + } + size_needed += 4; + char *b = (char *)pikaMalloc(size_needed); + if (!b) + { + return NULL; + } + int ok = __utf8_to_utf32_LE_withBOM(str, len, b); + if (ok < 0) + { + pikaFree(b, size_needed); + return NULL; + } + Arg *arg = arg_newBytes((uint8_t *)b, size_needed); + pikaFree(b, size_needed); + return arg; + } + return NULL; +} + +char* _strlwr(char *str) +{ + int i = 0; + while (str[i] != '\0') + { + str[i] = tolower((int)str[i]); + i++; + } + return str; +} + +#endif diff --git a/port/cmsis-pack/pikascript/pikascript-lib/PikaStdLib/PikaStdData_String_Util.h b/port/cmsis-pack/pikascript/pikascript-lib/PikaStdLib/PikaStdData_String_Util.h index eefe5573b..cd9815bf4 100644 --- a/port/cmsis-pack/pikascript/pikascript-lib/PikaStdLib/PikaStdData_String_Util.h +++ b/port/cmsis-pack/pikascript/pikascript-lib/PikaStdLib/PikaStdData_String_Util.h @@ -1 +1,20 @@ -#include +#include + +#if PIKA_STRING_UTF8_ENABLE + +int _valid_utf8(const char *string, int length); +int _utf8_get(const char *string, int length, int at, char *out_buf); +int _utf8_get_offset(const char *string, int length, int at, int *out_char_len); +int _utf8_strlen(const char *string, int length); +int __str_repl(PikaObj *self, char *str, int str_len, int repl_at, int repl_len, char *val, int val_len); + +int __utf8_to_utf32_LE_withBOM(const char *utf8, int len, char *out_buf); +int __utf8_to_utf32_LE_noBOM(const char *utf8, int len, char *out_buf); +int __utf8_to_utf32_LE_noBOM_get_size(const char *utf8, int len); + +int __utf8_to_utf16_LE_withBOM(const char *utf8, int len, char *out_buf); +int __utf8_to_utf16_LE_noBOM(const char *utf8, int len, char *out_buf); +int __utf8_to_utf16_LE_noBOM_get_size(const char *utf8, int len); +Arg* _str_encode(char*str, char*encoding); + +#endif diff --git a/port/cmsis-pack/pikascript/pikascript-lib/PikaStdLib/PikaStdData_Tuple.c b/port/cmsis-pack/pikascript/pikascript-lib/PikaStdLib/PikaStdData_Tuple.c index 1684ef069..d2ac01b8b 100644 --- a/port/cmsis-pack/pikascript/pikascript-lib/PikaStdLib/PikaStdData_Tuple.c +++ b/port/cmsis-pack/pikascript/pikascript-lib/PikaStdLib/PikaStdData_Tuple.c @@ -74,3 +74,14 @@ char* PikaStdData_Tuple___str__(PikaObj* self) { int PikaStdData_Tuple___len__(PikaObj* self) { return PikaStdData_Tuple_len(self); } + +int PikaStdData_Tuple___contains__(PikaObj* self, Arg* val) { + PikaList* list = obj_getPtr(self, "list"); + for (size_t i = 0; i < list_getSize(list); i++) { + Arg* arg = list_getArg(list, i); + if (arg_isEqual(arg, val)) { + return 1; + } + } + return 0; +} diff --git a/port/cmsis-pack/pikascript/pikascript-lib/PikaStdLib/PikaStdLib_RangeObj.c b/port/cmsis-pack/pikascript/pikascript-lib/PikaStdLib/PikaStdLib_RangeObj.c index a687144ca..2e4725b29 100644 --- a/port/cmsis-pack/pikascript/pikascript-lib/PikaStdLib/PikaStdLib_RangeObj.c +++ b/port/cmsis-pack/pikascript/pikascript-lib/PikaStdLib/PikaStdLib_RangeObj.c @@ -1,14 +1,10 @@ #include "PikaObj.h" Arg* PikaStdLib_RangeObj___next__(PikaObj* self) { - int a1 = args_getInt(self->list, "a1"); - int a2 = args_getInt(self->list, "a2"); - int start = 0; - int end = 0; - int foot = 1; + int start = args_getInt(self->list, "_start"); + int end = args_getInt(self->list, "_end"); + int step = args_getInt(self->list, "_step"); /* only start and end */ - start = a1; - end = a2; /* start */ int iter_i = args_getInt(self->list, "iter_i"); /* iter_i is not inited */ @@ -19,6 +15,6 @@ Arg* PikaStdLib_RangeObj___next__(PikaObj* self) { if (iter_i >= end) { return arg_newNull(); } - args_setInt(self->list, "iter_i", iter_i + foot); + args_setInt(self->list, "iter_i", iter_i + step); return arg_newInt(iter_i); } diff --git a/port/cmsis-pack/pikascript/pikascript-lib/PikaStdLib/PikaStdLib_SysObj.c b/port/cmsis-pack/pikascript/pikascript-lib/PikaStdLib/PikaStdLib_SysObj.c index 3d788e6fd..0649c2df6 100644 --- a/port/cmsis-pack/pikascript/pikascript-lib/PikaStdLib/PikaStdLib_SysObj.c +++ b/port/cmsis-pack/pikascript/pikascript-lib/PikaStdLib/PikaStdLib_SysObj.c @@ -184,11 +184,30 @@ Arg* PikaStdLib_SysObj_iter(PikaObj* self, Arg* arg) { return arg_newNull(); } -Arg* PikaStdLib_SysObj_range(PikaObj* self, int a1, int a2) { +Arg* PikaStdLib_SysObj_range(PikaObj* self, PikaTuple* ax) { /* set template arg to create rangeObj */ Arg* obj_arg = arg_newDirectObj(New_PikaStdLib_RangeObj); - obj_setInt(arg_getPtr(obj_arg), "a1", a1); - obj_setInt(arg_getPtr(obj_arg), "a2", a2); + PikaObj* range_obj = arg_getPtr(obj_arg); + if (tuple_getSize(ax) == 1) { + int start = 0; + int end = arg_getInt(tuple_getArg(ax, 0)); + obj_setInt(range_obj, "_start", start); + obj_setInt(range_obj, "_end", end); + obj_setInt(range_obj, "_step", 1); + } else if (tuple_getSize(ax) == 2) { + int start = arg_getInt(tuple_getArg(ax, 0)); + int end = arg_getInt(tuple_getArg(ax, 1)); + obj_setInt(range_obj, "_start", start); + obj_setInt(range_obj, "_end", end); + obj_setInt(range_obj, "_step", 1); + } else if (tuple_getSize(ax) == 3) { + int start = arg_getInt(tuple_getArg(ax, 0)); + int end = arg_getInt(tuple_getArg(ax, 1)); + int step = arg_getInt(tuple_getArg(ax, 2)); + obj_setInt(range_obj, "_start", start); + obj_setInt(range_obj, "_end", end); + obj_setInt(range_obj, "_step", step); + } return obj_arg; } @@ -363,20 +382,18 @@ Arg* PikaStdLib_SysObj___slice__(PikaObj* self, return __vm_slice(self, end, obj, start, step); } -static void __print_arg(PikaObj* self, Arg* val) { +static char* __print_arg(PikaObj* self, Arg* val) { obj_setErrorCode(self, 0); ArgType arg_type = arg_getType(val); if (NULL != val) { if (arg_getType(val) == ARG_TYPE_BYTES) { - arg_printBytes(val); - return; + return __printBytes(self, val); } } if (argType_isObject(arg_type)) { char* to_str = obj_toStr(arg_getPtr(val)); if (NULL != to_str) { - __platform_printf("%s\r\n", to_str); - return; + return obj_cacheStr(self, to_str); } } Args* print_args = New_args(NULL); @@ -387,51 +404,39 @@ static void __print_arg(PikaObj* self, Arg* val) { obj_setSysOut(self, "Error: can not print val"); obj_setErrorCode(self, 1); args_deinit(print_args); - return; + return NULL; } - obj_setStr(self, "_buf", res); + res = obj_cacheStr(self, res); args_deinit(print_args); + return res; } -void PikaStdLib_SysObj_print(PikaObj* self, PikaTuple* val) { +void PikaStdLib_SysObj_print(PikaObj* self, PikaTuple* val, PikaDict* ops) { int arg_size = tuple_getSize(val); Arg* print_out_arg = arg_newStr(""); PIKA_BOOL is_get_print = PIKA_FALSE; for (int i = 0; i < arg_size; i++) { Arg* arg = tuple_getArg(val, i); - __print_arg(self, arg); - char* item = obj_getStr(self, "_buf"); + char* item = __print_arg(self, arg); if (NULL != item) { is_get_print = PIKA_TRUE; print_out_arg = arg_strAppend(print_out_arg, item); if (i < arg_size - 1) { print_out_arg = arg_strAppend(print_out_arg, " "); } - obj_removeArg(self, "_buf"); } } + char* end = dict_getStr(ops, "end"); + if (NULL == end) { + /* default */ + end = "\r\n"; + } if (PIKA_TRUE == is_get_print) { - __platform_printf("%s\r\n", arg_getStr(print_out_arg)); + __platform_printf("%s%s", arg_getStr(print_out_arg), end); } arg_deinit(print_out_arg); } -void PikaStdLib_SysObj_printNoEnd(PikaObj* self, Arg* val) { - obj_setErrorCode(self, 0); - Args* print_args = New_args(NULL); - args_setArg(print_args, arg_copy(val)); - char* res = args_print(print_args, "val"); - if (NULL == res) { - obj_setSysOut(self, "Error: print: can not print val"); - obj_setErrorCode(self, 1); - args_deinit(print_args); - return; - } - /* not empty */ - __platform_printf("%s", res); - args_deinit(print_args); -} - char* PikaStdLib_SysObj_cformat(PikaObj* self, char* fmt, PikaTuple* var) { #if PIKA_SYNTAX_FORMAT_ENABLE Args buffs = {0}; @@ -533,3 +538,7 @@ void PikaStdLib_SysObj_setattr(PikaObj* self, exit: return; } + +void PikaStdLib_SysObj_exit(PikaObj *self){ + pks_vm_exit(); +} diff --git a/port/cmsis-pack/pikascript/rust-msc-latest-win10.exe b/port/cmsis-pack/pikascript/rust-msc-latest-win10.exe index f0767ded1..41105f9ec 100644 Binary files a/port/cmsis-pack/pikascript/rust-msc-latest-win10.exe and b/port/cmsis-pack/pikascript/rust-msc-latest-win10.exe differ