Skip to content

Commit 237ac4e

Browse files
committed
Combined unary and pre expr
1 parent 781b0b8 commit 237ac4e

File tree

4 files changed

+15
-98
lines changed

4 files changed

+15
-98
lines changed

crates/deno_task_shell/src/grammar.pest

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,6 @@ unary_arithmetic_expr = !{
208208

209209
unary_pre_arithmetic_expr = !{
210210
(post_arithmetic_op | pre_arithmetic_op) ~ (parentheses_expr | VARIABLE | NUMBER)
211-
212211
}
213212

214213
unary_post_arithmetic_expr = !{

crates/deno_task_shell/src/parser.rs

Lines changed: 12 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -426,11 +426,6 @@ pub enum ArithmeticPart {
426426
operator: BinaryOp,
427427
right: Box<ArithmeticPart>,
428428
},
429-
#[error("Invalid unary arithmetic expression")]
430-
UnaryArithmeticExpr {
431-
operator: UnaryArithmeticOp,
432-
operand: Box<ArithmeticPart>,
433-
},
434429
#[error("Invalid pre arithmetic expression")]
435430
PreArithmeticExpr {
436431
operator: PreArithmeticOp,
@@ -486,21 +481,15 @@ pub enum AssignmentOp {
486481
#[cfg_attr(feature = "serialization", derive(serde::Serialize))]
487482
#[cfg_attr(feature = "serialization", serde(rename_all = "camelCase"))]
488483
#[derive(Debug, Clone, PartialEq, Eq, Copy)]
489-
pub enum UnaryArithmeticOp {
484+
pub enum PreArithmeticOp {
485+
Increment, // ++
486+
Decrement, // --
490487
Plus, // +
491488
Minus, // -
492489
LogicalNot, // !
493490
BitwiseNot, // ~
494491
}
495492

496-
#[cfg_attr(feature = "serialization", derive(serde::Serialize))]
497-
#[cfg_attr(feature = "serialization", serde(rename_all = "camelCase"))]
498-
#[derive(Debug, Clone, PartialEq, Eq, Copy)]
499-
pub enum PreArithmeticOp {
500-
Increment, // ++
501-
Decrement, // --
502-
}
503-
504493
#[cfg_attr(feature = "serialization", derive(serde::Serialize))]
505494
#[cfg_attr(feature = "serialization", serde(rename_all = "camelCase"))]
506495
#[derive(Debug, Clone, PartialEq, Eq, Copy)]
@@ -1470,16 +1459,16 @@ fn unary_pre_arithmetic_expr(pair: Pair<Rule>) -> Result<ArithmeticPart> {
14701459

14711460
match first.as_rule() {
14721461
Rule::pre_arithmetic_op => {
1473-
let op = parse_unary_arithmetic_op(first)?;
1474-
Ok(ArithmeticPart::UnaryArithmeticExpr {
1475-
operator: (op),
1462+
let op = parse_pre_arithmetic_op(first)?;
1463+
Ok(ArithmeticPart::PreArithmeticExpr {
1464+
operator: op,
14761465
operand: (Box::new(operand)),
14771466
})
14781467
}
14791468
Rule::post_arithmetic_op => {
14801469
let op = parse_pre_arithmetic_op(first)?;
14811470
Ok(ArithmeticPart::PreArithmeticExpr {
1482-
operator: (op),
1471+
operator: op,
14831472
operand: (Box::new(operand)),
14841473
})
14851474
}
@@ -1514,29 +1503,11 @@ fn unary_post_arithmetic_expr(pair: Pair<Rule>) -> Result<ArithmeticPart> {
15141503
}?;
15151504
let op = parse_post_arithmetic_op(second)?;
15161505
Ok(ArithmeticPart::PostArithmeticExpr {
1517-
operator: (op),
1506+
operator: op,
15181507
operand: (Box::new(operand)),
15191508
})
15201509
}
15211510

1522-
fn parse_unary_arithmetic_op(pair: Pair<Rule>) -> Result<UnaryArithmeticOp> {
1523-
let first = pair
1524-
.into_inner()
1525-
.next()
1526-
.ok_or_else(|| miette!("Expected unary operator"))?;
1527-
1528-
match first.as_rule() {
1529-
Rule::add => Ok(UnaryArithmeticOp::Plus),
1530-
Rule::subtract => Ok(UnaryArithmeticOp::Minus),
1531-
Rule::logical_not => Ok(UnaryArithmeticOp::LogicalNot),
1532-
Rule::bitwise_not => Ok(UnaryArithmeticOp::BitwiseNot),
1533-
_ => Err(miette!(
1534-
"Unexpected rule in unary arithmetic operator: {:?}",
1535-
first.as_rule()
1536-
)),
1537-
}
1538-
}
1539-
15401511
fn parse_pre_arithmetic_op(pair: Pair<Rule>) -> Result<PreArithmeticOp> {
15411512
let first = pair
15421513
.into_inner()
@@ -1545,6 +1516,10 @@ fn parse_pre_arithmetic_op(pair: Pair<Rule>) -> Result<PreArithmeticOp> {
15451516
match first.as_rule() {
15461517
Rule::increment => Ok(PreArithmeticOp::Increment),
15471518
Rule::decrement => Ok(PreArithmeticOp::Decrement),
1519+
Rule::add => Ok(PreArithmeticOp::Plus),
1520+
Rule::subtract => Ok(PreArithmeticOp::Minus),
1521+
Rule::logical_not => Ok(PreArithmeticOp::LogicalNot),
1522+
Rule::bitwise_not => Ok(PreArithmeticOp::BitwiseNot),
15481523
_ => Err(miette!(
15491524
"Unexpected rule in post arithmetic operator: {:?}",
15501525
first.as_rule()

crates/deno_task_shell/src/shell/execute.rs

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ use crate::parser::RedirectOp;
5252
use crate::parser::Sequence;
5353
use crate::parser::SequentialList;
5454
use crate::parser::SimpleCommand;
55-
use crate::parser::UnaryArithmeticOp;
5655
use crate::parser::Word;
5756
use crate::parser::WordPart;
5857
use crate::shell::types::WordEvalResult;
@@ -644,10 +643,6 @@ async fn evaluate_arithmetic_part(
644643
let rhs = Box::pin(evaluate_arithmetic_part(right, state)).await?;
645644
apply_conditional_binary_op(lhs, operator, rhs)
646645
}
647-
ArithmeticPart::UnaryArithmeticExpr { operator, operand } => {
648-
let val = Box::pin(evaluate_arithmetic_part(operand, state)).await?;
649-
apply_unary_op(*operator, val)
650-
}
651646
ArithmeticPart::PostArithmeticExpr { operand, operator } => {
652647
let val = Box::pin(evaluate_arithmetic_part(operand, state)).await?;
653648
apply_post_op(state, *operator, val, operand)
@@ -737,22 +732,6 @@ fn apply_conditional_binary_op(
737732
}
738733
}
739734

740-
fn apply_unary_op(
741-
op: UnaryArithmeticOp,
742-
val: ArithmeticResult,
743-
) -> Result<ArithmeticResult, Error> {
744-
match op {
745-
UnaryArithmeticOp::Plus => Ok(val),
746-
UnaryArithmeticOp::Minus => val.checked_neg(),
747-
UnaryArithmeticOp::LogicalNot => Ok(if val.is_zero() {
748-
ArithmeticResult::new(ArithmeticValue::Integer(1))
749-
} else {
750-
ArithmeticResult::new(ArithmeticValue::Integer(0))
751-
}),
752-
UnaryArithmeticOp::BitwiseNot => val.checked_not(),
753-
}
754-
}
755-
756735
fn apply_pre_op(
757736
state: &mut ShellState,
758737
op: PreArithmeticOp,
@@ -772,6 +751,9 @@ fn apply_pre_op(
772751
state.apply_changes(&result_clone.changes);
773752
Ok(result)
774753
}
754+
_ => {
755+
todo!("Pre arithmetic operator {:?} is not implemented", op)
756+
}
775757
}
776758
}
777759

crates/deno_task_shell/src/shell/types.rs

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,45 +1044,6 @@ impl ArithmeticResult {
10441044
})
10451045
}
10461046

1047-
pub fn checked_neg(&self) -> Result<ArithmeticResult, Error> {
1048-
let result = match &self.value {
1049-
ArithmeticValue::Integer(val) => val
1050-
.checked_neg()
1051-
.map(ArithmeticValue::Integer)
1052-
.ok_or_else(|| anyhow::anyhow!("Integer overflow: -{}", val))?,
1053-
ArithmeticValue::Float(val) => {
1054-
let result = -val;
1055-
if result.is_finite() {
1056-
ArithmeticValue::Float(result)
1057-
} else {
1058-
return Err(anyhow::anyhow!("Float overflow: -{}", val));
1059-
}
1060-
}
1061-
};
1062-
1063-
Ok(ArithmeticResult {
1064-
value: result,
1065-
changes: self.changes.clone(),
1066-
})
1067-
}
1068-
1069-
pub fn checked_not(&self) -> Result<ArithmeticResult, Error> {
1070-
let result = match &self.value {
1071-
ArithmeticValue::Integer(val) => ArithmeticValue::Integer(!val),
1072-
ArithmeticValue::Float(_) => {
1073-
return Err(anyhow::anyhow!(
1074-
"Invalid arithmetic result type for bitwise NOT: {}",
1075-
self
1076-
))
1077-
}
1078-
};
1079-
1080-
Ok(ArithmeticResult {
1081-
value: result,
1082-
changes: self.changes.clone(),
1083-
})
1084-
}
1085-
10861047
pub fn checked_shl(
10871048
&self,
10881049
other: &ArithmeticResult,

0 commit comments

Comments
 (0)