Skip to content

Commit 427b1f9

Browse files
committed
Simplify
1 parent 941aa62 commit 427b1f9

File tree

6 files changed

+67
-66
lines changed

6 files changed

+67
-66
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.7.3"
3+
version = "0.7.4"
44
edition = "2021"
55

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

src/elements/arithmetic_expression/calculator.rs

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -60,31 +60,6 @@ fn unary_operation(op: &str, stack: &mut Vec<Elem>, core: &mut ShellCore) -> Res
6060
}
6161

6262
pub fn calculate(elements: &Vec<Elem>, core: &mut ShellCore) -> Result<Elem, String> {
63-
let mut comma_pos = vec![];
64-
for (i, e) in elements.iter().enumerate() {
65-
match e {
66-
Elem::BinaryOp(c) => {
67-
if c == "," {
68-
comma_pos.push(i);
69-
}
70-
},
71-
_ => {},
72-
}
73-
}
74-
75-
let mut left = 0;
76-
for i in 0..comma_pos.len() {
77-
let right = comma_pos[i];
78-
if let Err(e) = calculate_sub(&elements[left..right], core) {
79-
return Err(e);
80-
}
81-
left = right + 1;
82-
}
83-
84-
calculate_sub(&elements[left..], core)
85-
}
86-
87-
fn calculate_sub(elements: &[Elem], core: &mut ShellCore) -> Result<Elem, String> {
8863
if elements.len() == 0 {
8964
return Ok(Elem::Integer(0));
9065
}

src/elements/arithmetic_expression/parser.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,8 @@ impl ArithmeticExpr {
162162
loop {
163163
Self::eat_blank(feeder, &mut ans, core);
164164

165-
if feeder.starts_with(":") {
165+
if feeder.starts_with(":")
166+
|| feeder.starts_with(",") {
166167
break;
167168
}
168169

src/elements/command/arithmetic.rs

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,24 @@ use crate::elements::arithmetic_expression::ArithmeticExpr;
88
#[derive(Debug, Clone)]
99
pub struct ArithmeticCommand {
1010
text: String,
11-
arith: Option<ArithmeticExpr>,
11+
arith: Vec<ArithmeticExpr>,
1212
redirects: Vec<Redirect>,
1313
force_fork: bool,
1414
}
1515

1616
impl Command for ArithmeticCommand {
1717
fn run(&mut self, core: &mut ShellCore, _: bool) {
18-
if self.arith.is_none() {
19-
core.data.set_param("?", "0");
20-
return;
21-
}
18+
let mut ans = true;
2219

23-
let ans = match self.arith.as_mut().unwrap().eval(core) {
24-
Some(s) => s != "0",
25-
None => false,
26-
};
20+
for a in &mut self.arith {
21+
match a.eval(core) {
22+
Some(s) => ans = s != "0",
23+
None => {
24+
ans = false;
25+
break;
26+
},
27+
}
28+
}
2729

2830
core.data.set_param("?", if ans {"0"} else {"1"} );
2931
}
@@ -39,7 +41,7 @@ impl ArithmeticCommand {
3941
fn new() -> ArithmeticCommand {
4042
ArithmeticCommand {
4143
text: String::new(),
42-
arith: None,
44+
arith: vec![],
4345
redirects: vec![],
4446
force_fork: false,
4547
}
@@ -54,20 +56,29 @@ impl ArithmeticCommand {
5456
let mut ans = Self::new();
5557
ans.text = feeder.consume(2);
5658

57-
if let Some(c) = ArithmeticExpr::parse(feeder, core) {
58-
if ! feeder.starts_with("))") {
59-
feeder.rewind();
60-
return None;
61-
}
59+
loop {
60+
if let Some(c) = ArithmeticExpr::parse(feeder, core) {
61+
if feeder.starts_with(",") {
62+
ans.text += &c.text;
63+
ans.text += &feeder.consume(1);
64+
ans.arith.push(c);
65+
continue;
66+
}
6267

63-
ans.text += &c.text;
64-
ans.text += &feeder.consume(2);
65-
ans.arith = Some(c);
66-
feeder.pop_backup();
67-
return Some(ans);
68-
}
68+
if feeder.starts_with("))") {
69+
ans.text += &c.text;
70+
ans.text += &feeder.consume(2);
71+
ans.arith.push(c);
72+
feeder.pop_backup();
73+
return Some(ans);
74+
}
6975

76+
break;
77+
}else{
78+
break;
79+
}
80+
}
7081
feeder.rewind();
71-
None
82+
return None;
7283
}
7384
}

src/elements/subword/arithmetic.rs

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,30 @@ use crate::elements::subword::Subword;
88
#[derive(Debug, Clone)]
99
pub struct Arithmetic {
1010
pub text: String,
11-
pub arith: ArithmeticExpr,
11+
pub arith: Vec<ArithmeticExpr>,
1212
}
1313

1414
impl Subword for Arithmetic {
1515
fn get_text(&self) -> &str { &self.text.as_ref() }
1616
fn boxed_clone(&self) -> Box<dyn Subword> {Box::new(self.clone())}
1717

1818
fn substitute(&mut self, core: &mut ShellCore) -> bool {
19-
match self.arith.eval(core) {
20-
Some(s) => {self.text = s; true},
21-
None => false,
19+
for a in &mut self.arith {
20+
match a.eval(core) {
21+
Some(s) => self.text = s,
22+
None => return false,
23+
}
2224
}
25+
26+
true
2327
}
2428
}
2529

2630
impl Arithmetic {
2731
fn new() -> Self {
2832
Self {
2933
text: String::new(),
30-
arith: ArithmeticExpr::new(),
34+
arith: vec![],
3135
}
3236
}
3337

@@ -40,19 +44,29 @@ impl Arithmetic {
4044
let mut ans = Self::new();
4145
ans.text = feeder.consume(3);
4246

43-
if let Some(c) = ArithmeticExpr::parse(feeder, core) {
44-
if ! feeder.starts_with("))") {
45-
feeder.rewind();
46-
return None;
47-
}
47+
loop {
48+
if let Some(c) = ArithmeticExpr::parse(feeder, core) {
49+
if feeder.starts_with(",") {
50+
ans.text += &c.text;
51+
ans.text += &feeder.consume(1);
52+
ans.arith.push(c);
53+
continue;
54+
}
4855

49-
ans.text += &c.text;
50-
ans.text += &feeder.consume(2);
51-
ans.arith = c;
52-
feeder.pop_backup();
53-
return Some(ans);
54-
}
56+
if feeder.starts_with("))") {
57+
ans.text += &c.text;
58+
ans.text += &feeder.consume(2);
59+
ans.arith.push(c);
60+
feeder.pop_backup();
61+
return Some(ans);
62+
}
5563

64+
break;
65+
}else{
66+
break;
67+
}
68+
}
69+
5670
feeder.rewind();
5771
None
5872
}

src/feeder/scanner.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ impl Feeder {
203203
">", "=", "&", "|", "^", "/", "%"], core);
204204
self.scanner_one_of(&["<<=", ">>=",
205205
"&&", "||", "**", "==", "!=", "*=", "/=", "%=", "+=", "-=", "&=", "^=", "|=",
206-
">>", "<<", "<=", ">=", "&", "^", "=", "+", "-", "/", "*", "%", "<", ">", "|", "^", ","])
206+
">>", "<<", "<=", ">=", "&", "^", "=", "+", "-", "/", "*", "%", "<", ">", "|", "^"/*, ","*/])
207207
}
208208

209209
pub fn scanner_uint(&mut self, core: &mut ShellCore) -> usize {

0 commit comments

Comments
 (0)