Skip to content

Commit

Permalink
Add String support for GDScript Enums
Browse files Browse the repository at this point in the history
  • Loading branch information
willnationsdev committed Aug 14, 2018
1 parent 0322081 commit ddc6ab3
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions modules/gdscript/gdscript_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4755,7 +4755,7 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
case GDScriptTokenizer::TK_PR_ENUM: {
//multiple constant declarations..

int last_assign = -1; // Incremented by 1 right before the assingment.
int last_assign = -1; // Incremented by 1 right before the assignment.
String enum_name;
Dictionary enum_dict;

Expand Down Expand Up @@ -4794,7 +4794,7 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {

tokenizer->advance();

if (tokenizer->get_token() == GDScriptTokenizer::TK_OP_ASSIGN) {
if (tokenizer->get_token() == GDScriptTokenizer::TK_OP_ASSIGN || tokenizer->get_token() == GDScriptTokenizer::TK_COLON) {
tokenizer->advance();

Node *subexpr = _parse_and_reduce_expression(p_class, true, true);
Expand All @@ -4812,13 +4812,16 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {

ConstantNode *subexpr_const = static_cast<ConstantNode *>(subexpr);

if (subexpr_const->value.get_type() != Variant::INT) {
_set_error("Expected an int value for enum");
if (subexpr_const->value.get_type() == Variant::INT) {
last_assign = subexpr_const->value;
} else if (subexpr_const->value.get_type() == Variant::STRING) {
last_assign++;
subexpr_const->datatype.builtin_type = Variant::STRING;
} else {
_set_error("Expected an int or string value for enum");
return;
}

last_assign = subexpr_const->value;

constant.expression = subexpr_const;

} else {
Expand All @@ -4833,14 +4836,15 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
tokenizer->advance();
}

if (enum_name != "") {
const ConstantNode *cn = static_cast<const ConstantNode *>(constant.expression);
const ConstantNode *cn = static_cast<const ConstantNode *>(constant.expression);

if (cn && enum_name != "") {
enum_dict[const_id] = cn->value;
}

constant.type.has_type = true;
constant.type.kind = DataType::BUILTIN;
constant.type.builtin_type = Variant::INT;
constant.type.builtin_type = (cn && cn->value.get_type() == Variant::STRING) ? Variant::STRING : Variant::INT;
p_class->constant_expressions.insert(const_id, constant);
}
}
Expand Down

0 comments on commit ddc6ab3

Please sign in to comment.