Fix up calls and pipelines

This commit is contained in:
JT
2021-09-03 14:15:01 +12:00
parent 7c8504ea24
commit df63490266
14 changed files with 145 additions and 61 deletions

View File

@ -34,7 +34,7 @@ impl Command for Benchmark {
let state = context.enter_scope();
let start_time = Instant::now();
eval_block(&state, block)?;
eval_block(&state, block, Value::nothing())?;
let end_time = Instant::now();
println!("{} ms", (end_time - start_time).as_millis());
Ok(Value::Nothing {

View File

@ -5,7 +5,7 @@ use nu_protocol::{
Signature, SyntaxShape,
};
use crate::{Alias, Benchmark, BuildString, Def, For, If, Let, LetEnv};
use crate::{Alias, Benchmark, BuildString, Def, For, If, Length, Let, LetEnv};
pub fn create_default_context() -> Rc<RefCell<EngineState>> {
let engine_state = Rc::new(RefCell::new(EngineState::new()));
@ -33,6 +33,8 @@ pub fn create_default_context() -> Rc<RefCell<EngineState>> {
working_set.add_decl(Box::new(Benchmark));
working_set.add_decl(Box::new(Length));
let sig = Signature::build("exit");
working_set.add_decl(sig.predeclare());
let sig = Signature::build("vars");

View File

@ -33,7 +33,7 @@ impl Command for For {
&self,
context: &EvaluationContext,
call: &Call,
_input: Value,
input: Value,
) -> Result<nu_protocol::Value, nu_protocol::ShellError> {
let var_id = call.positional[0]
.as_var()
@ -62,7 +62,7 @@ impl Command for For {
break;
} else {
state.add_var(var_id, x.clone());
eval_block(&state, block)?;
eval_block(&state, block, input.clone())?;
}
if let Value::Int { ref mut val, .. } = x {
*val += 1

View File

@ -29,7 +29,7 @@ impl Command for If {
&self,
context: &EvaluationContext,
call: &Call,
_input: Value,
input: Value,
) -> Result<nu_protocol::Value, nu_protocol::ShellError> {
let cond = &call.positional[0];
let then_block = call.positional[1]
@ -44,13 +44,13 @@ impl Command for If {
if val {
let block = engine_state.get_block(then_block);
let state = context.enter_scope();
eval_block(&state, block)
eval_block(&state, block, input)
} else if let Some(else_case) = else_case {
if let Some(else_expr) = else_case.as_keyword() {
if let Some(block_id) = else_expr.as_block() {
let block = engine_state.get_block(block_id);
let state = context.enter_scope();
eval_block(&state, block)
eval_block(&state, block, input)
} else {
eval_expression(context, else_expr)
}

View File

@ -0,0 +1,49 @@
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EvaluationContext};
use nu_protocol::{Signature, Value};
pub struct Length;
impl Command for Length {
fn name(&self) -> &str {
"length"
}
fn usage(&self) -> &str {
"Count the number of elements in the input."
}
fn signature(&self) -> nu_protocol::Signature {
Signature::build("length")
}
fn run(
&self,
_context: &EvaluationContext,
call: &Call,
input: Value,
) -> Result<nu_protocol::Value, nu_protocol::ShellError> {
match input {
Value::List { val, .. } => {
let length = val.count();
Ok(Value::Int {
val: length as i64,
span: call.head,
})
}
Value::Table { val, .. } => {
let length = val.count();
Ok(Value::Int {
val: length as i64,
span: call.head,
})
}
_ => Ok(Value::Int {
val: 1,
span: call.head,
}),
}
}
}

View File

@ -5,6 +5,7 @@ mod def;
mod default_context;
mod for_;
mod if_;
mod length;
mod let_;
mod let_env;
@ -15,5 +16,6 @@ pub use def::Def;
pub use default_context::create_default_context;
pub use for_::For;
pub use if_::If;
pub use length::Length;
pub use let_::Let;
pub use let_env::LetEnv;