forked from extern/nushell
Multiline scripts part 2 (#2795)
* Begin allowing comments and multiline scripts. * clippy * Finish moving to groups. Test pass * Keep going * WIP * WIP * BROKEN WIP * WIP * WIP * Fix more tests * WIP: alias starts working * Broken WIP * Broken WIP * Variables begin to work * captures start working * A little better but needs fixed scope * Shorthand env setting * Update main merge * Broken WIP * WIP * custom command parsing * Custom commands start working * Fix coloring and parsing of block * Almost there * Add some tests * Add more param types * Bump version * Fix benchmark * Fix stuff
This commit is contained in:
@ -2,17 +2,18 @@ use crate::commands::classified::block::run_block;
|
||||
use crate::commands::each;
|
||||
use crate::commands::WholeStreamCommand;
|
||||
use crate::prelude::*;
|
||||
use crate::{CommandArgs, CommandRegistry, Example, OutputStream};
|
||||
use crate::{CommandArgs, Example, OutputStream};
|
||||
use futures::stream::once;
|
||||
use nu_errors::ShellError;
|
||||
use nu_protocol::{hir::Block, Primitive, Scope, Signature, SyntaxShape, UntaggedValue, Value};
|
||||
use nu_parser::ParserScope;
|
||||
use nu_protocol::{hir::CapturedBlock, Primitive, Signature, SyntaxShape, UntaggedValue, Value};
|
||||
use nu_source::Tagged;
|
||||
|
||||
pub struct Reduce;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct ReduceArgs {
|
||||
block: Block,
|
||||
block: CapturedBlock,
|
||||
fold: Option<Value>,
|
||||
numbered: Tagged<bool>,
|
||||
}
|
||||
@ -44,12 +45,8 @@ impl WholeStreamCommand for Reduce {
|
||||
(A, A) -> A unless --fold is selected, in which case it may be A, B -> A."
|
||||
}
|
||||
|
||||
async fn run(
|
||||
&self,
|
||||
args: CommandArgs,
|
||||
registry: &CommandRegistry,
|
||||
) -> Result<OutputStream, ShellError> {
|
||||
reduce(args, registry).await
|
||||
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||
reduce(args).await
|
||||
}
|
||||
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
@ -79,27 +76,25 @@ impl WholeStreamCommand for Reduce {
|
||||
}
|
||||
|
||||
async fn process_row(
|
||||
block: Arc<Block>,
|
||||
scope: Arc<Scope>,
|
||||
mut context: Arc<EvaluationContext>,
|
||||
block: Arc<CapturedBlock>,
|
||||
context: &EvaluationContext,
|
||||
row: Value,
|
||||
) -> Result<InputStream, ShellError> {
|
||||
let row_clone = row.clone();
|
||||
let input_stream = once(async { Ok(row_clone) }).to_input_stream();
|
||||
|
||||
let scope = Scope::append_var(scope, "$it", row);
|
||||
context.scope.enter_scope();
|
||||
context.scope.add_vars(&block.captured.entries);
|
||||
context.scope.add_var("$it", row);
|
||||
let result = run_block(&block.block, context, input_stream).await;
|
||||
context.scope.exit_scope();
|
||||
|
||||
Ok(run_block(&block, Arc::make_mut(&mut context), input_stream, scope).await?)
|
||||
Ok(result?)
|
||||
}
|
||||
|
||||
async fn reduce(
|
||||
raw_args: CommandArgs,
|
||||
registry: &CommandRegistry,
|
||||
) -> Result<OutputStream, ShellError> {
|
||||
let registry = registry.clone();
|
||||
let base_scope = raw_args.call_info.scope.clone();
|
||||
let context = Arc::new(EvaluationContext::from_raw(&raw_args, ®istry));
|
||||
let (reduce_args, mut input): (ReduceArgs, _) = raw_args.process(®istry).await?;
|
||||
async fn reduce(raw_args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||
let context = Arc::new(EvaluationContext::from_raw(&raw_args));
|
||||
let (reduce_args, mut input): (ReduceArgs, _) = raw_args.process().await?;
|
||||
let block = Arc::new(reduce_args.block);
|
||||
let (ioffset, start) = match reduce_args.fold {
|
||||
None => {
|
||||
@ -126,12 +121,11 @@ async fn reduce(
|
||||
Ok(input
|
||||
.enumerate()
|
||||
.fold(initial, move |acc, input| {
|
||||
let context = context.clone();
|
||||
let block = Arc::clone(&block);
|
||||
let scope = base_scope.clone();
|
||||
let context = Arc::clone(&context);
|
||||
let row = each::make_indexed_item(input.0 + ioffset, input.1);
|
||||
|
||||
async {
|
||||
async move {
|
||||
let values = acc?.drain_vec().await;
|
||||
|
||||
let f = if values.len() == 1 {
|
||||
@ -145,8 +139,12 @@ async fn reduce(
|
||||
UntaggedValue::table(&values).into_untagged_value()
|
||||
};
|
||||
|
||||
let scope = Scope::append_var(scope, "$acc", f);
|
||||
process_row(block, scope, context, row).await
|
||||
context.scope.enter_scope();
|
||||
context.scope.add_var("$acc", f);
|
||||
let result = process_row(block, &*context, row).await;
|
||||
context.scope.exit_scope();
|
||||
|
||||
result
|
||||
}
|
||||
})
|
||||
.await?
|
||||
@ -156,10 +154,9 @@ async fn reduce(
|
||||
Ok(input
|
||||
.fold(initial, move |acc, row| {
|
||||
let block = Arc::clone(&block);
|
||||
let scope = base_scope.clone();
|
||||
let context = Arc::clone(&context);
|
||||
let context = context.clone();
|
||||
|
||||
async {
|
||||
async move {
|
||||
let values = acc?.drain_vec().await;
|
||||
|
||||
let f = if values.len() == 1 {
|
||||
@ -173,8 +170,11 @@ async fn reduce(
|
||||
UntaggedValue::table(&values).into_untagged_value()
|
||||
};
|
||||
|
||||
let scope = Scope::append_var(scope, "$acc", f);
|
||||
process_row(block, scope, context, row).await
|
||||
context.scope.enter_scope();
|
||||
context.scope.add_var("$acc", f);
|
||||
let result = process_row(block, &*context, row).await;
|
||||
context.scope.exit_scope();
|
||||
result
|
||||
}
|
||||
})
|
||||
.await?
|
||||
|
Reference in New Issue
Block a user