Skip to content

Commit

Permalink
parse for slice is ok
Browse files Browse the repository at this point in the history
  • Loading branch information
pikasTech committed May 30, 2022
1 parent 9ab1c55 commit a3096e9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 30 deletions.
19 changes: 19 additions & 0 deletions port/linux/test/parse-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
47 changes: 17 additions & 30 deletions src/PikaParser.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
*pEnd = strsAppend(&buffs, *pStart, " 1");
if (colon_i == 1) {
*pStep = "1";
goto exit;
}

/* slice without step */
if (colon_num == 1) {
uint8_t colon_i = 0;
if (colon_i == 0) {
*pEnd = strsAppend(&buffs, *pStart, " 1");
*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);
Expand Down

0 comments on commit a3096e9

Please sign in to comment.