diff --git a/port/linux/package/pikascript/PikaStdLib.py b/port/linux/package/pikascript/PikaStdLib.py index 378534e92..994c6a1fa 100644 --- a/port/linux/package/pikascript/PikaStdLib.py +++ b/port/linux/package/pikascript/PikaStdLib.py @@ -19,6 +19,12 @@ class SysObj(BaseObj): def remove(argPath: str): pass + def int(arg: any) -> int: + pass + + def float(arg: any) -> float: + pass + class List(TinyObj): def init(): diff --git a/port/linux/package/pikascript/pikascript-lib/PikaStdLib/PikaStdLib_SysObj.c b/port/linux/package/pikascript/pikascript-lib/PikaStdLib/PikaStdLib_SysObj.c index 66f61c289..77fcf0cb6 100644 --- a/port/linux/package/pikascript/pikascript-lib/PikaStdLib/PikaStdLib_SysObj.c +++ b/port/linux/package/pikascript/pikascript-lib/PikaStdLib/PikaStdLib_SysObj.c @@ -49,3 +49,29 @@ void PikaStdLib_SysObj_type(PikaObj* self, char* argPath) { return; } } + +float PikaStdLib_SysObj_float(PikaObj* self, Arg* arg) { + ArgType type = arg_getType(arg); + if (TYPE_INT == type) { + return (float)arg_getInt(arg); + } + if (TYPE_FLOAT == type) { + return (float)arg_getFloat(arg); + } + obj_setSysOut(self, "[error] convert to float type faild."); + obj_setErrorCode(self, 1); + return -99999.99999; +} + +int PikaStdLib_SysObj_int(PikaObj* self, Arg* arg) { + ArgType type = arg_getType(arg); + if (TYPE_INT == type) { + return (int)arg_getInt(arg); + } + if (TYPE_FLOAT == type) { + return (int)arg_getFloat(arg); + } + obj_setSysOut(self, "[error] convert to int type faild."); + obj_setErrorCode(self, 1); + return -999999999; +} \ No newline at end of file diff --git a/port/linux/test/pikaMain-test.cpp b/port/linux/test/pikaMain-test.cpp index ab29f25aa..bab762d7b 100644 --- a/port/linux/test/pikaMain-test.cpp +++ b/port/linux/test/pikaMain-test.cpp @@ -55,6 +55,36 @@ TEST(pikaMain, list_new) { EXPECT_EQ(a1, 7); EXPECT_STREQ(a2, "eee"); + /* deinit */ + obj_deinit(globals); + obj_deinit(pikaMain); + EXPECT_EQ(pikaMemNow(), 0); +} + +TEST(pikaMain, int_float_convert) { + /* init */ + pikaMemInfo.heapUsedMax = 0; + PikaObj* pikaMain = newRootObj((char*)"pikaMain", New_PikaMain); + /* run */ + Parameters* globals = + obj_runDirect(pikaMain, (char*) + "a = 1\n" + "a_f = float(a)\n" + "b = 1.3\n" + "b_i = int(b)\n" + ); + /* collect */ + int a = obj_getInt(globals, (char*)"a"); + float a_f = obj_getFloat(globals, (char*)"a_f"); + float b = obj_getFloat(globals, (char*)"b"); + int b_i = obj_getInt(globals, (char*)"b_i"); + + /* assert */ + EXPECT_EQ(a, 1); + EXPECT_FLOAT_EQ(a_f, 1); + EXPECT_FLOAT_EQ(b, 1.3); + EXPECT_FLOAT_EQ(b_i, 1); + /* deinit */ obj_deinit(globals); obj_deinit(pikaMain);