From a3096e9266ad4ce2155785d8d01c4f722d08f4c7 Mon Sep 17 00:00:00 2001 From: lyon Date: Mon, 30 May 2022 13:48:49 +0800 Subject: [PATCH] parse for slice is ok --- port/linux/test/parse-test.cpp | 19 +++++++++++++ src/PikaParser.c | 49 +++++++++++++--------------------- 2 files changed, 37 insertions(+), 31 deletions(-) diff --git a/port/linux/test/parse-test.cpp b/port/linux/test/parse-test.cpp index c1f8f5b73..26e4a8947 100644 --- a/port/linux/test/parse-test.cpp +++ b/port/linux/test/parse-test.cpp @@ -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); +} diff --git a/src/PikaParser.c b/src/PikaParser.c index fc576464d..5757996a6 100644 --- a/src/PikaParser.c +++ b/src/PikaParser.c @@ -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);