1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 07:58:11 +00:00

Shell: Allow parts of globs to be named in match expressions

This patchset allows a match expression to have a list of names for its
glob parts, which are assigned to the matched values in the body of the
match.
For example,
```sh
stuff=foobarblahblah/target_{1..30}
for $stuff {
    match $it {
        */* as (dir sub) {
            echo "doing things with $sub in $dir"
            make -C $dir $sub # or whatever...
        }
    }
}
```

With this, match expressions are now significantly more powerful!
This commit is contained in:
AnotherTest 2020-10-25 10:42:03 +03:30 committed by Andreas Kling
parent 0801b1fada
commit 1a4ac3531f
6 changed files with 115 additions and 10 deletions

View file

@ -452,6 +452,17 @@ void Formatter::visit(const AST::MatchExpr* node)
}
current_builder().append(' ');
if (entry.match_names.has_value() && !entry.match_names.value().is_empty()) {
current_builder().append("as (");
auto first = true;
for (auto& name : entry.match_names.value()) {
if (!first)
current_builder().append(' ');
first = false;
current_builder().append(name);
}
current_builder().append(") ");
}
in_new_block([&] {
if (entry.body)
entry.body->visit(*this);