From d8f35fea3650a34511ad9d3077dfb6968fa27ae4 Mon Sep 17 00:00:00 2001 From: lyon1998 Date: Sat, 2 Apr 2022 18:58:20 +0800 Subject: [PATCH] use lexer_forEachToken in parser --- src/PikaParser.c | 28 ++++++++-------------------- src/PikaParser.h | 12 +++++++++--- 2 files changed, 17 insertions(+), 23 deletions(-) diff --git a/src/PikaParser.c b/src/PikaParser.c index a85159f41..eceaa3a87 100644 --- a/src/PikaParser.c +++ b/src/PikaParser.c @@ -165,16 +165,12 @@ static enum StmtType Lexer_matchStmtType(char* right) { enum StmtType stmtType = STMT_none; char* rightWithoutSubStmt = strsDeleteBetween(&buffs, right, '(', ')'); - struct ParserState ps; - ParserState_init(&ps); - ParserState_parse(&ps, rightWithoutSubStmt); - uint8_t is_get_operator = 0; uint8_t is_get_method = 0; uint8_t is_get_string = 0; uint8_t is_get_number = 0; uint8_t is_get_symbol = 0; - ParserState_forEach(ps) { + Lexer_forEachToken(ps, rightWithoutSubStmt) { ParserState_iterStart(&ps); /* collect type */ if (ps.token1.type == TOKEN_operator) { @@ -459,8 +455,8 @@ char* Lexer_getTokens(Args* outBuffs, char* stmt) { } /* single operator */ - /* - +, -, *, ... / + /* + +, -, *, ... / */ char content[2] = {0}; content[0] = c0; @@ -651,18 +647,15 @@ char* Parser_solveBranckets(Args* outBuffs, Arg* right_arg = arg_setStr(NULL, "", ""); uint8_t is_in_brancket = 0; args_setStr(&buffs, "index", ""); - /* init parserState */ - struct ParserState ps; - ParserState_init(&ps); /* exit when NULL */ if (NULL == content) { arg_deinit(right_arg); right_arg = arg_setStr(right_arg, "", stmt); goto exit; } - ParserState_parse(&ps, content); + char* tokens = Lexer_getTokens(&buffs, content); /* exit when no '[' ']' */ - if (!Lexer_isContain(ps.tokens, TOKEN_devider, "[")) { + if (!Lexer_isContain(tokens, TOKEN_devider, "[")) { /* not contain '[', return origin */ arg_deinit(right_arg); if (strEqu(mode, "right")) { @@ -672,7 +665,7 @@ char* Parser_solveBranckets(Args* outBuffs, } goto exit; } - ParserState_forEach(ps) { + Lexer_forEachToken(ps, content) { ParserState_iterStart(&ps); /* matched [] */ /* found '[' */ @@ -726,11 +719,10 @@ char* Parser_solveBranckets(Args* outBuffs, } ParserState_iterEnd(&ps); } - + ParserState_deinit(&ps); exit: /* clean and return */ content = strsCopy(outBuffs, arg_getStr(right_arg)); - ParserState_deinit(&ps); arg_deinit(right_arg); strsDeinit(&buffs); return content; @@ -885,16 +877,12 @@ AST* AST_parseStmt(AST* ast, char* stmt) { char* subStmts = strsCut(&buffs, right, '(', ')'); /* add ',' at the end */ subStmts = strsAppend(&buffs, subStmts, ","); - struct ParserState ps; - /* init parserStage */ - ParserState_init(&ps); - ParserState_parse(&ps, subStmts); /* init process values */ Arg* subStmt = arg_setStr(NULL, "", ""); uint8_t is_in_brankets = 0; /* start iteration */ char* subStmt_str = NULL; - ParserState_forEach(ps) { + Lexer_forEachToken(ps, subStmts) { ParserState_iterStart(&ps); /* parse process */ if (strEqu(ps.token1.pyload, "(")) { diff --git a/src/PikaParser.h b/src/PikaParser.h index e39640f7a..0d01a350f 100644 --- a/src/PikaParser.h +++ b/src/PikaParser.h @@ -79,8 +79,14 @@ ByteCodeFrame* byteCodeFrame_appendFromAsm(ByteCodeFrame* bf, char* pikaAsm); int bytecodeFrame_fromMultiLine(ByteCodeFrame* bytecode_frame, char* python_lines); void Parser_compilePyToBytecodeArray(char* lines); -#define ParserState_forEach(ps) \ - ParserState_beforeIter(&ps); \ - for (int i = 0; i < ps.length; i++) +#define ParserState_forEach(parseState) \ + ParserState_beforeIter(&parseState); \ + for (int i = 0; i < parseState.length; i++) +#define Lexer_forEachToken(parseState, tokens) \ + struct ParserState ps; \ + /* init parserStage */ \ + ParserState_init(&parseState); \ + ParserState_parse(&parseState, tokens); \ + ParserState_forEach(parseState) #endif