More external cleanup

This commit is contained in:
JT 2021-10-09 11:30:10 +13:00
parent b654415494
commit 64d83142c3
4 changed files with 15 additions and 25 deletions

View File

@ -2,8 +2,6 @@ use nu_protocol::ast::{Block, Call, Expr, Expression, Operator, Statement};
use nu_protocol::engine::EvaluationContext;
use nu_protocol::{Range, ShellError, Span, Type, Unit, Value};
use crate::FromValue;
pub fn eval_operator(op: &Expression) -> Result<Operator, ShellError> {
match op {
Expression {
@ -72,7 +70,8 @@ fn eval_call(context: &EvaluationContext, call: &Call, input: Value) -> Result<V
fn eval_external(
context: &EvaluationContext,
name: &Span,
name: &str,
name_span: &Span,
args: &[Expression],
input: Value,
last_expression: bool,
@ -81,32 +80,21 @@ fn eval_external(
let decl_id = engine_state
.find_decl("run_external".as_bytes())
.ok_or_else(|| ShellError::ExternalNotSupported(*name))?;
.ok_or_else(|| ShellError::ExternalNotSupported(*name_span))?;
let command = engine_state.get_decl(decl_id);
let mut call = Call::new();
let name_span = name;
let name = engine_state.get_span_contents(name);
call.positional.push(Expression {
expr: Expr::String(String::from_utf8_lossy(name).to_string()),
expr: Expr::String(name.trim_start_matches('^').to_string()),
span: *name_span,
ty: Type::String,
custom_completion: None,
});
for arg in args {
let span = arg.span;
let result = eval_expression(context, arg)?;
let result: String = FromValue::from_value(&result)?;
call.positional.push(Expression {
expr: Expr::String(result),
span,
ty: Type::String,
custom_completion: None,
})
call.positional.push(arg.clone())
}
if last_expression {
@ -181,8 +169,8 @@ pub fn eval_expression(
}
Expr::RowCondition(_, expr) => eval_expression(context, expr),
Expr::Call(call) => eval_call(context, call, Value::nothing()),
Expr::ExternalCall(name, args) => {
eval_external(context, name, args, Value::nothing(), true)
Expr::ExternalCall(name, span, args) => {
eval_external(context, name, span, args, Value::nothing(), true)
}
Expr::Operator(_) => Ok(Value::Nothing { span: expr.span }),
Expr::BinaryOp(lhs, op, rhs) => {
@ -283,12 +271,13 @@ pub fn eval_block(
input = eval_call(context, call, input)?;
}
Expression {
expr: Expr::ExternalCall(name, args),
expr: Expr::ExternalCall(name, name_span, args),
..
} => {
input = eval_external(
context,
name,
name_span,
args,
input,
i == pipeline.expressions.len() - 1,

View File

@ -63,8 +63,8 @@ pub fn flatten_expression(
}
output
}
Expr::ExternalCall(name, args) => {
let mut output = vec![(*name, FlatShape::External)];
Expr::ExternalCall(_, name_span, args) => {
let mut output = vec![(*name_span, FlatShape::External)];
for arg in args {
//output.push((*arg, FlatShape::ExternalArg));

View File

@ -108,7 +108,8 @@ pub fn parse_external_call(
spans: &[Span],
) -> (Expression, Option<ParseError>) {
let mut args = vec![];
let name = spans[0];
let name_span = spans[0];
let name = String::from_utf8_lossy(working_set.get_span_contents(name_span)).to_string();
let mut error = None;
for span in &spans[1..] {
@ -129,7 +130,7 @@ pub fn parse_external_call(
}
(
Expression {
expr: Expr::ExternalCall(name, args),
expr: Expr::ExternalCall(name, name_span, args),
span: span(spans),
ty: Type::Unknown,
custom_completion: None,

View File

@ -14,7 +14,7 @@ pub enum Expr {
),
Var(VarId),
Call(Box<Call>),
ExternalCall(Span, Vec<Expression>),
ExternalCall(String, Span, Vec<Expression>),
Operator(Operator),
RowCondition(VarId, Box<Expression>),
BinaryOp(Box<Expression>, Box<Expression>, Box<Expression>), //lhs, op, rhs