Skip to content

Commit 15e78cc

Browse files
committed
Added variable incrementation
1 parent 2870dfb commit 15e78cc

File tree

4 files changed

+287
-15
lines changed

4 files changed

+287
-15
lines changed

crates/deno_task_shell/src/parser.rs

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,11 @@ pub enum ArithmeticPart {
431431
operator: UnaryArithmeticOp,
432432
operand: Box<ArithmeticPart>,
433433
},
434+
#[error("Invalid pre arithmetic expression")]
435+
PreArithmeticExpr {
436+
operator: PreArithmeticOp,
437+
operand: Box<ArithmeticPart>,
438+
},
434439
#[error("Invalid post arithmetic expression")]
435440
PostArithmeticExpr {
436441
operand: Box<ArithmeticPart>,
@@ -490,7 +495,15 @@ pub enum UnaryArithmeticOp {
490495

491496
#[cfg_attr(feature = "serialization", derive(serde::Serialize))]
492497
#[cfg_attr(feature = "serialization", serde(rename_all = "camelCase"))]
493-
#[derive(Debug, Clone, PartialEq, Eq)]
498+
#[derive(Debug, Clone, PartialEq, Eq, Copy)]
499+
pub enum PreArithmeticOp {
500+
Increment, // ++
501+
Decrement, // --
502+
}
503+
504+
#[cfg_attr(feature = "serialization", derive(serde::Serialize))]
505+
#[cfg_attr(feature = "serialization", serde(rename_all = "camelCase"))]
506+
#[derive(Debug, Clone, PartialEq, Eq, Copy)]
494507
pub enum PostArithmeticOp {
495508
Increment, // ++
496509
Decrement, // --
@@ -1419,7 +1432,9 @@ fn parse_arithmetic_expr(pair: Pair<Rule>) -> Result<ArithmeticPart> {
14191432

14201433
fn parse_unary_arithmetic_expr(pair: Pair<Rule>) -> Result<ArithmeticPart> {
14211434
let mut inner = pair.into_inner();
1422-
let first = inner.next().ok_or_else(|| miette!("Expected unary operator"))?;
1435+
let first = inner
1436+
.next()
1437+
.ok_or_else(|| miette!("Expected unary operator"))?;
14231438

14241439
match first.as_rule() {
14251440
Rule::unary_pre_arithmetic_expr => unary_pre_arithmetic_expr(first),
@@ -1433,7 +1448,9 @@ fn parse_unary_arithmetic_expr(pair: Pair<Rule>) -> Result<ArithmeticPart> {
14331448

14341449
fn unary_pre_arithmetic_expr(pair: Pair<Rule>) -> Result<ArithmeticPart> {
14351450
let mut inner = pair.into_inner();
1436-
let first = inner.next().ok_or_else(|| miette!("Expected unary pre operator"))?;
1451+
let first = inner
1452+
.next()
1453+
.ok_or_else(|| miette!("Expected unary pre operator"))?;
14371454
let second = inner.next().ok_or_else(|| miette!("Expected operand"))?;
14381455
let operand = match second.as_rule() {
14391456
Rule::parentheses_expr => {
@@ -1458,14 +1475,14 @@ fn unary_pre_arithmetic_expr(pair: Pair<Rule>) -> Result<ArithmeticPart> {
14581475
operator: (op),
14591476
operand: (Box::new(operand)),
14601477
})
1461-
},
1478+
}
14621479
Rule::post_arithmetic_op => {
1463-
let op = parse_post_arithmetic_op(first)?;
1464-
Ok(ArithmeticPart::PostArithmeticExpr {
1480+
let op = parse_pre_arithmetic_op(first)?;
1481+
Ok(ArithmeticPart::PreArithmeticExpr {
14651482
operator: (op),
14661483
operand: (Box::new(operand)),
14671484
})
1468-
},
1485+
}
14691486
_ => Err(miette!(
14701487
"Unexpected rule in unary arithmetic operator: {:?}",
14711488
first.as_rule()
@@ -1475,7 +1492,9 @@ fn unary_pre_arithmetic_expr(pair: Pair<Rule>) -> Result<ArithmeticPart> {
14751492

14761493
fn unary_post_arithmetic_expr(pair: Pair<Rule>) -> Result<ArithmeticPart> {
14771494
let mut inner = pair.into_inner();
1478-
let first = inner.next().ok_or_else(|| miette!("Expected unary post operator"))?;
1495+
let first = inner
1496+
.next()
1497+
.ok_or_else(|| miette!("Expected unary post operator"))?;
14791498
let second = inner.next().ok_or_else(|| miette!("Expected operand"))?;
14801499

14811500
let operand = match first.as_rule() {
@@ -1501,7 +1520,10 @@ fn unary_post_arithmetic_expr(pair: Pair<Rule>) -> Result<ArithmeticPart> {
15011520
}
15021521

15031522
fn parse_unary_arithmetic_op(pair: Pair<Rule>) -> Result<UnaryArithmeticOp> {
1504-
let first = pair.into_inner().next().ok_or_else(|| miette!("Expected unary operator"))?;
1523+
let first = pair
1524+
.into_inner()
1525+
.next()
1526+
.ok_or_else(|| miette!("Expected unary operator"))?;
15051527

15061528
match first.as_rule() {
15071529
Rule::add => Ok(UnaryArithmeticOp::Plus),
@@ -1515,8 +1537,26 @@ fn parse_unary_arithmetic_op(pair: Pair<Rule>) -> Result<UnaryArithmeticOp> {
15151537
}
15161538
}
15171539

1540+
fn parse_pre_arithmetic_op(pair: Pair<Rule>) -> Result<PreArithmeticOp> {
1541+
let first = pair
1542+
.into_inner()
1543+
.next()
1544+
.ok_or_else(|| miette!("Expected increament or decreament operator"))?;
1545+
match first.as_rule() {
1546+
Rule::increment => Ok(PreArithmeticOp::Increment),
1547+
Rule::decrement => Ok(PreArithmeticOp::Decrement),
1548+
_ => Err(miette!(
1549+
"Unexpected rule in post arithmetic operator: {:?}",
1550+
first.as_rule()
1551+
)),
1552+
}
1553+
}
1554+
15181555
fn parse_post_arithmetic_op(pair: Pair<Rule>) -> Result<PostArithmeticOp> {
1519-
let first = pair.into_inner().next().ok_or_else(|| miette!("Expected increament or decreament operator"))?;
1556+
let first = pair
1557+
.into_inner()
1558+
.next()
1559+
.ok_or_else(|| miette!("Expected increament or decreament operator"))?;
15201560
match first.as_rule() {
15211561
Rule::increment => Ok(PostArithmeticOp::Increment),
15221562
Rule::decrement => Ok(PostArithmeticOp::Decrement),

crates/deno_task_shell/src/shell/execute.rs

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ use crate::parser::Condition;
1919
use crate::parser::ConditionInner;
2020
use crate::parser::ElsePart;
2121
use crate::parser::IoFile;
22+
use crate::parser::PostArithmeticOp;
23+
use crate::parser::PreArithmeticOp;
2224
use crate::parser::RedirectOpInput;
2325
use crate::parser::RedirectOpOutput;
2426
use crate::parser::UnaryOp;
@@ -599,7 +601,10 @@ async fn evaluate_arithmetic_part(
599601
}?
600602
}
601603
};
602-
state.apply_env_var(name, &applied_value.to_string());
604+
state.apply_change(&EnvChange::SetShellVar(
605+
(&name).to_string(),
606+
applied_value.value.to_string(),
607+
));
603608
Ok(
604609
applied_value
605610
.clone()
@@ -643,9 +648,13 @@ async fn evaluate_arithmetic_part(
643648
let val = Box::pin(evaluate_arithmetic_part(operand, state)).await?;
644649
apply_unary_op(*operator, val)
645650
}
646-
ArithmeticPart::PostArithmeticExpr { operand, .. } => {
651+
ArithmeticPart::PostArithmeticExpr { operand, operator } => {
647652
let val = Box::pin(evaluate_arithmetic_part(operand, state)).await?;
648-
Ok(val)
653+
apply_post_op(state, *operator, val, operand)
654+
}
655+
ArithmeticPart::PreArithmeticExpr { operator, operand } => {
656+
let val = Box::pin(evaluate_arithmetic_part(operand, state)).await?;
657+
apply_pre_op(state, *operator, val, operand)
649658
}
650659
ArithmeticPart::Variable(name) => state
651660
.get_var(name)
@@ -744,6 +753,50 @@ fn apply_unary_op(
744753
}
745754
}
746755

756+
fn apply_pre_op(
757+
state: &mut ShellState,
758+
op: PreArithmeticOp,
759+
val: ArithmeticResult,
760+
operand: &ArithmeticPart,
761+
) -> Result<ArithmeticResult, Error> {
762+
match op {
763+
PreArithmeticOp::Increment => {
764+
let result = val.pre_increament(operand)?;
765+
let result_clone = result.clone();
766+
state.apply_changes(&result_clone.changes);
767+
Ok(result)
768+
}
769+
PreArithmeticOp::Decrement => {
770+
let result = val.pre_decreament(operand)?;
771+
let result_clone = result.clone();
772+
state.apply_changes(&result_clone.changes);
773+
Ok(result)
774+
}
775+
}
776+
}
777+
778+
fn apply_post_op(
779+
state: &mut ShellState,
780+
op: PostArithmeticOp,
781+
val: ArithmeticResult,
782+
operand: &ArithmeticPart,
783+
) -> Result<ArithmeticResult, Error> {
784+
match op {
785+
PostArithmeticOp::Increment => {
786+
let result = val.post_increament(operand)?;
787+
let result_clone = result.clone();
788+
state.apply_changes(&result_clone.changes);
789+
Ok(result)
790+
}
791+
PostArithmeticOp::Decrement => {
792+
let result = val.post_decreament(operand)?;
793+
let result_clone = result.clone();
794+
state.apply_changes(&result_clone.changes);
795+
Ok(result)
796+
}
797+
}
798+
}
799+
747800
async fn execute_pipe_sequence(
748801
pipe_sequence: PipeSequence,
749802
state: ShellState,
@@ -1350,6 +1403,7 @@ fn evaluate_word_parts(
13501403
if !current_text.is_empty() {
13511404
result.extend(evaluate_word_text(state, current_text, is_quoted)?);
13521405
}
1406+
result.with_changes(changes);
13531407
Ok(result)
13541408
}
13551409
.boxed_local()

0 commit comments

Comments
 (0)