Skip to content

Commit 5ced079

Browse files
committed
Clean up
1 parent 681725a commit 5ced079

File tree

3 files changed

+173
-268
lines changed

3 files changed

+173
-268
lines changed

crates/deno_task_shell/src/parser.rs

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -426,16 +426,11 @@ pub enum ArithmeticPart {
426426
operator: BinaryOp,
427427
right: Box<ArithmeticPart>,
428428
},
429-
#[error("Invalid pre arithmetic expression")]
430-
PreArithmeticExpr {
431-
operator: PreArithmeticOp,
429+
#[error("Invalid unary arithmetic expression")]
430+
UnaryAritheticExpr {
431+
operator: UnaryArithmeticOp,
432432
operand: Box<ArithmeticPart>,
433433
},
434-
#[error("Invalid post arithmetic expression")]
435-
PostArithmeticExpr {
436-
operand: Box<ArithmeticPart>,
437-
operator: PostArithmeticOp,
438-
},
439434
#[error("Invalid variable")]
440435
Variable(String),
441436
#[error("Invalid number")]
@@ -498,6 +493,14 @@ pub enum PostArithmeticOp {
498493
Decrement, // --
499494
}
500495

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 UnaryArithmeticOp {
500+
Pre(PreArithmeticOp),
501+
Post(PostArithmeticOp),
502+
}
503+
501504
#[cfg_attr(feature = "serialization", derive(serde::Serialize))]
502505
#[cfg_attr(
503506
feature = "serialization",
@@ -1460,16 +1463,16 @@ fn unary_pre_arithmetic_expr(pair: Pair<Rule>) -> Result<ArithmeticPart> {
14601463
match first.as_rule() {
14611464
Rule::pre_arithmetic_op => {
14621465
let op = parse_pre_arithmetic_op(first)?;
1463-
Ok(ArithmeticPart::PreArithmeticExpr {
1464-
operator: op,
1465-
operand: (Box::new(operand)),
1466+
Ok(ArithmeticPart::UnaryAritheticExpr {
1467+
operator: UnaryArithmeticOp::Pre(op),
1468+
operand: Box::new(operand),
14661469
})
14671470
}
14681471
Rule::post_arithmetic_op => {
14691472
let op = parse_pre_arithmetic_op(first)?;
1470-
Ok(ArithmeticPart::PreArithmeticExpr {
1471-
operator: op,
1472-
operand: (Box::new(operand)),
1473+
Ok(ArithmeticPart::UnaryAritheticExpr {
1474+
operator: UnaryArithmeticOp::Pre(op),
1475+
operand: Box::new(operand),
14731476
})
14741477
}
14751478
_ => Err(miette!(
@@ -1502,17 +1505,17 @@ fn unary_post_arithmetic_expr(pair: Pair<Rule>) -> Result<ArithmeticPart> {
15021505
)),
15031506
}?;
15041507
let op = parse_post_arithmetic_op(second)?;
1505-
Ok(ArithmeticPart::PostArithmeticExpr {
1506-
operator: op,
1507-
operand: (Box::new(operand)),
1508+
Ok(ArithmeticPart::UnaryAritheticExpr {
1509+
operator: UnaryArithmeticOp::Post(op),
1510+
operand: Box::new(operand),
15081511
})
15091512
}
15101513

15111514
fn parse_pre_arithmetic_op(pair: Pair<Rule>) -> Result<PreArithmeticOp> {
15121515
let first = pair
15131516
.into_inner()
15141517
.next()
1515-
.ok_or_else(|| miette!("Expected increment or decreament operator"))?;
1518+
.ok_or_else(|| miette!("Expected increment or decrement operator"))?;
15161519
match first.as_rule() {
15171520
Rule::increment => Ok(PreArithmeticOp::Increment),
15181521
Rule::decrement => Ok(PreArithmeticOp::Decrement),
@@ -1531,7 +1534,7 @@ fn parse_post_arithmetic_op(pair: Pair<Rule>) -> Result<PostArithmeticOp> {
15311534
let first = pair
15321535
.into_inner()
15331536
.next()
1534-
.ok_or_else(|| miette!("Expected increment or decreament operator"))?;
1537+
.ok_or_else(|| miette!("Expected increment or decrement operator"))?;
15351538
match first.as_rule() {
15361539
Rule::increment => Ok(PostArithmeticOp::Increment),
15371540
Rule::decrement => Ok(PostArithmeticOp::Decrement),

crates/deno_task_shell/src/shell/execute.rs

Lines changed: 33 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -13,48 +13,23 @@ use thiserror::Error;
1313
use tokio::task::JoinHandle;
1414
use tokio_util::sync::CancellationToken;
1515

16-
use crate::parser::AssignmentOp;
17-
use crate::parser::BinaryOp;
18-
use crate::parser::Condition;
19-
use crate::parser::ConditionInner;
20-
use crate::parser::ElsePart;
21-
use crate::parser::IoFile;
22-
use crate::parser::PostArithmeticOp;
23-
use crate::parser::PreArithmeticOp;
24-
use crate::parser::RedirectOpInput;
25-
use crate::parser::RedirectOpOutput;
26-
use crate::parser::UnaryOp;
27-
use crate::shell::commands::ShellCommand;
28-
use crate::shell::commands::ShellCommandContext;
29-
use crate::shell::types::pipe;
30-
use crate::shell::types::ArithmeticResult;
31-
use crate::shell::types::ArithmeticValue;
32-
use crate::shell::types::EnvChange;
33-
use crate::shell::types::ExecuteResult;
34-
use crate::shell::types::FutureExecuteResult;
35-
use crate::shell::types::ShellPipeReader;
36-
use crate::shell::types::ShellPipeWriter;
37-
use crate::shell::types::ShellState;
38-
39-
use crate::parser::Arithmetic;
40-
use crate::parser::ArithmeticPart;
41-
use crate::parser::BinaryArithmeticOp;
42-
use crate::parser::Command;
43-
use crate::parser::CommandInner;
44-
use crate::parser::IfClause;
45-
use crate::parser::PipeSequence;
46-
use crate::parser::PipeSequenceOperator;
47-
use crate::parser::Pipeline;
48-
use crate::parser::PipelineInner;
49-
use crate::parser::Redirect;
50-
use crate::parser::RedirectFd;
51-
use crate::parser::RedirectOp;
52-
use crate::parser::Sequence;
53-
use crate::parser::SequentialList;
54-
use crate::parser::SimpleCommand;
55-
use crate::parser::Word;
56-
use crate::parser::WordPart;
57-
use crate::shell::types::WordEvalResult;
16+
use crate::parser::{
17+
AssignmentOp, BinaryOp, Condition, ConditionInner, ElsePart, IoFile,
18+
RedirectOpInput, RedirectOpOutput, UnaryArithmeticOp, UnaryOp,
19+
};
20+
use crate::shell::commands::{ShellCommand, ShellCommandContext};
21+
use crate::shell::types::{
22+
pipe, ArithmeticResult, ArithmeticValue, EnvChange, ExecuteResult,
23+
FutureExecuteResult, ShellPipeReader, ShellPipeWriter, ShellState,
24+
WordEvalResult,
25+
};
26+
27+
use crate::parser::{
28+
Arithmetic, ArithmeticPart, BinaryArithmeticOp, Command, CommandInner,
29+
IfClause, PipeSequence, PipeSequenceOperator, Pipeline, PipelineInner,
30+
Redirect, RedirectFd, RedirectOp, Sequence, SequentialList, SimpleCommand,
31+
Word, WordPart,
32+
};
5833

5934
use super::command::execute_unresolved_command_name;
6035
use super::command::UnresolvedCommandName;
@@ -643,13 +618,17 @@ async fn evaluate_arithmetic_part(
643618
let rhs = Box::pin(evaluate_arithmetic_part(right, state)).await?;
644619
apply_conditional_binary_op(lhs, operator, rhs)
645620
}
646-
ArithmeticPart::PostArithmeticExpr { operand, operator } => {
621+
// ArithmeticPart::PostArithmeticExpr { operand, operator } => {
622+
// let val = Box::pin(evaluate_arithmetic_part(operand, state)).await?;
623+
// apply_post_op(state, *operator, val, operand)
624+
// }
625+
// ArithmeticPart::PreArithmeticExpr { operator, operand } => {
626+
// let val = Box::pin(evaluate_arithmetic_part(operand, state)).await?;
627+
// apply_pre_op(state, *operator, val, operand)
628+
// }
629+
ArithmeticPart::UnaryAritheticExpr { operator, operand } => {
647630
let val = Box::pin(evaluate_arithmetic_part(operand, state)).await?;
648-
apply_post_op(state, *operator, val, operand)
649-
}
650-
ArithmeticPart::PreArithmeticExpr { operator, operand } => {
651-
let val = Box::pin(evaluate_arithmetic_part(operand, state)).await?;
652-
apply_pre_op(state, *operator, val, operand)
631+
apply_unary_op(state, *operator, val, operand)
653632
}
654633
ArithmeticPart::Variable(name) => state
655634
.get_var(name)
@@ -732,51 +711,16 @@ fn apply_conditional_binary_op(
732711
}
733712
}
734713

735-
fn apply_pre_op(
736-
state: &mut ShellState,
737-
op: PreArithmeticOp,
738-
val: ArithmeticResult,
739-
operand: &ArithmeticPart,
740-
) -> Result<ArithmeticResult, Error> {
741-
match op {
742-
PreArithmeticOp::Increment => {
743-
let result = val.pre_increment(operand)?;
744-
let result_clone = result.clone();
745-
state.apply_changes(&result_clone.changes);
746-
Ok(result)
747-
}
748-
PreArithmeticOp::Decrement => {
749-
let result = val.pre_decreament(operand)?;
750-
let result_clone = result.clone();
751-
state.apply_changes(&result_clone.changes);
752-
Ok(result)
753-
}
754-
_ => {
755-
todo!("Pre arithmetic operator {:?} is not implemented", op)
756-
}
757-
}
758-
}
759-
760-
fn apply_post_op(
714+
fn apply_unary_op(
761715
state: &mut ShellState,
762-
op: PostArithmeticOp,
716+
op: UnaryArithmeticOp,
763717
val: ArithmeticResult,
764718
operand: &ArithmeticPart,
765719
) -> Result<ArithmeticResult, Error> {
766-
match op {
767-
PostArithmeticOp::Increment => {
768-
let result = val.post_increment(operand)?;
769-
let result_clone = result.clone();
770-
state.apply_changes(&result_clone.changes);
771-
Ok(result)
772-
}
773-
PostArithmeticOp::Decrement => {
774-
let result = val.post_decreament(operand)?;
775-
let result_clone = result.clone();
776-
state.apply_changes(&result_clone.changes);
777-
Ok(result)
778-
}
779-
}
720+
let result = val.unary_op(operand, op)?;
721+
let result_clone = result.clone();
722+
state.apply_changes(&result_clone.changes);
723+
Ok(result)
780724
}
781725

782726
async fn execute_pipe_sequence(

0 commit comments

Comments
 (0)