support s[2] = 'q'

This commit is contained in:
lyon 2022-01-18 23:37:48 +08:00
parent 5b8a41f0d5
commit 8894e270f3
5 changed files with 22 additions and 5 deletions

View File

@ -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:

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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", "");