Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: address issues with array unset #211

Merged
merged 1 commit into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions brush-core/src/builtins/unset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,14 @@ impl builtins::Command for UnsetCommand {
context.shell.env.unset(name.as_str())?
}
brush_parser::word::Parameter::NamedWithIndex { name, index } => {
// TODO: Evaluate index?
// First evaluate the index expression.
let index_as_expr = brush_parser::arithmetic::parse(index.as_str())?;
let evaluated_index = context.shell.eval_arithmetic(index_as_expr).await?;

context
.shell
.env
.unset_index(name.as_str(), index.as_str())?
.unset_index(name.as_str(), evaluated_index.to_string().as_str())?
}
brush_parser::word::Parameter::NamedWithAllIndices {
name: _,
Expand Down
2 changes: 1 addition & 1 deletion brush-parser/src/word.rs
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,7 @@ peg::parser! {
p:special_parameter() { Parameter::Special(p) } /
non_posix_extensions_enabled() p:variable_name() "[@]" { Parameter::NamedWithAllIndices { name: p.to_owned(), concatenate: false } } /
non_posix_extensions_enabled() p:variable_name() "[*]" { Parameter::NamedWithAllIndices { name: p.to_owned(), concatenate: true } } /
non_posix_extensions_enabled() p:variable_name() "[" index:$((!"]" [_])*) "]" {?
non_posix_extensions_enabled() p:variable_name() "[" index:$(arithmetic_word(<"]">)) "]" {?
Ok(Parameter::NamedWithIndex { name: p.to_owned(), index: index.to_owned() })
} /
p:variable_name() { Parameter::Named(p.to_owned()) }
Expand Down
5 changes: 5 additions & 0 deletions brush-shell/tests/cases/arrays.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -270,3 +270,8 @@ cases:
stdin: |
declare -A myarray=([0]="default" ["a"]="other")
echo "Value: ${myarray[$non_existent_var]}"

- name: "Nested array reference"
stdin: |
x=(2 3 4)
echo ${x[${x[0]}]}
13 changes: 12 additions & 1 deletion brush-shell/tests/cases/builtins/unset.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,23 @@ cases:
declare -p myarray

- name: "Unset array element with interesting expression"
known_failure: true # TODO: case needs implementing
stdin: |
declare -a myarray=(a b c d e)

echo "Initial array:"
declare -p myarray
echo

i=4
unset myarray[i]
echo "After removing element at index 4:"
declare -p myarray
echo

unset myarray[6/2]
echo "After removing element at index 3:"
declare -p myarray
echo

- name: "Unset local in same function"
stdin: |
Expand Down
Loading