Skip to content

Commit

Permalink
Properly support nested ternary expressions
Browse files Browse the repository at this point in the history
Fixes 14324.
  • Loading branch information
bojidar-bg committed Dec 11, 2017
1 parent dfb3634 commit 2c190b9
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions modules/gdscript/gdscript_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1140,6 +1140,7 @@ GDScriptParser::Node *GDScriptParser::_parse_expression(Node *p_parent, bool p_s
bool unary = false;
bool ternary = false;
bool error = false;
bool right_to_left = false;

switch (expression[i].op) {

Expand Down Expand Up @@ -1194,11 +1195,13 @@ GDScriptParser::Node *GDScriptParser::_parse_expression(Node *p_parent, bool p_s
case OperatorNode::OP_TERNARY_IF:
priority = 14;
ternary = true;
right_to_left = true;
break;
case OperatorNode::OP_TERNARY_ELSE:
priority = 14;
error = true;
break; // Errors out when found without IF (since IF would consume it)
// Rigth-to-left should be false in this case, otherwise it would always error.
break;

case OperatorNode::OP_ASSIGN: priority = 15; break;
case OperatorNode::OP_ASSIGN_ADD: priority = 15; break;
Expand All @@ -1218,13 +1221,13 @@ GDScriptParser::Node *GDScriptParser::_parse_expression(Node *p_parent, bool p_s
}
}

if (priority < min_priority) {
if (priority < min_priority || (right_to_left && priority == min_priority)) {
// < is used for left to right (default)
// <= is used for right to left
if (error) {
_set_error("Unexpected operator");
return NULL;
}
// < is used for left to right (default)
// <= is used for right to left
next_op = i;
min_priority = priority;
is_unary = unary;
Expand Down

0 comments on commit 2c190b9

Please sign in to comment.