From 825344910f84a486375a7a1c956befc706732532 Mon Sep 17 00:00:00 2001 From: lyon Date: Sat, 27 Nov 2021 20:11:35 +0800 Subject: [PATCH] use lexer to get operator --- port/linux/.vscode/launch.json | 2 +- port/linux/test/parse-test.cpp | 20 +++++++++ src/PikaParser.c | 76 ++++++++++++++++++++++++---------- 3 files changed, 74 insertions(+), 24 deletions(-) diff --git a/port/linux/.vscode/launch.json b/port/linux/.vscode/launch.json index cc2bd584f..d22ddec12 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/src/boot/demo06-pikamain/pikascript_demo06-pikamain", "args": [ - // "--gtest_filter=VM*" + // "--gtest_filter=VM.a_jjcc" // "--gtest_filter=queue*" // "--gtest_filter=parser*" // "--gtest_filter=args*", diff --git a/port/linux/test/parse-test.cpp b/port/linux/test/parse-test.cpp index 986d70e9f..c3880f07b 100644 --- a/port/linux/test/parse-test.cpp +++ b/port/linux/test/parse-test.cpp @@ -1033,3 +1033,23 @@ TEST(lexser, num_1) { args_deinit(buffs); EXPECT_EQ(pikaMemNow(), 0); } + +TEST(lexser, jjcc) { + /* init */ + pikaMemInfo.heapUsedMax = 0; + Args* buffs = New_strBuff(); + + /* run */ + char* tokens = Lexer_getTokens(buffs, (char*)"a = (1 + 1.1) * 3 - 2 /4.0"); + char* printTokens = Lexer_printTokens(buffs, tokens); + printf((char*)"%s\n", printTokens); + + /* assert */ + EXPECT_STREQ(printTokens, + "{sym}a{opt}={dvd}({lit}1{opt}+{lit}1.1{dvd}){opt}*{lit}3{opt}" + "-{lit}2{opt}/{lit}4.0"); + + /* deinit */ + args_deinit(buffs); + EXPECT_EQ(pikaMemNow(), 0); +} \ No newline at end of file diff --git a/src/PikaParser.c b/src/PikaParser.c index 520214836..7afa0c306 100644 --- a/src/PikaParser.c +++ b/src/PikaParser.c @@ -480,6 +480,58 @@ char* Lexer_getTokens(Args* outBuffs, char* stmt) { return tokens; } +uint8_t Lexer_isContain(char* tokens, char* operator) { + Args* buffs = New_strBuff(); + char* tokens_buff = strsCopy(buffs, tokens); + uint8_t res = 0; + uint16_t token_size = strCountSign(tokens, 0x1F) + 1; + for (int i = 0; i < token_size; i++) { + char* token = strsPopToken(buffs, tokens_buff, 0x1F); + if (TOKEN_operator == token[0]) { + if (strEqu(token + 1, operator)) { + res = 1; + goto exit; + } + } + } +exit: + args_deinit(buffs); + return res; +} + +char* Lexer_getOperator(Args* outBuffs, char* stmt) { + Args* buffs = New_strBuff(); + char* tokens = Lexer_getTokens(buffs, stmt); + uint16_t token_size = strCountSign(tokens, 0x1F) + 1; + char* operator= NULL; + if (Lexer_isContain(tokens, "*")) { + operator= strsCopy(buffs, "*"); + } + if (Lexer_isContain(tokens, "/")) { + operator= strsCopy(buffs, "/"); + } + if (Lexer_isContain(tokens, "+")) { + operator= strsCopy(buffs, "+"); + } + if (Lexer_isContain(tokens, "-")) { + operator= strsCopy(buffs, "-"); + } + if (Lexer_isContain(tokens, "<")) { + operator= strsCopy(buffs, "<"); + } + if (Lexer_isContain(tokens, ">")) { + operator= strsCopy(buffs, ">"); + } + if (Lexer_isContain(tokens, "==")) { + operator= strsCopy(buffs, "=="); + } +exit: + /* out put */ + operator= strsCopy(outBuffs, operator); + args_deinit(buffs); + return operator; +} + AST* AST_parseStmt(AST* ast, char* stmt) { Args* buffs = New_strBuff(); char* assignment = strsGetFirstToken(buffs, stmt, '('); @@ -508,29 +560,7 @@ AST* AST_parseStmt(AST* ast, char* stmt) { /* solve method stmt */ if (STMT_operator == stmtType) { char* rightWithoutSubStmt = strs_deleteBetween(buffs, right, '(', ')'); - char operator[2] = {0}; - if (strIsContain(rightWithoutSubStmt, '*')) { - operator[0] = '*'; - } - if (strIsContain(rightWithoutSubStmt, '/')) { - operator[0] = '/'; - } - if (strIsContain(rightWithoutSubStmt, '+')) { - operator[0] = '+'; - } - if (strIsContain(rightWithoutSubStmt, '-')) { - operator[0] = '-'; - } - if (strIsContain(rightWithoutSubStmt, '<')) { - operator[0] = '<'; - } - if (strIsContain(rightWithoutSubStmt, '>')) { - operator[0] = '>'; - } - if (checkIsEqu(rightWithoutSubStmt)) { - operator[0] = '='; - operator[1] = '='; - } + char* operator= Lexer_getOperator(buffs, rightWithoutSubStmt); obj_setStr(ast, (char*)"operator", operator); char* rightBuff = strsCopy(buffs, right); char* subStmt1 =