2021-09-02 18:21:37 +00:00
|
|
|
use nu_protocol::ast::{Block, Call, Expr, Expression, Operator, Statement};
|
|
|
|
use nu_protocol::engine::EvaluationContext;
|
2021-09-19 19:29:58 +00:00
|
|
|
use nu_protocol::{Range, ShellError, Span, Type, Value};
|
2021-07-16 01:10:22 +00:00
|
|
|
|
2021-08-15 22:33:34 +00:00
|
|
|
pub fn eval_operator(op: &Expression) -> Result<Operator, ShellError> {
|
2021-07-23 05:14:49 +00:00
|
|
|
match op {
|
|
|
|
Expression {
|
|
|
|
expr: Expr::Operator(operator),
|
|
|
|
..
|
|
|
|
} => Ok(operator.clone()),
|
2021-09-05 23:16:27 +00:00
|
|
|
Expression { span, expr, .. } => {
|
|
|
|
Err(ShellError::UnknownOperator(format!("{:?}", expr), *span))
|
|
|
|
}
|
2021-07-16 01:10:22 +00:00
|
|
|
}
|
2021-07-23 05:14:49 +00:00
|
|
|
}
|
2021-07-16 01:10:22 +00:00
|
|
|
|
2021-09-03 02:15:01 +00:00
|
|
|
fn eval_call(context: &EvaluationContext, call: &Call, input: Value) -> Result<Value, ShellError> {
|
2021-09-02 22:58:15 +00:00
|
|
|
let engine_state = context.engine_state.borrow();
|
2021-09-02 08:25:22 +00:00
|
|
|
let decl = engine_state.get_decl(call.decl_id);
|
2021-09-05 23:16:27 +00:00
|
|
|
if let Some(block_id) = decl.get_block_id() {
|
2021-09-02 22:58:15 +00:00
|
|
|
let state = context.enter_scope();
|
2021-08-27 02:30:10 +00:00
|
|
|
for (arg, param) in call.positional.iter().zip(
|
2021-09-02 18:21:37 +00:00
|
|
|
decl.signature()
|
2021-08-27 02:30:10 +00:00
|
|
|
.required_positional
|
|
|
|
.iter()
|
2021-09-02 18:21:37 +00:00
|
|
|
.chain(decl.signature().optional_positional.iter()),
|
2021-08-27 02:30:10 +00:00
|
|
|
) {
|
2021-08-15 22:33:34 +00:00
|
|
|
let result = eval_expression(&state, arg)?;
|
2021-07-23 21:19:30 +00:00
|
|
|
let var_id = param
|
|
|
|
.var_id
|
|
|
|
.expect("internal error: all custom parameters must have var_ids");
|
|
|
|
|
2021-08-15 22:33:34 +00:00
|
|
|
state.add_var(var_id, result);
|
2021-07-23 21:19:30 +00:00
|
|
|
}
|
2021-09-07 03:37:02 +00:00
|
|
|
|
|
|
|
if let Some(rest_positional) = decl.signature().rest_positional {
|
|
|
|
let mut rest_items = vec![];
|
|
|
|
|
|
|
|
for arg in call.positional.iter().skip(
|
|
|
|
decl.signature().required_positional.len()
|
|
|
|
+ decl.signature().optional_positional.len(),
|
|
|
|
) {
|
|
|
|
let result = eval_expression(&state, arg)?;
|
|
|
|
rest_items.push(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
let span = if let Some(rest_item) = rest_items.first() {
|
|
|
|
rest_item.span()
|
|
|
|
} else {
|
|
|
|
Span::unknown()
|
|
|
|
};
|
|
|
|
|
|
|
|
state.add_var(
|
|
|
|
rest_positional
|
|
|
|
.var_id
|
|
|
|
.expect("Internal error: rest positional parameter lacks var_id"),
|
|
|
|
Value::List {
|
2021-09-07 07:07:11 +00:00
|
|
|
vals: rest_items,
|
2021-09-07 03:37:02 +00:00
|
|
|
span,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
2021-09-02 08:25:22 +00:00
|
|
|
let engine_state = state.engine_state.borrow();
|
|
|
|
let block = engine_state.get_block(block_id);
|
2021-09-03 02:15:01 +00:00
|
|
|
eval_block(&state, block, input)
|
2021-07-29 22:56:51 +00:00
|
|
|
} else {
|
2021-09-03 02:15:01 +00:00
|
|
|
decl.run(context, call, input)
|
2021-07-16 01:10:22 +00:00
|
|
|
}
|
2021-07-23 05:14:49 +00:00
|
|
|
}
|
2021-07-16 01:10:22 +00:00
|
|
|
|
2021-09-19 21:48:33 +00:00
|
|
|
fn eval_external(
|
|
|
|
context: &EvaluationContext,
|
|
|
|
name: &Span,
|
|
|
|
args: &[Span],
|
|
|
|
input: Value,
|
2021-09-23 16:42:03 +00:00
|
|
|
last_expression: bool,
|
2021-09-19 21:48:33 +00:00
|
|
|
) -> Result<Value, ShellError> {
|
|
|
|
let engine_state = context.engine_state.borrow();
|
|
|
|
|
|
|
|
let decl_id = engine_state
|
|
|
|
.find_decl("run_external".as_bytes())
|
|
|
|
.ok_or_else(|| ShellError::ExternalNotSupported(*name))?;
|
|
|
|
|
|
|
|
let command = engine_state.get_decl(decl_id);
|
|
|
|
|
|
|
|
let mut call = Call::new();
|
|
|
|
call.positional = [*name]
|
|
|
|
.iter()
|
|
|
|
.chain(args.iter())
|
|
|
|
.map(|span| {
|
|
|
|
let contents = engine_state.get_span_contents(span);
|
|
|
|
let val = String::from_utf8_lossy(contents);
|
|
|
|
Expression {
|
|
|
|
expr: Expr::String(val.into()),
|
|
|
|
span: *span,
|
|
|
|
ty: Type::String,
|
|
|
|
custom_completion: None,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
2021-09-23 16:42:03 +00:00
|
|
|
if last_expression {
|
|
|
|
call.named.push(("last_expression".into(), None))
|
|
|
|
}
|
|
|
|
|
2021-09-20 09:42:03 +00:00
|
|
|
command.run(context, &call, input)
|
2021-09-19 21:48:33 +00:00
|
|
|
}
|
|
|
|
|
2021-09-02 22:58:15 +00:00
|
|
|
pub fn eval_expression(
|
|
|
|
context: &EvaluationContext,
|
|
|
|
expr: &Expression,
|
|
|
|
) -> Result<Value, ShellError> {
|
2021-07-23 05:14:49 +00:00
|
|
|
match &expr.expr {
|
2021-07-24 05:57:17 +00:00
|
|
|
Expr::Bool(b) => Ok(Value::Bool {
|
|
|
|
val: *b,
|
|
|
|
span: expr.span,
|
|
|
|
}),
|
2021-07-23 05:14:49 +00:00
|
|
|
Expr::Int(i) => Ok(Value::Int {
|
|
|
|
val: *i,
|
|
|
|
span: expr.span,
|
|
|
|
}),
|
2021-08-08 20:21:21 +00:00
|
|
|
Expr::Float(f) => Ok(Value::Float {
|
|
|
|
val: *f,
|
|
|
|
span: expr.span,
|
|
|
|
}),
|
2021-09-11 11:13:04 +00:00
|
|
|
Expr::Range(from, next, to, operator) => {
|
2021-09-04 21:52:57 +00:00
|
|
|
let from = if let Some(f) = from {
|
2021-09-04 22:40:15 +00:00
|
|
|
eval_expression(context, f)?
|
2021-09-04 21:52:57 +00:00
|
|
|
} else {
|
2021-09-11 11:13:04 +00:00
|
|
|
Value::Nothing {
|
2021-09-04 21:52:57 +00:00
|
|
|
span: Span::unknown(),
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-09-11 11:13:04 +00:00
|
|
|
let next = if let Some(s) = next {
|
|
|
|
eval_expression(context, s)?
|
2021-09-04 21:52:57 +00:00
|
|
|
} else {
|
2021-09-11 11:13:04 +00:00
|
|
|
Value::Nothing {
|
2021-09-04 21:52:57 +00:00
|
|
|
span: Span::unknown(),
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-09-11 11:13:04 +00:00
|
|
|
let to = if let Some(t) = to {
|
|
|
|
eval_expression(context, t)?
|
|
|
|
} else {
|
|
|
|
Value::Nothing {
|
|
|
|
span: Span::unknown(),
|
2021-09-04 21:52:57 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(Value::Range {
|
2021-09-11 11:13:04 +00:00
|
|
|
val: Box::new(Range::new(expr.span, from, next, to, operator)?),
|
2021-09-04 21:52:57 +00:00
|
|
|
span: expr.span,
|
|
|
|
})
|
|
|
|
}
|
2021-09-02 22:58:15 +00:00
|
|
|
Expr::Var(var_id) => context
|
2021-07-30 21:57:22 +00:00
|
|
|
.get_var(*var_id)
|
2021-09-06 02:20:02 +00:00
|
|
|
.map_err(move |_| ShellError::VariableNotFoundAtRuntime(expr.span)),
|
2021-10-02 02:59:11 +00:00
|
|
|
Expr::CellPath(cell_path) => Ok(Value::CellPath {
|
|
|
|
val: cell_path.clone(),
|
|
|
|
span: expr.span,
|
|
|
|
}),
|
2021-09-26 18:39:19 +00:00
|
|
|
Expr::FullCellPath(cell_path) => {
|
|
|
|
let value = eval_expression(context, &cell_path.head)?;
|
2021-09-06 22:02:24 +00:00
|
|
|
|
2021-09-26 18:39:19 +00:00
|
|
|
value.follow_cell_path(&cell_path.tail)
|
2021-09-06 22:02:24 +00:00
|
|
|
}
|
2021-09-09 21:47:20 +00:00
|
|
|
Expr::RowCondition(_, expr) => eval_expression(context, expr),
|
2021-09-03 03:49:14 +00:00
|
|
|
Expr::Call(call) => eval_call(context, call, Value::nothing()),
|
2021-09-23 16:42:03 +00:00
|
|
|
Expr::ExternalCall(name, args) => {
|
|
|
|
eval_external(context, name, args, Value::nothing(), true)
|
|
|
|
}
|
2021-08-08 21:02:47 +00:00
|
|
|
Expr::Operator(_) => Ok(Value::Nothing { span: expr.span }),
|
2021-07-23 05:14:49 +00:00
|
|
|
Expr::BinaryOp(lhs, op, rhs) => {
|
2021-08-10 06:31:34 +00:00
|
|
|
let op_span = op.span;
|
2021-09-02 22:58:15 +00:00
|
|
|
let lhs = eval_expression(context, lhs)?;
|
2021-08-15 22:33:34 +00:00
|
|
|
let op = eval_operator(op)?;
|
2021-09-02 22:58:15 +00:00
|
|
|
let rhs = eval_expression(context, rhs)?;
|
2021-07-23 05:14:49 +00:00
|
|
|
|
|
|
|
match op {
|
2021-08-10 06:31:34 +00:00
|
|
|
Operator::Plus => lhs.add(op_span, &rhs),
|
2021-08-25 19:29:36 +00:00
|
|
|
Operator::Minus => lhs.sub(op_span, &rhs),
|
|
|
|
Operator::Multiply => lhs.mul(op_span, &rhs),
|
|
|
|
Operator::Divide => lhs.div(op_span, &rhs),
|
|
|
|
Operator::LessThan => lhs.lt(op_span, &rhs),
|
|
|
|
Operator::LessThanOrEqual => lhs.lte(op_span, &rhs),
|
|
|
|
Operator::GreaterThan => lhs.gt(op_span, &rhs),
|
|
|
|
Operator::GreaterThanOrEqual => lhs.gte(op_span, &rhs),
|
|
|
|
Operator::Equal => lhs.eq(op_span, &rhs),
|
|
|
|
Operator::NotEqual => lhs.ne(op_span, &rhs),
|
2021-09-05 23:16:27 +00:00
|
|
|
x => Err(ShellError::UnsupportedOperator(x, op_span)),
|
2021-07-23 05:14:49 +00:00
|
|
|
}
|
2021-07-16 01:10:22 +00:00
|
|
|
}
|
2021-07-23 05:14:49 +00:00
|
|
|
Expr::Subexpression(block_id) => {
|
2021-09-02 22:58:15 +00:00
|
|
|
let engine_state = context.engine_state.borrow();
|
2021-09-02 08:25:22 +00:00
|
|
|
let block = engine_state.get_block(*block_id);
|
2021-07-23 05:14:49 +00:00
|
|
|
|
2021-09-02 22:58:15 +00:00
|
|
|
let state = context.enter_scope();
|
2021-09-03 02:15:01 +00:00
|
|
|
eval_block(&state, block, Value::nothing())
|
2021-07-23 05:14:49 +00:00
|
|
|
}
|
2021-08-08 21:02:47 +00:00
|
|
|
Expr::Block(block_id) => Ok(Value::Block {
|
|
|
|
val: *block_id,
|
|
|
|
span: expr.span,
|
|
|
|
}),
|
2021-07-23 21:19:30 +00:00
|
|
|
Expr::List(x) => {
|
|
|
|
let mut output = vec![];
|
|
|
|
for expr in x {
|
2021-09-02 22:58:15 +00:00
|
|
|
output.push(eval_expression(context, expr)?);
|
2021-07-23 21:19:30 +00:00
|
|
|
}
|
2021-08-08 21:02:47 +00:00
|
|
|
Ok(Value::List {
|
2021-09-07 07:07:11 +00:00
|
|
|
vals: output,
|
2021-08-08 21:02:47 +00:00
|
|
|
span: expr.span,
|
|
|
|
})
|
2021-07-23 21:19:30 +00:00
|
|
|
}
|
2021-08-28 19:17:30 +00:00
|
|
|
Expr::Table(headers, vals) => {
|
|
|
|
let mut output_headers = vec![];
|
|
|
|
for expr in headers {
|
2021-09-02 22:58:15 +00:00
|
|
|
output_headers.push(eval_expression(context, expr)?.as_string()?);
|
2021-08-28 19:17:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut output_rows = vec![];
|
|
|
|
for val in vals {
|
|
|
|
let mut row = vec![];
|
|
|
|
for expr in val {
|
2021-09-02 22:58:15 +00:00
|
|
|
row.push(eval_expression(context, expr)?);
|
2021-08-28 19:17:30 +00:00
|
|
|
}
|
2021-09-07 07:07:11 +00:00
|
|
|
output_rows.push(Value::Record {
|
|
|
|
cols: output_headers.clone(),
|
|
|
|
vals: row,
|
|
|
|
span: expr.span,
|
|
|
|
});
|
2021-08-28 19:17:30 +00:00
|
|
|
}
|
2021-09-07 07:07:11 +00:00
|
|
|
Ok(Value::List {
|
|
|
|
vals: output_rows,
|
2021-08-28 19:17:30 +00:00
|
|
|
span: expr.span,
|
|
|
|
})
|
|
|
|
}
|
2021-09-02 22:58:15 +00:00
|
|
|
Expr::Keyword(_, _, expr) => eval_expression(context, expr),
|
2021-07-23 21:19:30 +00:00
|
|
|
Expr::String(s) => Ok(Value::String {
|
|
|
|
val: s.clone(),
|
|
|
|
span: expr.span,
|
|
|
|
}),
|
2021-08-08 21:02:47 +00:00
|
|
|
Expr::Signature(_) => Ok(Value::Nothing { span: expr.span }),
|
|
|
|
Expr::Garbage => Ok(Value::Nothing { span: expr.span }),
|
2021-07-16 01:10:22 +00:00
|
|
|
}
|
2021-07-23 05:14:49 +00:00
|
|
|
}
|
|
|
|
|
2021-09-03 02:15:01 +00:00
|
|
|
pub fn eval_block(
|
|
|
|
context: &EvaluationContext,
|
|
|
|
block: &Block,
|
|
|
|
mut input: Value,
|
|
|
|
) -> Result<Value, ShellError> {
|
2021-09-23 16:42:03 +00:00
|
|
|
for stmt in block.stmts.iter() {
|
2021-09-03 02:15:01 +00:00
|
|
|
if let Statement::Pipeline(pipeline) = stmt {
|
2021-09-23 16:42:03 +00:00
|
|
|
for (i, elem) in pipeline.expressions.iter().enumerate() {
|
2021-09-03 02:15:01 +00:00
|
|
|
match elem {
|
|
|
|
Expression {
|
|
|
|
expr: Expr::Call(call),
|
|
|
|
..
|
|
|
|
} => {
|
|
|
|
input = eval_call(context, call, input)?;
|
|
|
|
}
|
2021-09-19 21:48:33 +00:00
|
|
|
Expression {
|
|
|
|
expr: Expr::ExternalCall(name, args),
|
|
|
|
..
|
|
|
|
} => {
|
2021-09-23 16:42:03 +00:00
|
|
|
input = eval_external(
|
|
|
|
context,
|
|
|
|
name,
|
|
|
|
args,
|
|
|
|
input,
|
|
|
|
i == pipeline.expressions.len() - 1,
|
|
|
|
)?;
|
2021-09-19 21:48:33 +00:00
|
|
|
}
|
2021-09-03 02:15:01 +00:00
|
|
|
|
|
|
|
elem => {
|
|
|
|
input = eval_expression(context, elem)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-07-16 01:10:22 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-23 05:14:49 +00:00
|
|
|
|
2021-09-03 02:15:01 +00:00
|
|
|
Ok(input)
|
2021-07-16 01:10:22 +00:00
|
|
|
}
|