Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: remove unneeded string cloning for arithmetic eval #324

Merged
merged 1 commit into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 32 additions & 17 deletions brush-core/src/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,25 +52,40 @@ pub trait ExpandAndEvaluate {

impl ExpandAndEvaluate for ast::UnexpandedArithmeticExpr {
async fn eval(&self, shell: &mut Shell, trace_if_needed: bool) -> Result<i64, EvalError> {
// Per documentation, first shell-expand it.
let expanded_self = expansion::basic_expand_str_without_tilde(shell, self.value.as_str())
.await
.map_err(|_e| EvalError::FailedToExpandExpression)?;

// Now parse.
let expr = brush_parser::arithmetic::parse(&expanded_self)
.map_err(|_e| EvalError::ParseError(expanded_self))?;

// Trace if applicable.
if trace_if_needed && shell.options.print_commands_and_arguments {
shell
.trace_command(std::format!("(( {expr} ))"))
.map_err(|_err| EvalError::TraceError)?;
}
expand_and_eval(shell, self.value.as_str(), trace_if_needed).await
}
}

// Now evaluate.
expr.eval(shell)
/// Evaluate the given arithmetic expression, returning the resulting numeric value.
///
/// # Arguments
///
/// * `shell` - The shell to use for evaluation.
/// * `expr` - The unexpanded arithmetic expression to evaluate.
/// * `trace_if_needed` - Whether to trace the evaluation.
pub(crate) async fn expand_and_eval(
shell: &mut Shell,
expr: &str,
trace_if_needed: bool,
) -> Result<i64, EvalError> {
// Per documentation, first shell-expand it.
let expanded_self = expansion::basic_expand_str_without_tilde(shell, expr)
reubeno marked this conversation as resolved.
Show resolved Hide resolved
.await
.map_err(|_e| EvalError::FailedToExpandExpression)?;

// Now parse.
let expr = brush_parser::arithmetic::parse(&expanded_self)
.map_err(|_e| EvalError::ParseError(expanded_self))?;

// Trace if applicable.
if trace_if_needed && shell.options.print_commands_and_arguments {
shell
.trace_command(std::format!("(( {expr} ))"))
.map_err(|_err| EvalError::TraceError)?;
}

// Now evaluate.
expr.eval(shell)
}

/// Trait implemented by evaluatable arithmetic expressions.
Expand Down
8 changes: 4 additions & 4 deletions brush-core/src/expansion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use brush_parser::word::ParameterTransformOp;
use brush_parser::word::SubstringMatchKind;
use itertools::Itertools;

use crate::arithmetic;
use crate::arithmetic::ExpandAndEvaluate;
use crate::commands;
use crate::env;
Expand Down Expand Up @@ -1334,10 +1335,9 @@ impl<'a> WordExpander<'a> {
let index_to_use = if for_set_associative_array {
self.basic_expand_to_str(index).await?
} else {
let index_expr = ast::UnexpandedArithmeticExpr {
value: index.to_owned(),
};
self.expand_arithmetic_expr(index_expr).await?
arithmetic::expand_and_eval(self.shell, index, false)
.await?
.to_string()
};

Ok(index_to_use)
Expand Down
63 changes: 13 additions & 50 deletions brush-core/src/extendedtests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ use brush_parser::ast;
use std::path::Path;

use crate::{
arithmetic::ExpandAndEvaluate,
env, error, escape, expansion, namedoptions, patterns,
arithmetic, env, error, escape, expansion, namedoptions, patterns,
sys::{
fs::{MetadataExt, PathExt},
users,
Expand Down Expand Up @@ -269,14 +268,8 @@ async fn apply_binary_predicate(
Ok(left > right)
}
ast::BinaryPredicate::ArithmeticEqualTo => {
let unexpanded_left = ast::UnexpandedArithmeticExpr {
value: left.value.clone(),
};
let unexpanded_right = ast::UnexpandedArithmeticExpr {
value: right.value.clone(),
};
let left = unexpanded_left.eval(shell, false).await?;
let right = unexpanded_right.eval(shell, false).await?;
let left = arithmetic::expand_and_eval(shell, left.value.as_str(), false).await?;
let right = arithmetic::expand_and_eval(shell, right.value.as_str(), false).await?;

if shell.options.print_commands_and_arguments {
shell.trace_command(std::format!("[[ {left} {op} {right} ]]"))?;
Expand All @@ -285,14 +278,8 @@ async fn apply_binary_predicate(
Ok(left == right)
}
ast::BinaryPredicate::ArithmeticNotEqualTo => {
let unexpanded_left = ast::UnexpandedArithmeticExpr {
value: left.value.clone(),
};
let unexpanded_right = ast::UnexpandedArithmeticExpr {
value: right.value.clone(),
};
let left = unexpanded_left.eval(shell, false).await?;
let right = unexpanded_right.eval(shell, false).await?;
let left = arithmetic::expand_and_eval(shell, left.value.as_str(), false).await?;
let right = arithmetic::expand_and_eval(shell, right.value.as_str(), false).await?;

if shell.options.print_commands_and_arguments {
shell.trace_command(std::format!("[[ {left} {op} {right} ]]"))?;
Expand All @@ -301,14 +288,8 @@ async fn apply_binary_predicate(
Ok(left != right)
}
ast::BinaryPredicate::ArithmeticLessThan => {
let unexpanded_left = ast::UnexpandedArithmeticExpr {
value: left.value.clone(),
};
let unexpanded_right = ast::UnexpandedArithmeticExpr {
value: right.value.clone(),
};
let left = unexpanded_left.eval(shell, false).await?;
let right = unexpanded_right.eval(shell, false).await?;
let left = arithmetic::expand_and_eval(shell, left.value.as_str(), false).await?;
let right = arithmetic::expand_and_eval(shell, right.value.as_str(), false).await?;

if shell.options.print_commands_and_arguments {
shell.trace_command(std::format!("[[ {left} {op} {right} ]]"))?;
Expand All @@ -317,14 +298,8 @@ async fn apply_binary_predicate(
Ok(left < right)
}
ast::BinaryPredicate::ArithmeticLessThanOrEqualTo => {
let unexpanded_left = ast::UnexpandedArithmeticExpr {
value: left.value.clone(),
};
let unexpanded_right = ast::UnexpandedArithmeticExpr {
value: right.value.clone(),
};
let left = unexpanded_left.eval(shell, false).await?;
let right = unexpanded_right.eval(shell, false).await?;
let left = arithmetic::expand_and_eval(shell, left.value.as_str(), false).await?;
let right = arithmetic::expand_and_eval(shell, right.value.as_str(), false).await?;

if shell.options.print_commands_and_arguments {
shell.trace_command(std::format!("[[ {left} {op} {right} ]]"))?;
Expand All @@ -333,14 +308,8 @@ async fn apply_binary_predicate(
Ok(left <= right)
}
ast::BinaryPredicate::ArithmeticGreaterThan => {
let unexpanded_left = ast::UnexpandedArithmeticExpr {
value: left.value.clone(),
};
let unexpanded_right = ast::UnexpandedArithmeticExpr {
value: right.value.clone(),
};
let left = unexpanded_left.eval(shell, false).await?;
let right = unexpanded_right.eval(shell, false).await?;
let left = arithmetic::expand_and_eval(shell, left.value.as_str(), false).await?;
let right = arithmetic::expand_and_eval(shell, right.value.as_str(), false).await?;

if shell.options.print_commands_and_arguments {
shell.trace_command(std::format!("[[ {left} {op} {right} ]]"))?;
Expand All @@ -349,14 +318,8 @@ async fn apply_binary_predicate(
Ok(left > right)
}
ast::BinaryPredicate::ArithmeticGreaterThanOrEqualTo => {
let unexpanded_left = ast::UnexpandedArithmeticExpr {
value: left.value.clone(),
};
let unexpanded_right = ast::UnexpandedArithmeticExpr {
value: right.value.clone(),
};
let left = unexpanded_left.eval(shell, false).await?;
let right = unexpanded_right.eval(shell, false).await?;
let left = arithmetic::expand_and_eval(shell, left.value.as_str(), false).await?;
let right = arithmetic::expand_and_eval(shell, right.value.as_str(), false).await?;

if shell.options.print_commands_and_arguments {
shell.trace_command(std::format!("[[ {left} {op} {right} ]]"))?;
Expand Down
5 changes: 2 additions & 3 deletions brush-core/src/interp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::os::unix::process::ExitStatusExt;
use std::path::{Path, PathBuf};
use std::sync::Arc;

use crate::arithmetic::ExpandAndEvaluate;
use crate::arithmetic::{self, ExpandAndEvaluate};
use crate::commands::{self, CommandArg, CommandSpawnResult};
use crate::env::{EnvironmentLookup, EnvironmentScope};
use crate::openfiles::{OpenFile, OpenFiles};
Expand Down Expand Up @@ -1219,8 +1219,7 @@ async fn apply_assignment(

if will_be_indexed_array {
array_index = Some(
ast::UnexpandedArithmeticExpr { value: idx.clone() }
.eval(shell, false)
arithmetic::expand_and_eval(shell, idx.as_str(), false)
.await?
.to_string(),
);
Expand Down
Loading