nushell/crates/nu-engine/src/eval.rs

261 lines
8.4 KiB
Rust
Raw Normal View History

2021-09-12 15:34:43 +00:00
use std::process::Command;
2021-09-02 18:21:37 +00:00
use nu_protocol::ast::{Block, Call, Expr, Expression, Operator, Statement};
use nu_protocol::engine::EvaluationContext;
use nu_protocol::{Range, ShellError, Span, Value};
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-23 05:14:49 +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 {
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-23 05:14:49 +00:00
}
2021-09-12 15:34:43 +00:00
pub fn eval_external(
context: &EvaluationContext,
command_span: &Span,
spans: &Vec<Span>,
expr: &Expression,
) -> Result<Value, ShellError> {
let state = context.engine_state.borrow();
let name = state.get_span_contents(command_span);
let cmd = std::str::from_utf8(name).unwrap();
let args = spans
.iter()
.map(|span| {
let val = state.get_span_contents(span);
std::str::from_utf8(val).unwrap()
})
.collect::<Vec<&str>>();
let output = Command::new(cmd).args(args).output().unwrap();
println!("{:?}", output);
let test = output.stdout;
println!("{:?}", test);
Err(ShellError::ExternalNotSupported(expr.span))
}
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,
}),
Expr::Range(from, next, to, operator) => {
let from = if let Some(f) = from {
2021-09-04 22:40:15 +00:00
eval_expression(context, f)?
} else {
Value::Nothing {
span: Span::unknown(),
}
};
let next = if let Some(s) = next {
eval_expression(context, s)?
} else {
Value::Nothing {
span: Span::unknown(),
}
};
let to = if let Some(t) = to {
eval_expression(context, t)?
} else {
Value::Nothing {
span: Span::unknown(),
}
};
Ok(Value::Range {
val: Box::new(Range::new(expr.span, from, next, to, operator)?),
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-09-06 22:02:24 +00:00
Expr::FullCellPath(column_path) => {
let value = eval_expression(context, &column_path.head)?;
value.follow_cell_path(&column_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-12 15:34:43 +00:00
Expr::ExternalCall(command_span, spans) => {
eval_external(context, command_span, spans, expr)
}
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-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 {
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
}
output_rows.push(Value::Record {
cols: output_headers.clone(),
vals: row,
span: expr.span,
});
2021-08-28 19:17:30 +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-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-07-23 05:14:49 +00:00
for stmt in &block.stmts {
2021-09-03 02:15:01 +00:00
if let Statement::Pipeline(pipeline) = stmt {
for elem in &pipeline.expressions {
match elem {
Expression {
expr: Expr::Call(call),
..
} => {
input = eval_call(context, call, input)?;
}
elem => {
input = eval_expression(context, elem)?;
}
}
}
}
}
2021-07-23 05:14:49 +00:00
2021-09-03 02:15:01 +00:00
Ok(input)
}