diff --git a/port/cmsis-pack/pikascript/PikaDebug.pyi b/port/cmsis-pack/pikascript/PikaDebug.pyi index dc1db98f7..6aeedde0f 100644 --- a/port/cmsis-pack/pikascript/PikaDebug.pyi +++ b/port/cmsis-pack/pikascript/PikaDebug.pyi @@ -1,7 +1,4 @@ -#api -from PikaObj import * - -class Debuger(TinyObj): +class Debuger: def __init__(self): pass diff --git a/port/cmsis-pack/pikascript/PikaStdData.pyi b/port/cmsis-pack/pikascript/PikaStdData.pyi index 28c07f867..362491070 100644 --- a/port/cmsis-pack/pikascript/PikaStdData.pyi +++ b/port/cmsis-pack/pikascript/PikaStdData.pyi @@ -1,28 +1,32 @@ -from PikaObj import * - - -class List(TinyObj): +class Tuple: def __init__(self): ... - # add an arg after the end of list - def append(self, arg: any): ... # get an arg by the index def get(self, i: int) -> any: ... - # set an arg by the index - def set(self, i: int, arg: any): ... # get the length of list def len(self) -> int: ... # support for loop def __iter__(self) -> any: ... # support for loop def __next__(self) -> any: ... - # support list[] = val - def __setitem__(self, __key: any, __val: any): ... # support val = list[] def __getitem__(self, __key: any) -> any: ... def __del__(self): ... + def __str__(self) -> str: ... + def __len__(self) -> int: ... -class Dict(TinyObj): +class List(Tuple): + def __init__(self): ... + # add an arg after the end of list + def append(self, arg: any): ... + # set an arg by the index + def set(self, i: int, arg: any): ... + # support list[] = val + def __setitem__(self, __key: any, __val: any): ... + def __str__(self) -> str: ... + + +class Dict: def __init__(self): ... # get an arg by the key def get(self, key: str) -> any: ... @@ -37,9 +41,19 @@ class Dict(TinyObj): # support val = dict[] def __getitem__(self, __key: any) -> any: ... def __del__(self): ... + def __str__(self) -> str: ... + def keys(self) -> dict_keys: ... + def __len__(self) -> int: ... -class String(TinyObj): +class dict_keys: + def __iter__(self) -> any: ... + def __next__(self) -> any: ... + def __str__(self) -> str: ... + def __len__(self) -> int: ... + + +class String: def __init__(self, s: str): ... def set(self, s: str): ... def get(self) -> str: ... @@ -51,6 +65,8 @@ class String(TinyObj): def __getitem__(self, __key: any) -> any: ... # support str() def __str__(self) -> str: ... + def __len__(self) -> int: ... + def encode(self) -> bytes: ... def startwith(self, prefix: str) -> int: ... def endwith(self, suffix: str) -> int: ... @@ -59,13 +75,37 @@ class String(TinyObj): def isalnum(self) -> int: ... def isalpha(self) -> int: ... def isspace(self) -> int: ... + def split(self, s: str) -> List: ... + def replace(self, old: str, new: str) -> str: ... + def strip(self) -> str: ... -class ByteArray(List): +class ByteArray: # convert a string to ByteArray - def fromString(self, s: str): ... + def __init__(self, bytes: any): ... + # support for loop + def __iter__(self) -> any: ... + # support for loop + def __next__(self) -> any: ... + # support [] index + def __getitem__(self, __key: int) -> int: ... + def __setitem__(self, __key: int, __val: int): ... + def __str__(self) -> str: ... + def decode(self) -> str: ... -class Utils(TinyObj): +class FILEIO: + def init(self, path: str, mode: str): ... + def read(self, size: int) -> any: ... + def write(self, s: any) -> int: ... + def close(self): ... + def seek(self, offset: int, *fromwhere) -> int: ... + def tell(self) -> int: ... + def readline(self) -> str: ... + def readlines(self) -> List: ... + def writelines(self, lines: List): ... + + +class Utils: # convert a int to bytes def int_to_bytes(self, val: int) -> bytes: ... diff --git a/port/cmsis-pack/pikascript/PikaStdLib.pyi b/port/cmsis-pack/pikascript/PikaStdLib.pyi index 9a81c7f9a..20c981bdf 100644 --- a/port/cmsis-pack/pikascript/PikaStdLib.pyi +++ b/port/cmsis-pack/pikascript/PikaStdLib.pyi @@ -1,7 +1,4 @@ -from PikaObj import * - - -class MemChecker(TinyObj): +class MemChecker: def max(self): ... def now(self): ... def getMax(self) -> float: ... @@ -9,37 +6,60 @@ class MemChecker(TinyObj): def resetMax(self): ... -class SysObj(BaseObj): - def type(self, arg: any): ... - def remove(self, argPath: str): ... - def int(self, arg: any) -> int: ... - def float(self, arg: any) -> float: ... - def str(self, arg: any) -> str: ... - def iter(self, arg: any) -> any: ... - def range(self, a1: int, a2: int) -> any: ... - def print(self, *val): ... - def printNoEnd(self, val: any): ... - def __setitem__(self, obj: any, key: any, val: any, obj_str: str): ... - def __getitem__(self, obj: any, key: any) -> any: ... - def __slice__(self, obj: any, start: any, end: any, step: int) -> any: ... - def len(self, arg: any) -> int: ... - def list(self) -> any: ... - def dict(self) -> any: ... - def hex(self, val: int) -> str: ... - def ord(self, val: str) -> int: ... - def chr(self, val: int) -> str: ... - def bytes(self, val: any) -> bytes: ... - def cformat(self, fmt: str, *var) -> str: ... - def id(self, obj: any) -> int: ... +class SysObj: + @staticmethod + def type(arg: any) -> any: ... + @staticmethod + def remove(argPath: str): ... + @staticmethod + def int(arg: any) -> int: ... + @staticmethod + def float(arg: any) -> float: ... + @staticmethod + def str(arg: any) -> str: ... + @staticmethod + def iter(arg: any) -> any: ... + @staticmethod + def range(a1: int, a2: int) -> any: ... + @staticmethod + def print(*val): ... + @staticmethod + def printNoEnd(val: any): ... + @staticmethod + def __setitem__(obj: any, key: any, val: any) -> any: ... + @staticmethod + def __getitem__(obj: any, key: any) -> any: ... + @staticmethod + def __slice__(obj: any, start: any, end: any, step: int) -> any: ... + @staticmethod + def len(arg: any) -> int: ... + @staticmethod + def list() -> any: ... + @staticmethod + def dict() -> any: ... + @staticmethod + def hex(val: int) -> str: ... + @staticmethod + def ord(val: str) -> int: ... + @staticmethod + def chr(val: int) -> str: ... + @staticmethod + def bytes(val: any) -> bytes: ... + @staticmethod + def cformat(fmt: str, *var) -> str: ... + @staticmethod + def id(obj: any) -> int: ... + @staticmethod + def open(path: str, mode: str) -> object: ... -class RangeObj(TinyObj): +class RangeObj: def __next__(self) -> any: ... -class StringObj(TinyObj): +class StringObj: def __next__(self) -> any: ... -class PikaObj(TinyObj): +class PikaObj: ... diff --git a/port/cmsis-pack/pikascript/PikaStdTask.pyi b/port/cmsis-pack/pikascript/PikaStdTask.pyi index 1d2ee16dc..e262d60ee 100644 --- a/port/cmsis-pack/pikascript/PikaStdTask.pyi +++ b/port/cmsis-pack/pikascript/PikaStdTask.pyi @@ -1,9 +1,8 @@ -#api -from PikaObj import * import PikaStdData +import PikaStdLib -class Task(TinyObj): +class Task(PikaStdLib.SysObj): calls = PikaStdData.List() def __init__(self): diff --git a/port/cmsis-pack/pikascript/PikaTech.PikaScript.pdsc b/port/cmsis-pack/pikascript/PikaTech.PikaScript.pdsc index b84adc7e4..8312e596e 100644 --- a/port/cmsis-pack/pikascript/PikaTech.PikaScript.pdsc +++ b/port/cmsis-pack/pikascript/PikaTech.PikaScript.pdsc @@ -12,14 +12,14 @@ --> - + https://github.com/pikasTech/pikascript.git - + - - - PikaScript v1.8.7 + + - PikaScript v1.10.0 - + @@ -27,84 +27,84 @@ Python MCU - - - - Arm Compiler 5 (armcc) or Arm Compiler 6 (armclang). - - - - - - GNU Tools for Arm Embedded Processors. - - - - Support All Cortex-M based processors - - - - - - - - - - - - - - - - - - - - - Require CMSIS-CORE Support - - - - - Compile Cortex-M Processors with GNU Tools for Arm Embedded Processors. - - - - - Compile Cortex-M Processors with GNU Tools for Arm Embedded Processors. - - - - - - Compile Cortex-M Processors with GNU Tools for Arm Embedded Processors. - - - - - - Compile Cortex-M Processors with GNU Tools for Arm Embedded Processors. - - - - - - - The Kernel of the PikaScript - - - - - The Kernel of the PikaScript - - - - - The Kernel of the PikaScript - - - - + + + + Arm Compiler 5 (armcc) or Arm Compiler 6 (armclang). + + + + + + GNU Tools for Arm Embedded Processors. + + + + Support All Cortex-M based processors + + + + + + + + + + + + + + + + + + + + + Require CMSIS-CORE Support + + + + + Compile Cortex-M Processors with GNU Tools for Arm Embedded Processors. + + + + + Compile Cortex-M Processors with GNU Tools for Arm Embedded Processors. + + + + + + Compile Cortex-M Processors with GNU Tools for Arm Embedded Processors. + + + + + + Compile Cortex-M Processors with GNU Tools for Arm Embedded Processors. + + + + + + + The Kernel of the PikaScript + + + + + The Kernel of the PikaScript + + + + + The Kernel of the PikaScript + + + + - + - - - an ultra-lightweight Python engine - https://pikadoc.readthedocs.io/en/latest/index.html + + + an ultra-lightweight Python engine + https://pikadoc.readthedocs.io/en/latest/index.html - - PikaScript Kernel - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + PikaScript Kernel + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + #define RTE_Script_PikaScript - - - - The standard library for PikaScript - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + The standard library for PikaScript + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - + + \ No newline at end of file diff --git a/port/cmsis-pack/pikascript/makepdsc.py b/port/cmsis-pack/pikascript/makepdsc.py new file mode 100644 index 000000000..2d5b237b4 --- /dev/null +++ b/port/cmsis-pack/pikascript/makepdsc.py @@ -0,0 +1,56 @@ +import os +from xmlrpc.server import list_public_methods + + +class Group: + path: str + files: list + subfix = ".c" + name: str + + def __init__(self, name: str, path: str, subfix: str, format=''): + self.path = path + self.files = [] + self.subfix = subfix + self.format = format + self.name = name + self.update() + + def setformat(self, format: str): + self.format = format + + def update(self): + files = os.listdir(self.path) + self.files = [file for file in files if file.endswith(self.subfix)] + + def __str__(self): + res = "" + for file in self.files: + res += self.format % (self.path + '/' + file) + '\n' + return res + + def write(self): + with open(self.name + ".xml", 'w') as f: + f.write(str(self)) + + +def collect(name, groupList: list[Group]): + with open(name + ".xml", 'w') as f: + for group in groupList: + print(group) + f.write(str(group)) + + +kernal_c = Group("kernalH", "pikascript-core", ".c") +kernal_h = Group("kernalC", "pikascript-core", ".h", + format='') +kernal_cfg = Group("kernalCfg", "pikascript-core", ".cfg", + format='') +lib_c = Group("libC", "pikascript-lib/PikaStdLib", ".c") +lib_h = Group("libH", "pikascript-lib/PikaStdLib", ".h") + +api_c = Group("apiC", "pikascript-api", ".c") +api_h = Group("apiH", "pikascript-api", ".h") + +collect("kernal", [kernal_c, kernal_h, kernal_cfg]) +collect("lib", [lib_c, lib_h, api_c, api_h]) diff --git a/port/cmsis-pack/pikascript/pikascript-api/PikaStdData-api.c b/port/cmsis-pack/pikascript/pikascript-api/PikaStdData-api.c index 25ee3bb0c..9c319e1a7 100644 --- a/port/cmsis-pack/pikascript/pikascript-api/PikaStdData-api.c +++ b/port/cmsis-pack/pikascript/pikascript-api/PikaStdData-api.c @@ -17,6 +17,11 @@ void PikaStdData_DictMethod(PikaObj *self, Args *args){ method_returnArg(args, res); } +void PikaStdData_FILEIOMethod(PikaObj *self, Args *args){ + Arg* res = PikaStdData_FILEIO(self); + method_returnArg(args, res); +} + void PikaStdData_ListMethod(PikaObj *self, Args *args){ Arg* res = PikaStdData_List(self); method_returnArg(args, res); @@ -27,18 +32,31 @@ void PikaStdData_StringMethod(PikaObj *self, Args *args){ method_returnArg(args, res); } +void PikaStdData_TupleMethod(PikaObj *self, Args *args){ + Arg* res = PikaStdData_Tuple(self); + method_returnArg(args, res); +} + void PikaStdData_UtilsMethod(PikaObj *self, Args *args){ Arg* res = PikaStdData_Utils(self); method_returnArg(args, res); } +void PikaStdData_dict_keysMethod(PikaObj *self, Args *args){ + Arg* res = PikaStdData_dict_keys(self); + method_returnArg(args, res); +} + 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); return self; } diff --git a/port/cmsis-pack/pikascript/pikascript-api/PikaStdData.h b/port/cmsis-pack/pikascript/pikascript-api/PikaStdData.h index 207fe9976..18dd45094 100644 --- a/port/cmsis-pack/pikascript/pikascript-api/PikaStdData.h +++ b/port/cmsis-pack/pikascript/pikascript-api/PikaStdData.h @@ -11,8 +11,11 @@ PikaObj *New_PikaStdData(Args *args); Arg* PikaStdData_ByteArray(PikaObj *self); Arg* PikaStdData_Dict(PikaObj *self); +Arg* PikaStdData_FILEIO(PikaObj *self); Arg* PikaStdData_List(PikaObj *self); Arg* PikaStdData_String(PikaObj *self); +Arg* PikaStdData_Tuple(PikaObj *self); Arg* PikaStdData_Utils(PikaObj *self); +Arg* PikaStdData_dict_keys(PikaObj *self); #endif diff --git a/port/cmsis-pack/pikascript/pikascript-api/PikaStdData_ByteArray-api.c b/port/cmsis-pack/pikascript/pikascript-api/PikaStdData_ByteArray-api.c index ce6bb8cda..f3843a2b2 100644 --- a/port/cmsis-pack/pikascript/pikascript-api/PikaStdData_ByteArray-api.c +++ b/port/cmsis-pack/pikascript/pikascript-api/PikaStdData_ByteArray-api.c @@ -2,19 +2,57 @@ /* Warning! Don't modify this file! */ /* ******************************** */ #include "PikaStdData_ByteArray.h" -#include "PikaStdData_List.h" +#include "TinyObj.h" #include #include #include "BaseObj.h" -void PikaStdData_ByteArray_fromStringMethod(PikaObj *self, Args *args){ - char* s = args_getStr(args, "s"); - PikaStdData_ByteArray_fromString(self, s); +void PikaStdData_ByteArray___getitem__Method(PikaObj *self, Args *args){ + int __key = args_getInt(args, "__key"); + int res = PikaStdData_ByteArray___getitem__(self, __key); + method_returnInt(args, res); +} + +void PikaStdData_ByteArray___init__Method(PikaObj *self, Args *args){ + Arg* bytes = args_getArg(args, "bytes"); + PikaStdData_ByteArray___init__(self, bytes); +} + +void PikaStdData_ByteArray___iter__Method(PikaObj *self, Args *args){ + Arg* res = PikaStdData_ByteArray___iter__(self); + method_returnArg(args, res); +} + +void PikaStdData_ByteArray___next__Method(PikaObj *self, Args *args){ + Arg* res = PikaStdData_ByteArray___next__(self); + method_returnArg(args, res); +} + +void PikaStdData_ByteArray___setitem__Method(PikaObj *self, Args *args){ + int __key = args_getInt(args, "__key"); + int __val = args_getInt(args, "__val"); + PikaStdData_ByteArray___setitem__(self, __key, __val); +} + +void PikaStdData_ByteArray___str__Method(PikaObj *self, Args *args){ + char* res = PikaStdData_ByteArray___str__(self); + method_returnStr(args, res); +} + +void PikaStdData_ByteArray_decodeMethod(PikaObj *self, Args *args){ + char* res = PikaStdData_ByteArray_decode(self); + method_returnStr(args, res); } PikaObj *New_PikaStdData_ByteArray(Args *args){ - PikaObj *self = New_PikaStdData_List(args); - class_defineMethod(self, "fromString(s:str)", PikaStdData_ByteArray_fromStringMethod); + 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); return self; } diff --git a/port/cmsis-pack/pikascript/pikascript-api/PikaStdData_ByteArray.h b/port/cmsis-pack/pikascript/pikascript-api/PikaStdData_ByteArray.h index 3e1f968e2..a975059b2 100644 --- a/port/cmsis-pack/pikascript/pikascript-api/PikaStdData_ByteArray.h +++ b/port/cmsis-pack/pikascript/pikascript-api/PikaStdData_ByteArray.h @@ -9,6 +9,12 @@ PikaObj *New_PikaStdData_ByteArray(Args *args); -void PikaStdData_ByteArray_fromString(PikaObj *self, char* s); +int PikaStdData_ByteArray___getitem__(PikaObj *self, int __key); +void PikaStdData_ByteArray___init__(PikaObj *self, Arg* bytes); +Arg* PikaStdData_ByteArray___iter__(PikaObj *self); +Arg* PikaStdData_ByteArray___next__(PikaObj *self); +void PikaStdData_ByteArray___setitem__(PikaObj *self, int __key, int __val); +char* PikaStdData_ByteArray___str__(PikaObj *self); +char* PikaStdData_ByteArray_decode(PikaObj *self); #endif diff --git a/port/cmsis-pack/pikascript/pikascript-api/PikaStdData_Dict-api.c b/port/cmsis-pack/pikascript/pikascript-api/PikaStdData_Dict-api.c index 677e6856c..650718fa2 100644 --- a/port/cmsis-pack/pikascript/pikascript-api/PikaStdData_Dict-api.c +++ b/port/cmsis-pack/pikascript/pikascript-api/PikaStdData_Dict-api.c @@ -26,6 +26,11 @@ void PikaStdData_Dict___iter__Method(PikaObj *self, Args *args){ method_returnArg(args, res); } +void PikaStdData_Dict___len__Method(PikaObj *self, Args *args){ + int res = PikaStdData_Dict___len__(self); + method_returnInt(args, res); +} + void PikaStdData_Dict___next__Method(PikaObj *self, Args *args){ Arg* res = PikaStdData_Dict___next__(self); method_returnArg(args, res); @@ -37,21 +42,31 @@ void PikaStdData_Dict___setitem__Method(PikaObj *self, Args *args){ PikaStdData_Dict___setitem__(self, __key, __val); } +void PikaStdData_Dict___str__Method(PikaObj *self, Args *args){ + char* res = PikaStdData_Dict___str__(self); + method_returnStr(args, res); +} + void PikaStdData_Dict_getMethod(PikaObj *self, Args *args){ char* key = args_getStr(args, "key"); Arg* res = PikaStdData_Dict_get(self, key); method_returnArg(args, res); } +void PikaStdData_Dict_keysMethod(PikaObj *self, Args *args){ + PikaObj* res = PikaStdData_Dict_keys(self); + method_returnObj(args, res); +} + void PikaStdData_Dict_removeMethod(PikaObj *self, Args *args){ char* key = args_getStr(args, "key"); PikaStdData_Dict_remove(self, key); } void PikaStdData_Dict_setMethod(PikaObj *self, Args *args){ - Arg* arg = args_getArg(args, "arg"); char* key = args_getStr(args, "key"); - PikaStdData_Dict_set(self, arg, key); + Arg* arg = args_getArg(args, "arg"); + PikaStdData_Dict_set(self, key, arg); } PikaObj *New_PikaStdData_Dict(Args *args){ @@ -60,9 +75,12 @@ PikaObj *New_PikaStdData_Dict(Args *args){ class_defineMethod(self, "__getitem__(__key:any)->any", 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); return self; diff --git a/port/cmsis-pack/pikascript/pikascript-api/PikaStdData_Dict.h b/port/cmsis-pack/pikascript/pikascript-api/PikaStdData_Dict.h index 1da0813ec..aab9367f0 100644 --- a/port/cmsis-pack/pikascript/pikascript-api/PikaStdData_Dict.h +++ b/port/cmsis-pack/pikascript/pikascript-api/PikaStdData_Dict.h @@ -13,10 +13,13 @@ void PikaStdData_Dict___del__(PikaObj *self); Arg* PikaStdData_Dict___getitem__(PikaObj *self, Arg* __key); void PikaStdData_Dict___init__(PikaObj *self); Arg* PikaStdData_Dict___iter__(PikaObj *self); +int PikaStdData_Dict___len__(PikaObj *self); 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_keys(PikaObj *self); void PikaStdData_Dict_remove(PikaObj *self, char* key); -void PikaStdData_Dict_set(PikaObj *self, Arg* arg, char* key); +void PikaStdData_Dict_set(PikaObj *self, char* key, Arg* arg); #endif diff --git a/port/cmsis-pack/pikascript/pikascript-api/PikaStdData_FILEIO-api.c b/port/cmsis-pack/pikascript/pikascript-api/PikaStdData_FILEIO-api.c new file mode 100644 index 000000000..439b3fbc1 --- /dev/null +++ b/port/cmsis-pack/pikascript/pikascript-api/PikaStdData_FILEIO-api.c @@ -0,0 +1,75 @@ +/* ******************************** */ +/* Warning! Don't modify this file! */ +/* ******************************** */ +#include "PikaStdData_FILEIO.h" +#include "TinyObj.h" +#include +#include +#include "BaseObj.h" + +void PikaStdData_FILEIO_closeMethod(PikaObj *self, Args *args){ + PikaStdData_FILEIO_close(self); +} + +void PikaStdData_FILEIO_initMethod(PikaObj *self, Args *args){ + char* path = args_getStr(args, "path"); + char* mode = args_getStr(args, "mode"); + PikaStdData_FILEIO_init(self, path, mode); +} + +void PikaStdData_FILEIO_readMethod(PikaObj *self, Args *args){ + int size = args_getInt(args, "size"); + Arg* res = PikaStdData_FILEIO_read(self, size); + method_returnArg(args, res); +} + +void PikaStdData_FILEIO_readlineMethod(PikaObj *self, Args *args){ + char* res = PikaStdData_FILEIO_readline(self); + method_returnStr(args, res); +} + +void PikaStdData_FILEIO_readlinesMethod(PikaObj *self, Args *args){ + PikaObj* res = PikaStdData_FILEIO_readlines(self); + method_returnObj(args, res); +} + +void PikaStdData_FILEIO_seekMethod(PikaObj *self, Args *args){ + int offset = args_getInt(args, "offset"); + PikaTuple* fromwhere = args_getPtr(args, "fromwhere"); + int res = PikaStdData_FILEIO_seek(self, offset, fromwhere); + method_returnInt(args, res); +} + +void PikaStdData_FILEIO_tellMethod(PikaObj *self, Args *args){ + int res = PikaStdData_FILEIO_tell(self); + method_returnInt(args, res); +} + +void PikaStdData_FILEIO_writeMethod(PikaObj *self, Args *args){ + Arg* s = args_getArg(args, "s"); + int res = PikaStdData_FILEIO_write(self, s); + method_returnInt(args, res); +} + +void PikaStdData_FILEIO_writelinesMethod(PikaObj *self, Args *args){ + PikaObj* lines = args_getPtr(args, "lines"); + PikaStdData_FILEIO_writelines(self, lines); +} + +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)", 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); + return self; +} + +Arg *PikaStdData_FILEIO(PikaObj *self){ + return obj_newObjInPackage(New_PikaStdData_FILEIO); +} diff --git a/port/cmsis-pack/pikascript/pikascript-api/PikaStdData_FILEIO.h b/port/cmsis-pack/pikascript/pikascript-api/PikaStdData_FILEIO.h new file mode 100644 index 000000000..f3e46c732 --- /dev/null +++ b/port/cmsis-pack/pikascript/pikascript-api/PikaStdData_FILEIO.h @@ -0,0 +1,22 @@ +/* ******************************** */ +/* Warning! Don't modify this file! */ +/* ******************************** */ +#ifndef __PikaStdData_FILEIO__H +#define __PikaStdData_FILEIO__H +#include +#include +#include "PikaObj.h" + +PikaObj *New_PikaStdData_FILEIO(Args *args); + +void PikaStdData_FILEIO_close(PikaObj *self); +void PikaStdData_FILEIO_init(PikaObj *self, char* path, char* mode); +Arg* PikaStdData_FILEIO_read(PikaObj *self, int size); +char* PikaStdData_FILEIO_readline(PikaObj *self); +PikaObj* PikaStdData_FILEIO_readlines(PikaObj *self); +int PikaStdData_FILEIO_seek(PikaObj *self, int offset, PikaTuple* fromwhere); +int PikaStdData_FILEIO_tell(PikaObj *self); +int PikaStdData_FILEIO_write(PikaObj *self, Arg* s); +void PikaStdData_FILEIO_writelines(PikaObj *self, PikaObj* lines); + +#endif diff --git a/port/cmsis-pack/pikascript/pikascript-api/PikaStdData_List-api.c b/port/cmsis-pack/pikascript/pikascript-api/PikaStdData_List-api.c index 3e6bb9b10..6b95bacb1 100644 --- a/port/cmsis-pack/pikascript/pikascript-api/PikaStdData_List-api.c +++ b/port/cmsis-pack/pikascript/pikascript-api/PikaStdData_List-api.c @@ -2,74 +2,43 @@ /* Warning! Don't modify this file! */ /* ******************************** */ #include "PikaStdData_List.h" -#include "TinyObj.h" +#include "PikaStdData_Tuple.h" #include #include #include "BaseObj.h" -void PikaStdData_List___del__Method(PikaObj *self, Args *args){ - PikaStdData_List___del__(self); -} - -void PikaStdData_List___getitem__Method(PikaObj *self, Args *args){ - Arg* __key = args_getArg(args, "__key"); - Arg* res = PikaStdData_List___getitem__(self, __key); - method_returnArg(args, res); -} - void PikaStdData_List___init__Method(PikaObj *self, Args *args){ PikaStdData_List___init__(self); } -void PikaStdData_List___iter__Method(PikaObj *self, Args *args){ - Arg* res = PikaStdData_List___iter__(self); - method_returnArg(args, res); -} - -void PikaStdData_List___next__Method(PikaObj *self, Args *args){ - Arg* res = PikaStdData_List___next__(self); - method_returnArg(args, res); -} - void PikaStdData_List___setitem__Method(PikaObj *self, Args *args){ Arg* __key = args_getArg(args, "__key"); Arg* __val = args_getArg(args, "__val"); PikaStdData_List___setitem__(self, __key, __val); } +void PikaStdData_List___str__Method(PikaObj *self, Args *args){ + char* res = PikaStdData_List___str__(self); + method_returnStr(args, res); +} + void PikaStdData_List_appendMethod(PikaObj *self, Args *args){ Arg* arg = args_getArg(args, "arg"); PikaStdData_List_append(self, arg); } -void PikaStdData_List_getMethod(PikaObj *self, Args *args){ - int i = args_getInt(args, "i"); - Arg* res = PikaStdData_List_get(self, i); - method_returnArg(args, res); -} - -void PikaStdData_List_lenMethod(PikaObj *self, Args *args){ - int res = PikaStdData_List_len(self); - method_returnInt(args, res); -} - void PikaStdData_List_setMethod(PikaObj *self, Args *args){ - Arg* arg = args_getArg(args, "arg"); int i = args_getInt(args, "i"); - PikaStdData_List_set(self, arg, i); + Arg* arg = args_getArg(args, "arg"); + PikaStdData_List_set(self, i, arg); } PikaObj *New_PikaStdData_List(Args *args){ - PikaObj *self = New_TinyObj(args); - class_defineMethod(self, "__del__()", PikaStdData_List___del__Method); - class_defineMethod(self, "__getitem__(__key:any)->any", PikaStdData_List___getitem__Method); + PikaObj *self = New_PikaStdData_Tuple(args); class_defineMethod(self, "__init__()", PikaStdData_List___init__Method); - class_defineMethod(self, "__iter__()->any", PikaStdData_List___iter__Method); - class_defineMethod(self, "__next__()->any", PikaStdData_List___next__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, "get(i:int)->any", PikaStdData_List_getMethod); - class_defineMethod(self, "len()->int", PikaStdData_List_lenMethod); class_defineMethod(self, "set(i:int,arg:any)", PikaStdData_List_setMethod); return self; } diff --git a/port/cmsis-pack/pikascript/pikascript-api/PikaStdData_List.h b/port/cmsis-pack/pikascript/pikascript-api/PikaStdData_List.h index 6575e3bb6..1ce72ca44 100644 --- a/port/cmsis-pack/pikascript/pikascript-api/PikaStdData_List.h +++ b/port/cmsis-pack/pikascript/pikascript-api/PikaStdData_List.h @@ -9,15 +9,10 @@ PikaObj *New_PikaStdData_List(Args *args); -void PikaStdData_List___del__(PikaObj *self); -Arg* PikaStdData_List___getitem__(PikaObj *self, Arg* __key); void PikaStdData_List___init__(PikaObj *self); -Arg* PikaStdData_List___iter__(PikaObj *self); -Arg* PikaStdData_List___next__(PikaObj *self); void PikaStdData_List___setitem__(PikaObj *self, Arg* __key, Arg* __val); +char* PikaStdData_List___str__(PikaObj *self); void PikaStdData_List_append(PikaObj *self, Arg* arg); -Arg* PikaStdData_List_get(PikaObj *self, int i); -int PikaStdData_List_len(PikaObj *self); -void PikaStdData_List_set(PikaObj *self, Arg* arg, int i); +void PikaStdData_List_set(PikaObj *self, int i, Arg* arg); #endif diff --git a/port/cmsis-pack/pikascript/pikascript-api/PikaStdData_String-api.c b/port/cmsis-pack/pikascript/pikascript-api/PikaStdData_String-api.c index da157d8ed..e102253f9 100644 --- a/port/cmsis-pack/pikascript/pikascript-api/PikaStdData_String-api.c +++ b/port/cmsis-pack/pikascript/pikascript-api/PikaStdData_String-api.c @@ -23,6 +23,11 @@ void PikaStdData_String___iter__Method(PikaObj *self, Args *args){ method_returnArg(args, res); } +void PikaStdData_String___len__Method(PikaObj *self, Args *args){ + int res = PikaStdData_String___len__(self); + method_returnInt(args, res); +} + void PikaStdData_String___next__Method(PikaObj *self, Args *args){ Arg* res = PikaStdData_String___next__(self); method_returnArg(args, res); @@ -39,6 +44,11 @@ void PikaStdData_String___str__Method(PikaObj *self, Args *args){ method_returnStr(args, res); } +void PikaStdData_String_encodeMethod(PikaObj *self, Args *args){ + Arg* res = PikaStdData_String_encode(self); + method_returnArg(args, res); +} + void PikaStdData_String_endwithMethod(PikaObj *self, Args *args){ char* suffix = args_getStr(args, "suffix"); int res = PikaStdData_String_endwith(self, suffix); @@ -75,25 +85,45 @@ void PikaStdData_String_isspaceMethod(PikaObj *self, Args *args){ method_returnInt(args, res); } +void PikaStdData_String_replaceMethod(PikaObj *self, Args *args){ + char* old = args_getStr(args, "old"); + char* new = args_getStr(args, "new"); + char* res = PikaStdData_String_replace(self, old, new); + method_returnStr(args, res); +} + void PikaStdData_String_setMethod(PikaObj *self, Args *args){ char* s = args_getStr(args, "s"); PikaStdData_String_set(self, s); } +void PikaStdData_String_splitMethod(PikaObj *self, Args *args){ + char* s = args_getStr(args, "s"); + PikaObj* res = PikaStdData_String_split(self, s); + method_returnObj(args, res); +} + void PikaStdData_String_startwithMethod(PikaObj *self, Args *args){ char* prefix = args_getStr(args, "prefix"); int res = PikaStdData_String_startwith(self, prefix); method_returnInt(args, res); } +void PikaStdData_String_stripMethod(PikaObj *self, Args *args){ + char* res = PikaStdData_String_strip(self); + method_returnStr(args, res); +} + 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, "endwith(suffix:str)->int", PikaStdData_String_endwithMethod); class_defineMethod(self, "get()->str", PikaStdData_String_getMethod); class_defineMethod(self, "isalnum()->int", PikaStdData_String_isalnumMethod); @@ -101,8 +131,11 @@ PikaObj *New_PikaStdData_String(Args *args){ 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, "startwith(prefix:str)->int", PikaStdData_String_startwithMethod); + class_defineMethod(self, "strip()->str", PikaStdData_String_stripMethod); return self; } diff --git a/port/cmsis-pack/pikascript/pikascript-api/PikaStdData_String.h b/port/cmsis-pack/pikascript/pikascript-api/PikaStdData_String.h index 2812d83b3..4a2a4e73a 100644 --- a/port/cmsis-pack/pikascript/pikascript-api/PikaStdData_String.h +++ b/port/cmsis-pack/pikascript/pikascript-api/PikaStdData_String.h @@ -12,9 +12,11 @@ PikaObj *New_PikaStdData_String(Args *args); Arg* PikaStdData_String___getitem__(PikaObj *self, Arg* __key); void PikaStdData_String___init__(PikaObj *self, char* s); Arg* PikaStdData_String___iter__(PikaObj *self); +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); int PikaStdData_String_endwith(PikaObj *self, char* suffix); char* PikaStdData_String_get(PikaObj *self); int PikaStdData_String_isalnum(PikaObj *self); @@ -22,7 +24,10 @@ int PikaStdData_String_isalpha(PikaObj *self); int PikaStdData_String_isdigit(PikaObj *self); int PikaStdData_String_islower(PikaObj *self); int PikaStdData_String_isspace(PikaObj *self); +char* PikaStdData_String_replace(PikaObj *self, char* old, char* new); void PikaStdData_String_set(PikaObj *self, char* s); +PikaObj* PikaStdData_String_split(PikaObj *self, char* s); int PikaStdData_String_startwith(PikaObj *self, char* prefix); +char* PikaStdData_String_strip(PikaObj *self); #endif diff --git a/port/cmsis-pack/pikascript/pikascript-api/PikaStdData_Tuple-api.c b/port/cmsis-pack/pikascript/pikascript-api/PikaStdData_Tuple-api.c new file mode 100644 index 000000000..11e99ed08 --- /dev/null +++ b/port/cmsis-pack/pikascript/pikascript-api/PikaStdData_Tuple-api.c @@ -0,0 +1,71 @@ +/* ******************************** */ +/* Warning! Don't modify this file! */ +/* ******************************** */ +#include "PikaStdData_Tuple.h" +#include "TinyObj.h" +#include +#include +#include "BaseObj.h" + +void PikaStdData_Tuple___del__Method(PikaObj *self, Args *args){ + PikaStdData_Tuple___del__(self); +} + +void PikaStdData_Tuple___getitem__Method(PikaObj *self, Args *args){ + Arg* __key = args_getArg(args, "__key"); + Arg* res = PikaStdData_Tuple___getitem__(self, __key); + method_returnArg(args, res); +} + +void PikaStdData_Tuple___init__Method(PikaObj *self, Args *args){ + PikaStdData_Tuple___init__(self); +} + +void PikaStdData_Tuple___iter__Method(PikaObj *self, Args *args){ + Arg* res = PikaStdData_Tuple___iter__(self); + method_returnArg(args, res); +} + +void PikaStdData_Tuple___len__Method(PikaObj *self, Args *args){ + int res = PikaStdData_Tuple___len__(self); + method_returnInt(args, res); +} + +void PikaStdData_Tuple___next__Method(PikaObj *self, Args *args){ + Arg* res = PikaStdData_Tuple___next__(self); + method_returnArg(args, res); +} + +void PikaStdData_Tuple___str__Method(PikaObj *self, Args *args){ + char* res = PikaStdData_Tuple___str__(self); + method_returnStr(args, res); +} + +void PikaStdData_Tuple_getMethod(PikaObj *self, Args *args){ + int i = args_getInt(args, "i"); + Arg* res = PikaStdData_Tuple_get(self, i); + method_returnArg(args, res); +} + +void PikaStdData_Tuple_lenMethod(PikaObj *self, Args *args){ + int res = PikaStdData_Tuple_len(self); + method_returnInt(args, res); +} + +PikaObj *New_PikaStdData_Tuple(Args *args){ + PikaObj *self = New_TinyObj(args); + class_defineMethod(self, "__del__()", PikaStdData_Tuple___del__Method); + class_defineMethod(self, "__getitem__(__key:any)->any", 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); + return self; +} + +Arg *PikaStdData_Tuple(PikaObj *self){ + return obj_newObjInPackage(New_PikaStdData_Tuple); +} diff --git a/port/cmsis-pack/pikascript/pikascript-api/PikaStdData_Tuple.h b/port/cmsis-pack/pikascript/pikascript-api/PikaStdData_Tuple.h new file mode 100644 index 000000000..93e5ecc2d --- /dev/null +++ b/port/cmsis-pack/pikascript/pikascript-api/PikaStdData_Tuple.h @@ -0,0 +1,22 @@ +/* ******************************** */ +/* Warning! Don't modify this file! */ +/* ******************************** */ +#ifndef __PikaStdData_Tuple__H +#define __PikaStdData_Tuple__H +#include +#include +#include "PikaObj.h" + +PikaObj *New_PikaStdData_Tuple(Args *args); + +void PikaStdData_Tuple___del__(PikaObj *self); +Arg* PikaStdData_Tuple___getitem__(PikaObj *self, Arg* __key); +void PikaStdData_Tuple___init__(PikaObj *self); +Arg* PikaStdData_Tuple___iter__(PikaObj *self); +int PikaStdData_Tuple___len__(PikaObj *self); +Arg* PikaStdData_Tuple___next__(PikaObj *self); +char* PikaStdData_Tuple___str__(PikaObj *self); +Arg* PikaStdData_Tuple_get(PikaObj *self, int i); +int PikaStdData_Tuple_len(PikaObj *self); + +#endif diff --git a/port/cmsis-pack/pikascript/pikascript-api/PikaStdData_dict_keys-api.c b/port/cmsis-pack/pikascript/pikascript-api/PikaStdData_dict_keys-api.c new file mode 100644 index 000000000..4199d88b3 --- /dev/null +++ b/port/cmsis-pack/pikascript/pikascript-api/PikaStdData_dict_keys-api.c @@ -0,0 +1,41 @@ +/* ******************************** */ +/* Warning! Don't modify this file! */ +/* ******************************** */ +#include "PikaStdData_dict_keys.h" +#include "TinyObj.h" +#include +#include +#include "BaseObj.h" + +void PikaStdData_dict_keys___iter__Method(PikaObj *self, Args *args){ + Arg* res = PikaStdData_dict_keys___iter__(self); + method_returnArg(args, res); +} + +void PikaStdData_dict_keys___len__Method(PikaObj *self, Args *args){ + int res = PikaStdData_dict_keys___len__(self); + method_returnInt(args, res); +} + +void PikaStdData_dict_keys___next__Method(PikaObj *self, Args *args){ + Arg* res = PikaStdData_dict_keys___next__(self); + method_returnArg(args, res); +} + +void PikaStdData_dict_keys___str__Method(PikaObj *self, Args *args){ + char* res = PikaStdData_dict_keys___str__(self); + method_returnStr(args, res); +} + +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); + return self; +} + +Arg *PikaStdData_dict_keys(PikaObj *self){ + return obj_newObjInPackage(New_PikaStdData_dict_keys); +} diff --git a/port/cmsis-pack/pikascript/pikascript-api/PikaStdData_dict_keys.h b/port/cmsis-pack/pikascript/pikascript-api/PikaStdData_dict_keys.h new file mode 100644 index 000000000..ca415ed99 --- /dev/null +++ b/port/cmsis-pack/pikascript/pikascript-api/PikaStdData_dict_keys.h @@ -0,0 +1,17 @@ +/* ******************************** */ +/* Warning! Don't modify this file! */ +/* ******************************** */ +#ifndef __PikaStdData_dict_keys__H +#define __PikaStdData_dict_keys__H +#include +#include +#include "PikaObj.h" + +PikaObj *New_PikaStdData_dict_keys(Args *args); + +Arg* PikaStdData_dict_keys___iter__(PikaObj *self); +int PikaStdData_dict_keys___len__(PikaObj *self); +Arg* PikaStdData_dict_keys___next__(PikaObj *self); +char* PikaStdData_dict_keys___str__(PikaObj *self); + +#endif diff --git a/port/cmsis-pack/pikascript/pikascript-api/PikaStdLib_SysObj-api.c b/port/cmsis-pack/pikascript/pikascript-api/PikaStdLib_SysObj-api.c index 46fa3e695..4017d6ca9 100644 --- a/port/cmsis-pack/pikascript/pikascript-api/PikaStdLib_SysObj-api.c +++ b/port/cmsis-pack/pikascript/pikascript-api/PikaStdLib_SysObj-api.c @@ -2,32 +2,32 @@ /* Warning! Don't modify this file! */ /* ******************************** */ #include "PikaStdLib_SysObj.h" -#include "BaseObj.h" +#include "TinyObj.h" #include #include #include "BaseObj.h" void PikaStdLib_SysObj___getitem__Method(PikaObj *self, Args *args){ - Arg* key = args_getArg(args, "key"); Arg* obj = args_getArg(args, "obj"); - Arg* res = PikaStdLib_SysObj___getitem__(self, key, obj); + Arg* key = args_getArg(args, "key"); + Arg* res = PikaStdLib_SysObj___getitem__(self, obj, key); method_returnArg(args, res); } void PikaStdLib_SysObj___setitem__Method(PikaObj *self, Args *args){ - Arg* key = args_getArg(args, "key"); Arg* obj = args_getArg(args, "obj"); - char* obj_str = args_getStr(args, "obj_str"); + Arg* key = args_getArg(args, "key"); Arg* val = args_getArg(args, "val"); - PikaStdLib_SysObj___setitem__(self, key, obj, obj_str, val); + Arg* res = PikaStdLib_SysObj___setitem__(self, obj, key, val); + method_returnArg(args, res); } void PikaStdLib_SysObj___slice__Method(PikaObj *self, Args *args){ - Arg* end = args_getArg(args, "end"); Arg* obj = args_getArg(args, "obj"); Arg* start = args_getArg(args, "start"); + Arg* end = args_getArg(args, "end"); int step = args_getInt(args, "step"); - Arg* res = PikaStdLib_SysObj___slice__(self, end, obj, start, step); + Arg* res = PikaStdLib_SysObj___slice__(self, obj, start, end, step); method_returnArg(args, res); } @@ -96,6 +96,13 @@ void PikaStdLib_SysObj_listMethod(PikaObj *self, Args *args){ method_returnArg(args, res); } +void PikaStdLib_SysObj_openMethod(PikaObj *self, Args *args){ + char* path = args_getStr(args, "path"); + char* mode = args_getStr(args, "mode"); + PikaObj* res = PikaStdLib_SysObj_open(self, path, mode); + method_returnObj(args, res); +} + void PikaStdLib_SysObj_ordMethod(PikaObj *self, Args *args){ char* val = args_getStr(args, "val"); int res = PikaStdLib_SysObj_ord(self, val); @@ -132,13 +139,14 @@ void PikaStdLib_SysObj_strMethod(PikaObj *self, Args *args){ void PikaStdLib_SysObj_typeMethod(PikaObj *self, Args *args){ Arg* arg = args_getArg(args, "arg"); - PikaStdLib_SysObj_type(self, arg); + Arg* res = PikaStdLib_SysObj_type(self, arg); + method_returnArg(args, res); } PikaObj *New_PikaStdLib_SysObj(Args *args){ - PikaObj *self = New_BaseObj(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,obj_str:str)", PikaStdLib_SysObj___setitem__Method); + class_defineMethod(self, "__setitem__(obj:any,key:any,val:any)->any", PikaStdLib_SysObj___setitem__Method); class_defineMethod(self, "__slice__(obj:any,start:any,end:any,step:int)->any", PikaStdLib_SysObj___slice__Method); class_defineMethod(self, "bytes(val:any)->bytes", PikaStdLib_SysObj_bytesMethod); class_defineMethod(self, "cformat(fmt:str,*var)->str", PikaStdLib_SysObj_cformatMethod); @@ -151,13 +159,14 @@ PikaObj *New_PikaStdLib_SysObj(Args *args){ class_defineMethod(self, "iter(arg:any)->any", PikaStdLib_SysObj_iterMethod); class_defineMethod(self, "len(arg:any)->int", PikaStdLib_SysObj_lenMethod); class_defineMethod(self, "list()->any", PikaStdLib_SysObj_listMethod); + class_defineMethod(self, "open(path:str,mode:str)->object", PikaStdLib_SysObj_openMethod); class_defineMethod(self, "ord(val:str)->int", PikaStdLib_SysObj_ordMethod); class_defineMethod(self, "print(*val)", PikaStdLib_SysObj_printMethod); class_defineMethod(self, "printNoEnd(val:any)", PikaStdLib_SysObj_printNoEndMethod); class_defineMethod(self, "range(a1:int,a2:int)->any", PikaStdLib_SysObj_rangeMethod); class_defineMethod(self, "remove(argPath:str)", PikaStdLib_SysObj_removeMethod); class_defineMethod(self, "str(arg:any)->str", PikaStdLib_SysObj_strMethod); - class_defineMethod(self, "type(arg:any)", PikaStdLib_SysObj_typeMethod); + class_defineMethod(self, "type(arg:any)->any", PikaStdLib_SysObj_typeMethod); return self; } diff --git a/port/cmsis-pack/pikascript/pikascript-api/PikaStdLib_SysObj.h b/port/cmsis-pack/pikascript/pikascript-api/PikaStdLib_SysObj.h index e393e1d58..e29ded624 100644 --- a/port/cmsis-pack/pikascript/pikascript-api/PikaStdLib_SysObj.h +++ b/port/cmsis-pack/pikascript/pikascript-api/PikaStdLib_SysObj.h @@ -9,9 +9,9 @@ PikaObj *New_PikaStdLib_SysObj(Args *args); -Arg* PikaStdLib_SysObj___getitem__(PikaObj *self, Arg* key, Arg* obj); -void PikaStdLib_SysObj___setitem__(PikaObj *self, Arg* key, Arg* obj, char* obj_str, Arg* val); -Arg* PikaStdLib_SysObj___slice__(PikaObj *self, Arg* end, Arg* obj, Arg* start, int step); +Arg* PikaStdLib_SysObj___getitem__(PikaObj *self, Arg* obj, Arg* key); +Arg* PikaStdLib_SysObj___setitem__(PikaObj *self, Arg* obj, Arg* key, Arg* val); +Arg* PikaStdLib_SysObj___slice__(PikaObj *self, Arg* obj, Arg* start, Arg* end, int step); Arg* PikaStdLib_SysObj_bytes(PikaObj *self, Arg* val); char* PikaStdLib_SysObj_cformat(PikaObj *self, char* fmt, PikaTuple* var); char* PikaStdLib_SysObj_chr(PikaObj *self, int val); @@ -23,12 +23,13 @@ int PikaStdLib_SysObj_int(PikaObj *self, Arg* arg); Arg* PikaStdLib_SysObj_iter(PikaObj *self, Arg* arg); 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_remove(PikaObj *self, char* argPath); char* PikaStdLib_SysObj_str(PikaObj *self, Arg* arg); -void PikaStdLib_SysObj_type(PikaObj *self, Arg* arg); +Arg* PikaStdLib_SysObj_type(PikaObj *self, Arg* arg); #endif diff --git a/port/cmsis-pack/pikascript/pikascript-api/PikaStdTask_Task-api.c b/port/cmsis-pack/pikascript/pikascript-api/PikaStdTask_Task-api.c index b0f530d71..0adabe3e3 100644 --- a/port/cmsis-pack/pikascript/pikascript-api/PikaStdTask_Task-api.c +++ b/port/cmsis-pack/pikascript/pikascript-api/PikaStdTask_Task-api.c @@ -2,7 +2,7 @@ /* Warning! Don't modify this file! */ /* ******************************** */ #include "PikaStdTask_Task.h" -#include "TinyObj.h" +#include "PikaStdLib_SysObj.h" #include "PikaStdData_List.h" #include #include @@ -47,7 +47,7 @@ void PikaStdTask_Task_run_until_msMethod(PikaObj *self, Args *args){ } PikaObj *New_PikaStdTask_Task(Args *args){ - PikaObj *self = New_TinyObj(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); diff --git a/port/cmsis-pack/pikascript/pikascript-api/compiler-info.txt b/port/cmsis-pack/pikascript/pikascript-api/compiler-info.txt index 33f64f707..f16f03323 100644 --- a/port/cmsis-pack/pikascript/pikascript-api/compiler-info.txt +++ b/port/cmsis-pack/pikascript/pikascript-api/compiler-info.txt @@ -1 +1 @@ -Compiler { dist_path: "pikascript-api/", source_path: "", class_list: {"PikaDebug": ClassInfo { this_class_name: "PikaDebug", this_class_name_without_file: "PikaDebug", super_class_name: "TinyObj", method_list: {"Debuger": MethodInfo { class_name: "PikaDebug", name: "Debuger", arg_list: None, return_type: Some(PyType { type_name: "any" }), is_constructor: true }}, object_list: {}, import_list: {}, script_list: Script { content: "" }, is_package: true }, "PikaDebug_Debuger": ClassInfo { this_class_name: "PikaDebug_Debuger", this_class_name_without_file: "Debuger", super_class_name: "TinyObj", method_list: {"__init__": MethodInfo { class_name: "PikaDebug_Debuger", name: "__init__", arg_list: None, return_type: None, is_constructor: false }, "set_trace": MethodInfo { class_name: "PikaDebug_Debuger", name: "set_trace", arg_list: None, return_type: None, is_constructor: false }}, object_list: {}, import_list: {}, script_list: Script { content: "" }, is_package: false }, "PikaMain": ClassInfo { this_class_name: "PikaMain", this_class_name_without_file: "PikaMain", super_class_name: "PikaStdLib_SysObj", method_list: {}, object_list: {"PikaStdLib": ObjectInfo { class_name: "PikaMain", name: "PikaStdLib", import_class_name: "PikaStdLib" }}, import_list: {}, script_list: Script { content: "@BEGIN@import PikaStdLib@END@@BEGIN@@END@@BEGIN@print('hello PikaScript!')@END@@BEGIN@@END@@BEGIN@mem = PikaStdLib.MemChecker()@END@@BEGIN@print('mem used max:')@END@@BEGIN@mem.max()@END@@BEGIN@@END@" }, is_package: false }, "PikaStdData": ClassInfo { this_class_name: "PikaStdData", this_class_name_without_file: "PikaStdData", super_class_name: "TinyObj", method_list: {"ByteArray": MethodInfo { class_name: "PikaStdData", name: "ByteArray", arg_list: None, return_type: Some(PyType { type_name: "any" }), is_constructor: true }, "Dict": MethodInfo { class_name: "PikaStdData", name: "Dict", arg_list: None, return_type: Some(PyType { type_name: "any" }), is_constructor: true }, "List": MethodInfo { class_name: "PikaStdData", name: "List", arg_list: None, return_type: Some(PyType { type_name: "any" }), is_constructor: true }, "String": MethodInfo { class_name: "PikaStdData", name: "String", arg_list: None, return_type: Some(PyType { type_name: "any" }), is_constructor: true }, "Utils": MethodInfo { class_name: "PikaStdData", name: "Utils", arg_list: None, return_type: Some(PyType { type_name: "any" }), is_constructor: true }}, object_list: {}, import_list: {}, script_list: Script { content: "" }, is_package: true }, "PikaStdData_ByteArray": ClassInfo { this_class_name: "PikaStdData_ByteArray", this_class_name_without_file: "ByteArray", super_class_name: "PikaStdData_List", method_list: {"fromString": MethodInfo { class_name: "PikaStdData_ByteArray", name: "fromString", arg_list: Some(ArgList { py_arg_list: "s:str", list: {"s": PyArg { py_type: PyType { type_name: "str" }, name: "s" }} }), return_type: None, is_constructor: false }}, object_list: {}, import_list: {}, script_list: Script { content: "" }, is_package: false }, "PikaStdData_Dict": ClassInfo { this_class_name: "PikaStdData_Dict", this_class_name_without_file: "Dict", super_class_name: "TinyObj", method_list: {"__del__": MethodInfo { class_name: "PikaStdData_Dict", name: "__del__", arg_list: None, return_type: None, is_constructor: false }, "__getitem__": MethodInfo { class_name: "PikaStdData_Dict", name: "__getitem__", arg_list: Some(ArgList { py_arg_list: "__key:any", list: {"__key": PyArg { py_type: PyType { type_name: "any" }, name: "__key" }} }), return_type: Some(PyType { type_name: "any" }), is_constructor: false }, "__init__": MethodInfo { class_name: "PikaStdData_Dict", name: "__init__", arg_list: None, return_type: None, is_constructor: false }, "__iter__": MethodInfo { class_name: "PikaStdData_Dict", name: "__iter__", arg_list: None, return_type: Some(PyType { type_name: "any" }), is_constructor: false }, "__next__": MethodInfo { class_name: "PikaStdData_Dict", name: "__next__", arg_list: None, return_type: Some(PyType { type_name: "any" }), is_constructor: false }, "__setitem__": MethodInfo { class_name: "PikaStdData_Dict", name: "__setitem__", arg_list: Some(ArgList { py_arg_list: "__key:any,__val:any", list: {"__key": PyArg { py_type: PyType { type_name: "any" }, name: "__key" }, "__val": PyArg { py_type: PyType { type_name: "any" }, name: "__val" }} }), return_type: None, is_constructor: false }, "get": MethodInfo { class_name: "PikaStdData_Dict", name: "get", arg_list: Some(ArgList { py_arg_list: "key:str", list: {"key": PyArg { py_type: PyType { type_name: "str" }, name: "key" }} }), return_type: Some(PyType { type_name: "any" }), is_constructor: false }, "remove": MethodInfo { class_name: "PikaStdData_Dict", name: "remove", arg_list: Some(ArgList { py_arg_list: "key:str", list: {"key": PyArg { py_type: PyType { type_name: "str" }, name: "key" }} }), return_type: None, is_constructor: false }, "set": MethodInfo { class_name: "PikaStdData_Dict", name: "set", arg_list: Some(ArgList { py_arg_list: "key:str,arg:any", list: {"arg": PyArg { py_type: PyType { type_name: "any" }, name: "arg" }, "key": PyArg { py_type: PyType { type_name: "str" }, name: "key" }} }), return_type: None, is_constructor: false }}, object_list: {}, import_list: {}, script_list: Script { content: "" }, is_package: false }, "PikaStdData_List": ClassInfo { this_class_name: "PikaStdData_List", this_class_name_without_file: "List", super_class_name: "TinyObj", method_list: {"__del__": MethodInfo { class_name: "PikaStdData_List", name: "__del__", arg_list: None, return_type: None, is_constructor: false }, "__getitem__": MethodInfo { class_name: "PikaStdData_List", name: "__getitem__", arg_list: Some(ArgList { py_arg_list: "__key:any", list: {"__key": PyArg { py_type: PyType { type_name: "any" }, name: "__key" }} }), return_type: Some(PyType { type_name: "any" }), is_constructor: false }, "__init__": MethodInfo { class_name: "PikaStdData_List", name: "__init__", arg_list: None, return_type: None, is_constructor: false }, "__iter__": MethodInfo { class_name: "PikaStdData_List", name: "__iter__", arg_list: None, return_type: Some(PyType { type_name: "any" }), is_constructor: false }, "__next__": MethodInfo { class_name: "PikaStdData_List", name: "__next__", arg_list: None, return_type: Some(PyType { type_name: "any" }), is_constructor: false }, "__setitem__": MethodInfo { class_name: "PikaStdData_List", name: "__setitem__", arg_list: Some(ArgList { py_arg_list: "__key:any,__val:any", list: {"__key": PyArg { py_type: PyType { type_name: "any" }, name: "__key" }, "__val": PyArg { py_type: PyType { type_name: "any" }, name: "__val" }} }), return_type: None, is_constructor: false }, "append": MethodInfo { class_name: "PikaStdData_List", name: "append", arg_list: Some(ArgList { py_arg_list: "arg:any", list: {"arg": PyArg { py_type: PyType { type_name: "any" }, name: "arg" }} }), return_type: None, is_constructor: false }, "get": MethodInfo { class_name: "PikaStdData_List", name: "get", arg_list: Some(ArgList { py_arg_list: "i:int", list: {"i": PyArg { py_type: PyType { type_name: "int" }, name: "i" }} }), return_type: Some(PyType { type_name: "any" }), is_constructor: false }, "len": MethodInfo { class_name: "PikaStdData_List", name: "len", arg_list: None, return_type: Some(PyType { type_name: "int" }), is_constructor: false }, "set": MethodInfo { class_name: "PikaStdData_List", name: "set", arg_list: Some(ArgList { py_arg_list: "i:int,arg:any", list: {"arg": PyArg { py_type: PyType { type_name: "any" }, name: "arg" }, "i": PyArg { py_type: PyType { type_name: "int" }, name: "i" }} }), return_type: None, is_constructor: false }}, object_list: {}, import_list: {}, script_list: Script { content: "" }, is_package: false }, "PikaStdData_String": ClassInfo { this_class_name: "PikaStdData_String", this_class_name_without_file: "String", super_class_name: "TinyObj", method_list: {"__getitem__": MethodInfo { class_name: "PikaStdData_String", name: "__getitem__", arg_list: Some(ArgList { py_arg_list: "__key:any", list: {"__key": PyArg { py_type: PyType { type_name: "any" }, name: "__key" }} }), return_type: Some(PyType { type_name: "any" }), is_constructor: false }, "__init__": MethodInfo { class_name: "PikaStdData_String", name: "__init__", arg_list: Some(ArgList { py_arg_list: "s:str", list: {"s": PyArg { py_type: PyType { type_name: "str" }, name: "s" }} }), return_type: None, is_constructor: false }, "__iter__": MethodInfo { class_name: "PikaStdData_String", name: "__iter__", arg_list: None, return_type: Some(PyType { type_name: "any" }), is_constructor: false }, "__next__": MethodInfo { class_name: "PikaStdData_String", name: "__next__", arg_list: None, return_type: Some(PyType { type_name: "any" }), is_constructor: false }, "__setitem__": MethodInfo { class_name: "PikaStdData_String", name: "__setitem__", arg_list: Some(ArgList { py_arg_list: "__key:any,__val:any", list: {"__key": PyArg { py_type: PyType { type_name: "any" }, name: "__key" }, "__val": PyArg { py_type: PyType { type_name: "any" }, name: "__val" }} }), return_type: None, is_constructor: false }, "__str__": MethodInfo { class_name: "PikaStdData_String", name: "__str__", arg_list: None, return_type: Some(PyType { type_name: "str" }), is_constructor: false }, "endwith": MethodInfo { class_name: "PikaStdData_String", name: "endwith", arg_list: Some(ArgList { py_arg_list: "suffix:str", list: {"suffix": PyArg { py_type: PyType { type_name: "str" }, name: "suffix" }} }), return_type: Some(PyType { type_name: "int" }), is_constructor: false }, "get": MethodInfo { class_name: "PikaStdData_String", name: "get", arg_list: None, return_type: Some(PyType { type_name: "str" }), is_constructor: false }, "isalnum": MethodInfo { class_name: "PikaStdData_String", name: "isalnum", arg_list: None, return_type: Some(PyType { type_name: "int" }), is_constructor: false }, "isalpha": MethodInfo { class_name: "PikaStdData_String", name: "isalpha", arg_list: None, return_type: Some(PyType { type_name: "int" }), is_constructor: false }, "isdigit": MethodInfo { class_name: "PikaStdData_String", name: "isdigit", arg_list: None, return_type: Some(PyType { type_name: "int" }), is_constructor: false }, "islower": MethodInfo { class_name: "PikaStdData_String", name: "islower", arg_list: None, return_type: Some(PyType { type_name: "int" }), is_constructor: false }, "isspace": MethodInfo { class_name: "PikaStdData_String", name: "isspace", arg_list: None, return_type: Some(PyType { type_name: "int" }), is_constructor: false }, "set": MethodInfo { class_name: "PikaStdData_String", name: "set", arg_list: Some(ArgList { py_arg_list: "s:str", list: {"s": PyArg { py_type: PyType { type_name: "str" }, name: "s" }} }), return_type: None, is_constructor: false }, "startwith": MethodInfo { class_name: "PikaStdData_String", name: "startwith", arg_list: Some(ArgList { py_arg_list: "prefix:str", list: {"prefix": PyArg { py_type: PyType { type_name: "str" }, name: "prefix" }} }), return_type: Some(PyType { type_name: "int" }), is_constructor: false }}, object_list: {}, import_list: {}, script_list: Script { content: "" }, is_package: false }, "PikaStdData_Utils": ClassInfo { this_class_name: "PikaStdData_Utils", this_class_name_without_file: "Utils", super_class_name: "TinyObj", method_list: {"int_to_bytes": MethodInfo { class_name: "PikaStdData_Utils", name: "int_to_bytes", arg_list: Some(ArgList { py_arg_list: "val:int", list: {"val": PyArg { py_type: PyType { type_name: "int" }, name: "val" }} }), return_type: Some(PyType { type_name: "bytes" }), is_constructor: false }}, object_list: {}, import_list: {}, script_list: Script { content: "" }, is_package: false }, "PikaStdLib": ClassInfo { this_class_name: "PikaStdLib", this_class_name_without_file: "PikaStdLib", super_class_name: "TinyObj", method_list: {"MemChecker": MethodInfo { class_name: "PikaStdLib", name: "MemChecker", arg_list: None, return_type: Some(PyType { type_name: "any" }), is_constructor: true }, "PikaObj": MethodInfo { class_name: "PikaStdLib", name: "PikaObj", arg_list: None, return_type: Some(PyType { type_name: "any" }), is_constructor: true }, "RangeObj": MethodInfo { class_name: "PikaStdLib", name: "RangeObj", arg_list: None, return_type: Some(PyType { type_name: "any" }), is_constructor: true }, "StringObj": MethodInfo { class_name: "PikaStdLib", name: "StringObj", arg_list: None, return_type: Some(PyType { type_name: "any" }), is_constructor: true }, "SysObj": MethodInfo { class_name: "PikaStdLib", name: "SysObj", arg_list: None, return_type: Some(PyType { type_name: "any" }), is_constructor: true }}, object_list: {}, import_list: {}, script_list: Script { content: "" }, is_package: true }, "PikaStdLib_MemChecker": ClassInfo { this_class_name: "PikaStdLib_MemChecker", this_class_name_without_file: "MemChecker", super_class_name: "TinyObj", method_list: {"getMax": MethodInfo { class_name: "PikaStdLib_MemChecker", name: "getMax", arg_list: None, return_type: Some(PyType { type_name: "float" }), is_constructor: false }, "getNow": MethodInfo { class_name: "PikaStdLib_MemChecker", name: "getNow", arg_list: None, return_type: Some(PyType { type_name: "float" }), is_constructor: false }, "max": MethodInfo { class_name: "PikaStdLib_MemChecker", name: "max", arg_list: None, return_type: None, is_constructor: false }, "now": MethodInfo { class_name: "PikaStdLib_MemChecker", name: "now", arg_list: None, return_type: None, is_constructor: false }, "resetMax": MethodInfo { class_name: "PikaStdLib_MemChecker", name: "resetMax", arg_list: None, return_type: None, is_constructor: false }}, object_list: {}, import_list: {}, script_list: Script { content: "" }, is_package: false }, "PikaStdLib_PikaObj": ClassInfo { this_class_name: "PikaStdLib_PikaObj", this_class_name_without_file: "PikaObj", super_class_name: "TinyObj", method_list: {}, object_list: {}, import_list: {}, script_list: Script { content: "" }, is_package: false }, "PikaStdLib_RangeObj": ClassInfo { this_class_name: "PikaStdLib_RangeObj", this_class_name_without_file: "RangeObj", super_class_name: "TinyObj", method_list: {"__next__": MethodInfo { class_name: "PikaStdLib_RangeObj", name: "__next__", arg_list: None, return_type: Some(PyType { type_name: "any" }), is_constructor: false }}, object_list: {}, import_list: {}, script_list: Script { content: "" }, is_package: false }, "PikaStdLib_StringObj": ClassInfo { this_class_name: "PikaStdLib_StringObj", this_class_name_without_file: "StringObj", super_class_name: "TinyObj", method_list: {"__next__": MethodInfo { class_name: "PikaStdLib_StringObj", name: "__next__", arg_list: None, return_type: Some(PyType { type_name: "any" }), is_constructor: false }}, object_list: {}, import_list: {}, script_list: Script { content: "" }, is_package: false }, "PikaStdLib_SysObj": ClassInfo { this_class_name: "PikaStdLib_SysObj", this_class_name_without_file: "SysObj", super_class_name: "BaseObj", method_list: {"__getitem__": MethodInfo { class_name: "PikaStdLib_SysObj", name: "__getitem__", arg_list: Some(ArgList { py_arg_list: "obj:any,key:any", list: {"key": PyArg { py_type: PyType { type_name: "any" }, name: "key" }, "obj": PyArg { py_type: PyType { type_name: "any" }, name: "obj" }} }), return_type: Some(PyType { type_name: "any" }), is_constructor: false }, "__setitem__": MethodInfo { class_name: "PikaStdLib_SysObj", name: "__setitem__", arg_list: Some(ArgList { py_arg_list: "obj:any,key:any,val:any,obj_str:str", list: {"key": PyArg { py_type: PyType { type_name: "any" }, name: "key" }, "obj": PyArg { py_type: PyType { type_name: "any" }, name: "obj" }, "obj_str": PyArg { py_type: PyType { type_name: "str" }, name: "obj_str" }, "val": PyArg { py_type: PyType { type_name: "any" }, name: "val" }} }), return_type: None, is_constructor: false }, "__slice__": MethodInfo { class_name: "PikaStdLib_SysObj", name: "__slice__", arg_list: Some(ArgList { py_arg_list: "obj:any,start:any,end:any,step:int", list: {"end": PyArg { py_type: PyType { type_name: "any" }, name: "end" }, "obj": PyArg { py_type: PyType { type_name: "any" }, name: "obj" }, "start": PyArg { py_type: PyType { type_name: "any" }, name: "start" }, "step": PyArg { py_type: PyType { type_name: "int" }, name: "step" }} }), return_type: Some(PyType { type_name: "any" }), is_constructor: false }, "bytes": MethodInfo { class_name: "PikaStdLib_SysObj", name: "bytes", arg_list: Some(ArgList { py_arg_list: "val:any", list: {"val": PyArg { py_type: PyType { type_name: "any" }, name: "val" }} }), return_type: Some(PyType { type_name: "bytes" }), is_constructor: false }, "cformat": MethodInfo { class_name: "PikaStdLib_SysObj", name: "cformat", arg_list: Some(ArgList { py_arg_list: "fmt:str,*var", list: {"fmt": PyArg { py_type: PyType { type_name: "str" }, name: "fmt" }, "var": PyArg { py_type: PyType { type_name: "@tupleVarPar" }, name: "var" }} }), return_type: Some(PyType { type_name: "str" }), is_constructor: false }, "chr": MethodInfo { class_name: "PikaStdLib_SysObj", name: "chr", arg_list: Some(ArgList { py_arg_list: "val:int", list: {"val": PyArg { py_type: PyType { type_name: "int" }, name: "val" }} }), return_type: Some(PyType { type_name: "str" }), is_constructor: false }, "dict": MethodInfo { class_name: "PikaStdLib_SysObj", name: "dict", arg_list: None, return_type: Some(PyType { type_name: "any" }), is_constructor: false }, "float": MethodInfo { class_name: "PikaStdLib_SysObj", name: "float", arg_list: Some(ArgList { py_arg_list: "arg:any", list: {"arg": PyArg { py_type: PyType { type_name: "any" }, name: "arg" }} }), return_type: Some(PyType { type_name: "float" }), is_constructor: false }, "hex": MethodInfo { class_name: "PikaStdLib_SysObj", name: "hex", arg_list: Some(ArgList { py_arg_list: "val:int", list: {"val": PyArg { py_type: PyType { type_name: "int" }, name: "val" }} }), return_type: Some(PyType { type_name: "str" }), is_constructor: false }, "id": MethodInfo { class_name: "PikaStdLib_SysObj", name: "id", arg_list: Some(ArgList { py_arg_list: "obj:any", list: {"obj": PyArg { py_type: PyType { type_name: "any" }, name: "obj" }} }), return_type: Some(PyType { type_name: "int" }), is_constructor: false }, "int": MethodInfo { class_name: "PikaStdLib_SysObj", name: "int", arg_list: Some(ArgList { py_arg_list: "arg:any", list: {"arg": PyArg { py_type: PyType { type_name: "any" }, name: "arg" }} }), return_type: Some(PyType { type_name: "int" }), is_constructor: false }, "iter": MethodInfo { class_name: "PikaStdLib_SysObj", name: "iter", arg_list: Some(ArgList { py_arg_list: "arg:any", list: {"arg": PyArg { py_type: PyType { type_name: "any" }, name: "arg" }} }), return_type: Some(PyType { type_name: "any" }), is_constructor: false }, "len": MethodInfo { class_name: "PikaStdLib_SysObj", name: "len", arg_list: Some(ArgList { py_arg_list: "arg:any", list: {"arg": PyArg { py_type: PyType { type_name: "any" }, name: "arg" }} }), return_type: Some(PyType { type_name: "int" }), is_constructor: false }, "list": MethodInfo { class_name: "PikaStdLib_SysObj", name: "list", arg_list: None, return_type: Some(PyType { type_name: "any" }), is_constructor: false }, "ord": MethodInfo { class_name: "PikaStdLib_SysObj", name: "ord", arg_list: Some(ArgList { py_arg_list: "val:str", list: {"val": PyArg { py_type: PyType { type_name: "str" }, name: "val" }} }), return_type: Some(PyType { type_name: "int" }), is_constructor: false }, "print": MethodInfo { class_name: "PikaStdLib_SysObj", name: "print", arg_list: Some(ArgList { py_arg_list: "*val", list: {"val": PyArg { py_type: PyType { type_name: "@tupleVarPar" }, name: "val" }} }), return_type: None, is_constructor: false }, "printNoEnd": MethodInfo { class_name: "PikaStdLib_SysObj", name: "printNoEnd", arg_list: Some(ArgList { py_arg_list: "val:any", list: {"val": PyArg { py_type: PyType { type_name: "any" }, name: "val" }} }), return_type: None, is_constructor: false }, "range": MethodInfo { class_name: "PikaStdLib_SysObj", name: "range", arg_list: Some(ArgList { py_arg_list: "a1:int,a2:int", list: {"a1": PyArg { py_type: PyType { type_name: "int" }, name: "a1" }, "a2": PyArg { py_type: PyType { type_name: "int" }, name: "a2" }} }), return_type: Some(PyType { type_name: "any" }), is_constructor: false }, "remove": MethodInfo { class_name: "PikaStdLib_SysObj", name: "remove", arg_list: Some(ArgList { py_arg_list: "argPath:str", list: {"argPath": PyArg { py_type: PyType { type_name: "str" }, name: "argPath" }} }), return_type: None, is_constructor: false }, "str": MethodInfo { class_name: "PikaStdLib_SysObj", name: "str", arg_list: Some(ArgList { py_arg_list: "arg:any", list: {"arg": PyArg { py_type: PyType { type_name: "any" }, name: "arg" }} }), return_type: Some(PyType { type_name: "str" }), is_constructor: false }, "type": MethodInfo { class_name: "PikaStdLib_SysObj", name: "type", arg_list: Some(ArgList { py_arg_list: "arg:any", list: {"arg": PyArg { py_type: PyType { type_name: "any" }, name: "arg" }} }), return_type: None, is_constructor: false }}, object_list: {}, import_list: {}, script_list: Script { content: "" }, is_package: false }, "PikaStdTask": ClassInfo { this_class_name: "PikaStdTask", this_class_name_without_file: "PikaStdTask", super_class_name: "TinyObj", method_list: {"Task": MethodInfo { class_name: "PikaStdTask", name: "Task", arg_list: None, return_type: Some(PyType { type_name: "any" }), is_constructor: true }}, object_list: {}, import_list: {}, script_list: Script { content: "" }, is_package: true }, "PikaStdTask_Task": ClassInfo { this_class_name: "PikaStdTask_Task", this_class_name_without_file: "Task", super_class_name: "TinyObj", method_list: {"__init__": MethodInfo { class_name: "PikaStdTask_Task", name: "__init__", arg_list: None, return_type: None, is_constructor: false }, "call_always": MethodInfo { class_name: "PikaStdTask_Task", name: "call_always", arg_list: Some(ArgList { py_arg_list: "fun_todo:any", list: {"fun_todo": PyArg { py_type: PyType { type_name: "any" }, name: "fun_todo" }} }), return_type: None, is_constructor: false }, "call_period_ms": MethodInfo { class_name: "PikaStdTask_Task", name: "call_period_ms", arg_list: Some(ArgList { py_arg_list: "fun_todo:any,period_ms:int", list: {"fun_todo": PyArg { py_type: PyType { type_name: "any" }, name: "fun_todo" }, "period_ms": PyArg { py_type: PyType { type_name: "int" }, name: "period_ms" }} }), return_type: None, is_constructor: false }, "call_when": MethodInfo { class_name: "PikaStdTask_Task", name: "call_when", arg_list: Some(ArgList { py_arg_list: "fun_todo:any,fun_when:any", list: {"fun_todo": PyArg { py_type: PyType { type_name: "any" }, name: "fun_todo" }, "fun_when": PyArg { py_type: PyType { type_name: "any" }, name: "fun_when" }} }), return_type: None, is_constructor: false }, "platformGetTick": MethodInfo { class_name: "PikaStdTask_Task", name: "platformGetTick", arg_list: None, return_type: None, is_constructor: false }, "run_forever": MethodInfo { class_name: "PikaStdTask_Task", name: "run_forever", arg_list: None, return_type: None, is_constructor: false }, "run_once": MethodInfo { class_name: "PikaStdTask_Task", name: "run_once", arg_list: None, return_type: None, is_constructor: false }, "run_until_ms": MethodInfo { class_name: "PikaStdTask_Task", name: "run_until_ms", arg_list: Some(ArgList { py_arg_list: "until_ms:int", list: {"until_ms": PyArg { py_type: PyType { type_name: "int" }, name: "until_ms" }} }), return_type: None, is_constructor: false }}, object_list: {"calls": ObjectInfo { class_name: "PikaStdTask_Task", name: "calls", import_class_name: "PikaStdData_List" }}, import_list: {}, script_list: Script { content: "" }, is_package: false }}, class_now_name: Some("PikaDebug_Debuger"), package_now_name: Some("PikaDebug"), compiled_list: ["main", "PikaStdLib", "PikaStdLib", "PikaStdTask", "PikaStdData", "PikaStdData", "PikaDebug"] } \ No newline at end of file +Compiler { dist_path: "pikascript-api/", source_path: "", class_list: {"PikaDebug": ClassInfo { this_class_name: "PikaDebug", this_class_name_without_file: "PikaDebug", super_class_name: "TinyObj", method_list: {"Debuger": MethodInfo { class_name: "PikaDebug", name: "Debuger", arg_list: None, return_type: Some(PyType { type_name: "any" }), is_constructor: true }}, object_list: {}, import_list: {}, script_list: Script { content: "" }, is_package: true }, "PikaDebug_Debuger": ClassInfo { this_class_name: "PikaDebug_Debuger", this_class_name_without_file: "Debuger", super_class_name: "TinyObj", method_list: {"__init__": MethodInfo { class_name: "PikaDebug_Debuger", name: "__init__", arg_list: None, return_type: None, is_constructor: false }, "set_trace": MethodInfo { class_name: "PikaDebug_Debuger", name: "set_trace", arg_list: None, return_type: None, is_constructor: false }}, object_list: {}, import_list: {}, script_list: Script { content: "" }, is_package: false }, "PikaMain": ClassInfo { this_class_name: "PikaMain", this_class_name_without_file: "PikaMain", super_class_name: "PikaStdLib_SysObj", method_list: {}, object_list: {"PikaStdLib": ObjectInfo { class_name: "PikaMain", name: "PikaStdLib", import_class_name: "PikaStdLib" }}, import_list: {}, script_list: Script { content: "@BEGIN@import PikaStdLib@END@@BEGIN@@END@@BEGIN@print('hello PikaScript!')@END@@BEGIN@@END@@BEGIN@mem = PikaStdLib.MemChecker()@END@@BEGIN@print('mem used max:')@END@@BEGIN@mem.max()@END@@BEGIN@@END@" }, is_package: false }, "PikaStdData": ClassInfo { this_class_name: "PikaStdData", this_class_name_without_file: "PikaStdData", super_class_name: "TinyObj", method_list: {"ByteArray": MethodInfo { class_name: "PikaStdData", name: "ByteArray", arg_list: None, return_type: Some(PyType { type_name: "any" }), is_constructor: true }, "Dict": MethodInfo { class_name: "PikaStdData", name: "Dict", arg_list: None, return_type: Some(PyType { type_name: "any" }), is_constructor: true }, "FILEIO": MethodInfo { class_name: "PikaStdData", name: "FILEIO", arg_list: None, return_type: Some(PyType { type_name: "any" }), is_constructor: true }, "List": MethodInfo { class_name: "PikaStdData", name: "List", arg_list: None, return_type: Some(PyType { type_name: "any" }), is_constructor: true }, "String": MethodInfo { class_name: "PikaStdData", name: "String", arg_list: None, return_type: Some(PyType { type_name: "any" }), is_constructor: true }, "Tuple": MethodInfo { class_name: "PikaStdData", name: "Tuple", arg_list: None, return_type: Some(PyType { type_name: "any" }), is_constructor: true }, "Utils": MethodInfo { class_name: "PikaStdData", name: "Utils", arg_list: None, return_type: Some(PyType { type_name: "any" }), is_constructor: true }, "dict_keys": MethodInfo { class_name: "PikaStdData", name: "dict_keys", arg_list: None, return_type: Some(PyType { type_name: "any" }), is_constructor: true }}, object_list: {}, import_list: {}, script_list: Script { content: "" }, is_package: true }, "PikaStdData_ByteArray": ClassInfo { this_class_name: "PikaStdData_ByteArray", this_class_name_without_file: "ByteArray", super_class_name: "TinyObj", method_list: {"__getitem__": MethodInfo { class_name: "PikaStdData_ByteArray", name: "__getitem__", arg_list: Some(ArgList { py_arg_list: "__key:int", list: [PyArg { py_type: PyType { type_name: "int" }, name: "__key" }] }), return_type: Some(PyType { type_name: "int" }), is_constructor: false }, "__init__": MethodInfo { class_name: "PikaStdData_ByteArray", name: "__init__", arg_list: Some(ArgList { py_arg_list: "bytes:any", list: [PyArg { py_type: PyType { type_name: "any" }, name: "bytes" }] }), return_type: None, is_constructor: false }, "__iter__": MethodInfo { class_name: "PikaStdData_ByteArray", name: "__iter__", arg_list: None, return_type: Some(PyType { type_name: "any" }), is_constructor: false }, "__next__": MethodInfo { class_name: "PikaStdData_ByteArray", name: "__next__", arg_list: None, return_type: Some(PyType { type_name: "any" }), is_constructor: false }, "__setitem__": MethodInfo { class_name: "PikaStdData_ByteArray", name: "__setitem__", arg_list: Some(ArgList { py_arg_list: "__key:int,__val:int", list: [PyArg { py_type: PyType { type_name: "int" }, name: "__key" }, PyArg { py_type: PyType { type_name: "int" }, name: "__val" }] }), return_type: None, is_constructor: false }, "__str__": MethodInfo { class_name: "PikaStdData_ByteArray", name: "__str__", arg_list: None, return_type: Some(PyType { type_name: "str" }), is_constructor: false }, "decode": MethodInfo { class_name: "PikaStdData_ByteArray", name: "decode", arg_list: None, return_type: Some(PyType { type_name: "str" }), is_constructor: false }}, object_list: {}, import_list: {}, script_list: Script { content: "" }, is_package: false }, "PikaStdData_Dict": ClassInfo { this_class_name: "PikaStdData_Dict", this_class_name_without_file: "Dict", super_class_name: "TinyObj", method_list: {"__del__": MethodInfo { class_name: "PikaStdData_Dict", name: "__del__", arg_list: None, return_type: None, is_constructor: false }, "__getitem__": MethodInfo { class_name: "PikaStdData_Dict", name: "__getitem__", arg_list: Some(ArgList { py_arg_list: "__key:any", list: [PyArg { py_type: PyType { type_name: "any" }, name: "__key" }] }), return_type: Some(PyType { type_name: "any" }), is_constructor: false }, "__init__": MethodInfo { class_name: "PikaStdData_Dict", name: "__init__", arg_list: None, return_type: None, is_constructor: false }, "__iter__": MethodInfo { class_name: "PikaStdData_Dict", name: "__iter__", arg_list: None, return_type: Some(PyType { type_name: "any" }), is_constructor: false }, "__len__": MethodInfo { class_name: "PikaStdData_Dict", name: "__len__", arg_list: None, return_type: Some(PyType { type_name: "int" }), is_constructor: false }, "__next__": MethodInfo { class_name: "PikaStdData_Dict", name: "__next__", arg_list: None, return_type: Some(PyType { type_name: "any" }), is_constructor: false }, "__setitem__": MethodInfo { class_name: "PikaStdData_Dict", name: "__setitem__", arg_list: Some(ArgList { py_arg_list: "__key:any,__val:any", list: [PyArg { py_type: PyType { type_name: "any" }, name: "__key" }, PyArg { py_type: PyType { type_name: "any" }, name: "__val" }] }), return_type: None, is_constructor: false }, "__str__": MethodInfo { class_name: "PikaStdData_Dict", name: "__str__", arg_list: None, return_type: Some(PyType { type_name: "str" }), is_constructor: false }, "get": MethodInfo { class_name: "PikaStdData_Dict", name: "get", arg_list: Some(ArgList { py_arg_list: "key:str", list: [PyArg { py_type: PyType { type_name: "str" }, name: "key" }] }), return_type: Some(PyType { type_name: "any" }), is_constructor: false }, "keys": MethodInfo { class_name: "PikaStdData_Dict", name: "keys", arg_list: None, return_type: Some(PyType { type_name: "dict_keys" }), is_constructor: false }, "remove": MethodInfo { class_name: "PikaStdData_Dict", name: "remove", arg_list: Some(ArgList { py_arg_list: "key:str", list: [PyArg { py_type: PyType { type_name: "str" }, name: "key" }] }), return_type: None, is_constructor: false }, "set": MethodInfo { class_name: "PikaStdData_Dict", name: "set", arg_list: Some(ArgList { py_arg_list: "key:str,arg:any", list: [PyArg { py_type: PyType { type_name: "str" }, name: "key" }, PyArg { py_type: PyType { type_name: "any" }, name: "arg" }] }), return_type: None, is_constructor: false }}, object_list: {}, import_list: {}, script_list: Script { content: "" }, is_package: false }, "PikaStdData_FILEIO": ClassInfo { this_class_name: "PikaStdData_FILEIO", this_class_name_without_file: "FILEIO", super_class_name: "TinyObj", method_list: {"close": MethodInfo { class_name: "PikaStdData_FILEIO", name: "close", arg_list: None, return_type: None, is_constructor: false }, "init": MethodInfo { class_name: "PikaStdData_FILEIO", name: "init", arg_list: Some(ArgList { py_arg_list: "path:str,mode:str", list: [PyArg { py_type: PyType { type_name: "str" }, name: "path" }, PyArg { py_type: PyType { type_name: "str" }, name: "mode" }] }), return_type: None, is_constructor: false }, "read": MethodInfo { class_name: "PikaStdData_FILEIO", name: "read", arg_list: Some(ArgList { py_arg_list: "size:int", list: [PyArg { py_type: PyType { type_name: "int" }, name: "size" }] }), return_type: Some(PyType { type_name: "any" }), is_constructor: false }, "readline": MethodInfo { class_name: "PikaStdData_FILEIO", name: "readline", arg_list: None, return_type: Some(PyType { type_name: "str" }), is_constructor: false }, "readlines": MethodInfo { class_name: "PikaStdData_FILEIO", name: "readlines", arg_list: None, return_type: Some(PyType { type_name: "List" }), is_constructor: false }, "seek": MethodInfo { class_name: "PikaStdData_FILEIO", name: "seek", arg_list: Some(ArgList { py_arg_list: "offset:int,*fromwhere", list: [PyArg { py_type: PyType { type_name: "int" }, name: "offset" }, PyArg { py_type: PyType { type_name: "@tupleVarPar" }, name: "fromwhere" }] }), return_type: Some(PyType { type_name: "int" }), is_constructor: false }, "tell": MethodInfo { class_name: "PikaStdData_FILEIO", name: "tell", arg_list: None, return_type: Some(PyType { type_name: "int" }), is_constructor: false }, "write": MethodInfo { class_name: "PikaStdData_FILEIO", name: "write", arg_list: Some(ArgList { py_arg_list: "s:any", list: [PyArg { py_type: PyType { type_name: "any" }, name: "s" }] }), return_type: Some(PyType { type_name: "int" }), is_constructor: false }, "writelines": MethodInfo { class_name: "PikaStdData_FILEIO", name: "writelines", arg_list: Some(ArgList { py_arg_list: "lines:List", list: [PyArg { py_type: PyType { type_name: "List" }, name: "lines" }] }), return_type: None, is_constructor: false }}, object_list: {}, import_list: {}, script_list: Script { content: "" }, is_package: false }, "PikaStdData_List": ClassInfo { this_class_name: "PikaStdData_List", this_class_name_without_file: "List", super_class_name: "PikaStdData_Tuple", method_list: {"__init__": MethodInfo { class_name: "PikaStdData_List", name: "__init__", arg_list: None, return_type: None, is_constructor: false }, "__setitem__": MethodInfo { class_name: "PikaStdData_List", name: "__setitem__", arg_list: Some(ArgList { py_arg_list: "__key:any,__val:any", list: [PyArg { py_type: PyType { type_name: "any" }, name: "__key" }, PyArg { py_type: PyType { type_name: "any" }, name: "__val" }] }), return_type: None, is_constructor: false }, "__str__": MethodInfo { class_name: "PikaStdData_List", name: "__str__", arg_list: None, return_type: Some(PyType { type_name: "str" }), is_constructor: false }, "append": MethodInfo { class_name: "PikaStdData_List", name: "append", arg_list: Some(ArgList { py_arg_list: "arg:any", list: [PyArg { py_type: PyType { type_name: "any" }, name: "arg" }] }), return_type: None, is_constructor: false }, "set": MethodInfo { class_name: "PikaStdData_List", name: "set", arg_list: Some(ArgList { py_arg_list: "i:int,arg:any", list: [PyArg { py_type: PyType { type_name: "int" }, name: "i" }, PyArg { py_type: PyType { type_name: "any" }, name: "arg" }] }), return_type: None, is_constructor: false }}, object_list: {}, import_list: {}, script_list: Script { content: "" }, is_package: false }, "PikaStdData_String": ClassInfo { this_class_name: "PikaStdData_String", this_class_name_without_file: "String", super_class_name: "TinyObj", method_list: {"__getitem__": MethodInfo { class_name: "PikaStdData_String", name: "__getitem__", arg_list: Some(ArgList { py_arg_list: "__key:any", list: [PyArg { py_type: PyType { type_name: "any" }, name: "__key" }] }), return_type: Some(PyType { type_name: "any" }), is_constructor: false }, "__init__": MethodInfo { class_name: "PikaStdData_String", name: "__init__", arg_list: Some(ArgList { py_arg_list: "s:str", list: [PyArg { py_type: PyType { type_name: "str" }, name: "s" }] }), return_type: None, is_constructor: false }, "__iter__": MethodInfo { class_name: "PikaStdData_String", name: "__iter__", arg_list: None, return_type: Some(PyType { type_name: "any" }), is_constructor: false }, "__len__": MethodInfo { class_name: "PikaStdData_String", name: "__len__", arg_list: None, return_type: Some(PyType { type_name: "int" }), is_constructor: false }, "__next__": MethodInfo { class_name: "PikaStdData_String", name: "__next__", arg_list: None, return_type: Some(PyType { type_name: "any" }), is_constructor: false }, "__setitem__": MethodInfo { class_name: "PikaStdData_String", name: "__setitem__", arg_list: Some(ArgList { py_arg_list: "__key:any,__val:any", list: [PyArg { py_type: PyType { type_name: "any" }, name: "__key" }, PyArg { py_type: PyType { type_name: "any" }, name: "__val" }] }), return_type: None, is_constructor: false }, "__str__": MethodInfo { class_name: "PikaStdData_String", name: "__str__", arg_list: None, return_type: Some(PyType { type_name: "str" }), is_constructor: false }, "encode": MethodInfo { class_name: "PikaStdData_String", name: "encode", arg_list: None, return_type: Some(PyType { type_name: "bytes" }), is_constructor: false }, "endwith": MethodInfo { class_name: "PikaStdData_String", name: "endwith", arg_list: Some(ArgList { py_arg_list: "suffix:str", list: [PyArg { py_type: PyType { type_name: "str" }, name: "suffix" }] }), return_type: Some(PyType { type_name: "int" }), is_constructor: false }, "get": MethodInfo { class_name: "PikaStdData_String", name: "get", arg_list: None, return_type: Some(PyType { type_name: "str" }), is_constructor: false }, "isalnum": MethodInfo { class_name: "PikaStdData_String", name: "isalnum", arg_list: None, return_type: Some(PyType { type_name: "int" }), is_constructor: false }, "isalpha": MethodInfo { class_name: "PikaStdData_String", name: "isalpha", arg_list: None, return_type: Some(PyType { type_name: "int" }), is_constructor: false }, "isdigit": MethodInfo { class_name: "PikaStdData_String", name: "isdigit", arg_list: None, return_type: Some(PyType { type_name: "int" }), is_constructor: false }, "islower": MethodInfo { class_name: "PikaStdData_String", name: "islower", arg_list: None, return_type: Some(PyType { type_name: "int" }), is_constructor: false }, "isspace": MethodInfo { class_name: "PikaStdData_String", name: "isspace", arg_list: None, return_type: Some(PyType { type_name: "int" }), is_constructor: false }, "replace": MethodInfo { class_name: "PikaStdData_String", name: "replace", arg_list: Some(ArgList { py_arg_list: "old:str,new:str", list: [PyArg { py_type: PyType { type_name: "str" }, name: "old" }, PyArg { py_type: PyType { type_name: "str" }, name: "new" }] }), return_type: Some(PyType { type_name: "str" }), is_constructor: false }, "set": MethodInfo { class_name: "PikaStdData_String", name: "set", arg_list: Some(ArgList { py_arg_list: "s:str", list: [PyArg { py_type: PyType { type_name: "str" }, name: "s" }] }), return_type: None, is_constructor: false }, "split": MethodInfo { class_name: "PikaStdData_String", name: "split", arg_list: Some(ArgList { py_arg_list: "s:str", list: [PyArg { py_type: PyType { type_name: "str" }, name: "s" }] }), return_type: Some(PyType { type_name: "List" }), is_constructor: false }, "startwith": MethodInfo { class_name: "PikaStdData_String", name: "startwith", arg_list: Some(ArgList { py_arg_list: "prefix:str", list: [PyArg { py_type: PyType { type_name: "str" }, name: "prefix" }] }), return_type: Some(PyType { type_name: "int" }), is_constructor: false }, "strip": MethodInfo { class_name: "PikaStdData_String", name: "strip", arg_list: None, return_type: Some(PyType { type_name: "str" }), is_constructor: false }}, object_list: {}, import_list: {}, script_list: Script { content: "" }, is_package: false }, "PikaStdData_Tuple": ClassInfo { this_class_name: "PikaStdData_Tuple", this_class_name_without_file: "Tuple", super_class_name: "TinyObj", method_list: {"__del__": MethodInfo { class_name: "PikaStdData_Tuple", name: "__del__", arg_list: None, return_type: None, is_constructor: false }, "__getitem__": MethodInfo { class_name: "PikaStdData_Tuple", name: "__getitem__", arg_list: Some(ArgList { py_arg_list: "__key:any", list: [PyArg { py_type: PyType { type_name: "any" }, name: "__key" }] }), return_type: Some(PyType { type_name: "any" }), is_constructor: false }, "__init__": MethodInfo { class_name: "PikaStdData_Tuple", name: "__init__", arg_list: None, return_type: None, is_constructor: false }, "__iter__": MethodInfo { class_name: "PikaStdData_Tuple", name: "__iter__", arg_list: None, return_type: Some(PyType { type_name: "any" }), is_constructor: false }, "__len__": MethodInfo { class_name: "PikaStdData_Tuple", name: "__len__", arg_list: None, return_type: Some(PyType { type_name: "int" }), is_constructor: false }, "__next__": MethodInfo { class_name: "PikaStdData_Tuple", name: "__next__", arg_list: None, return_type: Some(PyType { type_name: "any" }), is_constructor: false }, "__str__": MethodInfo { class_name: "PikaStdData_Tuple", name: "__str__", arg_list: None, return_type: Some(PyType { type_name: "str" }), is_constructor: false }, "get": MethodInfo { class_name: "PikaStdData_Tuple", name: "get", arg_list: Some(ArgList { py_arg_list: "i:int", list: [PyArg { py_type: PyType { type_name: "int" }, name: "i" }] }), return_type: Some(PyType { type_name: "any" }), is_constructor: false }, "len": MethodInfo { class_name: "PikaStdData_Tuple", name: "len", arg_list: None, return_type: Some(PyType { type_name: "int" }), is_constructor: false }}, object_list: {}, import_list: {}, script_list: Script { content: "" }, is_package: false }, "PikaStdData_Utils": ClassInfo { this_class_name: "PikaStdData_Utils", this_class_name_without_file: "Utils", super_class_name: "TinyObj", method_list: {"int_to_bytes": MethodInfo { class_name: "PikaStdData_Utils", name: "int_to_bytes", arg_list: Some(ArgList { py_arg_list: "val:int", list: [PyArg { py_type: PyType { type_name: "int" }, name: "val" }] }), return_type: Some(PyType { type_name: "bytes" }), is_constructor: false }}, object_list: {}, import_list: {}, script_list: Script { content: "" }, is_package: false }, "PikaStdData_dict_keys": ClassInfo { this_class_name: "PikaStdData_dict_keys", this_class_name_without_file: "dict_keys", super_class_name: "TinyObj", method_list: {"__iter__": MethodInfo { class_name: "PikaStdData_dict_keys", name: "__iter__", arg_list: None, return_type: Some(PyType { type_name: "any" }), is_constructor: false }, "__len__": MethodInfo { class_name: "PikaStdData_dict_keys", name: "__len__", arg_list: None, return_type: Some(PyType { type_name: "int" }), is_constructor: false }, "__next__": MethodInfo { class_name: "PikaStdData_dict_keys", name: "__next__", arg_list: None, return_type: Some(PyType { type_name: "any" }), is_constructor: false }, "__str__": MethodInfo { class_name: "PikaStdData_dict_keys", name: "__str__", arg_list: None, return_type: Some(PyType { type_name: "str" }), is_constructor: false }}, object_list: {}, import_list: {}, script_list: Script { content: "" }, is_package: false }, "PikaStdLib": ClassInfo { this_class_name: "PikaStdLib", this_class_name_without_file: "PikaStdLib", super_class_name: "TinyObj", method_list: {"MemChecker": MethodInfo { class_name: "PikaStdLib", name: "MemChecker", arg_list: None, return_type: Some(PyType { type_name: "any" }), is_constructor: true }, "PikaObj": MethodInfo { class_name: "PikaStdLib", name: "PikaObj", arg_list: None, return_type: Some(PyType { type_name: "any" }), is_constructor: true }, "RangeObj": MethodInfo { class_name: "PikaStdLib", name: "RangeObj", arg_list: None, return_type: Some(PyType { type_name: "any" }), is_constructor: true }, "StringObj": MethodInfo { class_name: "PikaStdLib", name: "StringObj", arg_list: None, return_type: Some(PyType { type_name: "any" }), is_constructor: true }, "SysObj": MethodInfo { class_name: "PikaStdLib", name: "SysObj", arg_list: None, return_type: Some(PyType { type_name: "any" }), is_constructor: true }}, object_list: {}, import_list: {}, script_list: Script { content: "" }, is_package: true }, "PikaStdLib_MemChecker": ClassInfo { this_class_name: "PikaStdLib_MemChecker", this_class_name_without_file: "MemChecker", super_class_name: "TinyObj", method_list: {"getMax": MethodInfo { class_name: "PikaStdLib_MemChecker", name: "getMax", arg_list: None, return_type: Some(PyType { type_name: "float" }), is_constructor: false }, "getNow": MethodInfo { class_name: "PikaStdLib_MemChecker", name: "getNow", arg_list: None, return_type: Some(PyType { type_name: "float" }), is_constructor: false }, "max": MethodInfo { class_name: "PikaStdLib_MemChecker", name: "max", arg_list: None, return_type: None, is_constructor: false }, "now": MethodInfo { class_name: "PikaStdLib_MemChecker", name: "now", arg_list: None, return_type: None, is_constructor: false }, "resetMax": MethodInfo { class_name: "PikaStdLib_MemChecker", name: "resetMax", arg_list: None, return_type: None, is_constructor: false }}, object_list: {}, import_list: {}, script_list: Script { content: "" }, is_package: false }, "PikaStdLib_PikaObj": ClassInfo { this_class_name: "PikaStdLib_PikaObj", this_class_name_without_file: "PikaObj", super_class_name: "TinyObj", method_list: {}, object_list: {}, import_list: {}, script_list: Script { content: "" }, is_package: false }, "PikaStdLib_RangeObj": ClassInfo { this_class_name: "PikaStdLib_RangeObj", this_class_name_without_file: "RangeObj", super_class_name: "TinyObj", method_list: {"__next__": MethodInfo { class_name: "PikaStdLib_RangeObj", name: "__next__", arg_list: None, return_type: Some(PyType { type_name: "any" }), is_constructor: false }}, object_list: {}, import_list: {}, script_list: Script { content: "" }, is_package: false }, "PikaStdLib_StringObj": ClassInfo { this_class_name: "PikaStdLib_StringObj", this_class_name_without_file: "StringObj", super_class_name: "TinyObj", method_list: {"__next__": MethodInfo { class_name: "PikaStdLib_StringObj", name: "__next__", arg_list: None, return_type: Some(PyType { type_name: "any" }), is_constructor: false }}, object_list: {}, import_list: {}, script_list: Script { content: "" }, is_package: false }, "PikaStdLib_SysObj": ClassInfo { this_class_name: "PikaStdLib_SysObj", this_class_name_without_file: "SysObj", super_class_name: "TinyObj", method_list: {"__getitem__": MethodInfo { class_name: "PikaStdLib_SysObj", name: "__getitem__", arg_list: Some(ArgList { py_arg_list: "obj:any,key:any", list: [PyArg { py_type: PyType { type_name: "any" }, name: "obj" }, PyArg { py_type: PyType { type_name: "any" }, name: "key" }] }), return_type: Some(PyType { type_name: "any" }), is_constructor: false }, "__setitem__": MethodInfo { class_name: "PikaStdLib_SysObj", name: "__setitem__", arg_list: Some(ArgList { py_arg_list: "obj:any,key:any,val:any", list: [PyArg { py_type: PyType { type_name: "any" }, name: "obj" }, PyArg { py_type: PyType { type_name: "any" }, name: "key" }, PyArg { py_type: PyType { type_name: "any" }, name: "val" }] }), return_type: Some(PyType { type_name: "any" }), is_constructor: false }, "__slice__": MethodInfo { class_name: "PikaStdLib_SysObj", name: "__slice__", arg_list: Some(ArgList { py_arg_list: "obj:any,start:any,end:any,step:int", list: [PyArg { py_type: PyType { type_name: "any" }, name: "obj" }, PyArg { py_type: PyType { type_name: "any" }, name: "start" }, PyArg { py_type: PyType { type_name: "any" }, name: "end" }, PyArg { py_type: PyType { type_name: "int" }, name: "step" }] }), return_type: Some(PyType { type_name: "any" }), is_constructor: false }, "bytes": MethodInfo { class_name: "PikaStdLib_SysObj", name: "bytes", arg_list: Some(ArgList { py_arg_list: "val:any", list: [PyArg { py_type: PyType { type_name: "any" }, name: "val" }] }), return_type: Some(PyType { type_name: "bytes" }), is_constructor: false }, "cformat": MethodInfo { class_name: "PikaStdLib_SysObj", name: "cformat", arg_list: Some(ArgList { py_arg_list: "fmt:str,*var", list: [PyArg { py_type: PyType { type_name: "str" }, name: "fmt" }, PyArg { py_type: PyType { type_name: "@tupleVarPar" }, name: "var" }] }), return_type: Some(PyType { type_name: "str" }), is_constructor: false }, "chr": MethodInfo { class_name: "PikaStdLib_SysObj", name: "chr", arg_list: Some(ArgList { py_arg_list: "val:int", list: [PyArg { py_type: PyType { type_name: "int" }, name: "val" }] }), return_type: Some(PyType { type_name: "str" }), is_constructor: false }, "dict": MethodInfo { class_name: "PikaStdLib_SysObj", name: "dict", arg_list: None, return_type: Some(PyType { type_name: "any" }), is_constructor: false }, "float": MethodInfo { class_name: "PikaStdLib_SysObj", name: "float", arg_list: Some(ArgList { py_arg_list: "arg:any", list: [PyArg { py_type: PyType { type_name: "any" }, name: "arg" }] }), return_type: Some(PyType { type_name: "float" }), is_constructor: false }, "hex": MethodInfo { class_name: "PikaStdLib_SysObj", name: "hex", arg_list: Some(ArgList { py_arg_list: "val:int", list: [PyArg { py_type: PyType { type_name: "int" }, name: "val" }] }), return_type: Some(PyType { type_name: "str" }), is_constructor: false }, "id": MethodInfo { class_name: "PikaStdLib_SysObj", name: "id", arg_list: Some(ArgList { py_arg_list: "obj:any", list: [PyArg { py_type: PyType { type_name: "any" }, name: "obj" }] }), return_type: Some(PyType { type_name: "int" }), is_constructor: false }, "int": MethodInfo { class_name: "PikaStdLib_SysObj", name: "int", arg_list: Some(ArgList { py_arg_list: "arg:any", list: [PyArg { py_type: PyType { type_name: "any" }, name: "arg" }] }), return_type: Some(PyType { type_name: "int" }), is_constructor: false }, "iter": MethodInfo { class_name: "PikaStdLib_SysObj", name: "iter", arg_list: Some(ArgList { py_arg_list: "arg:any", list: [PyArg { py_type: PyType { type_name: "any" }, name: "arg" }] }), return_type: Some(PyType { type_name: "any" }), is_constructor: false }, "len": MethodInfo { class_name: "PikaStdLib_SysObj", name: "len", arg_list: Some(ArgList { py_arg_list: "arg:any", list: [PyArg { py_type: PyType { type_name: "any" }, name: "arg" }] }), return_type: Some(PyType { type_name: "int" }), is_constructor: false }, "list": MethodInfo { class_name: "PikaStdLib_SysObj", name: "list", arg_list: None, return_type: Some(PyType { type_name: "any" }), is_constructor: false }, "open": MethodInfo { class_name: "PikaStdLib_SysObj", name: "open", arg_list: Some(ArgList { py_arg_list: "path:str,mode:str", list: [PyArg { py_type: PyType { type_name: "str" }, name: "path" }, PyArg { py_type: PyType { type_name: "str" }, name: "mode" }] }), return_type: Some(PyType { type_name: "object" }), is_constructor: false }, "ord": MethodInfo { class_name: "PikaStdLib_SysObj", name: "ord", arg_list: Some(ArgList { py_arg_list: "val:str", list: [PyArg { py_type: PyType { type_name: "str" }, name: "val" }] }), return_type: Some(PyType { type_name: "int" }), is_constructor: false }, "print": MethodInfo { class_name: "PikaStdLib_SysObj", name: "print", arg_list: Some(ArgList { py_arg_list: "*val", list: [PyArg { py_type: PyType { type_name: "@tupleVarPar" }, name: "val" }] }), return_type: None, is_constructor: false }, "printNoEnd": MethodInfo { class_name: "PikaStdLib_SysObj", name: "printNoEnd", arg_list: Some(ArgList { py_arg_list: "val:any", list: [PyArg { py_type: PyType { type_name: "any" }, name: "val" }] }), return_type: None, is_constructor: false }, "range": MethodInfo { class_name: "PikaStdLib_SysObj", name: "range", arg_list: Some(ArgList { py_arg_list: "a1:int,a2:int", list: [PyArg { py_type: PyType { type_name: "int" }, name: "a1" }, PyArg { py_type: PyType { type_name: "int" }, name: "a2" }] }), return_type: Some(PyType { type_name: "any" }), is_constructor: false }, "remove": MethodInfo { class_name: "PikaStdLib_SysObj", name: "remove", arg_list: Some(ArgList { py_arg_list: "argPath:str", list: [PyArg { py_type: PyType { type_name: "str" }, name: "argPath" }] }), return_type: None, is_constructor: false }, "str": MethodInfo { class_name: "PikaStdLib_SysObj", name: "str", arg_list: Some(ArgList { py_arg_list: "arg:any", list: [PyArg { py_type: PyType { type_name: "any" }, name: "arg" }] }), return_type: Some(PyType { type_name: "str" }), is_constructor: false }, "type": MethodInfo { class_name: "PikaStdLib_SysObj", name: "type", arg_list: Some(ArgList { py_arg_list: "arg:any", list: [PyArg { py_type: PyType { type_name: "any" }, name: "arg" }] }), return_type: Some(PyType { type_name: "any" }), is_constructor: false }}, object_list: {}, import_list: {}, script_list: Script { content: "" }, is_package: false }, "PikaStdTask": ClassInfo { this_class_name: "PikaStdTask", this_class_name_without_file: "PikaStdTask", super_class_name: "TinyObj", method_list: {"Task": MethodInfo { class_name: "PikaStdTask", name: "Task", arg_list: None, return_type: Some(PyType { type_name: "any" }), is_constructor: true }}, object_list: {}, import_list: {}, script_list: Script { content: "" }, is_package: true }, "PikaStdTask_Task": ClassInfo { this_class_name: "PikaStdTask_Task", this_class_name_without_file: "Task", super_class_name: "PikaStdLib_SysObj", method_list: {"__init__": MethodInfo { class_name: "PikaStdTask_Task", name: "__init__", arg_list: None, return_type: None, is_constructor: false }, "call_always": MethodInfo { class_name: "PikaStdTask_Task", name: "call_always", arg_list: Some(ArgList { py_arg_list: "fun_todo:any", list: [PyArg { py_type: PyType { type_name: "any" }, name: "fun_todo" }] }), return_type: None, is_constructor: false }, "call_period_ms": MethodInfo { class_name: "PikaStdTask_Task", name: "call_period_ms", arg_list: Some(ArgList { py_arg_list: "fun_todo:any,period_ms:int", list: [PyArg { py_type: PyType { type_name: "any" }, name: "fun_todo" }, PyArg { py_type: PyType { type_name: "int" }, name: "period_ms" }] }), return_type: None, is_constructor: false }, "call_when": MethodInfo { class_name: "PikaStdTask_Task", name: "call_when", arg_list: Some(ArgList { py_arg_list: "fun_todo:any,fun_when:any", list: [PyArg { py_type: PyType { type_name: "any" }, name: "fun_todo" }, PyArg { py_type: PyType { type_name: "any" }, name: "fun_when" }] }), return_type: None, is_constructor: false }, "platformGetTick": MethodInfo { class_name: "PikaStdTask_Task", name: "platformGetTick", arg_list: None, return_type: None, is_constructor: false }, "run_forever": MethodInfo { class_name: "PikaStdTask_Task", name: "run_forever", arg_list: None, return_type: None, is_constructor: false }, "run_once": MethodInfo { class_name: "PikaStdTask_Task", name: "run_once", arg_list: None, return_type: None, is_constructor: false }, "run_until_ms": MethodInfo { class_name: "PikaStdTask_Task", name: "run_until_ms", arg_list: Some(ArgList { py_arg_list: "until_ms:int", list: [PyArg { py_type: PyType { type_name: "int" }, name: "until_ms" }] }), return_type: None, is_constructor: false }}, object_list: {"calls": ObjectInfo { class_name: "PikaStdTask_Task", name: "calls", import_class_name: "PikaStdData_List" }}, import_list: {}, script_list: Script { content: "" }, is_package: false }}, class_now_name: Some("PikaDebug_Debuger"), package_now_name: Some("PikaDebug"), compiled_list: ["main", "PikaStdLib", "PikaStdLib", "PikaStdTask", "PikaStdData", "PikaStdLib", "PikaStdData", "PikaDebug"] } \ No newline at end of file diff --git a/port/cmsis-pack/pikascript/pikascript-api/pikaScript.c b/port/cmsis-pack/pikascript/pikascript-api/pikaScript.c index 39b2f594b..64a6bdee2 100644 --- a/port/cmsis-pack/pikascript/pikascript-api/pikaScript.c +++ b/port/cmsis-pack/pikascript/pikascript-api/pikaScript.c @@ -8,8 +8,8 @@ PikaObj *__pikaMain; PikaObj *pikaScriptInit(void){ __platform_printf("======[pikascript packages installed]======\r\n"); - __platform_printf("PikaStdLib==v1.8.7\r\n"); - __platform_printf("pikascript-core==v1.8.7\r\n"); + pks_printVersion(); + __platform_printf("PikaStdLib==v1.10.0\r\n"); __platform_printf("===========================================\r\n"); __pikaMain = newRootObj("pikaMain", New_PikaMain); extern unsigned char pikaModules_py_a[]; diff --git a/port/cmsis-pack/pikascript/pikascript-core/PikaCompiler.c b/port/cmsis-pack/pikascript/pikascript-core/PikaCompiler.c index e38157984..071d29a6f 100644 --- a/port/cmsis-pack/pikascript/pikascript-core/PikaCompiler.c +++ b/port/cmsis-pack/pikascript/pikascript-core/PikaCompiler.c @@ -371,7 +371,7 @@ int Lib_loadLibraryFileToArray(char* origin_file_name, char* out_folder) { FILE* fp = __platform_fopen(output_file_path, "wb+"); char* array_name = strsGetLastToken(&buffs, origin_file_name, '/'); array_name = strsReplace(&buffs, array_name, ".", "_"); - __platform_printf(" loading %s[]...\n", array_name); + __platform_printf(" loading %s[]...\n", array_name); pika_fputs("#include \"PikaPlatform.h\"\n", fp); pika_fputs("/* warning: auto generated file, please do not modify */\n", fp); @@ -404,7 +404,7 @@ static void __Maker_compileModuleWithInfo(PikaMaker* self, char* module_name) { char* input_file_name = strsAppend(&buffs, module_name, ".py"); char* input_file_path = strsAppend(&buffs, obj_getStr(self, "pwd"), input_file_name); - __platform_printf(" compiling %s...\r\n", input_file_name); + __platform_printf(" compiling %s...\r\n", input_file_name); char* output_file_name = strsAppend(&buffs, module_name, ".py.o"); char* output_file_path = NULL; output_file_path = @@ -587,20 +587,30 @@ int32_t __foreach_handler_linkCompiledModules(Arg* argEach, Args* context) { return 0; } -void pikaMaker_linkCompiledModules(PikaMaker* self, char* lib_name) { +void pikaMaker_linkCompiledModulesFullPath(PikaMaker* self, char* lib_path) { Args context = {0}; LibObj* lib = New_LibObj(NULL); Args buffs = {0}; - __platform_printf(" linking %s...\n", lib_name); + __platform_printf(" linking %s...\n", lib_path); args_setPtr(&context, "__lib", lib); args_setPtr(&context, "__maker", self); args_foreach(self->list, __foreach_handler_linkCompiledModules, &context); args_deinit_stack(&context); char* pwd = obj_getStr(self, "pwd"); - char* folder_path = strsAppend(&buffs, pwd, "pikascript-api/"); - char* lib_file_path = strsAppend(&buffs, folder_path, lib_name); + char* lib_path_folder = strsCopy(&buffs, lib_path); + strPopLastToken(lib_path_folder, '/'); + char* folder_path = strsAppend(&buffs, pwd, lib_path_folder); + folder_path = strsAppend(&buffs, folder_path, "/"); + char* lib_file_path = strsAppend(&buffs, pwd, lib_path); LibObj_saveLibraryFile(lib, lib_file_path); Lib_loadLibraryFileToArray(lib_file_path, folder_path); LibObj_deinit(lib); strsDeinit(&buffs); } + +void pikaMaker_linkCompiledModules(PikaMaker* self, char* lib_name) { + Args buffs = {0}; + char* lib_file_path = strsAppend(&buffs, "pikascript-api/", lib_name); + pikaMaker_linkCompiledModulesFullPath(self, lib_file_path); + strsDeinit(&buffs); +} diff --git a/port/cmsis-pack/pikascript/pikascript-core/PikaCompiler.h b/port/cmsis-pack/pikascript/pikascript-core/PikaCompiler.h index c51d99f21..09e8794a9 100644 --- a/port/cmsis-pack/pikascript/pikascript-core/PikaCompiler.h +++ b/port/cmsis-pack/pikascript/pikascript-core/PikaCompiler.h @@ -29,6 +29,7 @@ char* pikaMaker_getFirstNocompiled(PikaMaker* self); void pikaMaker_compileModuleWithDepends(PikaMaker* self, char* module_name); void pikaMaker_linkCompiledModules(PikaMaker* self, char* lib_name); int LibObj_loadLibrary(LibObj* self, uint8_t* library_bytes); +void pikaMaker_linkCompiledModulesFullPath(PikaMaker* self, char* lib_path); #define LIB_VERSION_NUMBER 1 #define LIB_INFO_BLOCK_SIZE 32 diff --git a/port/cmsis-pack/pikascript/pikascript-core/PikaObj.c b/port/cmsis-pack/pikascript/pikascript-core/PikaObj.c index 47cbac0bf..ebdc3a621 100644 --- a/port/cmsis-pack/pikascript/pikascript-core/PikaObj.c +++ b/port/cmsis-pack/pikascript/pikascript-core/PikaObj.c @@ -122,75 +122,68 @@ int32_t obj_deinit(PikaObj* self) { return obj_deinit_no_del(self); } -int32_t obj_enable(PikaObj* self) { - obj_setInt(self, "isEnable", 1); - return 0; -} - -int32_t obj_disable(PikaObj* self) { - obj_setInt(self, "isEnable", 0); - return 0; -} - -int32_t obj_setInt(PikaObj* self, char* argPath, int64_t val) { +PIKA_RES obj_setInt(PikaObj* self, char* argPath, int64_t val) { PikaObj* obj = obj_getHostObj(self, argPath); if (NULL == obj) { /* [error] object no found */ - return 1; + return PIKA_RES_ERR_ARG_NO_FOUND; } char* name = strPointToLastToken(argPath, '.'); args_setInt(obj->list, name, val); - return 0; + return PIKA_RES_OK; } -int32_t obj_setPtr(PikaObj* self, char* argPath, void* pointer) { +PIKA_RES obj_setPtr(PikaObj* self, char* argPath, void* pointer) { PikaObj* obj = obj_getHostObj(self, argPath); if (NULL == obj) { - return 1; + /* [error] object no found */ + return PIKA_RES_ERR_ARG_NO_FOUND; } char* name = strPointToLastToken(argPath, '.'); args_setPtr(obj->list, name, pointer); - return 0; + return PIKA_RES_OK; } -int32_t obj_setRef(PikaObj* self, char* argPath, void* pointer) { +PIKA_RES obj_setRef(PikaObj* self, char* argPath, void* pointer) { PikaObj* obj = obj_getHostObj(self, argPath); if (NULL == obj) { - return 1; + /* [error] object no found */ + return PIKA_RES_ERR_ARG_NO_FOUND; } char* name = strPointToLastToken(argPath, '.'); args_setRef(obj->list, name, pointer); - return 0; + return PIKA_RES_OK; } -int32_t obj_setFloat(PikaObj* self, char* argPath, double value) { +PIKA_RES obj_setFloat(PikaObj* self, char* argPath, double value) { PikaObj* obj = obj_getHostObj(self, argPath); if (NULL == obj) { - return 1; + /* [error] object no found */ + return PIKA_RES_ERR_ARG_NO_FOUND; } char* name = strPointToLastToken(argPath, '.'); args_setFloat(obj->list, name, value); - return 0; + return PIKA_RES_OK; } -int32_t obj_setStr(PikaObj* self, char* argPath, char* str) { +PIKA_RES obj_setStr(PikaObj* self, char* argPath, char* str) { PikaObj* obj = obj_getHostObj(self, argPath); if (NULL == obj) { - return 1; + return PIKA_RES_ERR_ARG_NO_FOUND; } char* name = strPointToLastToken(argPath, '.'); args_setStr(obj->list, name, str); - return 0; + return PIKA_RES_OK; } -int32_t obj_setBytes(PikaObj* self, char* argPath, uint8_t* src, size_t size) { +PIKA_RES obj_setBytes(PikaObj* self, char* argPath, uint8_t* src, size_t size) { PikaObj* obj = obj_getHostObj(self, argPath); if (NULL == obj) { - return 1; + return PIKA_RES_ERR_ARG_NO_FOUND; } char* name = strPointToLastToken(argPath, '.'); args_setBytes(obj->list, name, src, size); - return 0; + return PIKA_RES_OK; } int64_t obj_getInt(PikaObj* self, char* argPath) { @@ -204,12 +197,18 @@ int64_t obj_getInt(PikaObj* self, char* argPath) { } Arg* obj_getArg(PikaObj* self, char* argPath) { - PikaObj* obj = obj_getHostObj(self, argPath); + PIKA_BOOL isClass = PIKA_FALSE; + PikaObj* obj = obj_getHostObjWithIsClass(self, argPath, &isClass); if (NULL == obj) { return NULL; } char* argName = strPointToLastToken(argPath, '.'); Arg* res = args_getArg(obj->list, argName); + if (isClass) { + obj_setArg(self, "_buf", res); + res = obj_getArg(self, "_buf"); + obj_deinit(obj); + } return res; } @@ -244,7 +243,7 @@ size_t obj_loadBytes(PikaObj* self, char* argPath, uint8_t* out_buff) { return size_mem; } -static int32_t __obj_setArg(PikaObj* self, +static PIKA_RES __obj_setArg(PikaObj* self, char* argPath, Arg* arg, uint8_t is_copy) { @@ -252,7 +251,7 @@ static int32_t __obj_setArg(PikaObj* self, PikaObj* obj = obj_getHostObj(self, argPath); if (NULL == obj) { /* object no found */ - return 1; + return PIKA_RES_ERR_ARG_NO_FOUND; } char* argName = strPointToLastToken(argPath, '.'); Arg* newArg; @@ -263,14 +262,14 @@ static int32_t __obj_setArg(PikaObj* self, } newArg = arg_setName(newArg, argName); args_setArg(obj->list, newArg); - return 0; + return PIKA_RES_OK; } -int32_t obj_setArg(PikaObj* self, char* argPath, Arg* arg) { +PIKA_RES obj_setArg(PikaObj* self, char* argPath, Arg* arg) { return __obj_setArg(self, argPath, arg, 1); }; -int32_t obj_setArg_noCopy(PikaObj* self, char* argPath, Arg* arg) { +PIKA_RES obj_setArg_noCopy(PikaObj* self, char* argPath, Arg* arg) { return __obj_setArg(self, argPath, arg, 0); } @@ -304,42 +303,24 @@ char* obj_getStr(PikaObj* self, char* argPath) { return res; } -int32_t obj_load(PikaObj* self, Args* args, char* name) { - args_copyArgByName(args, name, self->list); - return 0; -} - -int32_t obj_freeObj(PikaObj* self, char* objPath) { - PikaObj* obj = obj_getPtr(self, objPath); - obj_deinit(obj); - return 0; -} - -char* obj_print(PikaObj* self, char* name) { - if (NULL == self) { - return NULL; - } - return args_print(self->list, name); -} - PikaObj* obj_getClassObjByNewFun(PikaObj* context, char* name, NewFun newClassFun) { Args* initArgs = New_args(NULL); PikaObj* thisClass = newClassFun(initArgs); - obj_setPtr(thisClass, "_clsptr", (void*)newClassFun); - obj_setInt(thisClass, "_refcnt", 0); + thisClass->constructor = newClassFun; + thisClass->refcnt = 0; args_deinit(initArgs); return thisClass; } -Arg* obj_getMethodArg(PikaObj* obj, char* methodPath) { +Arg* _obj_getMethodArg(PikaObj* obj, char* methodPath, Arg* arg_reg) { Arg* method = NULL; char* methodName = strPointToLastToken(methodPath, '.'); method = obj_getArg(obj, methodName); PikaObj* methodHostClass; if (NULL != method) { - method = arg_copy(method); + method = arg_copy_noalloc(method, arg_reg); goto exit; } methodHostClass = obj_getClassObj(obj); @@ -347,14 +328,26 @@ Arg* obj_getMethodArg(PikaObj* obj, char* methodPath) { method = NULL; goto exit; } - method = arg_copy(obj_getArg(methodHostClass, methodName)); + method = arg_copy_noalloc(obj_getArg(methodHostClass, methodName), arg_reg); obj_deinit_no_del(methodHostClass); exit: return method; } +Arg* obj_getMethodArg(PikaObj* obj, char* methodPath) { + return _obj_getMethodArg(obj, methodPath, NULL); +} + +Arg* obj_getMethodArg_noalloc(PikaObj* obj, char* methodPath, Arg* arg_reg) { + return _obj_getMethodArg(obj, methodPath, arg_reg); +} + +NewFun obj_getClass(PikaObj* obj) { + return (NewFun)obj->constructor; +} + PikaObj* obj_getClassObj(PikaObj* obj) { - NewFun classPtr = (NewFun)obj_getPtr(obj, "_clsptr"); + NewFun classPtr = obj_getClass(obj); if (NULL == classPtr) { return NULL; } @@ -393,15 +386,14 @@ PikaObj* newNormalObj(NewFun newObjFun) { extern PikaObj* __pikaMain; PikaObj* newRootObj(char* name, NewFun newObjFun) { +#if PIKA_POOL_ENABLE + mem_pool_init(); +#endif PikaObj* newObj = newNormalObj(newObjFun); __pikaMain = newObj; return newObj; } -Arg* obj_getRefArg(PikaObj* self) { - return arg_setPtr(NULL, "", ARG_TYPE_OBJECT_NEW, self); -} - Arg* arg_newMetaObj(NewFun new_obj_fun) { Arg* arg_new = New_arg(NULL); /* m means meta-object */ @@ -411,7 +403,7 @@ Arg* arg_newMetaObj(NewFun new_obj_fun) { Arg* arg_newDirectObj(NewFun new_obj_fun) { PikaObj* newObj = newNormalObj(new_obj_fun); - Arg* arg_new = arg_setPtr(NULL, "", ARG_TYPE_OBJECT_NEW, newObj); + Arg* arg_new = arg_newPtr(ARG_TYPE_OBJECT_NEW, newObj); return arg_new; } @@ -442,7 +434,10 @@ exit: return res; } -static PikaObj* __obj_getObjDirect(PikaObj* self, char* name) { +static PikaObj* __obj_getObjDirect(PikaObj* self, + char* name, + PIKA_BOOL* pIsClass) { + *pIsClass = PIKA_FALSE; if (NULL == self) { return NULL; } @@ -456,11 +451,27 @@ static PikaObj* __obj_getObjDirect(PikaObj* self, char* name) { if (argType_isObject(type)) { return args_getPtr(self->list, name); } + /* found class */ + if (type == ARG_TYPE_METHOD_NATIVE_CONSTRUCTOR || + type == ARG_TYPE_METHOD_CONSTRUCTOR) { + *pIsClass = PIKA_TRUE; + PikaObj* method_args_obj = New_TinyObj(NULL); + Arg* cls_obj_arg = obj_runMethodArg(self, method_args_obj, + args_getArg(self->list, name)); + obj_deinit(method_args_obj); + if (type == ARG_TYPE_METHOD_NATIVE_CONSTRUCTOR) { + obj_runNativeMethod(arg_getPtr(cls_obj_arg), "__init__", NULL); + } + PikaObj* res = arg_getPtr(cls_obj_arg); + arg_deinit(cls_obj_arg); + return res; + } return NULL; } static PikaObj* __obj_getObjWithKeepDeepth(PikaObj* self, char* objPath, + PIKA_BOOL* pIsClass, int32_t keepDeepth) { char objPath_buff[PIKA_PATH_BUFF_SIZE]; __platform_memcpy(objPath_buff, objPath, strGetSize(objPath) + 1); @@ -469,7 +480,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); + obj = __obj_getObjDirect(obj, token, pIsClass); if (obj == NULL) { goto exit; } @@ -480,11 +491,19 @@ exit: } PikaObj* obj_getObj(PikaObj* self, char* objPath) { - return __obj_getObjWithKeepDeepth(self, objPath, 0); + PIKA_BOOL isClass = PIKA_FALSE; + return __obj_getObjWithKeepDeepth(self, objPath, &isClass, 0); } PikaObj* obj_getHostObj(PikaObj* self, char* objPath) { - return __obj_getObjWithKeepDeepth(self, objPath, 1); + PIKA_BOOL isClass = PIKA_FALSE; + return __obj_getObjWithKeepDeepth(self, objPath, &isClass, 1); +} + +PikaObj* obj_getHostObjWithIsClass(PikaObj* self, + char* objPath, + PIKA_BOOL* pIsClass) { + return __obj_getObjWithKeepDeepth(self, objPath, pIsClass, 1); } Method methodArg_getPtr(Arg* method_arg) { @@ -495,9 +514,12 @@ Method methodArg_getPtr(Arg* method_arg) { return (Method)ptr; } -char* methodArg_getTypeList(Arg* method_arg, Args* buffs) { - char* method_dec = strsCopy(buffs, methodArg_getDec(method_arg)); - return strsCut(buffs, method_dec, '(', ')'); +char* methodArg_getTypeList(Arg* method_arg, char* buffs, size_t size) { + char* method_dec = strCopy(buffs, methodArg_getDec(method_arg)); + if (strGetSize(method_dec) > size) { + return NULL; + } + return strCut(buffs, method_dec, '(', ')'); } Method obj_getNativeMethod(PikaObj* self, char* method_name) { @@ -521,7 +543,15 @@ ByteCodeFrame* methodArg_getBytecodeFrame(Arg* method_arg) { char* methodArg_getDec(Arg* method_arg) { uint32_t size_ptr = sizeof(void*); void* info = arg_getContent(method_arg); - return (char*)((uintptr_t)info + 2 * size_ptr); + return (char*)((uintptr_t)info + 3 * size_ptr); +} + +PikaObj* methodArg_getDefContext(Arg* method_arg) { + uint32_t size_ptr = sizeof(void*); + void* info = arg_getContent(method_arg) + 2 * size_ptr; + PikaObj* context = NULL; + __platform_memcpy(&context, info, size_ptr); + return context; } static void obj_saveMethodInfo(PikaObj* self, MethodInfo* method_info) { @@ -532,11 +562,17 @@ static void obj_saveMethodInfo(PikaObj* self, MethodInfo* method_info) { uint32_t size_pars = strGetSize(pars); uintptr_t method_info_bytecode_frame = (uintptr_t)method_info->bytecode_frame; + uintptr_t method_info_def_context = (uintptr_t)method_info->def_context; + /* the first arg_value */ arg = arg_setPtr(arg, method_info->name, method_info->type, method_info->ptr); + /* the seconed */ arg = arg_append(arg, &(method_info_bytecode_frame), sizeof(method_info_bytecode_frame)); + arg = arg_append(arg, &(method_info_def_context), + sizeof(method_info_def_context)); arg = arg_append(arg, method_info->pars, size_pars + 1); + args_setArg(self->list, arg); strsDeinit(&buffs); } @@ -545,6 +581,7 @@ static int32_t __class_defineMethodWithType(PikaObj* self, char* declearation, Method method_ptr, ArgType method_type, + PikaObj* def_context, ByteCodeFrame* bytecode_frame) { int32_t size = strGetSize(declearation); int32_t res = 0; @@ -567,6 +604,7 @@ static int32_t __class_defineMethodWithType(PikaObj* self, method_info.name = method_name; method_info.ptr = (void*)method_ptr; method_info.type = method_type; + method_info.def_context = def_context; method_info.bytecode_frame = bytecode_frame; obj_saveMethodInfo(method_host, &method_info); res = 0; @@ -582,7 +620,7 @@ int32_t class_defineConstructor(PikaObj* self, Method methodPtr) { return __class_defineMethodWithType(self, declearation, methodPtr, ARG_TYPE_METHOD_NATIVE_CONSTRUCTOR, - NULL); + NULL, NULL); } /* define a native method as default */ @@ -590,35 +628,40 @@ int32_t class_defineMethod(PikaObj* self, char* declearation, Method methodPtr) { return __class_defineMethodWithType(self, declearation, methodPtr, - ARG_TYPE_METHOD_NATIVE, NULL); + ARG_TYPE_METHOD_NATIVE, NULL, NULL); } /* define object method, object method is which startwith (self) */ int32_t class_defineRunTimeConstructor(PikaObj* self, char* declearation, Method methodPtr, + PikaObj* def_context, ByteCodeFrame* bytecode_frame) { return __class_defineMethodWithType(self, declearation, methodPtr, ARG_TYPE_METHOD_CONSTRUCTOR, - bytecode_frame); + def_context, bytecode_frame); } /* define object method, object method is which startwith (self) */ int32_t class_defineObjectMethod(PikaObj* self, char* declearation, Method methodPtr, + PikaObj* def_context, ByteCodeFrame* bytecode_frame) { return __class_defineMethodWithType(self, declearation, methodPtr, - ARG_TYPE_METHOD_OBJECT, bytecode_frame); + ARG_TYPE_METHOD_OBJECT, def_context, + bytecode_frame); } /* define a static method as default */ int32_t class_defineStaticMethod(PikaObj* self, char* declearation, Method methodPtr, + PikaObj* def_context, ByteCodeFrame* bytecode_frame) { return __class_defineMethodWithType(self, declearation, methodPtr, - ARG_TYPE_METHOD_STATIC, bytecode_frame); + ARG_TYPE_METHOD_STATIC, def_context, + bytecode_frame); } VMParameters* obj_runDirect(PikaObj* self, char* cmd) { @@ -681,12 +724,13 @@ void obj_run(PikaObj* self, char* cmd) { obj_runDirect(self, cmd); } -void obj_runNativeMethod(PikaObj* self, char* method_name, Args* args) { +PIKA_RES obj_runNativeMethod(PikaObj* self, char* method_name, Args* args) { Method native_method = obj_getNativeMethod(self, method_name); if (NULL == native_method) { - return; + return PIKA_RES_ERR_ARG_NO_FOUND; } native_method(self, args); + return PIKA_RES_OK; } static void __clearBuff(char* buff, int size) { @@ -706,7 +750,8 @@ static void __obj_runCharBeforeRun(PikaObj* self) { enum shell_state obj_runChar(PikaObj* self, char inputChar) { struct shell_config* cfg = args_getStruct(self->list, "__shcfg"); - __obj_shellLineHandler_t __lineHandler_fun = obj_getPtr(self, "__shhdl"); + __obj_shellLineHandler_t __lineHandler_fun = + (__obj_shellLineHandler_t)obj_getPtr(self, "__shhdl"); char* rxBuff = (char*)obj_getBytes(self, "__shbuf"); char* input_line = NULL; int is_in_block = obj_getInt(self, "__shinb"); @@ -841,10 +886,6 @@ int32_t obj_getErrorCode(PikaObj* self) { return obj_getInt(self, "__errCode"); } -void args_setErrorCode(Args* args, int32_t errCode) { - args_setInt(args, "__errCode", errCode); -} - int32_t args_getErrorCode(Args* args) { if (!args_isArgExist(args, "__errCode")) { return 0; @@ -860,10 +901,6 @@ char* obj_getSysOut(PikaObj* self) { return obj_getStr(self, "__sysOut"); } -char* args_getSysOut(Args* args) { - return args_getStr(args, "__sysOut"); -} - void args_setSysOut(Args* args, char* str) { // args_setStr(args, "__sysOut", str); if (NULL == str) { @@ -875,15 +912,6 @@ void args_setSysOut(Args* args, char* str) { __platform_printf("%s\r\n", str); } -void obj_sysPrintf(PikaObj* self, char* fmt, ...) { - va_list args; - va_start(args, fmt); - char sysOut[128] = {0}; - __platform_vsprintf(sysOut, fmt, args); - obj_setSysOut(self, sysOut); - va_end(args); -} - void method_returnBytes(Args* args, uint8_t* val) { args_setBytes(args, "return", val, PIKA_BYTES_DEFAULT_SIZE); } @@ -932,30 +960,16 @@ PikaObj* New_PikaObj(void) { PikaObj* self = pikaMalloc(sizeof(PikaObj)); /* List */ self->list = New_args(NULL); + self->refcnt = 0; + self->constructor = NULL; return self; } -void obj_refcntInc(PikaObj* self) { - obj_setInt(self, "_refcnt", obj_getInt(self, "_refcnt") + 1); -} - -void obj_refcntDec(PikaObj* self) { - obj_setInt(self, "_refcnt", obj_getInt(self, "_refcnt") - 1); -} - -int obj_refcntNow(PikaObj* self) { - return obj_getInt(self, "_refcnt"); -} - Arg* arg_setRef(Arg* self, char* name, PikaObj* obj) { obj_refcntInc(obj); return arg_setPtr(self, name, ARG_TYPE_OBJECT, obj); } -Arg* arg_setWeakRef(Arg* self, char* name, PikaObj* obj) { - return arg_setPtr(self, name, ARG_TYPE_OBJECT, obj); -} - int32_t obj_newDirectObj(PikaObj* self, char* objName, NewFun newFunPtr) { Arg* new_obj = arg_newDirectObj(newFunPtr); new_obj = arg_setName(new_obj, objName); @@ -991,11 +1005,19 @@ PikaObj* obj_importModuleWithByteCode(PikaObj* self, PikaObj* obj_importModuleWithByteCodeFrame(PikaObj* self, char* name, ByteCodeFrame* byteCode_frame) { - obj_newDirectObj(self, name, New_TinyObj); + PikaObj* New_PikaStdLib_SysObj(Args * args); + obj_newDirectObj(self, name, New_PikaStdLib_SysObj); pikaVM_runByteCodeFrame(obj_getObj(self, name), byteCode_frame); return self; } +PikaObj* Obj_linkLibraryFile(PikaObj* self, char* input_file_name) { + obj_newMetaObj(self, "__lib", New_LibObj); + LibObj* lib = obj_getObj(self, "__lib"); + LibObj_loadLibraryFile(lib, input_file_name); + return self; +} + PikaObj* obj_linkLibrary(PikaObj* self, uint8_t* library_bytes) { obj_newMetaObj(self, "__lib", New_LibObj); LibObj* lib = obj_getObj(self, "__lib"); @@ -1065,5 +1087,82 @@ char* obj_toStr(PikaObj* self) { char* str_res = obj_getStr(self, "__res"); return str_res; } - return NULL; + + /* normal object */ + Args buffs = {0}; + char* str_res = + strsFormat(&buffs, PIKA_SPRINTF_BUFF_SIZE, "", self); + obj_setStr(self, "__res", str_res); + strsDeinit(&buffs); + return obj_getStr(self, "__res"); +} + +void pks_eventLicener_registEvent(PikaEventListener* self, + uint32_t eventId, + PikaObj* eventHandleObj) { + Args buffs = {0}; + char* event_name = + strsFormat(&buffs, PIKA_SPRINTF_BUFF_SIZE, "%ld", eventId); + obj_newDirectObj(self, event_name, New_TinyObj); + PikaObj* event_item = obj_getObj(self, event_name); + obj_setRef(event_item, "eventHandleObj", eventHandleObj); + strsDeinit(&buffs); +} + +PikaObj* pks_eventLisener_getEventHandleObj(PikaEventListener* self, + uint32_t eventId) { + Args buffs = {0}; + char* event_name = + strsFormat(&buffs, PIKA_SPRINTF_BUFF_SIZE, "%ld", eventId); + PikaObj* event_item = obj_getObj(self, event_name); + PikaObj* eventHandleObj = obj_getPtr(event_item, "eventHandleObj"); + strsDeinit(&buffs); + return eventHandleObj; +} + +void pks_eventLisener_init(PikaEventListener** p_self) { + *p_self = newNormalObj(New_TinyObj); +} + +void pks_eventLisener_deinit(PikaEventListener** p_self) { + if (NULL != *p_self) { + obj_deinit(*p_self); + *p_self = NULL; + } +} + +void pks_eventLisener_sendSignal(PikaEventListener* self, + uint32_t eventId, + int eventSignal) { + PikaObj* eventHandleObj = pks_eventLisener_getEventHandleObj(self, eventId); + if (NULL == eventHandleObj) { + __platform_printf( + "Error: can not find event handler by id: [0x%02x]\r\n", eventId); + return; + } + obj_setInt(eventHandleObj, "eventSignal", eventSignal); + /* clang-format off */ + PIKA_PYTHON( + eventCallBack(eventSignal) +) + /* clang-format on */ + const uint8_t bytes[] = { + 0x08, 0x00, /* instruct array size */ + 0x10, 0x81, 0x01, 0x00, 0x00, 0x02, 0x0d, 0x00, /* instruct array */ + 0x1b, 0x00, /* const pool size */ + 0x00, 0x65, 0x76, 0x65, 0x6e, 0x74, 0x53, 0x69, 0x67, + 0x6e, 0x61, 0x6c, 0x00, 0x65, 0x76, 0x65, 0x6e, 0x74, + 0x43, 0x61, 0x6c, 0x6c, 0x42, 0x61, 0x63, 0x6b, 0x00, /* const pool */ + }; + pikaVM_runByteCode(eventHandleObj, (uint8_t*)bytes); +} + +/* print major version info */ +void pks_printVersion(void) { + __platform_printf("pikascript-core==v%d.%d.%d (%s)\r\n", PIKA_VERSION_MAJOR, + PIKA_VERSION_MINOR, PIKA_VERSION_MICRO, PIKA_EDIT_TIME); +} + +void* obj_getStruct(PikaObj* self, char* name) { + return args_getStruct(self->list, name); } diff --git a/port/cmsis-pack/pikascript/pikascript-core/PikaObj.h b/port/cmsis-pack/pikascript/pikascript-core/PikaObj.h index f7a393953..b0904c2bf 100644 --- a/port/cmsis-pack/pikascript/pikascript-core/PikaObj.h +++ b/port/cmsis-pack/pikascript/pikascript-core/PikaObj.h @@ -31,6 +31,7 @@ #include "dataArgs.h" #include "dataLink.h" #include "dataMemory.h" +#include "dataStrs.h" typedef struct InstructUnit InstructUnit; struct InstructUnit { @@ -68,6 +69,8 @@ struct ByteCodeFrame { typedef struct PikaObj PikaObj; struct PikaObj { Args* list; + uint8_t refcnt; + void* constructor; }; typedef PikaObj* (*NewFun)(Args* args); @@ -81,6 +84,7 @@ struct MethodInfo { char* dec; char* ptr; char* pars; + PikaObj* def_context; ArgType type; ByteCodeFrame* bytecode_frame; }; @@ -96,14 +100,14 @@ int32_t obj_enable(PikaObj* self); int32_t obj_disable(PikaObj* self); // arg type operations -int32_t obj_setInt(PikaObj* self, char* argPath, int64_t val); -int32_t obj_setRef(PikaObj* self, char* argPath, void* pointer); -int32_t obj_setPtr(PikaObj* self, char* argPath, void* pointer); -int32_t obj_setFloat(PikaObj* self, char* argPath, double value); -int32_t obj_setStr(PikaObj* self, char* argPath, char* str); -int32_t obj_setArg(PikaObj* self, char* argPath, Arg* arg); -int32_t obj_setArg_noCopy(PikaObj* self, char* argPath, Arg* arg); -int32_t obj_setBytes(PikaObj* self, char* argPath, uint8_t* src, size_t size); +PIKA_RES obj_setInt(PikaObj* self, char* argPath, int64_t val); +PIKA_RES obj_setRef(PikaObj* self, char* argPath, void* pointer); +PIKA_RES obj_setPtr(PikaObj* self, char* argPath, void* pointer); +PIKA_RES obj_setFloat(PikaObj* self, char* argPath, double value); +PIKA_RES obj_setStr(PikaObj* self, char* argPath, char* str); +PIKA_RES obj_setArg(PikaObj* self, char* argPath, Arg* arg); +PIKA_RES obj_setArg_noCopy(PikaObj* self, char* argPath, Arg* arg); +PIKA_RES obj_setBytes(PikaObj* self, char* argPath, uint8_t* src, size_t size); void* obj_getPtr(PikaObj* self, char* argPath); double obj_getFloat(PikaObj* self, char* argPath); @@ -123,6 +127,9 @@ 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, + char* objPath, + PIKA_BOOL* pIsClass); // subProcess int32_t obj_freeObj(PikaObj* self, char* subObjectName); @@ -133,11 +140,13 @@ int32_t class_defineMethod(PikaObj* self, char* declearation, Method methodPtr); int32_t class_defineObjectMethod(PikaObj* self, char* declearation, Method methodPtr, + PikaObj* def_context, ByteCodeFrame* bytecode_frame); int32_t class_defineStaticMethod(PikaObj* self, char* declearation, Method methodPtr, + PikaObj* def_context, ByteCodeFrame* bytecode_frame); int32_t class_defineConstructor(PikaObj* self, @@ -147,6 +156,7 @@ int32_t class_defineConstructor(PikaObj* self, int32_t class_defineRunTimeConstructor(PikaObj* self, char* declearation, Method methodPtr, + PikaObj* def_context, ByteCodeFrame* bytecode_frame); int32_t obj_removeArg(PikaObj* self, char* argPath); @@ -155,6 +165,7 @@ PikaObj* obj_getClassObjByNewFun(PikaObj* self, char* name, NewFun newClassFun); PikaObj* newRootObj(char* name, NewFun newObjFun); PikaObj* obj_getClassObj(PikaObj* obj); Arg* obj_getMethodArg(PikaObj* obj, char* methodPath); +Arg* obj_getMethodArg_noalloc(PikaObj* obj, char* methodPath, Arg* arg_reg); void obj_setErrorCode(PikaObj* self, int32_t errCode); int32_t obj_getErrorCode(PikaObj* self); @@ -180,7 +191,7 @@ double method_getFloat(Args* args, char* argName); char* method_getStr(Args* args, char* argName); void method_returnArg(Args* args, Arg* arg); char* methodArg_getDec(Arg* method_arg); -char* methodArg_getTypeList(Arg* method_arg, Args* buffs); +char* methodArg_getTypeList(Arg* method_arg, char* buffs, size_t size); ByteCodeFrame* methodArg_getBytecodeFrame(Arg* method_arg); Method methodArg_getPtr(Arg* method_arg); @@ -214,16 +225,16 @@ void obj_shellLineProcess(PikaObj* self, */ int pikaCompile(char* output_file_name, char* py_lines); Method obj_getNativeMethod(PikaObj* self, char* method_name); -void obj_runNativeMethod(PikaObj* self, char* method_name, Args* args); +PIKA_RES obj_runNativeMethod(PikaObj* self, char* method_name, Args* args); Arg* obj_newObjInPackage(NewFun newObjFun); -void obj_refcntInc(PikaObj* self); -void obj_refcntDec(PikaObj* self); -int obj_refcntNow(PikaObj* self); PikaObj* newNormalObj(NewFun newObjFun); Arg* arg_setRef(Arg* self, char* name, PikaObj* obj); Arg* arg_setWeakRef(Arg* self, char* name, PikaObj* obj); +#define arg_newRef(obj) arg_setRef(NULL, "", (obj)) +#define arg_newWeakRef(obj) arg_setWeakRef(NULL, "", (obj)) + PikaObj* obj_importModuleWithByteCodeFrame(PikaObj* self, char* name, ByteCodeFrame* bytecode_frame); @@ -252,4 +263,32 @@ enum shell_state obj_runChar(PikaObj* self, char inputChar); #define PIKA_PYTHON(x) #define PIKA_PYTHON_END +typedef PikaObj PikaEventListener; + +void pks_eventLisener_sendSignal(PikaEventListener* self, + uint32_t eventId, + int eventSignal); + +void pks_eventLicener_registEvent(PikaEventListener* self, + uint32_t eventId, + PikaObj* eventHandleObj); + +void pks_eventLisener_init(PikaEventListener** p_self); +void pks_eventLisener_deinit(PikaEventListener** p_self); +PikaObj* methodArg_getDefContext(Arg* method_arg); +PikaObj* Obj_linkLibraryFile(PikaObj* self, char* input_file_name); +NewFun obj_getClass(PikaObj* obj); + +void pks_printVersion(void); +void* obj_getStruct(PikaObj* self, char* name); +PikaObj* pks_eventLisener_getEventHandleObj(PikaEventListener* self, + uint32_t eventId); + +#define obj_refcntDec(self) (((self)->refcnt--)) +#define obj_refcntInc(self) (((self)->refcnt)++) +#define obj_refcntNow(self) ((self)->refcnt) + +#define obj_setStruct(PikaObj_p_self, char_p_name, struct_) \ + args_setStruct(((PikaObj_p_self)->list), char_p_name, struct_) + #endif diff --git a/port/cmsis-pack/pikascript/pikascript-core/PikaParser.c b/port/cmsis-pack/pikascript/pikascript-core/PikaParser.c index 926c3e517..fb3761eb9 100644 --- a/port/cmsis-pack/pikascript/pikascript-core/PikaParser.c +++ b/port/cmsis-pack/pikascript/pikascript-core/PikaParser.c @@ -50,6 +50,74 @@ void ParserState_iterStart(struct ParserState* ps); void ParserState_iterEnd(struct ParserState* ps); char* Parser_popToken(Args* buffs_p, char* tokens); +/* Syntex item */ +const SyntaxItem syntexItemList[] = { + /* clang-format off */ + { + .asmCode = "RUN", + .astNodeName = "method", + .isUseNodeValue = PIKA_TRUE + }, + { + .asmCode = "OPT", + .astNodeName = "operator", + .isUseNodeValue = PIKA_TRUE + }, + { + .asmCode = "BYT", + .astNodeName = "bytes", + .isUseNodeValue = PIKA_TRUE + }, + { + .asmCode = "NUM", + .astNodeName = "num", + .isUseNodeValue = PIKA_TRUE + }, + { + .asmCode = "IMP", + .astNodeName = "import", + .isUseNodeValue = PIKA_TRUE + }, + { + .asmCode = "REF", + .astNodeName = "ref", + .isUseNodeValue = PIKA_TRUE + }, + { + .asmCode = "STR", + .astNodeName = "string", + .isUseNodeValue = PIKA_TRUE + }, + +#if PIKA_SYNTAX_SLICE_ENABLE + { + .asmCode = "SLC", + .astNodeName = "slice", + .isUseNodeValue = PIKA_FALSE + }, +#endif + +#if PIKA_BUILTIN_STRUCT_ENABLE + { + .asmCode = "DCT", + .astNodeName = "dict", + .isUseNodeValue = PIKA_FALSE + }, + { + .asmCode = "LST", + .astNodeName = "list", + .isUseNodeValue = PIKA_FALSE + }, +#endif + + { + .asmCode = "OUT", + .astNodeName = "left", + .isUseNodeValue = PIKA_TRUE + } + /* clang-format on */ +}; + uint16_t Tokens_getSize(char* tokens) { if (strEqu("", tokens)) { return 0; @@ -63,8 +131,8 @@ char* strsPopTokenWithSkip_byStr(Args* outBuffs, char skipStart, char skipEnd) { uint8_t divider_index = 0; - Arg* keeped_arg = arg_setStr(NULL, "", ""); - Arg* poped_arg = arg_setStr(NULL, "", ""); + Arg* keeped_arg = arg_newStr(""); + Arg* poped_arg = arg_newStr(""); ParserState_forEachToken(ps, stmts) { ParserState_iterStart(&ps); if (ps.branket_deepth == 0) { @@ -95,6 +163,7 @@ char* strsPopTokenWithSkip_byStr(Args* outBuffs, } char* strsGetCleanCmd(Args* outBuffs, char* cmd) { + pika_assert(cmd != NULL); int32_t size = strGetSize(cmd); /* lexer may generate more chars than input */ char* strOut = args_getBuff(outBuffs, size * 2); @@ -183,50 +252,83 @@ static enum StmtType Lexer_matchStmtType(char* right) { char* rightWithoutSubStmt = __removeTokensBetween(&buffs, right, "(", ")"); rightWithoutSubStmt = __removeTokensBetween(&buffs, rightWithoutSubStmt, "[", "]"); + rightWithoutSubStmt = + __removeTokensBetween(&buffs, rightWithoutSubStmt, "{", "}"); - uint8_t is_get_operator = 0; - uint8_t is_get_method = 0; - uint8_t is_get_string = 0; - uint8_t is_get_bytes = 0; - uint8_t is_get_number = 0; - uint8_t is_get_symbol = 0; - uint8_t is_get_index = 0; - uint8_t is_get_import = 0; + PIKA_BOOL is_get_operator = PIKA_FALSE; + PIKA_BOOL is_get_method = PIKA_FALSE; + PIKA_BOOL is_get_string = PIKA_FALSE; + PIKA_BOOL is_get_bytes = PIKA_FALSE; + PIKA_BOOL is_get_number = PIKA_FALSE; + PIKA_BOOL is_get_symbol = PIKA_FALSE; + PIKA_BOOL is_get_list = PIKA_FALSE; + PIKA_BOOL is_get_slice = PIKA_FALSE; + PIKA_BOOL is_get_dict = PIKA_FALSE; + PIKA_BOOL is_get_import = PIKA_FALSE; + PIKA_BOOL is_get_chain = PIKA_FALSE; ParserState_forEachToken(ps, rightWithoutSubStmt) { ParserState_iterStart(&ps); /* collect type */ if (strEqu(ps.token1.pyload, " import ")) { - is_get_import = 1; + is_get_import = PIKA_TRUE; goto iter_continue; } - if (strEqu(ps.token1.pyload, "[")) { - is_get_index = 1; + if (strEqu(ps.token2.pyload, "[")) { + /* (symble | iteral | <]> | <)>) + <[> */ + if (TOKEN_symbol == ps.token1.type || + TOKEN_literal == ps.token1.type || + strEqu(ps.token1.pyload, "]") || + strEqu(ps.token1.pyload, ")")) { + is_get_slice = PIKA_TRUE; + goto iter_continue; + } + /* ( <,> | <=> ) + <[> */ + is_get_list = PIKA_TRUE; + goto iter_continue; + } + if (strEqu(ps.token1.pyload, "[") && ps.iter_index == 1) { + /* VOID + <[> */ + is_get_list = PIKA_TRUE; + goto iter_continue; + } + if (strEqu(ps.token1.pyload, "...")) { + goto iter_continue; + } + if (strIsStartWith(ps.token1.pyload, ".")) { + if (ps.iter_index != 1) { + is_get_chain = PIKA_TRUE; + goto iter_continue; + } + } + if (strEqu(ps.token1.pyload, "{")) { + is_get_dict = PIKA_TRUE; goto iter_continue; } if (ps.token1.type == TOKEN_operator) { - is_get_operator = 1; + is_get_operator = PIKA_TRUE; goto iter_continue; } - if (ps.token2.type == TOKEN_devider) { - is_get_method = 1; + /* <(> */ + if (strEqu(ps.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] == '"') { - is_get_string = 1; + is_get_string = PIKA_TRUE; goto iter_continue; } if (ps.token1.pyload[1] == '\'' || ps.token1.pyload[1] == '"') { if (ps.token1.pyload[0] == 'b') { - is_get_bytes = 1; + is_get_bytes = PIKA_TRUE; goto iter_continue; } } - is_get_number = 1; + is_get_number = PIKA_TRUE; goto iter_continue; } if (ps.token1.type == TOKEN_symbol) { - is_get_symbol = 1; + is_get_symbol = PIKA_TRUE; goto iter_continue; } iter_continue: @@ -240,10 +342,22 @@ static enum StmtType Lexer_matchStmtType(char* right) { stmtType = STMT_operator; goto exit; } - if (is_get_index) { + if (is_get_slice) { + stmtType = STMT_slice; + goto exit; + } + if (is_get_list) { stmtType = STMT_list; goto exit; } + if (is_get_dict) { + stmtType = STMT_dict; + goto exit; + } + if (is_get_chain) { + stmtType = STMT_chain; + goto exit; + } if (is_get_method) { stmtType = STMT_method; goto exit; @@ -306,6 +420,7 @@ uint8_t Parser_checkIsDirect(char* str) { Args buffs = {0}; char* tokens = Lexer_getTokens(&buffs, str); uint8_t res = 0; + pika_assert(NULL != tokens); if (Parser_isContainToken(tokens, TOKEN_operator, "=")) { res = 1; goto exit; @@ -467,7 +582,7 @@ char* Lexer_getTokens(Args* outBuffs, char* stmt) { /* match devider*/ if (('(' == c0) || (')' == c0) || (',' == c0) || ('[' == c0) || - (']' == c0) || (':' == c0)) { + (']' == c0) || (':' == c0) || ('{' == c0) || ('}' == c0)) { tokens_arg = Lexer_setSymbel(tokens_arg, stmt, i, &symbol_start_index); char content[2] = {0}; @@ -549,6 +664,12 @@ char* Lexer_getTokens(Args* outBuffs, char* stmt) { tokens_arg = Lexer_setToken(tokens_arg, TOKEN_operator, content); continue; } + + // not the string operator + if ((cn1 >= 'a' && cn1 <= 'z') || (cn1 >= 'A' && cn1 <= 'Z') || + (cn1 >= '0' && cn1 <= '9') || cn1 == '_' || cn1 == '.') { + goto after_match_string_operator; + } /* not */ if ('n' == c0) { if (('o' == c1) && ('t' == c2) && (' ' == c3)) { @@ -603,6 +724,8 @@ char* Lexer_getTokens(Args* outBuffs, char* stmt) { continue; } } + after_match_string_operator: + /* skip spaces */ if (' ' == c0) { /* not get symbal */ @@ -676,14 +799,41 @@ static const char operators[][9] = { char* Lexer_getOperator(Args* outBuffs, char* stmt) { Args buffs = {0}; - char* tokens = Lexer_getTokens(&buffs, stmt); char* operator= NULL; + char* tokens = Lexer_getTokens(&buffs, stmt); + + // use parse state foreach to get operator for (uint32_t i = 0; i < sizeof(operators) / 9; i++) { - if (Parser_isContainToken(tokens, TOKEN_operator, - (char*)operators[i])) { - operator= strsCopy(&buffs, (char*)operators[i]); - } + ParserState_forEachToken(ps, tokens) { + ParserState_iterStart(&ps); + // get operator + if (strEqu(ps.token2.pyload, (char*)operators[i])) { + // solve the iuuse of "~-1" + operator= strsCopy(&buffs, (char*)operators[i]); + ParserState_iterEnd(&ps); + break; + } + ParserState_iterEnd(&ps); + }; + ParserState_deinit(&ps); } + + /* solve the iuuse of "~-1" */ + if (strEqu(operator, "-")) { + ParserState_forEachToken(ps, stmt) { + ParserState_iterStart(&ps); + if (strEqu(ps.token2.pyload, "-")) { + if (ps.token1.type == TOKEN_operator) { + operator= strsCopy(&buffs, ps.token1.pyload); + ParserState_iterEnd(&ps); + break; + } + } + ParserState_iterEnd(&ps); + }; + ParserState_deinit(&ps); + } + /* match the last operator in equal level */ if ((strEqu(operator, "+")) || (strEqu(operator, "-"))) { ParserState_forEachToken(ps, stmt) { @@ -699,6 +849,9 @@ char* Lexer_getOperator(Args* outBuffs, char* stmt) { ParserState_deinit(&ps); } /* out put */ + if (NULL == operator) { + return NULL; + } operator= strsCopy(outBuffs, operator); strsDeinit(&buffs); return operator; @@ -724,7 +877,7 @@ void ParserState_iterStart(struct ParserState* ps) { ps->token2.token = Parser_popToken(ps->iter_buffs, ps->tokens); /* store last token */ arg_deinit(ps->last_token); - ps->last_token = arg_setStr(NULL, "", ps->token2.token); + ps->last_token = arg_newStr(ps->token2.token); LexToken_update(&ps->token1); LexToken_update(&ps->token2); @@ -756,6 +909,7 @@ void ParserState_init(struct ParserState* ps) { 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); } @@ -772,18 +926,28 @@ void ParserState_deinit(struct ParserState* ps) { } void ParserState_parse(struct ParserState* ps, char* stmt) { + if (NULL == stmt) { + ps->result = PIKA_RES_ERR_SYNTAX_ERROR; + return; + } ps->tokens = Lexer_getTokens(ps->buffs_p, stmt); + if (NULL == ps->tokens) { + ps->result = PIKA_RES_ERR_SYNTAX_ERROR; + return; + } ps->length = Tokens_getSize(ps->tokens); } void ParserState_beforeIter(struct ParserState* ps) { /* clear first token */ + if (ps->result != PIKA_RES_OK) { + return; + } Parser_popToken(ps->buffs_p, ps->tokens); - ps->last_token = - arg_setStr(NULL, "", Parser_popToken(ps->buffs_p, ps->tokens)); + ps->last_token = arg_newStr(Parser_popToken(ps->buffs_p, ps->tokens)); } -#if PIKA_SYNTEX_ITEM_SLICE_ENABLE +#if PIKA_SYNTAX_SLICE_ENABLE static void Slice_getPars(Args* outBuffs, char* inner, char** pStart, @@ -840,27 +1004,26 @@ static void Slice_getPars(Args* outBuffs, } #endif -#if PIKA_SYNTEX_ITEM_SLICE_ENABLE -char* Parser_solveBranckets(Args* outBuffs, - char* content, - char* stmt, - char* mode) { +#if PIKA_SYNTAX_SLICE_ENABLE +char* Suger_solveLeftBranckets(Args* outBuffs, char* right, char** left_p) { /* init objects */ Args buffs = {0}; - Arg* right_arg = arg_setStr(NULL, "", ""); + Arg* right_arg = arg_newStr(""); + char* left = *left_p; uint8_t is_in_brancket = 0; args_setStr(&buffs, "inner", ""); uint8_t matched = 0; + char* right_res = NULL; /* exit when NULL */ - if (NULL == content) { + if (NULL == left) { arg_deinit(right_arg); - right_arg = arg_setStr(right_arg, "", stmt); + right_arg = arg_setStr(right_arg, "", right); goto exit; } /* exit when not match (symble|iteral)'[' */ - ParserState_forEachToken(ps, content) { + ParserState_forEachToken(ps, left) { ParserState_iterStart(&ps); if (strEqu(ps.token2.pyload, "[")) { if (TOKEN_symbol == ps.token1.type || @@ -876,16 +1039,12 @@ char* Parser_solveBranckets(Args* outBuffs, if (!matched) { /* not contain '[', return origin */ arg_deinit(right_arg); - if (strEqu(mode, "right")) { - right_arg = arg_setStr(right_arg, "", content); - } else if (strEqu(mode, "left")) { - right_arg = arg_setStr(right_arg, "", stmt); - } + right_arg = arg_setStr(right_arg, "", right); goto exit; } /* matched [] */ - ParserState_forEachTokenExistPs(ps, content) { + ParserState_forEachTokenExistPs(ps, left) { ParserState_iterStart(&ps); /* found '[' */ if ((TOKEN_devider == ps.token2.type) && @@ -898,7 +1057,7 @@ char* Parser_solveBranckets(Args* outBuffs, (strEqu(ps.token2.pyload, "]"))) { is_in_brancket = 0; char* inner = args_getStr(&buffs, "inner"); - Arg* inner_arg = arg_setStr(NULL, "", inner); + Arg* inner_arg = arg_newStr(inner); inner_arg = arg_strAppend(inner_arg, ps.token1.pyload); args_setStr(&buffs, "inner", arg_getStr(inner_arg)); arg_deinit(inner_arg); @@ -908,41 +1067,20 @@ char* Parser_solveBranckets(Args* outBuffs, char* end = NULL; char* step = NULL; Slice_getPars(&buffs, inner, &start, &end, &step); - /* __slice__(obj, start, end, step) */ - if (strEqu(mode, "right")) { - right_arg = arg_strAppend(right_arg, "__slice__("); - } else if (strEqu(mode, "left")) { - right_arg = arg_strAppend(right_arg, "__setitem__("); - } + /* obj = __setitem__(obj, key, val) */ + right_arg = arg_strAppend(right_arg, "__setitem__("); right_arg = arg_strAppend(right_arg, args_getStr(&buffs, "obj")); right_arg = arg_strAppend(right_arg, ","); - /* slice only one item */ - /* end = start + 1 */ right_arg = arg_strAppend(right_arg, start); - /* __slice__(obj, index, indxe + 1, 1) */ - if (strEqu(mode, "right")) { - right_arg = arg_strAppend(right_arg, ","); - right_arg = arg_strAppend(right_arg, end); - right_arg = arg_strAppend(right_arg, ","); - right_arg = arg_strAppend(right_arg, step); - } - if (strEqu(mode, "left")) { - right_arg = arg_strAppend(right_arg, ","); - right_arg = arg_strAppend(right_arg, stmt); - right_arg = arg_strAppend(right_arg, - "," - "'"); - right_arg = - arg_strAppend(right_arg, args_getStr(&buffs, "obj")); - right_arg = arg_strAppend(right_arg, "'"); - } + right_arg = arg_strAppend(right_arg, ","); + right_arg = arg_strAppend(right_arg, right); right_arg = arg_strAppend(right_arg, ")"); /* clean the inner */ args_setStr(&buffs, "inner", ""); /* in brancket and found '[' */ } else if (is_in_brancket && (!strEqu(ps.token1.pyload, "["))) { char* inner = args_getStr(&buffs, "inner"); - Arg* index_arg = arg_setStr(NULL, "", inner); + Arg* index_arg = arg_newStr(inner); index_arg = arg_strAppend(index_arg, ps.token1.pyload); args_setStr(&buffs, "inner", arg_getStr(index_arg)); arg_deinit(index_arg); @@ -955,27 +1093,24 @@ char* Parser_solveBranckets(Args* outBuffs, ParserState_iterEnd(&ps); } ParserState_deinit(&ps); + /* clean the left */ + for (size_t i = 0; i < strGetSize(left); i++) { + if (left[i] == '[') { + left[i] = '\0'; + break; + } + } exit: /* clean and return */ - content = strsCopy(outBuffs, arg_getStr(right_arg)); + right_res = strsCopy(outBuffs, arg_getStr(right_arg)); arg_deinit(right_arg); strsDeinit(&buffs); - return content; + return right_res; } #endif -#if PIKA_SYNTEX_ITEM_SLICE_ENABLE -char* Parser_solveRightBranckets(Args* outBuffs, char* right) { - return Parser_solveBranckets(outBuffs, right, NULL, "right"); -} - -char* Parser_solveLeftBranckets(Args* outBuffs, char* right, char* left) { - return Parser_solveBranckets(outBuffs, left, right, "left"); -} -#endif - -#if PIKA_SYNTEX_ITEM_FORMAT_ENABLE -char* Parser_solveFormat(Args* outBuffs, char* right) { +#if PIKA_SYNTAX_FORMAT_ENABLE +char* Suger_solveFormat(Args* outBuffs, char* right) { /* quick skip */ if (!strIsContain(right, '%')) { return right; @@ -995,8 +1130,8 @@ char* Parser_solveFormat(Args* outBuffs, char* right) { } char* res = right; - Arg* str_buf = arg_setStr(NULL, "", ""); - Arg* var_buf = arg_setStr(NULL, "", ""); + Arg* str_buf = arg_newStr(""); + Arg* var_buf = arg_newStr(""); PIKA_BOOL is_in_format = PIKA_FALSE; PIKA_BOOL is_tuple = PIKA_FALSE; PIKA_BOOL is_out_vars = PIKA_FALSE; @@ -1079,9 +1214,9 @@ uint8_t Parser_solveSelfOperator(Args* outbuffs, char** left_p) { char* left_new = NULL; char* right_new = NULL; - Arg* left_arg = arg_setStr(NULL, "", ""); - Arg* right_arg = arg_setStr(NULL, "", ""); - Arg* right_arg_new = arg_setStr(NULL, "", ""); + Arg* left_arg = arg_newStr(""); + Arg* right_arg = arg_newStr(""); + Arg* right_arg_new = arg_newStr(""); uint8_t is_left_exist = 0; Args buffs = {0}; @@ -1151,6 +1286,103 @@ 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); +} + +AST* AST_parseStmt(AST* ast, char* stmt); +PIKA_RES AST_parseSubStmt(AST* ast, char* node_content) { + queueObj_pushObj(ast, (char*)"stmt"); + AST_parseStmt(queueObj_getCurrentObj(ast), node_content); + return PIKA_RES_OK; +} + +char* Parser_popSubStmt(Args* outbuffs, char** stmt_p, char* delimiter) { + Arg* substmt_arg = arg_newStr(""); + Arg* newstmt_arg = arg_newStr(""); + char* stmt = *stmt_p; + PIKA_BOOL is_get_substmt = PIKA_FALSE; + Args buffs = {0}; + ParserState_forEachToken(ps, stmt) { + ParserState_iterStart(&ps); + if (is_get_substmt) { + /* get new stmt */ + newstmt_arg = arg_strAppend(newstmt_arg, ps.token1.pyload); + ParserState_iterEnd(&ps); + continue; + } + if (ps.branket_deepth > 0) { + /* ignore */ + substmt_arg = arg_strAppend(substmt_arg, ps.token1.pyload); + ParserState_iterEnd(&ps); + continue; + } + if (strEqu(ps.token1.pyload, delimiter)) { + /* found delimiter */ + is_get_substmt = PIKA_TRUE; + ParserState_iterEnd(&ps); + continue; + } + /* collect substmt */ + substmt_arg = arg_strAppend(substmt_arg, ps.token1.pyload); + ParserState_iterEnd(&ps); + } + ParserState_deinit(&ps); + + strsDeinit(&buffs); + + char* substmt = strsCacheArg(outbuffs, substmt_arg); + char* newstmt = strsCacheArg(outbuffs, newstmt_arg); + *stmt_p = newstmt; + return substmt; +} + +char* Parser_popLastSubStmt(Args* outbuffs, char** stmt_p, char* delimiter) { + uint8_t last_stmt_i = 0; + char* stmt = *stmt_p; + ParserState_forEachToken(ps, stmt) { + ParserState_iterStart(&ps); + if (strIsStartWith(ps.token1.pyload, delimiter)) { + /* found delimiter */ + if (!strEqu(delimiter, "[") && ps.branket_deepth > 0) { + /* ignore */ + ParserState_iterEnd(&ps); + continue; + } + + /* for "[" */ + if (ps.branket_deepth > 1) { + /* ignore */ + ParserState_iterEnd(&ps); + continue; + } + + last_stmt_i = ps.iter_index; + } + ParserState_iterEnd(&ps); + } + ParserState_deinit(&ps); + + Arg* mainStmt = arg_newStr(""); + Arg* lastStmt = arg_newStr(""); + { + ParserState_forEachToken(ps, stmt) { + ParserState_iterStart(&ps); + if (ps.iter_index < last_stmt_i) { + mainStmt = arg_strAppend(mainStmt, ps.token1.pyload); + } + if (ps.iter_index >= last_stmt_i) { + lastStmt = arg_strAppend(lastStmt, ps.token1.pyload); + } + ParserState_iterEnd(&ps); + } + ParserState_deinit(&ps); + } + + *stmt_p = strsCacheArg(outbuffs, mainStmt); + return strsCacheArg(outbuffs, lastStmt); +} + AST* AST_parseStmt(AST* ast, char* stmt) { Args buffs = {0}; char* assignment = strsGetFirstToken(&buffs, stmt, '('); @@ -1161,6 +1393,7 @@ AST* AST_parseStmt(AST* ast, char* stmt) { char* left = NULL; char* right = NULL; char* import = NULL; + PIKA_RES result = PIKA_RES_OK; right = stmt; /* solve check direct */ @@ -1193,27 +1426,18 @@ AST* AST_parseStmt(AST* ast, char* stmt) { isLeftExist = Parser_solveSelfOperator(&buffs, stmt, &right, &left); } -#if PIKA_SYNTEX_ITEM_SLICE_ENABLE - char* right_new = right; +#if PIKA_SYNTAX_SLICE_ENABLE /* solve the [] stmt */ - right = Parser_solveRightBranckets(&buffs, right); - right_new = Parser_solveLeftBranckets(&buffs, right, left); - /* left is contain the '[]' */ - if (!strEqu(right_new, right)) { - /* update new right */ - right = right_new; - /* cancel left */ - isLeftExist = 0; - } + right = Suger_solveLeftBranckets(&buffs, right, &left); #endif -#if PIKA_SYNTEX_ITEM_FORMAT_ENABLE - right = Parser_solveFormat(&buffs, right); +#if PIKA_SYNTAX_FORMAT_ENABLE + right = Suger_solveFormat(&buffs, right); #endif /* set left */ if (isLeftExist) { - obj_setStr(ast, (char*)"left", left); + AST_setThisNode(ast, (char*)"left", left); } /* match statment type */ enum StmtType stmtType = Lexer_matchStmtType(right); @@ -1221,53 +1445,90 @@ AST* AST_parseStmt(AST* ast, char* stmt) { if (STMT_operator == stmtType) { char* rightWithoutSubStmt = strsDeleteBetween(&buffs, right, '(', ')'); char* operator= Lexer_getOperator(&buffs, rightWithoutSubStmt); - obj_setStr(ast, (char*)"operator", operator); + if (NULL == operator) { + result = PIKA_RES_ERR_SYNTAX_ERROR; + goto exit; + } + AST_setThisNode(ast, (char*)"operator", operator); char* rightBuff = strsCopy(&buffs, right); char* subStmt1 = strsPopTokenWithSkip_byStr(&buffs, rightBuff, operator, '(', ')'); char* subStmt2 = rightBuff; - queueObj_pushObj(ast, (char*)"stmt"); - AST_parseStmt(queueObj_getCurrentObj(ast), subStmt1); - queueObj_pushObj(ast, (char*)"stmt"); - AST_parseStmt(queueObj_getCurrentObj(ast), subStmt2); + AST_parseSubStmt(ast, subStmt1); + AST_parseSubStmt(ast, subStmt2); goto exit; } -#if PIKA_BUILTIN_LIST_ENABLE +#if PIKA_BUILTIN_STRUCT_ENABLE /* solve list stmt */ if (STMT_list == stmtType) { - obj_setStr(ast, (char*)"list", "list"); + AST_setThisNode(ast, (char*)"list", "list"); char* subStmts = strsCut(&buffs, right, '[', ']'); subStmts = strsAppend(&buffs, subStmts, ","); - Arg* subStmt = arg_setStr(NULL, "", ""); - char* subStmt_str = NULL; - ParserState_forEachToken(ps, subStmts) { - ParserState_iterStart(&ps); - if (ps.branket_deepth > 0) { - /* in brankets */ - /* append token to subStmt */ - subStmt = arg_strAppend(subStmt, ps.token1.pyload); - subStmt_str = arg_getStr(subStmt); - } else { - /* not in brankets */ - if (strEqu(ps.token1.pyload, ",")) { - /* found "," push subStmt */ - queueObj_pushObj(ast, (char*)"stmt"); - subStmt_str = arg_getStr(subStmt); - AST_parseStmt(queueObj_getCurrentObj(ast), subStmt_str); - /* clear subStmt */ - arg_deinit(subStmt); - subStmt = arg_setStr(NULL, "", ""); - } else { - /* not "," append subStmt */ - subStmt = arg_strAppend(subStmt, ps.token1.pyload); - subStmt_str = arg_getStr(subStmt); - } + while (1) { + char* subStmt = Parser_popSubStmt(&buffs, &subStmts, ","); + AST_parseSubStmt(ast, subStmt); + if (strEqu(subStmts, "")) { + break; + } + } + goto exit; + } +#endif + +#if PIKA_BUILTIN_STRUCT_ENABLE + /* solve dict stmt */ + if (STMT_dict == stmtType) { + AST_setThisNode(ast, (char*)"dict", "dict"); + char* subStmts = strsCut(&buffs, right, '{', '}'); + subStmts = strsAppend(&buffs, subStmts, ","); + while (1) { + char* subStmt = Parser_popSubStmt(&buffs, &subStmts, ","); + char* key = Parser_popSubStmt(&buffs, &subStmt, ":"); + char* value = subStmt; + AST_parseSubStmt(ast, key); + AST_parseSubStmt(ast, value); + if (strEqu(subStmts, "")) { + break; + } + } + goto exit; + } +#endif + + /* solve method chain */ + if (STMT_chain == stmtType) { + char* stmt = strsCopy(&buffs, right); + char* lastStmt = Parser_popLastSubStmt(&buffs, &stmt, "."); + AST_parseSubStmt(ast, stmt); + AST_parseStmt(ast, lastStmt); + goto exit; + } + +#if PIKA_SYNTAX_SLICE_ENABLE + if (STMT_slice == stmtType) { + /* solve slice stmt */ + AST_setThisNode(ast, (char*)"slice", "slice"); + char* stmt = strsCopy(&buffs, right); + char* laststmt = Parser_popLastSubStmt(&buffs, &stmt, "["); + AST_parseSubStmt(ast, stmt); + char* slice_list = strsCut(&buffs, laststmt, '[', ']'); + slice_list = strsAppend(&buffs, slice_list, ":"); + int index = 0; + while (1) { + char* slice_str = Parser_popSubStmt(&buffs, &slice_list, ":"); + if (index == 0 && strEqu(slice_str, "")) { + AST_parseSubStmt(ast, "0"); + } else if (index == 1 && strEqu(slice_str, "")) { + AST_parseSubStmt(ast, "-1"); + } else { + AST_parseSubStmt(ast, slice_str); + } + index++; + if (strEqu("", slice_list)) { + break; } - ParserState_iterEnd(&ps); } - ParserState_deinit(&ps); - arg_deinit(subStmt); goto exit; } #endif @@ -1275,56 +1536,30 @@ AST* AST_parseStmt(AST* ast, char* stmt) { /* solve method stmt */ if (STMT_method == stmtType) { method = strsGetFirstToken(&buffs, right, '('); - obj_setStr(ast, (char*)"method", method); + AST_setThisNode(ast, (char*)"method", method); char* subStmts = strsCut(&buffs, right, '(', ')'); + pika_assert(NULL != subStmts); /* add ',' at the end */ subStmts = strsAppend(&buffs, subStmts, ","); - /* init process values */ - Arg* subStmt = arg_setStr(NULL, "", ""); - /* start iteration */ - char* subStmt_str = NULL; - ParserState_forEachToken(ps, subStmts) { - ParserState_iterStart(&ps); - /* parse process */ - if (ps.branket_deepth > 0) { - /* in brankets */ - /* append token to subStmt */ - subStmt = arg_strAppend(subStmt, ps.token1.pyload); - subStmt_str = arg_getStr(subStmt); - } else { - /* not in brankets */ - if (strEqu(ps.token1.pyload, ",")) { - /* found "," push subStmt */ - queueObj_pushObj(ast, (char*)"stmt"); - subStmt_str = arg_getStr(subStmt); - AST_parseStmt(queueObj_getCurrentObj(ast), subStmt_str); - /* clear subStmt */ - arg_deinit(subStmt); - subStmt = arg_setStr(NULL, "", ""); - } else { - /* not "," append subStmt */ - subStmt = arg_strAppend(subStmt, ps.token1.pyload); - subStmt_str = arg_getStr(subStmt); - } + while (1) { + char* substmt = Parser_popSubStmt(&buffs, &subStmts, ","); + AST_parseSubStmt(ast, substmt); + if (strEqu("", subStmts)) { + break; } - /* parse preocess end */ - ParserState_iterEnd(&ps); - continue; } - ParserState_deinit(&ps); - arg_deinit(subStmt); goto exit; } /* solve reference stmt */ if (STMT_reference == stmtType) { ref = right; - obj_setStr(ast, (char*)"ref", ref); + AST_setThisNode(ast, (char*)"ref", ref); goto exit; } /* solve import stmt */ if (STMT_import == stmtType) { import = strsGetLastToken(&buffs, right, ' '); - obj_setStr(ast, (char*)"import", import); + AST_setThisNode(ast, (char*)"import", import); goto exit; } /* solve str stmt */ @@ -1339,7 +1574,7 @@ AST* AST_parseStmt(AST* ast, char* stmt) { str = strsReplace(&buffs, str, "\\\"", "\""); str = strsReplace(&buffs, str, "\\'", "'"); } - obj_setStr(ast, (char*)"string", str); + AST_setThisNode(ast, (char*)"string", str); goto exit; } /* solve bytes stmt */ @@ -1347,17 +1582,21 @@ AST* AST_parseStmt(AST* ast, char* stmt) { str = right + 1; str = strsDeleteChar(&buffs, str, '\''); str = strsDeleteChar(&buffs, str, '\"'); - obj_setStr(ast, (char*)"bytes", str); + AST_setThisNode(ast, (char*)"bytes", str); goto exit; } /* solve number stmt */ if (STMT_number == stmtType) { num = right; - obj_setStr(ast, (char*)"num", num); + AST_setThisNode(ast, (char*)"num", num); goto exit; } exit: strsDeinit(&buffs); + if (result != PIKA_RES_OK) { + AST_deinit(ast); + return NULL; + } return ast; } @@ -1472,7 +1711,7 @@ AST* AST_parseLine(char* line, Stack* block_stack) { if (strIsStartWith(line_start, keyword) && (line_start[keyword_len] == ' ')) { stmt = strsCut(&buffs, line_start, ' ', ':'); - obj_setStr(ast, "block", keyword); + AST_setThisNode(ast, "block", keyword); if (NULL != block_stack) { stack_pushStr(block_stack, keyword); } @@ -1488,7 +1727,7 @@ AST* AST_parseLine(char* line, Stack* block_stack) { if ((strIsStartWith(line_start, keyward)) && ((line_start[keyward_size] == ' ') || (line_start[keyward_size] == 0))) { - obj_setStr(ast, keyward, ""); + AST_setThisNode(ast, keyward, ""); stmt = ""; goto block_matched; } @@ -1499,18 +1738,15 @@ AST* AST_parseLine(char* line, Stack* block_stack) { Args* list_buffs = New_strBuff(); char* line_buff = strsCopy(list_buffs, line_start + 4); char* arg_in = strsPopToken(list_buffs, line_buff, ' '); - obj_setStr(ast, "arg_in", arg_in); + AST_setThisNode(ast, "arg_in", arg_in); strsPopToken(list_buffs, line_buff, ' '); - if (strIsStartWith(line_buff, "range(")) { - obj_setInt(ast, "isRange", 1); - } 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); - obj_setStr(ast, "block", "for"); - obj_setStr(ast, "list_in", list_in); + AST_setThisNode(ast, "block", "for"); + AST_setThisNode(ast, "list_in", list_in); if (NULL != block_stack) { stack_pushStr(block_stack, "for"); } @@ -1522,15 +1758,42 @@ AST* AST_parseLine(char* line, Stack* block_stack) { if (strIsStartWith(line_start, "else")) { if ((line_start[4] == ' ') || (line_start[4] == ':')) { stmt = ""; - obj_setStr(ast, "block", "else"); + AST_setThisNode(ast, "block", "else"); if (NULL != block_stack) { stack_pushStr(block_stack, "else"); } } goto block_matched; } + +#if PIKA_SYNTAX_EXCEPTION_ENABLE + /* try */ + 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"); + } + } + goto block_matched; + } + + /* except */ + 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"); + } + } + goto block_matched; + } +#endif + if (strEqu(line_start, "return")) { - obj_setStr(ast, "return", ""); + AST_setThisNode(ast, "return", ""); stmt = ""; goto block_matched; } @@ -1538,22 +1801,48 @@ AST* AST_parseLine(char* line, Stack* block_stack) { char* lineBuff = strsCopy(&buffs, line_start); strsPopToken(&buffs, lineBuff, ' '); stmt = lineBuff; - obj_setStr(ast, "return", ""); + AST_setThisNode(ast, "return", ""); goto block_matched; } + +#if PIKA_SYNTAX_EXCEPTION_ENABLE + if (strEqu(line_start, "raise")) { + AST_setThisNode(ast, "raise", ""); + stmt = "RuntimeError"; + goto block_matched; + } + if (strIsStartWith(line_start, "raise ")) { + char* lineBuff = strsCopy(&buffs, line_start); + strsPopToken(&buffs, lineBuff, ' '); + stmt = lineBuff; + if (strEqu("", stmt)) { + stmt = "RuntimeError"; + } + AST_setThisNode(ast, "raise", ""); + goto block_matched; + } +#endif + if (strIsStartWith(line_start, "global ")) { stmt = ""; char* global_list = line_start + 7; global_list = strsGetCleanCmd(&buffs, global_list); - obj_setStr(ast, "global", global_list); + AST_setThisNode(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); goto block_matched; } if (strIsStartWith(line_start, (char*)"def ")) { stmt = ""; char* declear = strsCut(&buffs, line_start, ' ', ':'); declear = strsGetCleanCmd(&buffs, declear); - obj_setStr(ast, "block", "def"); - obj_setStr(ast, "declear", declear); + AST_setThisNode(ast, "block", "def"); + AST_setThisNode(ast, "declear", declear); if (NULL != block_stack) { stack_pushStr(block_stack, "def"); } @@ -1563,8 +1852,8 @@ AST* AST_parseLine(char* line, Stack* block_stack) { stmt = ""; char* declear = strsCut(&buffs, line_start, ' ', ':'); declear = strsGetCleanCmd(&buffs, declear); - obj_setStr(ast, "block", "class"); - obj_setStr(ast, "declear", declear); + AST_setThisNode(ast, "block", "class"); + AST_setThisNode(ast, "declear", declear); if (NULL != block_stack) { stack_pushStr(block_stack, "class"); } @@ -1572,6 +1861,11 @@ AST* AST_parseLine(char* line, Stack* block_stack) { } block_matched: + if (NULL == stmt) { + AST_deinit(ast); + ast = NULL; + goto exit; + } stmt = strsGetCleanCmd(&buffs, stmt); ast = AST_parseStmt(ast, stmt); goto exit; @@ -1580,7 +1874,8 @@ exit: return ast; } -static char* Parser_PreProcess_import(Args* buffs_p, char* line) { +#if PIKA_SYNTAX_IMPORT_EX_ENABLE +static char* Suger_import(Args* buffs_p, char* line) { Args buffs = {0}; char* line_out = line; char* alias = NULL; @@ -1622,8 +1917,10 @@ exit: strsDeinit(&buffs); return line_out; } +#endif -static char* Parser_PreProcess_from(Args* buffs_p, char* line) { +#if PIKA_SYNTAX_IMPORT_EX_ENABLE +static char* Suger_from(Args* buffs_p, char* line) { Args buffs = {0}; char* line_out = line; char* class = NULL; @@ -1677,8 +1974,10 @@ exit: strsDeinit(&buffs); return line_out; } +#endif static char* Parser_linePreProcess(Args* buffs_p, char* line) { + line = Parser_removeAnnotation(line); /* check syntex error */ if (Lexer_isError(line)) { line = NULL; @@ -1686,9 +1985,10 @@ static char* Parser_linePreProcess(Args* buffs_p, char* line) { } /* process EOL */ line = strsDeleteChar(buffs_p, line, '\r'); - line = Parser_removeAnnotation(line); - line = Parser_PreProcess_import(buffs_p, line); - line = Parser_PreProcess_from(buffs_p, line); +#if PIKA_SYNTAX_IMPORT_EX_ENABLE + line = Suger_import(buffs_p, line); + line = Suger_from(buffs_p, line); +#endif exit: return line; } @@ -1769,13 +2069,13 @@ char* Parser_parsePyLines(Args* outBuffs, char* py_lines) { Stack block_stack; stack_init(&block_stack); - Arg* asm_buff = arg_setStr(NULL, "", ""); + Arg* asm_buff = arg_newStr(""); uint32_t lines_offset = 0; uint32_t lines_size = strGetSize(py_lines); uint16_t lines_num = strCountSign(py_lines, '\n'); uint16_t lines_index = 0; uint8_t is_in_multi_comment = 0; - Arg* line_connection_arg = arg_setStr(NULL, "", ""); + Arg* line_connection_arg = arg_newStr(""); uint8_t is_line_connection = 0; char* out_ASM = NULL; char* single_ASM; @@ -1797,7 +2097,7 @@ char* Parser_parsePyLines(Args* outBuffs, line = strsCopy(&buffs, arg_getStr(line_connection_arg)); /* reflash the line_connection_arg */ arg_deinit(line_connection_arg); - line_connection_arg = arg_setStr(NULL, "", ""); + line_connection_arg = arg_newStr(""); } /* check connection */ @@ -1832,6 +2132,9 @@ char* Parser_parsePyLines(Args* outBuffs, /* parse single Line to Asm */ single_ASM = Parser_LineToAsm(&buffs, line, &block_stack); +#if PIKA_DEBUG + pika_assert(NULL != single_ASM); +#endif if (NULL == single_ASM) { out_ASM = NULL; strsDeinit(&buffs); @@ -1888,6 +2191,26 @@ char* Parser_multiLineToAsm(Args* outBuffs, char* multi_line) { return Parser_parsePyLines(outBuffs, NULL, multi_line); } +char* Parser_fileToAsm(Args* outBuffs, char* filename) { + Args buffs = {0}; + Arg* file_arg = arg_loadFile(NULL, filename); + pika_assert(NULL != file_arg); + if (NULL == file_arg) { + return NULL; + } + char* lines = (char*)arg_getBytes(file_arg); + /* replace the "\r\n" to "\n" */ + lines = strsReplace(&buffs, lines, "\r\n", "\n"); + /* clear the void line */ + lines = strsReplace(&buffs, lines, "\n\n", "\n"); + /* add '\n' at the end */ + lines = strsAppend(&buffs, lines, "\n\n"); + char* res = Parser_multiLineToAsm(&buffs, lines); + arg_deinit(file_arg); + strsDeinit(&buffs); + return res; +} + char* AST_appandPikaASM(AST* ast, AST* subAst, Args* outBuffs, char* pikaAsm) { int deepth = obj_getInt(ast, "deepth"); Args buffs = {0}; @@ -1899,56 +2222,25 @@ char* AST_appandPikaASM(AST* ast, AST* subAst, Args* outBuffs, char* pikaAsm) { obj_setInt(ast, "deepth", deepth + 1); pikaAsm = AST_appandPikaASM(ast, subStmt, &buffs, pikaAsm); } - char* method = obj_getStr(subAst, "method"); - char* list = obj_getStr(subAst, "list"); - char* operator= obj_getStr(subAst, "operator"); - char* ref = obj_getStr(subAst, "ref"); - char* left = obj_getStr(subAst, "left"); - char* str = obj_getStr(subAst, "string"); - char* bytes = obj_getStr(subAst, "bytes"); - char* num = obj_getStr(subAst, "num"); - char* import = obj_getStr(subAst, "import"); char* buff = args_getBuff(&buffs, PIKA_SPRINTF_BUFF_SIZE); - if (NULL != list) { - __platform_sprintf(buff, "%d LST \n", deepth); - pikaAsm = strsAppend(&buffs, pikaAsm, buff); - } - if (NULL != ref) { - __platform_sprintf(buff, "%d REF %s\n", deepth, ref); - pikaAsm = strsAppend(&buffs, pikaAsm, buff); - } - if (NULL != operator) { - __platform_sprintf(buff, "%d OPT %s\n", deepth, operator); - pikaAsm = strsAppend(&buffs, pikaAsm, buff); - } - if (NULL != method) { - __platform_sprintf(buff, "%d RUN %s\n", deepth, method); - pikaAsm = strsAppend(&buffs, pikaAsm, buff); - } - if (NULL != str) { - __platform_sprintf(buff, "%d STR ", deepth); - Arg* abuff = arg_setStr(NULL, "", buff); - abuff = arg_strAppend(abuff, str); - abuff = arg_strAppend(abuff, "\n"); - pikaAsm = strsAppend(&buffs, pikaAsm, arg_getStr(abuff)); - arg_deinit(abuff); - } - if (NULL != bytes) { - __platform_sprintf(buff, "%d BYT %s\n", deepth, bytes); - pikaAsm = strsAppend(&buffs, pikaAsm, buff); - } - if (NULL != num) { - __platform_sprintf(buff, "%d NUM %s\n", deepth, num); - pikaAsm = strsAppend(&buffs, pikaAsm, buff); - } - if (NULL != left) { - __platform_sprintf(buff, "%d OUT %s\n", deepth, left); - pikaAsm = strsAppend(&buffs, pikaAsm, buff); - } - if (NULL != import) { - __platform_sprintf(buff, "%d IMP %s\n", deepth, import); - pikaAsm = strsAppend(&buffs, pikaAsm, buff); + + /* append the syntax item */ + for (size_t i = 0; i < sizeof(syntexItemList) / sizeof(SyntaxItem); i++) { + char* astNodeVal = obj_getStr(subAst, syntexItemList[i].astNodeName); + if (NULL != astNodeVal) { + /* e.g. "0 RUN print \n" */ + __platform_sprintf(buff, "%d %s ", deepth, + syntexItemList[i].asmCode); + Arg* abuff = arg_newStr(buff); + if (syntexItemList[i].isUseNodeValue) { + abuff = arg_strAppend(abuff, astNodeVal); + } + abuff = arg_strAppend(abuff, "\n"); + pikaAsm = strsAppend(&buffs, pikaAsm, arg_getStr(abuff)); + arg_deinit(abuff); + } } + obj_setInt(ast, "deepth", deepth - 1); goto exit; exit: @@ -1995,6 +2287,20 @@ char* AST_toPikaASM(AST* ast, Args* outBuffs) { ASM_addBlockDeepth(ast, outBuffs, pikaAsm, block_type_num); pikaAsm = strsAppend(outBuffs, pikaAsm, (char*)"0 JMP -1\n"); } +#if PIKA_SYNTAX_EXCEPTION_ENABLE + /* goto the while start when exit while block */ + if (strEqu(block_type, "try")) { + pikaAsm = + ASM_addBlockDeepth(ast, outBuffs, pikaAsm, block_type_num); + pikaAsm = strsAppend(outBuffs, pikaAsm, (char*)"0 NTR \n"); + pikaAsm = strsAppend(outBuffs, pikaAsm, (char*)"0 GER \n"); + pikaAsm = strsAppend(outBuffs, pikaAsm, (char*)"0 JEZ 2\n"); + } + + if (strEqu(block_type, "except")) { + pikaAsm = strsAppend(outBuffs, pikaAsm, (char*)"0 SER 0\n"); + } +#endif /* goto the while start when exit while block */ if (strEqu(block_type, "for")) { pikaAsm = @@ -2041,7 +2347,7 @@ char* AST_toPikaASM(AST* ast, Args* outBuffs) { if (strEqu(obj_getStr(ast, "block"), "for")) { /* for "for" iter */ char* arg_in = obj_getStr(ast, "arg_in"); - Arg* newAsm_arg = arg_setStr(NULL, "", ""); + Arg* newAsm_arg = arg_newStr(""); char _l_x[] = "_lx"; char block_deepth_char = '0'; block_deepth_char += obj_getInt(ast, "blockDeepth"); @@ -2054,7 +2360,7 @@ char* AST_toPikaASM(AST* ast, Args* outBuffs) { newAsm_arg = arg_strAppend(newAsm_arg, "\n"); pikaAsm = strsAppend(&buffs, pikaAsm, arg_getStr(newAsm_arg)); arg_deinit(newAsm_arg); - newAsm_arg = arg_setStr(NULL, "", ""); + newAsm_arg = arg_newStr(""); /* get next */ /* run next(_l) */ /* check item is exist */ @@ -2093,6 +2399,12 @@ char* AST_toPikaASM(AST* ast, Args* outBuffs) { pikaAsm = strsAppend(&buffs, pikaAsm, "0 NEL 1\n"); goto exit; } +#if PIKA_SYNTAX_EXCEPTION_ENABLE + if (strEqu(obj_getStr(ast, "block"), "try")) { + pikaAsm = strsAppend(&buffs, pikaAsm, "0 TRY \n"); + goto exit; + } +#endif if (strEqu(obj_getStr(ast, "block"), "elif")) { /* skip if __else is 0 */ pikaAsm = strsAppend(&buffs, pikaAsm, "0 NEL 1\n"); @@ -2158,6 +2470,15 @@ char* AST_toPikaASM(AST* ast, Args* outBuffs) { is_block_matched = 1; goto exit; } +#if PIKA_SYNTAX_EXCEPTION_ENABLE + if (obj_isArgExist(ast, "raise")) { + /* parse stmt ast */ + pikaAsm = AST_appandPikaASM(ast, ast, &buffs, pikaAsm); + pikaAsm = strsAppend(&buffs, pikaAsm, "0 RIS \n"); + is_block_matched = 1; + goto exit; + } +#endif if (obj_isArgExist(ast, "global")) { /* parse stmt ast */ pikaAsm = AST_appandPikaASM(ast, ast, &buffs, pikaAsm); @@ -2167,17 +2488,26 @@ char* AST_toPikaASM(AST* ast, Args* outBuffs) { is_block_matched = 1; goto exit; } + if (obj_isArgExist(ast, "del")) { + /* parse stmt ast */ + pikaAsm = AST_appandPikaASM(ast, ast, &buffs, pikaAsm); + pikaAsm = strsAppend(&buffs, pikaAsm, "0 DEL "); + pikaAsm = strsAppend(&buffs, pikaAsm, obj_getStr(ast, "del")); + pikaAsm = strsAppend(&buffs, pikaAsm, "\n"); + is_block_matched = 1; + goto exit; + } if (obj_isArgExist(ast, "break")) { /* parse stmt ast */ pikaAsm = AST_appandPikaASM(ast, ast, &buffs, pikaAsm); - pikaAsm = strsAppend(&buffs, pikaAsm, "0 BRK\n"); + pikaAsm = strsAppend(&buffs, pikaAsm, "0 BRK \n"); is_block_matched = 1; goto exit; } if (obj_isArgExist(ast, "continue")) { /* parse stmt ast */ pikaAsm = AST_appandPikaASM(ast, ast, &buffs, pikaAsm); - pikaAsm = strsAppend(&buffs, pikaAsm, "0 CTN\n"); + pikaAsm = strsAppend(&buffs, pikaAsm, "0 CTN \n"); is_block_matched = 1; goto exit; } @@ -2209,13 +2539,18 @@ ByteCodeFrame* byteCodeFrame_appendFromAsm(ByteCodeFrame* self, char* pikaAsm) { .line_pointer = pikaAsm, }; uint16_t const_pool_offset; - char* data; uint16_t exist_offset; - + int invoke_deepth_int = 0; for (int i = 0; i < strCountSign(pikaAsm, '\n'); i++) { Args buffs = {0}; char* line = strsGetLine(&buffs, asmer.line_pointer); - Arg* line_buff = arg_setStr(NULL, "", line); + char* data = NULL; + char ins_str[4] = ""; + char invoke_deepth[3] = ""; + uint8_t space_num = 0; + uint8_t invoke_deepth_i = 0; + uint8_t ins_str_i = 0; + Arg* line_buff = arg_newStr(line); strsDeinit(&buffs); line = arg_getStr(line_buff); InstructUnit ins_unit = {0}; @@ -2225,7 +2560,7 @@ ByteCodeFrame* byteCodeFrame_appendFromAsm(ByteCodeFrame* self, char* pikaAsm) { } /* process block deepth flag*/ if ('B' == line[0]) { - asmer.block_deepth_now = line[1] - '0'; + asmer.block_deepth_now = fast_atoi(line + 1); asmer.is_new_line = 1; goto next_line; } @@ -2235,7 +2570,27 @@ ByteCodeFrame* byteCodeFrame_appendFromAsm(ByteCodeFrame* self, char* pikaAsm) { /* get constPool offset */ const_pool_offset = 0; - data = line + 6; + for (int i = 0; i < (int)strGetSize(line); i++) { + if (space_num < 2) { + if (line[i] == ' ') { + space_num++; + if (space_num == 2) { + data = line + i + 1; + break; + } + continue; + } + } + if (space_num == 0) { + invoke_deepth[invoke_deepth_i++] = line[i]; + continue; + } + if (space_num == 1) { + ins_str[ins_str_i++] = line[i]; + continue; + } + } + exist_offset = constPool_getOffsetByData(&(self->const_pool), data); /* get const offset */ @@ -2252,11 +2607,12 @@ ByteCodeFrame* byteCodeFrame_appendFromAsm(ByteCodeFrame* self, char* pikaAsm) { const_pool_offset = exist_offset; } + invoke_deepth_int = fast_atoi(invoke_deepth); /* load Asm to byte code unit */ instructUnit_setBlockDeepth(&ins_unit, asmer.block_deepth_now); - instructUnit_setInvokeDeepth(&ins_unit, line[0] - '0'); + instructUnit_setInvokeDeepth(&ins_unit, invoke_deepth_int); instructUnit_setConstPoolIndex(&ins_unit, const_pool_offset); - instructUnit_setInstruct(&ins_unit, pikaVM_getInstructFromAsm(line)); + instructUnit_setInstruct(&ins_unit, pikaVM_getInstructFromAsm(ins_str)); if (asmer.is_new_line) { instructUnit_setIsNewLine(&ins_unit, 1); asmer.is_new_line = 0; @@ -2283,7 +2639,14 @@ void Parser_compilePyToBytecodeArray(char* lines) { bytecodeFrame_fromMultiLine(&bytecode_frame, lines); /* do something */ byteCodeFrame_print(&bytecode_frame); + + __platform_printf("\n\n/* clang-format off */\n"); + __platform_printf("PIKA_PYTHON(\n"); + __platform_printf("%s\n", lines); + __platform_printf(")\n"); + __platform_printf("/* clang-format on */\n"); byteCodeFrame_printAsArray(&bytecode_frame); /* deinit */ byteCodeFrame_deinit(&bytecode_frame); + __platform_printf("\n\n"); } diff --git a/port/cmsis-pack/pikascript/pikascript-core/PikaParser.h b/port/cmsis-pack/pikascript/pikascript-core/PikaParser.h index 521d92f35..fa262985f 100644 --- a/port/cmsis-pack/pikascript/pikascript-core/PikaParser.h +++ b/port/cmsis-pack/pikascript/pikascript-core/PikaParser.h @@ -46,9 +46,12 @@ enum StmtType { STMT_bytes, STMT_number, STMT_method, + STMT_chain, STMT_operator, STMT_import, STMT_list, + STMT_slice, + STMT_dict, STMT_none, }; @@ -60,6 +63,12 @@ struct Asmer { char* line_pointer; }; +typedef struct SyntaxItem { + char* astNodeName; + char* asmCode; + PIKA_BOOL isUseNodeValue; +} SyntaxItem; + typedef struct LexToken LexToken; struct LexToken { char* token; @@ -78,6 +87,7 @@ struct ParserState { Arg* last_token; Args* iter_buffs; Args* buffs_p; + PIKA_RES result; }; char* Parser_multiLineToAsm(Args* outBuffs, char* multiLine); @@ -92,7 +102,7 @@ char* Parser_parsePyLines(Args* outBuffs, char* py_lines); #define ParserState_forEach(parseState) \ ParserState_beforeIter(&parseState); \ - for (int i = 0; i < parseState.length; i++) + for (int __i = 0; __i < parseState.length; __i++) #define ParserState_forEachTokenExistPs(parseState, tokens) \ /* init parserStage */ \ @@ -101,9 +111,10 @@ char* Parser_parsePyLines(Args* outBuffs, ParserState_forEach(parseState) #define ParserState_forEachToken(parseState, tokens) \ - struct ParserState parseState; \ + struct ParserState parseState; \ ParserState_forEachTokenExistPs(parseState, tokens) uint16_t Tokens_getSize(char* tokens); +char* Parser_fileToAsm(Args* outBuffs, char* filename); #endif diff --git a/port/cmsis-pack/pikascript/pikascript-core/PikaPlatform.c b/port/cmsis-pack/pikascript/pikascript-core/PikaPlatform.c index 7f0d16e72..edc549182 100644 --- a/port/cmsis-pack/pikascript/pikascript-core/PikaPlatform.c +++ b/port/cmsis-pack/pikascript/pikascript-core/PikaPlatform.c @@ -101,6 +101,7 @@ PIKA_WEAK void __platform_wait(void) { while (1) { }; } + PIKA_WEAK void* __platform_memset(void* mem, int ch, size_t size) { return memset(mem, ch, size); } @@ -119,6 +120,7 @@ PIKA_WEAK char __platform_getchar(void) { #endif } +/* fopen */ PIKA_WEAK FILE* __platform_fopen(const char* filename, const char* modes) { #if defined(__linux) || defined(_WIN32) return fopen(filename, modes); @@ -129,6 +131,7 @@ PIKA_WEAK FILE* __platform_fopen(const char* filename, const char* modes) { #endif } +/* fclose */ PIKA_WEAK int __platform_fclose(FILE* stream) { #if defined(__linux) || defined(_WIN32) return fclose(stream); @@ -139,6 +142,7 @@ PIKA_WEAK int __platform_fclose(FILE* stream) { #endif } +/* fwrite */ PIKA_WEAK size_t __platform_fwrite(const void* ptr, size_t size, size_t n, @@ -152,6 +156,7 @@ PIKA_WEAK size_t __platform_fwrite(const void* ptr, #endif } +/* fread */ PIKA_WEAK size_t __platform_fread(void* ptr, size_t size, size_t n, @@ -164,3 +169,25 @@ PIKA_WEAK size_t __platform_fread(void* ptr, } #endif } + +/* fseek */ +PIKA_WEAK int __platform_fseek(FILE* stream, long offset, int whence) { +#if defined(__linux) || defined(_WIN32) + return fseek(stream, offset, whence); +#else + __platform_printf("[error]: __platform_fseek need implementation!\r\n"); + while (1) { + } +#endif +} + +/* ftell */ +PIKA_WEAK long __platform_ftell(FILE* stream) { +#if defined(__linux) || defined(_WIN32) + return ftell(stream); +#else + __platform_printf("[error]: __platform_ftell need implementation!\r\n"); + while (1) { + } +#endif +} diff --git a/port/cmsis-pack/pikascript/pikascript-core/PikaPlatform.h b/port/cmsis-pack/pikascript/pikascript-core/PikaPlatform.h index fbe953d7f..3f940db49 100644 --- a/port/cmsis-pack/pikascript/pikascript-core/PikaPlatform.h +++ b/port/cmsis-pack/pikascript/pikascript-core/PikaPlatform.h @@ -36,6 +36,18 @@ #include #include +/* clang-format off */ +#if PIKA_ASSERT_ENABLE + #define pika_assert(expr) \ + if(!(expr)) { \ + __platform_printf("Assertion failed: %s\nfile: %s:%d\n", #expr, __FILE__, __LINE__); \ + abort(); \ + } +#else + #define pika_assert(...) +#endif +/* clang-format on */ + /* Compiler */ #if defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 5000000) /* ARM Compiler \ */ @@ -65,23 +77,24 @@ #define __platform_printf(...) rt_kprintf(__VA_ARGS__) #endif -/* clang-format off */ typedef enum { - PIKA_RES_ERR_ARG_NO_FOUND = -12, - PIKA_RES_ERR_UNKNOWN_INSTRUCTION = -11, - PIKA_RES_ERR_OUT_OF_RANGE = -10, - PIKA_RES_ERR_IO_ERROR = -9, - PIKA_RES_ERR_INSUFFICIENT_RESOURCE = -8, - PIKA_RES_ERR_INVALID_PARAM = -7, - PIKA_RES_ERR_INVALID_PTR = -6, - PIKA_RES_ERR_UNALIGNED_PTR = -5, - PIKA_RES_ERR_INVALID_VERSION_NUMBER = -4, - PIKA_RES_ERR_ILLEGAL_MAGIC_CODE = -3, - PIKA_RES_ERR_OPERATION_FAILED = -2, - PIKA_RES_ERR_UNKNOWN = -1, - PIKA_RES_OK = 0, + PIKA_RES_OK = 0, + PIKA_RES_ERR_RUNTIME_ERROR, + PIKA_RES_ERR_ARG_NO_FOUND, + PIKA_RES_ERR_UNKNOWN_INSTRUCTION, + PIKA_RES_ERR_OUT_OF_RANGE, + PIKA_RES_ERR_IO_ERROR, + PIKA_RES_ERR_INSUFFICIENT_RESOURCE, + PIKA_RES_ERR_INVALID_PARAM, + PIKA_RES_ERR_INVALID_PTR, + PIKA_RES_ERR_UNALIGNED_PTR, + PIKA_RES_ERR_INVALID_VERSION_NUMBER, + PIKA_RES_ERR_ILLEGAL_MAGIC_CODE, + PIKA_RES_ERR_OPERATION_FAILED, + PIKA_RES_ERR_UNKNOWN, + PIKA_RES_ERR_SYNTAX_ERROR, + PIKA_RES_ERR_IO, } PIKA_RES; -/* clang-format on*/ /* clang-format off */ @@ -139,6 +152,8 @@ FILE* __platform_fopen(const char* filename, const char* modes); int __platform_fclose(FILE* stream); size_t __platform_fwrite(const void* ptr, size_t size, size_t n, FILE* stream); size_t __platform_fread(void* ptr, size_t size, size_t n, FILE* stream); +int __platform_fseek(FILE* stream, long offset, int whence); +long __platform_ftell(FILE* stream); /* error */ void __platform_error_handle(void); diff --git a/port/cmsis-pack/pikascript/pikascript-core/PikaVM.c b/port/cmsis-pack/pikascript/pikascript-core/PikaVM.c index f2d501d86..d3e6a36a1 100644 --- a/port/cmsis-pack/pikascript/pikascript-core/PikaVM.c +++ b/port/cmsis-pack/pikascript/pikascript-core/PikaVM.c @@ -32,6 +32,9 @@ #include "PikaParser.h" #include "PikaPlatform.h" #include "dataStrs.h" +#if PIKA_MATH_ENABLE +#include +#endif /* head declear start */ static uint8_t VMState_getInputArgNum(VMState* vs); @@ -40,7 +43,8 @@ static VMParameters* __pikaVM_runByteCodeFrameWithState( VMParameters* locals, VMParameters* globals, ByteCodeFrame* bytecode_frame, - uint16_t pc); + uint16_t pc, + TryInfo* try_info); /* head declear end */ @@ -71,16 +75,32 @@ static char* VMState_getConstWithInstructUnit(VMState* vs, instructUnit_getConstPoolIndex(ins_unit)); } -static int32_t VMState_getAddrOffsetOfJUM(VMState* vs) { +static int32_t VMState_getAddrOffsetOfJmpBack(VMState* vs) { int offset = 0; - InstructUnit* ins_unit_now = VMState_getInstructNow(vs); + int loop_deepth = -1; + + /* find loop deepth */ while (1) { - offset += instructUnit_getSize(ins_unit_now); - ins_unit_now = VMState_getInstructWithOffset(vs, offset); + offset -= instructUnit_getSize(ins_unit_now); + InstructUnit* ins_unit_now = VMState_getInstructWithOffset(vs, offset); uint16_t invoke_deepth = instructUnit_getInvokeDeepth(ins_unit_now); enum Instruct ins = instructUnit_getInstruct(ins_unit_now); char* data = VMState_getConstWithInstructUnit(vs, ins_unit_now); - if ((0 == invoke_deepth) && (JMP == ins) && strEqu(data, "-1")) { + if ((0 == invoke_deepth) && (JEZ == ins) && strEqu(data, "2")) { + loop_deepth = instructUnit_getBlockDeepth(ins_unit_now); + break; + } + } + + offset = 0; + while (1) { + offset += instructUnit_getSize(ins_unit_now); + InstructUnit* ins_unit_now = VMState_getInstructWithOffset(vs, offset); + enum Instruct ins = instructUnit_getInstruct(ins_unit_now); + char* data = VMState_getConstWithInstructUnit(vs, ins_unit_now); + int block_deepth_now = instructUnit_getBlockDeepth(ins_unit_now); + if ((block_deepth_now == loop_deepth) && (JMP == ins) && + strEqu(data, "-1")) { return offset; } } @@ -138,25 +158,253 @@ static int32_t VMState_getAddrOffsetFromJmp(VMState* vs) { } static int32_t VMState_getAddrOffsetOfBreak(VMState* vs) { - int32_t offset = VMState_getAddrOffsetOfJUM(vs); + int32_t offset = VMState_getAddrOffsetOfJmpBack(vs); /* byteCode */ offset += instructUnit_getSize(); return offset; } +static int32_t VMState_getAddrOffsetOfRaise(VMState* vs) { + int offset = 0; + InstructUnit* ins_unit_now = VMState_getInstructNow(vs); + while (1) { + offset += instructUnit_getSize(ins_unit_now); + ins_unit_now = VMState_getInstructWithOffset(vs, offset); + enum Instruct ins = instructUnit_getInstruct(ins_unit_now); + if ((NTR == ins)) { + return offset; + } + } +} + static int32_t VMState_getAddrOffsetOfContinue(VMState* vs) { - int32_t offset = VMState_getAddrOffsetOfJUM(vs); + int32_t offset = VMState_getAddrOffsetOfJmpBack(vs); /* byteCode */ return offset; } -typedef Arg* (*VM_instruct_handler)(PikaObj* self, VMState* vs, char* data); +static void VMState_delLReg(VMState* vs, uint8_t index) { + PikaObj* obj = vs->lreg[index]; + if (NULL != obj) { + obj_refcntDec(obj); + vs->lreg[index] = NULL; + if (0 == obj_refcntNow(obj)) { + obj_deinit(obj); + } + } +} -static Arg* VM_instruction_handler_NON(PikaObj* self, VMState* vs, char* data) { +static void VMState_initReg(VMState* vs) { + for (uint8_t i = 0; i < PIKA_REGIST_SIZE; i++) { + vs->lreg[i] = NULL; + vs->ireg[i] = 0; + } +} + +static PIKA_BOOL _checkLReg(char* data) { + if ((data[0] == '_') && (data[1] == 'l') && (data[2] >= '0') && + (data[2] <= '9')) { + return PIKA_TRUE; + } + return PIKA_FALSE; +} + +static uint8_t _getLRegIndex(char* data) { + return data[2] - '0'; +} + +static void VMState_setLReg(VMState* vs, uint8_t index, PikaObj* obj) { + obj_refcntInc(obj); + vs->lreg[index] = obj; +} + +typedef Arg* (*VM_instruct_handler)(PikaObj* self, + VMState* vs, + char* data, + Arg* arg_ret_reg); + +static Arg* VM_instruction_handler_NON(PikaObj* self, + VMState* vs, + char* data, + Arg* arg_ret_reg) { return NULL; } -static Arg* VM_instruction_handler_NEW(PikaObj* self, VMState* vs, char* data) { +Arg* __vm_get(PikaObj* self, Arg* key, Arg* obj) { + ArgType obj_type = arg_getType(obj); + int index = 0; + if (ARG_TYPE_INT == arg_getType(key)) { + index = arg_getInt(key); + } + if (ARG_TYPE_STRING == obj_type) { + char* str_pyload = arg_getStr(obj); + char char_buff[] = " "; + if (index < 0) { + index = strGetSize(str_pyload) + index; + } + char_buff[0] = str_pyload[index]; + return arg_newStr(char_buff); + } + if (ARG_TYPE_BYTES == obj_type) { + uint8_t* bytes_pyload = arg_getBytes(obj); + uint8_t byte_buff[] = " "; + if (index < 0) { + index = arg_getBytesSize(obj) + index; + } + byte_buff[0] = bytes_pyload[index]; + return arg_newBytes(byte_buff, 1); + } + if (argType_isObject(obj_type)) { + PikaObj* arg_obj = arg_getPtr(obj); + obj_setArg(arg_obj, "__key", key); + /* clang-format off */ + PIKA_PYTHON( + __res = __getitem__(__key) + ) + /* clang-format on */ + const uint8_t bytes[] = { + 0x0c, 0x00, /* instruct array size */ + 0x10, 0x81, 0x01, 0x00, 0x00, 0x02, 0x07, 0x00, 0x00, 0x04, 0x13, + 0x00, + /* instruct array */ + 0x19, 0x00, /* const pool size */ + 0x00, 0x5f, 0x5f, 0x6b, 0x65, 0x79, 0x00, 0x5f, 0x5f, 0x67, 0x65, + 0x74, 0x69, 0x74, 0x65, 0x6d, 0x5f, 0x5f, 0x00, 0x5f, 0x5f, 0x72, + 0x65, 0x73, 0x00, + /* const pool */ + }; + pikaVM_runByteCode(arg_obj, (uint8_t*)bytes); + return arg_copy(args_getArg(arg_obj->list, "__res")); + } + return arg_newNull(); +} + +Arg* __vm_slice(PikaObj* self, Arg* end, Arg* obj, Arg* start, int step) { +#if PIKA_SYNTAX_SLICE_ENABLE + /* No interger index only support __getitem__ */ + if (!(arg_getType(start) == ARG_TYPE_INT && + arg_getType(end) == ARG_TYPE_INT)) { + return __vm_get(self, start, obj); + } + + int start_i = arg_getInt(start); + int end_i = arg_getInt(end); + + /* __slice__ is equal to __getitem__ */ + if (end_i - start_i == 1) { + return __vm_get(self, start, obj); + } + + if (ARG_TYPE_STRING == arg_getType(obj)) { + size_t len = strGetSize(arg_getStr(obj)); + if (start_i < 0) { + start_i += len; + } + if (end_i < 0) { + end_i += len + 1; + } + Arg* sliced_arg = arg_newStr(""); + for (int i = start_i; i < end_i; i++) { + Arg* i_arg = arg_newInt(i); + Arg* item_arg = __vm_get(self, i_arg, obj); + sliced_arg = arg_strAppend(sliced_arg, arg_getStr(item_arg)); + arg_deinit(item_arg); + arg_deinit(i_arg); + } + return sliced_arg; + } + + if (ARG_TYPE_BYTES == arg_getType(obj)) { + size_t len = arg_getBytesSize(obj); + if (start_i < 0) { + start_i += len; + } + if (end_i < 0) { + end_i += len + 1; + } + Arg* sliced_arg = arg_newBytes(NULL, 0); + for (int i = start_i; i < end_i; i++) { + Arg* i_arg = arg_newInt(i); + Arg* item_arg = __vm_get(self, i_arg, obj); + uint8_t* bytes_origin = arg_getBytes(sliced_arg); + size_t size_origin = arg_getBytesSize(sliced_arg); + Arg* sliced_arg_new = arg_newBytes(NULL, size_origin + 1); + __platform_memcpy(arg_getBytes(sliced_arg_new), bytes_origin, + size_origin); + __platform_memcpy(arg_getBytes(sliced_arg_new) + size_origin, + arg_getBytes(item_arg), 1); + arg_deinit(sliced_arg); + sliced_arg = sliced_arg_new; + arg_deinit(item_arg); + arg_deinit(i_arg); + } + return sliced_arg; + } + return arg_newNull(); +#else + return __vm_get(self, start, obj); +#endif +} + +static Arg* VM_instruction_handler_SLC(PikaObj* self, + VMState* vs, + char* data, + Arg* arg_ret_reg) { +#if PIKA_SYNTAX_SLICE_ENABLE + int arg_num_input = VMState_getInputArgNum(vs); + if (arg_num_input < 2) { + return arg_newNull(); + } + if (arg_num_input == 2) { + Arg* key = stack_popArg_alloc(&vs->stack); + Arg* obj = stack_popArg_alloc(&vs->stack); + Arg* res = __vm_get(self, key, obj); + arg_deinit(key); + arg_deinit(obj); + return res; + } + if (arg_num_input == 3) { + Arg* end = stack_popArg_alloc(&vs->stack); + Arg* start = stack_popArg_alloc(&vs->stack); + Arg* obj = stack_popArg_alloc(&vs->stack); + Arg* res = __vm_slice(self, end, obj, start, 1); + arg_deinit(end); + arg_deinit(obj); + arg_deinit(start); + return res; + } + return arg_newNull(); +#else + Arg* key = stack_popArg_alloc(&vs->stack); + Arg* obj = stack_popArg_alloc(&vs->stack); + Arg* res = __vm_get(self, key, obj); + arg_deinit(key); + arg_deinit(obj); + return res; +#endif +} + +static Arg* VM_instruction_handler_TRY(PikaObj* self, + VMState* vs, + char* data, + Arg* arg_ret_reg) { + pika_assert(NULL != vs->try_info); + vs->try_info->try_state = TRY_STATE_TOP; + return NULL; +} + +static Arg* VM_instruction_handler_NTR(PikaObj* self, + VMState* vs, + char* data, + Arg* arg_ret_reg) { + vs->try_info->try_state = TRY_STATE_NONE; + return NULL; +} + +static Arg* VM_instruction_handler_NEW(PikaObj* self, + VMState* vs, + char* data, + Arg* arg_ret_reg) { Arg* origin_arg = obj_getArg(vs->locals, data); Arg* new_arg = arg_copy(origin_arg); origin_arg = arg_setType(origin_arg, ARG_TYPE_OBJECT); @@ -164,26 +412,56 @@ static Arg* VM_instruction_handler_NEW(PikaObj* self, VMState* vs, char* data) { return new_arg; } -static Arg* VM_instruction_handler_REF(PikaObj* self, VMState* vs, char* data) { +static Arg* VM_instruction_handler_REF(PikaObj* self, + VMState* vs, + char* data, + Arg* arg_ret_reg) { if (strEqu(data, (char*)"True")) { - return arg_setInt(NULL, "", 1); + return arg_setInt(arg_ret_reg, "", 1); } if (strEqu(data, (char*)"False")) { - return arg_setInt(NULL, "", 0); + return arg_setInt(arg_ret_reg, "", 0); } - /* find in local list first */ - Arg* arg = arg_copy(obj_getArg(vs->locals, data)); - if (NULL == arg) { - /* find in global list second */ - arg = arg_copy(obj_getArg(vs->globals, data)); + if (strEqu(data, (char*)"None")) { + return arg_setNull(arg_ret_reg); } + if (strEqu(data, (char*)"RuntimeError")) { + return arg_setInt(arg_ret_reg, "", PIKA_RES_ERR_RUNTIME_ERROR); + } + Arg* arg = NULL; + if (data[0] == '.') { + /* find host from stack */ + Arg* host_obj = stack_popArg_alloc(&(vs->stack)); + if (argType_isObject(arg_getType(host_obj))) { + arg = arg_copy_noalloc(obj_getArg(arg_getPtr(host_obj), data + 1), + arg_ret_reg); + } + arg_deinit(host_obj); + } else { + /* find in local list first */ + arg = arg_copy_noalloc(obj_getArg(vs->locals, data), arg_ret_reg); + if (NULL == arg) { + /* find in global list second */ + arg = arg_copy_noalloc(obj_getArg(vs->globals, data), arg_ret_reg); + } + } + if (NULL == arg) { - VMState_setErrorCode(vs, 1); + VMState_setErrorCode(vs, PIKA_RES_ERR_ARG_NO_FOUND); __platform_printf("NameError: name '%s' is not defined\r\n", data); } return arg; } +static Arg* VM_instruction_handler_GER(PikaObj* self, + VMState* vs, + char* data, + Arg* arg_ret_reg) { + PIKA_RES err = (PIKA_RES)vs->try_error_code; + Arg* err_arg = arg_newInt(err); + 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); @@ -192,36 +470,42 @@ static int32_t __foreach_handler_deinitTuple(Arg* argEach, Args* context) { return PIKA_RES_OK; } -static Arg* VMState_runMethodArg(VMState* vs, - PikaObj* method_host_obj, - PikaObj* method_args_obj, - Arg* method_arg) { +Arg* _obj_runMethodArgWithState(PikaObj* self, + PikaObj* method_args_obj, + Arg* method_arg, + TryInfo* try_state, + Arg* ret_arg_reg) { + pika_assert(NULL != try_state); Arg* return_arg = NULL; /* get method Ptr */ Method method_ptr = methodArg_getPtr(method_arg); /* get method type list */ ArgType method_type = arg_getType(method_arg); /* error */ - if (ARG_TYPE_VOID == method_type) { + if (ARG_TYPE_NONE == method_type) { return NULL; } ByteCodeFrame* method_bytecodeFrame = methodArg_getBytecodeFrame(method_arg); - obj_setErrorCode(method_host_obj, 0); + PikaObj* method_context = methodArg_getDefContext(method_arg); + if (NULL != method_context) { + self = method_context; + } + obj_setErrorCode(self, PIKA_RES_OK); /* run method */ if (method_type == ARG_TYPE_METHOD_NATIVE) { /* native method */ - method_ptr(method_host_obj, method_args_obj->list); + method_ptr(self, method_args_obj->list); /* get method return */ - return_arg = - arg_copy(args_getArg(method_args_obj->list, (char*)"return")); + return_arg = arg_copy_noalloc( + args_getArg(method_args_obj->list, (char*)"return"), ret_arg_reg); } else if (method_type == ARG_TYPE_METHOD_NATIVE_CONSTRUCTOR) { /* native method */ - method_ptr(method_host_obj, method_args_obj->list); + method_ptr(self, method_args_obj->list); /* get method return */ - return_arg = - arg_copy(args_getArg(method_args_obj->list, (char*)"return")); + return_arg = arg_copy_noalloc( + args_getArg(method_args_obj->list, (char*)"return"), ret_arg_reg); } else { /* static method and object method */ /* byteCode */ @@ -229,35 +513,73 @@ static Arg* VMState_runMethodArg(VMState* vs, &(method_bytecodeFrame->instruct_array), 0); uint16_t pc = (uintptr_t)method_ptr - insturctArray_start; method_args_obj = __pikaVM_runByteCodeFrameWithState( - method_host_obj, method_args_obj, vs->globals, method_bytecodeFrame, - pc); + self, method_args_obj, self, method_bytecodeFrame, pc, try_state); /* get method return */ - return_arg = - arg_copy(args_getArg(method_args_obj->list, (char*)"return")); + return_arg = arg_copy_noalloc( + args_getArg(method_args_obj->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) { + return _obj_runMethodArgWithState(self, method_args_obj, method_arg, + try_state, NULL); +} + +Arg* obj_runMethodArgWithState_noalloc(PikaObj* self, + PikaObj* method_args_obj, + Arg* method_arg, + TryInfo* try_state, + Arg* ret_arg_reg) { + return _obj_runMethodArgWithState(self, method_args_obj, method_arg, + try_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}; + return obj_runMethodArgWithState(self, method_args_obj, method_arg, + &try_info); +} + static int VMState_loadArgsFromMethodArg(VMState* vs, PikaObj* method_host_obj, Args* args, Arg* method_arg, char* method_name, int arg_num_used) { - Args buffs = {0}; + char _buffs1[PIKA_LINE_BUFF_SIZE / 2] = {0}; + char* buffs1 = (char*)_buffs1; + 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; uint8_t arg_num = 0; - + ArgType method_type = ARG_TYPE_UNDEF; + uint8_t arg_num_input = 0; + PikaTuple* tuple = NULL; + char* variable_tuple_name = NULL; + char* type_list_buff = NULL; + int variable_arg_start = 0; /* get method type list */ - char* type_list = methodArg_getTypeList(method_arg, &buffs); + char* type_list = + methodArg_getTypeList(method_arg, buffs1, sizeof(_buffs1)); if (NULL == type_list) { - goto exit; + __platform_printf( + "OverflowError: type list is too long, please use bigger " + "PIKA_LINE_BUFF_SIZE\r\n"); + while (1) + ; } - ArgType method_type = arg_getType(method_arg); + method_type = arg_getType(method_arg); /* check variable */ if (strIsContain(type_list, '*')) { @@ -274,7 +596,7 @@ static int VMState_loadArgsFromMethodArg(VMState* vs, /* delete the 'self' */ arg_num_dec--; } - uint8_t arg_num_input = VMState_getInputArgNum(vs); + arg_num_input = VMState_getInputArgNum(vs); /* check arg num */ if (method_type == ARG_TYPE_METHOD_NATIVE_CONSTRUCTOR || @@ -285,7 +607,7 @@ static int VMState_loadArgsFromMethodArg(VMState* vs, } else { /* check arg num decleard and input */ if (arg_num_dec != arg_num_input - arg_num_used) { - VMState_setErrorCode(vs, 3); + VMState_setErrorCode(vs, PIKA_RES_ERR_INVALID_PARAM); __platform_printf( "TypeError: %s() takes %d positional argument but %d were " "given\r\n", @@ -300,20 +622,19 @@ static int VMState_loadArgsFromMethodArg(VMState* vs, arg_num = arg_num_dec; } - PikaTuple* tuple = NULL; - char* variable_tuple_name = NULL; - - /* get variable tuple name */ - char* type_list_buff = strsCopy(&buffs, type_list); - int variable_arg_start = 0; - for (int i = 0; i < arg_num_dec; i++) { - char* arg_def = strPopLastToken(type_list_buff, ','); - if (strIsStartWith(arg_def, "*")) { - /* skip the '*' */ - variable_tuple_name = arg_def + 1; - variable_arg_start = arg_num_dec - i - 1; - is_get_variable_arg = PIKA_TRUE; - break; + if (strIsContain(type_list, '*')) { + /* get variable tuple name */ + type_list_buff = strCopy(buffs2, type_list); + variable_arg_start = 0; + for (int i = 0; i < arg_num_dec; i++) { + char* arg_def = strPopLastToken(type_list_buff, ','); + if (strIsStartWith(arg_def, "*")) { + /* skip the '*' */ + variable_tuple_name = arg_def + 1; + variable_arg_start = arg_num_dec - i - 1; + is_get_variable_arg = PIKA_TRUE; + break; + } } } @@ -335,9 +656,9 @@ static int VMState_loadArgsFromMethodArg(VMState* vs, arg_name = arg_def; } else { /* clear the variable arg name */ - arg_name = strsCopy(&buffs, ""); + arg_name = ""; } - Arg* call_arg = stack_popArg(&(vs->stack)); + Arg* call_arg = stack_popArg_alloc(&(vs->stack)); call_arg = arg_setName(call_arg, arg_name); /* load the variable arg */ if (PIKA_TRUE == is_get_variable_arg) { @@ -372,89 +693,226 @@ static int VMState_loadArgsFromMethodArg(VMState* vs, args_setArg(args, call_arg); } exit: - strsDeinit(&buffs); return arg_num; } -#if PIKA_BUILTIN_LIST_ENABLE -void PikaStdData_List_append(PikaObj* self, Arg* arg); -void PikaStdData_List___init__(PikaObj* self); +void __vm_List_append(PikaObj* self, Arg* arg) { + PikaList* list = obj_getPtr(self, "list"); + list_append(list, arg); +} + +void __vm_List___init__(PikaObj* self) { + if (!obj_isArgExist(self, "list")) { + PikaList* list = New_list(); + obj_setPtr(self, "list", list); + } +} + +#if PIKA_BUILTIN_STRUCT_ENABLE PikaObj* New_PikaStdData_List(Args* args); +PikaObj* New_PikaStdData_Tuple(Args* args); #endif -static Arg* VM_instruction_handler_LST(PikaObj* self, VMState* vs, char* data) { -#if PIKA_BUILTIN_LIST_ENABLE + +static Arg* _vm_create_list_or_tuple(PikaObj* self, + VMState* vs, + PIKA_BOOL is_list) { +#if PIKA_BUILTIN_STRUCT_ENABLE + NewFun constructor = is_list ? New_PikaStdData_List : New_PikaStdData_Tuple; uint8_t arg_num = VMState_getInputArgNum(vs); - PikaObj* list = newNormalObj(New_PikaStdData_List); - PikaStdData_List___init__(list); + PikaObj* list = newNormalObj(constructor); + __vm_List___init__(list); Stack stack = {0}; stack_init(&stack); /* load to local stack to change sort */ for (int i = 0; i < arg_num; i++) { - Arg* arg = stack_popArg(&(vs->stack)); + Arg* arg = stack_popArg_alloc(&(vs->stack)); stack_pushArg(&stack, arg); } for (int i = 0; i < arg_num; i++) { - Arg* arg = stack_popArg(&stack); - PikaStdData_List_append(list, arg); + Arg* arg = stack_popArg_alloc(&stack); + __vm_List_append(list, arg); arg_deinit(arg); } stack_deinit(&stack); - return arg_setPtr(NULL, "", ARG_TYPE_OBJECT, list); + return arg_newPtr(ARG_TYPE_OBJECT, list); #else - return VM_instruction_handler_NON(self, vs, data); + return VM_instruction_handler_NON(self, vs, "", NULL); #endif } -static Arg* VM_instruction_handler_RUN(PikaObj* self, VMState* vs, char* data) { - Args buffs = {0}; +static Arg* VM_instruction_handler_LST(PikaObj* self, + VMState* vs, + char* data, + Arg* arg_ret_reg) { + return _vm_create_list_or_tuple(self, vs, PIKA_TRUE); +} + +void __vm_Dict___init__(PikaObj* self) { + PikaDict* dict = New_dict(); + PikaDict* keys = New_dict(); + obj_setPtr(self, "dict", dict); + obj_setPtr(self, "_keys", keys); +} + +void __vm_Dict_set(PikaObj* self, Arg* arg, char* key) { + PikaDict* dict = obj_getPtr(self, "dict"); + PikaDict* keys = obj_getPtr(self, "_keys"); + Arg* arg_key = arg_setStr(NULL, key, key); + Arg* arg_new = arg_copy(arg); + arg_setName(arg_new, key); + dict_setArg(dict, arg_new); + dict_setArg(keys, arg_key); +} + +#if PIKA_BUILTIN_STRUCT_ENABLE +PikaObj* New_PikaStdData_Dict(Args* args); +#endif + +static Arg* VM_instruction_handler_DCT(PikaObj* self, + VMState* vs, + char* data, + Arg* arg_ret_reg) { +#if PIKA_BUILTIN_STRUCT_ENABLE + uint8_t arg_num = VMState_getInputArgNum(vs); + PikaObj* dict = newNormalObj(New_PikaStdData_Dict); + __vm_Dict___init__(dict); + Stack stack = {0}; + stack_init(&stack); + /* load to local stack to change sort */ + for (int i = 0; i < arg_num; i++) { + Arg* arg = stack_popArg_alloc(&(vs->stack)); + stack_pushArg(&stack, arg); + } + for (int i = 0; i < arg_num / 2; i++) { + Arg* key_arg = stack_popArg_alloc(&stack); + Arg* val_arg = stack_popArg_alloc(&stack); + __vm_Dict_set(dict, val_arg, arg_getStr(key_arg)); + arg_deinit(key_arg); + arg_deinit(val_arg); + } + stack_deinit(&stack); + return arg_newPtr(ARG_TYPE_OBJECT, dict); +#else + return VM_instruction_handler_NON(self, vs, data, arg_ret_reg); +#endif +} + +static Arg* VM_instruction_handler_RUN(PikaObj* self, + VMState* vs, + char* data, + Arg* arg_ret_reg) { Arg* return_arg = NULL; VMParameters* sub_locals = NULL; char* methodPath = data; - PikaObj* method_host_obj; + PikaObj* method_host_obj = NULL; Arg* method_arg = NULL; + Arg* host_arg = NULL; + PIKA_BOOL isClass = PIKA_FALSE; char* sys_out; int arg_num_used = 0; - /* return arg directly */ - if (strEqu(data, "")) { - return_arg = stack_popArg(&(vs->stack)); + arg_newReg(arg_reg1, 64); + TryInfo sub_try_info = {.try_state = TRY_STATE_NONE, + .try_result = TRY_RESULT_NONE}; + pika_assert(NULL != vs->try_info); + if (vs->try_info->try_state == TRY_STATE_TOP || + vs->try_error_code == TRY_STATE_INNER) { + sub_try_info.try_state = TRY_STATE_INNER; + } + + /* tuple or single arg */ + if (data[0] == 0) { + if (VMState_getInputArgNum(vs) < 2) { + /* return arg directly */ + Arg* arg1 = stack_popArg(&(vs->stack), &arg_reg1); + return_arg = arg_copy_noalloc(arg1, arg_ret_reg); + arg_deinit(arg1); + goto exit; + } + /* create a tuple */ + return_arg = _vm_create_list_or_tuple(self, vs, PIKA_FALSE); goto exit; } + /* return tiny obj */ if (strEqu(data, "TinyObj")) { return_arg = arg_newMetaObj(New_TinyObj); goto exit; } + /* get method host obj from reg */ + if (NULL == method_host_obj && _checkLReg(data)) { + uint8_t reg_index = _getLRegIndex(data); + method_host_obj = vs->lreg[reg_index]; + } + + /* get method host obj from stack */ + if (NULL == method_host_obj && methodPath[0] == '.') { + /* get method host obj from stack */ + Arg* stack_tmp[PIKA_ARG_NUM_MAX] = {0}; + int arg_num = VMState_getInputArgNum(vs); + if (arg_num > PIKA_ARG_NUM_MAX) { + __platform_printf( + "[ERROR] Too many args in RUN instruction, please use bigger " + "#define PIKA_ARG_NUM_MAX\n"); + while (1) { + } + } + for (int i = 0; i < arg_num; i++) { + stack_tmp[i] = stack_popArg_alloc(&(vs->stack)); + } + host_arg = stack_tmp[arg_num - 1]; + if (argType_isObject(arg_getType(host_arg))) { + method_host_obj = arg_getPtr(host_arg); + arg_num_used++; + } + /* push back other args to stack */ + for (int i = arg_num - 2; i >= 0; i--) { + stack_pushArg(&(vs->stack), stack_tmp[i]); + } + } + /* get method host obj from self */ - method_host_obj = obj_getHostObj(self, methodPath); + if (NULL == method_host_obj) { + method_host_obj = obj_getHostObjWithIsClass(self, methodPath, &isClass); + } + /* get method host obj from local scope */ if (NULL == method_host_obj) { - method_host_obj = obj_getHostObj(vs->locals, methodPath); + method_host_obj = + obj_getHostObjWithIsClass(vs->locals, methodPath, &isClass); } + + /* method host obj is not found */ if (NULL == method_host_obj) { /* error, not found object */ - VMState_setErrorCode(vs, 1); + VMState_setErrorCode(vs, PIKA_RES_ERR_ARG_NO_FOUND); __platform_printf("Error: method '%s' no found.\r\n", data); goto exit; } + /* get method in local */ - method_arg = obj_getMethodArg(method_host_obj, methodPath); + method_arg = + obj_getMethodArg_noalloc(method_host_obj, methodPath, &arg_reg1); if (NULL == method_arg) { /* get method in global */ - method_arg = obj_getMethodArg(vs->globals, methodPath); + method_arg = + obj_getMethodArg_noalloc(vs->globals, methodPath, &arg_reg1); } - /* assert method*/ - if (NULL == method_arg || ARG_TYPE_VOID == arg_getType(method_arg)) { + + /* assert method type */ + if (NULL == method_arg || ARG_TYPE_NONE == arg_getType(method_arg)) { /* error, method no found */ - VMState_setErrorCode(vs, 2); + VMState_setErrorCode(vs, PIKA_RES_ERR_ARG_NO_FOUND); __platform_printf("NameError: name '%s' is not defined\r\n", data); goto exit; } + /* create sub local scope */ sub_locals = New_PikaObj(); + /* load args from vmState to sub_local->list */ - arg_num_used = VMState_loadArgsFromMethodArg( - vs, method_host_obj, sub_locals->list, method_arg, data, 0); + arg_num_used += VMState_loadArgsFromMethodArg( + vs, method_host_obj, sub_locals->list, method_arg, data, arg_num_used); /* load args faild */ if (vs->error_code != 0) { @@ -462,15 +920,16 @@ static Arg* VM_instruction_handler_RUN(PikaObj* self, VMState* vs, char* data) { } /* run method arg */ - return_arg = - VMState_runMethodArg(vs, method_host_obj, sub_locals, method_arg); + return_arg = obj_runMethodArgWithState_noalloc( + method_host_obj, sub_locals, method_arg, &sub_try_info, arg_ret_reg); /* __init__() */ if (ARG_TYPE_OBJECT_NEW == arg_getType(return_arg)) { arg_setType(return_arg, ARG_TYPE_OBJECT); /* init object */ PikaObj* new_obj = arg_getPtr(return_arg); - Arg* method_arg = obj_getMethodArg(new_obj, "__init__"); + Arg* method_arg = + obj_getMethodArg_noalloc(new_obj, "__init__", &arg_reg1); PikaObj* sub_locals = New_PikaObj(); Arg* return_arg_init = NULL; if (NULL == method_arg) { @@ -482,8 +941,8 @@ static Arg* VM_instruction_handler_RUN(PikaObj* self, VMState* vs, char* data) { if (vs->error_code != 0) { goto init_exit; } - return_arg_init = - VMState_runMethodArg(vs, new_obj, sub_locals, method_arg); + return_arg_init = obj_runMethodArgWithState(new_obj, sub_locals, + method_arg, &sub_try_info); init_exit: if (NULL != return_arg_init) { arg_deinit(return_arg_init); @@ -497,10 +956,18 @@ static Arg* VM_instruction_handler_RUN(PikaObj* self, VMState* vs, char* data) { if (NULL != sys_out) { args_setSysOut(vs->locals->list, sys_out); } + /* transfer errCode */ if (0 != obj_getErrorCode(method_host_obj)) { /* method error */ - VMState_setErrorCode(vs, 6); + VMState_setErrorCode(vs, PIKA_RES_ERR_RUNTIME_ERROR); + } + + /* check try result */ + if (sub_try_info.try_result == TRY_RESULT_RAISE) { + /* try error */ + VMState_setErrorCode(vs, PIKA_RES_ERR_RUNTIME_ERROR); + vs->jmp = VM_JMP_RAISE; } goto exit; @@ -511,7 +978,14 @@ exit: if (NULL != sub_locals) { obj_deinit(sub_locals); } - strsDeinit(&buffs); + if (NULL != host_arg) { + arg_deinit(host_arg); + } + if (NULL != method_host_obj && isClass) { + /* class method */ + obj_deinit(method_host_obj); + } + return return_arg; } @@ -540,20 +1014,26 @@ static char* __get_transferd_str(Args* buffs, char* str, size_t* iout_p) { return transfered_str; } -static Arg* VM_instruction_handler_STR(PikaObj* self, VMState* vs, char* data) { +static Arg* VM_instruction_handler_STR(PikaObj* self, + VMState* vs, + char* data, + Arg* arg_ret_reg) { if (strIsContain(data, '\\')) { Args buffs = {0}; size_t i_out = 0; char* transfered_str = __get_transferd_str(&buffs, data, &i_out); - Arg* return_arg = New_arg(NULL); + Arg* return_arg = arg_ret_reg; return_arg = arg_setStr(return_arg, "", transfered_str); strsDeinit(&buffs); return return_arg; } - return arg_setStr(NULL, "", data); + return arg_setStr(arg_ret_reg, "", data); } -static Arg* VM_instruction_handler_BYT(PikaObj* self, VMState* vs, char* data) { +static Arg* VM_instruction_handler_BYT(PikaObj* self, + VMState* vs, + char* data, + Arg* arg_ret_reg) { if (strIsContain(data, '\\')) { Args buffs = {0}; size_t i_out = 0; @@ -564,18 +1044,34 @@ static Arg* VM_instruction_handler_BYT(PikaObj* self, VMState* vs, char* data) { strsDeinit(&buffs); return return_arg; } - return arg_setBytes(NULL, "", (uint8_t*)data, strGetSize(data)); + return arg_newBytes((uint8_t*)data, strGetSize(data)); } -static Arg* VM_instruction_handler_OUT(PikaObj* self, VMState* vs, char* data) { - Arg* outArg = stack_popArg(&(vs->stack)); +static Arg* VM_instruction_handler_OUT(PikaObj* self, + VMState* vs, + char* data, + Arg* arg_ret_reg) { + arg_newReg(outArg_reg, PIKA_ARG_BUFF_SIZE); + Arg* outArg = stack_popArg(&vs->stack, &outArg_reg); + // Arg* outArg = stack_popArg_alloc(&vs->stack); ArgType outArg_type = arg_getType(outArg); + + if (_checkLReg(data)) { + uint8_t index = _getLRegIndex(data); + if (argType_isObject(outArg_type)) { + PikaObj* obj = arg_getPtr(outArg); + VMState_setLReg(vs, index, obj); + arg_deinit(outArg); + } + return NULL; + } + PikaObj* hostObj = vs->locals; /* match global_list */ if (args_isArgExist(vs->locals->list, "__gl")) { char* global_list = args_getStr(vs->locals->list, "__gl"); /* use a arg as buff */ - Arg* global_list_arg = arg_setStr(NULL, "", global_list); + Arg* global_list_arg = arg_newStr(global_list); char* global_list_buff = arg_getStr(global_list_arg); /* for each arg arg in global_list */ char token_buff[32] = {0}; @@ -603,7 +1099,10 @@ static Arg* VM_instruction_handler_OUT(PikaObj* self, VMState* vs, char* data) { } /* run as */ -static Arg* VM_instruction_handler_RAS(PikaObj* self, VMState* vs, char* data) { +static Arg* VM_instruction_handler_RAS(PikaObj* self, + VMState* vs, + char* data, + Arg* arg_ret_reg) { if (strEqu(data, "$origin")) { /* use origin object to run */ obj_removeArg(vs->locals, "__runAs"); @@ -615,8 +1114,11 @@ static Arg* VM_instruction_handler_RAS(PikaObj* self, VMState* vs, char* data) { return NULL; } -static Arg* VM_instruction_handler_NUM(PikaObj* self, VMState* vs, char* data) { - Arg* numArg = New_arg(NULL); +static Arg* VM_instruction_handler_NUM(PikaObj* self, + VMState* vs, + char* data, + Arg* arg_ret_reg) { + Arg* numArg = arg_ret_reg; /* hex */ if (data[1] == 'x' || data[1] == 'X') { return arg_setInt(numArg, "", strtol(data, NULL, 0)); @@ -627,6 +1129,11 @@ static Arg* VM_instruction_handler_NUM(PikaObj* self, VMState* vs, char* data) { __platform_memcpy(strtol_buff + 1, data + 2, strGetSize(data) - 2); return arg_setInt(numArg, "", strtol(strtol_buff, NULL, 0)); } + if (data[1] == 'b' || data[1] == 'B') { + char strtol_buff[10] = {0}; + __platform_memcpy(strtol_buff, data + 2, strGetSize(data) - 2); + return arg_setInt(numArg, "", strtol(strtol_buff, NULL, 2)); + } /* float */ if (strIsContain(data, '.')) { return arg_setFloat(numArg, "", atof(data)); @@ -635,30 +1142,55 @@ static Arg* VM_instruction_handler_NUM(PikaObj* self, VMState* vs, char* data) { return arg_setInt(numArg, "", fast_atoi(data)); } -static Arg* VM_instruction_handler_JMP(PikaObj* self, VMState* vs, char* data) { +static Arg* VM_instruction_handler_JMP(PikaObj* self, + VMState* vs, + char* data, + Arg* arg_ret_reg) { vs->jmp = fast_atoi(data); return NULL; } -static Arg* VM_instruction_handler_JEZ(PikaObj* self, VMState* vs, char* data) { +static Arg* VM_instruction_handler_SER(PikaObj* self, + VMState* vs, + char* data, + Arg* arg_ret_reg) { + vs->try_error_code = fast_atoi(data); + return NULL; +} + +static Arg* VM_instruction_handler_JEZ(PikaObj* self, + VMState* vs, + char* data, + Arg* arg_ret_reg) { int thisBlockDeepth; thisBlockDeepth = VMState_getBlockDeepthNow(vs); - Arg* assertArg = stack_popArg(&(vs->stack)); - int assert = arg_getInt(assertArg); - arg_deinit(assertArg); - char __else[] = "__else0"; - __else[6] = '0' + thisBlockDeepth; - args_setInt(self->list, __else, !assert); - if (0 == assert) { - /* set __else flag */ - vs->jmp = fast_atoi(data); + int jmp_expect = fast_atoi(data); + arg_newReg(pika_assertArg_reg, PIKA_ARG_BUFF_SIZE); + Arg* pika_assertArg = stack_popArg(&(vs->stack), &pika_assertArg_reg); + int pika_assert = 0; + if (NULL != pika_assertArg) { + pika_assert = arg_getInt(pika_assertArg); } + arg_deinit(pika_assertArg); + vs->ireg[thisBlockDeepth] = !pika_assert; + + if (0 == pika_assert) { + /* jump */ + vs->jmp = jmp_expect; + } + + /* restore loop deepth */ + if (2 == jmp_expect && 0 == pika_assert) { + int block_deepth_now = VMState_getBlockDeepthNow(vs); + vs->loop_deepth = block_deepth_now; + } + return NULL; } static uint8_t VMState_getInputArgNum(VMState* vs) { InstructUnit* ins_unit_now = VMState_getInstructNow(vs); - uint8_t invode_deepth_this = instructUnit_getInvokeDeepth(ins_unit_now); + uint8_t invoke_deepth_this = instructUnit_getInvokeDeepth(ins_unit_now); int32_t pc_this = vs->pc; uint8_t num = 0; while (1) { @@ -668,32 +1200,37 @@ static uint8_t VMState_getInputArgNum(VMState* vs) { if (pc_this < 0) { break; } - if (invode_deepth == invode_deepth_this + 1) { + if (invode_deepth == invoke_deepth_this + 1) { num++; } if (instructUnit_getIsNewLine(ins_unit_now)) { break; } - if (invode_deepth <= invode_deepth_this) { + if (invode_deepth <= invoke_deepth_this) { break; } } return num; } -static Arg* VM_instruction_handler_OPT(PikaObj* self, VMState* vs, char* data) { - Arg* outArg = NULL; +static Arg* VM_instruction_handler_OPT(PikaObj* self, + VMState* vs, + char* data, + Arg* arg_ret_reg) { + Arg* outArg = arg_ret_reg; uint8_t input_arg_num = VMState_getInputArgNum(vs); - Arg* arg2 = NULL; Arg* arg1 = NULL; + Arg* arg2 = NULL; + arg_newReg(arg_reg1, PIKA_ARG_BUFF_SIZE); + arg_newReg(arg_reg2, PIKA_ARG_BUFF_SIZE); if (input_arg_num == 2) { /* tow input */ - arg2 = stack_popArg(&(vs->stack)); - arg1 = stack_popArg(&(vs->stack)); + arg2 = stack_popArg(&(vs->stack), &arg_reg2); + arg1 = stack_popArg(&(vs->stack), &arg_reg1); } else if (input_arg_num == 1) { /* only one input */ - arg2 = stack_popArg(&(vs->stack)); - arg1 = arg_setNull(NULL); + arg2 = stack_popArg(&(vs->stack), &arg_reg2); + arg1 = NULL; } ArgType type_arg1 = arg_getType(arg1); ArgType type_arg2 = arg_getType(arg2); @@ -765,7 +1302,7 @@ static Arg* VM_instruction_handler_OPT(PikaObj* self, VMState* vs, char* data) { } if (strEqu("/", data)) { if (0 == num2_f) { - VMState_setErrorCode(vs, 1); + VMState_setErrorCode(vs, PIKA_RES_ERR_OPERATION_FAILED); args_setSysOut(vs->locals->list, "ZeroDivisionError: division by zero"); outArg = NULL; @@ -783,23 +1320,62 @@ static Arg* VM_instruction_handler_OPT(PikaObj* self, VMState* vs, char* data) { goto OPT_exit; } if (strEqu("%", data)) { - outArg = arg_setInt(outArg, "", num1_i % num2_i); + if ((type_arg1 == ARG_TYPE_INT) && (type_arg2 == ARG_TYPE_INT)) { + outArg = arg_setInt(outArg, "", num1_i % num2_i); + goto OPT_exit; + } + VMState_setErrorCode(vs, PIKA_RES_ERR_OPERATION_FAILED); + __platform_printf( + "TypeError: unsupported operand type(s) for %: 'float'\n"); + outArg = NULL; goto OPT_exit; } if (strEqu("**", data)) { - float res = 1; - for (int i = 0; i < num2_i; i++) { - res = res * num1_f; + if (type_arg1 == ARG_TYPE_INT && type_arg2 == ARG_TYPE_INT) { + int res = 1; + for (int i = 0; i < num2_i; i++) { + res = res * num1_i; + } + outArg = arg_setInt(outArg, "", res); + goto OPT_exit; + } else if (type_arg1 == ARG_TYPE_FLOAT && type_arg2 == ARG_TYPE_INT) { + float res = 1; + for (int i = 0; i < num2_i; i++) { + res = res * num1_f; + } + outArg = arg_setFloat(outArg, "", res); + goto OPT_exit; + } else { +#if PIKA_MATH_ENABLE + float res = 1; + res = pow(num1_f, num2_f); + outArg = arg_setFloat(outArg, "", res); + goto OPT_exit; +#else + VMState_setErrorCode(vs, PIKA_RES_ERR_OPERATION_FAILED); + __platform_printf( + "Operation float ** float is not enabled, please set " + "PIKA_MATH_ENABLE\n"); +#endif } - outArg = arg_setFloat(outArg, "", res); - goto OPT_exit; } if (strEqu("//", data)) { - outArg = arg_setInt(outArg, "", num1_i / num2_i); + if ((type_arg1 == ARG_TYPE_INT) && (type_arg2 == ARG_TYPE_INT)) { + outArg = arg_setInt(outArg, "", num1_i / num2_i); + goto OPT_exit; + } + VMState_setErrorCode(vs, PIKA_RES_ERR_OPERATION_FAILED); + __platform_printf( + "TypeError: unsupported operand type(s) for //: 'float'\n"); + outArg = NULL; goto OPT_exit; } if (strEqu("==", data) || strEqu("!=", data)) { int8_t is_equ = -1; + if (type_arg1 == ARG_TYPE_NONE && type_arg2 == ARG_TYPE_NONE) { + is_equ = 1; + goto EQU_exit; + } /* type not equl, and type is not int or float */ if (type_arg1 != type_arg2) { if ((type_arg1 != ARG_TYPE_FLOAT) && (type_arg1 != ARG_TYPE_INT)) { @@ -857,23 +1433,58 @@ static Arg* VM_instruction_handler_OPT(PikaObj* self, VMState* vs, char* data) { goto OPT_exit; } if (strEqu("&", data)) { - outArg = arg_setInt(outArg, "", num1_i & num2_i); + if ((type_arg1 == ARG_TYPE_INT) && (type_arg2 == ARG_TYPE_INT)) { + outArg = arg_setInt(outArg, "", num1_i & num2_i); + goto OPT_exit; + } + VMState_setErrorCode(vs, PIKA_RES_ERR_OPERATION_FAILED); + __platform_printf( + "TypeError: unsupported operand type(s) for &: 'float'\n"); + outArg = NULL; goto OPT_exit; } if (strEqu("|", data)) { - outArg = arg_setInt(outArg, "", num1_i | num2_i); + if ((type_arg1 == ARG_TYPE_INT) && (type_arg2 == ARG_TYPE_INT)) { + outArg = arg_setInt(outArg, "", num1_i | num2_i); + goto OPT_exit; + } + VMState_setErrorCode(vs, PIKA_RES_ERR_OPERATION_FAILED); + __platform_printf( + "TypeError: unsupported operand type(s) for |: 'float'\n"); + outArg = NULL; goto OPT_exit; } if (strEqu("~", data)) { - outArg = arg_setInt(outArg, "", ~num2_i); + if (type_arg2 == ARG_TYPE_INT) { + outArg = arg_setInt(outArg, "", ~num2_i); + goto OPT_exit; + } + VMState_setErrorCode(vs, PIKA_RES_ERR_OPERATION_FAILED); + __platform_printf( + "TypeError: unsupported operand type(s) for ~: 'float'\n"); + outArg = NULL; goto OPT_exit; } if (strEqu(">>", data)) { - outArg = arg_setInt(outArg, "", num1_i >> num2_i); + if ((type_arg1 == ARG_TYPE_INT) && (type_arg2 == ARG_TYPE_INT)) { + outArg = arg_setInt(outArg, "", num1_i >> num2_i); + goto OPT_exit; + } + VMState_setErrorCode(vs, PIKA_RES_ERR_OPERATION_FAILED); + __platform_printf( + "TypeError: unsupported operand type(s) for >>: 'float'\n"); + outArg = NULL; goto OPT_exit; } if (strEqu("<<", data)) { - outArg = arg_setInt(outArg, "", num1_i << num2_i); + if ((type_arg1 == ARG_TYPE_INT) && (type_arg2 == ARG_TYPE_INT)) { + outArg = arg_setInt(outArg, "", num1_i << num2_i); + goto OPT_exit; + } + VMState_setErrorCode(vs, PIKA_RES_ERR_OPERATION_FAILED); + __platform_printf( + "TypeError: unsupported operand type(s) for <<: 'float'\n"); + outArg = NULL; goto OPT_exit; } if (strEqu(" and ", data)) { @@ -925,15 +1536,15 @@ static Arg* __VM_instruction_handler_DEF(PikaObj* self, if (instructUnit_getBlockDeepth(ins_unit_now) == thisBlockDeepth + 1) { if (is_in_class) { class_defineObjectMethod(hostObj, data, (Method)ins_unit_now, - vs->bytecode_frame); + self, vs->bytecode_frame); } else { if (is_class) { class_defineRunTimeConstructor(hostObj, data, - (Method)ins_unit_now, + (Method)ins_unit_now, self, vs->bytecode_frame); } else { class_defineStaticMethod(hostObj, data, - (Method)ins_unit_now, + (Method)ins_unit_now, self, vs->bytecode_frame); } } @@ -945,62 +1556,119 @@ static Arg* __VM_instruction_handler_DEF(PikaObj* self, return NULL; } -static Arg* VM_instruction_handler_DEF(PikaObj* self, VMState* vs, char* data) { +static Arg* VM_instruction_handler_DEF(PikaObj* self, + VMState* vs, + char* data, + Arg* arg_ret_reg) { return __VM_instruction_handler_DEF(self, vs, data, 0); } -static Arg* VM_instruction_handler_CLS(PikaObj* self, VMState* vs, char* data) { +static Arg* VM_instruction_handler_CLS(PikaObj* self, + VMState* vs, + char* data, + Arg* arg_ret_reg) { return __VM_instruction_handler_DEF(self, vs, data, 1); } -static Arg* VM_instruction_handler_RET(PikaObj* self, VMState* vs, char* data) { +static Arg* VM_instruction_handler_RET(PikaObj* self, + VMState* vs, + char* data, + Arg* arg_ret_reg) { /* exit jmp signal */ - vs->jmp = -999; - Arg* return_arg = stack_popArg(&(vs->stack)); + vs->jmp = VM_JMP_EXIT; + Arg* return_arg = stack_popArg_alloc(&(vs->stack)); method_returnArg(vs->locals->list, return_arg); return NULL; } -static Arg* VM_instruction_handler_NEL(PikaObj* self, VMState* vs, char* data) { +static Arg* VM_instruction_handler_RIS(PikaObj* self, + VMState* vs, + char* data, + Arg* arg_ret_reg) { + Arg* err_arg = stack_popArg_alloc(&(vs->stack)); + PIKA_RES err = (PIKA_RES)arg_getInt(err_arg); + VMState_setErrorCode(vs, err); + arg_deinit(err_arg); + /* raise jmp */ + if (vs->try_info->try_state == TRY_STATE_TOP) { + vs->jmp = VM_JMP_RAISE; + } else if (vs->try_info->try_state == TRY_STATE_INNER) { + vs->try_info->try_result = TRY_RESULT_RAISE; + return VM_instruction_handler_RET(self, vs, data, arg_ret_reg); + } + return NULL; +} + +static Arg* VM_instruction_handler_NEL(PikaObj* self, + VMState* vs, + char* data, + Arg* arg_ret_reg) { int thisBlockDeepth = VMState_getBlockDeepthNow(vs); - char __else[] = "__else0"; - __else[6] = '0' + thisBlockDeepth; - if (0 == args_getInt(self->list, __else)) { + if (0 == vs->ireg[thisBlockDeepth]) { /* set __else flag */ vs->jmp = fast_atoi(data); } return NULL; } -static Arg* VM_instruction_handler_DEL(PikaObj* self, VMState* vs, char* data) { - obj_removeArg(vs->locals, data); +static Arg* VM_instruction_handler_DEL(PikaObj* self, + VMState* vs, + char* data, + Arg* arg_ret_reg) { + if (_checkLReg(data)) { + uint8_t reg_index = _getLRegIndex(data); + VMState_delLReg(vs, reg_index); + return NULL; + } + if (obj_isArgExist(vs->locals, data)) { + obj_removeArg(vs->locals, data); + return NULL; + } + if (obj_isArgExist(vs->globals, data)) { + obj_removeArg(vs->globals, data); + return NULL; + } + VMState_setErrorCode(vs, PIKA_RES_ERR_OPERATION_FAILED); + __platform_printf("NameError: name '%s' is not defined\n", data); return NULL; } -static Arg* VM_instruction_handler_EST(PikaObj* self, VMState* vs, char* data) { +static Arg* VM_instruction_handler_EST(PikaObj* self, + VMState* vs, + char* data, + Arg* arg_ret_reg) { Arg* arg = obj_getArg(vs->locals, data); if (arg == NULL) { - return arg_setInt(NULL, "", 0); + return arg_setInt(arg_ret_reg, "", 0); } - if (ARG_TYPE_NULL == arg_getType(arg)) { - return arg_setInt(NULL, "", 0); + if (ARG_TYPE_NONE == arg_getType(arg)) { + return arg_setInt(arg_ret_reg, "", 0); } - return arg_setInt(NULL, "", 1); + return arg_setInt(arg_ret_reg, "", 1); } -static Arg* VM_instruction_handler_BRK(PikaObj* self, VMState* vs, char* data) { +static Arg* VM_instruction_handler_BRK(PikaObj* self, + VMState* vs, + char* data, + Arg* arg_ret_reg) { /* break jmp signal */ - vs->jmp = -998; + vs->jmp = VM_JMP_BREAK; return NULL; } -static Arg* VM_instruction_handler_CTN(PikaObj* self, VMState* vs, char* data) { +static Arg* VM_instruction_handler_CTN(PikaObj* self, + VMState* vs, + char* data, + Arg* arg_ret_reg) { /* continue jmp signal */ - vs->jmp = -997; + vs->jmp = VM_JMP_CONTINUE; return NULL; } -static Arg* VM_instruction_handler_GLB(PikaObj* self, VMState* vs, char* data) { +static Arg* VM_instruction_handler_GLB(PikaObj* self, + VMState* vs, + char* data, + Arg* arg_ret_reg) { Arg* global_list_buff = NULL; char* global_list = args_getStr(vs->locals->list, "__gl"); /* create new global_list */ @@ -1009,7 +1677,7 @@ static Arg* VM_instruction_handler_GLB(PikaObj* self, VMState* vs, char* data) { goto exit; } /* append to exist global_list */ - global_list_buff = arg_setStr(NULL, "", global_list); + global_list_buff = arg_newStr(global_list); global_list_buff = arg_strAppend(global_list_buff, ","); global_list_buff = arg_strAppend(global_list_buff, data); args_setStr(vs->locals->list, "__gl", arg_getStr(global_list_buff)); @@ -1021,7 +1689,10 @@ exit: return NULL; } -static Arg* VM_instruction_handler_IMP(PikaObj* self, VMState* vs, char* data) { +static Arg* VM_instruction_handler_IMP(PikaObj* self, + VMState* vs, + char* data, + Arg* arg_ret_reg) { /* the module is already imported, skip. */ if (obj_isArgExist(self, data)) { return NULL; @@ -1036,7 +1707,7 @@ static Arg* VM_instruction_handler_IMP(PikaObj* self, VMState* vs, char* data) { if (0 == obj_importModule(self, data)) { return NULL; } - VMState_setErrorCode(vs, 3); + VMState_setErrorCode(vs, PIKA_RES_ERR_ARG_NO_FOUND); __platform_printf("ModuleNotFoundError: No module named '%s'\r\n", data); return NULL; } @@ -1046,7 +1717,7 @@ const VM_instruct_handler VM_instruct_handler_table[__INSTRCUTION_CNT] = { #include "__instruction_table.cfg" }; -enum Instruct pikaVM_getInstructFromAsm(char* line) { +enum Instruct pikaVM_getInstructFromAsm(char* ins_str) { #define __INS_COMPIRE #include "__instruction_table.cfg" return NON; @@ -1056,32 +1727,40 @@ static int pikaVM_runInstructUnit(PikaObj* self, VMState* vs, InstructUnit* ins_unit) { enum Instruct instruct = instructUnit_getInstruct(ins_unit); - Arg* return_arg; + arg_newReg(return_Arg_reg, PIKA_ARG_BUFF_SIZE); + Arg* return_arg = &return_Arg_reg; // char invode_deepth1_str[2] = {0}; int32_t pc_next = vs->pc + instructUnit_getSize(); char* data = VMState_getConstWithInstructUnit(vs, ins_unit); /* run instruct */ - return_arg = VM_instruct_handler_table[instruct](self, vs, data); + pika_assert(NULL != vs->try_info); + return_arg = + VM_instruct_handler_table[instruct](self, vs, data, &return_Arg_reg); if (NULL != return_arg) { stack_pushArg(&(vs->stack), return_arg); } goto nextLine; nextLine: /* exit */ - if (-999 == vs->jmp) { - pc_next = -99999; + if (VM_JMP_EXIT == vs->jmp) { + pc_next = VM_PC_EXIT; goto exit; } /* break */ - if (-998 == vs->jmp) { + if (VM_JMP_BREAK == vs->jmp) { pc_next = vs->pc + VMState_getAddrOffsetOfBreak(vs); goto exit; } /* continue */ - if (-997 == vs->jmp) { + if (VM_JMP_CONTINUE == vs->jmp) { pc_next = vs->pc + VMState_getAddrOffsetOfContinue(vs); goto exit; } + /* raise */ + if (VM_JMP_RAISE == vs->jmp) { + pc_next = vs->pc + VMState_getAddrOffsetOfRaise(vs); + goto exit; + } /* static jmp */ if (vs->jmp != 0) { pc_next = vs->pc + VMState_getAddrOffsetFromJmp(vs); @@ -1094,7 +1773,7 @@ exit: vs->jmp = 0; /* reach the end */ if (pc_next >= (int)VMState_getInstructArraySize(vs)) { - return -99999; + return VM_PC_EXIT; } return pc_next; } @@ -1177,10 +1856,14 @@ exit: return globals; } -VMParameters* pikaVM_runFile(PikaObj* self, char* filename) { - Arg* file_arg = arg_loadFile(NULL, filename); - char* lines = (char*)arg_getBytes(file_arg); +VMParameters* pikaVM_runSingleFile(PikaObj* self, char* filename) { Args buffs = {0}; + Arg* file_arg = arg_loadFile(NULL, filename); + pika_assert(NULL != file_arg); + if (NULL == file_arg) { + return NULL; + } + char* lines = (char*)arg_getBytes(file_arg); /* replace the "\r\n" to "\n" */ lines = strsReplace(&buffs, lines, "\r\n", "\n"); /* clear the void line */ @@ -1201,16 +1884,12 @@ VMParameters* pikaVM_runByteCode(PikaObj* self, uint8_t* bytecode) { return __pikaVM_runPyLines_or_byteCode(self, NULL, bytecode); } -static void* constPool_getStart(ConstPool* self) { - return self->content_start; -} - void constPool_update(ConstPool* self) { self->content_start = (void*)arg_getContent(self->arg_buff); } void constPool_init(ConstPool* self) { - self->arg_buff = arg_setStr(NULL, "", ""); + self->arg_buff = arg_newStr(""); constPool_update(self); self->content_offset_now = 0; self->size = strGetSize(constPool_getStart(self)) + 1; @@ -1244,10 +1923,6 @@ char* constPool_getNow(ConstPool* self) { (uintptr_t)(self->content_offset_now)); } -uint16_t constPool_getLastOffset(ConstPool* self) { - return self->size; -} - uint16_t constPool_getOffsetByData(ConstPool* self, char* data) { uint16_t ptr_befor = self->content_offset_now; /* set ptr_now to begin */ @@ -1328,7 +2003,7 @@ void byteCodeFrame_deinit(ByteCodeFrame* self) { } void instructArray_init(InstructArray* self) { - self->arg_buff = arg_setNull(NULL); + self->arg_buff = arg_newNull(); instructArray_update(self); self->size = 0; self->content_offset_now = 0; @@ -1359,10 +2034,6 @@ void instructUnit_init(InstructUnit* ins_unit) { ins_unit->isNewLine_instruct = 0; } -static void* instructArray_getStart(InstructArray* self) { - return self->content_start; -} - void instructArray_update(InstructArray* self) { self->content_start = (void*)arg_getContent(self->arg_buff); } @@ -1482,9 +2153,9 @@ void byteCodeFrame_print(ByteCodeFrame* self) { void VMState_solveUnusedStack(VMState* vs) { uint8_t top = stack_getTop(&(vs->stack)); for (int i = 0; i < top; i++) { - Arg* arg = stack_popArg(&(vs->stack)); + Arg* arg = stack_popArg_alloc(&(vs->stack)); ArgType type = arg_getType(arg); - if (type == ARG_TYPE_VOID) { + if (type == ARG_TYPE_NONE) { arg_deinit(arg); continue; } @@ -1493,10 +2164,9 @@ void VMState_solveUnusedStack(VMState* vs) { continue; } if (argType_isObject(type)) { - __platform_printf("\r\n", - (uintptr_t)arg_getPtr(arg)); - } - if (type == ARG_TYPE_INT) { + char* res = obj_toStr(arg_getPtr(arg)); + __platform_printf("%s\r\n", res); + } else if (type == ARG_TYPE_INT) { __platform_printf("%d\r\n", (int)arg_getInt(arg)); } else if (type == ARG_TYPE_FLOAT) { __platform_printf("%f\r\n", arg_getFloat(arg)); @@ -1504,6 +2174,9 @@ void VMState_solveUnusedStack(VMState* vs) { __platform_printf("%s\r\n", arg_getStr(arg)); } else if (type == ARG_TYPE_BYTES) { arg_printBytes(arg); + } else if (ARG_TYPE_POINTER == type || + ARG_TYPE_METHOD_NATIVE_CONSTRUCTOR) { + __platform_printf("%p\r\n", arg_getPtr(arg)); } arg_deinit(arg); } @@ -1514,7 +2187,9 @@ static VMParameters* __pikaVM_runByteCodeFrameWithState( VMParameters* locals, VMParameters* globals, ByteCodeFrame* bytecode_frame, - uint16_t pc) { + uint16_t pc, + TryInfo* try_info) { + pika_assert(NULL != try_info); int size = bytecode_frame->instruct_array.size; /* locals is the local scope */ VMState vs = { @@ -1523,12 +2198,16 @@ static VMParameters* __pikaVM_runByteCodeFrameWithState( .globals = globals, .jmp = 0, .pc = pc, - .error_code = 0, - .line_error_code = 0, + .loop_deepth = 0, + .error_code = PIKA_RES_OK, + .line_error_code = PIKA_RES_OK, + .try_error_code = PIKA_RES_OK, + .try_info = try_info, }; stack_init(&(vs.stack)); + VMState_initReg(&vs); while (vs.pc < size) { - if (vs.pc == -99999) { + if (vs.pc == VM_PC_EXIT) { break; } InstructUnit* this_ins_unit = VMState_getInstructNow(&vs); @@ -1549,18 +2228,23 @@ static VMParameters* __pikaVM_runByteCodeFrameWithState( } head_ins_unit--; } + if (vs.try_info->try_state) { + vs.try_error_code = vs.error_code; + } /* print inses of a line */ - while (1) { - if (head_ins_unit != this_ins_unit) { - __platform_printf(" "); - } else { - __platform_printf(" -> "); - } - instructUnit_printWithConst(head_ins_unit, - &(bytecode_frame->const_pool)); - head_ins_unit++; - if (head_ins_unit > this_ins_unit) { - break; + if (!vs.try_info->try_state) { + while (1) { + if (head_ins_unit != this_ins_unit) { + __platform_printf(" "); + } else { + __platform_printf(" -> "); + } + instructUnit_printWithConst(head_ins_unit, + &(bytecode_frame->const_pool)); + head_ins_unit++; + if (head_ins_unit > this_ins_unit) { + break; + } } } __platform_error_handle(); @@ -1574,12 +2258,11 @@ static VMParameters* __pikaVM_runByteCodeFrameWithState( 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; return __pikaVM_runByteCodeFrameWithState(self, self, self, byteCode_frame, - 0); -} - -char* constPool_getByOffset(ConstPool* self, uint16_t offset) { - return (char*)((uintptr_t)constPool_getStart(self) + (uintptr_t)offset); + 0, &try_info); } InstructUnit* instructArray_getByOffset(InstructArray* self, int32_t offset) { @@ -1627,3 +2310,22 @@ void byteCodeFrame_printAsArray(ByteCodeFrame* self) { __platform_printf("};\n"); __platform_printf("pikaVM_runByteCode(self, (uint8_t*)bytes);\n"); } + +PikaObj* pikaVM_runFile(PikaObj* self, char* file_name) { + Args buffs = {0}; + char* module_name = strsCopy(&buffs, file_name); + strPopLastToken(module_name, '.'); + + __platform_printf("(pikascript) pika compiler:\r\n"); + PikaMaker* maker = New_PikaMaker(); + pikaMaker_compileModuleWithDepends(maker, module_name); + pikaMaker_linkCompiledModules(maker, "pikaModules_cache.py.a"); + obj_deinit(maker); + __platform_printf("(pikascript) all succeed.\r\n\r\n"); + + pikaMemMaxReset(); + Obj_linkLibraryFile(self, "pikascript-api/pikaModules_cache.py.a"); + self = pikaVM_runSingleFile(self, file_name); + strsDeinit(&buffs); + return self; +} diff --git a/port/cmsis-pack/pikascript/pikascript-core/PikaVM.h b/port/cmsis-pack/pikascript/pikascript-core/PikaVM.h index 09276a54e..cecf710b6 100644 --- a/port/cmsis-pack/pikascript/pikascript-core/PikaVM.h +++ b/port/cmsis-pack/pikascript/pikascript-core/PikaVM.h @@ -38,6 +38,32 @@ enum Instruct { __INSTRCUTION_CNT, }; +typedef enum { + VM_JMP_EXIT = -999, + VM_JMP_CONTINUE = -997, + VM_JMP_BREAK = -998, + VM_JMP_RAISE = -996, +} VM_JMP; + +typedef enum { VM_PC_EXIT = -99999 } VM_PC; + +typedef enum { + TRY_STATE_NONE = 0, + TRY_STATE_TOP, + TRY_STATE_INNER, +} TRY_STATE; + +typedef enum { + TRY_RESULT_NONE = 0, + TRY_RESULT_RAISE, +} TRY_RESULT; + +typedef struct TryInfo TryInfo; +struct TryInfo { + TRY_STATE try_state; + TRY_RESULT try_result; +}; + typedef struct VMState VMState; struct VMState { VMParameters* locals; @@ -46,10 +72,16 @@ struct VMState { int32_t jmp; int32_t pc; ByteCodeFrame* bytecode_frame; + uint8_t loop_deepth; uint8_t error_code; uint8_t line_error_code; + uint8_t try_error_code; + PikaObj* lreg[PIKA_REGIST_SIZE]; + PIKA_BOOL ireg[PIKA_REGIST_SIZE]; + TryInfo* try_info; }; + VMParameters* pikaVM_run(PikaObj* self, char* pyLine); VMParameters* pikaVM_runAsm(PikaObj* self, char* pikaAsm); VMParameters* pikaVM_runByteCodeFrame(PikaObj* self, @@ -95,11 +127,15 @@ enum Instruct pikaVM_getInstructFromAsm(char* line); void constPool_init(ConstPool* self); void constPool_deinit(ConstPool* self); void constPool_append(ConstPool* self, char* content); + +#define constPool_getStart(self) ((self)->content_start) +#define constPool_getLastOffset(self) ((self)->size) +#define constPool_getByOffset(self, offset) \ + (char*)((uintptr_t)constPool_getStart((self)) + (uintptr_t)(offset)) + char* constPool_getNow(ConstPool* self); char* constPool_getNext(ConstPool* self); char* constPool_getByIndex(ConstPool* self, uint16_t index); -char* constPool_getByOffset(ConstPool* self, uint16_t offset); -uint16_t constPool_getLastOffset(ConstPool* self); void constPool_print(ConstPool* self); void byteCodeFrame_init(ByteCodeFrame* bf); @@ -118,6 +154,7 @@ InstructUnit* instructArray_getByOffset(InstructArray* self, int32_t offset); #define instructUnit_getSize(InstructUnit_p_self) ((size_t)sizeof(InstructUnit)) #define instructArray_getSize(InsturctArry_p_self) \ ((size_t)(InsturctArry_p_self)->size) +#define instructArray_getStart(InsturctArry_p_self) ((self)->content_start) uint16_t constPool_getOffsetByData(ConstPool* self, char* data); void instructArray_printWithConst(InstructArray* self, ConstPool* const_pool); @@ -131,6 +168,14 @@ void byteCodeFrame_init(ByteCodeFrame* self); VMParameters* pikaVM_runByteCode(PikaObj* self, uint8_t* bytecode); InstructUnit* instructArray_getNow(InstructArray* self); InstructUnit* instructArray_getNext(InstructArray* self); -VMParameters* pikaVM_runFile(PikaObj* self, char* filename); +VMParameters* pikaVM_runSingleFile(PikaObj* self, char* filename); +Arg* obj_runMethodArg(PikaObj* self, PikaObj* method_args_obj, Arg* method_arg); +PikaObj* pikaVM_runFile(PikaObj* self, char* file_name); +Arg* __vm_slice(PikaObj* self, Arg* end, Arg* obj, Arg* start, int step); +Arg* __vm_get(PikaObj* self, Arg* key, Arg* obj); +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); #endif diff --git a/port/cmsis-pack/pikascript/pikascript-core/PikaVersion.h b/port/cmsis-pack/pikascript/pikascript-core/PikaVersion.h new file mode 100644 index 000000000..ad651135b --- /dev/null +++ b/port/cmsis-pack/pikascript/pikascript-core/PikaVersion.h @@ -0,0 +1,5 @@ +#define PIKA_VERSION_MAJOR 1 +#define PIKA_VERSION_MINOR 10 +#define PIKA_VERSION_MICRO 0 + +#define PIKA_EDIT_TIME "2022/08/05 15:00:46" diff --git a/port/cmsis-pack/pikascript/pikascript-core/__instruction_def.h b/port/cmsis-pack/pikascript/pikascript-core/__instruction_def.h index 84c419c1d..b4e1d62da 100644 --- a/port/cmsis-pack/pikascript/pikascript-core/__instruction_def.h +++ b/port/cmsis-pack/pikascript/pikascript-core/__instruction_def.h @@ -37,7 +37,7 @@ #if defined(__INS_COMPIRE) #define def_ins(__INS_NAME) \ - if (0 == strncmp(line + 2, "" #__INS_NAME "", 3)) { \ + if (0 == strncmp(ins_str, "" #__INS_NAME "", 3)) { \ return __INS_NAME; \ } #endif diff --git a/port/cmsis-pack/pikascript/pikascript-core/__instruction_table.cfg b/port/cmsis-pack/pikascript/pikascript-core/__instruction_table.cfg index c72d64563..b52d5c936 100644 --- a/port/cmsis-pack/pikascript/pikascript-core/__instruction_table.cfg +++ b/port/cmsis-pack/pikascript/pikascript-core/__instruction_table.cfg @@ -30,26 +30,63 @@ //! just append ins to the end, insert ins would brake the pre-compiled //! bytecode. +/* none */ def_ins(NON) +/* get referance */ def_ins(REF) +/* run function */ def_ins(RUN) +/* string */ def_ins(STR) +/* output */ def_ins(OUT) +/* number */ def_ins(NUM) +/* jump */ def_ins(JMP) +/* jump qual zero */ def_ins(JEZ) +/* operator */ def_ins(OPT) +/* define */ def_ins(DEF) +/* return */ def_ins(RET) +/* not equal */ def_ins(NEL) +/* delete */ def_ins(DEL) +/* exist */ def_ins(EST) +/* break */ def_ins(BRK) +/* continue */ def_ins(CTN) +/* global */ def_ins(GLB) +/* run as */ def_ins(RAS) +/* new */ def_ins(NEW) +/* class */ def_ins(CLS) +/* bytes */ def_ins(BYT) +/* list */ def_ins(LST) +/* import */ def_ins(IMP) +/* try */ +def_ins(TRY) +/* not try */ +def_ins(NTR) +/* raise */ +def_ins(RIS) +/* get error code */ +def_ins(GER) +/* set error code */ +def_ins(SER) +/* dict */ +def_ins(DCT) +/* slice */ +def_ins(SLC) diff --git a/port/cmsis-pack/pikascript/pikascript-core/dataArg.c b/port/cmsis-pack/pikascript/pikascript-core/dataArg.c index 8e4461011..decda490c 100644 --- a/port/cmsis-pack/pikascript/pikascript-core/dataArg.c +++ b/port/cmsis-pack/pikascript/pikascript-core/dataArg.c @@ -32,7 +32,7 @@ #include "dataString.h" #include "stdlib.h" -uint16_t arg_getTotleSize(Arg* self) { +uint32_t arg_getTotleSize(Arg* self) { return arg_totleSize(self); } @@ -53,13 +53,15 @@ static Arg* arg_init_hash(Hash nameHash, uint32_t size, Arg* next) { Arg* self = (Arg*)pikaMalloc(sizeof(Arg) + size); - self->next = next; + arg_setNext(self, next); self->size = size; self->name_hash = nameHash; self->type = type; - __platform_memset(self->content, 0, aline_by(size, sizeof(uint32_t))); + self->serialized = PIKA_TRUE; + __platform_memset(arg_getContent(self), 0, + aline_by(size, sizeof(uint32_t))); if (NULL != content) { - __platform_memcpy(self->content, content, size); + __platform_memcpy(arg_getContent(self), content, size); } return self; @@ -68,33 +70,41 @@ static Arg* arg_init_hash(Hash nameHash, static Arg* arg_init(char* name, ArgType type, uint8_t* content, - uint16_t size, + uint32_t size, Arg* next) { Hash nameHash = hash_time33(name); return arg_init_hash(nameHash, type, content, size, next); } -uint16_t arg_totleSize(Arg* self) { +void arg_init_stack(Arg* self, uint8_t* buffer, uint32_t size) { + self->_.buffer = buffer; + self->size = size; + self->type = ARG_TYPE_UNDEF; + self->name_hash = 0; + self->serialized = PIKA_FALSE; +} + +uint32_t arg_totleSize(Arg* self) { return ((Arg*)self)->size + sizeof(Arg); } void arg_freeContent(Arg* self) { if (NULL != self) { - uint16_t totleSize = arg_totleSize(self); + uint32_t totleSize = arg_totleSize(self); pikaFree(self, totleSize); return; } } -Arg* arg_setContent(Arg* self, uint8_t* content, uint16_t size) { +Arg* arg_setContent(Arg* self, uint8_t* content, uint32_t size) { if (NULL == self) { /* malloc */ - return arg_init("", ARG_TYPE_VOID, content, size, NULL); + return arg_init("", ARG_TYPE_NONE, content, size, NULL); } /* only copy */ - if (arg_getSize(self) == size) { - __platform_memcpy(((Arg*)self)->content, content, size); + if (arg_getSize(self) >= size) { + __platform_memcpy(arg_getContent((Arg*)self), content, size); return self; } @@ -109,7 +119,7 @@ Arg* arg_setContent(Arg* self, uint8_t* content, uint16_t size) { Arg* arg_setNameHash(Arg* self, Hash nameHash) { if (NULL == self) { - return arg_init_hash(nameHash, ARG_TYPE_VOID, NULL, 0, NULL); + return arg_init_hash(nameHash, ARG_TYPE_NONE, NULL, 0, NULL); } Arg* arg = (Arg*)self; arg->name_hash = nameHash; @@ -130,6 +140,9 @@ Arg* arg_setType(Arg* self, ArgType type) { Arg* arg_setBytes(Arg* self, char* name, uint8_t* src, size_t size) { self = arg_newContent(self, size + sizeof(size_t) + 1); + if (NULL == self) { + return NULL; + } self = arg_setName(self, name); self = arg_setType(self, ARG_TYPE_BYTES); void* dir = arg_getContent(self); @@ -146,8 +159,8 @@ Arg* arg_setBytes(Arg* self, char* name, uint8_t* src, size_t size) { } Arg* arg_newContent(Arg* self, uint32_t size) { - Arg* newContent = arg_init("", ARG_TYPE_VOID, NULL, size, NULL); arg_freeContent(self); + Arg* newContent = arg_init("", ARG_TYPE_NONE, NULL, size, NULL); return newContent; } @@ -211,15 +224,28 @@ void* arg_getHeapStructDeinitFun(Arg* self) { } Arg* arg_setInt(Arg* self, char* name, int64_t val) { - return arg_init(name, ARG_TYPE_INT, (uint8_t*)&val, sizeof(val), NULL); + if (NULL == self) { + return arg_init(name, ARG_TYPE_INT, (uint8_t*)&val, sizeof(val), NULL); + } + self = arg_setContent(self, (uint8_t*)&val, sizeof(val)); + self = arg_setType(self, ARG_TYPE_INT); + self = arg_setName(self, name); + return self; } Arg* arg_setNull(Arg* self) { - return arg_init("", ARG_TYPE_NULL, NULL, 0, NULL); + return arg_init("", ARG_TYPE_NONE, NULL, 0, NULL); } Arg* arg_setFloat(Arg* self, char* name, double val) { - return arg_init(name, ARG_TYPE_FLOAT, (uint8_t*)&val, sizeof(val), NULL); + if (NULL == self) { + return arg_init(name, ARG_TYPE_FLOAT, (uint8_t*)&val, sizeof(val), + NULL); + } + self = arg_setContent(self, (uint8_t*)&val, sizeof(val)); + self = arg_setType(self, ARG_TYPE_FLOAT); + self = arg_setName(self, name); + return self; } double arg_getFloat(Arg* self) { @@ -227,7 +253,7 @@ double arg_getFloat(Arg* self) { return -999.999; } - return *(double*)self->content; + return *(double*)arg_getContent(self); } Arg* arg_setPtr(Arg* self, char* name, ArgType type, void* pointer) { @@ -235,22 +261,26 @@ Arg* arg_setPtr(Arg* self, char* name, ArgType type, void* pointer) { } Arg* arg_setStr(Arg* self, char* name, char* string) { + if (NULL == string) { + return NULL; + } return arg_init(name, ARG_TYPE_STRING, (uint8_t*)string, strGetSize(string) + 1, NULL); } int64_t arg_getInt(Arg* self) { + pika_assert(NULL!=self); if (NULL == arg_getContent(self)) { return -999999; } - return *(int64_t*)self->content; + return *(int64_t*)arg_getContent(self); } void* arg_getPtr(Arg* self) { if (NULL == arg_getContent(self)) { return NULL; } - return *(void**)self->content; + return *(void**)arg_getContent(self); } char* arg_getStr(Arg* self) { return (char*)arg_getContent(self); @@ -265,12 +295,12 @@ Hash arg_getNameHash(Arg* self) { ArgType arg_getType(Arg* self) { if (NULL == self) { - return ARG_TYPE_NULL; + return ARG_TYPE_NONE; } - return self->type; + return (ArgType)self->type; } -uint16_t arg_getContentSize(Arg* self) { +uint32_t arg_getContentSize(Arg* self) { return arg_getSize(self); } @@ -278,22 +308,45 @@ Arg* New_arg(void* voidPointer) { return NULL; } -Arg* arg_copy(Arg* argToBeCopy) { - if (NULL == argToBeCopy) { +Arg* arg_copy(Arg* arg_src) { + if (NULL == arg_src) { return NULL; } - ArgType arg_type = arg_getType(argToBeCopy); + ArgType arg_type = arg_getType(arg_src); if (ARG_TYPE_OBJECT == arg_type) { - obj_refcntInc(arg_getPtr(argToBeCopy)); + obj_refcntInc((PikaObj*)arg_getPtr(arg_src)); } Arg* argCopied = New_arg(NULL); - argCopied = arg_setContent(argCopied, arg_getContent(argToBeCopy), - arg_getContentSize(argToBeCopy)); - argCopied = arg_setNameHash(argCopied, arg_getNameHash(argToBeCopy)); - argCopied = arg_setType(argCopied, arg_getType(argToBeCopy)); + 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_copy_noalloc(Arg* arg_src, Arg* arg_dict) { + if (NULL == arg_src) { + return NULL; + } + if (NULL == arg_dict){ + return arg_copy(arg_src); + } + /* size is too big to be copied by noalloc */ + if (arg_getSize(arg_src) > arg_getSize(arg_dict)) { + return arg_copy(arg_src); + } + ArgType arg_type = arg_getType(arg_src); + if (ARG_TYPE_OBJECT == arg_type) { + obj_refcntInc((PikaObj*)arg_getPtr(arg_src)); + } + arg_dict->serialized = PIKA_FALSE; + 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)); + return arg_dict; +} + Arg* arg_append(Arg* self, void* new_content, size_t new_size) { uint8_t* old_content = arg_getContent(self); size_t old_size = arg_getContentSize(self); @@ -365,31 +418,14 @@ exit: } void arg_deinit(Arg* self) { + if (NULL == self) { + return; + } /* deinit arg pointed heap */ arg_deinitHeap(self); + if (!self->serialized) { + return; + } /* free the ref */ arg_freeContent(self); } - -Arg* arg_getNext(Arg* self) { - return self->next; -} - -uint16_t arg_getSize(Arg* self) { - return self->size; -} - -uint8_t* arg_getContent(Arg* self) { - return self->content; -} - -void arg_setNext(Arg* self, Arg* next) { - self->next = next; -} - -uint8_t argType_isObject(ArgType type) { - if (ARG_TYPE_OBJECT == type || ARG_TYPE_OBJECT_NEW == type) { - return 1; - } - return 0; -} diff --git a/port/cmsis-pack/pikascript/pikascript-core/dataArg.h b/port/cmsis-pack/pikascript/pikascript-core/dataArg.h index ecf459279..987044deb 100644 --- a/port/cmsis-pack/pikascript/pikascript-core/dataArg.h +++ b/port/cmsis-pack/pikascript/pikascript-core/dataArg.h @@ -35,8 +35,6 @@ typedef uint32_t Hash; typedef enum { ARG_TYPE_UNDEF = 0, ARG_TYPE_NONE, - ARG_TYPE_NULL, - ARG_TYPE_VOID, ARG_TYPE_INT, ARG_TYPE_FLOAT, ARG_TYPE_STRING, @@ -58,30 +56,34 @@ typedef enum { typedef void (*StructDeinitFun)(void* struct_); typedef struct Arg Arg; -struct Arg { +typedef union { Arg* next; - uint16_t size; + uint8_t* buffer; +} _arg_union; +struct Arg { + _arg_union _; + uint32_t size; uint8_t type; - uint8_t sub_type; + PIKA_BOOL serialized; Hash name_hash; uint8_t content[]; }; Arg* arg_getNext(Arg* self); -uint16_t arg_getSize(Arg* self); +uint32_t arg_getSize(Arg* self); uint8_t* arg_getContent(Arg* self); -uint16_t arg_totleSize(Arg* self); +uint32_t arg_totleSize(Arg* self); void arg_setNext(Arg* self, Arg* next); -uint16_t arg_getTotleSize(Arg* self); +uint32_t arg_getTotleSize(Arg* self); void arg_freeContent(Arg* self); Arg* arg_setName(Arg* self, char* name); -Arg* arg_setContent(Arg* self, uint8_t* content, uint16_t size); +Arg* arg_setContent(Arg* self, uint8_t* content, uint32_t size); Arg* arg_newContent(Arg* self, uint32_t size); Arg* arg_setType(Arg* self, ArgType type); Hash arg_getNameHash(Arg* self); ArgType arg_getType(Arg* self); -uint16_t arg_getContentSize(Arg* self); +uint32_t arg_getContentSize(Arg* self); Hash hash_time33(char* str); Arg* arg_setInt(Arg* self, char* name, int64_t val); @@ -89,6 +91,14 @@ Arg* arg_setFloat(Arg* self, char* name, double val); Arg* arg_setPtr(Arg* self, char* name, ArgType type, void* pointer); Arg* arg_setStr(Arg* self, char* name, char* string); Arg* arg_setNull(Arg* self); +Arg* arg_setBytes(Arg* self, char* name, uint8_t* src, size_t size); + +#define arg_newInt(val) arg_setInt(NULL, "", (val)) +#define arg_newFloat(val) arg_setFloat(NULL, "", (val)) +#define arg_newPtr(type, pointer) arg_setPtr(NULL, "", (type), (pointer)) +#define arg_newStr(string) arg_setStr(NULL, "", (string)) +#define arg_newNull() arg_setNull(NULL) +#define arg_newBytes(src, size) arg_setBytes(NULL, "", (src), (size)) int64_t arg_getInt(Arg* self); double arg_getFloat(Arg* self); @@ -97,6 +107,7 @@ char* arg_getStr(Arg* self); uint8_t* arg_getBytes(Arg* self); size_t arg_getBytesSize(Arg* self); Arg* arg_copy(Arg* argToBeCopy); +Arg* arg_copy_noalloc(Arg* argToBeCopy, Arg* argToBeCopyTo); uint8_t* arg_getContent(Arg* self); void arg_deinit(Arg* self); @@ -114,9 +125,25 @@ Arg* arg_setHeapStruct(Arg* self, void* struct_deinit_fun); void* arg_getHeapStruct(Arg* self); void arg_deinitHeap(Arg* self); -Arg* arg_setBytes(Arg* self, char* name, uint8_t* src, size_t size); void arg_printBytes(Arg* self); Arg* arg_loadFile(Arg* self, char* filename); uint8_t argType_isObject(ArgType type); +#define arg_getNext(self) ((self)->_.next) +#define arg_getSize(self) ((self)->size) +#define arg_getContent(self) \ + ((self)->serialized ? (self)->content : ((self)->_.buffer)) +#define arg_getNext(self) ((self)->_.next) +#define arg_setNext(self, __next) ((self)->_.next = (__next)) + +#define argType_isObject(type) \ + ((type) == ARG_TYPE_OBJECT || (type) == ARG_TYPE_OBJECT_NEW) + #endif + +#define arg_newReg(__name, __size) \ + Arg __name = {0}; \ + uint8_t __##__name##_buff[__size] = {0}; \ + arg_init_stack(&__name, __##__name##_buff, __size) + +void arg_init_stack(Arg* self, uint8_t* buffer, uint32_t size); diff --git a/port/cmsis-pack/pikascript/pikascript-core/dataArgs.c b/port/cmsis-pack/pikascript/pikascript-core/dataArgs.c index 5f65cd2a8..6eac0f505 100644 --- a/port/cmsis-pack/pikascript/pikascript-core/dataArgs.c +++ b/port/cmsis-pack/pikascript/pikascript-core/dataArgs.c @@ -79,12 +79,22 @@ PIKA_RES args_setStr(Args* self, char* name, char* strIn) { PIKA_RES errCode = PIKA_RES_OK; Arg* argNew = New_arg(NULL); argNew = arg_setStr(argNew, name, strIn); + if (NULL == argNew) { + return PIKA_RES_ERR_INVALID_PTR; + } args_setArg(self, argNew); return errCode; } PIKA_RES args_pushArg(Args* self, Arg* arg) { - link_addNode(self, arg); + Arg* new_arg = NULL; + if (!arg->serialized) { + new_arg = arg_copy(arg); + arg_deinit(arg); + } else { + new_arg = arg; + } + link_addNode(self, new_arg); return PIKA_RES_OK; } @@ -172,7 +182,7 @@ ArgType args_getType(Args* self, char* name) { Arg* arg = NULL; arg = args_getArg(self, name); if (NULL == arg) { - return ARG_TYPE_NULL; + return ARG_TYPE_NONE; } return arg_getType(arg); } @@ -216,6 +226,9 @@ PIKA_RES args_setStructWithSize(Args* self, void* args_getStruct(Args* self, char* name) { Arg* struct_arg = args_getArg(self, name); + if (NULL == struct_arg) { + return NULL; + } return arg_getContent(struct_arg); } @@ -293,6 +306,9 @@ PIKA_RES __updateArg(Args* self, Arg* argNew) { arg_setNext((Arg*)priorNode, (Arg*)nodeToUpdate); goto exit; exit: + if (!argNew->serialized) { + return PIKA_RES_OK; + } arg_freeContent(argNew); return PIKA_RES_OK; } @@ -402,7 +418,7 @@ char* getPrintStringFromPtr(Args* self, char* name, void* val) { Args buffs = {0}; char* res = NULL; uint64_t intVal = (uintptr_t)val; - char* valString = strsFormat(&buffs, 32, "0x%llx", intVal); + char* valString = strsFormat(&buffs, 32, "%p", intVal); res = getPrintSring(self, name, valString); strsDeinit(&buffs); return res; @@ -435,7 +451,8 @@ char* args_print(Args* self, char* name) { goto exit; } - if (argType_isObject(type)) { + if (argType_isObject(type) || ARG_TYPE_POINTER == type || + ARG_TYPE_METHOD_NATIVE_CONSTRUCTOR) { void* val = args_getPtr(self, name); res = getPrintStringFromPtr(self, name, val); goto exit; @@ -544,6 +561,26 @@ Arg* list_getArg(PikaList* self, int index) { return args_getArg(&self->super, i_str); } +int list_getInt(PikaList* self, int index) { + Arg* arg = list_getArg(self, index); + return arg_getInt(arg); +} + +double list_getFloat(PikaList* self, int index) { + Arg* arg = list_getArg(self, index); + return arg_getFloat(arg); +} + +char* list_getStr(PikaList* self, int index) { + Arg* arg = list_getArg(self, index); + return arg_getStr(arg); +} + +void* list_getPtr(PikaList* self, int index) { + Arg* arg = list_getArg(self, index); + return arg_getPtr(arg); +} + PIKA_RES list_append(PikaList* self, Arg* arg) { int top = args_getInt(&self->super, "top"); char buff[11]; @@ -583,8 +620,14 @@ char* strsFormatArg(Args* out_buffs, char* fmt, Arg* arg) { res = strsFormat(&buffs, PIKA_SPRINTF_BUFF_SIZE, fmt, val); goto exit; } + if (ARG_TYPE_NONE == type) { + res = strsFormat(&buffs, PIKA_SPRINTF_BUFF_SIZE, fmt, "None"); + goto exit; + } exit: - res = strsCopy(out_buffs, res); + if (NULL != res) { + res = strsCopy(out_buffs, res); + } strsDeinit(&buffs); return res; } @@ -594,7 +637,7 @@ char* strsFormatList(Args* out_buffs, char* fmt, PikaList* list) { char* res = NULL; char* fmt_buff = strsCopy(&buffs, fmt); char* fmt_item = strsPopToken(&buffs, fmt_buff, '%'); - Arg* res_buff = arg_setStr(NULL, "", fmt_item); + Arg* res_buff = arg_newStr(fmt_item); for (size_t i = 0; i < list_getSize(list); i++) { Args buffs_item = {0}; @@ -602,6 +645,10 @@ char* strsFormatList(Args* out_buffs, char* fmt, PikaList* list) { char* fmt_item = strsPopToken(&buffs_item, fmt_buff, '%'); fmt_item = strsAppend(&buffs_item, "%", fmt_item); char* str_format = strsFormatArg(&buffs_item, fmt_item, arg); + if (NULL == str_format) { + strsDeinit(&buffs_item); + goto exit; + } res_buff = arg_strAppend(res_buff, str_format); strsDeinit(&buffs_item); } diff --git a/port/cmsis-pack/pikascript/pikascript-core/dataArgs.h b/port/cmsis-pack/pikascript/pikascript-core/dataArgs.h index 763e43c4a..96e830f4a 100644 --- a/port/cmsis-pack/pikascript/pikascript-core/dataArgs.h +++ b/port/cmsis-pack/pikascript/pikascript-core/dataArgs.h @@ -162,6 +162,10 @@ PikaDict* New_dict(void); #define list_deinit(self) (args_deinit((&((self)->super)))) PIKA_RES list_append(PikaList* self, Arg* arg); PIKA_RES list_setArg(PikaList* self, int index, Arg* arg); +int list_getInt(PikaList* self, int index); +double list_getFloat(PikaList* self, int index); +char* list_getStr(PikaList* self, int index); +void* list_getPtr(PikaList* self, int index); Arg* list_getArg(PikaList* self, int index); size_t list_getSize(PikaList* self); char* strsFormatArg(Args* out_buffs, char* fmt, Arg* arg); @@ -170,6 +174,12 @@ char* strsFormatArg(Args* out_buffs, char* fmt, Arg* arg); #define tuple_deinit(self) (list_deinit((&((self)->super)))) #define tuple_getArg(self, index) (list_getArg((&((self)->super)), (index))) #define tuple_getSize(self) (list_getSize((&((self)->super)))) +#define tuple_getInt(self, index) (list_getInt((&((self)->super)), (index))) +#define tuple_getFloat(self, index) \ + (list_getFloat((&((self)->super)), (index))) +#define tuple_getStr(self, index) (list_getStr((&((self)->super)), (index))) +#define tuple_getPtr(self, index) (list_getPtr((&((self)->super)), (index))) +#define tuple_getType(self, index) (list_getType((&((self)->super)), (index))) PikaList* New_list(void); PikaTuple* New_tuple(void); diff --git a/port/cmsis-pack/pikascript/pikascript-core/dataLink.c b/port/cmsis-pack/pikascript/pikascript-core/dataLink.c index f12f48423..f933216e9 100644 --- a/port/cmsis-pack/pikascript/pikascript-core/dataLink.c +++ b/port/cmsis-pack/pikascript/pikascript-core/dataLink.c @@ -55,7 +55,7 @@ void link_addNode(Link* self, void* content) { self->firstNode = content; // change the first node to new node - arg_setNext(content, (Arg*)secondNode); + arg_setNext((Arg*)content, (Arg*)secondNode); } static void __link_removeNode(Link* self, diff --git a/port/cmsis-pack/pikascript/pikascript-core/dataLinkNode.c b/port/cmsis-pack/pikascript/pikascript-core/dataLinkNode.c index e3b87876a..b6f0c4d53 100644 --- a/port/cmsis-pack/pikascript/pikascript-core/dataLinkNode.c +++ b/port/cmsis-pack/pikascript/pikascript-core/dataLinkNode.c @@ -32,11 +32,3 @@ void linkNode_deinit(LinkNode* self) { arg_deinit((Arg*)self); } - -void linkNode_init(LinkNode* self, void* args) { - /* attribute */ -} - -LinkNode* New_linkNode(void* args) { - return NULL; -} diff --git a/port/cmsis-pack/pikascript/pikascript-core/dataMemory.c b/port/cmsis-pack/pikascript/pikascript-core/dataMemory.c index 63073e215..b289982cf 100644 --- a/port/cmsis-pack/pikascript/pikascript-core/dataMemory.c +++ b/port/cmsis-pack/pikascript/pikascript-core/dataMemory.c @@ -76,12 +76,12 @@ void pikaFree(void* mem, uint32_t size) { pikaMemInfo.heapUsed -= size; } -uint16_t pikaMemNow(void) { +uint32_t pikaMemNow(void) { return pikaMemInfo.heapUsed; // return 0; } -uint16_t pikaMemMax(void) { +uint32_t pikaMemMax(void) { return pikaMemInfo.heapUsedMax; } @@ -109,6 +109,7 @@ Pool pool_init(uint32_t size, uint8_t aline) { pool.mem = __platform_malloc(pool_aline(&pool, pool.size)); pool.first_free_block = 0; pool.purl_free_block_start = 0; + pool.inited = PIKA_TRUE; return pool; } @@ -295,3 +296,27 @@ uint8_t bitmap_get(BitMap bitmap, uint32_t index) { void bitmap_deinit(BitMap bitmap) { __platform_free(bitmap); } + +#if PIKA_POOL_ENABLE +Pool pikaPool = {0}; +void* __user_malloc(size_t size) { + return pool_malloc(&pikaPool, size); +} +void __user_free(void* ptrm, size_t size) { + pool_free(&pikaPool, ptrm, size); +} +#endif + +void mem_pool_init(void) { +#if PIKA_POOL_ENABLE + if (!pikaPool.inited) { + pikaPool = pool_init(PIKA_POOL_SIZE, PIKA_POOL_ALIGN); + } +#endif +} + +void mem_pool_deinit(void) { +#if PIKA_POOL_ENABLE + pool_deinit(&pikaPool); +#endif +} diff --git a/port/cmsis-pack/pikascript/pikascript-core/dataMemory.h b/port/cmsis-pack/pikascript/pikascript-core/dataMemory.h index b3b6d528b..ea87d23cf 100644 --- a/port/cmsis-pack/pikascript/pikascript-core/dataMemory.h +++ b/port/cmsis-pack/pikascript/pikascript-core/dataMemory.h @@ -29,6 +29,7 @@ #define __MEMORY_H__ #include "PikaPlatform.h" +#include "PikaVersion.h" /*! \NOTE: Make sure #include "plooc_class.h" is close to the class definition */ @@ -54,14 +55,15 @@ struct Pool{ uint32_t size; uint32_t first_free_block; uint32_t purl_free_block_start; + PIKA_BOOL inited; ) }; /* clang-format on */ void pikaFree(void* mem, uint32_t size); void* pikaMalloc(uint32_t size); -uint16_t pikaMemNow(void); -uint16_t pikaMemMax(void); +uint32_t pikaMemNow(void); +uint32_t pikaMemMax(void); void pikaMemMaxReset(void); uint32_t aline_by(uint32_t size, uint32_t aline); @@ -72,11 +74,8 @@ uint8_t bitmap_get(BitMap bitmap, uint32_t index); uint8_t bitmap_getByte(BitMap bitmap, uint32_t index); void bitmap_deinit(BitMap bitmap); -Pool pool_init(uint32_t size, uint8_t aline); -void* pool_malloc(Pool* pool, uint32_t size); -void pool_free(Pool* pool, void* mem, uint32_t size); -void pool_deinit(Pool* pool); -void pool_printBlocks(Pool* pool, uint32_t block_min, uint32_t block_max); +void mem_pool_deinit(void); +void mem_pool_init(void); #undef __DATA_MEMORY_CLASS_IMPLEMENT__ #endif diff --git a/port/cmsis-pack/pikascript/pikascript-core/dataQueue.c b/port/cmsis-pack/pikascript/pikascript-core/dataQueue.c index c9c85a818..a8f9beab1 100644 --- a/port/cmsis-pack/pikascript/pikascript-core/dataQueue.c +++ b/port/cmsis-pack/pikascript/pikascript-core/dataQueue.c @@ -56,25 +56,6 @@ int32_t queue_pushArg(Queue* queue, Arg* arg) { return args_setArg(args, arg); } -Arg* __queue_popArg(Queue* queue, uint8_t is_deinit_arg) { - Args* args = queue; - uint64_t top = args_getInt(args, "__t"); - uint64_t bottom = args_getInt(args, "__b"); - if (top - bottom < 1) { - return NULL; - } - /* add bottom */ - args_setInt(args, "__b", bottom + 1); - char buff[11]; - Arg* res = args_getArg(args, fast_itoa(buff, bottom)); - if (is_deinit_arg) { - args_removeArg(args, res); - } else { - args_removeArg_notDeinitArg(args, res); - } - return res; -} - Arg* __queue_popArg_noRmoveArg(Queue* queue) { Args* args = queue; uint64_t top = args_getInt(args, "__t"); @@ -90,16 +71,8 @@ Arg* __queue_popArg_noRmoveArg(Queue* queue) { return res; } -Arg* queue_popArg(Queue* queue) { - return __queue_popArg(queue, 1); -} - -Arg* queue_popArg_notDeinitArg(Queue* queue) { - return __queue_popArg(queue, 0); -} - int32_t queue_pushInt(Queue* queue, int val) { - return queue_pushArg(queue, arg_setInt(NULL, "", val)); + return queue_pushArg(queue, arg_newInt(val)); } int64_t queue_popInt(Queue* queue) { @@ -107,7 +80,7 @@ int64_t queue_popInt(Queue* queue) { } int32_t queue_pushFloat(Queue* queue, double val) { - return queue_pushArg(queue, arg_setFloat(NULL, "", val)); + return queue_pushArg(queue, arg_newFloat(val)); } double queue_popFloat(Queue* queue) { @@ -115,7 +88,7 @@ double queue_popFloat(Queue* queue) { } int32_t queue_pushStr(Queue* queue, char* str) { - return queue_pushArg(queue, arg_setStr(NULL, "", str)); + return queue_pushArg(queue, arg_newStr(str)); } char* queue_popStr(Queue* queue) { diff --git a/port/cmsis-pack/pikascript/pikascript-core/dataStack.c b/port/cmsis-pack/pikascript/pikascript-core/dataStack.c index 863929095..d1925e3e2 100644 --- a/port/cmsis-pack/pikascript/pikascript-core/dataStack.c +++ b/port/cmsis-pack/pikascript/pikascript-core/dataStack.c @@ -31,7 +31,7 @@ void stack_reset(Stack* stack) { stack->sp = (uint8_t*)arg_getContent(stack->stack_pyload); - stack->sp_size = (int16_t*)arg_getContent(stack->stack_size_array); + stack->sp_size = (int32_t*)arg_getContent(stack->stack_size_array); stack->top = 0; } @@ -44,12 +44,12 @@ int32_t stack_init(Stack* stack) { return 0; }; -void stack_pushSize(Stack* stack, int16_t size) { +void stack_pushSize(Stack* stack, int32_t size) { *(stack->sp_size) = size; stack->sp_size++; } -int16_t stack_popSize(Stack* stack) { +int32_t stack_popSize(Stack* stack) { stack->sp_size--; return *(stack->sp_size); } @@ -74,7 +74,15 @@ void stack_pushPyload(Stack* stack, Arg* content, size_t size) { while (1) { } } - __platform_memcpy(stack->sp, content, size); + if (content->serialized) { + __platform_memcpy(stack->sp, content, size); + } else { + __platform_memcpy(stack->sp, content, sizeof(Arg)); + __platform_memcpy(stack->sp + sizeof(Arg), content->_.buffer, + size - sizeof(Arg)); + /* transfer to serialized form */ + ((Arg*)stack->sp)->serialized = PIKA_TRUE; + } stack->sp += size; } @@ -83,7 +91,7 @@ uint8_t* stack_popPyload(Stack* stack, size_t size) { return stack->sp; } -int32_t stack_pushArg(Stack* stack, Arg* arg) { +static int32_t _stack_pushArg(Stack* stack, Arg* arg, PIKA_BOOL is_alloc) { stack->top++; size_t size = arg_getTotleSize(arg); @@ -94,41 +102,65 @@ int32_t stack_pushArg(Stack* stack, Arg* arg) { #endif /* add ref_cnt to keep object in stack */ if (argType_isObject(arg_getType(arg))) { - obj_refcntInc(arg_getPtr(arg)); + obj_refcntInc((PikaObj*)arg_getPtr(arg)); } stack_pushSize(stack, size); stack_pushPyload(stack, arg, size); - arg_deinit(arg); + if (is_alloc) { + arg_deinit(arg); + } else { + arg_deinitHeap(arg); + } return 0; } +int32_t stack_pushArg(Stack* stack, Arg* arg) { + if (arg->serialized) { + return _stack_pushArg(stack, arg, PIKA_TRUE); + } + return _stack_pushArg(stack, arg, PIKA_FALSE); +} + int32_t stack_pushStr(Stack* stack, char* str) { - Arg* newArg = arg_setStr(NULL, "", str); + Arg* newArg = arg_newStr(str); return stack_pushArg(stack, newArg); } -Arg* stack_popArg(Stack* stack) { +Arg* _stack_popArg(Stack* stack, Arg* arg_dict, PIKA_BOOL is_alloc) { if (stack->top == 0) { return NULL; } stack->top--; - int16_t size = stack_popSize(stack); - Arg* arg = arg_copy((Arg*)stack_popPyload(stack, size)); + int32_t size = stack_popSize(stack); + Arg* arg = NULL; + if (is_alloc) { + arg = arg_copy((Arg*)stack_popPyload(stack, size)); + } else { + arg = arg_copy_noalloc((Arg*)stack_popPyload(stack, size), arg_dict); + } ArgType type = arg_getType(arg); /* decrase ref_cnt */ if (argType_isObject(type)) { - obj_refcntDec(arg_getPtr(arg)); + obj_refcntDec((PikaObj*)arg_getPtr(arg)); } return arg; } +Arg* stack_popArg_alloc(Stack* stack) { + return _stack_popArg(stack, NULL, PIKA_TRUE); +} + +Arg* stack_popArg(Stack* stack, Arg* arg_dict) { + return _stack_popArg(stack, arg_dict, PIKA_FALSE); +} + char* stack_popStr(Stack* stack, char* outBuff) { - Arg* arg = stack_popArg(stack); + Arg* arg = stack_popArg_alloc(stack); strcpy(outBuff, arg_getStr(arg)); arg_deinit(arg); return outBuff; } -int8_t stack_getTop(Stack* stack) { +int32_t stack_getTop(Stack* stack) { return stack->top; } diff --git a/port/cmsis-pack/pikascript/pikascript-core/dataStack.h b/port/cmsis-pack/pikascript/pikascript-core/dataStack.h index bd622311e..37b868793 100644 --- a/port/cmsis-pack/pikascript/pikascript-core/dataStack.h +++ b/port/cmsis-pack/pikascript/pikascript-core/dataStack.h @@ -33,8 +33,8 @@ typedef struct Stack_t { Arg* stack_pyload; Arg* stack_size_array; uint8_t* sp; - int16_t* sp_size; - int16_t top; + int32_t* sp_size; + int32_t top; size_t stack_totle_size; } Stack; @@ -42,11 +42,12 @@ int32_t stack_deinit(Stack* stack); int32_t stack_pushStr(Stack* stack, char* str); char* stack_popStr(Stack* stack, char* outBuff); -Arg* stack_popArg(Stack* stack); int32_t stack_pushArg(Stack* stack, Arg* arg); -int8_t stack_getTop(Stack* stack); +Arg* stack_popArg_alloc(Stack* stack); +Arg* stack_popArg(Stack* stack, Arg* arg_dict); +int32_t stack_getTop(Stack* stack); int32_t stack_init(Stack* stack); -int16_t stack_popSize(Stack* stack); -void stack_pushSize(Stack* stack, int16_t size); +int32_t stack_popSize(Stack* stack); +void stack_pushSize(Stack* stack, int32_t size); void stack_reset(Stack* stack); #endif diff --git a/port/cmsis-pack/pikascript/pikascript-core/dataString.c b/port/cmsis-pack/pikascript/pikascript-core/dataString.c index f1a2cdca0..40b41f670 100644 --- a/port/cmsis-pack/pikascript/pikascript-core/dataString.c +++ b/port/cmsis-pack/pikascript/pikascript-core/dataString.c @@ -28,18 +28,6 @@ #include "dataString.h" #include "PikaPlatform.h" -char* strAppendWithSize_unlimited(char* strOut, char* pData, int32_t Size) { - int32_t strOut_i = strGetSize(strOut); - for (int32_t i = 0; i < Size; i++) { - strOut[strOut_i + i] = pData[i]; - } - strOut_i += Size; - // add \0 to the end of strOut - strOut[strOut_i] = 0; - - return strOut; -} - char* strCut(char* strOut, char* strIn, char startSign, char endSign) { int32_t Size = strGetSize(strIn); int32_t iStart = 0; @@ -89,10 +77,6 @@ char* strDeleteChar(char* strOut, char* strIn, char ch) { return strOut; } -char* strDeleteEnter(char* str) { - return strDeleteChar(str, str, '\n'); -} - char* strAppendWithSize(char* strOut, char* pData, int32_t Size) { int32_t strOut_i = strGetSize(strOut); for (int32_t i = 0; i < Size; i++) { @@ -120,33 +104,10 @@ int32_t strGetTokenNum(char* strIn, char sign) { } size_t strGetSize(char* pData) { + pika_assert(pData != NULL); return strlen(pData); } -char* strAppend_unlimited(char* strOut, char* pData) { - uint32_t Size = 0; - Size = strGetSize(pData); - return strAppendWithSize_unlimited(strOut, pData, Size); -} - -char* strGetLastLine(char* strOut, char* strIn) { - int32_t size = strGetSize(strIn); - char sign = '\n'; - uint32_t beginIndex = 0; - - /* skip the latest '\n' */ - for (int32_t i = size - 2; i > -1; i--) { - if (strIn[i] == sign) { - beginIndex = i + 1; - break; - } - } - - __platform_memcpy(strOut, strIn + beginIndex, size - beginIndex); - strOut[size - beginIndex + 1] = 0; - return strOut; -} - char* strPointToLastToken(char* strIn, char sign) { int32_t size = strGetSize(strIn); for (int32_t i = size - 1; i > -1; i--) { @@ -226,31 +187,6 @@ char* strGetFirstToken(char* strOut, char* strIn, char sign) { return strOut; } -int32_t strGetToken(char* string, char** argv, char sign) { - int32_t argc = 0; - uint32_t i = 0; - // arg_i point32_t to the arg operated now - int32_t arg_i = 0; - // if not found ' ', then put chars from CMD to argv_tem - int32_t char_i = 0; - for (i = 0; i < strGetSize(string); i++) { - if (string[i] != sign) { - argv[arg_i][char_i] = string[i]; - char_i++; - } - if (string[i] == sign) { - // write '\0' to the end of argv - argv[arg_i][char_i] = 0; - arg_i++; - char_i = 0; - } - // write '\0' to the end of last argv - argv[arg_i][char_i] = 0; - } - argc = arg_i + 1; - return argc; -} - char* strAppend(char* strOut, char* pData) { uint32_t Size = 0; Size = strGetSize(pData); @@ -287,13 +223,6 @@ char* strRemovePrefix(char* inputStr, char* prefix, char* outputStr) { return outputStr; } -char* strClear(char* str) { - for (uint32_t i = 0; i < sizeof(str); i++) { - str[i] = 0; - } - return str; -} - int32_t strIsContain(char* str, char ch) { for (uint32_t i = 0; i < strGetSize(str); i++) { if (str[i] == ch) { @@ -324,3 +253,21 @@ char* strGetLine(char* strOut, char* strIn) { strOut[lineSize] = 0; return strOut; } + +char* strGetLastLine(char* strOut, char* strIn) { + int32_t size = strGetSize(strIn); + char sign = '\n'; + uint32_t beginIndex = 0; + + /* skip the latest '\n' */ + for (int32_t i = size - 2; i > -1; i--) { + if (strIn[i] == sign) { + beginIndex = i + 1; + break; + } + } + + __platform_memcpy(strOut, strIn + beginIndex, size - beginIndex); + strOut[size - beginIndex + 1] = 0; + return strOut; +} diff --git a/port/cmsis-pack/pikascript/pikascript-core/dataString.h b/port/cmsis-pack/pikascript/pikascript-core/dataString.h index 218d04492..8c37a35fc 100644 --- a/port/cmsis-pack/pikascript/pikascript-core/dataString.h +++ b/port/cmsis-pack/pikascript/pikascript-core/dataString.h @@ -37,7 +37,7 @@ char* strAppend_unlimited(char* strOut, char* pData); char* strAppendWithSize(char* strOut, char* pData, int32_t Size); /* cut */ char* strCut(char* strOut, char* strIn, char startSign, char endSign); -/* assert */ +/* pika_assert */ int32_t strIsStartWith(char* str, char* strStart); int32_t strEqu(char* str1, char* str2); /* delete */ @@ -60,5 +60,6 @@ char* strPointToLastToken(char* strIn, char sign); char* strGetLine(char* strOut, char* strIn); int32_t strGetLineSize(char* str); char* strPopLastToken(char* strIn, char sign); +char* strGetLastLine(char* strOut, char* strIn); #endif diff --git a/port/cmsis-pack/pikascript/pikascript-core/dataStrs.c b/port/cmsis-pack/pikascript/pikascript-core/dataStrs.c index 8669e2bcf..381b7f22f 100644 --- a/port/cmsis-pack/pikascript/pikascript-core/dataStrs.c +++ b/port/cmsis-pack/pikascript/pikascript-core/dataStrs.c @@ -53,6 +53,8 @@ char* strsGetDirectStr(Args* buffs_p, char* argPath) { } char* strsAppend(Args* buffs_p, char* strOrigin, char* strToAppend) { + pika_assert(NULL != strToAppend); + pika_assert(NULL != strOrigin); int32_t size = strGetSize(strOrigin) + strGetSize(strToAppend); char* buff = args_getBuff(buffs_p, size); char* strOut = strCopy(buff, strOrigin); @@ -99,11 +101,19 @@ char* strsPopToken(Args* buffs_p, char* tokens, char sign) { } char* strsCopy(Args* buffs_p, char* source) { + pika_assert(source != NULL); int32_t size = strGetSize(source); char* buff = args_getBuff(buffs_p, size); return strCopy(buff, source); } +char* strsCacheArg(Args* buffs_p, Arg* arg) { + pika_assert(arg != NULL); + char* res = strsCopy(buffs_p, arg_getStr(arg)); + arg_deinit(arg); + return res; +} + char* strsFormat(Args* buffs_p, uint16_t buffSize, const char* fmt, ...) { va_list args; va_start(args, fmt); @@ -114,6 +124,7 @@ char* strsFormat(Args* buffs_p, uint16_t buffSize, const char* fmt, ...) { } Arg* arg_strAppend(Arg* arg_in, char* str_to_append) { + pika_assert(NULL != str_to_append); Args buffs = {0}; char* str_out = strsAppend(&buffs, arg_getStr(arg_in), str_to_append); Arg* arg_out = arg_setStr(arg_in, "", str_out); diff --git a/port/cmsis-pack/pikascript/pikascript-core/dataStrs.h b/port/cmsis-pack/pikascript/pikascript-core/dataStrs.h index 40e369055..df8d8df28 100644 --- a/port/cmsis-pack/pikascript/pikascript-core/dataStrs.h +++ b/port/cmsis-pack/pikascript/pikascript-core/dataStrs.h @@ -43,4 +43,5 @@ Arg* arg_strAppend(Arg* arg_in, char* str_to_append); char* strsReplace(Args* buffs, char* orig, char* rep, char* with); char* strsGetLine(Args* buffs, char* code); void strsDeinit(Args* buffs); +char* strsCacheArg(Args* buffs_p, Arg* arg); #endif 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 4c22599f1..2e84114fe 100644 --- a/port/cmsis-pack/pikascript/pikascript-core/pika_config_valid.h +++ b/port/cmsis-pack/pikascript/pikascript-core/pika_config_valid.h @@ -27,24 +27,16 @@ #ifndef __PIKA_CFG_VALID_H__ #define __PIKA_CFG_VALID_H__ - /* default configuration */ - #define PIKA_LINE_BUFF_SIZE 128 - #define PIKA_SPRINTF_BUFF_SIZE 256 - #define PIKA_STACK_BUFF_SIZE 256 - #define PIKA_NAME_BUFF_SIZE 32 - #define PIKA_PATH_BUFF_SIZE 64 - #define PIKA_BYTES_DEFAULT_SIZE 64 - #define PIKA_ARG_ALIGN_ENABLE 1 - #define PIKA_METHOD_CACHE_ENABLE 0 - #define PIKA_BUILTIN_LIST_ENABLE 0 - #define PIKA_BUILTIN_DICT_ENABLE 0 - #define PIKA_READ_FILE_BUFF_SIZE 0x10000 - #define PIKA_INIT_STRING_ENABLE 0 - #define PIKA_SYNTEX_ITEM_SLICE_ENABLE 1 - #define PIKA_SYNTEX_ITEM_FORMAT_ENABLE 1 - #define PIKA_PLOOC_ENABLE 0 - #define PIKA_STD_DEVICE_UNIX_TIME_ENABLE 1 - +/* clang-format off */ + +/* + * Don't modify the "pika_config_valid.h" file! + * If you want to change the config, create "pika_config.h", + * then #define PIKA_CONFIG_ENABLE in the Compiler Options. + * To see more: + * https://pikadoc.readthedocs.io/en/latest/%E4%BC%98%E5%8C%96%E5%86%85%E5%AD%98%E5%8D%A0%E7%94%A8%E3%80%81%E9%85%8D%E7%BD%AE%20libc.html + */ + /* optimize options */ #define PIKA_OPTIMIZE_SIZE 0 #define PIKA_OPTIMIZE_SPEED 1 @@ -53,50 +45,213 @@ #define PIKA_SYNTAX_LEVEL_MINIMAL 0 #define PIKA_SYNTAX_LEVEL_MAXIMAL 1 - /* default optimize */ - #define PIKA_OPTIMIZE PIKA_OPTIMIZE_SIZE - /* default syntax support level */ - #define PIKA_SYNTAX_LEVEL PIKA_SYNTAX_LEVEL_MAXIMAL - /* use user config */ #ifdef PIKA_CONFIG_ENABLE #include "pika_config.h" #endif - /* config for size optimize */ - #if PIKA_OPTIMIZE == PIKA_OPTIMIZE_SIZE - #undef PIKA_METHOD_CACHE_ENABLE - #define PIKA_METHOD_CACHE_ENABLE 0 + /* default optimize */ + #ifndef PIKA_OPTIMIZE + #define PIKA_OPTIMIZE PIKA_OPTIMIZE_SIZE + #endif + + /* default syntax support level */ + #ifndef PIKA_SYNTAX_LEVEL + #define PIKA_SYNTAX_LEVEL PIKA_SYNTAX_LEVEL_MAXIMAL + #endif + + /* auto config for optimize */ + #if PIKA_OPTIMIZE == PIKA_OPTIMIZE_SIZE + #ifndef PIKA_METHOD_CACHE_ENABLE + #define PIKA_METHOD_CACHE_ENABLE 0 + #endif - /* config for speed optimize */ #elif PIKA_OPTIMIZE == PIKA_OPTIMIZE_SPEED - #undef PIKA_METHOD_CACHE_ENABLE - #define PIKA_METHOD_CACHE_ENABLE 1 + #ifndef PIKA_METHOD_CACHE_ENABLE + #define PIKA_METHOD_CACHE_ENABLE 1 + #endif #endif - /* config for syntax level */ + /* auto config for syntax level */ #if PIKA_SYNTAX_LEVEL == PIKA_SYNTAX_LEVEL_MINIMAL - #undef PIKA_SYNTEX_ITEM_SLICE_ENABLE - #define PIKA_SYNTEX_ITEM_SLICE_ENABLE 0 - #undef PIKA_BUILTIN_LIST_ENABLE - #define PIKA_BUILTIN_LIST_ENABLE 0 - #undef PIKA_BUILTIN_DICT_ENABLE - #define PIKA_BUILTIN_DICT_ENABLE 0 - #undef PIKA_SYNTEX_ITEM_FORMAT_ENABLE - #define PIKA_SYNTEX_ITEM_FORMAT_ENABLE 0 - #undef PIKA_STD_DEVICE_UNIX_TIME_ENABLE - #define PIKA_STD_DEVICE_UNIX_TIME_ENABLE 0 + #ifndef PIKA_SYNTAX_SLICE_ENABLE + #define PIKA_SYNTAX_SLICE_ENABLE 0 + #endif + + #ifndef PIKA_BUILTIN_STRUCT_ENABLE + #define PIKA_BUILTIN_STRUCT_ENABLE 0 + #endif + + #ifndef PIKA_SYNTAX_FORMAT_ENABLE + #define PIKA_SYNTAX_FORMAT_ENABLE 0 + #endif + + #ifndef PIKA_STD_DEVICE_UNIX_TIME_ENABLE + #define PIKA_STD_DEVICE_UNIX_TIME_ENABLE 0 + #endif + + #ifndef PIKA_SYNTAX_EXCEPTION_ENABLE + #define PIKA_SYNTAX_EXCEPTION_ENABLE 0 + #endif + + #ifndef PIKA_SYNTAX_IMPORT_EX_ENABLE + #define PIKA_SYNTAX_IMPORT_EX_ENABLE 0 + #endif + + #ifndef PIKA_EVENT_ENABLE + #define PIKA_EVENT_ENABLE 0 + #endif + + #ifndef PIKA_FILEIO_ENABLE + #define PIKA_FILEIO_ENABLE 0 + #endif + #elif PIKA_SYNTAX_LEVEL == PIKA_SYNTAX_LEVEL_MAXIMAL - #undef PIKA_SYNTEX_ITEM_SLICE_ENABLE - #define PIKA_SYNTEX_ITEM_SLICE_ENABLE 1 - #undef PIKA_BUILTIN_LIST_ENABLE - #define PIKA_BUILTIN_LIST_ENABLE 1 - #undef PIKA_BUILTIN_DICT_ENABLE - #define PIKA_BUILTIN_DICT_ENABLE 1 - #undef PIKA_SYNTEX_ITEM_FORMAT_ENABLE - #define PIKA_SYNTEX_ITEM_FORMAT_ENABLE 1 - #undef PIKA_STD_DEVICE_UNIX_TIME_ENABLE - #define PIKA_STD_DEVICE_UNIX_TIME_ENABLE 1 + #ifndef PIKA_SYNTAX_SLICE_ENABLE + #define PIKA_SYNTAX_SLICE_ENABLE 1 + #endif + + #ifndef PIKA_BUILTIN_STRUCT_ENABLE + #define PIKA_BUILTIN_STRUCT_ENABLE 1 + #endif + + #ifndef PIKA_SYNTAX_FORMAT_ENABLE + #define PIKA_SYNTAX_FORMAT_ENABLE 1 + #endif + + #ifndef PIKA_STD_DEVICE_UNIX_TIME_ENABLE + #define PIKA_STD_DEVICE_UNIX_TIME_ENABLE 1 + #endif + + #ifndef PIKA_SYNTAX_EXCEPTION_ENABLE + #define PIKA_SYNTAX_EXCEPTION_ENABLE 1 + #endif + + #ifndef PIKA_SYNTAX_IMPORT_EX_ENABLE + #define PIKA_SYNTAX_IMPORT_EX_ENABLE 1 + #endif + + #ifndef PIKA_EVENT_ENABLE + #define PIKA_EVENT_ENABLE 1 + #endif + + #ifndef PIKA_FILEIO_ENABLE + #define PIKA_FILEIO_ENABLE 1 + #endif + + #endif + + /* default configuration */ + #ifndef PIKA_LINE_BUFF_SIZE + #define PIKA_LINE_BUFF_SIZE 128 + #endif + + #ifndef PIKA_SPRINTF_BUFF_SIZE + #define PIKA_SPRINTF_BUFF_SIZE 256 + #endif + + #ifndef PIKA_STACK_BUFF_SIZE + #define PIKA_STACK_BUFF_SIZE 256 + #endif + + #ifndef PIKA_NAME_BUFF_SIZE + #define PIKA_NAME_BUFF_SIZE 32 + #endif + + #ifndef PIKA_PATH_BUFF_SIZE + #define PIKA_PATH_BUFF_SIZE 96 + #endif + + #ifndef PIKA_BYTES_DEFAULT_SIZE + #define PIKA_BYTES_DEFAULT_SIZE 64 + #endif + + #ifndef PIKA_ARG_ALIGN_ENABLE + #define PIKA_ARG_ALIGN_ENABLE 1 + #endif + + #ifndef PIKA_METHOD_CACHE_ENABLE + #define PIKA_METHOD_CACHE_ENABLE 0 + #endif + + #ifndef PIKA_BUILTIN_STRUCT_ENABLE + #define PIKA_BUILTIN_STRUCT_ENABLE 0 + #endif + + #ifndef PIKA_READ_FILE_BUFF_SIZE + #define PIKA_READ_FILE_BUFF_SIZE 0x1024 * 10 + #endif + + #ifndef PIKA_INIT_STRING_ENABLE + #define PIKA_INIT_STRING_ENABLE 1 + #endif + + #ifndef PIKA_SYNTAX_SLICE_ENABLE + #define PIKA_SYNTAX_SLICE_ENABLE 1 + #endif + + #ifndef PIKA_SYNTAX_FORMAT_ENABLE + #define PIKA_SYNTAX_FORMAT_ENABLE 1 + #endif + + #ifndef PIKA_SYNTAX_EXCEPTION_ENABLE + #define PIKA_SYNTAX_EXCEPTION_ENABLE 1 + #endif + + #ifndef PIKA_SYNTAX_IMPORT_EX_ENABLE + #define PIKA_SYNTAX_IMPORT_EX_ENABLE 1 + #endif + + #ifndef PIKA_PLOOC_ENABLE + #define PIKA_PLOOC_ENABLE 0 + #endif + + #ifndef PIKA_STD_DEVICE_UNIX_TIME_ENABLE + #define PIKA_STD_DEVICE_UNIX_TIME_ENABLE 1 + #endif + + #ifndef PIKA_POOL_ENABLE + #define PIKA_POOL_ENABLE 0 + #endif + + #ifndef PIKA_POOL_SIZE + #define PIKA_POOL_SIZE 0x4000 + #endif + + #ifndef PIKA_POOL_ALIGN + #define PIKA_POOL_ALIGN 8 + #endif + + #ifndef PIKA_ASSERT_ENABLE + #define PIKA_ASSERT_ENABLE 0 + #endif + + #ifndef PIKA_EVENT_ENABLE + #define PIKA_EVENT_ENABLE 1 + #endif + + #ifndef PIKA_DEBUG_ENABLE + #define PIKA_DEBUG_ENABLE 0 + #endif + + #ifndef PIKA_FILEIO_ENABLE + #define PIKA_FILEIO_ENABLE 1 + #endif + + #ifndef PIKA_ARG_NUM_MAX + #define PIKA_ARG_NUM_MAX 16 + #endif + + #ifndef PIKA_MATH_ENABLE + #define PIKA_MATH_ENABLE 0 + #endif + + #ifndef PIKA_REGIST_SIZE + #define PIKA_REGIST_SIZE 10 + #endif + + #ifndef PIKA_ARG_BUFF_SIZE + #define PIKA_ARG_BUFF_SIZE 8 #endif /* configuration validation */ diff --git a/port/cmsis-pack/pikascript/pikascript-lib/PikaStdLib/PikaDebuger_Debuger.c b/port/cmsis-pack/pikascript/pikascript-lib/PikaStdLib/PikaDebuger_Debuger.c index 1f7477e80..2f5513b16 100644 --- a/port/cmsis-pack/pikascript/pikascript-lib/PikaStdLib/PikaDebuger_Debuger.c +++ b/port/cmsis-pack/pikascript/pikascript-lib/PikaStdLib/PikaDebuger_Debuger.c @@ -26,7 +26,7 @@ static enum shell_state __obj_shellLineHandler_debug(PikaObj* self, /* print */ if (strIsStartWith(input_line, "p ")) { char* path = input_line + 2; - Arg* asm_buff = arg_setStr(NULL, "", "B0\n1 REF "); + Arg* asm_buff = arg_newStr("B0\n1 REF "); asm_buff = arg_strAppend(asm_buff, path); asm_buff = arg_strAppend(asm_buff, "\n0 RUN print\n"); pikaVM_runAsm(__pikaMain, arg_getStr(asm_buff)); diff --git a/port/cmsis-pack/pikascript/pikascript-lib/PikaStdLib/PikaStdData_ByteArray.c b/port/cmsis-pack/pikascript/pikascript-lib/PikaStdLib/PikaStdData_ByteArray.c new file mode 100644 index 000000000..585c1665b --- /dev/null +++ b/port/cmsis-pack/pikascript/pikascript-lib/PikaStdLib/PikaStdData_ByteArray.c @@ -0,0 +1,71 @@ +#include "PikaStdData_ByteArray.h" + +void PikaStdData_ByteArray___init__(PikaObj* self, Arg* bytes) { + obj_setArg(self, "raw", bytes); +} + +Arg* PikaStdData_ByteArray___iter__(PikaObj* self) { + obj_setInt(self, "__iter_i", 0); + return arg_newRef(self); +} + +Arg* PikaStdData_ByteArray___next__(PikaObj* self) { + int __iter_i = args_getInt(self->list, "__iter_i"); + uint8_t* data = obj_getBytes(self, "raw"); + uint16_t len = obj_getBytesSize(self, "raw"); + Arg* res = NULL; + char char_buff[] = " "; + if (__iter_i < len) { + char_buff[0] = data[__iter_i]; + res = arg_newInt(char_buff[0]); + } else { + return arg_newNull(); + } + args_setInt(self->list, "__iter_i", __iter_i + 1); + return res; +} + +int PikaStdData_ByteArray___getitem__(PikaObj* self, int __key) { + uint8_t* data = obj_getBytes(self, "raw"); + uint16_t len = obj_getBytesSize(self, "raw"); + if (__key < len) { + return data[__key]; + } else { + return 0; + } +} + +void PikaStdData_ByteArray___setitem__(PikaObj* self, int __key, int __val) { + uint8_t* data = obj_getBytes(self, "raw"); + uint16_t len = obj_getBytesSize(self, "raw"); + if (__key < len) { + data[__key] = __val; + } +} + +char* PikaStdData_ByteArray___str__(PikaObj* self) { + uint8_t* data = obj_getBytes(self, "raw"); + uint16_t len = obj_getBytesSize(self, "raw"); + Arg* str_arg = arg_newStr(""); + str_arg = arg_strAppend(str_arg, "bytearray(b'"); + for (int i = 0; i < len; i++) { + char u8_str[] = "\\x00"; + uint8_t u8 = data[i]; + __platform_sprintf(u8_str, "\\x%02x", u8); + str_arg = arg_strAppend(str_arg, u8_str); + } + str_arg = arg_strAppend(str_arg, "')"); + obj_removeArg(self, "_buf"); + obj_setStr(self, "_buf", arg_getStr(str_arg)); + arg_deinit(str_arg); + return obj_getStr(self, "_buf"); +} + +char* PikaStdData_ByteArray_decode(PikaObj* self) { + uint8_t* data = obj_getBytes(self, "raw"); + Arg* str_arg = arg_newStr((char*)data); + obj_removeArg(self, "_buf"); + 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_Dict.c b/port/cmsis-pack/pikascript/pikascript-lib/PikaStdLib/PikaStdData_Dict.c index 85bdc8f1f..e045ba028 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,9 @@ +#include "PikaStdData_Dict.h" #include "BaseObj.h" #include "PikaObj.h" +#include "PikaStdData_dict_keys.h" #include "PikaStdLib_SysObj.h" +#include "dataStrs.h" Arg* PikaStdData_Dict_get(PikaObj* self, char* key) { PikaDict* dict = obj_getPtr(self, "dict"); @@ -8,47 +11,145 @@ Arg* PikaStdData_Dict_get(PikaObj* self, char* key) { } void PikaStdData_Dict___init__(PikaObj* self) { - PikaDict* dict = New_dict(); - obj_setPtr(self, "dict", dict); + __vm_Dict___init__(self); } -void PikaStdData_Dict_set(PikaObj* self, Arg* arg, char* key) { - PikaDict* dict = obj_getPtr(self, "dict"); - arg_setName(arg, key); - dict_setArg(dict, arg_copy(arg)); +void PikaStdData_Dict_set(PikaObj* self, char* key, Arg* arg) { + __vm_Dict_set(self, arg, key); } void PikaStdData_Dict_remove(PikaObj* self, char* key) { PikaDict* dict = obj_getPtr(self, "dict"); + PikaDict* keys = obj_getPtr(self, "_keys"); dict_removeArg(dict, dict_getArg(dict, key)); + dict_removeArg(keys, dict_getArg(keys, key)); } Arg* PikaStdData_Dict___iter__(PikaObj* self) { obj_setInt(self, "__iter_i", 0); - return arg_setRef(NULL, "", self); + return arg_newRef(self); } Arg* PikaStdData_Dict___next__(PikaObj* self) { int __iter_i = args_getInt(self->list, "__iter_i"); - PikaDict* dict = obj_getPtr(self, "dict"); - Arg* res = arg_copy(args_getArgByidex(&dict->super, __iter_i)); + PikaDict* keys = obj_getPtr(self, "_keys"); + Arg* res = arg_copy(args_getArgByidex(&keys->super, __iter_i)); if (NULL == res) { - return arg_setNull(NULL); + return arg_newNull(); } args_setInt(self->list, "__iter_i", __iter_i + 1); return res; } -void PikaStdData_Dict___setitem__(PikaObj* self) { - PikaStdData_Dict_set(self, obj_getArg(self, "__val"), - obj_getStr(self, "__key")); +void PikaStdData_Dict___setitem__(PikaObj* self, Arg* __key, Arg* __val) { + PikaStdData_Dict_set(self, obj_getStr(self, "__key"), + obj_getArg(self, "__val")); } -Arg* PikaStdData_Dict___getitem__(PikaObj* self) { +Arg* PikaStdData_Dict___getitem__(PikaObj* self, Arg* __key) { return PikaStdData_Dict_get(self, obj_getStr(self, "__key")); } void PikaStdData_Dict___del__(PikaObj* self) { PikaDict* dict = obj_getPtr(self, "dict"); + PikaDict* keys = obj_getPtr(self, "_keys"); dict_deinit(dict); + dict_deinit(keys); +} + +void PikaStdData_dict_keys___init__(PikaObj* self, PikaObj* dict) { + obj_setPtr(self, "dictptr", dict); +} + +PikaObj* PikaStdData_Dict_keys(PikaObj* self) { + PikaObj* dict_keys = newNormalObj(New_PikaStdData_dict_keys); + PikaStdData_dict_keys___init__(dict_keys, self); + return dict_keys; +} + +Arg* PikaStdData_dict_keys___iter__(PikaObj* self) { + obj_setInt(self, "__iter_i", 0); + return arg_newRef(self); +} + +Arg* PikaStdData_dict_keys___next__(PikaObj* self) { + int __iter_i = args_getInt(self->list, "__iter_i"); + PikaObj* dictptr = obj_getPtr(self, "dictptr"); + PikaDict* keys = obj_getPtr(dictptr, "_keys"); + Arg* res = arg_copy(args_getArgByidex(&keys->super, __iter_i)); + if (NULL == res) { + return arg_newNull(); + } + args_setInt(self->list, "__iter_i", __iter_i + 1); + return res; +} + +char* PikaStdLib_SysObj_str(PikaObj* self, Arg* arg); +char* PikaStdData_dict_keys___str__(PikaObj* self) { + Arg* str_arg = arg_newStr("dict_keys(["); + PikaObj* dictptr = obj_getPtr(self, "dictptr"); + PikaDict* keys = obj_getPtr(dictptr, "_keys"); + + int i = 0; + while (PIKA_TRUE) { + Arg* item = args_getArgByidex(&keys->super, i); + if (NULL == 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++; + } + + str_arg = arg_strAppend(str_arg, "])"); + obj_setStr(self, "_buf", arg_getStr(str_arg)); + arg_deinit(str_arg); + return obj_getStr(self, "_buf"); +} + +char* PikaStdData_Dict___str__(PikaObj* self) { + Arg* str_arg = arg_newStr("{"); + + PikaDict* keys = obj_getPtr(self, "_keys"); + PikaDict* dict = obj_getPtr(self, "dict"); + + int i = 0; + while (PIKA_TRUE) { + Arg* item_key = args_getArgByidex(&keys->super, i); + Arg* item_val = args_getArgByidex(&dict->super, i); + if (NULL == item_key) { + break; + } + if (i != 0) { + str_arg = arg_strAppend(str_arg, ", "); + } + char* key_str = PikaStdLib_SysObj_str(self, item_key); + str_arg = arg_strAppend(str_arg, "'"); + str_arg = arg_strAppend(str_arg, key_str); + str_arg = arg_strAppend(str_arg, "'"); + str_arg = arg_strAppend(str_arg, ": "); + + char* val_str = PikaStdLib_SysObj_str(self, item_val); + str_arg = arg_strAppend(str_arg, val_str); + i++; + } + + str_arg = arg_strAppend(str_arg, "}"); + obj_setStr(self, "_buf", arg_getStr(str_arg)); + arg_deinit(str_arg); + return obj_getStr(self, "_buf"); +} + +int PikaStdData_Dict___len__(PikaObj* self) { + PikaDict* dict = obj_getPtr(self, "dict"); + return args_getSize(&dict->super); +} + +int PikaStdData_dict_keys___len__(PikaObj* self) { + PikaObj* dictptr = obj_getPtr(self, "dictptr"); + PikaDict* keys = obj_getPtr(dictptr, "_keys"); + return args_getSize(&keys->super); } diff --git a/port/cmsis-pack/pikascript/pikascript-lib/PikaStdLib/PikaStdData_FILEIO.c b/port/cmsis-pack/pikascript/pikascript-lib/PikaStdLib/PikaStdData_FILEIO.c new file mode 100644 index 000000000..ca1d90afa --- /dev/null +++ b/port/cmsis-pack/pikascript/pikascript-lib/PikaStdLib/PikaStdData_FILEIO.c @@ -0,0 +1,177 @@ +#include "PikaStdData_FILEIO.h" +#include +#include "PikaStdData_List.h" + +void PikaStdData_FILEIO_init(PikaObj* self, char* mode, char* path) { + if (obj_isArgExist(self, "_f")) { + /* already initialized */ + return; + } + FILE* f = __platform_fopen(path, mode); + if (f == NULL) { + printf("Error: can't open file %s\n", path); + return; + } + obj_setPtr(self, "_f", f); + obj_setStr(self, "_mode", mode); +} + +void PikaStdData_FILEIO_close(PikaObj* self) { + FILE* f = obj_getPtr(self, "_f"); + if (f == NULL) { + return; + } + __platform_fclose(f); + obj_setPtr(self, "_f", NULL); +} + +Arg* PikaStdData_FILEIO_read(PikaObj* self, int size) { + if (size <= 0) { + /* read all */ + size = PIKA_READ_FILE_BUFF_SIZE; + } + FILE* f = obj_getPtr(self, "_f"); + if (f == NULL) { + return NULL; + } + Arg* buf_arg = arg_newBytes(NULL, size); + uint8_t* buf = arg_getBytes(buf_arg); + /* read */ + int n = __platform_fread(buf, 1, size, f); + if (n < size) { + /* EOF */ + buf[n] = '\0'; + } + char* mode = obj_getStr(self, "_mode"); + if (strIsContain(mode, 'b')) { + /* binary */ + Arg* res = arg_newBytes(buf, n); + arg_deinit(buf_arg); + return res; + } else { + /* text */ + Arg* res = arg_newStr((char*)buf); + arg_deinit(buf_arg); + return res; + } +} + +int PikaStdData_FILEIO_write(PikaObj* self, Arg* s) { + FILE* f = obj_getPtr(self, "_f"); + int res = -1; + if (f == NULL) { + obj_setErrorCode(self, PIKA_RES_ERR_IO); + __platform_printf("Error: can't write to file\n"); + return res; + } + char* mode = obj_getStr(self, "_mode"); + if (strIsContain(mode, 'b')) { + /* binary */ + res = __platform_fwrite(arg_getBytes(s), 1, arg_getSize(s), f); + } else { + /* text */ + char* str = arg_getStr(s); + res = __platform_fwrite(str, 1, strlen(str), f); + } + return res; +} + +int PikaStdData_FILEIO_seek(PikaObj *self, int offset, PikaTuple* fromwhere){ + FILE* f = obj_getPtr(self, "_f"); + if (f == NULL) { + obj_setErrorCode(self, PIKA_RES_ERR_IO); + __platform_printf("Error: can't seek in file\n"); + return -1; + } + if (tuple_getSize(fromwhere) == 1) { + int whence = tuple_getInt(fromwhere, 0); + __platform_fseek(f, offset, whence); + return __platform_ftell(f); + } + __platform_fseek(f, offset, 0); + return __platform_ftell(f); +} + +int PikaStdData_FILEIO_tell(PikaObj* self) { + FILE* f = obj_getPtr(self, "_f"); + if (f == NULL) { + obj_setErrorCode(self, PIKA_RES_ERR_IO); + __platform_printf("Error: can't tell in file\n"); + return -1; + } + return __platform_ftell(f); +} + +char* PikaStdData_FILEIO_readline(PikaObj* self) { + FILE* f = obj_getPtr(self, "_f"); + if (f == NULL) { + obj_setErrorCode(self, PIKA_RES_ERR_IO); + __platform_printf("Error: can't read line from file\n"); + return NULL; + } + obj_setBytes(self, "_line_buff", NULL, PIKA_LINE_BUFF_SIZE); + char* line_buff = (char*)obj_getBytes(self, "_line_buff"); + while (1) { + char char_buff[2] = {0}; + int n = __platform_fread(char_buff, 1, 1, f); + if (n == 0) { + /* EOF */ + return NULL; + } + if (char_buff[0] == '\n') { + /* end of line */ + strAppend(line_buff, char_buff); + return line_buff; + } + if (strGetSize(line_buff) >= PIKA_LINE_BUFF_SIZE) { + /* line too long */ + obj_setErrorCode(self, PIKA_RES_ERR_IO); + __platform_printf("Error: line too long\n"); + return NULL; + } + strAppend(line_buff, char_buff); + } +} + +PikaObj* PikaStdData_FILEIO_readlines(PikaObj* self) { + FILE* f = obj_getPtr(self, "_f"); + if (f == NULL) { + obj_setErrorCode(self, PIKA_RES_ERR_IO); + __platform_printf("Error: can't read lines from file\n"); + return NULL; + } + PikaObj* line_list = newNormalObj(New_PikaStdData_List); + PikaStdData_List___init__(line_list); + while (1) { + char* line = PikaStdData_FILEIO_readline(self); + if (line == NULL) { + break; + } + Arg* arg_str = arg_newStr(line); + PikaStdData_List_append(line_list, arg_str); + arg_deinit(arg_str); + } + return line_list; +} + +void PikaStdData_FILEIO_writelines(PikaObj* self, PikaObj* lines) { + FILE* f = obj_getPtr(self, "_f"); + if (f == NULL) { + obj_setErrorCode(self, PIKA_RES_ERR_IO); + __platform_printf("Error: can't write lines to file\n"); + return; + } + PikaList* list = obj_getPtr(lines, "list"); + if (list == NULL) { + obj_setErrorCode(self, PIKA_RES_ERR_IO); + __platform_printf("Error: can't write lines to file\n"); + return; + } + for (size_t i = 0; i < list_getSize(list); i++) { + char* line = list_getStr(list, i); + Arg* arg_str = arg_newStr(line); + PikaStdData_FILEIO_write(self, arg_str); + arg_deinit(arg_str); + } + return; +} diff --git a/port/cmsis-pack/pikascript/pikascript-lib/PikaStdLib/PikaStdData_List.c b/port/cmsis-pack/pikascript/pikascript-lib/PikaStdLib/PikaStdData_List.c index f3fb0ff9a..db3b938c4 100644 --- a/port/cmsis-pack/pikascript/pikascript-lib/PikaStdLib/PikaStdData_List.c +++ b/port/cmsis-pack/pikascript/pikascript-lib/PikaStdLib/PikaStdData_List.c @@ -1,29 +1,14 @@ +#include "PikaStdData_List.h" #include "BaseObj.h" #include "PikaObj.h" +#include "PikaStdData_Tuple.h" +#include "dataStrs.h" void PikaStdData_List_append(PikaObj* self, Arg* arg) { - PikaList* list = obj_getPtr(self, "list"); - list_append(list, arg); + __vm_List_append(self, arg); } -int PikaStdData_List_len(PikaObj* self) { - PikaList* list = obj_getPtr(self, "list"); - return list_getSize(list); -} - -Arg* PikaStdData_List_get(PikaObj* self, int i) { - PikaList* list = obj_getPtr(self, "list"); - return arg_copy(list_getArg(list, i)); -} - -void PikaStdData_List___init__(PikaObj* self) { - if (!obj_isArgExist(self, "list")) { - PikaList* list = New_list(); - obj_setPtr(self, "list", list); - } -} - -void PikaStdData_List_set(PikaObj* self, Arg* arg, int i) { +void PikaStdData_List_set(PikaObj* self, int i, Arg* arg) { PikaList* list = obj_getPtr(self, "list"); if (PIKA_RES_OK != list_setArg(list, i, arg)) { obj_setErrorCode(self, 1); @@ -31,52 +16,36 @@ void PikaStdData_List_set(PikaObj* self, Arg* arg, int i) { } } -Arg* PikaStdData_List___iter__(PikaObj* self) { - obj_setInt(self, "__iter_i", 0); - return arg_setRef(NULL, "", self); +void PikaStdData_List___setitem__(PikaObj* self, Arg* __key, Arg* __val) { + PikaStdData_List_set(self, obj_getInt(self, "__key"), + obj_getArg(self, "__val")); } -Arg* PikaStdData_List___next__(PikaObj* self) { - int __iter_i = args_getInt(self->list, "__iter_i"); - Arg* res = PikaStdData_List_get(self, __iter_i); - if (NULL == res) { - return arg_setNull(NULL); +void PikaStdData_List___init__(PikaObj* self) { + __vm_List___init__(self); +} + +char* PikaStdLib_SysObj_str(PikaObj* self, Arg* arg); +char* PikaStdData_List___str__(PikaObj* self) { + Arg* str_arg = arg_newStr("["); + PikaList* list = obj_getPtr(self, "list"); + + int i = 0; + while (PIKA_TRUE) { + Arg* item = list_getArg(list, i); + if (NULL == 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++; } - args_setInt(self->list, "__iter_i", __iter_i + 1); - return res; -} -Arg* PikaStdData_List___getitem__(PikaObj* self) { - return PikaStdData_List_get(self, obj_getInt(self, "__key")); -} - -void PikaStdData_List___setitem__(PikaObj* self) { - PikaStdData_List_set(self, obj_getArg(self, "__val"), - obj_getInt(self, "__key")); -} - -void PikaStdData_ByteArray_fromString(PikaObj* self, char* s) { - for (uint32_t i = 0; i < strGetSize(s); i++) { - obj_setInt(self, "__val", (int)s[i]); - PIKA_PYTHON_BEGIN - /* clang-format off */ - PIKA_PYTHON( - append(__val) - ) - /* clang-format on */ - const uint8_t bytes[] = { - 0x08, 0x00, /* instruct array size */ - 0x10, 0x81, 0x01, 0x00, 0x00, 0x02, 0x07, 0x00, /* instruct array */ - 0x0e, 0x00, /* const pool size */ - 0x00, 0x5f, 0x5f, 0x76, 0x61, 0x6c, 0x00, 0x61, - 0x70, 0x70, 0x65, 0x6e, 0x64, 0x00, /* const pool */ - }; - pikaVM_runByteCode(self, (uint8_t*)bytes); - PIKA_PYTHON_END - } -} - -void PikaStdData_List___del__(PikaObj* self) { - Args* list = obj_getPtr(self, "list"); - args_deinit(list); + 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 bc10981c5..c0297ee6b 100644 --- a/port/cmsis-pack/pikascript/pikascript-lib/PikaStdLib/PikaStdData_String.c +++ b/port/cmsis-pack/pikascript/pikascript-lib/PikaStdLib/PikaStdData_String.c @@ -1,9 +1,11 @@ #include "PikaStdData_String.h" +#include "PikaStdData_List.h" #include "PikaStdData_String_Util.h" +#include "dataStrs.h" Arg* PikaStdData_String___iter__(PikaObj* self) { obj_setInt(self, "__iter_i", 0); - return arg_setRef(NULL, "", self); + return arg_newRef(self); } void PikaStdData_String_set(PikaObj* self, char* s) { @@ -26,9 +28,9 @@ Arg* PikaStdData_String___next__(PikaObj* self) { char char_buff[] = " "; if (__iter_i < len) { char_buff[0] = str[__iter_i]; - res = arg_setStr(NULL, "", (char*)char_buff); + res = arg_newStr((char*)char_buff); } else { - return arg_setNull(NULL); + return arg_newNull(); } args_setInt(self->list, "__iter_i", __iter_i + 1); return res; @@ -41,9 +43,9 @@ Arg* PikaStdData_String___getitem__(PikaObj* self, Arg* __key) { char char_buff[] = " "; if (key_i < len) { char_buff[0] = str[key_i]; - return arg_setStr(NULL, "", (char*)char_buff); + return arg_newStr((char*)char_buff); } else { - return arg_setNull(NULL); + return arg_newNull(); } } @@ -64,12 +66,12 @@ char* PikaStdData_String___str__(PikaObj* self) { return obj_getStr(self, "str"); } -int PikaStdData_String_startwith(PikaObj *self, char* prefix){ - char *str = obj_getStr(self,"str"); - char *p = prefix; - int i=0; - while(*p!='\0'){ - if(*p!=str[i]) +int PikaStdData_String_startwith(PikaObj* self, char* prefix) { + char* str = obj_getStr(self, "str"); + char* p = prefix; + int i = 0; + while (*p != '\0') { + if (*p != str[i]) return 0; p++; i++; @@ -77,12 +79,12 @@ int PikaStdData_String_startwith(PikaObj *self, char* prefix){ return 1; } -int PikaStdData_String_endwith(PikaObj *self, char* suffix){ - char *str = obj_getStr(self,"str"); - int len1=strlen(str); - int len2=strlen(suffix); - while(len2>=1){ - if(suffix[len2-1]!=str[len1-1]) +int PikaStdData_String_endwith(PikaObj* self, char* suffix) { + char* str = obj_getStr(self, "str"); + int len1 = strlen(str); + int len2 = strlen(suffix); + while (len2 >= 1) { + if (suffix[len2 - 1] != str[len1 - 1]) return 0; len2--; len1--; @@ -90,57 +92,127 @@ int PikaStdData_String_endwith(PikaObj *self, char* suffix){ return 1; } -int PikaStdData_String_isdigit(PikaObj *self){ - char *str = obj_getStr(self,"str"); - int i=0; - while(str[i]!='\0'){ - if(!isdigit((int)str[i])) - return 0; - i++; +int PikaStdData_String_isdigit(PikaObj* self) { + char* str = obj_getStr(self, "str"); + int i = 0; + while (str[i] != '\0') { + if (!isdigit((int)str[i])) + return 0; + i++; } return 1; } -int PikaStdData_String_islower(PikaObj *self){ - char *str = obj_getStr(self,"str"); - int i=0; - while(str[i]!='\0'){ - if(!islower((int)str[i])) - return 0; - i++; +int PikaStdData_String_islower(PikaObj* self) { + char* str = obj_getStr(self, "str"); + int i = 0; + while (str[i] != '\0') { + if (!islower((int)str[i])) + return 0; + i++; } return 1; } -int PikaStdData_String_isalnum(PikaObj *self){ - char *str = obj_getStr(self,"str"); - int i=0; - while(str[i]!='\0'){ - if(!isalnum((int)str[i])) - return 0; - i++; +int PikaStdData_String_isalnum(PikaObj* self) { + char* str = obj_getStr(self, "str"); + int i = 0; + while (str[i] != '\0') { + if (!isalnum((int)str[i])) + return 0; + i++; } return 1; } -int PikaStdData_String_isalpha(PikaObj *self){ - char *str = obj_getStr(self,"str"); - int i=0; - while(str[i]!='\0'){ - if(!isalpha((int)str[i])) - return 0; - i++; +int PikaStdData_String_isalpha(PikaObj* self) { + char* str = obj_getStr(self, "str"); + int i = 0; + while (str[i] != '\0') { + if (!isalpha((int)str[i])) + return 0; + i++; } return 1; } -int PikaStdData_String_isspace(PikaObj *self){ - char *str = obj_getStr(self,"str"); - int i=0; - while(str[i]!='\0'){ - if(!isspace((int)str[i])) - return 0; - i++; +int PikaStdData_String_isspace(PikaObj* self) { + char* str = obj_getStr(self, "str"); + int i = 0; + while (str[i] != '\0') { + if (!isspace((int)str[i])) + return 0; + i++; } return 1; -} \ No newline at end of file +} + +PikaObj* PikaStdData_String_split(PikaObj* self, char* s) { + /* 创建 list 对象 */ + PikaObj* list = newNormalObj(New_PikaStdData_List); + /* 初始化 list */ + PikaStdData_List___init__(list); + + Args buffs = {0}; + 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); + /* 用 arg_set 的 api 创建 arg */ + Arg* token_arg = arg_newStr(token); + /* 添加到 list 对象 */ + PikaStdData_List_append(list, token_arg); + /* 销毁 arg */ + arg_deinit(token_arg); + } + + strsDeinit(&buffs); + return list; +} + +int PikaStdData_String___len__(PikaObj* self) { + char* str = obj_getStr(self, "str"); + return strGetSize(str); +} + +char* PikaStdData_String_strip(PikaObj* self) { + Args buffs = {0}; + 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); + break; + } + } + + for (int i = strGetSize(str) - 1; i >= 0; i--) { + if (str[i] != ' ') { + str[i + 1] = '\0'; + break; + } + } + + obj_setStr(self, "_buf", str_start); + strsDeinit(&buffs); + return obj_getStr(self, "_buf"); +} + +char* PikaStdData_String_replace(PikaObj* self, char* old, char* new) { + Args buffs = {0}; + 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; +} 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 ff3b139e3..eefe5573b 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 @@ -#include \ No newline at end of file +#include diff --git a/port/cmsis-pack/pikascript/pikascript-lib/PikaStdLib/PikaStdData_Tuple.c b/port/cmsis-pack/pikascript/pikascript-lib/PikaStdLib/PikaStdData_Tuple.c new file mode 100644 index 000000000..68e4d541f --- /dev/null +++ b/port/cmsis-pack/pikascript/pikascript-lib/PikaStdLib/PikaStdData_Tuple.c @@ -0,0 +1,70 @@ +#include "PikaStdData_Tuple.h" +#include "PikaVM.h" +#include "dataStrs.h" + +int PikaStdData_Tuple_len(PikaObj* self) { + PikaList* list = obj_getPtr(self, "list"); + return list_getSize(list); +} + +Arg* PikaStdData_Tuple_get(PikaObj* self, int i) { + PikaList* list = obj_getPtr(self, "list"); + return arg_copy(list_getArg(list, i)); +} + +void PikaStdData_Tuple___init__(PikaObj* self) { + __vm_List___init__(self); +} + +Arg* PikaStdData_Tuple___iter__(PikaObj* self) { + obj_setInt(self, "__iter_i", 0); + return arg_newRef(self); +} + +Arg* PikaStdData_Tuple___next__(PikaObj* self) { + int __iter_i = args_getInt(self->list, "__iter_i"); + Arg* res = PikaStdData_Tuple_get(self, __iter_i); + if (NULL == res) { + return arg_newNull(); + } + args_setInt(self->list, "__iter_i", __iter_i + 1); + return res; +} + +Arg* PikaStdData_Tuple___getitem__(PikaObj* self, Arg* __key) { + return PikaStdData_Tuple_get(self, obj_getInt(self, "__key")); +} + +void PikaStdData_Tuple___del__(PikaObj* self) { + Args* list = obj_getPtr(self, "list"); + args_deinit(list); +} + +char* PikaStdLib_SysObj_str(PikaObj* self, Arg* arg); +char* PikaStdData_Tuple___str__(PikaObj* self) { + Arg* str_arg = arg_newStr("("); + PikaList* list = obj_getPtr(self, "list"); + + int i = 0; + while (PIKA_TRUE) { + Arg* item = list_getArg(list, i); + if (NULL == 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++; + } + + str_arg = arg_strAppend(str_arg, ")"); + obj_setStr(self, "_buf", arg_getStr(str_arg)); + arg_deinit(str_arg); + return obj_getStr(self, "_buf"); +} + +int PikaStdData_Tuple___len__(PikaObj* self) { + return PikaStdData_Tuple_len(self); +} diff --git a/port/cmsis-pack/pikascript/pikascript-lib/PikaStdLib/PikaStdData_Utils.c b/port/cmsis-pack/pikascript/pikascript-lib/PikaStdLib/PikaStdData_Utils.c index 2384f0ad7..be15c5d52 100644 --- a/port/cmsis-pack/pikascript/pikascript-lib/PikaStdLib/PikaStdData_Utils.c +++ b/port/cmsis-pack/pikascript/pikascript-lib/PikaStdLib/PikaStdData_Utils.c @@ -1,4 +1,5 @@ #include "PikaStdData_Utils.h" +#include "dataStrs.h" Arg* PikaStdData_Utils_int_to_bytes(PikaObj* self, int val) { if (val > 0xFF) { @@ -6,8 +7,8 @@ Arg* PikaStdData_Utils_int_to_bytes(PikaObj* self, int val) { __platform_printf( "OverflowError: cannot convert value larger than 0xFF to " "bytes\r\n"); - return arg_setNull(NULL); + return arg_newNull(); } uint8_t val_bytes = (uint8_t)val; - return arg_setBytes(NULL, "", &val_bytes, 1); + return arg_newBytes(&val_bytes, 1); } 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 7eb538536..a687144ca 100644 --- a/port/cmsis-pack/pikascript/pikascript-lib/PikaStdLib/PikaStdLib_RangeObj.c +++ b/port/cmsis-pack/pikascript/pikascript-lib/PikaStdLib/PikaStdLib_RangeObj.c @@ -17,8 +17,8 @@ Arg* PikaStdLib_RangeObj___next__(PikaObj* self) { } /* exit */ if (iter_i >= end) { - return arg_setNull(NULL); + return arg_newNull(); } args_setInt(self->list, "iter_i", iter_i + foot); - return arg_setInt(NULL, "", iter_i); + return arg_newInt(iter_i); } diff --git a/port/cmsis-pack/pikascript/pikascript-lib/PikaStdLib/PikaStdLib_StringObj.c b/port/cmsis-pack/pikascript/pikascript-lib/PikaStdLib/PikaStdLib_StringObj.c index 0d518bd29..ad15ee2a4 100644 --- a/port/cmsis-pack/pikascript/pikascript-lib/PikaStdLib/PikaStdLib_StringObj.c +++ b/port/cmsis-pack/pikascript/pikascript-lib/PikaStdLib/PikaStdLib_StringObj.c @@ -1,5 +1,5 @@ #include "PikaObj.h" Arg* PikaStdLib_StringObj___next__(PikaObj* self) { - return arg_setNull(NULL); + return arg_newNull(); } 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 8dbbf498c..4f28ec426 100644 --- a/port/cmsis-pack/pikascript/pikascript-lib/PikaStdLib/PikaStdLib_SysObj.c +++ b/port/cmsis-pack/pikascript/pikascript-lib/PikaStdLib/PikaStdLib_SysObj.c @@ -1,4 +1,5 @@ #include "PikaStdLib_SysObj.h" +#include "PikaStdData_FILEIO.h" #include "PikaStdLib_RangeObj.h" #include "PikaStdLib_StringObj.h" #include "PikaVM.h" @@ -19,49 +20,55 @@ void PikaStdLib_SysObj_remove(PikaObj* self, char* argPath) { } } -void PikaStdLib_SysObj_type(PikaObj* self, Arg* arg) { +Arg* PikaStdLib_SysObj_type(PikaObj* self, Arg* arg) { if (NULL == arg) { obj_setSysOut(self, "[error] type: arg no found."); obj_setErrorCode(self, 1); - return; + return arg_newNull(); } ArgType type = arg_getType(arg); if (ARG_TYPE_INT == type) { - obj_setSysOut(self, ""); - return; + return arg_newStr(""); } if (ARG_TYPE_FLOAT == type) { - obj_setSysOut(self, ""); - return; + return arg_newStr(""); } if (ARG_TYPE_STRING == type) { - obj_setSysOut(self, ""); - return; + return arg_newStr(""); } - if (ARG_TYPE_OBJECT == type) { - obj_setSysOut(self, ""); - return; + if (argType_isObject(type)) { + PikaObj* obj = arg_getPtr(arg); + NewFun clsptr = obj_getClass(obj); + PikaObj* New_PikaStdData_List(Args * args); + if (clsptr == New_PikaStdData_List) { + return arg_newStr(""); + } + /* dict */ + PikaObj* New_PikaStdData_Dict(Args * args); + if (clsptr == New_PikaStdData_Dict) { + return arg_newStr(""); + } + return arg_newStr(""); } if (ARG_TYPE_OBJECT_META == type) { - obj_setSysOut(self, ""); - return; + return arg_newStr(""); } if (ARG_TYPE_BYTES == type) { - obj_setSysOut(self, ""); - return; + return arg_newStr(""); } if (ARG_TYPE_METHOD_NATIVE == type) { - obj_setSysOut(self, ""); - return; + return arg_newStr(""); } if (ARG_TYPE_METHOD_OBJECT == type) { - obj_setSysOut(self, ""); - return; + return arg_newStr(""); } if (ARG_TYPE_METHOD_STATIC == type) { - obj_setSysOut(self, ""); - return; + return arg_newStr(""); } + if (ARG_TYPE_NONE == type) { + return arg_newStr(""); + } + return arg_newNull(); } double PikaStdLib_SysObj_float(PikaObj* self, Arg* arg) { @@ -104,9 +111,10 @@ int PikaStdLib_SysObj_int(PikaObj* self, Arg* arg) { } char* PikaStdLib_SysObj_str(PikaObj* self, Arg* arg) { + obj_removeArg(self, "__buf"); ArgType type = arg_getType(arg); Args buffs = {0}; - char* res = NULL; + char* res = ""; if (ARG_TYPE_INT == type) { int val = arg_getInt(arg); res = strsFormat(&buffs, 11, "%d", val); @@ -121,6 +129,12 @@ char* PikaStdLib_SysObj_str(PikaObj* self, Arg* arg) { res = (char*)arg_getBytes(arg); goto exit; } + if (ARG_TYPE_STRING == type) { + res = arg_getStr(arg); + } + if (ARG_TYPE_NONE == type) { + res = "None"; + } if (argType_isObject(type)) { res = obj_toStr(arg_getPtr(arg)); if (NULL != res) { @@ -146,7 +160,7 @@ Arg* PikaStdLib_SysObj_iter(PikaObj* self, Arg* arg) { /* object */ if (argType_isObject(arg_getType(arg))) { PikaObj* arg_obj = arg_getPtr(arg); - NewFun _clsptr = obj_getPtr(arg_obj, "_clsptr"); + NewFun _clsptr = (NewFun)arg_obj->constructor; if (_clsptr == New_PikaStdLib_RangeObj) { /* found RangeObj, return directly */ return arg_copy(arg); @@ -167,7 +181,7 @@ Arg* PikaStdLib_SysObj_iter(PikaObj* self, Arg* arg) { obj_removeArg(arg_obj, "__res"); return res; } - return arg_setNull(NULL); + return arg_newNull(); } Arg* PikaStdLib_SysObj_range(PikaObj* self, int a1, int a2) { @@ -178,96 +192,61 @@ Arg* PikaStdLib_SysObj_range(PikaObj* self, int a1, int a2) { return obj_arg; } -Arg* PikaStdLib_SysObj___getitem__(PikaObj* self, Arg* key, Arg* obj) { - ArgType obj_type = arg_getType(obj); - int index = 0; - if (ARG_TYPE_INT == arg_getType(key)) { - index = arg_getInt(key); - } - if (ARG_TYPE_STRING == obj_type) { - char* str_pyload = arg_getStr(obj); - char char_buff[] = " "; - if (index < 0) { - index = strGetSize(str_pyload) + index; - } - char_buff[0] = str_pyload[index]; - return arg_setStr(NULL, "", char_buff); - } - if (ARG_TYPE_BYTES == obj_type) { - uint8_t* bytes_pyload = arg_getBytes(obj); - uint8_t byte_buff[] = " "; - if (index < 0) { - index = arg_getBytesSize(obj) + index; - } - byte_buff[0] = bytes_pyload[index]; - return arg_setBytes(NULL, "", byte_buff, 1); - } - if (argType_isObject(obj_type)) { - PikaObj* arg_obj = arg_getPtr(obj); - obj_setArg(arg_obj, "__key", key); - // pikaVM_runAsm(arg_obj, - // "B0\n" - // "1 REF __key\n" - // "0 RUN __getitem__\n" - // "0 OUT __res\n"); - const uint8_t bytes[] = { - 0x0c, 0x00, /* instruct array size */ - 0x10, 0x81, 0x01, 0x00, 0x00, 0x02, 0x07, 0x00, 0x00, 0x04, 0x0f, - 0x00, - /* instruct array */ - 0x15, 0x00, /* const pool size */ - 0x00, 0x5f, 0x5f, 0x6b, 0x65, 0x79, 0x00, 0x5f, 0x5f, 0x67, 0x65, - 0x74, 0x5f, 0x5f, 0x00, 0x5f, 0x5f, 0x72, 0x65, 0x73, - 0x00, /* const pool */ - }; - pikaVM_runByteCode(arg_obj, (uint8_t*)bytes); - return arg_copy(args_getArg(arg_obj->list, "__res")); - } - return arg_setNull(NULL); +Arg* PikaStdLib_SysObj___getitem__(PikaObj* self, Arg* obj, Arg* key) { + return __vm_get(self, key, obj); } -void PikaStdLib_SysObj___setitem__(PikaObj* self, - Arg* key, - Arg* obj, - char* obj_str, - Arg* val) { +Arg* PikaStdLib_SysObj___setitem__(PikaObj* self, + Arg* obj, + Arg* key, + Arg* val) { ArgType obj_type = arg_getType(obj); if (ARG_TYPE_STRING == obj_type) { int index = arg_getInt(key); char* str_val = arg_getStr(val); char* str_pyload = arg_getStr(obj); str_pyload[index] = str_val[0]; - obj_setStr(self, obj_str, str_pyload); + return arg_newStr(str_pyload); } if (ARG_TYPE_BYTES == obj_type) { int index = arg_getInt(key); - uint8_t* bytes_val = arg_getBytes(val); + uint8_t byte_val = 0; + if (ARG_TYPE_BYTES == arg_getType(val)) { + uint8_t* bytes_val = arg_getBytes(val); + byte_val = bytes_val[0]; + } + if (ARG_TYPE_INT == arg_getType(val)) { + byte_val = arg_getInt(val); + } uint8_t* bytes_pyload = arg_getBytes(obj); size_t bytes_len = arg_getBytesSize(obj); - bytes_pyload[index] = bytes_val[0]; - obj_setBytes(self, obj_str, bytes_pyload, bytes_len); + bytes_pyload[index] = byte_val; + return arg_newBytes(bytes_pyload, bytes_len); } if (argType_isObject(obj_type)) { PikaObj* arg_obj = arg_getPtr(obj); obj_setArg(arg_obj, "__key", key); obj_setArg(arg_obj, "__val", val); - // pikaVM_runAsm(arg_obj, - // "B0\n" - // "1 REF __key\n" - // "1 REF __val\n" - // "0 RUN __setitem__\n"); + /* clang-format off */ + PIKA_PYTHON( + __setitem__(__key, __val) + ) + /* clang-format on */ const uint8_t bytes[] = { 0x0c, 0x00, /* instruct array size */ 0x10, 0x81, 0x01, 0x00, 0x10, 0x01, 0x07, 0x00, 0x00, 0x02, 0x0d, 0x00, /* instruct array */ - 0x15, 0x00, /* const pool size */ + 0x19, 0x00, /* const pool size */ 0x00, 0x5f, 0x5f, 0x6b, 0x65, 0x79, 0x00, 0x5f, 0x5f, 0x76, 0x61, - 0x6c, 0x00, 0x5f, 0x5f, 0x73, 0x65, 0x74, 0x5f, 0x5f, - 0x00, /* const pool */ + 0x6c, 0x00, 0x5f, 0x5f, 0x73, 0x65, 0x74, 0x69, 0x74, 0x65, 0x6d, + 0x5f, 0x5f, 0x00, + /* const pool */ }; pikaVM_runByteCode(arg_obj, (uint8_t*)bytes); + return arg_newRef(arg_obj); } + return NULL; } int PikaStdLib_SysObj_len(PikaObj* self, Arg* arg) { @@ -277,34 +256,61 @@ int PikaStdLib_SysObj_len(PikaObj* self, Arg* arg) { if (ARG_TYPE_BYTES == arg_getType(arg)) { return arg_getBytesSize(arg); } + + if (argType_isObject(arg_getType(arg))) { + PikaObj* arg_obj = arg_getPtr(arg); + Arg* method_arg = obj_getMethodArg(arg_obj, "__len__"); + if (NULL != method_arg) { + arg_deinit(method_arg); + + /* clang-format off */ + PIKA_PYTHON( + __res = __len__() + ) + /* clang-format on */ + const uint8_t bytes[] = { + 0x08, 0x00, /* instruct array size */ + 0x00, 0x82, 0x01, 0x00, 0x00, 0x04, 0x09, 0x00, /* instruct + array */ + 0x0f, 0x00, /* const pool size */ + 0x00, 0x5f, 0x5f, 0x6c, 0x65, 0x6e, 0x5f, 0x5f, 0x00, + 0x5f, 0x5f, 0x72, 0x65, 0x73, 0x00, /* const pool */ + }; + pikaVM_runByteCode(arg_obj, (uint8_t*)bytes); + return obj_getInt(arg_obj, "__res"); + } + } + obj_setErrorCode(self, 1); __platform_printf("[Error] len: arg type not support\r\n"); return -1; } Arg* PikaStdLib_SysObj_list(PikaObj* self) { -#if PIKA_BUILTIN_LIST_ENABLE +#if PIKA_BUILTIN_STRUCT_ENABLE PikaObj* New_PikaStdData_List(Args * args); - return arg_newMetaObj(New_PikaStdData_List); -#endif + return arg_newDirectObj(New_PikaStdData_List); +#else obj_setErrorCode(self, 1); __platform_printf("[Error] built-in list is not enabled.\r\n"); - return arg_setNull(NULL); + return arg_newNull(); +#endif } Arg* PikaStdLib_SysObj_dict(PikaObj* self) { -#if PIKA_BUILTIN_DICT_ENABLE +#if PIKA_BUILTIN_STRUCT_ENABLE PikaObj* New_PikaStdData_Dict(Args * args); - return arg_newMetaObj(New_PikaStdData_Dict); -#endif + return arg_newDirectObj(New_PikaStdData_Dict); +#else obj_setErrorCode(self, 1); __platform_printf("[Error] built-in dist is not enabled.\r\n"); - return arg_setNull(NULL); + return arg_newNull(); +#endif } char* PikaStdLib_SysObj_hex(PikaObj* self, int val) { char buff[PIKA_SPRINTF_BUFF_SIZE] = {0}; - if (val > 0) { + if (val >= 0) { __platform_sprintf(buff, "0x%02x", val); } else { __platform_sprintf(buff, "-0x%02x", -val); @@ -333,7 +339,7 @@ Arg* PikaStdLib_SysObj_bytes(PikaObj* self, Arg* val) { if (ARG_TYPE_INT == type) { int size = arg_getInt(val); /* src is NULL so the bytes are all '\0' */ - Arg* bytes = arg_setBytes(NULL, "", NULL, size); + Arg* bytes = arg_newBytes(NULL, size); return bytes; } if (ARG_TYPE_BYTES == type) { @@ -341,12 +347,12 @@ Arg* PikaStdLib_SysObj_bytes(PikaObj* self, Arg* val) { } if (ARG_TYPE_STRING == type) { int size = strGetSize(arg_getStr(val)); - Arg* bytes = arg_setBytes(NULL, "", (uint8_t*)arg_getStr(val), size); + Arg* bytes = arg_newBytes((uint8_t*)arg_getStr(val), size); return bytes; } obj_setErrorCode(self, 1); __platform_printf("Error: input arg type not supported.\r\n"); - return arg_setNull(NULL); + return arg_newNull(); } Arg* PikaStdLib_SysObj___slice__(PikaObj* self, @@ -354,70 +360,7 @@ Arg* PikaStdLib_SysObj___slice__(PikaObj* self, Arg* obj, Arg* start, int step) { -#if PIKA_SYNTEX_ITEM_SLICE_ENABLE - /* No interger index only support __getitem__ */ - if (!(arg_getType(start) == ARG_TYPE_INT && - arg_getType(end) == ARG_TYPE_INT)) { - return PikaStdLib_SysObj___getitem__(self, start, obj); - } - - int start_i = arg_getInt(start); - int end_i = arg_getInt(end); - - /* __slice__ is equal to __getitem__ */ - if (end_i - start_i == 1) { - return PikaStdLib_SysObj___getitem__(self, start, obj); - } - - if (ARG_TYPE_STRING == arg_getType(obj)) { - size_t len = strGetSize(arg_getStr(obj)); - if (start_i < 0) { - start_i += len; - } - if (end_i < 0) { - end_i += len + 1; - } - Arg* sliced_arg = arg_setStr(NULL, "", ""); - for (int i = start_i; i < end_i; i++) { - Arg* i_arg = arg_setInt(NULL, "", i); - Arg* item_arg = PikaStdLib_SysObj___getitem__(self, i_arg, obj); - sliced_arg = arg_strAppend(sliced_arg, arg_getStr(item_arg)); - arg_deinit(item_arg); - arg_deinit(i_arg); - } - return sliced_arg; - } - - if (ARG_TYPE_BYTES == arg_getType(obj)) { - size_t len = arg_getBytesSize(obj); - if (start_i < 0) { - start_i += len; - } - if (end_i < 0) { - end_i += len + 1; - } - Arg* sliced_arg = arg_setBytes(NULL, "", NULL, 0); - for (int i = start_i; i < end_i; i++) { - Arg* i_arg = arg_setInt(NULL, "", i); - Arg* item_arg = PikaStdLib_SysObj___getitem__(self, i_arg, obj); - uint8_t* bytes_origin = arg_getBytes(sliced_arg); - size_t size_origin = arg_getBytesSize(sliced_arg); - Arg* sliced_arg_new = arg_setBytes(NULL, "", NULL, size_origin + 1); - __platform_memcpy(arg_getBytes(sliced_arg_new), bytes_origin, - size_origin); - __platform_memcpy(arg_getBytes(sliced_arg_new) + size_origin, - arg_getBytes(item_arg), 1); - arg_deinit(sliced_arg); - sliced_arg = sliced_arg_new; - arg_deinit(item_arg); - arg_deinit(i_arg); - } - return sliced_arg; - } - return arg_setNull(NULL); -#else - return PikaStdLib_SysObj___getitem__(self, start, obj); -#endif + return __vm_slice(self, end, obj, start, step); } static void __print_arg(PikaObj* self, Arg* val) { @@ -452,7 +395,7 @@ static void __print_arg(PikaObj* self, Arg* val) { void PikaStdLib_SysObj_print(PikaObj* self, PikaTuple* val) { int arg_size = tuple_getSize(val); - Arg* print_out_arg = arg_setStr(NULL, "", ""); + 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); @@ -490,6 +433,7 @@ void PikaStdLib_SysObj_printNoEnd(PikaObj* self, Arg* val) { } char* PikaStdLib_SysObj_cformat(PikaObj* self, char* fmt, PikaTuple* var) { +#if PIKA_SYNTAX_FORMAT_ENABLE Args buffs = {0}; pikaMemMaxReset(); char* res = strsFormatList(&buffs, fmt, &var->super); @@ -497,6 +441,11 @@ char* PikaStdLib_SysObj_cformat(PikaObj* self, char* fmt, PikaTuple* var) { res = obj_getStr(self, "_buf"); strsDeinit(&buffs); return res; +#else + obj_setErrorCode(self, 1); + __platform_printf("[Error] PIKA_SYNTAX_FORMAT_ENABLE is not enabled.\r\n"); + return NULL; +#endif } int PikaStdLib_SysObj_id(PikaObj* self, Arg* obj) { @@ -508,3 +457,15 @@ int PikaStdLib_SysObj_id(PikaObj* self, Arg* obj) { } return ptr & (0x7FFFFFFF); } + +PikaObj* PikaStdLib_SysObj_open(PikaObj* self, char* path, char* mode) { +#if PIKA_FILEIO_ENABLE + PikaObj* file = newNormalObj(New_PikaStdData_FILEIO); + PikaStdData_FILEIO_init(file, mode, path); + return file; +#else + obj_setErrorCode(self, 1); + __platform_printf("[Error] PIKA_FILEIO_ENABLE is not enabled.\r\n"); + return NULL; +#endif +} diff --git a/port/cmsis-pack/pikascript/pikascript-lib/PikaStdLib/PikaStdTask_Task.c b/port/cmsis-pack/pikascript/pikascript-lib/PikaStdLib/PikaStdTask_Task.c index 0c04f60af..5c54a02d1 100644 --- a/port/cmsis-pack/pikascript/pikascript-lib/PikaStdLib/PikaStdTask_Task.c +++ b/port/cmsis-pack/pikascript/pikascript-lib/PikaStdLib/PikaStdTask_Task.c @@ -1,10 +1,8 @@ #include "BaseObj.h" #include "PikaVM.h" -extern PikaObj* __pikaMain; void PikaStdTask_Task___init__(PikaObj* self) { obj_setInt(self, "is_period", 0); - obj_setRef(__pikaMain, "__calls", obj_getObj(self, "calls")); } void PikaStdTask_Task_call_always(PikaObj* self, Arg* fun_todo) { @@ -90,116 +88,104 @@ void PikaStdTask_Task_call_period_ms(PikaObj* self, } void PikaStdTask_Task_run_once(PikaObj* self) { - /* transfer the tick to pikaMain */ - obj_setInt(__pikaMain, "__tick", obj_getInt(self, "tick")); /* clang-format off */ PIKA_PYTHON( - len = __calls.len() - mode = 'none' - info_index = 0 - for i in range(0, len): - if len == 0: - break - if info_index == 0: - mode = __calls[i] - info_index = 1 - elif info_index == 1: - if mode == 'always': - todo = __calls[i] + len = calls.len() + mode = 'none' + info_index = 0 + for i in range(0, len): + if len == 0: + break + if info_index == 0: + mode = calls[i] + info_index = 1 + elif info_index == 1: + if mode == 'always': + todo = calls[i] + todo() + info_index = 0 + elif mode == 'when': + when = calls[i] + info_index = 2 + elif mode == 'period_ms': + period_ms = calls[i] + info_index = 2 + elif info_index == 2: + if mode == 'when': + if when(): + todo = calls[i] todo() - info_index = 0 - elif mode == 'when': - when = __calls[i] - info_index = 2 - elif mode == 'period_ms': - period_ms = __calls[i] - info_index = 2 - elif info_index == 2: - if mode == 'when': - if when(): - todo = __calls[i] - todo() - info_index = 0 - elif mode == 'period_ms': - todo = __calls[i] - info_index = 3 - elif info_index == 3: - if mode == 'period_ms': - if __tick > __calls[i]: - todo() - __calls[i] = __tick + period_ms - info_index = 0 + info_index = 0 + elif mode == 'period_ms': + todo = calls[i] + info_index = 3 + elif info_index == 3: + if mode == 'period_ms': + if tick > calls[i]: + todo() + calls[i] = tick + period_ms + info_index = 0 ) /* clang-format on */ const uint8_t bytes[] = { - 0x60, 0x02, /* instruct array size */ - 0x00, 0x82, 0x01, 0x00, 0x00, 0x04, 0x0d, 0x00, 0x00, 0x83, 0x11, 0x00, - 0x00, 0x04, 0x16, 0x00, 0x00, 0x85, 0x1b, 0x00, 0x00, 0x04, 0x1d, 0x00, - 0x20, 0x85, 0x1b, 0x00, 0x20, 0x01, 0x0d, 0x00, 0x10, 0x02, 0x28, 0x00, - 0x00, 0x02, 0x2e, 0x00, 0x00, 0x04, 0x33, 0x00, 0x00, 0x82, 0x37, 0x00, - 0x00, 0x04, 0x44, 0x00, 0x00, 0x0d, 0x44, 0x00, 0x00, 0x07, 0x46, 0x00, - 0x11, 0x81, 0x0d, 0x00, 0x11, 0x05, 0x1b, 0x00, 0x01, 0x08, 0x48, 0x00, - 0x01, 0x07, 0x4b, 0x00, 0x02, 0x8e, 0x00, 0x00, 0x11, 0x81, 0x1d, 0x00, - 0x11, 0x05, 0x1b, 0x00, 0x01, 0x08, 0x48, 0x00, 0x01, 0x07, 0x4b, 0x00, - 0x12, 0x81, 0x4d, 0x00, 0x12, 0x01, 0x44, 0x00, 0x22, 0x01, 0x44, 0x00, - 0x22, 0x05, 0x4b, 0x00, 0x12, 0x08, 0x55, 0x00, 0x12, 0x05, 0x4b, 0x00, - 0x02, 0x02, 0x57, 0x00, 0x02, 0x04, 0x16, 0x00, 0x02, 0x85, 0x4b, 0x00, - 0x02, 0x04, 0x1d, 0x00, 0x01, 0x8b, 0x4b, 0x00, 0x11, 0x01, 0x1d, 0x00, - 0x11, 0x05, 0x4b, 0x00, 0x01, 0x08, 0x48, 0x00, 0x01, 0x07, 0x4b, 0x00, - 0x12, 0x81, 0x16, 0x00, 0x12, 0x03, 0x61, 0x00, 0x02, 0x08, 0x48, 0x00, - 0x02, 0x07, 0x4b, 0x00, 0x13, 0x81, 0x4d, 0x00, 0x13, 0x01, 0x44, 0x00, - 0x23, 0x01, 0x44, 0x00, 0x23, 0x05, 0x4b, 0x00, 0x13, 0x08, 0x55, 0x00, - 0x13, 0x05, 0x4b, 0x00, 0x03, 0x02, 0x57, 0x00, 0x03, 0x04, 0x68, 0x00, - 0x03, 0x82, 0x68, 0x00, 0x03, 0x85, 0x1b, 0x00, 0x03, 0x04, 0x1d, 0x00, - 0x02, 0x8b, 0x4b, 0x00, 0x12, 0x01, 0x16, 0x00, 0x12, 0x03, 0x6d, 0x00, - 0x02, 0x08, 0x48, 0x00, 0x02, 0x07, 0x4b, 0x00, 0x13, 0x81, 0x4d, 0x00, - 0x13, 0x01, 0x44, 0x00, 0x23, 0x01, 0x44, 0x00, 0x23, 0x05, 0x4b, 0x00, - 0x13, 0x08, 0x55, 0x00, 0x13, 0x05, 0x4b, 0x00, 0x03, 0x02, 0x57, 0x00, - 0x03, 0x04, 0x6d, 0x00, 0x03, 0x85, 0x46, 0x00, 0x03, 0x04, 0x1d, 0x00, - 0x02, 0x8b, 0x4b, 0x00, 0x12, 0x01, 0x16, 0x00, 0x12, 0x03, 0x72, 0x00, - 0x02, 0x08, 0x48, 0x00, 0x02, 0x07, 0x4b, 0x00, 0x13, 0x81, 0x4d, 0x00, - 0x13, 0x01, 0x44, 0x00, 0x23, 0x01, 0x44, 0x00, 0x23, 0x05, 0x4b, 0x00, - 0x13, 0x08, 0x55, 0x00, 0x13, 0x05, 0x4b, 0x00, 0x03, 0x02, 0x57, 0x00, - 0x03, 0x04, 0x72, 0x00, 0x03, 0x85, 0x46, 0x00, 0x03, 0x04, 0x1d, 0x00, - 0x01, 0x8b, 0x4b, 0x00, 0x11, 0x01, 0x1d, 0x00, 0x11, 0x05, 0x46, 0x00, - 0x01, 0x08, 0x48, 0x00, 0x01, 0x07, 0x4b, 0x00, 0x12, 0x81, 0x16, 0x00, - 0x12, 0x03, 0x6d, 0x00, 0x02, 0x08, 0x48, 0x00, 0x02, 0x07, 0x4b, 0x00, - 0x03, 0x82, 0x6d, 0x00, 0x03, 0x07, 0x4b, 0x00, 0x14, 0x81, 0x4d, 0x00, - 0x14, 0x01, 0x44, 0x00, 0x24, 0x01, 0x44, 0x00, 0x24, 0x05, 0x4b, 0x00, - 0x14, 0x08, 0x55, 0x00, 0x14, 0x05, 0x4b, 0x00, 0x04, 0x02, 0x57, 0x00, - 0x04, 0x04, 0x68, 0x00, 0x04, 0x82, 0x68, 0x00, 0x03, 0x85, 0x1b, 0x00, - 0x03, 0x04, 0x1d, 0x00, 0x02, 0x8b, 0x4b, 0x00, 0x12, 0x01, 0x16, 0x00, - 0x12, 0x03, 0x72, 0x00, 0x02, 0x08, 0x48, 0x00, 0x02, 0x07, 0x4b, 0x00, - 0x13, 0x81, 0x4d, 0x00, 0x13, 0x01, 0x44, 0x00, 0x23, 0x01, 0x44, 0x00, - 0x23, 0x05, 0x4b, 0x00, 0x13, 0x08, 0x55, 0x00, 0x13, 0x05, 0x4b, 0x00, - 0x03, 0x02, 0x57, 0x00, 0x03, 0x04, 0x68, 0x00, 0x03, 0x85, 0x7c, 0x00, - 0x03, 0x04, 0x1d, 0x00, 0x01, 0x8b, 0x4b, 0x00, 0x11, 0x01, 0x1d, 0x00, - 0x11, 0x05, 0x7c, 0x00, 0x01, 0x08, 0x48, 0x00, 0x01, 0x07, 0x4b, 0x00, - 0x12, 0x81, 0x16, 0x00, 0x12, 0x03, 0x72, 0x00, 0x02, 0x08, 0x48, 0x00, - 0x02, 0x07, 0x4b, 0x00, 0x13, 0x81, 0x7e, 0x00, 0x23, 0x01, 0x4d, 0x00, - 0x23, 0x01, 0x44, 0x00, 0x33, 0x01, 0x44, 0x00, 0x33, 0x05, 0x4b, 0x00, - 0x23, 0x08, 0x55, 0x00, 0x23, 0x05, 0x4b, 0x00, 0x13, 0x02, 0x57, 0x00, - 0x03, 0x08, 0x85, 0x00, 0x03, 0x07, 0x4b, 0x00, 0x04, 0x82, 0x68, 0x00, - 0x14, 0x81, 0x4d, 0x00, 0x14, 0x01, 0x44, 0x00, 0x24, 0x01, 0x7e, 0x00, - 0x24, 0x01, 0x72, 0x00, 0x14, 0x08, 0x55, 0x00, 0x14, 0x03, 0x4d, 0x00, - 0x04, 0x02, 0x87, 0x00, 0x03, 0x85, 0x1b, 0x00, 0x03, 0x04, 0x1d, 0x00, - 0x00, 0x86, 0x8f, 0x00, 0x00, 0x8c, 0x33, 0x00, /* instruct array */ - 0x92, 0x00, /* const pool size */ - 0x00, 0x5f, 0x5f, 0x63, 0x61, 0x6c, 0x6c, 0x73, 0x2e, 0x6c, 0x65, 0x6e, - 0x00, 0x6c, 0x65, 0x6e, 0x00, 0x6e, 0x6f, 0x6e, 0x65, 0x00, 0x6d, 0x6f, - 0x64, 0x65, 0x00, 0x30, 0x00, 0x69, 0x6e, 0x66, 0x6f, 0x5f, 0x69, 0x6e, - 0x64, 0x65, 0x78, 0x00, 0x72, 0x61, 0x6e, 0x67, 0x65, 0x00, 0x69, 0x74, - 0x65, 0x72, 0x00, 0x5f, 0x6c, 0x30, 0x00, 0x5f, 0x6c, 0x30, 0x2e, 0x5f, - 0x5f, 0x6e, 0x65, 0x78, 0x74, 0x5f, 0x5f, 0x00, 0x69, 0x00, 0x32, 0x00, - 0x3d, 0x3d, 0x00, 0x31, 0x00, 0x5f, 0x5f, 0x63, 0x61, 0x6c, 0x6c, 0x73, - 0x00, 0x2b, 0x00, 0x5f, 0x5f, 0x73, 0x6c, 0x69, 0x63, 0x65, 0x5f, 0x5f, - 0x00, 0x61, 0x6c, 0x77, 0x61, 0x79, 0x73, 0x00, 0x74, 0x6f, 0x64, 0x6f, - 0x00, 0x77, 0x68, 0x65, 0x6e, 0x00, 0x70, 0x65, 0x72, 0x69, 0x6f, 0x64, - 0x5f, 0x6d, 0x73, 0x00, 0x33, 0x00, 0x5f, 0x5f, 0x74, 0x69, 0x63, 0x6b, - 0x00, 0x3e, 0x00, 0x5f, 0x5f, 0x73, 0x65, 0x74, 0x5f, 0x5f, 0x00, 0x2d, + 0xf0, 0x01, /* instruct array size */ + 0x00, 0x82, 0x01, 0x00, 0x00, 0x04, 0x0b, 0x00, 0x00, 0x83, 0x0f, 0x00, + 0x00, 0x04, 0x14, 0x00, 0x00, 0x85, 0x19, 0x00, 0x00, 0x04, 0x1b, 0x00, + 0x20, 0x85, 0x19, 0x00, 0x20, 0x01, 0x0b, 0x00, 0x10, 0x02, 0x26, 0x00, + 0x00, 0x02, 0x2c, 0x00, 0x00, 0x04, 0x31, 0x00, 0x00, 0x82, 0x35, 0x00, + 0x00, 0x04, 0x42, 0x00, 0x00, 0x0d, 0x42, 0x00, 0x00, 0x07, 0x44, 0x00, + 0x11, 0x81, 0x0b, 0x00, 0x11, 0x05, 0x19, 0x00, 0x01, 0x08, 0x46, 0x00, + 0x01, 0x07, 0x49, 0x00, 0x02, 0x8e, 0x00, 0x00, 0x11, 0x81, 0x1b, 0x00, + 0x11, 0x05, 0x19, 0x00, 0x01, 0x08, 0x46, 0x00, 0x01, 0x07, 0x49, 0x00, + 0x12, 0x81, 0x4b, 0x00, 0x12, 0x01, 0x42, 0x00, 0x02, 0x1d, 0x00, 0x00, + 0x02, 0x04, 0x14, 0x00, 0x02, 0x85, 0x49, 0x00, 0x02, 0x04, 0x1b, 0x00, + 0x01, 0x8b, 0x49, 0x00, 0x11, 0x01, 0x1b, 0x00, 0x11, 0x05, 0x49, 0x00, + 0x01, 0x08, 0x46, 0x00, 0x01, 0x07, 0x49, 0x00, 0x12, 0x81, 0x14, 0x00, + 0x12, 0x03, 0x51, 0x00, 0x02, 0x08, 0x46, 0x00, 0x02, 0x07, 0x49, 0x00, + 0x13, 0x81, 0x4b, 0x00, 0x13, 0x01, 0x42, 0x00, 0x03, 0x1d, 0x00, 0x00, + 0x03, 0x04, 0x58, 0x00, 0x03, 0x82, 0x58, 0x00, 0x03, 0x85, 0x19, 0x00, + 0x03, 0x04, 0x1b, 0x00, 0x02, 0x8b, 0x49, 0x00, 0x12, 0x01, 0x14, 0x00, + 0x12, 0x03, 0x5d, 0x00, 0x02, 0x08, 0x46, 0x00, 0x02, 0x07, 0x49, 0x00, + 0x13, 0x81, 0x4b, 0x00, 0x13, 0x01, 0x42, 0x00, 0x03, 0x1d, 0x00, 0x00, + 0x03, 0x04, 0x5d, 0x00, 0x03, 0x85, 0x44, 0x00, 0x03, 0x04, 0x1b, 0x00, + 0x02, 0x8b, 0x49, 0x00, 0x12, 0x01, 0x14, 0x00, 0x12, 0x03, 0x62, 0x00, + 0x02, 0x08, 0x46, 0x00, 0x02, 0x07, 0x49, 0x00, 0x13, 0x81, 0x4b, 0x00, + 0x13, 0x01, 0x42, 0x00, 0x03, 0x1d, 0x00, 0x00, 0x03, 0x04, 0x62, 0x00, + 0x03, 0x85, 0x44, 0x00, 0x03, 0x04, 0x1b, 0x00, 0x01, 0x8b, 0x49, 0x00, + 0x11, 0x01, 0x1b, 0x00, 0x11, 0x05, 0x44, 0x00, 0x01, 0x08, 0x46, 0x00, + 0x01, 0x07, 0x49, 0x00, 0x12, 0x81, 0x14, 0x00, 0x12, 0x03, 0x5d, 0x00, + 0x02, 0x08, 0x46, 0x00, 0x02, 0x07, 0x49, 0x00, 0x03, 0x82, 0x5d, 0x00, + 0x03, 0x07, 0x49, 0x00, 0x14, 0x81, 0x4b, 0x00, 0x14, 0x01, 0x42, 0x00, + 0x04, 0x1d, 0x00, 0x00, 0x04, 0x04, 0x58, 0x00, 0x04, 0x82, 0x58, 0x00, + 0x03, 0x85, 0x19, 0x00, 0x03, 0x04, 0x1b, 0x00, 0x02, 0x8b, 0x49, 0x00, + 0x12, 0x01, 0x14, 0x00, 0x12, 0x03, 0x62, 0x00, 0x02, 0x08, 0x46, 0x00, + 0x02, 0x07, 0x49, 0x00, 0x13, 0x81, 0x4b, 0x00, 0x13, 0x01, 0x42, 0x00, + 0x03, 0x1d, 0x00, 0x00, 0x03, 0x04, 0x58, 0x00, 0x03, 0x85, 0x6c, 0x00, + 0x03, 0x04, 0x1b, 0x00, 0x01, 0x8b, 0x49, 0x00, 0x11, 0x01, 0x1b, 0x00, + 0x11, 0x05, 0x6c, 0x00, 0x01, 0x08, 0x46, 0x00, 0x01, 0x07, 0x49, 0x00, + 0x12, 0x81, 0x14, 0x00, 0x12, 0x03, 0x62, 0x00, 0x02, 0x08, 0x46, 0x00, + 0x02, 0x07, 0x49, 0x00, 0x13, 0x81, 0x6e, 0x00, 0x23, 0x01, 0x4b, 0x00, + 0x23, 0x01, 0x42, 0x00, 0x13, 0x1d, 0x00, 0x00, 0x03, 0x08, 0x73, 0x00, + 0x03, 0x07, 0x49, 0x00, 0x04, 0x82, 0x58, 0x00, 0x14, 0x81, 0x4b, 0x00, + 0x14, 0x01, 0x42, 0x00, 0x24, 0x01, 0x6e, 0x00, 0x24, 0x01, 0x62, 0x00, + 0x14, 0x08, 0x75, 0x00, 0x04, 0x02, 0x77, 0x00, 0x04, 0x04, 0x4b, 0x00, + 0x03, 0x85, 0x19, 0x00, 0x03, 0x04, 0x1b, 0x00, 0x00, 0x86, 0x83, 0x00, + 0x00, 0x8c, 0x31, 0x00, /* instruct array */ + 0x86, 0x00, /* const pool size */ + 0x00, 0x63, 0x61, 0x6c, 0x6c, 0x73, 0x2e, 0x6c, 0x65, 0x6e, 0x00, 0x6c, + 0x65, 0x6e, 0x00, 0x6e, 0x6f, 0x6e, 0x65, 0x00, 0x6d, 0x6f, 0x64, 0x65, + 0x00, 0x30, 0x00, 0x69, 0x6e, 0x66, 0x6f, 0x5f, 0x69, 0x6e, 0x64, 0x65, + 0x78, 0x00, 0x72, 0x61, 0x6e, 0x67, 0x65, 0x00, 0x69, 0x74, 0x65, 0x72, + 0x00, 0x5f, 0x6c, 0x30, 0x00, 0x5f, 0x6c, 0x30, 0x2e, 0x5f, 0x5f, 0x6e, + 0x65, 0x78, 0x74, 0x5f, 0x5f, 0x00, 0x69, 0x00, 0x32, 0x00, 0x3d, 0x3d, + 0x00, 0x31, 0x00, 0x63, 0x61, 0x6c, 0x6c, 0x73, 0x00, 0x61, 0x6c, 0x77, + 0x61, 0x79, 0x73, 0x00, 0x74, 0x6f, 0x64, 0x6f, 0x00, 0x77, 0x68, 0x65, + 0x6e, 0x00, 0x70, 0x65, 0x72, 0x69, 0x6f, 0x64, 0x5f, 0x6d, 0x73, 0x00, + 0x33, 0x00, 0x74, 0x69, 0x63, 0x6b, 0x00, 0x3e, 0x00, 0x2b, 0x00, 0x5f, + 0x5f, 0x73, 0x65, 0x74, 0x69, 0x74, 0x65, 0x6d, 0x5f, 0x5f, 0x00, 0x2d, 0x31, 0x00, /* const pool */ }; - pikaVM_runByteCode(__pikaMain, (uint8_t*)bytes); + pikaVM_runByteCode(self, (uint8_t*)bytes); } void __Task_update_tick(PikaObj* self) { diff --git a/port/cmsis-pack/pikascript/requestment.txt b/port/cmsis-pack/pikascript/requestment.txt index 9cf59d239..b983f8f19 100644 --- a/port/cmsis-pack/pikascript/requestment.txt +++ b/port/cmsis-pack/pikascript/requestment.txt @@ -1,2 +1,2 @@ -pikascript-core==v1.8.7 -PikaStdLib==v1.8.7 +pikascript-core==v1.10.0 +PikaStdLib==v1.10.0 \ No newline at end of file diff --git a/port/cmsis-pack/pikascript/rust-msc-latest-win10.exe b/port/cmsis-pack/pikascript/rust-msc-latest-win10.exe index cc503e1f2..917cc5c09 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