Skip to content

Commit 322ceb6

Browse files
committed
Support += on substitution
1 parent 1ed15e6 commit 322ceb6

File tree

8 files changed

+49
-9
lines changed

8 files changed

+49
-9
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "sush"
3-
version = "0.8.1"
3+
version = "0.8.2"
44
edition = "2021"
55

66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

src/core/data.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,13 @@ impl Data {
123123
}
124124
}
125125

126+
pub fn is_array(&mut self, key: &str) -> bool {
127+
match self.get_value(key) {
128+
Some(Value::EvaluatedArray(_)) => true,
129+
_ => false,
130+
}
131+
}
132+
126133
pub fn get_position_params(&self) -> Vec<String> {
127134
match self.position_parameters.last() {
128135
Some(v) => v[1..].to_vec(),

src/elements/error

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
../../test/test_compound.bash
2+
../../test/test_others.bash
3+
../../test/test_others.bash
4+
../../test/test_others.bash

src/elements/substitution.rs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,39 @@ pub struct Substitution {
1111
pub text: String,
1212
pub key: String,
1313
pub value: Value,
14+
pub append: bool,
1415
}
1516

1617
impl Substitution {
1718
pub fn eval(&mut self, core: &mut ShellCore) -> Value {
18-
match &self.value {
19+
match self.value.clone() {
1920
Value::None => Value::EvaluatedSingle("".to_string()),
20-
Value::Single(v) => Self::eval_as_value(&v, core),
21-
Value::Array(a) => Self::eval_as_array(&mut a.clone(), core),
21+
Value::Single(v) => self.eval_as_value(&v, core),
22+
Value::Array(a) => self.eval_as_array(&mut a.clone(), core),
2223
_ => Value::None,
2324
}
2425
}
2526

26-
fn eval_as_value(w: &Word, core: &mut ShellCore) -> Value {
27+
fn eval_as_value(&self, w: &Word, core: &mut ShellCore) -> Value {
28+
let prev = match self.append {
29+
true => core.data.get_param(&self.key),
30+
false => "".to_string(),
31+
};
32+
2733
match w.eval_as_value(core) {
28-
Some(s) => Value::EvaluatedSingle(s),
34+
Some(s) => Value::EvaluatedSingle(prev + &s),
2935
None => Value::None,
3036
}
3137
}
3238

33-
fn eval_as_array(a: &mut Array, core: &mut ShellCore) -> Value {
39+
fn eval_as_array(&self, a: &mut Array, core: &mut ShellCore) -> Value {
40+
let prev = match self.append {
41+
true => core.data.get_array_all(&self.key),
42+
false => vec![],
43+
};
44+
3445
match a.eval(core) {
35-
Some(values) => Value::EvaluatedArray(values),
46+
Some(values) => Value::EvaluatedArray([prev, values].concat()),
3647
None => Value::None,
3748
}
3849
}
@@ -42,6 +53,7 @@ impl Substitution {
4253
text: String::new(),
4354
key: String::new(),
4455
value: Value::None,
56+
append: false,
4557
}
4658
}
4759

@@ -55,7 +67,13 @@ impl Substitution {
5567

5668
let mut name_eq = feeder.consume(len);
5769
ans.text += &name_eq;
70+
5871
name_eq.pop();
72+
if name_eq.ends_with("+") {
73+
ans.append = true;
74+
name_eq.pop();
75+
}
76+
5977
ans.key = name_eq.clone();
6078

6179
if let Some(a) = Array::parse(feeder, core) {

src/feeder/scanner.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,10 @@ impl Feeder {
229229
return 0;
230230
}
231231

232-
if self.remaining.chars().nth(name_len).unwrap_or('x') == '=' {
232+
if self.remaining[name_len..].starts_with("=") {
233233
name_len + 1
234+
} else if self.remaining[name_len..].starts_with("+=") {
235+
name_len + 2
234236
}else{
235237
0
236238
}

test/error

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
../../test/test_others.bash
2+
../../test/test_others.bash

test/ok

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@
44
./test_compound.bash
55
./test_others.bash
66
./test_job.bash
7+
../../test/test_others.bash
8+
../../test/test_others.bash

test/test_others.bash

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,12 @@ res=$($com <<< 'A=${ }')
472472
res=$($com <<< 'A=B cd ; echo $A')
473473
[ "$res" == "" ] || err $LINENO
474474

475+
res=$($com <<< 'A=aaa ; A+=bbb ; echo $A')
476+
[ "$res" == "aaabbb" ] || err $LINENO
477+
478+
res=$($com <<< 'A=(aaa bbb) ; A+=(ccc ddd) ; echo ${A[@]}')
479+
[ "$res" == "aaa bbb ccc ddd" ] || err $LINENO
480+
475481
# arithmetic calculation
476482

477483
res=$($com <<< 'echo $((12345 ))aaa')

0 commit comments

Comments
 (0)