Skip to content

Commit 88bde18

Browse files
committed
Enable to use NOT
1 parent 9f9f346 commit 88bde18

File tree

3 files changed

+44
-7
lines changed

3 files changed

+44
-7
lines changed

src/elements/command/test.rs

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,22 @@ enum Elem {
1414
Operand(String),
1515
RightParen,
1616
LeftParen,
17-
Not, // !
17+
Not, // !
1818
And, // &&
1919
Or, // ||
2020
Ans(bool),
2121
}
2222

23-
pub fn op_order(op: &Elem) -> u8 {
23+
fn op_order(op: &Elem) -> u8 {
2424
match op {
2525
Elem::FileCheckOption(_) => 14,
26+
Elem::Not => 13,
27+
Elem::And | Elem::Or => 12,
2628
_ => 0,
2729
}
2830
}
2931

30-
pub fn to_string(op: &Elem) -> String {
32+
fn to_string(op: &Elem) -> String {
3133
match op {
3234
Elem::FileCheckOption(op) => op.to_string(),
3335
Elem::Word(w) => w.text.clone(),
@@ -42,14 +44,14 @@ pub fn to_string(op: &Elem) -> String {
4244
}
4345
}
4446

45-
pub fn to_operand(w: &Word, core: &mut ShellCore) -> Result<Elem, String> {
47+
fn to_operand(w: &Word, core: &mut ShellCore) -> Result<Elem, String> {
4648
match w.eval_as_value(core) {
4749
Some(v) => Ok(Elem::Operand(v)),
4850
None => return Err(format!("{}: wrong substitution", &w.text)),
4951
}
5052
}
5153

52-
pub fn pop_operand(stack: &mut Vec<Elem>, core: &mut ShellCore) -> Result<Elem, String> {
54+
fn pop_operand(stack: &mut Vec<Elem>, core: &mut ShellCore) -> Result<Elem, String> {
5355
let n = match stack.pop() {
5456
Some(Elem::Word(w)) => {
5557
match to_operand(&w, core) {
@@ -93,6 +95,13 @@ impl Command for TestCommand {
9395
Elem::FileCheckOption(ref op) => {
9496
Self::unary_operation(&op, &mut stack, core)
9597
},
98+
Elem::Not => match stack.pop() {
99+
Some(Elem::Ans(res)) => {
100+
stack.push(Elem::Ans(!res));
101+
Ok(())
102+
},
103+
_ => Err("no operand to negate".to_string()),
104+
},
96105
_ => Err( error_message::syntax("TODO")),
97106
};
98107

@@ -132,7 +141,7 @@ impl Command for TestCommand {
132141
}
133142

134143
impl TestCommand {
135-
pub fn rev_polish(&mut self) -> Result<Vec<Elem>, Elem> {
144+
fn rev_polish(&mut self) -> Result<Vec<Elem>, Elem> {
136145
let mut ans = vec![];
137146
let mut stack = vec![];
138147
let mut last = None;
@@ -260,6 +269,26 @@ impl TestCommand {
260269
true
261270
}
262271

272+
fn eat_not_and_or(feeder: &mut Feeder, ans: &mut Self) -> bool {
273+
if feeder.starts_with("!") {
274+
ans.text += &feeder.consume(1);
275+
ans.elements.push( Elem::Not );
276+
return true;
277+
}
278+
if feeder.starts_with("&&") {
279+
ans.text += &feeder.consume(2);
280+
ans.elements.push( Elem::And );
281+
return true;
282+
}
283+
if feeder.starts_with("||") {
284+
ans.text += &feeder.consume(2);
285+
ans.elements.push( Elem::Or );
286+
return true;
287+
}
288+
289+
false
290+
}
291+
263292
fn eat_paren(feeder: &mut Feeder, ans: &mut Self) -> bool {
264293
if feeder.starts_with("(") {
265294
ans.paren_stack.push( '(' );
@@ -313,6 +342,7 @@ impl TestCommand {
313342
}
314343

315344
if Self::eat_file_check_option(feeder, &mut ans, core)
345+
|| Self::eat_not_and_or(feeder, &mut ans)
316346
|| Self::eat_paren(feeder, &mut ans)
317347
|| Self::eat_word(feeder, &mut ans, core) {
318348
continue;

test/ok

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
./test_job.bash
66
../../../test/test_compound.bash
77
../../../test/test_compound.bash
8+
../../../test/test_compound.bash

test/test_compound.bash

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ res=$($com <<< '(( 0 + 1 + 2+3 ))')
532532
[ "$?" = "0" ] || err $LINENO
533533

534534
### [[ TEST ###
535-
#
535+
536536
res=$($com -c '[[ -a /etc/passwd ]]')
537537
[ "$?" = "0" ] || err $LINENO
538538

@@ -545,4 +545,10 @@ res=$($com -c '[[ -a ]]')
545545
res=$($com -c '[[ -a /etc/passwd x ]]')
546546
[ "$?" = "2" ] || err $LINENO
547547

548+
res=$($com -c '[[ ! -a /etc/passwd ]]')
549+
[ "$?" = "1" ] || err $LINENO
550+
551+
res=$($com -c '[[ ! -a /etc/passwdaaa ]]')
552+
[ "$?" = "0" ] || err $LINENO
553+
548554
echo $0 >> ./ok

0 commit comments

Comments
 (0)