mirror of
https://github.com/nushell/nushell.git
synced 2025-01-22 14:18:55 +01:00
More external cleanup
This commit is contained in:
parent
b654415494
commit
64d83142c3
@ -2,8 +2,6 @@ use nu_protocol::ast::{Block, Call, Expr, Expression, Operator, Statement};
|
|||||||
use nu_protocol::engine::EvaluationContext;
|
use nu_protocol::engine::EvaluationContext;
|
||||||
use nu_protocol::{Range, ShellError, Span, Type, Unit, Value};
|
use nu_protocol::{Range, ShellError, Span, Type, Unit, Value};
|
||||||
|
|
||||||
use crate::FromValue;
|
|
||||||
|
|
||||||
pub fn eval_operator(op: &Expression) -> Result<Operator, ShellError> {
|
pub fn eval_operator(op: &Expression) -> Result<Operator, ShellError> {
|
||||||
match op {
|
match op {
|
||||||
Expression {
|
Expression {
|
||||||
@ -72,7 +70,8 @@ fn eval_call(context: &EvaluationContext, call: &Call, input: Value) -> Result<V
|
|||||||
|
|
||||||
fn eval_external(
|
fn eval_external(
|
||||||
context: &EvaluationContext,
|
context: &EvaluationContext,
|
||||||
name: &Span,
|
name: &str,
|
||||||
|
name_span: &Span,
|
||||||
args: &[Expression],
|
args: &[Expression],
|
||||||
input: Value,
|
input: Value,
|
||||||
last_expression: bool,
|
last_expression: bool,
|
||||||
@ -81,32 +80,21 @@ fn eval_external(
|
|||||||
|
|
||||||
let decl_id = engine_state
|
let decl_id = engine_state
|
||||||
.find_decl("run_external".as_bytes())
|
.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 command = engine_state.get_decl(decl_id);
|
||||||
|
|
||||||
let mut call = Call::new();
|
let mut call = Call::new();
|
||||||
|
|
||||||
let name_span = name;
|
|
||||||
let name = engine_state.get_span_contents(name);
|
|
||||||
call.positional.push(Expression {
|
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,
|
span: *name_span,
|
||||||
ty: Type::String,
|
ty: Type::String,
|
||||||
custom_completion: None,
|
custom_completion: None,
|
||||||
});
|
});
|
||||||
|
|
||||||
for arg in args {
|
for arg in args {
|
||||||
let span = arg.span;
|
call.positional.push(arg.clone())
|
||||||
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,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if last_expression {
|
if last_expression {
|
||||||
@ -181,8 +169,8 @@ pub fn eval_expression(
|
|||||||
}
|
}
|
||||||
Expr::RowCondition(_, expr) => eval_expression(context, expr),
|
Expr::RowCondition(_, expr) => eval_expression(context, expr),
|
||||||
Expr::Call(call) => eval_call(context, call, Value::nothing()),
|
Expr::Call(call) => eval_call(context, call, Value::nothing()),
|
||||||
Expr::ExternalCall(name, args) => {
|
Expr::ExternalCall(name, span, args) => {
|
||||||
eval_external(context, name, args, Value::nothing(), true)
|
eval_external(context, name, span, args, Value::nothing(), true)
|
||||||
}
|
}
|
||||||
Expr::Operator(_) => Ok(Value::Nothing { span: expr.span }),
|
Expr::Operator(_) => Ok(Value::Nothing { span: expr.span }),
|
||||||
Expr::BinaryOp(lhs, op, rhs) => {
|
Expr::BinaryOp(lhs, op, rhs) => {
|
||||||
@ -283,12 +271,13 @@ pub fn eval_block(
|
|||||||
input = eval_call(context, call, input)?;
|
input = eval_call(context, call, input)?;
|
||||||
}
|
}
|
||||||
Expression {
|
Expression {
|
||||||
expr: Expr::ExternalCall(name, args),
|
expr: Expr::ExternalCall(name, name_span, args),
|
||||||
..
|
..
|
||||||
} => {
|
} => {
|
||||||
input = eval_external(
|
input = eval_external(
|
||||||
context,
|
context,
|
||||||
name,
|
name,
|
||||||
|
name_span,
|
||||||
args,
|
args,
|
||||||
input,
|
input,
|
||||||
i == pipeline.expressions.len() - 1,
|
i == pipeline.expressions.len() - 1,
|
||||||
|
@ -63,8 +63,8 @@ pub fn flatten_expression(
|
|||||||
}
|
}
|
||||||
output
|
output
|
||||||
}
|
}
|
||||||
Expr::ExternalCall(name, args) => {
|
Expr::ExternalCall(_, name_span, args) => {
|
||||||
let mut output = vec![(*name, FlatShape::External)];
|
let mut output = vec![(*name_span, FlatShape::External)];
|
||||||
|
|
||||||
for arg in args {
|
for arg in args {
|
||||||
//output.push((*arg, FlatShape::ExternalArg));
|
//output.push((*arg, FlatShape::ExternalArg));
|
||||||
|
@ -108,7 +108,8 @@ pub fn parse_external_call(
|
|||||||
spans: &[Span],
|
spans: &[Span],
|
||||||
) -> (Expression, Option<ParseError>) {
|
) -> (Expression, Option<ParseError>) {
|
||||||
let mut args = vec![];
|
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;
|
let mut error = None;
|
||||||
|
|
||||||
for span in &spans[1..] {
|
for span in &spans[1..] {
|
||||||
@ -129,7 +130,7 @@ pub fn parse_external_call(
|
|||||||
}
|
}
|
||||||
(
|
(
|
||||||
Expression {
|
Expression {
|
||||||
expr: Expr::ExternalCall(name, args),
|
expr: Expr::ExternalCall(name, name_span, args),
|
||||||
span: span(spans),
|
span: span(spans),
|
||||||
ty: Type::Unknown,
|
ty: Type::Unknown,
|
||||||
custom_completion: None,
|
custom_completion: None,
|
||||||
|
@ -14,7 +14,7 @@ pub enum Expr {
|
|||||||
),
|
),
|
||||||
Var(VarId),
|
Var(VarId),
|
||||||
Call(Box<Call>),
|
Call(Box<Call>),
|
||||||
ExternalCall(Span, Vec<Expression>),
|
ExternalCall(String, Span, Vec<Expression>),
|
||||||
Operator(Operator),
|
Operator(Operator),
|
||||||
RowCondition(VarId, Box<Expression>),
|
RowCondition(VarId, Box<Expression>),
|
||||||
BinaryOp(Box<Expression>, Box<Expression>, Box<Expression>), //lhs, op, rhs
|
BinaryOp(Box<Expression>, Box<Expression>, Box<Expression>), //lhs, op, rhs
|
||||||
|
Loading…
Reference in New Issue
Block a user