2021-09-02 20:21:37 +02:00
|
|
|
use nu_protocol::ast::{Block, Call, Expr, Expression, Operator, Statement};
|
|
|
|
use nu_protocol::engine::EvaluationContext;
|
2021-09-02 03:29:43 +02:00
|
|
|
use nu_protocol::{IntoRowStream, IntoValueStream, ShellError, Span, Value};
|
2021-07-16 03:10:22 +02:00
|
|
|
|
2021-08-16 00:33:34 +02:00
|
|
|
pub fn eval_operator(op: &Expression) -> Result<Operator, ShellError> {
|
2021-07-23 07:14:49 +02:00
|
|
|
match op {
|
|
|
|
Expression {
|
|
|
|
expr: Expr::Operator(operator),
|
|
|
|
..
|
|
|
|
} => Ok(operator.clone()),
|
2021-08-10 07:55:25 +02:00
|
|
|
Expression { span, .. } => Err(ShellError::Unsupported(*span)),
|
2021-07-16 03:10:22 +02:00
|
|
|
}
|
2021-07-23 07:14:49 +02:00
|
|
|
}
|
2021-07-16 03:10:22 +02:00
|
|
|
|
2021-09-03 00:58:15 +02:00
|
|
|
fn eval_call(context: &EvaluationContext, call: &Call) -> Result<Value, ShellError> {
|
|
|
|
let engine_state = context.engine_state.borrow();
|
2021-09-02 10:25:22 +02:00
|
|
|
let decl = engine_state.get_decl(call.decl_id);
|
2021-09-02 20:21:37 +02:00
|
|
|
if let Some(block_id) = decl.get_custom_command() {
|
2021-09-03 00:58:15 +02:00
|
|
|
let state = context.enter_scope();
|
2021-08-27 04:30:10 +02:00
|
|
|
for (arg, param) in call.positional.iter().zip(
|
2021-09-02 20:21:37 +02:00
|
|
|
decl.signature()
|
2021-08-27 04:30:10 +02:00
|
|
|
.required_positional
|
|
|
|
.iter()
|
2021-09-02 20:21:37 +02:00
|
|
|
.chain(decl.signature().optional_positional.iter()),
|
2021-08-27 04:30:10 +02:00
|
|
|
) {
|
2021-08-16 00:33:34 +02:00
|
|
|
let result = eval_expression(&state, arg)?;
|
2021-07-23 23:19:30 +02:00
|
|
|
let var_id = param
|
|
|
|
.var_id
|
|
|
|
.expect("internal error: all custom parameters must have var_ids");
|
|
|
|
|
2021-08-16 00:33:34 +02:00
|
|
|
state.add_var(var_id, result);
|
2021-07-23 23:19:30 +02:00
|
|
|
}
|
2021-09-02 10:25:22 +02:00
|
|
|
let engine_state = state.engine_state.borrow();
|
|
|
|
let block = engine_state.get_block(block_id);
|
2021-08-16 00:33:34 +02:00
|
|
|
eval_block(&state, block)
|
2021-07-30 00:56:51 +02:00
|
|
|
} else {
|
2021-09-03 00:58:15 +02:00
|
|
|
decl.run(
|
|
|
|
context,
|
|
|
|
call,
|
|
|
|
Value::Nothing {
|
|
|
|
span: Span::unknown(),
|
|
|
|
},
|
|
|
|
)
|
2021-07-16 03:10:22 +02:00
|
|
|
}
|
2021-07-23 07:14:49 +02:00
|
|
|
}
|
2021-07-16 03:10:22 +02:00
|
|
|
|
2021-09-03 00:58:15 +02:00
|
|
|
pub fn eval_expression(
|
|
|
|
context: &EvaluationContext,
|
|
|
|
expr: &Expression,
|
|
|
|
) -> Result<Value, ShellError> {
|
2021-07-23 07:14:49 +02:00
|
|
|
match &expr.expr {
|
2021-07-24 07:57:17 +02:00
|
|
|
Expr::Bool(b) => Ok(Value::Bool {
|
|
|
|
val: *b,
|
|
|
|
span: expr.span,
|
|
|
|
}),
|
2021-07-23 07:14:49 +02:00
|
|
|
Expr::Int(i) => Ok(Value::Int {
|
|
|
|
val: *i,
|
|
|
|
span: expr.span,
|
|
|
|
}),
|
2021-08-08 22:21:21 +02:00
|
|
|
Expr::Float(f) => Ok(Value::Float {
|
|
|
|
val: *f,
|
|
|
|
span: expr.span,
|
|
|
|
}),
|
2021-09-03 00:58:15 +02:00
|
|
|
Expr::Var(var_id) => context
|
2021-07-30 23:57:22 +02:00
|
|
|
.get_var(*var_id)
|
|
|
|
.map_err(move |_| ShellError::VariableNotFound(expr.span)),
|
2021-09-03 00:58:15 +02:00
|
|
|
Expr::Call(call) => eval_call(context, call),
|
2021-07-23 07:14:49 +02:00
|
|
|
Expr::ExternalCall(_, _) => Err(ShellError::Unsupported(expr.span)),
|
2021-08-08 23:02:47 +02:00
|
|
|
Expr::Operator(_) => Ok(Value::Nothing { span: expr.span }),
|
2021-07-23 07:14:49 +02:00
|
|
|
Expr::BinaryOp(lhs, op, rhs) => {
|
2021-08-10 08:31:34 +02:00
|
|
|
let op_span = op.span;
|
2021-09-03 00:58:15 +02:00
|
|
|
let lhs = eval_expression(context, lhs)?;
|
2021-08-16 00:33:34 +02:00
|
|
|
let op = eval_operator(op)?;
|
2021-09-03 00:58:15 +02:00
|
|
|
let rhs = eval_expression(context, rhs)?;
|
2021-07-23 07:14:49 +02:00
|
|
|
|
|
|
|
match op {
|
2021-08-10 08:31:34 +02:00
|
|
|
Operator::Plus => lhs.add(op_span, &rhs),
|
2021-08-25 21:29:36 +02: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),
|
|
|
|
_ => Err(ShellError::Unsupported(op_span)),
|
2021-07-23 07:14:49 +02:00
|
|
|
}
|
2021-07-16 03:10:22 +02:00
|
|
|
}
|
2021-07-23 07:14:49 +02:00
|
|
|
|
|
|
|
Expr::Subexpression(block_id) => {
|
2021-09-03 00:58:15 +02:00
|
|
|
let engine_state = context.engine_state.borrow();
|
2021-09-02 10:25:22 +02:00
|
|
|
let block = engine_state.get_block(*block_id);
|
2021-07-23 07:14:49 +02:00
|
|
|
|
2021-09-03 00:58:15 +02:00
|
|
|
let state = context.enter_scope();
|
2021-08-16 00:33:34 +02:00
|
|
|
eval_block(&state, block)
|
2021-07-23 07:14:49 +02:00
|
|
|
}
|
2021-08-08 23:02:47 +02:00
|
|
|
Expr::Block(block_id) => Ok(Value::Block {
|
|
|
|
val: *block_id,
|
|
|
|
span: expr.span,
|
|
|
|
}),
|
2021-07-23 23:19:30 +02:00
|
|
|
Expr::List(x) => {
|
|
|
|
let mut output = vec![];
|
|
|
|
for expr in x {
|
2021-09-03 00:58:15 +02:00
|
|
|
output.push(eval_expression(context, expr)?);
|
2021-07-23 23:19:30 +02:00
|
|
|
}
|
2021-08-08 23:02:47 +02:00
|
|
|
Ok(Value::List {
|
2021-09-01 23:20:53 +02:00
|
|
|
val: output.into_value_stream(),
|
2021-08-08 23:02:47 +02:00
|
|
|
span: expr.span,
|
|
|
|
})
|
2021-07-23 23:19:30 +02:00
|
|
|
}
|
2021-08-28 21:17:30 +02:00
|
|
|
Expr::Table(headers, vals) => {
|
|
|
|
let mut output_headers = vec![];
|
|
|
|
for expr in headers {
|
2021-09-03 00:58:15 +02:00
|
|
|
output_headers.push(eval_expression(context, expr)?.as_string()?);
|
2021-08-28 21:17:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut output_rows = vec![];
|
|
|
|
for val in vals {
|
|
|
|
let mut row = vec![];
|
|
|
|
for expr in val {
|
2021-09-03 00:58:15 +02:00
|
|
|
row.push(eval_expression(context, expr)?);
|
2021-08-28 21:17:30 +02:00
|
|
|
}
|
|
|
|
output_rows.push(row);
|
|
|
|
}
|
|
|
|
Ok(Value::Table {
|
|
|
|
headers: output_headers,
|
2021-09-01 23:20:53 +02:00
|
|
|
val: output_rows.into_row_stream(),
|
2021-08-28 21:17:30 +02:00
|
|
|
span: expr.span,
|
|
|
|
})
|
|
|
|
}
|
2021-09-03 00:58:15 +02:00
|
|
|
Expr::Keyword(_, _, expr) => eval_expression(context, expr),
|
2021-07-23 23:19:30 +02:00
|
|
|
Expr::String(s) => Ok(Value::String {
|
|
|
|
val: s.clone(),
|
|
|
|
span: expr.span,
|
|
|
|
}),
|
2021-08-08 23:02:47 +02:00
|
|
|
Expr::Signature(_) => Ok(Value::Nothing { span: expr.span }),
|
|
|
|
Expr::Garbage => Ok(Value::Nothing { span: expr.span }),
|
2021-07-16 03:10:22 +02:00
|
|
|
}
|
2021-07-23 07:14:49 +02:00
|
|
|
}
|
|
|
|
|
2021-09-02 20:21:37 +02:00
|
|
|
pub fn eval_block(state: &EvaluationContext, block: &Block) -> Result<Value, ShellError> {
|
2021-08-08 23:02:47 +02:00
|
|
|
let mut last = Ok(Value::Nothing {
|
|
|
|
span: Span { start: 0, end: 0 },
|
|
|
|
});
|
2021-07-16 03:10:22 +02:00
|
|
|
|
2021-07-23 07:14:49 +02:00
|
|
|
for stmt in &block.stmts {
|
2021-07-30 00:56:51 +02:00
|
|
|
if let Statement::Expression(expression) = stmt {
|
2021-08-16 00:33:34 +02:00
|
|
|
last = Ok(eval_expression(state, expression)?);
|
2021-07-16 03:10:22 +02:00
|
|
|
}
|
|
|
|
}
|
2021-07-23 07:14:49 +02:00
|
|
|
|
|
|
|
last
|
2021-07-16 03:10:22 +02:00
|
|
|
}
|