Repository: reubeno/brush (rust)

Issue:
Bash arithmetic expressions are applied recursively
```console
$ brush --version
brush 0.2.14 (git:dd1d39c)

$ brush -c 'one=1 two=2 exp=one+two; echo $((exp))'
0

$ bash -c 'one=1 two=2 exp=one+two; echo $((exp))'
3
```

Pull request description:
fix(arithmetic): recursively evaluate var references
In an arithmetic expression, we need to recursively parse and evaluate the body of any bare variable references.

Resolves #345.

Patch:
diff --git a/brush-core/src/arithmetic.rs b/brush-core/src/arithmetic.rs
index ae92d7c..c13257a 100644
--- a/brush-core/src/arithmetic.rs
+++ b/brush-core/src/arithmetic.rs
@@ -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)]


Test patch:
diff --git a/brush-shell/tests/cases/arithmetic.yaml b/brush-shell/tests/cases/arithmetic.yaml
index 68b3fd0..096a46e 100644
--- a/brush-shell/tests/cases/arithmetic.yaml
+++ b/brush-shell/tests/cases/arithmetic.yaml
@@ -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))"
@@ -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: |