Skip to content

Commit

Permalink
GDScript: Add is not operator
Browse files Browse the repository at this point in the history
  • Loading branch information
dalexeev committed Feb 4, 2024
1 parent b4e2a24 commit 2bf2595
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
28 changes: 27 additions & 1 deletion modules/gdscript/gdscript_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand Down
11 changes: 11 additions & 0 deletions modules/gdscript/tests/scripts/parser/features/is_not_operator.gd
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
GDTEST_OK
true false
false true
true false
true false
false true

0 comments on commit 2bf2595

Please sign in to comment.