diff options
3 files changed, 44 insertions, 1 deletions
diff --git a/modules/gdscript/gdscript_parser.cpp b/modules/gdscript/gdscript_parser.cpp index 2839d7b123..8293990dc6 100644 --- a/modules/gdscript/gdscript_parser.cpp +++ b/modules/gdscript/gdscript_parser.cpp @@ -3285,6 +3285,19 @@ GDScriptParser::ExpressionNode *GDScriptParser::parse_lambda(ExpressionNode *p_p } GDScriptParser::ExpressionNode *GDScriptParser::parse_type_test(ExpressionNode *p_previous_operand, bool p_can_assign) { + // x is not int + // ^ ^^^ ExpressionNode, TypeNode + // ^^^^^^^^^^^^ TypeTestNode + // ^^^^^^^^^^^^ UnaryOpNode + UnaryOpNode *not_node = nullptr; + if (match(GDScriptTokenizer::Token::NOT)) { + not_node = alloc_node<UnaryOpNode>(); + not_node->operation = UnaryOpNode::OP_LOGIC_NOT; + not_node->variant_op = Variant::OP_NOT; + reset_extents(not_node, p_previous_operand); + update_extents(not_node); + } + TypeTestNode *type_test = alloc_node<TypeTestNode>(); reset_extents(type_test, p_previous_operand); update_extents(type_test); @@ -3293,8 +3306,21 @@ GDScriptParser::ExpressionNode *GDScriptParser::parse_type_test(ExpressionNode * type_test->test_type = parse_type(); complete_extents(type_test); + if (not_node != nullptr) { + not_node->operand = type_test; + complete_extents(not_node); + } + if (type_test->test_type == nullptr) { - push_error(R"(Expected type specifier after "is".)"); + if (not_node == nullptr) { + push_error(R"(Expected type specifier after "is".)"); + } else { + push_error(R"(Expected type specifier after "is not".)"); + } + } + + if (not_node != nullptr) { + return not_node; } return type_test; diff --git a/modules/gdscript/tests/scripts/parser/features/is_not_operator.gd b/modules/gdscript/tests/scripts/parser/features/is_not_operator.gd new file mode 100644 index 0000000000..b744e6170b --- /dev/null +++ b/modules/gdscript/tests/scripts/parser/features/is_not_operator.gd @@ -0,0 +1,11 @@ +func test(): + var i: Variant = 123 + var s: Variant = "str" + prints(i is int, i is not int) + prints(s is int, s is not int) + + var a: Variant = false + var b: Variant = true + prints(a == b is int, a == b is not int) + prints(a == (b is int), a == (b is not int)) + prints((a == b) is int, (a == b) is not int) diff --git a/modules/gdscript/tests/scripts/parser/features/is_not_operator.out b/modules/gdscript/tests/scripts/parser/features/is_not_operator.out new file mode 100644 index 0000000000..f0535f9c83 --- /dev/null +++ b/modules/gdscript/tests/scripts/parser/features/is_not_operator.out @@ -0,0 +1,6 @@ +GDTEST_OK +true false +false true +true false +true false +false true |
