a = b[c+d] is ok

This commit is contained in:
lyon 2022-01-17 21:16:51 +08:00
parent 772bef847a
commit d2bd8b44e0
2 changed files with 32 additions and 2 deletions

View File

@ -1817,3 +1817,25 @@ TEST(parser, __get__) {
args_deinit(buffs);
EXPECT_EQ(pikaMemNow(), 0);
}
TEST(parser, __get__2) {
pikaMemInfo.heapUsedMax = 0;
Args* buffs = New_strBuff();
char* lines = (char*)
"a = b[c+d]\n";
printf("%s", lines);
char* pikaAsm = Parser_multiLineToAsm(buffs, (char*)lines);
printf("%s", pikaAsm);
EXPECT_STREQ(pikaAsm,
"B0\n"
"1 REF b\n"
"2 REF c\n"
"2 REF d\n"
"1 OPT +\n"
"0 RUN __get__\n"
"0 OUT a\n"
);
args_deinit(buffs);
EXPECT_EQ(pikaMemNow(), 0);
}

View File

@ -599,6 +599,8 @@ char* Parser_solveRightBranckets(Args* outBuffs, char* right) {
char *pyload1, *pyload2 = NULL;
Arg* right_arg = arg_setStr(NULL, "", "");
Arg* token1_arg = NULL;
uint8_t is_in_brancket = 0;
args_setStr(buffs, "index", "");
enum TokenType token_type1, token_type2;
do {
tokens = Lexer_getTokens(buffs, right);
@ -625,16 +627,22 @@ char* Parser_solveRightBranckets(Args* outBuffs, char* right) {
/* matched [] */
if ((TOKEN_devider == token_type2) && (strEqu(pyload2, "["))) {
args_setStr(buffs, "obj", pyload1);
is_in_brancket = 1;
} else if ((TOKEN_devider == token_type2) &&
(strEqu(pyload2, "]"))) {
args_setStr(buffs, "index", pyload1);
is_in_brancket = 0;
right_arg = arg_strAppend(right_arg, "__get__(");
right_arg = arg_strAppend(right_arg, args_getStr(buffs, "obj"));
right_arg = arg_strAppend(right_arg, ",");
right_arg =
arg_strAppend(right_arg, args_getStr(buffs, "index"));
right_arg = arg_strAppend(right_arg, ")");
} else {
} else if (is_in_brancket) {
char* index = args_getStr(buffs, "index");
Arg* index_arg = arg_setStr(NULL, "", index);
index_arg = arg_strAppend(index_arg, pyload2);
args_setStr(buffs, "index", arg_getStr(index_arg));
arg_deinit(index_arg);
}
args_deinit(token_buffs);
}