GDScript: Add `is not` operator

This commit is contained in:
Danil Alexeev 2024-02-04 15:40:19 +03:00
parent b4e2a24c1f
commit 2bf25954b4
No known key found for this signature in database
GPG Key ID: 124453E157DA8DC7
3 changed files with 44 additions and 1 deletions

View File

@ -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;

View File

@ -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)

View File

@ -0,0 +1,6 @@
GDTEST_OK
true false
false true
true false
true false
false true