mirror of
https://gitee.com/Lyon1998/pikapython.git
synced 2025-01-15 17:02:53 +08:00
support str() in sysobj
This commit is contained in:
parent
c32322a05a
commit
2f8ce3768c
@ -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
|
||||
|
||||
|
@ -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)) {
|
||||
|
@ -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);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user