mirror of
https://github.com/nushell/nushell.git
synced 2025-02-16 18:41:44 +01:00
Add support for while loops (#7101)
This commit is contained in:
parent
75556f6c5f
commit
69b089845c
@ -28,6 +28,7 @@ mod mut_;
|
|||||||
pub(crate) mod overlay;
|
pub(crate) mod overlay;
|
||||||
mod use_;
|
mod use_;
|
||||||
mod version;
|
mod version;
|
||||||
|
mod while_;
|
||||||
|
|
||||||
pub use alias::Alias;
|
pub use alias::Alias;
|
||||||
pub use ast::Ast;
|
pub use ast::Ast;
|
||||||
@ -59,6 +60,7 @@ pub use mut_::Mut;
|
|||||||
pub use overlay::*;
|
pub use overlay::*;
|
||||||
pub use use_::Use;
|
pub use use_::Use;
|
||||||
pub use version::Version;
|
pub use version::Version;
|
||||||
|
pub use while_::While;
|
||||||
#[cfg(feature = "plugin")]
|
#[cfg(feature = "plugin")]
|
||||||
mod register;
|
mod register;
|
||||||
|
|
||||||
|
99
crates/nu-command/src/core_commands/while_.rs
Normal file
99
crates/nu-command/src/core_commands/while_.rs
Normal file
@ -0,0 +1,99 @@
|
|||||||
|
use nu_engine::{eval_block, eval_expression, CallExt};
|
||||||
|
use nu_protocol::ast::Call;
|
||||||
|
use nu_protocol::engine::{Block, Command, EngineState, Stack};
|
||||||
|
use nu_protocol::{Category, Example, PipelineData, ShellError, Signature, SyntaxShape, Value};
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct While;
|
||||||
|
|
||||||
|
impl Command for While {
|
||||||
|
fn name(&self) -> &str {
|
||||||
|
"while"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn usage(&self) -> &str {
|
||||||
|
"Conditionally run a block in a loop."
|
||||||
|
}
|
||||||
|
|
||||||
|
fn signature(&self) -> nu_protocol::Signature {
|
||||||
|
Signature::build("while")
|
||||||
|
.required("cond", SyntaxShape::Expression, "condition to check")
|
||||||
|
.required(
|
||||||
|
"block",
|
||||||
|
SyntaxShape::Block,
|
||||||
|
"block to loop if check succeeds",
|
||||||
|
)
|
||||||
|
.category(Category::Core)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn extra_usage(&self) -> &str {
|
||||||
|
r#"This command is a parser keyword. For details, check:
|
||||||
|
https://www.nushell.sh/book/thinking_in_nu.html"#
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_parser_keyword(&self) -> bool {
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run(
|
||||||
|
&self,
|
||||||
|
engine_state: &EngineState,
|
||||||
|
stack: &mut Stack,
|
||||||
|
call: &Call,
|
||||||
|
_input: PipelineData,
|
||||||
|
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
||||||
|
let cond = call.positional_nth(0).expect("checked through parser");
|
||||||
|
let block: Block = call.req(engine_state, stack, 1)?;
|
||||||
|
|
||||||
|
loop {
|
||||||
|
let result = eval_expression(engine_state, stack, cond)?;
|
||||||
|
match &result {
|
||||||
|
Value::Bool { val, .. } => {
|
||||||
|
if *val {
|
||||||
|
let block = engine_state.get_block(block.block_id);
|
||||||
|
eval_block(
|
||||||
|
engine_state,
|
||||||
|
stack,
|
||||||
|
block,
|
||||||
|
PipelineData::new(call.head),
|
||||||
|
call.redirect_stdout,
|
||||||
|
call.redirect_stderr,
|
||||||
|
)?
|
||||||
|
.into_value(call.head);
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
x => {
|
||||||
|
return Err(ShellError::CantConvert(
|
||||||
|
"bool".into(),
|
||||||
|
x.get_type().to_string(),
|
||||||
|
result.span()?,
|
||||||
|
None,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(PipelineData::new(call.head))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn examples(&self) -> Vec<Example> {
|
||||||
|
vec![Example {
|
||||||
|
description: "Loop while a condition is true",
|
||||||
|
example: "mut x = 0; while $x < 10 { $x = $x + 1 }",
|
||||||
|
result: None,
|
||||||
|
}]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_examples() {
|
||||||
|
use crate::test_examples;
|
||||||
|
|
||||||
|
test_examples(While {})
|
||||||
|
}
|
||||||
|
}
|
@ -62,6 +62,7 @@ pub fn create_default_context() -> EngineState {
|
|||||||
Mut,
|
Mut,
|
||||||
Use,
|
Use,
|
||||||
Version,
|
Version,
|
||||||
|
While,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Charts
|
// Charts
|
||||||
|
@ -88,6 +88,7 @@ mod use_;
|
|||||||
mod where_;
|
mod where_;
|
||||||
#[cfg(feature = "which-support")]
|
#[cfg(feature = "which-support")]
|
||||||
mod which;
|
mod which;
|
||||||
|
mod while_;
|
||||||
mod with_env;
|
mod with_env;
|
||||||
mod wrap;
|
mod wrap;
|
||||||
mod zip;
|
mod zip;
|
||||||
|
11
crates/nu-command/tests/commands/while_.rs
Normal file
11
crates/nu-command/tests/commands/while_.rs
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
use nu_test_support::nu;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn while_sum() {
|
||||||
|
let actual = nu!(
|
||||||
|
cwd: ".",
|
||||||
|
"mut total = 0; mut x = 0; while $x <= 10 { $total = $total + $x; $x = $x + 1 }; $total"
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(actual.out, "55");
|
||||||
|
}
|
@ -968,7 +968,6 @@ mod input_types {
|
|||||||
working_set.add_decl(Box::new(GroupBy));
|
working_set.add_decl(Box::new(GroupBy));
|
||||||
working_set.add_decl(Box::new(LsTest));
|
working_set.add_decl(Box::new(LsTest));
|
||||||
working_set.add_decl(Box::new(ToCustom));
|
working_set.add_decl(Box::new(ToCustom));
|
||||||
working_set.add_decl(Box::new(Let));
|
|
||||||
working_set.add_decl(Box::new(AggMin));
|
working_set.add_decl(Box::new(AggMin));
|
||||||
working_set.add_decl(Box::new(Collect));
|
working_set.add_decl(Box::new(Collect));
|
||||||
working_set.add_decl(Box::new(WithColumn));
|
working_set.add_decl(Box::new(WithColumn));
|
||||||
|
Loading…
Reference in New Issue
Block a user