support XXX is not YYY

This commit is contained in:
lyon 2023-04-30 21:40:27 +08:00
parent f5b0bf5b5c
commit 72ec29fbe0
6 changed files with 59 additions and 17 deletions

View File

@ -20,7 +20,7 @@
// "--gtest_filter=eventloop.test1" // "--gtest_filter=eventloop.test1"
// "--gtest_filter=parser.tuple_single" // "--gtest_filter=parser.tuple_single"
// "--gtest_filter=parser.*" // "--gtest_filter=parser.*"
"--gtest_filter=parser.default_tuple" "--gtest_filter=VM.is"
], ],
"stopAtEntry": false, "stopAtEntry": false,
"cwd": "${workspaceFolder}", "cwd": "${workspaceFolder}",

View File

@ -1591,32 +1591,36 @@ static void _AST_parse_slice(AST* ast, Args* buffs, char* stmt) {
} }
} }
char* Suger_not_in(Args* out_buffs, char* line) { #include <string.h>
#if PIKA_NANO_ENABLE #include "pikaScript.h"
return line;
#endif char* _Suger_process(Args* out_buffs,
char* line,
char* token1,
char* token2,
char* format) {
char* ret = line; char* ret = line;
char* stmt1 = ""; char* stmt1 = "";
char* stmt2 = ""; char* stmt2 = "";
PIKA_BOOL got_not_in = PIKA_FALSE; PIKA_BOOL got_tokens = PIKA_FALSE;
PIKA_BOOL skip = PIKA_FALSE; PIKA_BOOL skip = PIKA_FALSE;
Args buffs = {0}; Args buffs = {0};
if (1 != Cursor_count(line, TOKEN_operator, " not ")) {
if (1 != Cursor_count(line, TOKEN_operator, token1)) {
ret = line; ret = line;
goto __exit; goto __exit;
} }
if (1 != Cursor_count(line, TOKEN_operator, " in ")) { if (1 != Cursor_count(line, TOKEN_operator, token2)) {
ret = line; ret = line;
goto __exit; goto __exit;
} }
/* stmt1 not in stmt2 => not stmt1 in stmt2 */
Cursor_forEach(cs, line) { Cursor_forEach(cs, line) {
Cursor_iterStart(&cs); Cursor_iterStart(&cs);
if (!got_not_in) { if (!got_tokens) {
if (strEqu(cs.token1.pyload, " not ") && if (strEqu(cs.token1.pyload, token1) &&
strEqu(cs.token2.pyload, " in ")) { strEqu(cs.token2.pyload, token2)) {
got_not_in = PIKA_TRUE; got_tokens = PIKA_TRUE;
Cursor_iterEnd(&cs); Cursor_iterEnd(&cs);
continue; continue;
} }
@ -1632,18 +1636,29 @@ char* Suger_not_in(Args* out_buffs, char* line) {
Cursor_iterEnd(&cs); Cursor_iterEnd(&cs);
} }
Cursor_deinit(&cs); Cursor_deinit(&cs);
if (!got_not_in) {
if (!got_tokens) {
ret = line; ret = line;
goto __exit; goto __exit;
} }
ret = strsFormat(out_buffs, strGetSize(line) + 3, " not %s in %s", stmt1,
stmt2); ret = strsFormat(out_buffs,
goto __exit; strGetSize(line) + strlen(token1) + strlen(token2), format,
stmt1, stmt2);
__exit: __exit:
strsDeinit(&buffs); strsDeinit(&buffs);
return ret; return ret;
} }
char* Suger_not_in(Args* out_buffs, char* line) {
return _Suger_process(out_buffs, line, " not ", " in ", " not %s in %s");
}
char* Suger_is_not(Args* out_buffs, char* line) {
return _Suger_process(out_buffs, line, " is ", " not ", " not %s is %s");
}
AST* AST_parseStmt(AST* ast, char* stmt) { AST* AST_parseStmt(AST* ast, char* stmt) {
Args buffs = {0}; Args buffs = {0};
char* assignment = Cursor_splitCollect(&buffs, stmt, "(", 0); char* assignment = Cursor_splitCollect(&buffs, stmt, "(", 0);
@ -1713,6 +1728,7 @@ AST* AST_parseStmt(AST* ast, char* stmt) {
/* solve operator stmt */ /* solve operator stmt */
if (STMT_operator == stmtType) { if (STMT_operator == stmtType) {
right = Suger_not_in(&buffs, right); right = Suger_not_in(&buffs, right);
right = Suger_is_not(&buffs, right);
char* rightWithoutSubStmt = _remove_sub_stmt(&buffs, right); char* rightWithoutSubStmt = _remove_sub_stmt(&buffs, right);
char* operator= Lexer_getOperator(&buffs, rightWithoutSubStmt); char* operator= Lexer_getOperator(&buffs, rightWithoutSubStmt);
if (NULL == operator) { if (NULL == operator) {

View File

@ -2826,6 +2826,11 @@ static Arg* VM_instruction_handler_OPT(PikaObj* self,
} }
goto exit; goto exit;
} }
if ((op.t1 != op.t2) && (op.t1 != ARG_TYPE_NONE) &&
(op.t2 != ARG_TYPE_NONE)) {
op.res = arg_setInt(op.res, "", 0);
goto exit;
}
#endif #endif
op.opt = "=="; op.opt = "==";
_OPT_EQU(&op); _OPT_EQU(&op);

View File

@ -2848,6 +2848,8 @@ TEST_RUN_SINGLE_FILE_PASS(datastruct,
TEST_RUN_LINES_EXCEPT_OUTPUT(vm, single_tuple, "(1,)", "(1,)\r\n") TEST_RUN_LINES_EXCEPT_OUTPUT(vm, single_tuple, "(1,)", "(1,)\r\n")
TEST_RUN_LINES_EXCEPT_OUTPUT(vm, single_tuple_str, "('test',)", "('test',)\r\n") TEST_RUN_LINES_EXCEPT_OUTPUT(vm, single_tuple_str, "('test',)", "('test',)\r\n")
TEST_RUN_SINGLE_FILE_PASS(vm, is_not, "test/python/builtin/is_not.py")
#endif #endif
TEST_END TEST_END

View File

@ -5614,6 +5614,15 @@ TEST_LINES2ASM(default_tuple,
"0 RET \n" "0 RET \n"
"B0\n") "B0\n")
TEST_LINES2ASM(is_not,
"A is not None",
"B0\n"
"2 REF A\n"
"2 REF None\n"
"1 OPT is \n"
"0 OPT not \n"
"B0\n")
#endif #endif
TEST_END TEST_END

View File

@ -0,0 +1,10 @@
assert (1 is not 1) == False
assert (1 is not 2) == True
assert (1 is not None) == True
assert (None is not None) == False
assert (None is not 1) == True
assert (1 is not 1.0) == True
assert (1.0 is not 1) == True
assert (1.0 is not 1.0) == False
print("PASS")