Skip to content

Commit 6c19d8c

Browse files
committed
Added remaining unary operations
1 parent 97d53c2 commit 6c19d8c

File tree

4 files changed

+81
-46
lines changed

4 files changed

+81
-46
lines changed

crates/deno_task_shell/src/grammar.pest

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -202,34 +202,33 @@ bitwise_or = { "|" }
202202
logical_and = { "&&" }
203203
logical_or = { "||" }
204204

205+
unary_plus = { "+" }
206+
unary_minus = { "-" }
207+
logical_not = { "!" }
208+
bitwise_not = { "~" }
209+
increment = { "++" }
210+
decrement = { "--" }
211+
205212
unary_arithmetic_expr = !{
206213
unary_pre_arithmetic_expr | unary_post_arithmetic_expr
207214
}
208215

209216
unary_pre_arithmetic_expr = !{
210-
(post_arithmetic_op | pre_arithmetic_op) ~ (parentheses_expr | VARIABLE | NUMBER)
217+
pre_arithmetic_op ~ (parentheses_expr | VARIABLE | NUMBER)
211218
}
212219

213220
unary_post_arithmetic_expr = !{
214221
(parentheses_expr | VARIABLE | NUMBER) ~ post_arithmetic_op
215222
}
216223

217-
pre_arithmetic_op= _{
218-
unary_plus | unary_minus | logical_not | bitwise_not
224+
pre_arithmetic_op= !{
225+
increment | decrement | unary_plus | unary_minus | logical_not | bitwise_not
219226
}
220227

221-
unary_plus = { "+" }
222-
unary_minus = { "-" }
223-
logical_not = { "!" }
224-
bitwise_not = { "~" }
225-
226228
post_arithmetic_op = !{
227229
increment | decrement
228230
}
229231

230-
increment = { "++" }
231-
decrement = { "--" }
232-
233232
assignment_operator = _{
234233
assign | multiply_assign | divide_assign | modulo_assign | add_assign | subtract_assign |
235234
left_shift_assign | right_shift_assign | bitwise_and_assign | bitwise_xor_assign | bitwise_or_assign

crates/deno_task_shell/src/parser.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1469,9 +1469,9 @@ fn unary_pre_arithmetic_expr(pair: Pair<Rule>) -> Result<ArithmeticPart> {
14691469
})
14701470
}
14711471
Rule::post_arithmetic_op => {
1472-
let op = parse_pre_arithmetic_op(first)?;
1472+
let op = parse_post_arithmetic_op(first)?;
14731473
Ok(ArithmeticPart::UnaryArithmeticExpr {
1474-
operator: UnaryArithmeticOp::Pre(op),
1474+
operator: UnaryArithmeticOp::Post(op),
14751475
operand: Box::new(operand),
14761476
})
14771477
}
@@ -1519,12 +1519,12 @@ fn parse_pre_arithmetic_op(pair: Pair<Rule>) -> Result<PreArithmeticOp> {
15191519
match first.as_rule() {
15201520
Rule::increment => Ok(PreArithmeticOp::Increment),
15211521
Rule::decrement => Ok(PreArithmeticOp::Decrement),
1522-
Rule::add => Ok(PreArithmeticOp::Plus),
1523-
Rule::subtract => Ok(PreArithmeticOp::Minus),
1522+
Rule::unary_plus => Ok(PreArithmeticOp::Plus),
1523+
Rule::unary_minus => Ok(PreArithmeticOp::Minus),
15241524
Rule::logical_not => Ok(PreArithmeticOp::LogicalNot),
15251525
Rule::bitwise_not => Ok(PreArithmeticOp::BitwiseNot),
15261526
_ => Err(miette!(
1527-
"Unexpected rule in post arithmetic operator: {:?}",
1527+
"Unexpected rule in pre arithmetic operator: {:?}",
15281528
first.as_rule()
15291529
)),
15301530
}

crates/deno_task_shell/src/shell/types.rs

Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -636,17 +636,19 @@ impl ArithmeticResult {
636636
ArithmeticValue::Integer(val) => match operand {
637637
ArithmeticPart::Variable(name) => {
638638
let mut new_changes = self.changes.clone();
639-
new_changes.push(EnvChange::SetShellVar(
640-
name.to_string(),
641-
match op_type {
642-
PreArithmeticOp::Increment => (*val + 1).to_string(),
643-
PreArithmeticOp::Decrement => (*val - 1).to_string(),
644-
_ => todo!(
645-
"Unary arithmetic operation not implemented {:?}",
646-
op_type
647-
),
648-
},
649-
));
639+
if op_type == PreArithmeticOp::Increment
640+
|| op_type == PreArithmeticOp::Decrement
641+
{
642+
new_changes.push(EnvChange::SetShellVar(
643+
name.to_string(),
644+
match op_type {
645+
PreArithmeticOp::Increment => (*val + 1).to_string(),
646+
PreArithmeticOp::Decrement => (*val - 1).to_string(),
647+
_ => Err(miette!("No change to ENV need for: {}", self))?,
648+
},
649+
));
650+
}
651+
650652
Ok(ArithmeticResult {
651653
value: match op_type {
652654
PreArithmeticOp::Increment => {
@@ -655,10 +657,14 @@ impl ArithmeticResult {
655657
PreArithmeticOp::Decrement => {
656658
ArithmeticValue::Integer(*val - 1)
657659
}
658-
_ => todo!(
659-
"Unary arithmetic operation not implemented {:?}",
660-
op_type
661-
),
660+
PreArithmeticOp::Plus => ArithmeticValue::Integer((*val).abs()),
661+
PreArithmeticOp::Minus => {
662+
ArithmeticValue::Integer(-(*val).abs())
663+
}
664+
PreArithmeticOp::BitwiseNot => ArithmeticValue::Integer(!*val),
665+
PreArithmeticOp::LogicalNot => {
666+
ArithmeticValue::Integer(if *val == 0 { 1 } else { 0 })
667+
}
662668
},
663669
changes: new_changes,
664670
})
@@ -671,17 +677,19 @@ impl ArithmeticResult {
671677
ArithmeticValue::Float(val) => match operand {
672678
ArithmeticPart::Variable(name) => {
673679
let mut new_changes = self.changes.clone();
674-
new_changes.push(EnvChange::SetShellVar(
675-
name.to_string(),
676-
match op_type {
677-
PreArithmeticOp::Increment => (*val + 1.0).to_string(),
678-
PreArithmeticOp::Decrement => (*val - 1.0).to_string(),
679-
_ => todo!(
680-
"Unary arithmetic operation not implemented {:?}",
681-
op_type
682-
),
683-
},
684-
));
680+
if op_type == PreArithmeticOp::Increment
681+
|| op_type == PreArithmeticOp::Decrement
682+
{
683+
new_changes.push(EnvChange::SetShellVar(
684+
name.to_string(),
685+
match op_type {
686+
PreArithmeticOp::Increment => (*val + 1.0).to_string(),
687+
PreArithmeticOp::Decrement => (*val - 1.0).to_string(),
688+
_ => Err(miette!("No change to ENV need for: {}", self))?,
689+
},
690+
));
691+
}
692+
685693
Ok(ArithmeticResult {
686694
value: match op_type {
687695
PreArithmeticOp::Increment => {
@@ -690,10 +698,14 @@ impl ArithmeticResult {
690698
PreArithmeticOp::Decrement => {
691699
ArithmeticValue::Float(*val - 1.0)
692700
}
693-
_ => todo!(
694-
"Unary arithmetic operation not implemented {:?}",
695-
op_type
696-
),
701+
PreArithmeticOp::Plus => ArithmeticValue::Float((*val).abs()),
702+
PreArithmeticOp::Minus => ArithmeticValue::Float(-(*val).abs()),
703+
PreArithmeticOp::BitwiseNot => {
704+
ArithmeticValue::Integer(!(*val as i64))
705+
}
706+
PreArithmeticOp::LogicalNot => {
707+
ArithmeticValue::Float(if *val == 0.0 { 1.0 } else { 0.0 })
708+
}
697709
},
698710
changes: new_changes,
699711
})

crates/tests/src/lib.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -904,6 +904,30 @@ async fn arithmetic() {
904904
.assert_stdout("0\n")
905905
.run()
906906
.await;
907+
908+
TestBuilder::new()
909+
.command("echo $((a=1, +a))")
910+
.assert_stdout("1\n")
911+
.run()
912+
.await;
913+
914+
TestBuilder::new()
915+
.command("echo $((a=1, -a))")
916+
.assert_stdout("-1\n")
917+
.run()
918+
.await;
919+
920+
TestBuilder::new()
921+
.command("echo $((a=3, ~a))")
922+
.assert_stdout("-4\n")
923+
.run()
924+
.await;
925+
926+
TestBuilder::new()
927+
.command("echo $((a=0, !a))")
928+
.assert_stdout("1\n")
929+
.run()
930+
.await;
907931
}
908932

909933
#[tokio::test]

0 commit comments

Comments
 (0)