Skip to content

Commit

Permalink
Add state modification in arithmetic ops
Browse files Browse the repository at this point in the history
  • Loading branch information
prsabahrami committed Oct 8, 2024
1 parent 8c41141 commit 197a89a
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 24 deletions.
2 changes: 0 additions & 2 deletions crates/deno_task_shell/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1493,7 +1493,6 @@ fn parse_post_arithmetic_op(pair: Pair<Rule>) -> Result<PostArithmeticOp> {
}

fn parse_variable_expansion(part: Pair<Rule>) -> Result<WordPart> {
println!("{:?}", part);
let mut inner = part.into_inner();
let variable = inner
.next()
Expand Down Expand Up @@ -1547,7 +1546,6 @@ fn parse_variable_expansion(part: Pair<Rule>) -> Result<WordPart> {
} else {
None
};
println!("PARSED MOD: {:?}", parsed_modifier);
Ok(WordPart::Variable(variable_name, parsed_modifier))
}

Expand Down
30 changes: 14 additions & 16 deletions crates/deno_task_shell/src/shell/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,9 @@ async fn evaluate_arithmetic(
) -> Result<ArithmeticResult, Error> {
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)
}
Expand All @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -1358,7 +1356,7 @@ fn evaluate_word_parts(
stderr: ShellPipeWriter,
) -> LocalBoxFuture<Result<WordPartsResult, EvaluateWordTextError>> {
// recursive async, so requires boxing
let mut changes: Vec<EnvChange> = Vec::new();
// let mut changes: Vec<EnvChange> = Vec::new();

async move {
let mut result = WordPartsResult::new(Vec::new(), Vec::new());
Expand All @@ -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 {
Expand Down Expand Up @@ -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;
}
Expand All @@ -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 => {
Expand Down
17 changes: 12 additions & 5 deletions crates/deno_task_shell/src/shell/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,14 @@ impl ArithmeticResult {
}
}

pub fn with_changes(&mut self, changes: Vec<EnvChange>) {
self.changes.extend(changes);
}

pub fn set_value(&mut self, value: ArithmeticValue) {
self.value = value;
}

pub fn checked_add(
&self,
other: &ArithmeticResult,
Expand Down Expand Up @@ -1089,11 +1097,6 @@ impl ArithmeticResult {
changes,
})
}

pub fn with_changes(mut self, changes: Vec<EnvChange>) -> Self {
self.changes = changes;
self
}
}

impl From<String> for ArithmeticResult {
Expand Down Expand Up @@ -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<EnvChange>) {
self.changes.extend(changes);
}
}

impl From<WordPartsResult> for String {
Expand Down
6 changes: 6 additions & 0 deletions crates/tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion scripts/test.sh
Original file line number Diff line number Diff line change
@@ -1 +1 @@
(export TEST=1) && echo $TEST
a=1 && echo $((a=2, a + 1)) && echo $a

0 comments on commit 197a89a

Please sign in to comment.