From 0cdc8ef645f5fe84d4fd45019cd98d2cb94fd05a Mon Sep 17 00:00:00 2001 From: Xavier Sellier Date: Mon, 11 Dec 2017 10:41:43 -0500 Subject: [PATCH] Properly support nested ternary expressions Fixes 14324 Original work @bojidar-bg --- modules/gdscript/gd_parser.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/modules/gdscript/gd_parser.cpp b/modules/gdscript/gd_parser.cpp index 99533ae030a..938ed01ef9d 100644 --- a/modules/gdscript/gd_parser.cpp +++ b/modules/gdscript/gd_parser.cpp @@ -972,6 +972,7 @@ GDParser::Node *GDParser::_parse_expression(Node *p_parent, bool p_static, bool bool unary = false; bool ternary = false; bool error = false; + bool right_to_left = false; switch (expression[i].op) { @@ -1022,11 +1023,13 @@ GDParser::Node *GDParser::_parse_expression(Node *p_parent, bool p_static, bool 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; @@ -1046,13 +1049,13 @@ GDParser::Node *GDParser::_parse_expression(Node *p_parent, bool p_static, bool } } - 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;