This commit is contained in:
lyon 2021-11-27 11:58:15 +08:00
parent dd2f4fe27f
commit 7c5b460816
2 changed files with 39 additions and 20 deletions

View File

@ -854,20 +854,37 @@ TEST(parser, comp_signed_num) {
EXPECT_EQ(pikaMemNow(), 0); EXPECT_EQ(pikaMemNow(), 0);
} }
TEST(lexser, symbol_add) {
/* init */
pikaMemInfo.heapUsedMax = 0;
Args* buffs = New_strBuff();
/* run */
char* tokens = Lexer_getTokens(buffs, (char*)" res = add(1,2)");
char* printTokens = Lexer_printTokens(buffs, tokens);
printf((char*)"%s\n", printTokens);
/* assert */
EXPECT_STREQ(printTokens,
"{sym}res{opt}={sym}add{dvd}({lit}1{dvd},{lit}2{dvd})");
/* deinit */
args_deinit(buffs);
EXPECT_EQ(pikaMemNow(), 0);
}
TEST(lexser, symbol_1) { TEST(lexser, symbol_1) {
/* init */ /* init */
pikaMemInfo.heapUsedMax = 0; pikaMemInfo.heapUsedMax = 0;
Args* buffs = New_strBuff(); Args* buffs = New_strBuff();
/* run */ /* run */
char* tokens = Lexer_getTokens(buffs, (char*) char* tokens = Lexer_getTokens(buffs, (char*)"a(");
"a("
);
char* printTokens = Lexer_printTokens(buffs, tokens); char* printTokens = Lexer_printTokens(buffs, tokens);
printf((char*)"%s\n", printTokens); printf((char*)"%s\n", printTokens);
/* assert */ /* assert */
EXPECT_STREQ(printTokens, "{sym}a{bkt}("); EXPECT_STREQ(printTokens, "{sym}a{dvd}(");
/* deinit */ /* deinit */
args_deinit(buffs); args_deinit(buffs);
@ -898,9 +915,7 @@ TEST(lexser, symbol_Nag) {
Args* buffs = New_strBuff(); Args* buffs = New_strBuff();
/* run */ /* run */
char* tokens = Lexer_getTokens(buffs, (char*) char* tokens = Lexer_getTokens(buffs, (char*)"-10-20");
"-10-20"
);
char* printTokens = Lexer_printTokens(buffs, tokens); char* printTokens = Lexer_printTokens(buffs, tokens);
printf((char*)"%s\n", printTokens); printf((char*)"%s\n", printTokens);
@ -932,7 +947,11 @@ TEST(lexser, operator_all) {
printf((char*)"%s\n", printTokens); printf((char*)"%s\n", printTokens);
/* assert */ /* assert */
EXPECT_STREQ(printTokens, "{opt}not{opt}or{opt}and{opt}+{opt}+={opt}-{opt}-={opt}*{opt}**{opt}*={opt}**={opt}/{opt}//{opt}/={opt}//={opt}%{opt}%={opt}={opt}=={opt}!={opt}>{opt}>={opt}>>{opt}<{opt}<={opt}<<{opt}&{opt}|{opt}^{opt}~"); EXPECT_STREQ(printTokens,
"{opt}not{opt}or{opt}and{opt}+{opt}+={opt}-{opt}-={opt}*{opt}*"
"*{opt}*={opt}**={opt}/{opt}//{opt}/={opt}//"
"={opt}%{opt}%={opt}={opt}=={opt}!={opt}>{opt}>={opt}>>{opt}<{"
"opt}<={opt}<<{opt}&{opt}|{opt}^{opt}~");
/* deinit */ /* deinit */
args_deinit(buffs); args_deinit(buffs);
@ -945,14 +964,14 @@ TEST(lexser, symbol_2) {
Args* buffs = New_strBuff(); Args* buffs = New_strBuff();
/* run */ /* run */
char* tokens = Lexer_getTokens(buffs, (char*) char* tokens = Lexer_getTokens(buffs, (char*)"a+b-c(25**=ek)!=-28");
"a+b-c(25**=ek)!=-28"
);
char* printTokens = Lexer_printTokens(buffs, tokens); char* printTokens = Lexer_printTokens(buffs, tokens);
printf((char*)"%s\n", printTokens); printf((char*)"%s\n", printTokens);
/* assert */ /* assert */
EXPECT_STREQ(printTokens, "{sym}a{opt}+{sym}b{opt}-{sym}c{bkt}({lit}25{opt}**={sym}ek{bkt}){opt}!={lit}-28"); EXPECT_STREQ(printTokens,
"{sym}a{opt}+{sym}b{opt}-{sym}c{dvd}({lit}25{opt}**={sym}ek{"
"dvd}){opt}!={lit}-28");
/* deinit */ /* deinit */
args_deinit(buffs); args_deinit(buffs);

View File

@ -77,7 +77,7 @@ enum TokenType {
TOKEN_symbol, TOKEN_symbol,
TOKEN_keyword, TOKEN_keyword,
TOKEN_operator, TOKEN_operator,
TOKEN_brancket, TOKEN_devider,
TOKEN_literal, TOKEN_literal,
}; };
@ -183,8 +183,8 @@ char* Lexer_printTokens(Args* outBuffs, char* tokens) {
printOut = strsAppend(buffs, printOut, "{opt}"); printOut = strsAppend(buffs, printOut, "{opt}");
printOut = strsAppend(buffs, printOut, token + 1); printOut = strsAppend(buffs, printOut, token + 1);
} }
if (token[0] == TOKEN_brancket) { if (token[0] == TOKEN_devider) {
printOut = strsAppend(buffs, printOut, "{bkt}"); printOut = strsAppend(buffs, printOut, "{dvd}");
printOut = strsAppend(buffs, printOut, token + 1); printOut = strsAppend(buffs, printOut, token + 1);
} }
if (token[0] == TOKEN_symbol) { if (token[0] == TOKEN_symbol) {
@ -274,13 +274,13 @@ char* Lexer_getTokens(Args* outBuffs, char* stmt) {
if (-1 == symbol_start_index) { if (-1 == symbol_start_index) {
symbol_start_index = i; symbol_start_index = i;
} }
/* match brancket*/ /* match devider*/
if (('(' == c0) || (')' == c0)) { if (('(' == c0) || (')' == c0) || (',' == c0)) {
tokens_arg = tokens_arg =
Lexer_setSymbel(tokens_arg, stmt, i, &symbol_start_index); Lexer_setSymbel(tokens_arg, stmt, i, &symbol_start_index);
char content[2] = {0}; char content[2] = {0};
content[0] = c0; content[0] = c0;
tokens_arg = Lexer_setToken(tokens_arg, TOKEN_brancket, content); tokens_arg = Lexer_setToken(tokens_arg, TOKEN_devider, content);
continue; continue;
} }
/* match operator */ /* match operator */
@ -400,7 +400,7 @@ char* Lexer_getTokens(Args* outBuffs, char* stmt) {
} }
} }
/* skip spaces */ /* skip spaces */
if (' ' == c0) { if ((' ' == c0) && (i == symbol_start_index)) {
symbol_start_index++; symbol_start_index++;
} }
if (i == size - 1) { if (i == size - 1) {