diff --git a/crates/nu-cli/src/commands/classified/expr.rs b/crates/nu-cli/src/commands/classified/expr.rs new file mode 100644 index 000000000..b64d1a2da --- /dev/null +++ b/crates/nu-cli/src/commands/classified/expr.rs @@ -0,0 +1,36 @@ +use crate::evaluate::evaluate_baseline_expr; +use crate::prelude::*; +use log::{log_enabled, trace}; +use nu_errors::ShellError; +use nu_protocol::hir::SpannedExpression; + +use futures_util::pin_mut; +use nu_protocol::Scope; + +pub(crate) fn run_expression_block( + expr: SpannedExpression, + context: &mut Context, + input: Option, +) -> Result, ShellError> { + if log_enabled!(log::Level::Trace) { + trace!(target: "nu::run::expr", "->"); + trace!(target: "nu::run::expr", "{:?}", expr); + } + + let registry = context.registry().clone(); + + let stream = async_stream! { + if let Some(input) = input { + let values = input.values; + pin_mut!(values); + + while let Some(row) = values.next().await { + yield evaluate_baseline_expr(&expr, ®istry, &Scope::new(row)); + } + } else { + yield evaluate_baseline_expr(&expr, ®istry, &Scope::empty()); + } + }; + + Ok(Some(stream.to_input_stream())) +} diff --git a/crates/nu-cli/src/commands/classified/mod.rs b/crates/nu-cli/src/commands/classified/mod.rs index 5d64a3b4c..3c60d8b00 100644 --- a/crates/nu-cli/src/commands/classified/mod.rs +++ b/crates/nu-cli/src/commands/classified/mod.rs @@ -1,4 +1,5 @@ mod dynamic; +pub(crate) mod expr; pub(crate) mod external; pub(crate) mod internal; pub(crate) mod pipeline; diff --git a/crates/nu-cli/src/commands/classified/pipeline.rs b/crates/nu-cli/src/commands/classified/pipeline.rs index f99bd3c44..5d94b6c61 100644 --- a/crates/nu-cli/src/commands/classified/pipeline.rs +++ b/crates/nu-cli/src/commands/classified/pipeline.rs @@ -1,3 +1,4 @@ +use crate::commands::classified::expr::run_expression_block; use crate::commands::classified::external::run_external_command; use crate::commands::classified::internal::run_internal_command; use crate::context::Context; @@ -21,9 +22,7 @@ pub(crate) async fn run_pipeline( return Err(ShellError::unimplemented("Dynamic commands")) } - // (Some(ClassifiedCommand::Expr(_)), _) | (_, Some(ClassifiedCommand::Expr(_))) => { - // return Err(ShellError::unimplemented("Expression-only commands")) - // } + (Some(ClassifiedCommand::Expr(expr)), _) => run_expression_block(*expr, ctx, input)?, (Some(ClassifiedCommand::Error(err)), _) => return Err(err.into()), (_, Some(ClassifiedCommand::Error(err))) => return Err(err.clone().into()), @@ -38,7 +37,6 @@ pub(crate) async fn run_pipeline( } (None, _) => break, - _ => unimplemented!("Not yet implented cases in run_pipeline"), }; } diff --git a/crates/nu-cli/tests/commands/where_.rs b/crates/nu-cli/tests/commands/where_.rs index 01ad98426..01ccb9705 100644 --- a/crates/nu-cli/tests/commands/where_.rs +++ b/crates/nu-cli/tests/commands/where_.rs @@ -20,6 +20,24 @@ fn filters_with_nothing_comparison() { assert_eq!(actual, "7"); } +#[test] +fn explicit_block_condition() { + let actual = nu!( + cwd: "tests/fixtures/formats", pipeline( + r#" + open sample.db + | where table_name == ints + | get table_values + | first 4 + | where {= $it.z > 4200} + | get z + | echo $it + "# + )); + + assert_eq!(actual, "4253"); +} + #[test] fn binary_operator_comparisons() { let actual = nu!( diff --git a/crates/nu-parser/src/parse.rs b/crates/nu-parser/src/parse.rs index c0275e6cf..49ac91132 100644 --- a/crates/nu-parser/src/parse.rs +++ b/crates/nu-parser/src/parse.rs @@ -459,7 +459,7 @@ fn parse_arg( ), } } - SyntaxShape::Block => { + SyntaxShape::Block | SyntaxShape::Condition => { // Blocks have one of two forms: the literal block and the implied block // To parse a literal block, we need to detect that what we have is itself a block let mut chars = lite_arg.item.chars(); @@ -496,14 +496,6 @@ fn parse_arg( } } } - SyntaxShape::Condition => { - // We have an implied condition, but we can't parse this here - // it needed to have been parsed up higher where we have control over more than one arg - ( - garbage(lite_arg.span), - Some(ParseError::mismatch("condition", lite_arg.clone())), - ) - } } } @@ -604,19 +596,19 @@ fn classify_positional_arg( commands.push(ClassifiedCommand::Expr(Box::new(binary))); SpannedExpression::new(Expression::Block(commands), span) } else if idx < lite_cmd.args.len() { - let (arg, err) = - parse_arg(SyntaxShape::FullColumnPath, registry, &lite_cmd.args[idx]); - if error.is_none() { - error = err; - } - arg - } else { - // TODO - this needs to get fixed and return an actual error let (arg, err) = parse_arg(SyntaxShape::Condition, registry, &lite_cmd.args[idx]); if error.is_none() { error = err; } arg + } else { + if error.is_none() { + error = Some(ParseError::argument_error( + lite_cmd.name.clone(), + ArgumentError::MissingMandatoryPositional("condition".into()), + )) + } + garbage(lite_cmd.span()) } } PositionalType::Mandatory(_, shape) => { @@ -806,6 +798,42 @@ pub fn classify_pipeline( span: Span::new(0, 0), }, })) + } else if lite_cmd.name.item == "=" { + let idx = 0; + let expr = if (idx + 2) < lite_cmd.args.len() { + let (lhs, err) = parse_arg(SyntaxShape::Any, registry, &lite_cmd.args[idx]); + if error.is_none() { + error = err; + } + let (op, err) = parse_arg(SyntaxShape::Operator, registry, &lite_cmd.args[idx + 1]); + if error.is_none() { + error = err; + } + let (rhs, err) = parse_arg(SyntaxShape::Any, registry, &lite_cmd.args[idx + 2]); + if error.is_none() { + error = err; + } + let span = Span::new(lhs.span.start(), rhs.span.end()); + SpannedExpression::new( + Expression::Binary(Box::new(Binary::new(lhs, op, rhs))), + span, + ) + } else if idx < lite_cmd.args.len() { + let (arg, err) = parse_arg(SyntaxShape::Any, registry, &lite_cmd.args[idx]); + if error.is_none() { + error = err; + } + arg + } else { + if error.is_none() { + error = Some(ParseError::argument_error( + lite_cmd.name.clone(), + ArgumentError::MissingMandatoryPositional("an expression".into()), + )) + } + garbage(lite_cmd.span()) + }; + commands.push(ClassifiedCommand::Expr(Box::new(expr))) } else if let Some(signature) = registry.get(&lite_cmd.name.item) { let (internal_command, err) = classify_internal_command(&lite_cmd, registry, &signature);