Skip to content

Commit

Permalink
GDScript: add this_class keyword
Browse files Browse the repository at this point in the history
  • Loading branch information
ChibiDenDen committed Jan 18, 2020
1 parent d4a222c commit 2ff8c1b
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 0 deletions.
15 changes: 15 additions & 0 deletions core/math/expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1222,6 +1222,8 @@ Error Expression::_get_token(Token &r_token) {
r_token.type = TK_OP_AND;
} else if (id == "self") {
r_token.type = TK_SELF;
} else if (id == "this_class") {
r_token.type = TK_THIS_CLASS;
} else {

for (int i = 0; i < Variant::VARIANT_MAX; i++) {
Expand Down Expand Up @@ -1485,6 +1487,11 @@ Expression::ENode *Expression::_parse_expression() {
SelfNode *self = alloc_node<SelfNode>();
expr = self;
} break;
case TK_THIS_CLASS: {

ThisClassNode *this_class = alloc_node<ThisClassNode>();
expr = this_class;
} break;
case TK_CONSTANT: {
ConstantNode *constant = alloc_node<ConstantNode>();
constant->value = tk.value;
Expand Down Expand Up @@ -1959,6 +1966,14 @@ bool Expression::_execute(const Array &p_inputs, Object *p_instance, Expression:
}
r_ret = p_instance;
} break;
case Expression::ENode::TYPE_THIS_CLASS: {

if (!p_instance) {
r_error_str = RTR("this_class can't be used because instance is null (not passed)");
return true;
}
r_ret = p_instance->get_class();
} break;
case Expression::ENode::TYPE_OPERATOR: {

const Expression::OperatorNode *op = static_cast<const Expression::OperatorNode *>(p_node);
Expand Down
9 changes: 9 additions & 0 deletions core/math/expression.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ class Expression : public Reference {
TK_IDENTIFIER,
TK_BUILTIN_FUNC,
TK_SELF,
TK_THIS_CLASS,
TK_CONSTANT,
TK_BASIC_TYPE,
TK_COLON,
Expand Down Expand Up @@ -206,6 +207,7 @@ class Expression : public Reference {
TYPE_INPUT,
TYPE_CONSTANT,
TYPE_SELF,
TYPE_THIS_CLASS,
TYPE_OPERATOR,
TYPE_INDEX,
TYPE_NAMED_INDEX,
Expand Down Expand Up @@ -273,6 +275,13 @@ class Expression : public Reference {
}
};

struct ThisClassNode : public ENode {

ThisClassNode() {
type = TYPE_THIS_CLASS;
}
};

struct IndexNode : public ENode {
ENode *base;
ENode *index;
Expand Down
4 changes: 4 additions & 0 deletions modules/gdscript/gdscript_compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,10 @@ int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser::
}
return (GDScriptFunction::ADDR_TYPE_SELF << GDScriptFunction::ADDR_BITS);
} break;
case GDScriptParser::Node::TYPE_THIS_CLASS: {
//return constant
return (GDScriptFunction::ADDR_TYPE_CLASS << GDScriptFunction::ADDR_BITS);
} break;
case GDScriptParser::Node::TYPE_ARRAY: {

const GDScriptParser::ArrayNode *an = static_cast<const GDScriptParser::ArrayNode *>(p_expression);
Expand Down
9 changes: 9 additions & 0 deletions modules/gdscript/gdscript_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,11 @@ GDScriptParser::Node *GDScriptParser::_parse_expression(Node *p_parent, bool p_s
SelfNode *self = alloc_node<SelfNode>();
tokenizer->advance();
expr = self;
} else if (tokenizer->get_token() == GDScriptTokenizer::TK_THIS_CLASS) {
//constant defined by tokenizer
ThisClassNode *this_class = alloc_node<ThisClassNode>();
tokenizer->advance();
expr = this_class;
} else if (tokenizer->get_token() == GDScriptTokenizer::TK_BUILT_IN_TYPE && tokenizer->get_token(1) == GDScriptTokenizer::TK_PERIOD) {

Variant::Type bi_type = tokenizer->get_token_type();
Expand Down Expand Up @@ -5576,6 +5581,10 @@ bool GDScriptParser::_parse_type(DataType &r_type, bool p_can_be_void) {
full_name = r_type.native_type;
}
} break;
case GDScriptTokenizer::TK_THIS_CLASS: {
r_type.kind = DataType::CLASS;
r_type.class_type = current_class;
} break;
default: {
return false;
}
Expand Down
5 changes: 5 additions & 0 deletions modules/gdscript/gdscript_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ class GDScriptParser {
TYPE_ARRAY,
TYPE_DICTIONARY,
TYPE_SELF,
TYPE_THIS_CLASS,
TYPE_OPERATOR,
TYPE_CONTROL_FLOW,
TYPE_LOCAL_VAR,
Expand Down Expand Up @@ -345,6 +346,10 @@ class GDScriptParser {
SelfNode() { type = TYPE_SELF; }
};

struct ThisClassNode : public Node {
ThisClassNode() { type = TYPE_THIS_CLASS; }
};

struct OperatorNode : public Node {
enum Operator {
//call/constructor operator
Expand Down
2 changes: 2 additions & 0 deletions modules/gdscript/gdscript_tokenizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ static const _kws _keyword_list[] = {
{ GDScriptTokenizer::TK_CF_MATCH, "match" },
{ GDScriptTokenizer::TK_CF_PASS, "pass" },
{ GDScriptTokenizer::TK_SELF, "self" },
{ GDScriptTokenizer::TK_THIS_CLASS, "this_class" },
{ GDScriptTokenizer::TK_CONST_PI, "PI" },
{ GDScriptTokenizer::TK_CONST_TAU, "TAU" },
{ GDScriptTokenizer::TK_WILDCARD, "_" },
Expand Down Expand Up @@ -291,6 +292,7 @@ bool GDScriptTokenizer::is_token_literal(int p_offset, bool variable_safe) const
case TK_CF_MATCH:
case TK_CF_PASS:
case TK_SELF:
case TK_THIS_CLASS:
case TK_CONST_PI:
case TK_CONST_TAU:
case TK_WILDCARD:
Expand Down
1 change: 1 addition & 0 deletions modules/gdscript/gdscript_tokenizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class GDScriptTokenizer {
TK_IDENTIFIER,
TK_CONSTANT,
TK_SELF,
TK_THIS_CLASS,
TK_BUILT_IN_TYPE,
TK_BUILT_IN_FUNC,
TK_OP_IN,
Expand Down

0 comments on commit 2ff8c1b

Please sign in to comment.