Canonical expr block (#1584)

* Add the canonical form for an expression block

* Remove commented out code
This commit is contained in:
Jonathan Turner
2020-04-14 06:59:33 +12:00
committed by GitHub
parent 08a09e2273
commit e3da037b80
5 changed files with 102 additions and 21 deletions

View File

@ -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<InputStream>,
) -> Result<Option<InputStream>, 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, &registry, &Scope::new(row));
}
} else {
yield evaluate_baseline_expr(&expr, &registry, &Scope::empty());
}
};
Ok(Some(stream.to_input_stream()))
}

View File

@ -1,4 +1,5 @@
mod dynamic;
pub(crate) mod expr;
pub(crate) mod external;
pub(crate) mod internal;
pub(crate) mod pipeline;

View File

@ -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"),
};
}