diff --git a/Cargo.lock b/Cargo.lock index f927de8c51..fce28ee3b6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -104,7 +104,7 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "220044e6a1bb31ddee4e3db724d29767f352de47445a6cd75e1a173142136c83" dependencies = [ - "nom 7.1.1", + "nom", "vte", ] @@ -479,7 +479,7 @@ version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6fac387a98bb7c37292057cffc56d62ecb629900026402633ae9160df93a8766" dependencies = [ - "nom 7.1.1", + "nom", ] [[package]] @@ -2225,16 +2225,6 @@ dependencies = [ "autocfg", ] -[[package]] -name = "meval" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f79496a5651c8d57cd033c5add8ca7ee4e3d5f7587a4777484640d9cb60392d9" -dependencies = [ - "fnv", - "nom 1.2.4", -] - [[package]] name = "miette" version = "5.3.0" @@ -2439,12 +2429,6 @@ version = "0.1.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "72ef4a56884ca558e5ddb05a1d1e7e1bfd9a68d9ed024c21704cc98872dae1bb" -[[package]] -name = "nom" -version = "1.2.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a5b8c256fd9471521bcb84c3cdba98921497f1a331cbc15b8030fc63b82050ce" - [[package]] name = "nom" version = "7.1.1" @@ -2465,7 +2449,7 @@ dependencies = [ "indent_write", "joinery", "memchr", - "nom 7.1.1", + "nom", ] [[package]] @@ -2628,7 +2612,6 @@ dependencies = [ "log", "lscolors", "md-5", - "meval", "mime", "mime_guess", "notify", @@ -3672,7 +3655,7 @@ version = "0.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a4a63d338dec139f56dacc692ca63ad35a6be6a797442479b55acd611d79e906" dependencies = [ - "nom 7.1.1", + "nom", ] [[package]] @@ -5574,7 +5557,7 @@ dependencies = [ "bstr", "const_format", "itertools", - "nom 7.1.1", + "nom", "nom-supreme", "pori", "regex", diff --git a/crates/nu-command/Cargo.toml b/crates/nu-command/Cargo.toml index d9cc14ad90..05992db689 100644 --- a/crates/nu-command/Cargo.toml +++ b/crates/nu-command/Cargo.toml @@ -57,7 +57,6 @@ itertools = "0.10.0" log = "0.4.14" lscolors = { version = "0.12.0", features = ["crossterm"], default-features = false } md5 = { package = "md-5", version = "0.10.0" } -meval = "0.2.0" mime = "0.3.16" mime_guess = "2.0.4" notify = "4.0.17" diff --git a/crates/nu-command/src/default_context.rs b/crates/nu-command/src/default_context.rs index 9d257236c7..2a044a6e95 100644 --- a/crates/nu-command/src/default_context.rs +++ b/crates/nu-command/src/default_context.rs @@ -401,7 +401,6 @@ pub fn create_default_context() -> EngineState { MathAbs, MathAvg, MathCeil, - MathEval, MathFloor, MathMax, MathMedian, @@ -481,6 +480,7 @@ pub fn create_default_context() -> EngineState { StrDecimalDeprecated, StrIntDeprecated, StrFindReplaceDeprecated, + MathEvalDeprecated, }; #[cfg(feature = "plugin")] diff --git a/crates/nu-command/src/deprecated/math_eval.rs b/crates/nu-command/src/deprecated/math_eval.rs new file mode 100644 index 0000000000..e62567c6ec --- /dev/null +++ b/crates/nu-command/src/deprecated/math_eval.rs @@ -0,0 +1,40 @@ +use nu_protocol::ast::Call; +use nu_protocol::engine::{Command, EngineState, Stack}; +use nu_protocol::{Category, PipelineData, ShellError, Signature, SyntaxShape}; + +#[derive(Clone)] +pub struct SubCommand; + +impl Command for SubCommand { + fn name(&self) -> &str { + "math eval" + } + + fn signature(&self) -> Signature { + Signature::build("math eval") + .rest( + "ignored", + SyntaxShape::Any, + "arguments to deprecated command are ignored", + ) + .category(Category::Deprecated) + } + + fn usage(&self) -> &str { + "Deprecated command" + } + + fn run( + &self, + _engine_state: &EngineState, + _stack: &mut Stack, + call: &Call, + _input: PipelineData, + ) -> Result { + Err(ShellError::DeprecatedCommand( + self.name().to_string(), + "math ".to_string(), + call.head, + )) + } +} diff --git a/crates/nu-command/src/deprecated/mod.rs b/crates/nu-command/src/deprecated/mod.rs index a478c9a9e2..4f9373b91c 100644 --- a/crates/nu-command/src/deprecated/mod.rs +++ b/crates/nu-command/src/deprecated/mod.rs @@ -1,5 +1,6 @@ mod deprecated_commands; mod hash_base64; +mod math_eval; mod source; mod str_datetime; mod str_decimal; @@ -8,6 +9,7 @@ mod str_int; pub use deprecated_commands::*; pub use hash_base64::HashBase64; +pub use math_eval::SubCommand as MathEvalDeprecated; pub use source::Source; pub use str_datetime::StrDatetimeDeprecated; pub use str_decimal::StrDecimalDeprecated; diff --git a/crates/nu-command/src/math/eval.rs b/crates/nu-command/src/math/eval.rs deleted file mode 100644 index 7eef75e45f..0000000000 --- a/crates/nu-command/src/math/eval.rs +++ /dev/null @@ -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 { - let spanned_expr: Option> = call.opt(engine_state, stack, 0)?; - eval(spanned_expr, input, engine_state, call.head) - } - - fn examples(&self) -> Vec { - 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>, - input: PipelineData, - engine_state: &EngineState, - head: Span, -) -> Result { - 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 { - 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 {}) - } -} diff --git a/crates/nu-command/src/math/mod.rs b/crates/nu-command/src/math/mod.rs index 69573bbf82..52a6982087 100644 --- a/crates/nu-command/src/math/mod.rs +++ b/crates/nu-command/src/math/mod.rs @@ -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; diff --git a/crates/nu-command/tests/commands/math/eval.rs b/crates/nu-command/tests/commands/math/eval.rs deleted file mode 100644 index 8db3a72108..0000000000 --- a/crates/nu-command/tests/commands/math/eval.rs +++ /dev/null @@ -1,85 +0,0 @@ -use nu_test_support::{nu, pipeline}; - -#[test] -fn evaluates_two_plus_two() { - let actual = nu!( - cwd: ".", pipeline( - r#" - math eval "2 + 2" - "# - )); - - assert!(actual.out.contains('4')); -} - -#[test] -fn evaluates_two_to_the_power_four() { - let actual = nu!( - cwd: ".", pipeline( - r#" - echo "2 ^ 4" | math eval - "# - )); - - assert!(actual.out.contains("16")); -} - -#[test] -fn evaluates_three_multiplied_by_five() { - let actual = nu!( - cwd: ".", pipeline( - r#" - echo "3 * 5" | math eval - "# - )); - - assert!(actual.out.contains("15")); -} - -#[test] -fn evaluates_twenty_four_divided_by_two() { - let actual = nu!( - cwd: ".", pipeline( - r#" - echo "24 / 2" | math eval - "# - )); - - assert!(actual.out.contains("12")); -} - -#[test] -fn evaluates_twenty_eight_minus_seven() { - let actual = nu!( - cwd: ".", pipeline( - r#" - echo "28 - 7" | math eval - "# - )); - - assert!(actual.out.contains("21")); -} - -#[test] -fn evaluates_pi() { - let actual = nu!( - cwd: ".", pipeline( - r#" - math eval pi - "# - )); - - assert!(actual.out.contains("3.14")); -} - -#[test] -fn evaluates_tau() { - let actual = nu!( - cwd: ".", pipeline( - r#" - math eval tau - "# - )); - - assert!(actual.out.contains("6.28")); -} diff --git a/crates/nu-command/tests/commands/math/mod.rs b/crates/nu-command/tests/commands/math/mod.rs index 77c22cecc6..9de7102fc5 100644 --- a/crates/nu-command/tests/commands/math/mod.rs +++ b/crates/nu-command/tests/commands/math/mod.rs @@ -1,5 +1,4 @@ mod avg; -mod eval; mod median; mod round; mod sqrt; diff --git a/crates/nu-command/tests/commands/reduce.rs b/crates/nu-command/tests/commands/reduce.rs index e26d1f0c33..ccb1e0bdbb 100644 --- a/crates/nu-command/tests/commands/reduce.rs +++ b/crates/nu-command/tests/commands/reduce.rs @@ -8,7 +8,7 @@ fn reduce_table_column() { echo "[{month:2,total:30}, {month:3,total:10}, {month:4,total:3}, {month:5,total:60}]" | from json | get total - | reduce -f 20 { |it, acc| $it + (math eval $"($acc)^1.05")} + | reduce -f 20 { |it, acc| $it + $acc ** 1.05} | into string -d 1 "# ) @@ -23,7 +23,7 @@ fn reduce_table_column_with_path() { cwd: ".", pipeline( r#" [{month:2,total:30}, {month:3,total:10}, {month:4,total:3}, {month:5,total:60}] - | reduce -f 20 { |it, acc| $it.total + (math eval $"($acc)^1.05")} + | reduce -f 20 { |it, acc| $it.total + $acc ** 1.05} | into string -d 1 "# ) diff --git a/crates/nu-command/tests/commands/str_/collect.rs b/crates/nu-command/tests/commands/str_/collect.rs index 820fa8d0d6..02a80a45dd 100644 --- a/crates/nu-command/tests/commands/str_/collect.rs +++ b/crates/nu-command/tests/commands/str_/collect.rs @@ -38,16 +38,3 @@ fn construct_a_path() { assert_eq!(actual.out, "sample.txt"); } - -#[test] -fn sum_one_to_four() { - let actual = nu!( - cwd: ".", pipeline( - r#" - 1..4 | each { |it| $it } | into string | str join "+" | math eval - "# - ) - ); - - assert!(actual.out.contains("10")); -}