support len() for string, support int() for string

This commit is contained in:
lyon1998 2022-04-09 01:15:21 +08:00
parent e5d07cf0ce
commit 757f00893a
5 changed files with 63 additions and 1 deletions

View File

@ -46,6 +46,9 @@ class SysObj(BaseObj):
def __get__(self, obj: any, key: any) -> any:
pass
def len(self, arg:any) -> int:
pass
class RangeObj(TinyObj):
def __next__(self) -> any:

View File

@ -72,6 +72,9 @@ int PikaStdLib_SysObj_int(PikaObj* self, Arg* arg) {
if (ARG_TYPE_FLOAT == type) {
return (int)arg_getFloat(arg);
}
if (ARG_TYPE_STRING == type) {
return (int)fast_atoi(arg_getStr(arg));
}
obj_setSysOut(self, "[error] convert to int type faild.");
obj_setErrorCode(self, 1);
return -999999999;
@ -204,3 +207,12 @@ void PikaStdLib_SysObj___set__(PikaObj* self,
pikaVM_runByteCode(arg_obj, (uint8_t*)bytes);
}
}
int PikaStdLib_SysObj_len(PikaObj* self, Arg* arg) {
if (ARG_TYPE_STRING == arg_getType(arg)) {
return strGetSize(arg_getStr(arg));
}
obj_setErrorCode(self, 1);
__platform_printf("[Error] len: arg type not support\r\n");
return -1;
}

View File

@ -46,6 +46,9 @@ class SysObj(BaseObj):
def __get__(self, obj: any, key: any) -> any:
pass
def len(self, arg:any) -> int:
pass
class RangeObj(TinyObj):
def __next__(self) -> any:

View File

@ -72,6 +72,9 @@ int PikaStdLib_SysObj_int(PikaObj* self, Arg* arg) {
if (ARG_TYPE_FLOAT == type) {
return (int)arg_getFloat(arg);
}
if (ARG_TYPE_STRING == type) {
return (int)fast_atoi(arg_getStr(arg));
}
obj_setSysOut(self, "[error] convert to int type faild.");
obj_setErrorCode(self, 1);
return -999999999;
@ -204,3 +207,12 @@ void PikaStdLib_SysObj___set__(PikaObj* self,
pikaVM_runByteCode(arg_obj, (uint8_t*)bytes);
}
}
int PikaStdLib_SysObj_len(PikaObj* self, Arg* arg) {
if (ARG_TYPE_STRING == arg_getType(arg)) {
return strGetSize(arg_getStr(arg));
}
obj_setErrorCode(self, 1);
__platform_printf("[Error] len: arg type not support\r\n");
return -1;
}

View File

@ -1476,7 +1476,39 @@ TEST(pikaMain, class_args_err) {
/* collect */
/* assert */
EXPECT_STREQ(log_buff[4], (char*)"TypeError: PikaStdLib.MemChecker() takes no arguments\r\n");
EXPECT_STREQ(
log_buff[4],
(char*)"TypeError: PikaStdLib.MemChecker() takes no arguments\r\n");
/* deinit */
obj_deinit(pikaMain);
EXPECT_EQ(pikaMemNow(), 0);
}
TEST(pikaMain, int_) {
/* init */
pikaMemInfo.heapUsedMax = 0;
PikaObj* pikaMain = newRootObj((char*)"pikaMain", New_PikaMain);
/* run */
obj_run(pikaMain, (char*)"a = int('3')\n");
/* collect */
int a = obj_getInt(pikaMain, (char*)"a");
/* assert */
EXPECT_EQ(a, 3);
/* deinit */
obj_deinit(pikaMain);
EXPECT_EQ(pikaMemNow(), 0);
}
TEST(pikaMain, len_) {
/* init */
pikaMemInfo.heapUsedMax = 0;
PikaObj* pikaMain = newRootObj((char*)"pikaMain", New_PikaMain);
/* run */
obj_run(pikaMain, (char*)"a = len('331dd')\n");
/* collect */
int a = obj_getInt(pikaMain, (char*)"a");
/* assert */
EXPECT_EQ(a, 5);
/* deinit */
obj_deinit(pikaMain);
EXPECT_EQ(pikaMemNow(), 0);