From bbe6845cde78c2a1815e9c84638061d45656e443 Mon Sep 17 00:00:00 2001 From: lyon Date: Sat, 4 Sep 2021 16:57:44 +0800 Subject: [PATCH] literal is ok --- src/.vscode/launch.json | 4 +- .../pikascript/pikascript-core/PikaInvoke.c | 28 +------------- .../pikascript/pikascript-core/PikaObj.c | 38 ++++++++++++------- .../pikascript/pikascript-core/dataArgs.c | 18 +++++++-- .../pikascript/pikascript-core/dataStrs.c | 28 +++++++++++++- .../pikascript/pikascript-core/dataStrs.h | 1 + src/test/block-test.cpp | 16 ++++---- src/test/sysObj-test.cpp | 2 +- 8 files changed, 80 insertions(+), 55 deletions(-) diff --git a/src/.vscode/launch.json b/src/.vscode/launch.json index a913a6d34..beb8af611 100644 --- a/src/.vscode/launch.json +++ b/src/.vscode/launch.json @@ -10,8 +10,8 @@ "request": "launch", "program": "${workspaceFolder}/../build/src/test/pikascript_test", "args": [ - "--gtest_filter=object_test.test2" - // "--gtest_filter=block.assert1" + // "--gtest_filter=object_test.test2" + "--gtest_filter=block.assert1" // "--gtest_filter=sysObj.print" ], "stopAtEntry": false, diff --git a/src/package/pikascript/pikascript-core/PikaInvoke.c b/src/package/pikascript/pikascript-core/PikaInvoke.c index a3493e363..cc4d33c61 100644 --- a/src/package/pikascript/pikascript-core/PikaInvoke.c +++ b/src/package/pikascript/pikascript-core/PikaInvoke.c @@ -234,31 +234,7 @@ static void *getMethodPtr(PikaObj *methodHost, char *methodName) return res; } -static char *getCleanCmd(Args *buffs, char *cmd) -{ - int32_t size = strGetSize(cmd); - char *strOut = args_getBuff(buffs, size); - int32_t iOut = 0; - char delChar = ' '; - int32_t isInStr = 0; - for (int32_t i = 0; i < strGetSize(cmd); i++) - { - if ('\'' == cmd[i] || '\"' == cmd[i]) - { - isInStr = !isInStr; - } - if ((delChar == cmd[i]) && (!isInStr)) - { - /* do not load char */ - continue; - } - strOut[iOut] = cmd[i]; - iOut++; - } - /* add \0 */ - strOut[iOut] = 0; - return strOut; -} + Args *obj_invoke(PikaObj *self, char *cmd) { @@ -266,7 +242,7 @@ Args *obj_invoke(PikaObj *self, char *cmd) Args *res = New_args(NULL); args_setErrorCode(res, 0); Args *buffs = New_strBuff(); - char *cleanCmd = getCleanCmd(buffs, cmd); + char *cleanCmd = strsGetCleanCmd(buffs, cmd); char *methodToken = strsGetFirstToken(buffs, cleanCmd, '('); char *methodPath = methodToken; Args *args = NULL; diff --git a/src/package/pikascript/pikascript-core/PikaObj.c b/src/package/pikascript/pikascript-core/PikaObj.c index 2f3e99ec9..f657d9687 100644 --- a/src/package/pikascript/pikascript-core/PikaObj.c +++ b/src/package/pikascript/pikascript-core/PikaObj.c @@ -505,14 +505,6 @@ static void transferReturnVal(PikaObj *self, char *returnType, char *returnName, } } -Args *getRightRes(PikaObj *self, char *cmd) -{ - if (strIsContain(cmd, '(') && strIsContain(cmd, ')')) - { - return obj_invoke(self, cmd); - } -} - char *getRightCmd(Args *buffs, char *cmd) { char *right = NULL; @@ -534,6 +526,26 @@ char *getRightCmd(Args *buffs, char *cmd) return right; } +Args *getRightRes(PikaObj *self, char *cmd) +{ + Args *buffs = New_strBuff(); + Args *res = NULL; + if (strIsContain(cmd, '(') && strIsContain(cmd, ')')) + { + res = obj_invoke(self, cmd); + goto exit; + } + res = New_args(NULL); + args_setLiteral(res, "return", cmd); + char *returnType = strsAppend(buffs, "->", args_getType(res, "return")); + args_setStr(res, "returnType", returnType); + goto exit; + +exit: + args_deinit(buffs); + return res; +} + Args *obj_runDirect(PikaObj *self, char *cmd) { Args *buffs = New_strBuff(); @@ -632,17 +644,17 @@ void obj_runNoRes(PikaObj *slef, char *cmd) args_deinit(obj_runDirect(slef, cmd)); } - - - void obj_run(PikaObj *self, char *cmd) { /* safe, stop when error occord and error info would be print32_t */ Args *res = obj_runDirect(self, cmd); char *sysOut = args_getSysOut(res); - if (!strEqu("", sysOut)) + if (NULL != sysOut) { - printf("%s\r\n", sysOut); + if (!strEqu("", sysOut)) + { + printf("%s\r\n", sysOut); + } } if (0 != args_getErrorCode(res)) { diff --git a/src/package/pikascript/pikascript-core/dataArgs.c b/src/package/pikascript/pikascript-core/dataArgs.c index 5d3eb6149..2e4847427 100644 --- a/src/package/pikascript/pikascript-core/dataArgs.c +++ b/src/package/pikascript/pikascript-core/dataArgs.c @@ -386,13 +386,17 @@ exit: uint8_t args_setLiteral(Args *self, char *name, char *literal) { + Args *buffs = New_strBuff(); + literal = strsGetCleanCmd(buffs, literal); + uint8_t err = 0; char *directStr = strsGetDirectStr(self, literal); if (NULL != directStr) { /* direct string value */ args_setStr(self, name, directStr); /* ok */ - return 0; + err = 0; + goto exit; } if ((literal[0] >= '0') && (literal[0] <= '9')) { @@ -402,14 +406,20 @@ uint8_t args_setLiteral(Args *self, char *name, char *literal) args_setFloat(self, name, 0); args_set(self, name, literal); /* succeed */ - return 0; + err = 0; + goto exit; } args_setInt(self, name, 0); args_set(self, name, literal); /* succeed */ - return 0; + err = 0; + goto exit; } - return 1; + err = 1; + goto exit; +exit: + args_deinit(buffs); + return err; } int32_t args_set(Args *self, char *name, char *valStr) diff --git a/src/package/pikascript/pikascript-core/dataStrs.c b/src/package/pikascript/pikascript-core/dataStrs.c index 0cf6dd9d0..0388e748f 100644 --- a/src/package/pikascript/pikascript-core/dataStrs.c +++ b/src/package/pikascript/pikascript-core/dataStrs.c @@ -89,4 +89,30 @@ char *strsFormat(Args *buffs, uint16_t buffSize, const char *fmt, ...) vsnprintf(res, buffSize, fmt, args); va_end(args); return res; -} \ No newline at end of file +} + +char *strsGetCleanCmd(Args *buffs, char *cmd) +{ + int32_t size = strGetSize(cmd); + char *strOut = args_getBuff(buffs, size); + int32_t iOut = 0; + char delChar = ' '; + int32_t isInStr = 0; + for (int32_t i = 0; i < strGetSize(cmd); i++) + { + if ('\'' == cmd[i] || '\"' == cmd[i]) + { + isInStr = !isInStr; + } + if ((delChar == cmd[i]) && (!isInStr)) + { + /* do not load char */ + continue; + } + strOut[iOut] = cmd[i]; + iOut++; + } + /* add \0 */ + strOut[iOut] = 0; + return strOut; +} diff --git a/src/package/pikascript/pikascript-core/dataStrs.h b/src/package/pikascript/pikascript-core/dataStrs.h index 51bb35954..cf25cec6b 100644 --- a/src/package/pikascript/pikascript-core/dataStrs.h +++ b/src/package/pikascript/pikascript-core/dataStrs.h @@ -12,4 +12,5 @@ char *strsRemovePrefix(Args *buffs, char *inputStr, char *prefix); char *strsAppend(Args *buffs, char *strOrigin, char *strAppend); char *strsFormat(Args *buffs, uint16_t buffSize, const char *fmt, ...); char *strsGetDirectStr(Args *buffs, char *argPath); +char *strsGetCleanCmd(Args *buffs, char *cmd); #endif diff --git a/src/test/block-test.cpp b/src/test/block-test.cpp index 23ec7606e..57f832552 100644 --- a/src/test/block-test.cpp +++ b/src/test/block-test.cpp @@ -36,13 +36,13 @@ TEST(block, pop) EXPECT_EQ(pikaMemNow(), 0); } -// TEST(block, assert1) -// { -// PikaObj *block = block_init(); -// block_setAssert(block, (char *)"1"); -// int res = block_checkAssert(block); -// ASSERT_EQ(1, res); -// block_deinit(block); -// } +TEST(block, assert1) +{ + PikaObj *block = block_init(); + block_setAssert(block, (char *)"1"); + int res = block_checkAssert(block); + ASSERT_EQ(1, res); + block_deinit(block); +} void block_pushLine(PikaObj *self, char *line); diff --git a/src/test/sysObj-test.cpp b/src/test/sysObj-test.cpp index e58378cd1..bf80cc713 100644 --- a/src/test/sysObj-test.cpp +++ b/src/test/sysObj-test.cpp @@ -13,7 +13,7 @@ TEST(sysObj, print) char *sysOut = args_getSysOut(res); int errCode = args_getErrorCode(res); printf("sysout = %s\r\n", sysOut); - ASSERT_EQ(1, strEqu((char *)"hello world", sysOut)); + ASSERT_STREQ((char *)"hello world", sysOut); ASSERT_EQ(0, errCode); args_deinit(res); obj_deinit(obj);