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

Shell: Add support for indexing into variables

Now a variable may have an optional slice (only _one_ slice), which can
also use negative indices to index from the end.
This works on both lists and strings.
The contents of the slice have the same semantics as brace expansions.
For example:
```sh
$ x=(1 2 3 4 5 6)
$ echo $x[1..3] # select indices 1, 2, 3
2 3 4
$ echo $x[3,4,1,0] # select indices 3, 4, 1, 0 (in that order)
4 5 2 1
$ x="Well Hello Friends!"
$ echo $x[5..9]
Hello
```
This commit is contained in:
AnotherTest 2021-03-13 03:10:18 +03:30 committed by Andreas Kling
parent ddcef0452a
commit 3b8fa5a753
11 changed files with 391 additions and 56 deletions

View file

@ -202,12 +202,21 @@ void NodeVisitor::visit(const AST::Subshell* node)
node->block()->visit(*this);
}
void NodeVisitor::visit(const AST::SimpleVariable*)
void NodeVisitor::visit(const AST::Slice* node)
{
node->selector()->visit(*this);
}
void NodeVisitor::visit(const AST::SpecialVariable*)
void NodeVisitor::visit(const AST::SimpleVariable* node)
{
if (const AST::Node* slice = node->slice())
slice->visit(*this);
}
void NodeVisitor::visit(const AST::SpecialVariable* node)
{
if (const AST::Node* slice = node->slice())
slice->visit(*this);
}
void NodeVisitor::visit(const AST::Juxtaposition* node)