From 015d36d18bb3f9fe21452a470aac2c3a3597b92f Mon Sep 17 00:00:00 2001 From: Andreas Haas Date: Fri, 26 May 2017 19:45:39 +0200 Subject: [PATCH] GDScript: Use "is" keyword for type checking. Replaces the `extends` keyword with `is` in the context of testing for type compatibility. `extends` is still used for declaring class inheritance. Example: ```gdscript extends Node2D func _input(ev): if ev is InputEventKey: print("yay, key event") ``` --- modules/gdscript/gd_compiler.cpp | 2 +- modules/gdscript/gd_function.cpp | 6 +++--- modules/gdscript/gd_parser.cpp | 6 +++--- modules/gdscript/gd_parser.h | 2 +- modules/gdscript/gd_script.cpp | 1 + modules/gdscript/gd_tokenizer.cpp | 2 ++ modules/gdscript/gd_tokenizer.h | 1 + 7 files changed, 12 insertions(+), 8 deletions(-) diff --git a/modules/gdscript/gd_compiler.cpp b/modules/gdscript/gd_compiler.cpp index 12b43d60601..d4ede4cb17c 100644 --- a/modules/gdscript/gd_compiler.cpp +++ b/modules/gdscript/gd_compiler.cpp @@ -1014,7 +1014,7 @@ int GDCompiler::_parse_expression(CodeGen &codegen, const GDParser::Node *p_expr } } break; - case GDParser::OperatorNode::OP_EXTENDS: { + case GDParser::OperatorNode::OP_IS: { ERR_FAIL_COND_V(on->arguments.size() != 2, false); diff --git a/modules/gdscript/gd_function.cpp b/modules/gdscript/gd_function.cpp index fb32d23ad58..e8a8e209277 100644 --- a/modules/gdscript/gd_function.cpp +++ b/modules/gdscript/gd_function.cpp @@ -358,12 +358,12 @@ Variant GDFunction::call(GDInstance *p_instance, const Variant **p_args, int p_a if (a->get_type() != Variant::OBJECT || a->operator Object *() == NULL) { - err_text = "Left operand of 'extends' is not an instance of anything."; + err_text = "Left operand of 'is' is not an instance of anything."; break; } if (b->get_type() != Variant::OBJECT || b->operator Object *() == NULL) { - err_text = "Right operand of 'extends' is not a class."; + err_text = "Right operand of 'is' is not a class."; break; } #endif @@ -401,7 +401,7 @@ Variant GDFunction::call(GDInstance *p_instance, const Variant **p_args, int p_a if (!nc) { - err_text = "Right operand of 'extends' is not a class (type: '" + obj_B->get_class() + "')."; + err_text = "Right operand of 'is' is not a class (type: '" + obj_B->get_class() + "')."; break; } diff --git a/modules/gdscript/gd_parser.cpp b/modules/gdscript/gd_parser.cpp index 4ae62eb1d07..d64cd86de61 100644 --- a/modules/gdscript/gd_parser.cpp +++ b/modules/gdscript/gd_parser.cpp @@ -1077,7 +1077,7 @@ GDParser::Node *GDParser::_parse_expression(Node *p_parent, bool p_static, bool case GDTokenizer::TK_OP_BIT_AND: op = OperatorNode::OP_BIT_AND; break; case GDTokenizer::TK_OP_BIT_OR: op = OperatorNode::OP_BIT_OR; break; case GDTokenizer::TK_OP_BIT_XOR: op = OperatorNode::OP_BIT_XOR; break; - case GDTokenizer::TK_PR_EXTENDS: op = OperatorNode::OP_EXTENDS; break; + case GDTokenizer::TK_PR_IS: op = OperatorNode::OP_IS; break; case GDTokenizer::TK_CF_IF: op = OperatorNode::OP_TERNARY_IF; break; case GDTokenizer::TK_CF_ELSE: op = OperatorNode::OP_TERNARY_ELSE; break; default: valid = false; break; @@ -1117,7 +1117,7 @@ GDParser::Node *GDParser::_parse_expression(Node *p_parent, bool p_static, bool switch (expression[i].op) { - case OperatorNode::OP_EXTENDS: + case OperatorNode::OP_IS: priority = -1; break; //before anything @@ -1420,7 +1420,7 @@ GDParser::Node *GDParser::_reduce_expression(Node *p_node, bool p_to_const) { } } - if (op->op == OperatorNode::OP_EXTENDS) { + if (op->op == OperatorNode::OP_IS) { //nothing much return op; } diff --git a/modules/gdscript/gd_parser.h b/modules/gdscript/gd_parser.h index 7e7e19de1b6..3ad34666247 100644 --- a/modules/gdscript/gd_parser.h +++ b/modules/gdscript/gd_parser.h @@ -216,7 +216,7 @@ public: OP_CALL, OP_PARENT_CALL, OP_YIELD, - OP_EXTENDS, + OP_IS, //indexing operator OP_INDEX, OP_INDEX_NAMED, diff --git a/modules/gdscript/gd_script.cpp b/modules/gdscript/gd_script.cpp index 173014b138a..dcc0e24098d 100644 --- a/modules/gdscript/gd_script.cpp +++ b/modules/gdscript/gd_script.cpp @@ -1812,6 +1812,7 @@ void GDScriptLanguage::get_reserved_words(List *p_words) const { "breakpoint", "class", "extends", + "is", "func", "preload", "setget", diff --git a/modules/gdscript/gd_tokenizer.cpp b/modules/gdscript/gd_tokenizer.cpp index 5e1a48ae86e..f4e0cc8e291 100644 --- a/modules/gdscript/gd_tokenizer.cpp +++ b/modules/gdscript/gd_tokenizer.cpp @@ -90,6 +90,7 @@ const char *GDTokenizer::token_names[TK_MAX] = { "func", "class", "extends", + "is", "onready", "tool", "static", @@ -864,6 +865,7 @@ void GDTokenizerText::_advance() { { TK_PR_FUNCTION, "func" }, { TK_PR_CLASS, "class" }, { TK_PR_EXTENDS, "extends" }, + { TK_PR_IS, "is" }, { TK_PR_ONREADY, "onready" }, { TK_PR_TOOL, "tool" }, { TK_PR_STATIC, "static" }, diff --git a/modules/gdscript/gd_tokenizer.h b/modules/gdscript/gd_tokenizer.h index ea7629b6619..c0511760976 100644 --- a/modules/gdscript/gd_tokenizer.h +++ b/modules/gdscript/gd_tokenizer.h @@ -96,6 +96,7 @@ public: TK_PR_FUNCTION, TK_PR_CLASS, TK_PR_EXTENDS, + TK_PR_IS, TK_PR_ONREADY, TK_PR_TOOL, TK_PR_STATIC,