support str() in sysobj

This commit is contained in:
lyon 2022-01-09 00:12:38 +08:00
parent c32322a05a
commit 2f8ce3768c
3 changed files with 45 additions and 0 deletions

View File

@ -31,6 +31,9 @@ class SysObj(BaseObj):
def float(arg: any) -> float:
pass
def str(arg: any) -> str:
pass
def iter(arg: any) -> any:
pass

View File

@ -76,6 +76,27 @@ int PikaStdLib_SysObj_int(PikaObj* self, Arg* arg) {
return -999999999;
}
char* PikaStdLib_SysObj_str(PikaObj* self, Arg* arg) {
ArgType type = arg_getType(arg);
Args* buffs = New_strBuff();
char* res = NULL;
do {
if (TYPE_INT == type) {
int val = arg_getInt(arg);
res = strsFormat(buffs, 11, "%d", val);
break;
}
if (TYPE_FLOAT == type) {
float val = arg_getFloat(arg);
res = strsFormat(buffs, 11, "%f", val);
break;
}
} while (0);
obj_setStr(self, "__strtmp", res);
args_deinit(buffs);
return obj_getStr(self, "__strtmp");
}
Arg* PikaStdLib_SysObj_iter(PikaObj* self, Arg* arg) {
/* a MATE object */
if (TYPE_MATE_OBJECT == arg_getType(arg)) {

View File

@ -574,3 +574,24 @@ TEST(pikaMain, str_add_print) {
obj_deinit(pikaMain);
EXPECT_EQ(pikaMemNow(), 0);
}
TEST(pikaMain, int_float_to_str) {
/* init */
pikaMemInfo.heapUsedMax = 0;
PikaObj* pikaMain = newRootObj((char*)"pikaMain", New_PikaMain);
/* run */
obj_run(pikaMain, (char*)
"a = str(1)\n"
"b = str(1.2)\n"
"\n"
);
/* collect */
char* a = obj_getStr(pikaMain, (char*)"a");
char* b = obj_getStr(pikaMain, (char*)"b");
/* assert */
EXPECT_STREQ(a, "1");
EXPECT_STREQ(b, "1.200000");
/* deinit */
obj_deinit(pikaMain);
EXPECT_EQ(pikaMemNow(), 0);
}