mirror of
https://gitee.com/Lyon1998/pikapython.git
synced 2025-01-15 17:02:53 +08:00
use lexer to get operator
This commit is contained in:
parent
fdf9875b3f
commit
825344910f
2
port/linux/.vscode/launch.json
vendored
2
port/linux/.vscode/launch.json
vendored
@ -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*",
|
||||
|
@ -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);
|
||||
}
|
@ -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 =
|
||||
|
Loading…
x
Reference in New Issue
Block a user