From c68b2358d58c9acd09dc9618bb7ba89246d759f7 Mon Sep 17 00:00:00 2001 From: George Marques Date: Sat, 28 Jan 2023 19:49:14 -0300 Subject: GDScript: Allow variables in match patterns To restore an ability available in 3.x and reduce compatibility changes. --- modules/gdscript/gdscript_analyzer.cpp | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) (limited to 'modules/gdscript/gdscript_analyzer.cpp') diff --git a/modules/gdscript/gdscript_analyzer.cpp b/modules/gdscript/gdscript_analyzer.cpp index 8c3521c530..8660dbdd42 100644 --- a/modules/gdscript/gdscript_analyzer.cpp +++ b/modules/gdscript/gdscript_analyzer.cpp @@ -1901,11 +1901,22 @@ void GDScriptAnalyzer::resolve_match_pattern(GDScriptParser::PatternNode *p_matc break; case GDScriptParser::PatternNode::PT_EXPRESSION: if (p_match_pattern->expression) { - reduce_expression(p_match_pattern->expression); - if (!p_match_pattern->expression->is_constant) { - push_error(R"(Expression in match pattern must be a constant.)", p_match_pattern->expression); + GDScriptParser::ExpressionNode *expr = p_match_pattern->expression; + reduce_expression(expr); + result = expr->get_datatype(); + if (!expr->is_constant) { + while (expr && expr->type == GDScriptParser::Node::SUBSCRIPT) { + GDScriptParser::SubscriptNode *sub = static_cast(expr); + if (!sub->is_attribute) { + expr = nullptr; + } else { + expr = sub->base; + } + } + if (!expr || expr->type != GDScriptParser::Node::IDENTIFIER) { + push_error(R"(Expression in match pattern must be a constant expression, an identifier, or an attribute access ("A.B").)", expr); + } } - result = p_match_pattern->expression->get_datatype(); } break; case GDScriptParser::PatternNode::PT_BIND: -- cgit v1.2.3