operator is tested ok

This commit is contained in:
lyon 2021-11-26 22:18:01 +08:00
parent d0c147c4af
commit 1d64eb3df8
3 changed files with 174 additions and 70 deletions

View File

@ -1,6 +1,6 @@
#include "dataMemory.h"
#define use_const_pool 1
#define use_const_pool 0
#define use_dynamic_pool 0
#define pika_aline 8

View File

@ -862,9 +862,35 @@ TEST(lexser, operator_not) {
/* run */
char* tokens = Lexer_getTokens(buffs, (char*)"not not not ");
char* printTokens = Lexer_printTokens(buffs, tokens);
/* assert */
EXPECT_STREQ(printTokens, "<opt>not<opt>not<opt>not");
EXPECT_STREQ(printTokens, "[opt]not[opt]not[opt]not");
/* deinit */
args_deinit(buffs);
EXPECT_EQ(pikaMemNow(), 0);
}
TEST(lexser, operator_all) {
/* init */
pikaMemInfo.heapUsedMax = 0;
Args* buffs = New_strBuff();
/* run */
char* tokens = Lexer_getTokens(buffs, (char*)
"not or and "
"+ += - -="
"* ** *= **="
"/ // /= //="
"% %= = == !="
"> >= >>"
"< <= <<"
);
char* printTokens = Lexer_printTokens(buffs, tokens);
printf((char*)"%s\n", printTokens);
/* 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]<<");
/* deinit */
args_deinit(buffs);

View File

@ -181,7 +181,7 @@ char* Lexer_printTokens(Args* outBuffs, char* tokens) {
for (int i = 0; i < tokenSize; i++) {
char* token = strsPopToken(buffs, tokens, ' ');
if (token[0] == TOKEN_operator) {
printOut = strsAppend(buffs, printOut, "<opt>");
printOut = strsAppend(buffs, printOut, "[opt]");
printOut = strsAppend(buffs, printOut, token + 1);
}
}
@ -229,6 +229,150 @@ char* Lexer_getTokens(Args* outBuffs, char* stmt) {
if (i + 3 < size) {
c3 = stmt[i + 3];
}
// +
if ('+' == c0) {
/* += */
if ('=' == c1) {
tokens = Lexer_setToken(buffs, tokens, TOKEN_operator, "+=");
i = i + 1;
continue;
}
/* + */
tokens = Lexer_setToken(buffs, tokens, TOKEN_operator, "+");
continue;
}
// -
if ('-' == c0) {
/* -= */
if ('=' == c1) {
tokens = Lexer_setToken(buffs, tokens, TOKEN_operator, "-=");
i = i + 1;
continue;
}
/* - */
tokens = Lexer_setToken(buffs, tokens, TOKEN_operator, "-");
continue;
}
// *
if ('*' == c0) {
// **=
if (('*' == c1) && ('=' == c2)) {
tokens = Lexer_setToken(buffs, tokens, TOKEN_operator, "**=");
i = i + 2;
continue;
}
// *=
if ('=' == c1) {
tokens = Lexer_setToken(buffs, tokens, TOKEN_operator, "*=");
i = i + 1;
continue;
}
if ('*' == c1) {
tokens = Lexer_setToken(buffs, tokens, TOKEN_operator, "**");
i = i + 1;
continue;
}
// *
tokens = Lexer_setToken(buffs, tokens, TOKEN_operator, "*");
continue;
}
// =
if ('=' == c0) {
/* == */
if ('=' == c1) {
tokens = Lexer_setToken(buffs, tokens, TOKEN_operator, "==");
i = i + 1;
continue;
}
/* = */
tokens = Lexer_setToken(buffs, tokens, TOKEN_operator, "=");
continue;
}
// >
if ('>' == c0) {
// >=
if ('=' == c1) {
tokens = Lexer_setToken(buffs, tokens, TOKEN_operator, ">=");
i = i + 1;
continue;
}
// >>
if ('>' == c1) {
tokens = Lexer_setToken(buffs, tokens, TOKEN_operator, ">>");
i = i + 1;
continue;
}
// >
tokens = Lexer_setToken(buffs, tokens, TOKEN_operator, ">");
continue;
}
// <
if ('<' == c0) {
// <=
if ('=' == c1) {
tokens = Lexer_setToken(buffs, tokens, TOKEN_operator, "<=");
i = i + 1;
continue;
}
// <<
if ('<' == c1) {
tokens = Lexer_setToken(buffs, tokens, TOKEN_operator, "<<");
i = i + 1;
continue;
}
// <
tokens = Lexer_setToken(buffs, tokens, TOKEN_operator, "<");
continue;
}
// /
if ('/' == c0) {
// //=
if (('/' == c1) && ('=' == c2)) {
tokens = Lexer_setToken(buffs, tokens, TOKEN_operator, "//=");
i = i + 2;
continue;
}
// /=
if ('=' == c1) {
tokens = Lexer_setToken(buffs, tokens, TOKEN_operator, "/=");
i = i + 1;
continue;
}
if ('/' == c1) {
tokens = Lexer_setToken(buffs, tokens, TOKEN_operator, "//");
i = i + 1;
continue;
}
// /
tokens = Lexer_setToken(buffs, tokens, TOKEN_operator, "/");
continue;
}
// %
if ('%' == c0) {
/* %= */
if ('=' == c1) {
tokens = Lexer_setToken(buffs, tokens, TOKEN_operator, "%=");
i = i + 1;
continue;
}
/* % */
tokens = Lexer_setToken(buffs, tokens, TOKEN_operator, "%");
continue;
}
// !=
if ('!' == c0) {
/* != */
if ('=' == c1) {
tokens = Lexer_setToken(buffs, tokens, TOKEN_operator, "!=");
i = i + 1;
continue;
}
}
/* not */
if ('n' == c0) {
if (('o' == c1) && ('t' == c2) && (' ' == c3)) {
@ -253,72 +397,6 @@ char* Lexer_getTokens(Args* outBuffs, char* stmt) {
continue;
}
}
if ('+' == c0) {
/* += */
if ('=' == c1) {
tokens = Lexer_setToken(buffs, tokens, TOKEN_operator, "+=");
i = i + 2;
continue;
}
/* + */
tokens = Lexer_setToken(buffs, tokens, TOKEN_operator, "+");
continue;
}
if ('-' == c0) {
/* -= */
if ('=' == c1) {
tokens = Lexer_setToken(buffs, tokens, TOKEN_operator, "-=");
i = i + 2;
continue;
}
/* - */
tokens = Lexer_setToken(buffs, tokens, TOKEN_operator, "-");
continue;
}
if ('*' == c0) {
// **=
if (('*' == c1) && ('=' == c2)) {
tokens = Lexer_setToken(buffs, tokens, TOKEN_operator, "**=");
i = i + 3;
continue;
}
// *=
if ('=' == c1) {
tokens = Lexer_setToken(buffs, tokens, TOKEN_operator, "*=");
i = i + 2;
continue;
}
if ('*' == c1) {
tokens = Lexer_setToken(buffs, tokens, TOKEN_operator, "**");
i = i + 2;
continue;
}
// *
tokens = Lexer_setToken(buffs, tokens, TOKEN_operator, "*");
continue;
}
if ('/' == c0) {
// //=
if (('/' == c1) && ('=' == c2)) {
tokens = Lexer_setToken(buffs, tokens, TOKEN_operator, "//=");
i = i + 3;
continue;
}
// /=
if ('=' == c1) {
tokens = Lexer_setToken(buffs, tokens, TOKEN_operator, "/=");
i = i + 2;
continue;
}
if ('/' == c1) {
tokens = Lexer_setToken(buffs, tokens, TOKEN_operator, "//");
i = i + 2;
continue;
}
// /
tokens = Lexer_setToken(buffs, tokens, TOKEN_operator, "/");
continue;
}
}
tokens = strsCopy(outBuffs, tokens);
args_deinit(buffs);