From 8894e270f3219eab15dc228468cdee6dbd5ce29d Mon Sep 17 00:00:00 2001 From: lyon Date: Tue, 18 Jan 2022 23:37:48 +0800 Subject: [PATCH] support s[2] = 'q' --- port/linux/package/pikascript/PikaStdLib.py | 2 +- .../pikascript-lib/PikaStdLib/PikaStdLib_SysObj.c | 7 ++++++- port/linux/test/parse-test.cpp | 1 + port/linux/test/pikaMain-test.cpp | 12 +++++++++--- src/PikaParser.c | 5 +++++ 5 files changed, 22 insertions(+), 5 deletions(-) diff --git a/port/linux/package/pikascript/PikaStdLib.py b/port/linux/package/pikascript/PikaStdLib.py index f353627de..af6b902a3 100644 --- a/port/linux/package/pikascript/PikaStdLib.py +++ b/port/linux/package/pikascript/PikaStdLib.py @@ -43,7 +43,7 @@ class SysObj(BaseObj): def range(a1: int, a2: int, a3: int) -> any: pass - def __set__(obj: any, key: any, val: any): + def __set__(obj: any, key: any, val: any, obj_str: str): pass def __get__(obj: any, key: any) -> any: 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 2fdc0a998..1031d9b67 100644 --- a/port/linux/package/pikascript/pikascript-lib/PikaStdLib/PikaStdLib_SysObj.c +++ b/port/linux/package/pikascript/pikascript-lib/PikaStdLib/PikaStdLib_SysObj.c @@ -160,13 +160,18 @@ Arg* PikaStdLib_SysObj___get__(PikaObj* self, Arg* key, Arg* obj) { return arg_setNull(NULL); } -void PikaStdLib_SysObj___set__(PikaObj* self, Arg* key, Arg* obj, Arg* val) { +void PikaStdLib_SysObj___set__(PikaObj* self, + Arg* key, + Arg* obj, + char* obj_str, + Arg* val) { ArgType obj_type = arg_getType(obj); if (TYPE_STRING == obj_type) { int index = arg_getInt(key); char* str_val = arg_getStr(val); char* str_pyload = arg_getStr(obj); str_pyload[index] = str_val[0]; + obj_setStr(self, obj_str, str_pyload); } if ((TYPE_OBJECT == obj_type) || (TYPE_POINTER == obj_type)) { PikaObj* arg_obj = arg_getPtr(obj); diff --git a/port/linux/test/parse-test.cpp b/port/linux/test/parse-test.cpp index d8bad60d0..519b0407a 100644 --- a/port/linux/test/parse-test.cpp +++ b/port/linux/test/parse-test.cpp @@ -1881,6 +1881,7 @@ TEST(parser, __set__) { "1 REF a\n" "1 REF b\n" "1 REF c\n" + "1 STR a\n" "0 RUN __set__\n" ); args_deinit(buffs); diff --git a/port/linux/test/pikaMain-test.cpp b/port/linux/test/pikaMain-test.cpp index 16f454d93..b329e33cb 100644 --- a/port/linux/test/pikaMain-test.cpp +++ b/port/linux/test/pikaMain-test.cpp @@ -782,7 +782,7 @@ TEST(pikaMain, dict__set__get) { EXPECT_EQ(pikaMemNow(), 0); } -TEST(pikaMain, str___get__) { +TEST(pikaMain, str___get____set__) { /* init */ pikaMemInfo.heapUsedMax = 0; PikaObj* pikaMain = newRootObj((char*)"pikaMain", New_PikaMain); @@ -791,7 +791,7 @@ TEST(pikaMain, str___get__) { obj_run(pikaMain, (char*) "s = 'test'\n" "res = __get__(s, 2)\n" - "__set__(s, 2, 'q')\n" + "__set__(s, 2, 'q', 's')\n" "\n" ); /* collect */ @@ -799,7 +799,7 @@ TEST(pikaMain, str___get__) { char* s= obj_getStr(pikaMain, (char*)"s"); /* assert */ EXPECT_STREQ(res, (char*)"s"); - // EXPECT_STREQ(s, (char*)"teqt"); + EXPECT_STREQ(s, (char*)"teqt"); /* deinit */ obj_deinit(pikaMain); EXPECT_EQ(pikaMemNow(), 0); @@ -814,12 +814,18 @@ TEST(pikaMain, str__index__) { obj_run(pikaMain, (char*) "s = 'test'\n" "res = s[2]\n" + "res2 = 'eqrt'[2]\n" + "s[2] = 'q'\n" "\n" ); /* collect */ char* res = obj_getStr(pikaMain, (char*)"res"); + char* res2 = obj_getStr(pikaMain, (char*)"res2"); + char* s = obj_getStr(pikaMain, (char*)"s"); /* assert */ EXPECT_STREQ(res, (char*)"s"); + EXPECT_STREQ(res2, (char*)"r"); + EXPECT_STREQ(s, (char*)"teqt"); // EXPECT_STREQ(s, (char*)"teqt"); /* deinit */ obj_deinit(pikaMain); diff --git a/src/PikaParser.c b/src/PikaParser.c index d0d137cb8..b506ba084 100644 --- a/src/PikaParser.c +++ b/src/PikaParser.c @@ -661,6 +661,11 @@ char* Parser_solveBranckets(Args* outBuffs, if (strEqu(mode, "left")) { right_arg = arg_strAppend(right_arg, ","); right_arg = arg_strAppend(right_arg, stmt); + right_arg = arg_strAppend(right_arg, ","); + right_arg = arg_strAppend(right_arg, "'"); + right_arg = + arg_strAppend(right_arg, args_getStr(buffs, "obj")); + right_arg = arg_strAppend(right_arg, "'"); } right_arg = arg_strAppend(right_arg, ")"); args_setStr(buffs, "index", "");