diff --git a/port/linux/.vscode/launch.json b/port/linux/.vscode/launch.json index 8d5b8ff18..7738fcd01 100644 --- a/port/linux/.vscode/launch.json +++ b/port/linux/.vscode/launch.json @@ -18,6 +18,7 @@ "program": "${workspaceFolder}/build/test/pikascript_test", // "program": "${workspaceFolder}/../build/src/boot/demo06-pikamain/pikascript_demo06-pikamain", "args": [ + // "--gtest_filter=parser.__get__", // "--gtest_filter=pikaMain.print_with_enter", // "--gtest_filter=pikaMain.str_add", // "--gtest_filter=content*", diff --git a/port/linux/api-make-linux.sh b/port/linux/api-make-linux.sh old mode 100644 new mode 100755 diff --git a/port/linux/api-make-win10.sh b/port/linux/api-make-win10.sh old mode 100644 new mode 100755 diff --git a/port/linux/api-make.sh b/port/linux/api-make.sh old mode 100644 new mode 100755 diff --git a/port/linux/ci_benchmark.sh b/port/linux/ci_benchmark.sh old mode 100644 new mode 100755 diff --git a/port/linux/gtest.sh b/port/linux/gtest.sh old mode 100644 new mode 100755 diff --git a/port/linux/init.sh b/port/linux/init.sh old mode 100644 new mode 100755 diff --git a/port/linux/install_dependency.sh b/port/linux/install_dependency.sh old mode 100644 new mode 100755 diff --git a/port/linux/make.sh b/port/linux/make.sh old mode 100644 new mode 100755 diff --git a/port/linux/package/pikascript/pikascript-lib/PikaStdLib/PikaStdLib_SysObj.c b/port/linux/package/pikascript/pikascript-lib/PikaStdLib/PikaStdLib_SysObj.c index ad5f1d7d1..f11183548 100644 --- a/port/linux/package/pikascript/pikascript-lib/PikaStdLib/PikaStdLib_SysObj.c +++ b/port/linux/package/pikascript/pikascript-lib/PikaStdLib/PikaStdLib_SysObj.c @@ -138,13 +138,6 @@ Arg* PikaStdLib_SysObj_range(PikaObj* self, int a1, int a2, int a3) { return arg_setMetaObj("", "PikaStdLib_RangeObj", New_PikaStdLib_RangeObj); } -void __Sys_initObj(PikaObj* self, Arg* mate_obj) { - if (TYPE_MATE_OBJECT != arg_getType(mate_obj)) { - return; - } - Hash arg_hash = arg_getNameHash(mate_obj); -} - Arg* PikaStdLib_SysObj___get__(PikaObj* self, Arg* key, Arg* obj) { ArgType obj_type = arg_getType(obj); if ((TYPE_OBJECT == obj_type) || (TYPE_POINTER == obj_type)) { @@ -162,7 +155,6 @@ Arg* PikaStdLib_SysObj___get__(PikaObj* self, Arg* key, Arg* obj) { void PikaStdLib_SysObj___set__(PikaObj* self, Arg* key, Arg* obj, Arg* val) { ArgType obj_type = arg_getType(obj); - __Sys_initObj(self, obj); if ((TYPE_OBJECT == obj_type) || (TYPE_POINTER == obj_type)) { PikaObj* arg_obj = arg_getPtr(obj); obj_setArg(arg_obj, "__key", key); diff --git a/port/linux/pkg-push.sh b/port/linux/pkg-push.sh old mode 100644 new mode 100755 diff --git a/port/linux/pull-core.sh b/port/linux/pull-core.sh old mode 100644 new mode 100755 diff --git a/port/linux/push-core.sh b/port/linux/push-core.sh old mode 100644 new mode 100755 diff --git a/port/linux/run.sh b/port/linux/run.sh old mode 100644 new mode 100755 diff --git a/port/linux/test-banchmark.sh b/port/linux/test-banchmark.sh old mode 100644 new mode 100755 diff --git a/port/linux/test/parse-test.cpp b/port/linux/test/parse-test.cpp index 2dcf87259..5f4c43337 100644 --- a/port/linux/test/parse-test.cpp +++ b/port/linux/test/parse-test.cpp @@ -1798,3 +1798,20 @@ TEST(parser, print_ddd) { args_deinit(buffs); EXPECT_EQ(pikaMemNow(), 0); } + +TEST(parser, __get__) { + pikaMemInfo.heapUsedMax = 0; + Args* buffs = New_strBuff(); + char* lines = (char*) + "a = b[c]\n"; + printf("%s", lines); + char* pikaAsm = Parser_multiLineToAsm(buffs, (char*)lines); + printf("%s", pikaAsm); + // EXPECT_STREQ(pikaAsm, + // "B0\n" + // "1 STR [Info]: in Python config...\n" + // "0 RUN print\n" + // ); + args_deinit(buffs); + EXPECT_EQ(pikaMemNow(), 0); +} diff --git a/port/linux/update-compiler.sh b/port/linux/update-compiler.sh old mode 100644 new mode 100755 diff --git a/src/PikaParser.c b/src/PikaParser.c index f11ccc099..e5489d5d0 100644 --- a/src/PikaParser.c +++ b/src/PikaParser.c @@ -542,15 +542,27 @@ char* Lexer_popToken(Args* buffs, char* tokens_buff) { return strsPopToken(buffs, tokens_buff, 0x1F); } -uint8_t Lexer_isContain(char* tokens, char* operator) { +uint16_t Lexer_getTokenSize(char* tokens) { + return strCountSign(tokens, 0x1F) + 1; +} + +enum TokenType Lexer_getTokenType(char* token) { + return token[0]; +} + +char* Lexer_getTokenPyload(char* token) { + return (char*)((uintptr_t)token + 1); +} + +uint8_t Lexer_isContain(char* tokens, enum TokenType token_type, char* pyload) { Args* buffs = New_strBuff(); char* tokens_buff = strsCopy(buffs, tokens); uint8_t res = 0; - uint16_t token_size = strCountSign(tokens, 0x1F) + 1; + uint16_t token_size = Lexer_getTokenSize(tokens); for (int i = 0; i < token_size; i++) { char* token = Lexer_popToken(buffs, tokens_buff); - if (TOKEN_operator == token[0]) { - if (strEqu(token + 1, operator)) { + if (token_type == Lexer_getTokenType(token)) { + if (strEqu(Lexer_getTokenPyload(token), pyload)) { res = 1; goto exit; } @@ -570,7 +582,7 @@ char* Lexer_getOperator(Args* outBuffs, char* stmt) { "&", "^", "|", "<", "<=", ">", ">=", "!=", "==", "%=", "/=", "//=", "-=", "+=", "*=", "**=", " not ", " and ", " or "}; for (uint32_t i = 0; i < sizeof(operators) / 6; i++) { - if (Lexer_isContain(tokens, (char*)operators[i])) { + if (Lexer_isContain(tokens, TOKEN_operator, (char*)operators[i])) { operator= strsCopy(buffs, (char*)operators[i]); } } @@ -580,6 +592,55 @@ char* Lexer_getOperator(Args* outBuffs, char* stmt) { return operator; } +char* Parser_solveRightBranckets(Args* outBuffs, char* right) { + Args* buffs = New_args(NULL); + char* tokens = NULL; + char *token1, *token2 = NULL; + char *pyload1, *pyload2 = NULL; + Arg* right_arg = arg_setStr(NULL, "", ""); + Arg* token1_arg = NULL; + enum TokenType token_type1, token_type2; + do { + tokens = Lexer_getTokens(buffs, right); + if (!Lexer_isContain(tokens, TOKEN_devider, "[")) { + /* not contain '[', return origin */ + arg_deinit(right_arg); + right_arg = arg_setStr(right_arg, "", right); + break; + } + uint16_t len = Lexer_getTokenSize(tokens); + Lexer_popToken(buffs, tokens); + token1_arg = arg_setStr(NULL, "", Lexer_popToken(buffs, tokens)); + for (int i = 0; i < len; i++) { + char* token_buffs = New_strBuff(); + token1 = strsCopy(token_buffs, arg_getStr(token1_arg)); + arg_deinit(token1_arg); + token2 = Lexer_popToken(token_buffs, tokens); + token1_arg = arg_setStr(NULL, "", token2); + token_type1 = Lexer_getTokenType(token1); + token_type2 = Lexer_getTokenType(token2); + pyload1 = Lexer_getTokenPyload(token1); + pyload2 = Lexer_getTokenPyload(token2); + + /* matched [] */ + if ((TOKEN_devider == token_type1) && (strEqu(pyload1, "["))) { + } else if ((TOKEN_devider == token_type1) && + (strEqu(pyload1, "]"))) { + } else { + right_arg = arg_strAppend(right_arg, pyload1); + } + args_deinit(token_buffs); + } + arg_deinit(token1_arg); + } while (0); + + /* clean and retur */ + right = strsCopy(outBuffs, arg_getStr(right_arg)); + arg_deinit(right_arg); + args_deinit(buffs); + return right; +} + AST* AST_parseStmt(AST* ast, char* stmt) { Args* buffs = New_strBuff(); char* assignment = strsGetFirstToken(buffs, stmt, '('); @@ -604,6 +665,10 @@ AST* AST_parseStmt(AST* ast, char* stmt) { } else { right = stmt; } + /* solve the [] stmt */ + right = Parser_solveRightBranckets(buffs, right); + + /* match statment type */ enum StmtType stmtType = Lexer_matchStmtType(right); /* solve operator stmt */ if (STMT_operator == stmtType) { diff --git a/src/PikaVM.c b/src/PikaVM.c index bf2470457..206015702 100644 --- a/src/PikaVM.c +++ b/src/PikaVM.c @@ -60,7 +60,6 @@ enum Instruct { #define __INS_ENUM #include "__instruction_table.cfg" - __INSTRCUTION_CNT, }; @@ -370,27 +369,29 @@ static Arg* VM_instruction_handler_OPT(PikaObj* self, goto OPT_exit; } /* default: int and float */ - outArg = arg_setInt(outArg, "", - (num1_f - num2_f) * (num1_f - num2_f) < 0.000001); + outArg = + arg_setInt(outArg, "", + (num1_f - num2_f) * (num1_f - num2_f) < (float)0.000001); goto OPT_exit; } if (strEqu("!=", data)) { outArg = arg_setInt( - outArg, "", !((num1_f - num2_f) * (num1_f - num2_f) < 0.000001)); + outArg, "", + !((num1_f - num2_f) * (num1_f - num2_f) < (float)0.000001)); goto OPT_exit; } if (strEqu(">=", data)) { - outArg = - arg_setInt(outArg, "", - (num1_f > num2_f) || - ((num1_f - num2_f) * (num1_f - num2_f) < 0.000001)); + outArg = arg_setInt( + outArg, "", + (num1_f > num2_f) || + ((num1_f - num2_f) * (num1_f - num2_f) < (float)0.000001)); goto OPT_exit; } if (strEqu("<=", data)) { - outArg = - arg_setInt(outArg, "", - (num1_f < num2_f) || - ((num1_f - num2_f) * (num1_f - num2_f) < 0.000001)); + outArg = arg_setInt( + outArg, "", + (num1_f < num2_f) || + ((num1_f - num2_f) * (num1_f - num2_f) < (float)0.000001)); goto OPT_exit; } if (strEqu("&", data)) { diff --git a/src/dataStrs.c b/src/dataStrs.c index 43788bf78..249ab5ed1 100644 --- a/src/dataStrs.c +++ b/src/dataStrs.c @@ -142,8 +142,12 @@ char* strsReplace(Args* buffs, char* orig, char* rep, char* with) { len_with = strlen(with); // count the number of replacements needed ins = orig; - for (count = 0; (tmp = strstr(ins, rep)); ++count) { + tmp = strstr(ins, rep); + count = 0; + while (tmp) { + count++; ins = tmp + len_rep; + tmp = strstr(ins, rep); } tmp = args_getBuff(buffs, strlen(orig) + (len_with - len_rep) * count + 1); result = tmp;