!41 Support chain invoke like < String('test').split() >

* support chain invoke ok
* add test for S(S.replace()).replace()
* parse to RUN .split
* try to support chain invoke, not ok
* can devide a chain
This commit is contained in:
李昂 2022-07-05 06:23:49 +00:00
parent 2c46bc6b40
commit 4a943b479c
15 changed files with 342 additions and 139 deletions

View File

@ -392,7 +392,7 @@ Arg* PikaStdLib_SysObj___slice__(PikaObj* self,
Arg* obj,
Arg* start,
int step) {
#if PIKA_SYNTEX_ITEM_SLICE_ENABLE
#if PIKA_SYNTEX_SLICE_ENABLE
/* No interger index only support __get__ */
if (!(arg_getType(start) == ARG_TYPE_INT &&
arg_getType(end) == ARG_TYPE_INT)) {
@ -528,7 +528,7 @@ void PikaStdLib_SysObj_printNoEnd(PikaObj* self, Arg* val) {
}
char* PikaStdLib_SysObj_cformat(PikaObj* self, char* fmt, PikaTuple* var) {
#if PIKA_SYNTEX_ITEM_FORMAT_ENABLE
#if PIKA_SYNTEX_FORMAT_ENABLE
Args buffs = {0};
pikaMemMaxReset();
char* res = strsFormatList(&buffs, fmt, &var->super);
@ -538,7 +538,7 @@ char* PikaStdLib_SysObj_cformat(PikaObj* self, char* fmt, PikaTuple* var) {
return res;
#else
obj_setErrorCode(self, 1);
__platform_printf("[Error] PIKA_SYNTEX_ITEM_FORMAT_ENABLE is not enabled.\r\n");
__platform_printf("[Error] PIKA_SYNTEX_FORMAT_ENABLE is not enabled.\r\n");
return NULL;
#endif
}

View File

@ -11,7 +11,7 @@
"program": "${workspaceFolder}/build/test/pikascript_test",
// "program": "${workspaceFolder}/build/boot/demo06-pikamain/pikascript_demo06-pikamain",
"args": [
// "--gtest_filter=parser.configparser"
// "--gtest_filter=string.split_chain"
],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",

View File

@ -392,7 +392,7 @@ Arg* PikaStdLib_SysObj___slice__(PikaObj* self,
Arg* obj,
Arg* start,
int step) {
#if PIKA_SYNTEX_ITEM_SLICE_ENABLE
#if PIKA_SYNTEX_SLICE_ENABLE
/* No interger index only support __get__ */
if (!(arg_getType(start) == ARG_TYPE_INT &&
arg_getType(end) == ARG_TYPE_INT)) {
@ -528,7 +528,7 @@ void PikaStdLib_SysObj_printNoEnd(PikaObj* self, Arg* val) {
}
char* PikaStdLib_SysObj_cformat(PikaObj* self, char* fmt, PikaTuple* var) {
#if PIKA_SYNTEX_ITEM_FORMAT_ENABLE
#if PIKA_SYNTEX_FORMAT_ENABLE
Args buffs = {0};
pikaMemMaxReset();
char* res = strsFormatList(&buffs, fmt, &var->super);
@ -538,7 +538,7 @@ char* PikaStdLib_SysObj_cformat(PikaObj* self, char* fmt, PikaTuple* var) {
return res;
#else
obj_setErrorCode(self, 1);
__platform_printf("[Error] PIKA_SYNTEX_ITEM_FORMAT_ENABLE is not enabled.\r\n");
__platform_printf("[Error] PIKA_SYNTEX_FORMAT_ENABLE is not enabled.\r\n");
return NULL;
#endif
}

View File

@ -1,6 +1,6 @@
#include "test_common.h"
#if PIKA_SYNTEX_ITEM_SLICE_ENABLE
#if PIKA_SYNTEX_SLICE_ENABLE
TEST(builtin, bytes) {
/* init */
pikaMemInfo.heapUsedMax = 0;

View File

@ -1,6 +1,6 @@
#include "test_common.h"
#if PIKA_SYNTEX_ITEM_SLICE_ENABLE
#if PIKA_SYNTEX_SLICE_ENABLE
TEST(configparser, test1) {
char* s =
"[DEFAULT]\n"

View File

@ -1,6 +1,6 @@
#include "test_common.h"
#if PIKA_SYNTEX_ITEM_SLICE_ENABLE
#if PIKA_SYNTEX_SLICE_ENABLE
TEST(ctypes, test1) {
/* init */
pikaMemInfo.heapUsedMax = 0;

View File

@ -1800,7 +1800,7 @@ TEST(parser, print_ddd) {
EXPECT_EQ(pikaMemNow(), 0);
}
#if PIKA_SYNTEX_ITEM_SLICE_ENABLE
#if PIKA_SYNTEX_SLICE_ENABLE
TEST(parser, __get__3) {
pikaMemInfo.heapUsedMax = 0;
Args* buffs = New_strBuff();
@ -1839,7 +1839,7 @@ TEST(parser, __get__3) {
}
#endif
#if PIKA_SYNTEX_ITEM_SLICE_ENABLE
#if PIKA_SYNTEX_SLICE_ENABLE
TEST(parser, __get__) {
pikaMemInfo.heapUsedMax = 0;
Args* buffs = New_strBuff();
@ -1862,7 +1862,7 @@ TEST(parser, __get__) {
}
#endif
#if PIKA_SYNTEX_ITEM_SLICE_ENABLE
#if PIKA_SYNTEX_SLICE_ENABLE
TEST(parser, __get__2) {
pikaMemInfo.heapUsedMax = 0;
Args* buffs = New_strBuff();
@ -1889,7 +1889,7 @@ TEST(parser, __get__2) {
}
#endif
#if PIKA_SYNTEX_ITEM_SLICE_ENABLE
#if PIKA_SYNTEX_SLICE_ENABLE
TEST(parser, __set__) {
pikaMemInfo.heapUsedMax = 0;
Args* buffs = New_strBuff();
@ -2198,7 +2198,7 @@ TEST(asmer, asm_to_bytecode_0x0d) {
EXPECT_EQ(pikaMemNow(), 0);
}
#if PIKA_SYNTEX_ITEM_SLICE_ENABLE
#if PIKA_SYNTEX_SLICE_ENABLE
TEST(parser, list_1_2) {
pikaMemInfo.heapUsedMax = 0;
Args* buffs = New_strBuff();
@ -2615,7 +2615,7 @@ TEST(parser, str_equ) {
EXPECT_EQ(pikaMemNow(), 0);
}
#if PIKA_SYNTEX_ITEM_SLICE_ENABLE
#if PIKA_SYNTEX_SLICE_ENABLE
TEST(parser, bytes_index) {
pikaMemInfo.heapUsedMax = 0;
Args* buffs = New_strBuff();
@ -2693,7 +2693,7 @@ TEST(parser, tab) {
EXPECT_EQ(pikaMemNow(), 0);
}
#if PIKA_SYNTEX_ITEM_SLICE_ENABLE
#if PIKA_SYNTEX_SLICE_ENABLE
TEST(parser, parse_issue2) {
pikaMemInfo.heapUsedMax = 0;
Args* buffs = New_strBuff();
@ -2728,7 +2728,7 @@ TEST(parser, parse_issue3) {
EXPECT_EQ(pikaMemNow(), 0);
}
#if PIKA_SYNTEX_ITEM_SLICE_ENABLE
#if PIKA_SYNTEX_SLICE_ENABLE
TEST(parser, slice1) {
pikaMemInfo.heapUsedMax = 0;
Args* buffs = New_strBuff();
@ -2749,7 +2749,7 @@ TEST(parser, slice1) {
}
#endif
#if PIKA_SYNTEX_ITEM_SLICE_ENABLE
#if PIKA_SYNTEX_SLICE_ENABLE
TEST(parser, slice2) {
pikaMemInfo.heapUsedMax = 0;
Args* buffs = New_strBuff();
@ -2919,7 +2919,7 @@ TEST(parser, mpy1) {
EXPECT_EQ(pikaMemNow(), 0);
}
#if PIKA_SYNTEX_ITEM_SLICE_ENABLE
#if PIKA_SYNTEX_SLICE_ENABLE
TEST(parser, slice_12lkj) {
pikaMemInfo.heapUsedMax = 0;
Args* buffs = New_strBuff();
@ -3114,7 +3114,7 @@ TEST(parser, connection2) {
EXPECT_EQ(pikaMemNow(), 0);
}
#if PIKA_SYNTEX_ITEM_FORMAT_ENABLE
#if PIKA_SYNTEX_FORMAT_ENABLE
TEST(parser, format1) {
pikaMemInfo.heapUsedMax = 0;
Args* buffs = New_strBuff();
@ -3133,7 +3133,7 @@ TEST(parser, format1) {
}
#endif
#if PIKA_SYNTEX_ITEM_FORMAT_ENABLE
#if PIKA_SYNTEX_FORMAT_ENABLE
TEST(parser, format2) {
pikaMemInfo.heapUsedMax = 0;
Args* buffs = New_strBuff();
@ -3349,7 +3349,6 @@ TEST(parser, def_issue1) {
"0 DEF options(self)\n"
"0 JMP 1\n"
"B1\n"
"0 REF ...\n"
"B1\n"
"0 RET \n"
"B0\n");
@ -3357,9 +3356,48 @@ TEST(parser, def_issue1) {
EXPECT_EQ(pikaMemNow(), 0);
}
TEST(parser, configparser){
TEST(parser, configparser) {
Args buffs = {0};
char* res = Parser_fileToAsm(&buffs, "package/pikascript/pika_configparser.py");
__platform_printf("%s", res);
char* res =
Parser_fileToAsm(&buffs, "package/pikascript/pika_configparser.py");
// printf("%s", res);
strsDeinit(&buffs);
}
TEST(lexser, function_chain) {
/* init */
pikaMemInfo.heapUsedMax = 0;
Args* buffs = New_strBuff();
/* run */
char* tokens = Lexer_getTokens(buffs, "String('a,b,c').split(',')");
char* printTokens = Lexer_printTokens(buffs, tokens);
printf("%s\n", printTokens);
/* assert */
EXPECT_STREQ(
printTokens,
"{sym}String{dvd}({lit}'a,b,c'{dvd}){sym}.split{dvd}({lit}','{dvd})");
/* deinit */
args_deinit(buffs);
EXPECT_EQ(pikaMemNow(), 0);
}
TEST(parser, function_chain) {
pikaMemInfo.heapUsedMax = 0;
Args* buffs = New_strBuff();
char* lines = "a = String('a,b,c').split(',')\n";
__platform_printf("%s\n", lines);
char* pikaAsm = Parser_multiLineToAsm(buffs, lines);
__platform_printf("%s", pikaAsm);
EXPECT_STREQ(pikaAsm,
"B0\n"
"2 STR a,b,c\n"
"1 RUN String\n"
"1 STR ,\n"
"0 RUN .split\n"
"0 OUT a\n");
args_deinit(buffs);
EXPECT_EQ(pikaMemNow(), 0);
}

View File

@ -809,7 +809,7 @@ TEST(pikaMain, str___get____set__) {
EXPECT_EQ(pikaMemNow(), 0);
}
#if PIKA_SYNTEX_ITEM_SLICE_ENABLE
#if PIKA_SYNTEX_SLICE_ENABLE
TEST(pikaMain, str__index__) {
/* init */
pikaMemInfo.heapUsedMax = 0;
@ -837,7 +837,7 @@ TEST(pikaMain, str__index__) {
}
#endif
#if PIKA_SYNTEX_ITEM_SLICE_ENABLE
#if PIKA_SYNTEX_SLICE_ENABLE
TEST(pikaMain, bytes__index__) {
/* init */
pikaMemInfo.heapUsedMax = 0;
@ -865,7 +865,7 @@ TEST(pikaMain, bytes__index__) {
}
#endif
#if PIKA_SYNTEX_ITEM_SLICE_ENABLE
#if PIKA_SYNTEX_SLICE_ENABLE
TEST(pikaMain, list_index) {
/* init */
pikaMemInfo.heapUsedMax = 0;
@ -887,7 +887,7 @@ TEST(pikaMain, list_index) {
}
#endif
#if PIKA_SYNTEX_ITEM_SLICE_ENABLE
#if PIKA_SYNTEX_SLICE_ENABLE
TEST(pikaMain, dict_index) {
/* init */
pikaMemInfo.heapUsedMax = 0;
@ -1922,7 +1922,7 @@ TEST(pikaMain, REPL_push_mode) {
EXPECT_EQ(pikaMemNow(), 0);
}
#if PIKA_SYNTEX_ITEM_SLICE_ENABLE
#if PIKA_SYNTEX_SLICE_ENABLE
TEST(pikaMain, int_from_bytes) {
/* init */
pikaMemInfo.heapUsedMax = 0;
@ -1952,7 +1952,7 @@ TEST(pikaMain, int_from_bytes) {
// EXPECT_EQ(pikaMemNow(), 0);
// }
#if PIKA_SYNTEX_ITEM_SLICE_ENABLE
#if PIKA_SYNTEX_SLICE_ENABLE
TEST(pikaMain, utils_int_to_bytes) {
/* init */
pikaMemInfo.heapUsedMax = 0;
@ -1972,7 +1972,7 @@ TEST(pikaMain, utils_int_to_bytes) {
}
#endif
#if PIKA_SYNTEX_ITEM_SLICE_ENABLE
#if PIKA_SYNTEX_SLICE_ENABLE
TEST(pikaMain, neg_index) {
/* init */
pikaMemInfo.heapUsedMax = 0;
@ -1992,7 +1992,7 @@ TEST(pikaMain, neg_index) {
}
#endif
#if PIKA_SYNTEX_ITEM_SLICE_ENABLE
#if PIKA_SYNTEX_SLICE_ENABLE
TEST(pikaMain, slice1) {
/* init */
pikaMemInfo.heapUsedMax = 0;
@ -2009,7 +2009,7 @@ TEST(pikaMain, slice1) {
}
#endif
#if PIKA_SYNTEX_ITEM_SLICE_ENABLE
#if PIKA_SYNTEX_SLICE_ENABLE
TEST(pikaMain, slice2) {
/* init */
pikaMemInfo.heapUsedMax = 0;
@ -2058,7 +2058,7 @@ TEST(pikaMain, str_add1) {
EXPECT_EQ(pikaMemNow(), 0);
}
#if PIKA_SYNTEX_ITEM_SLICE_ENABLE
#if PIKA_SYNTEX_SLICE_ENABLE
TEST(pikaMain, slice_eei) {
/* init */
pikaMemInfo.heapUsedMax = 0;
@ -2075,7 +2075,7 @@ TEST(pikaMain, slice_eei) {
}
#endif
#if PIKA_SYNTEX_ITEM_SLICE_ENABLE
#if PIKA_SYNTEX_SLICE_ENABLE
TEST(pikaMain, slice_a97) {
/* init */
pikaMemInfo.heapUsedMax = 0;
@ -2092,7 +2092,7 @@ TEST(pikaMain, slice_a97) {
}
#endif
#if PIKA_SYNTEX_ITEM_SLICE_ENABLE
#if PIKA_SYNTEX_SLICE_ENABLE
TEST(pikaMain, slice_a90) {
/* init */
pikaMemInfo.heapUsedMax = 0;
@ -2109,7 +2109,7 @@ TEST(pikaMain, slice_a90) {
}
#endif
#if PIKA_SYNTEX_ITEM_SLICE_ENABLE
#if PIKA_SYNTEX_SLICE_ENABLE
TEST(pikaMain, slice_a12) {
/* init */
pikaMemInfo.heapUsedMax = 0;
@ -2145,7 +2145,7 @@ TEST(pikaMain, str_string) {
EXPECT_EQ(pikaMemNow(), 0);
}
#if PIKA_SYNTEX_ITEM_SLICE_ENABLE
#if PIKA_SYNTEX_SLICE_ENABLE
TEST(pikaMain, string_index) {
/* init */
pikaMemInfo.heapUsedMax = 0;

View File

@ -1,7 +1,7 @@
#include "test_common.h"
#if PIKA_SYNTEX_ITEM_SLICE_ENABLE
#if PIKA_SYNTEX_SLICE_ENABLE
TEST(stddata, test1) {
/* init */
pikaMemInfo.heapUsedMax = 0;

View File

@ -1,6 +1,6 @@
#include "test_common.h"
#if PIKA_SYNTEX_ITEM_FORMAT_ENABLE
#if PIKA_SYNTEX_FORMAT_ENABLE
TEST(string, cformat) {
/* init */
pikaMemInfo.heapUsedMax = 0;
@ -17,7 +17,7 @@ TEST(string, cformat) {
}
#endif
#if PIKA_SYNTEX_ITEM_FORMAT_ENABLE
#if PIKA_SYNTEX_FORMAT_ENABLE
TEST(string, cformat1) {
/* init */
pikaMemInfo.heapUsedMax = 0;
@ -34,7 +34,7 @@ TEST(string, cformat1) {
}
#endif
#if PIKA_SYNTEX_ITEM_FORMAT_ENABLE
#if PIKA_SYNTEX_FORMAT_ENABLE
TEST(string, format1) {
/* init */
pikaMemInfo.heapUsedMax = 0;
@ -50,7 +50,7 @@ TEST(string, format1) {
}
#endif
#if PIKA_SYNTEX_ITEM_FORMAT_ENABLE
#if PIKA_SYNTEX_FORMAT_ENABLE
TEST(string, format2) {
/* init */
pikaMemInfo.heapUsedMax = 0;
@ -66,7 +66,7 @@ TEST(string, format2) {
}
#endif
#if PIKA_SYNTEX_ITEM_FORMAT_ENABLE
#if PIKA_SYNTEX_FORMAT_ENABLE
TEST(string, print_file) {
/* init */
pikaMemInfo.heapUsedMax = 0;
@ -87,7 +87,7 @@ TEST(string, print_file) {
}
#endif
#if PIKA_SYNTEX_ITEM_FORMAT_ENABLE
#if PIKA_SYNTEX_FORMAT_ENABLE
TEST(string, format_parse1) {
pikaMemInfo.heapUsedMax = 0;
Args* buffs = New_strBuff();
@ -146,7 +146,6 @@ TEST(string, strip) {
EXPECT_EQ(pikaMemNow(), 0);
}
TEST(string, replace) {
/* init */
pikaMemInfo.heapUsedMax = 0;
@ -164,3 +163,63 @@ TEST(string, replace) {
obj_deinit(pikaMain);
EXPECT_EQ(pikaMemNow(), 0);
}
#if PIKA_SYNTEX_IMPORT_EX_ENABLE
TEST(string, replace_chain) {
/* init */
pikaMemInfo.heapUsedMax = 0;
PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain);
/* run */
obj_run(
pikaMain,
"from PikaStdData import String as S\n"
"res = PikaStdData.String(' a,b,c, d ').replace('a', 'A')\n"
"res2 = S(S('[test]').replace('[','')).replace(']','')\n"
"\n");
/* collect */
char* res = obj_getStr(pikaMain, "res");
char* res2 = obj_getStr(pikaMain, "res2");
/* assert */
EXPECT_STREQ(res, " A,b,c, d ");
EXPECT_STREQ(res2, "test");
/* deinit */
obj_deinit(pikaMain);
EXPECT_EQ(pikaMemNow(), 0);
}
#endif
TEST(string, split_chain) {
/* init */
pikaMemInfo.heapUsedMax = 0;
PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain);
/* run */
__platform_printf("BEGIN\r\n");
obj_run(pikaMain,
"PikaStdData.String(' a,b,c, d ').split(',')\n"
"\n");
/* collect */
/* assert */
EXPECT_STREQ(log_buff[0], "[ a, b, c, d ]\r\n");
EXPECT_STREQ(log_buff[1], "BEGIN\r\n");
/* deinit */
obj_deinit(pikaMain);
EXPECT_EQ(pikaMemNow(), 0);
}
TEST(string, str_chain) {
/* init */
pikaMemInfo.heapUsedMax = 0;
PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain);
/* run */
__platform_printf("BEGIN\r\n");
obj_run(pikaMain,
"PikaStdData.String('test').str\n"
"\n");
/* collect */
/* assert */
EXPECT_STREQ(log_buff[0], "test\r\n");
EXPECT_STREQ(log_buff[1], "BEGIN\r\n");
/* deinit */
obj_deinit(pikaMain);
EXPECT_EQ(pikaMemNow(), 0);
}

View File

@ -186,54 +186,64 @@ static enum StmtType Lexer_matchStmtType(char* right) {
rightWithoutSubStmt =
__removeTokensBetween(&buffs, rightWithoutSubStmt, "{", "}");
uint8_t is_get_operator = 0;
uint8_t is_get_method = 0;
uint8_t is_get_string = 0;
uint8_t is_get_bytes = 0;
uint8_t is_get_number = 0;
uint8_t is_get_symbol = 0;
uint8_t is_get_list = 0;
uint8_t is_get_dict = 0;
uint8_t is_get_import = 0;
PIKA_BOOL is_get_operator = PIKA_FALSE;
PIKA_BOOL is_get_method = PIKA_FALSE;
PIKA_BOOL is_get_string = PIKA_FALSE;
PIKA_BOOL is_get_bytes = PIKA_FALSE;
PIKA_BOOL is_get_number = PIKA_FALSE;
PIKA_BOOL is_get_symbol = PIKA_FALSE;
PIKA_BOOL is_get_list = PIKA_FALSE;
PIKA_BOOL is_get_dict = PIKA_FALSE;
PIKA_BOOL is_get_import = PIKA_FALSE;
PIKA_BOOL is_get_chain = PIKA_FALSE;
ParserState_forEachToken(ps, rightWithoutSubStmt) {
ParserState_iterStart(&ps);
/* collect type */
if (strEqu(ps.token1.pyload, " import ")) {
is_get_import = 1;
is_get_import = PIKA_TRUE;
goto iter_continue;
}
if (strEqu(ps.token1.pyload, "[")) {
is_get_list = 1;
is_get_list = PIKA_TRUE;
goto iter_continue;
}
if (strEqu(ps.token1.pyload, "...")) {
goto iter_continue;
}
if (strIsStartWith(ps.token1.pyload, ".")) {
if (ps.iter_index != 1) {
is_get_chain = PIKA_TRUE;
goto iter_continue;
}
}
if (strEqu(ps.token1.pyload, "{")) {
is_get_dict = 1;
is_get_dict = PIKA_TRUE;
goto iter_continue;
}
if (ps.token1.type == TOKEN_operator) {
is_get_operator = 1;
is_get_operator = PIKA_TRUE;
goto iter_continue;
}
if (ps.token2.type == TOKEN_devider) {
is_get_method = 1;
is_get_method = PIKA_TRUE;
goto iter_continue;
}
if (ps.token1.type == TOKEN_literal) {
if (ps.token1.pyload[0] == '\'' || ps.token1.pyload[0] == '"') {
is_get_string = 1;
is_get_string = PIKA_TRUE;
goto iter_continue;
}
if (ps.token1.pyload[1] == '\'' || ps.token1.pyload[1] == '"') {
if (ps.token1.pyload[0] == 'b') {
is_get_bytes = 1;
is_get_bytes = PIKA_TRUE;
goto iter_continue;
}
}
is_get_number = 1;
is_get_number = PIKA_TRUE;
goto iter_continue;
}
if (ps.token1.type == TOKEN_symbol) {
is_get_symbol = 1;
is_get_symbol = PIKA_TRUE;
goto iter_continue;
}
iter_continue:
@ -255,6 +265,10 @@ static enum StmtType Lexer_matchStmtType(char* right) {
stmtType = STMT_dict;
goto exit;
}
if (is_get_chain) {
stmtType = STMT_chain;
goto exit;
}
if (is_get_method) {
stmtType = STMT_method;
goto exit;
@ -845,7 +859,7 @@ void ParserState_beforeIter(struct ParserState* ps) {
arg_setStr(NULL, "", Parser_popToken(ps->buffs_p, ps->tokens));
}
#if PIKA_SYNTEX_ITEM_SLICE_ENABLE
#if PIKA_SYNTEX_SLICE_ENABLE
static void Slice_getPars(Args* outBuffs,
char* inner,
char** pStart,
@ -902,7 +916,7 @@ static void Slice_getPars(Args* outBuffs,
}
#endif
#if PIKA_SYNTEX_ITEM_SLICE_ENABLE
#if PIKA_SYNTEX_SLICE_ENABLE
char* Parser_solveBranckets(Args* outBuffs,
char* content,
char* stmt,
@ -1030,7 +1044,7 @@ exit:
}
#endif
#if PIKA_SYNTEX_ITEM_SLICE_ENABLE
#if PIKA_SYNTEX_SLICE_ENABLE
char* Parser_solveRightBranckets(Args* outBuffs, char* right) {
return Parser_solveBranckets(outBuffs, right, NULL, "right");
}
@ -1040,7 +1054,7 @@ char* Parser_solveLeftBranckets(Args* outBuffs, char* right, char* left) {
}
#endif
#if PIKA_SYNTEX_ITEM_FORMAT_ENABLE
#if PIKA_SYNTEX_FORMAT_ENABLE
char* Parser_solveFormat(Args* outBuffs, char* right) {
/* quick skip */
if (!strIsContain(right, '%')) {
@ -1217,6 +1231,17 @@ exit:
return is_left_exist;
}
PIKA_RES AST_setThisNode(AST* ast, char* node_type, char* node_content) {
return obj_setStr(ast, node_type, node_content);
}
AST* AST_parseStmt(AST* ast, char* stmt);
PIKA_RES AST_parseSubStmt(AST* ast, char* node_content) {
queueObj_pushObj(ast, (char*)"stmt");
AST_parseStmt(queueObj_getCurrentObj(ast), node_content);
return PIKA_RES_OK;
}
AST* AST_parseStmt(AST* ast, char* stmt) {
Args buffs = {0};
char* assignment = strsGetFirstToken(&buffs, stmt, '(');
@ -1260,7 +1285,7 @@ AST* AST_parseStmt(AST* ast, char* stmt) {
isLeftExist = Parser_solveSelfOperator(&buffs, stmt, &right, &left);
}
#if PIKA_SYNTEX_ITEM_SLICE_ENABLE
#if PIKA_SYNTEX_SLICE_ENABLE
char* right_new = right;
/* solve the [] stmt */
right = Parser_solveRightBranckets(&buffs, right);
@ -1272,13 +1297,13 @@ AST* AST_parseStmt(AST* ast, char* stmt) {
}
#endif
#if PIKA_SYNTEX_ITEM_FORMAT_ENABLE
#if PIKA_SYNTEX_FORMAT_ENABLE
right = Parser_solveFormat(&buffs, right);
#endif
/* set left */
if (isLeftExist) {
obj_setStr(ast, (char*)"left", left);
AST_setThisNode(ast, (char*)"left", left);
}
/* match statment type */
enum StmtType stmtType = Lexer_matchStmtType(right);
@ -1290,22 +1315,20 @@ AST* AST_parseStmt(AST* ast, char* stmt) {
result = PIKA_RES_ERR_SYNTAX_ERROR;
goto exit;
}
obj_setStr(ast, (char*)"operator", operator);
AST_setThisNode(ast, (char*)"operator", operator);
char* rightBuff = strsCopy(&buffs, right);
char* subStmt1 =
strsPopTokenWithSkip_byStr(&buffs, rightBuff, operator, '(', ')');
char* subStmt2 = rightBuff;
queueObj_pushObj(ast, (char*)"stmt");
AST_parseStmt(queueObj_getCurrentObj(ast), subStmt1);
queueObj_pushObj(ast, (char*)"stmt");
AST_parseStmt(queueObj_getCurrentObj(ast), subStmt2);
AST_parseSubStmt(ast, subStmt1);
AST_parseSubStmt(ast, subStmt2);
goto exit;
}
#if PIKA_BUILTIN_LIST_ENABLE
/* solve list stmt */
if (STMT_list == stmtType) {
obj_setStr(ast, (char*)"list", "list");
AST_setThisNode(ast, (char*)"list", "list");
char* subStmts = strsCut(&buffs, right, '[', ']');
subStmts = strsAppend(&buffs, subStmts, ",");
Arg* subStmt = arg_setStr(NULL, "", "");
@ -1321,9 +1344,8 @@ AST* AST_parseStmt(AST* ast, char* stmt) {
/* not in brankets */
if (strEqu(ps.token1.pyload, ",")) {
/* found "," push subStmt */
queueObj_pushObj(ast, (char*)"stmt");
subStmt_str = arg_getStr(subStmt);
AST_parseStmt(queueObj_getCurrentObj(ast), subStmt_str);
AST_parseSubStmt(ast, subStmt_str);
/* clear subStmt */
arg_deinit(subStmt);
subStmt = arg_setStr(NULL, "", "");
@ -1344,7 +1366,7 @@ AST* AST_parseStmt(AST* ast, char* stmt) {
#if PIKA_BUILTIN_DICT_ENABLE
/* solve list stmt */
if (STMT_dict == stmtType) {
obj_setStr(ast, (char*)"dict", "dict");
AST_setThisNode(ast, (char*)"dict", "dict");
char* subStmts = strsCut(&buffs, right, '{', '}');
subStmts = strsAppend(&buffs, subStmts, ",");
Arg* subStmt = arg_setStr(NULL, "", "");
@ -1361,9 +1383,8 @@ AST* AST_parseStmt(AST* ast, char* stmt) {
if (strEqu(ps.token1.pyload, ",") ||
strEqu(ps.token1.pyload, ":")) {
/* found "," or ":" push subStmt */
queueObj_pushObj(ast, (char*)"stmt");
subStmt_str = arg_getStr(subStmt);
AST_parseStmt(queueObj_getCurrentObj(ast), subStmt_str);
AST_parseSubStmt(ast, subStmt_str);
/* clear subStmt */
arg_deinit(subStmt);
subStmt = arg_setStr(NULL, "", "");
@ -1381,10 +1402,50 @@ AST* AST_parseStmt(AST* ast, char* stmt) {
}
#endif
/* solve method chain */
if (STMT_chain == stmtType) {
uint8_t last_chain_i = 0;
{
ParserState_forEachToken(ps, right) {
ParserState_iterStart(&ps);
if (ps.branket_deepth > 0) {
ParserState_iterEnd(&ps);
continue;
}
/* not in brankets */
if (strIsStartWith(ps.token1.pyload, ".")) {
last_chain_i = ps.iter_index;
}
ParserState_iterEnd(&ps);
}
ParserState_deinit(&ps);
}
Arg* chainStmt = arg_setStr(NULL, "", "");
Arg* mainStmt = arg_setStr(NULL, "", "");
ParserState_forEachToken(ps, right) {
ParserState_iterStart(&ps);
/* append substmt1 to last_chain_i*/
if (ps.iter_index < last_chain_i) {
chainStmt = arg_strAppend(chainStmt, ps.token1.pyload);
}
/* append substmt2 from last_chain_i to end */
if (ps.iter_index >= last_chain_i) {
mainStmt = arg_strAppend(mainStmt, ps.token1.pyload);
}
ParserState_iterEnd(&ps);
}
ParserState_deinit(&ps);
AST_parseSubStmt(ast, arg_getStr(chainStmt));
AST_parseStmt(ast, arg_getStr(mainStmt));
arg_deinit(chainStmt);
arg_deinit(mainStmt);
goto exit;
}
/* solve method stmt */
if (STMT_method == stmtType) {
method = strsGetFirstToken(&buffs, right, '(');
obj_setStr(ast, (char*)"method", method);
AST_setThisNode(ast, (char*)"method", method);
char* subStmts = strsCut(&buffs, right, '(', ')');
/* add ',' at the end */
subStmts = strsAppend(&buffs, subStmts, ",");
@ -1404,9 +1465,8 @@ AST* AST_parseStmt(AST* ast, char* stmt) {
/* not in brankets */
if (strEqu(ps.token1.pyload, ",")) {
/* found "," push subStmt */
queueObj_pushObj(ast, (char*)"stmt");
subStmt_str = arg_getStr(subStmt);
AST_parseStmt(queueObj_getCurrentObj(ast), subStmt_str);
AST_parseSubStmt(ast, subStmt_str);
/* clear subStmt */
arg_deinit(subStmt);
subStmt = arg_setStr(NULL, "", "");
@ -1427,13 +1487,13 @@ AST* AST_parseStmt(AST* ast, char* stmt) {
/* solve reference stmt */
if (STMT_reference == stmtType) {
ref = right;
obj_setStr(ast, (char*)"ref", ref);
AST_setThisNode(ast, (char*)"ref", ref);
goto exit;
}
/* solve import stmt */
if (STMT_import == stmtType) {
import = strsGetLastToken(&buffs, right, ' ');
obj_setStr(ast, (char*)"import", import);
AST_setThisNode(ast, (char*)"import", import);
goto exit;
}
/* solve str stmt */
@ -1448,7 +1508,7 @@ AST* AST_parseStmt(AST* ast, char* stmt) {
str = strsReplace(&buffs, str, "\\\"", "\"");
str = strsReplace(&buffs, str, "\\'", "'");
}
obj_setStr(ast, (char*)"string", str);
AST_setThisNode(ast, (char*)"string", str);
goto exit;
}
/* solve bytes stmt */
@ -1456,13 +1516,13 @@ AST* AST_parseStmt(AST* ast, char* stmt) {
str = right + 1;
str = strsDeleteChar(&buffs, str, '\'');
str = strsDeleteChar(&buffs, str, '\"');
obj_setStr(ast, (char*)"bytes", str);
AST_setThisNode(ast, (char*)"bytes", str);
goto exit;
}
/* solve number stmt */
if (STMT_number == stmtType) {
num = right;
obj_setStr(ast, (char*)"num", num);
AST_setThisNode(ast, (char*)"num", num);
goto exit;
}
exit:
@ -1585,7 +1645,7 @@ AST* AST_parseLine(char* line, Stack* block_stack) {
if (strIsStartWith(line_start, keyword) &&
(line_start[keyword_len] == ' ')) {
stmt = strsCut(&buffs, line_start, ' ', ':');
obj_setStr(ast, "block", keyword);
AST_setThisNode(ast, "block", keyword);
if (NULL != block_stack) {
stack_pushStr(block_stack, keyword);
}
@ -1601,7 +1661,7 @@ AST* AST_parseLine(char* line, Stack* block_stack) {
if ((strIsStartWith(line_start, keyward)) &&
((line_start[keyward_size] == ' ') ||
(line_start[keyward_size] == 0))) {
obj_setStr(ast, keyward, "");
AST_setThisNode(ast, keyward, "");
stmt = "";
goto block_matched;
}
@ -1612,15 +1672,15 @@ AST* AST_parseLine(char* line, Stack* block_stack) {
Args* list_buffs = New_strBuff();
char* line_buff = strsCopy(list_buffs, line_start + 4);
char* arg_in = strsPopToken(list_buffs, line_buff, ' ');
obj_setStr(ast, "arg_in", arg_in);
AST_setThisNode(ast, "arg_in", arg_in);
strsPopToken(list_buffs, line_buff, ' ');
char* list_in = strsPopToken(list_buffs, line_buff, ':');
list_in = strsAppend(list_buffs, "iter(", list_in);
list_in = strsAppend(list_buffs, list_in, ")");
list_in = strsCopy(&buffs, list_in);
args_deinit(list_buffs);
obj_setStr(ast, "block", "for");
obj_setStr(ast, "list_in", list_in);
AST_setThisNode(ast, "block", "for");
AST_setThisNode(ast, "list_in", list_in);
if (NULL != block_stack) {
stack_pushStr(block_stack, "for");
}
@ -1632,7 +1692,7 @@ AST* AST_parseLine(char* line, Stack* block_stack) {
if (strIsStartWith(line_start, "else")) {
if ((line_start[4] == ' ') || (line_start[4] == ':')) {
stmt = "";
obj_setStr(ast, "block", "else");
AST_setThisNode(ast, "block", "else");
if (NULL != block_stack) {
stack_pushStr(block_stack, "else");
}
@ -1645,7 +1705,7 @@ AST* AST_parseLine(char* line, Stack* block_stack) {
if (strIsStartWith(line_start, "try")) {
if ((line_start[3] == ' ') || (line_start[3] == ':')) {
stmt = "";
obj_setStr(ast, "block", "try");
AST_setThisNode(ast, "block", "try");
if (NULL != block_stack) {
stack_pushStr(block_stack, "try");
}
@ -1657,7 +1717,7 @@ AST* AST_parseLine(char* line, Stack* block_stack) {
if (strIsStartWith(line_start, "except")) {
if ((line_start[6] == ' ') || (line_start[6] == ':')) {
stmt = "";
obj_setStr(ast, "block", "except");
AST_setThisNode(ast, "block", "except");
if (NULL != block_stack) {
stack_pushStr(block_stack, "except");
}
@ -1667,7 +1727,7 @@ AST* AST_parseLine(char* line, Stack* block_stack) {
#endif
if (strEqu(line_start, "return")) {
obj_setStr(ast, "return", "");
AST_setThisNode(ast, "return", "");
stmt = "";
goto block_matched;
}
@ -1675,13 +1735,13 @@ AST* AST_parseLine(char* line, Stack* block_stack) {
char* lineBuff = strsCopy(&buffs, line_start);
strsPopToken(&buffs, lineBuff, ' ');
stmt = lineBuff;
obj_setStr(ast, "return", "");
AST_setThisNode(ast, "return", "");
goto block_matched;
}
#if PIKA_SYNTEX_EXCEPTION_ENABLE
if (strEqu(line_start, "raise")) {
obj_setStr(ast, "raise", "");
AST_setThisNode(ast, "raise", "");
stmt = "RuntimeError";
goto block_matched;
}
@ -1692,7 +1752,7 @@ AST* AST_parseLine(char* line, Stack* block_stack) {
if (strEqu("", stmt)) {
stmt = "RuntimeError";
}
obj_setStr(ast, "raise", "");
AST_setThisNode(ast, "raise", "");
goto block_matched;
}
#endif
@ -1701,15 +1761,15 @@ AST* AST_parseLine(char* line, Stack* block_stack) {
stmt = "";
char* global_list = line_start + 7;
global_list = strsGetCleanCmd(&buffs, global_list);
obj_setStr(ast, "global", global_list);
AST_setThisNode(ast, "global", global_list);
goto block_matched;
}
if (strIsStartWith(line_start, (char*)"def ")) {
stmt = "";
char* declear = strsCut(&buffs, line_start, ' ', ':');
declear = strsGetCleanCmd(&buffs, declear);
obj_setStr(ast, "block", "def");
obj_setStr(ast, "declear", declear);
AST_setThisNode(ast, "block", "def");
AST_setThisNode(ast, "declear", declear);
if (NULL != block_stack) {
stack_pushStr(block_stack, "def");
}
@ -1719,8 +1779,8 @@ AST* AST_parseLine(char* line, Stack* block_stack) {
stmt = "";
char* declear = strsCut(&buffs, line_start, ' ', ':');
declear = strsGetCleanCmd(&buffs, declear);
obj_setStr(ast, "block", "class");
obj_setStr(ast, "declear", declear);
AST_setThisNode(ast, "block", "class");
AST_setThisNode(ast, "declear", declear);
if (NULL != block_stack) {
stack_pushStr(block_stack, "class");
}
@ -1994,10 +2054,10 @@ char* Parser_parsePyLines(Args* outBuffs,
/* parse single Line to Asm */
single_ASM = Parser_LineToAsm(&buffs, line, &block_stack);
#if PIKA_DEBUG
pika_assert(NULL != single_ASM);
#endif
if(NULL == single_ASM){
#if PIKA_DEBUG
pika_assert(NULL != single_ASM);
#endif
if (NULL == single_ASM) {
out_ASM = NULL;
strsDeinit(&buffs);
goto exit;

View File

@ -46,6 +46,7 @@ enum StmtType {
STMT_bytes,
STMT_number,
STMT_method,
STMT_chain,
STMT_operator,
STMT_import,
STMT_list,

View File

@ -202,12 +202,23 @@ static Arg* VM_instruction_handler_REF(PikaObj* self, VMState* vs, char* data) {
if (strEqu(data, (char*)"RuntimeError")) {
return arg_setInt(NULL, "", PIKA_RES_ERR_RUNTIME_ERROR);
}
/* find in local list first */
Arg* arg = arg_copy(obj_getArg(vs->locals, data));
if (NULL == arg) {
/* find in global list second */
arg = arg_copy(obj_getArg(vs->globals, data));
Arg* arg = NULL;
if (data[0] == '.') {
/* find host from stack */
Arg* host_obj = stack_popArg(&(vs->stack));
if (argType_isObject(arg_getType(host_obj))) {
arg = arg_copy(obj_getArg(arg_getPtr(host_obj), data + 1));
}
arg_deinit(host_obj);
} else {
/* find in local list first */
arg = arg_copy(obj_getArg(vs->locals, data));
if (NULL == arg) {
/* find in global list second */
arg = arg_copy(obj_getArg(vs->globals, data));
}
}
if (NULL == arg) {
VMState_setErrorCode(vs, PIKA_RES_ERR_ARG_NO_FOUND);
__platform_printf("NameError: name '%s' is not defined\r\n", data);
@ -498,6 +509,7 @@ static Arg* VM_instruction_handler_RUN(PikaObj* self, VMState* vs, char* data) {
char* methodPath = data;
PikaObj* method_host_obj;
Arg* method_arg = NULL;
Arg* host_arg = NULL;
char* sys_out;
int arg_num_used = 0;
TryInfo sub_try_info = {.try_state = TRY_STATE_NONE,
@ -518,12 +530,38 @@ static Arg* VM_instruction_handler_RUN(PikaObj* self, VMState* vs, char* data) {
goto exit;
}
/* get method host obj from self */
method_host_obj = obj_getHostObj(self, methodPath);
/* get method host obj from local scope */
if (NULL == method_host_obj) {
method_host_obj = obj_getHostObj(vs->locals, methodPath);
if (methodPath[0] == '.') {
/* get method host obj from stack */
Arg* stack_tmp[PIKA_ARG_NUM_MAX] = {0};
int arg_num = VMState_getInputArgNum(vs);
if (arg_num > PIKA_ARG_NUM_MAX) {
__platform_printf(
"[ERROR] Too many args in RUN instruction, please use bigger "
"#define PIKA_ARG_NUM_MAX\n");
while (1) {
}
}
for (int i = 0; i < arg_num; i++) {
stack_tmp[i] = stack_popArg(&(vs->stack));
}
host_arg = stack_tmp[arg_num - 1];
if (argType_isObject(arg_getType(host_arg))) {
method_host_obj = arg_getPtr(host_arg);
arg_num_used++;
}
/* push back other args to stack */
for (int i = arg_num - 2; i >= 0; i--) {
stack_pushArg(&(vs->stack), stack_tmp[i]);
}
} else {
/* get method host obj from self */
method_host_obj = obj_getHostObj(self, methodPath);
/* get method host obj from local scope */
if (NULL == method_host_obj) {
method_host_obj = obj_getHostObj(vs->locals, methodPath);
}
}
if (NULL == method_host_obj) {
/* error, not found object */
VMState_setErrorCode(vs, PIKA_RES_ERR_ARG_NO_FOUND);
@ -546,8 +584,8 @@ static Arg* VM_instruction_handler_RUN(PikaObj* self, VMState* vs, char* data) {
/* create sub local scope */
sub_locals = New_PikaObj();
/* load args from vmState to sub_local->list */
arg_num_used = VMState_loadArgsFromMethodArg(
vs, method_host_obj, sub_locals->list, method_arg, data, 0);
arg_num_used += VMState_loadArgsFromMethodArg(
vs, method_host_obj, sub_locals->list, method_arg, data, arg_num_used);
/* load args faild */
if (vs->error_code != 0) {
@ -611,6 +649,9 @@ exit:
if (NULL != sub_locals) {
obj_deinit(sub_locals);
}
if (NULL != host_arg) {
arg_deinit(host_arg);
}
strsDeinit(&buffs);
return return_arg;
}

View File

@ -2,4 +2,4 @@
#define PIKA_VERSION_MINOR 9
#define PIKA_VERSION_MICRO 0
#define PIKA_EDIT_TIME "2022/07/04 15:52:45"
#define PIKA_EDIT_TIME "2022/07/05 14:19:29"

View File

@ -74,8 +74,8 @@
/* auto config for syntax level */
#if PIKA_SYNTAX_LEVEL == PIKA_SYNTAX_LEVEL_MINIMAL
#ifndef PIKA_SYNTEX_ITEM_SLICE_ENABLE
#define PIKA_SYNTEX_ITEM_SLICE_ENABLE 0
#ifndef PIKA_SYNTEX_SLICE_ENABLE
#define PIKA_SYNTEX_SLICE_ENABLE 0
#endif
#ifndef PIKA_BUILTIN_LIST_ENABLE
@ -86,8 +86,8 @@
#define PIKA_BUILTIN_DICT_ENABLE 0
#endif
#ifndef PIKA_SYNTEX_ITEM_FORMAT_ENABLE
#define PIKA_SYNTEX_ITEM_FORMAT_ENABLE 0
#ifndef PIKA_SYNTEX_FORMAT_ENABLE
#define PIKA_SYNTEX_FORMAT_ENABLE 0
#endif
#ifndef PIKA_STD_DEVICE_UNIX_TIME_ENABLE
@ -107,8 +107,8 @@
#endif
#elif PIKA_SYNTAX_LEVEL == PIKA_SYNTAX_LEVEL_MAXIMAL
#ifndef PIKA_SYNTEX_ITEM_SLICE_ENABLE
#define PIKA_SYNTEX_ITEM_SLICE_ENABLE 1
#ifndef PIKA_SYNTEX_SLICE_ENABLE
#define PIKA_SYNTEX_SLICE_ENABLE 1
#endif
#ifndef PIKA_BUILTIN_LIST_ENABLE
@ -119,8 +119,8 @@
#define PIKA_BUILTIN_DICT_ENABLE 1
#endif
#ifndef PIKA_SYNTEX_ITEM_FORMAT_ENABLE
#define PIKA_SYNTEX_ITEM_FORMAT_ENABLE 1
#ifndef PIKA_SYNTEX_FORMAT_ENABLE
#define PIKA_SYNTEX_FORMAT_ENABLE 1
#endif
#ifndef PIKA_STD_DEVICE_UNIX_TIME_ENABLE
@ -159,7 +159,7 @@
#endif
#ifndef PIKA_PATH_BUFF_SIZE
#define PIKA_PATH_BUFF_SIZE 64
#define PIKA_PATH_BUFF_SIZE 96
#endif
#ifndef PIKA_BYTES_DEFAULT_SIZE
@ -190,12 +190,12 @@
#define PIKA_INIT_STRING_ENABLE 1
#endif
#ifndef PIKA_SYNTEX_ITEM_SLICE_ENABLE
#define PIKA_SYNTEX_ITEM_SLICE_ENABLE 1
#ifndef PIKA_SYNTEX_SLICE_ENABLE
#define PIKA_SYNTEX_SLICE_ENABLE 1
#endif
#ifndef PIKA_SYNTEX_ITEM_FORMAT_ENABLE
#define PIKA_SYNTEX_ITEM_FORMAT_ENABLE 1
#ifndef PIKA_SYNTEX_FORMAT_ENABLE
#define PIKA_SYNTEX_FORMAT_ENABLE 1
#endif
#ifndef PIKA_SYNTEX_EXCEPTION_ENABLE
@ -238,6 +238,10 @@
#define PIKA_DEBUG_ENABLE 0
#endif
#ifndef PIKA_ARG_NUM_MAX
#define PIKA_ARG_NUM_MAX 16
#endif
/* configuration validation */
#endif