diff options
author | Rémi Verschelde <remi@verschelde.fr> | 2023-01-29 02:45:48 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-01-29 02:45:48 +0100 |
commit | a3dae9e548e5fbab7b7be070cf71f972656f759b (patch) | |
tree | bacf04c8cf902580022d94e707225c57dff561cb /modules/gdscript/gdscript_analyzer.cpp | |
parent | d866d6cd1bc79539a793611afa1ea8cf6cce2e6e (diff) | |
parent | c68b2358d58c9acd09dc9618bb7ba89246d759f7 (diff) | |
download | redot-engine-a3dae9e548e5fbab7b7be070cf71f972656f759b.tar.gz |
Merge pull request #72285 from vnen/gdscript-variable-match
GDScript: Allow variables in match patterns
Diffstat (limited to 'modules/gdscript/gdscript_analyzer.cpp')
-rw-r--r-- | modules/gdscript/gdscript_analyzer.cpp | 19 |
1 files changed, 15 insertions, 4 deletions
diff --git a/modules/gdscript/gdscript_analyzer.cpp b/modules/gdscript/gdscript_analyzer.cpp index 3ad8f12192..7ea0603d57 100644 --- a/modules/gdscript/gdscript_analyzer.cpp +++ b/modules/gdscript/gdscript_analyzer.cpp @@ -1891,11 +1891,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<GDScriptParser::SubscriptNode *>(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: |