mirror of
https://github.com/nushell/nushell.git
synced 2025-08-10 08:49:33 +02:00
Remove math eval
command (#7284)
Reasoning: Most missing math commands are implemented with #7258. The `meval` crate itself declares that it doesn't strive to stringent standards (https://docs.rs/meval/latest/meval/#related-projects). For example no particular special casing or transformations are performed to ensure numerical stability. It uses the same rust `std` library functions we use or have access to (and `f64`). While the command call syntax in nushell may be a bit more verbose, having a single source of truth and common commands is beneficial. Furthermore the `math` commands can themselves implement broadcasting over lists (or table columns). Closes #7073 Removed dependencies: - `meval` - `nom 1.2.4` (duplicate) User-Facing Changes: Scripts using `math eval` will break. We remove a further `eval` like behavior to get results through runtime evaluation (albeit limited in scope) Tests: - Updated tests that internally used `math eval`. - Removed one test that primarily used `math eval` to obtain a result from `str join`
This commit is contained in:
committed by
GitHub
parent
429127793f
commit
9bc4e6794d
@ -1,115 +0,0 @@
|
||||
use nu_engine::CallExt;
|
||||
use nu_protocol::ast::Call;
|
||||
use nu_protocol::engine::{Command, EngineState, Stack};
|
||||
use nu_protocol::{
|
||||
Category, Example, PipelineData, ShellError, Signature, Span, Spanned, SyntaxShape, Type, Value,
|
||||
};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct SubCommand;
|
||||
|
||||
impl Command for SubCommand {
|
||||
fn name(&self) -> &str {
|
||||
"math eval"
|
||||
}
|
||||
|
||||
fn usage(&self) -> &str {
|
||||
"Evaluate a math expression into a number"
|
||||
}
|
||||
|
||||
fn search_terms(&self) -> Vec<&str> {
|
||||
vec!["evaluation", "solve", "equation", "expression"]
|
||||
}
|
||||
|
||||
fn signature(&self) -> Signature {
|
||||
Signature::build("math eval")
|
||||
.input_output_types(vec![(Type::String, Type::Number)])
|
||||
.optional(
|
||||
"math expression",
|
||||
SyntaxShape::String,
|
||||
"the math expression to evaluate",
|
||||
)
|
||||
.category(Category::Math)
|
||||
}
|
||||
|
||||
fn run(
|
||||
&self,
|
||||
engine_state: &EngineState,
|
||||
stack: &mut Stack,
|
||||
call: &Call,
|
||||
input: PipelineData,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
let spanned_expr: Option<Spanned<String>> = call.opt(engine_state, stack, 0)?;
|
||||
eval(spanned_expr, input, engine_state, call.head)
|
||||
}
|
||||
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
vec![Example {
|
||||
description: "Evaluate math in the pipeline",
|
||||
example: "'10 / 4' | math eval",
|
||||
result: Some(Value::test_float(2.5)),
|
||||
}]
|
||||
}
|
||||
}
|
||||
|
||||
pub fn eval(
|
||||
spanned_expr: Option<Spanned<String>>,
|
||||
input: PipelineData,
|
||||
engine_state: &EngineState,
|
||||
head: Span,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
if let Some(expr) = spanned_expr {
|
||||
match parse(&expr.item, &expr.span) {
|
||||
Ok(value) => Ok(PipelineData::Value(value, None)),
|
||||
Err(err) => Err(ShellError::UnsupportedInput(
|
||||
format!("Math evaluation error: {}", err),
|
||||
"value originates from here".into(),
|
||||
head,
|
||||
expr.span,
|
||||
)),
|
||||
}
|
||||
} else {
|
||||
if let PipelineData::Value(Value::Nothing { .. }, ..) = input {
|
||||
return Ok(input);
|
||||
}
|
||||
input.map(
|
||||
move |val| match val.as_string() {
|
||||
Ok(string) => match parse(&string, &val.span().unwrap_or(head)) {
|
||||
Ok(value) => value,
|
||||
Err(err) => Value::Error {
|
||||
error: ShellError::UnsupportedInput(
|
||||
format!("Math evaluation error: {}", err),
|
||||
"value originates from here".into(),
|
||||
head,
|
||||
val.expect_span(),
|
||||
),
|
||||
},
|
||||
},
|
||||
Err(error) => Value::Error { error },
|
||||
},
|
||||
engine_state.ctrlc.clone(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse(math_expression: &str, span: &Span) -> Result<Value, String> {
|
||||
let mut ctx = meval::Context::new();
|
||||
ctx.var("tau", std::f64::consts::TAU);
|
||||
match meval::eval_str_with_context(math_expression, &ctx) {
|
||||
Ok(num) if num.is_infinite() || num.is_nan() => Err("cannot represent result".to_string()),
|
||||
Ok(num) => Ok(Value::float(num, *span)),
|
||||
Err(error) => Err(error.to_string().to_lowercase()),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_examples() {
|
||||
use crate::test_examples;
|
||||
|
||||
test_examples(SubCommand {})
|
||||
}
|
||||
}
|
@ -10,7 +10,6 @@ mod ceil;
|
||||
mod cos;
|
||||
mod cosh;
|
||||
mod euler;
|
||||
mod eval;
|
||||
mod floor;
|
||||
mod ln;
|
||||
mod log;
|
||||
@ -37,7 +36,6 @@ mod variance;
|
||||
pub use abs::SubCommand as MathAbs;
|
||||
pub use avg::SubCommand as MathAvg;
|
||||
pub use ceil::SubCommand as MathCeil;
|
||||
pub use eval::SubCommand as MathEval;
|
||||
pub use floor::SubCommand as MathFloor;
|
||||
pub use math_::MathCommand as Math;
|
||||
pub use max::SubCommand as MathMax;
|
||||
|
Reference in New Issue
Block a user