Skip to content

Commit

Permalink
fix(arithmetic): recursively evaluate var references (#351)
Browse files Browse the repository at this point in the history
  • Loading branch information
reubeno authored Jan 22, 2025
1 parent 6d5299f commit 1b82617
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
6 changes: 4 additions & 2 deletions brush-core/src/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,10 @@ fn deref_lvalue(shell: &mut Shell, lvalue: &ast::ArithmeticTarget) -> Result<i64
}
};

let value: i64 = value_str.parse().unwrap_or(0);
Ok(value)
let parsed_value = brush_parser::arithmetic::parse(value_str.as_ref())
.map_err(|_err| EvalError::ParseError(value_str.to_string()))?;

parsed_value.eval(shell)
}

#[allow(clippy::unnecessary_wraps)]
Expand Down
25 changes: 24 additions & 1 deletion brush-shell/tests/cases/arithmetic.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,27 @@ cases:
- name: "Variable references"
stdin: |
x=10
echo "x => $((x))"
echo "x + 1 => $((x+1))"
y=
echo "y => $((y))"
echo "y + 1 => $((y+1))"
z=notnumber
echo "z => $((z))"
echo "z + 1 => $((z+1))"
- name: "Complex variable references"
stdin: |
x=10
y=20
z=x+y
a=z+z
echo "z => $((z))"
echo "a => $((a))"
- name: "Nested expressions"
stdin: |
echo "1: $(($(echo -n 1; echo 2) + 37))"
Expand Down Expand Up @@ -119,6 +136,12 @@ cases:
echo "--x == $((--x))"
echo "x is now $x"
- name: "Assignment with references"
stdin: |
x=10+3
echo "y = x => $((y = x))"
echo "y is now $y"
- name: "Assignments in logical boolean expressions"
known_failure: false
stdin: |
Expand Down

0 comments on commit 1b82617

Please sign in to comment.