Add eval and argument implementation
Signed-off-by: trivernis <trivernis@protonmail.com>pull/3/head
parent
247550ac4e
commit
ca5057ea04
@ -0,0 +1,35 @@
|
||||
use nu_protocol::{ast::Expression, Span, Spanned};
|
||||
|
||||
use crate::{into_expression::IntoExpression, NewEmpty};
|
||||
|
||||
/// A struct representing the argument to a function
|
||||
pub enum Argument {
|
||||
Named((String, Option<Expression>)),
|
||||
Positional(Expression),
|
||||
}
|
||||
|
||||
impl Argument {
|
||||
/// Creates a new named argument. No value means passing the argument as a flag (like --verbose)
|
||||
pub fn named<S: ToString, E: IntoExpression>(name: S, value: Option<E>) -> Self {
|
||||
Self::Named((name.to_string(), value.map(|v| v.into_expression())))
|
||||
}
|
||||
|
||||
/// Creates a new positional argument
|
||||
pub fn positional<E: IntoExpression>(value: E) -> Self {
|
||||
Self::Positional(value.into_expression())
|
||||
}
|
||||
|
||||
pub(crate) fn into_nu_argument(self) -> nu_protocol::ast::Argument {
|
||||
match self {
|
||||
Argument::Named((name, value)) => nu_protocol::ast::Argument::Named((
|
||||
Spanned {
|
||||
item: name,
|
||||
span: Span::empty(),
|
||||
},
|
||||
None,
|
||||
value,
|
||||
)),
|
||||
Argument::Positional(value) => nu_protocol::ast::Argument::Positional(value),
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,70 @@
|
||||
use nu_protocol::{
|
||||
ast::{Expr, Expression},
|
||||
Span, Value,
|
||||
};
|
||||
|
||||
use crate::{IntoValue, NewEmpty};
|
||||
|
||||
pub trait IntoExpression {
|
||||
fn into_expression(self) -> Expression;
|
||||
}
|
||||
|
||||
trait ValueIntoExpression {
|
||||
fn into_expression(self) -> Expression;
|
||||
fn into_expr(self) -> Expr;
|
||||
}
|
||||
|
||||
impl<V: IntoValue> IntoExpression for V {
|
||||
#[inline]
|
||||
fn into_expression(self) -> Expression {
|
||||
self.into_value().into_expression()
|
||||
}
|
||||
}
|
||||
|
||||
impl ValueIntoExpression for Value {
|
||||
fn into_expression(self) -> Expression {
|
||||
let ty = self.get_type();
|
||||
|
||||
Expression {
|
||||
expr: self.into_expr(),
|
||||
span: Span::empty(),
|
||||
ty,
|
||||
custom_completion: None,
|
||||
}
|
||||
}
|
||||
|
||||
fn into_expr(self) -> Expr {
|
||||
match self {
|
||||
Value::Bool { val, .. } => Expr::Bool(val),
|
||||
Value::Int { val, .. } => Expr::Int(val),
|
||||
Value::Float { val, .. } => Expr::Float(val),
|
||||
Value::Filesize { val, .. } => Expr::Int(val),
|
||||
Value::Duration { val, .. } => Expr::Int(val),
|
||||
Value::Date { val, .. } => Expr::DateTime(val),
|
||||
Value::String { val, .. } => Expr::String(val),
|
||||
Value::Record {
|
||||
mut cols, mut vals, ..
|
||||
} => {
|
||||
let mut entries = Vec::new();
|
||||
|
||||
for i in 0..cols.len() {
|
||||
let col = cols.remove(i).into_expression();
|
||||
let val = vals.remove(i).into_expression();
|
||||
entries.push((col, val));
|
||||
}
|
||||
|
||||
Expr::Record(entries)
|
||||
}
|
||||
Value::List { vals, .. } => {
|
||||
let vals = vals.into_iter().map(|v| v.into_expression()).collect();
|
||||
Expr::List(vals)
|
||||
}
|
||||
Value::Block { val, .. } => Expr::Block(val),
|
||||
Value::Nothing { .. } => Expr::Nothing,
|
||||
Value::Error { error } => Expr::String(error.to_string()),
|
||||
Value::Binary { val, .. } => Expr::Binary(val),
|
||||
Value::CellPath { val, .. } => Expr::CellPath(val),
|
||||
_ => Expr::Nothing,
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue