mirror of
https://github.com/nushell/nushell.git
synced 2025-08-15 21:27:51 +02:00
Add commands
This commit is contained in:
@ -1,5 +1,3 @@
|
||||
use std::time::Instant;
|
||||
|
||||
use nu_protocol::ast::{Block, Call, Expr, Expression, Operator, Statement};
|
||||
use nu_protocol::engine::EvaluationContext;
|
||||
use nu_protocol::{IntoRowStream, IntoValueStream, ShellError, Span, Value};
|
||||
@ -14,11 +12,11 @@ pub fn eval_operator(op: &Expression) -> Result<Operator, ShellError> {
|
||||
}
|
||||
}
|
||||
|
||||
fn eval_call(state: &EvaluationContext, call: &Call) -> Result<Value, ShellError> {
|
||||
let engine_state = state.engine_state.borrow();
|
||||
fn eval_call(context: &EvaluationContext, call: &Call) -> Result<Value, ShellError> {
|
||||
let engine_state = context.engine_state.borrow();
|
||||
let decl = engine_state.get_decl(call.decl_id);
|
||||
if let Some(block_id) = decl.get_custom_command() {
|
||||
let state = state.enter_scope();
|
||||
let state = context.enter_scope();
|
||||
for (arg, param) in call.positional.iter().zip(
|
||||
decl.signature()
|
||||
.required_positional
|
||||
@ -35,158 +33,21 @@ fn eval_call(state: &EvaluationContext, call: &Call) -> Result<Value, ShellError
|
||||
let engine_state = state.engine_state.borrow();
|
||||
let block = engine_state.get_block(block_id);
|
||||
eval_block(&state, block)
|
||||
} else if decl.signature().name == "let" {
|
||||
let var_id = call.positional[0]
|
||||
.as_var()
|
||||
.expect("internal error: missing variable");
|
||||
|
||||
let keyword_expr = call.positional[1]
|
||||
.as_keyword()
|
||||
.expect("internal error: missing keyword");
|
||||
|
||||
let rhs = eval_expression(state, keyword_expr)?;
|
||||
|
||||
//println!("Adding: {:?} to {}", rhs, var_id);
|
||||
|
||||
state.add_var(var_id, rhs);
|
||||
Ok(Value::Nothing {
|
||||
span: call.positional[0].span,
|
||||
})
|
||||
} else if decl.signature().name == "let-env" {
|
||||
let env_var = call.positional[0]
|
||||
.as_string()
|
||||
.expect("internal error: missing variable");
|
||||
|
||||
let keyword_expr = call.positional[1]
|
||||
.as_keyword()
|
||||
.expect("internal error: missing keyword");
|
||||
|
||||
let rhs = eval_expression(state, keyword_expr)?;
|
||||
let rhs = rhs.as_string()?;
|
||||
|
||||
//println!("Adding: {:?} to {}", rhs, var_id);
|
||||
|
||||
state.add_env_var(env_var, rhs);
|
||||
Ok(Value::Nothing {
|
||||
span: call.positional[0].span,
|
||||
})
|
||||
} else if decl.signature().name == "if" {
|
||||
let cond = &call.positional[0];
|
||||
let then_block = call.positional[1]
|
||||
.as_block()
|
||||
.expect("internal error: expected block");
|
||||
let else_case = call.positional.get(2);
|
||||
|
||||
let result = eval_expression(state, cond)?;
|
||||
match result {
|
||||
Value::Bool { val, span } => {
|
||||
let engine_state = state.engine_state.borrow();
|
||||
if val {
|
||||
let block = engine_state.get_block(then_block);
|
||||
let state = state.enter_scope();
|
||||
eval_block(&state, block)
|
||||
} 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 = state.enter_scope();
|
||||
eval_block(&state, block)
|
||||
} else {
|
||||
eval_expression(state, else_expr)
|
||||
}
|
||||
} else {
|
||||
eval_expression(state, else_case)
|
||||
}
|
||||
} else {
|
||||
Ok(Value::Nothing { span })
|
||||
}
|
||||
}
|
||||
_ => Err(ShellError::CantConvert("bool".into(), result.span())),
|
||||
}
|
||||
} else if decl.signature().name == "build-string" {
|
||||
let mut output = vec![];
|
||||
|
||||
for expr in &call.positional {
|
||||
let val = eval_expression(state, expr)?;
|
||||
|
||||
output.push(val.into_string());
|
||||
}
|
||||
Ok(Value::String {
|
||||
val: output.join(""),
|
||||
span: call.head,
|
||||
})
|
||||
} else if decl.signature().name == "benchmark" {
|
||||
let block = call.positional[0]
|
||||
.as_block()
|
||||
.expect("internal error: expected block");
|
||||
let engine_state = state.engine_state.borrow();
|
||||
let block = engine_state.get_block(block);
|
||||
|
||||
let state = state.enter_scope();
|
||||
let start_time = Instant::now();
|
||||
eval_block(&state, block)?;
|
||||
let end_time = Instant::now();
|
||||
println!("{} ms", (end_time - start_time).as_millis());
|
||||
Ok(Value::Nothing {
|
||||
span: call.positional[0].span,
|
||||
})
|
||||
} else if decl.signature().name == "for" {
|
||||
let var_id = call.positional[0]
|
||||
.as_var()
|
||||
.expect("internal error: missing variable");
|
||||
|
||||
let keyword_expr = call.positional[1]
|
||||
.as_keyword()
|
||||
.expect("internal error: missing keyword");
|
||||
let end_val = eval_expression(state, keyword_expr)?;
|
||||
|
||||
let block = call.positional[2]
|
||||
.as_block()
|
||||
.expect("internal error: expected block");
|
||||
let engine_state = state.engine_state.borrow();
|
||||
let block = engine_state.get_block(block);
|
||||
|
||||
let state = state.enter_scope();
|
||||
|
||||
let mut x = Value::Int {
|
||||
val: 0,
|
||||
span: Span::unknown(),
|
||||
};
|
||||
|
||||
loop {
|
||||
if x == end_val {
|
||||
break;
|
||||
} else {
|
||||
state.add_var(var_id, x.clone());
|
||||
eval_block(&state, block)?;
|
||||
}
|
||||
if let Value::Int { ref mut val, .. } = x {
|
||||
*val += 1
|
||||
}
|
||||
}
|
||||
Ok(Value::Nothing {
|
||||
span: call.positional[0].span,
|
||||
})
|
||||
} else if decl.signature().name == "vars" {
|
||||
state.engine_state.borrow().print_vars();
|
||||
Ok(Value::Nothing { span: call.head })
|
||||
} else if decl.signature().name == "decls" {
|
||||
state.engine_state.borrow().print_decls();
|
||||
Ok(Value::Nothing { span: call.head })
|
||||
} else if decl.signature().name == "blocks" {
|
||||
state.engine_state.borrow().print_blocks();
|
||||
Ok(Value::Nothing { span: call.head })
|
||||
} else if decl.signature().name == "stack" {
|
||||
state.print_stack();
|
||||
Ok(Value::Nothing { span: call.head })
|
||||
} else if decl.signature().name == "def" || decl.signature().name == "alias" {
|
||||
Ok(Value::Nothing { span: call.head })
|
||||
} else {
|
||||
Err(ShellError::Unsupported(call.head))
|
||||
decl.run(
|
||||
context,
|
||||
call,
|
||||
Value::Nothing {
|
||||
span: Span::unknown(),
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn eval_expression(state: &EvaluationContext, expr: &Expression) -> Result<Value, ShellError> {
|
||||
pub fn eval_expression(
|
||||
context: &EvaluationContext,
|
||||
expr: &Expression,
|
||||
) -> Result<Value, ShellError> {
|
||||
match &expr.expr {
|
||||
Expr::Bool(b) => Ok(Value::Bool {
|
||||
val: *b,
|
||||
@ -200,17 +61,17 @@ pub fn eval_expression(state: &EvaluationContext, expr: &Expression) -> Result<V
|
||||
val: *f,
|
||||
span: expr.span,
|
||||
}),
|
||||
Expr::Var(var_id) => state
|
||||
Expr::Var(var_id) => context
|
||||
.get_var(*var_id)
|
||||
.map_err(move |_| ShellError::VariableNotFound(expr.span)),
|
||||
Expr::Call(call) => eval_call(state, call),
|
||||
Expr::Call(call) => eval_call(context, call),
|
||||
Expr::ExternalCall(_, _) => Err(ShellError::Unsupported(expr.span)),
|
||||
Expr::Operator(_) => Ok(Value::Nothing { span: expr.span }),
|
||||
Expr::BinaryOp(lhs, op, rhs) => {
|
||||
let op_span = op.span;
|
||||
let lhs = eval_expression(state, lhs)?;
|
||||
let lhs = eval_expression(context, lhs)?;
|
||||
let op = eval_operator(op)?;
|
||||
let rhs = eval_expression(state, rhs)?;
|
||||
let rhs = eval_expression(context, rhs)?;
|
||||
|
||||
match op {
|
||||
Operator::Plus => lhs.add(op_span, &rhs),
|
||||
@ -228,10 +89,10 @@ pub fn eval_expression(state: &EvaluationContext, expr: &Expression) -> Result<V
|
||||
}
|
||||
|
||||
Expr::Subexpression(block_id) => {
|
||||
let engine_state = state.engine_state.borrow();
|
||||
let engine_state = context.engine_state.borrow();
|
||||
let block = engine_state.get_block(*block_id);
|
||||
|
||||
let state = state.enter_scope();
|
||||
let state = context.enter_scope();
|
||||
eval_block(&state, block)
|
||||
}
|
||||
Expr::Block(block_id) => Ok(Value::Block {
|
||||
@ -241,7 +102,7 @@ pub fn eval_expression(state: &EvaluationContext, expr: &Expression) -> Result<V
|
||||
Expr::List(x) => {
|
||||
let mut output = vec![];
|
||||
for expr in x {
|
||||
output.push(eval_expression(state, expr)?);
|
||||
output.push(eval_expression(context, expr)?);
|
||||
}
|
||||
Ok(Value::List {
|
||||
val: output.into_value_stream(),
|
||||
@ -251,14 +112,14 @@ pub fn eval_expression(state: &EvaluationContext, expr: &Expression) -> Result<V
|
||||
Expr::Table(headers, vals) => {
|
||||
let mut output_headers = vec![];
|
||||
for expr in headers {
|
||||
output_headers.push(eval_expression(state, expr)?.as_string()?);
|
||||
output_headers.push(eval_expression(context, expr)?.as_string()?);
|
||||
}
|
||||
|
||||
let mut output_rows = vec![];
|
||||
for val in vals {
|
||||
let mut row = vec![];
|
||||
for expr in val {
|
||||
row.push(eval_expression(state, expr)?);
|
||||
row.push(eval_expression(context, expr)?);
|
||||
}
|
||||
output_rows.push(row);
|
||||
}
|
||||
@ -268,7 +129,7 @@ pub fn eval_expression(state: &EvaluationContext, expr: &Expression) -> Result<V
|
||||
span: expr.span,
|
||||
})
|
||||
}
|
||||
Expr::Keyword(_, _, expr) => eval_expression(state, expr),
|
||||
Expr::Keyword(_, _, expr) => eval_expression(context, expr),
|
||||
Expr::String(s) => Ok(Value::String {
|
||||
val: s.clone(),
|
||||
span: expr.span,
|
||||
|
Reference in New Issue
Block a user