parse for slice is ok

This commit is contained in:
lyon 2022-05-30 13:48:49 +08:00
parent 9ab1c55564
commit a3096e9266
2 changed files with 37 additions and 31 deletions

View File

@ -2767,3 +2767,22 @@ TEST(parser, slice1) {
args_deinit(buffs);
EXPECT_EQ(pikaMemNow(), 0);
}
TEST(parser, slice2) {
pikaMemInfo.heapUsedMax = 0;
Args* buffs = New_strBuff();
char* lines = "a = recv_buf[1:4:2]\n";
printf("%s", lines);
char* pikaAsm = Parser_multiLineToAsm(buffs, lines);
printf("%s", pikaAsm);
EXPECT_STREQ(pikaAsm,
"B0\n"
"1 REF recv_buf\n"
"1 NUM 1\n"
"1 NUM 4\n"
"1 NUM 2\n"
"0 RUN __slice__\n"
"0 OUT a\n");
args_deinit(buffs);
EXPECT_EQ(pikaMemNow(), 0);
}

View File

@ -755,54 +755,41 @@ static void __getSlicePars(Args* outBuffs,
char** pEnd,
char** pStep) {
Args buffs = {0};
uint8_t colon_num = 0;
*pStart = "";
*pEnd = "";
*pStep = "";
/* count colon_num */
/* slice */
uint8_t colon_i = 0;
ParserState_forEachToken(ps, inner) {
ParserState_iterStart(&ps);
if (strEqu(ps.token1.pyload, ":") && ps.branket_deepth == 0) {
colon_num++;
colon_i++;
goto iter_continue1;
}
if (colon_i == 0) {
*pStart = strsAppend(&buffs, *pStart, ps.token1.pyload);
}
if (colon_i == 1) {
*pEnd = strsAppend(&buffs, *pEnd, ps.token1.pyload);
}
if (colon_i == 2) {
*pStep = strsAppend(&buffs, *pStep, ps.token1.pyload);
}
iter_continue1:
ParserState_iterEnd(&ps);
}
ParserState_deinit(&ps);
/* just a index */
if (colon_num == 0) {
*pStart = inner;
if (colon_i == 1) {
*pStep = "1";
}
if (colon_i == 0) {
*pEnd = strsAppend(&buffs, *pStart, " + 1");
*pStep = "1";
goto exit;
}
/* slice without step */
if (colon_num == 1) {
uint8_t colon_i = 0;
*pStep = "1";
ParserState_forEachToken(ps, inner) {
ParserState_iterStart(&ps);
if (strEqu(ps.token1.pyload, ":") && ps.branket_deepth == 0) {
colon_i++;
goto iter_continue;
}
if (colon_i == 0) {
*pStart = strsAppend(&buffs, *pStart, ps.token1.pyload);
}
if (colon_i == 1) {
*pEnd = strsAppend(&buffs, *pEnd, ps.token1.pyload);
}
iter_continue:
ParserState_iterEnd(&ps);
}
ParserState_deinit(&ps);
}
/* slice with step */
exit:
/* output */
*pStart = strsCopy(outBuffs, *pStart);
*pEnd = strsCopy(outBuffs, *pEnd);