mirror of
https://github.com/godotengine/godot.git
synced 2025-11-01 06:01:14 +00:00
GDScript: Implement pattern guards for match statement
Within a match statement, it is now possible to add guards in each
branch:
var a = 0
match a:
0 when false: print("does not run")
0 when true: print("but this does")
This allows more complex logic for deciding which branch to take.
This commit is contained in:
parent
ec62b8a3ee
commit
54a1414500
15 changed files with 163 additions and 1 deletions
|
|
@ -1928,6 +1928,26 @@ Error GDScriptCompiler::_parse_block(CodeGen &codegen, const GDScriptParser::Sui
|
|||
}
|
||||
}
|
||||
|
||||
// If there's a guard, check its condition too.
|
||||
if (branch->guard_body != nullptr) {
|
||||
// Do this first so the guard does not run unless the pattern matched.
|
||||
gen->write_and_left_operand(pattern_result);
|
||||
|
||||
// Don't actually use the block for the guard.
|
||||
// The binds are already in the locals and we don't want to clear the result of the guard condition before we check the actual match.
|
||||
GDScriptCodeGenerator::Address guard_result = _parse_expression(codegen, err, static_cast<GDScriptParser::ExpressionNode *>(branch->guard_body->statements[0]));
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
|
||||
gen->write_and_right_operand(guard_result);
|
||||
gen->write_end_and(pattern_result);
|
||||
|
||||
if (guard_result.mode == GDScriptCodeGenerator::Address::TEMPORARY) {
|
||||
codegen.generator->pop_temporary();
|
||||
}
|
||||
}
|
||||
|
||||
// Check if pattern did match.
|
||||
gen->write_if(pattern_result);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue