From 1816e322ce905ecc73b8be91d519d8c045db162b Mon Sep 17 00:00:00 2001 From: lyon Date: Sat, 21 Jan 2023 23:19:36 +0800 Subject: [PATCH] fix `l[x()] = b` parse err --- port/linux/.vscode/launch.json | 2 +- src/PikaParser.c | 37 +++++++++++++++++++++++++++++----- src/PikaParser.h | 12 +++++------ test/parse-test.cpp | 36 ++++++++++++++++++++++++--------- 4 files changed, 66 insertions(+), 21 deletions(-) diff --git a/port/linux/.vscode/launch.json b/port/linux/.vscode/launch.json index 79b5243b7..ef3816fbf 100644 --- a/port/linux/.vscode/launch.json +++ b/port/linux/.vscode/launch.json @@ -11,7 +11,7 @@ "program": "${workspaceFolder}/build/test/pikascript_test", // "program": "${workspaceFolder}/build/boot/demo06-pikamain/pikascript_demo06-pikamain", "args": [ - // "--gtest_filter=mqtt.subscribe" + "--gtest_filter=parser.syntex_issue_l1l2" ], "stopAtEntry": false, "cwd": "${workspaceFolder}", diff --git a/src/PikaParser.c b/src/PikaParser.c index ab6668039..16c26431b 100644 --- a/src/PikaParser.c +++ b/src/PikaParser.c @@ -55,8 +55,9 @@ void Cursor_iterEnd(struct Cursor* cs); void Cursor_deinit(struct Cursor* cs); /* Cursor high level api */ -char* Cursor_popToken(Args* buffs, char** stmt, char* devide); +char* Cursor_popToken(Args* buffs, char** pStmt, char* devide); PIKA_BOOL Cursor_isContain(char* stmt, TokenType type, char* pyload); +char* Cursor_splitCollect(Args* buffs, char* stmt, char* devide, int index); char* Parser_linesToAsm(Args* outBuffs, char* multiLine); uint16_t TokenStream_getSize(char* tokenStream) { @@ -980,11 +981,11 @@ PIKA_BOOL Cursor_isContain(char* stmt, TokenType type, char* pyload) { return res; } -char* Cursor_popToken(Args* buffs, char** tokenStream, char* devide) { +char* Cursor_popToken(Args* buffs, char** pStmt, char* devide) { Arg* out_item = arg_newStr(""); Arg* tokenStream_after = arg_newStr(""); PIKA_BOOL is_find_devide = PIKA_FALSE; - Cursor_forEachToken(cs, *tokenStream) { + Cursor_forEachToken(cs, *pStmt) { Cursor_iterStart(&cs); if (!is_find_devide) { if ((cs.branket_deepth == 0 && strEqu(cs.token1.pyload, devide)) || @@ -1010,10 +1011,36 @@ char* Cursor_popToken(Args* buffs, char** tokenStream, char* devide) { char* token_after_str = strsCopy(buffs, arg_getStr(tokenStream_after)); arg_deinit(tokenStream_after); /* update tokenStream */ - *tokenStream = token_after_str; + *pStmt = token_after_str; return out_item_str; } +char* Cursor_splitCollect(Args* buffs, char* stmt, char* devide, int index) { + Arg* out_arg = arg_newStr(""); + int expect_branket = 0; + if (devide[0] == '(' || devide[0] == '[' || devide[0] == '{') { + expect_branket = 1; + } + int i = 0; + Cursor_forEachToken(cs, stmt) { + Cursor_iterStart(&cs); + if (cs.branket_deepth == expect_branket && + strEqu(cs.token1.pyload, devide)) { + i++; + if (i == index) { + Cursor_iterEnd(&cs); + break; + } + } + if (i == index) { + out_arg = arg_strAppend(out_arg, cs.token1.pyload); + } + Cursor_iterEnd(&cs); + } + Cursor_deinit(&cs); + return strsCacheArg(buffs, out_arg); +} + static void Slice_getPars(Args* outBuffs, char* inner, char** pStart, @@ -1583,7 +1610,7 @@ __exit: AST* AST_parseStmt(AST* ast, char* stmt) { Args buffs = {0}; - char* assignment = strsGetFirstToken(&buffs, stmt, '('); + char* assignment = Cursor_splitCollect(&buffs, stmt, "(", 0); char* method = NULL; char* ref = NULL; char* str = NULL; diff --git a/src/PikaParser.h b/src/PikaParser.h index 6f5b08816..9d53c7cb5 100644 --- a/src/PikaParser.h +++ b/src/PikaParser.h @@ -106,19 +106,19 @@ char* Parser_linesToArray(char* lines); char* instructUnit_fromAsmLine(Args* outBuffs, char* pikaAsm); ByteCodeFrame* byteCodeFrame_appendFromAsm(ByteCodeFrame* bf, char* pikaAsm); -#define Cursor_forEach(cursor) \ +#define _Cursor_forEach(cursor) \ _Cursor_beforeIter(&cursor); \ for (int __i = 0; __i < cursor.length; __i++) -#define Cursor_forEachTokenExistPs(cursor, tokenStream) \ +#define Cursor_forEachTokenExistPs(cursor, stmt) \ /* init parserStage */ \ _Cursor_init(&cursor); \ - _Cursor_parse(&cursor, tokenStream); \ - Cursor_forEach(cursor) + _Cursor_parse(&cursor, stmt); \ + _Cursor_forEach(cursor) -#define Cursor_forEachToken(cursor, tokenStream) \ +#define Cursor_forEachToken(cursor, stmt) \ struct Cursor cursor; \ - Cursor_forEachTokenExistPs(cursor, tokenStream) + Cursor_forEachTokenExistPs(cursor, stmt) uint16_t TokenStream_getSize(char* tokenStream); diff --git a/test/parse-test.cpp b/test/parse-test.cpp index ce3732eb0..522c8c588 100644 --- a/test/parse-test.cpp +++ b/test/parse-test.cpp @@ -5104,7 +5104,6 @@ TEST(parser, syntex_issue_lwekj) { EXPECT_EQ(pikaMemNow(), 0); } -#if 0 // NEED FIX TEST(parser, syntex_issue_lekj) { pikaMemInfo.heapUsedMax = 0; Args* buffs = New_strBuff(); @@ -5112,17 +5111,36 @@ TEST(parser, syntex_issue_lekj) { printf("%s\r\n", lines); char* pikaAsm = Parser_linesToAsm(buffs, lines); printf("%s", pikaAsm); - // EXPECT_STREQ(pikaAsm, - // "B0\n" - // "2 NUM 1\n" - // "2 REF a\n" - // "1 OPT in \n" - // "0 OPT not \n" - // "B0\n"); + EXPECT_STREQ(pikaAsm, + "B0\n" + "1 REF a\n" + "1 RUN b\n" + "1 NUM 1\n" + "0 RUN __setitem__\n" + "0 OUT a\n" + "B0\n"); + args_deinit(buffs); + EXPECT_EQ(pikaMemNow(), 0); +} + +TEST(parser, syntex_issue_l1l2) { + pikaMemInfo.heapUsedMax = 0; + Args* buffs = New_strBuff(); + char* lines = "test(a=1, b= 2)"; + printf("%s\r\n", lines); + char* pikaAsm = Parser_linesToAsm(buffs, lines); + printf("%s", pikaAsm); + EXPECT_STREQ(pikaAsm, + "B0\n" + "1 NUM 1\n" + "1 OUT a\n" + "1 NUM 2\n" + "1 OUT b\n" + "0 RUN test\n" + "B0\n"); args_deinit(buffs); EXPECT_EQ(pikaMemNow(), 0); } -#endif #endif