From 197a89aa0c3e513fa1c8f9b143f1468f1b7b10a9 Mon Sep 17 00:00:00 2001
From: prsabahrami
Date: Tue, 8 Oct 2024 00:15:31 -0400
Subject: [PATCH] Add state modification in arithmetic ops
---
crates/deno_task_shell/src/parser.rs | 2 --
crates/deno_task_shell/src/shell/execute.rs | 30 ++++++++++-----------
crates/deno_task_shell/src/shell/types.rs | 17 ++++++++----
crates/tests/src/lib.rs | 6 +++++
scripts/test.sh | 2 +-
5 files changed, 33 insertions(+), 24 deletions(-)
diff --git a/crates/deno_task_shell/src/parser.rs b/crates/deno_task_shell/src/parser.rs
index bc3f263..0502dfd 100644
--- a/crates/deno_task_shell/src/parser.rs
+++ b/crates/deno_task_shell/src/parser.rs
@@ -1493,7 +1493,6 @@ fn parse_post_arithmetic_op(pair: Pair) -> Result {
}
fn parse_variable_expansion(part: Pair) -> Result {
- println!("{:?}", part);
let mut inner = part.into_inner();
let variable = inner
.next()
@@ -1547,7 +1546,6 @@ fn parse_variable_expansion(part: Pair) -> Result {
} else {
None
};
- println!("PARSED MOD: {:?}", parsed_modifier);
Ok(WordPart::Variable(variable_name, parsed_modifier))
}
diff --git a/crates/deno_task_shell/src/shell/execute.rs b/crates/deno_task_shell/src/shell/execute.rs
index 4864019..052b87d 100644
--- a/crates/deno_task_shell/src/shell/execute.rs
+++ b/crates/deno_task_shell/src/shell/execute.rs
@@ -600,7 +600,9 @@ async fn evaluate_arithmetic(
) -> Result {
let mut result = ArithmeticResult::new(ArithmeticValue::Integer(0));
for part in &arithmetic.parts {
- result = Box::pin(evaluate_arithmetic_part(part, state)).await?;
+ let part_result = Box::pin(evaluate_arithmetic_part(part, state)).await?;
+ result.set_value(part_result.value);
+ result.with_changes(part_result.changes);
}
Ok(result)
}
@@ -615,7 +617,7 @@ async fn evaluate_arithmetic_part(
}
ArithmeticPart::VariableAssignment { name, op, value } => {
let val = Box::pin(evaluate_arithmetic_part(value, state)).await?;
- let applied_value = match op {
+ let mut applied_value = match op {
AssignmentOp::Assign => val.clone(),
_ => {
let var = state
@@ -640,14 +642,11 @@ async fn evaluate_arithmetic_part(
}
};
state.apply_env_var(name, &applied_value.to_string());
- Ok(
- applied_value
- .clone()
- .with_changes(vec![EnvChange::SetShellVar(
- name.clone(),
- applied_value.to_string(),
- )]),
- )
+ applied_value.with_changes(vec![EnvChange::SetShellVar(
+ name.clone(),
+ applied_value.to_string(),
+ )]);
+ Ok(applied_value)
}
ArithmeticPart::TripleConditionalExpr {
condition,
@@ -1072,8 +1071,7 @@ async fn execute_simple_command(
}
};
state.apply_env_var(&env_var.name, &word_result.value);
- let env_changes = word_result.changes;
- changes.extend(env_changes);
+ changes.extend(word_result.changes);
}
let result = execute_command_args(args, state, stdin, stdout, stderr).await;
match result {
@@ -1358,7 +1356,7 @@ fn evaluate_word_parts(
stderr: ShellPipeWriter,
) -> LocalBoxFuture> {
// recursive async, so requires boxing
- let mut changes: Vec = Vec::new();
+ // let mut changes: Vec = Vec::new();
async move {
let mut result = WordPartsResult::new(Vec::new(), Vec::new());
@@ -1376,7 +1374,7 @@ fn evaluate_word_parts(
.apply(value.as_ref(), state, stdin.clone(), stderr.clone())
.await?;
if let Some(env_changes) = env_changes {
- changes.extend(env_changes);
+ result.with_changes(env_changes);
}
Ok(Some(text))
} else if let Some(val) = value {
@@ -1410,7 +1408,7 @@ fn evaluate_word_parts(
value,
changes: env_changes,
} = res;
- changes.extend(env_changes);
+ result.with_changes(env_changes);
current_text.push(TextPart::Quoted(value.join(" ")));
continue;
}
@@ -1432,7 +1430,7 @@ fn evaluate_word_parts(
let arithmetic_result =
execute_arithmetic_expression(arithmetic, state).await?;
current_text.push(TextPart::Text(arithmetic_result.to_string()));
- changes.extend(arithmetic_result.changes);
+ result.with_changes(arithmetic_result.changes);
continue;
}
WordPart::ExitStatus => {
diff --git a/crates/deno_task_shell/src/shell/types.rs b/crates/deno_task_shell/src/shell/types.rs
index fbefa4a..4c9cecf 100644
--- a/crates/deno_task_shell/src/shell/types.rs
+++ b/crates/deno_task_shell/src/shell/types.rs
@@ -596,6 +596,14 @@ impl ArithmeticResult {
}
}
+ pub fn with_changes(&mut self, changes: Vec) {
+ self.changes.extend(changes);
+ }
+
+ pub fn set_value(&mut self, value: ArithmeticValue) {
+ self.value = value;
+ }
+
pub fn checked_add(
&self,
other: &ArithmeticResult,
@@ -1089,11 +1097,6 @@ impl ArithmeticResult {
changes,
})
}
-
- pub fn with_changes(mut self, changes: Vec) -> Self {
- self.changes = changes;
- self
- }
}
impl From for ArithmeticResult {
@@ -1135,6 +1138,10 @@ impl WordPartsResult {
pub fn join(&self, sep: &str) -> String {
self.value.join(sep)
}
+
+ pub fn with_changes(&mut self, changes: Vec) {
+ self.changes.extend(changes);
+ }
}
impl From for String {
diff --git a/crates/tests/src/lib.rs b/crates/tests/src/lib.rs
index d7c929c..d362d82 100644
--- a/crates/tests/src/lib.rs
+++ b/crates/tests/src/lib.rs
@@ -107,6 +107,12 @@ async fn commands() {
.await;
TestBuilder::new().command("unset").run().await;
+
+ TestBuilder::new()
+ .command("a=1 && echo $((a=2, a + 1)) && echo $a")
+ .assert_stdout("3\n2\n")
+ .run()
+ .await;
}
#[tokio::test]
diff --git a/scripts/test.sh b/scripts/test.sh
index f7d866e..bdebafb 100644
--- a/scripts/test.sh
+++ b/scripts/test.sh
@@ -1 +1 @@
-(export TEST=1) && echo $TEST
+a=1 && echo $((a=2, a + 1)) && echo $a