use crate::{
ast::{self, Expression},
ir, DeclId, FromValue, ShellError, Span, Value,
};
use super::{EngineState, Stack, StateWorkingSet};
#[derive(Debug, Clone)]
pub struct Call<'a> {
pub head: Span,
pub decl_id: DeclId,
pub inner: CallImpl<'a>,
}
#[derive(Debug, Clone)]
pub enum CallImpl<'a> {
AstRef(&'a ast::Call),
AstBox(Box<ast::Call>),
IrRef(&'a ir::Call),
IrBox(Box<ir::Call>),
}
impl Call<'_> {
pub fn new(span: Span) -> Self {
Call {
head: span,
decl_id: DeclId::new(0),
inner: CallImpl::AstBox(Box::new(ast::Call::new(span))),
}
}
pub fn to_owned(&self) -> Call<'static> {
Call {
head: self.head,
decl_id: self.decl_id,
inner: self.inner.to_owned(),
}
}
pub fn assert_ast_call(&self) -> Result<&ast::Call, ShellError> {
match &self.inner {
CallImpl::AstRef(call) => Ok(call),
CallImpl::AstBox(call) => Ok(call),
_ => Err(ShellError::NushellFailedSpanned {
msg: "Can't be used in IR context".into(),
label: "this command is not yet supported by IR evaluation".into(),
span: self.head,
}),
}
}
pub fn has_flag_const(
&self,
working_set: &StateWorkingSet,
flag_name: &str,
) -> Result<bool, ShellError> {
self.assert_ast_call()?
.has_flag_const(working_set, flag_name)
}
pub fn get_flag_const<T: FromValue>(
&self,
working_set: &StateWorkingSet,
name: &str,
) -> Result<Option<T>, ShellError> {
self.assert_ast_call()?.get_flag_const(working_set, name)
}
pub fn req_const<T: FromValue>(
&self,
working_set: &StateWorkingSet,
pos: usize,
) -> Result<T, ShellError> {
self.assert_ast_call()?.req_const(working_set, pos)
}
pub fn rest_const<T: FromValue>(
&self,
working_set: &StateWorkingSet,
starting_pos: usize,
) -> Result<Vec<T>, ShellError> {
self.assert_ast_call()?
.rest_const(working_set, starting_pos)
}
pub fn arguments_span(&self) -> Span {
match &self.inner {
CallImpl::AstRef(call) => call.arguments_span(),
CallImpl::AstBox(call) => call.arguments_span(),
CallImpl::IrRef(call) => call.arguments_span(),
CallImpl::IrBox(call) => call.arguments_span(),
}
}
pub fn span(&self) -> Span {
match &self.inner {
CallImpl::AstRef(call) => call.span(),
CallImpl::AstBox(call) => call.span(),
CallImpl::IrRef(call) => call.span(),
CallImpl::IrBox(call) => call.span(),
}
}
pub fn get_parser_info<'a>(&'a self, stack: &'a Stack, name: &str) -> Option<&'a Expression> {
match &self.inner {
CallImpl::AstRef(call) => call.get_parser_info(name),
CallImpl::AstBox(call) => call.get_parser_info(name),
CallImpl::IrRef(call) => call.get_parser_info(stack, name),
CallImpl::IrBox(call) => call.get_parser_info(stack, name),
}
}
pub fn rest_iter_flattened(
&self,
engine_state: &EngineState,
stack: &mut Stack,
eval_expression: fn(
&EngineState,
&mut Stack,
&ast::Expression,
) -> Result<Value, ShellError>,
starting_pos: usize,
) -> Result<Vec<Value>, ShellError> {
fn by_ast(
call: &ast::Call,
engine_state: &EngineState,
stack: &mut Stack,
eval_expression: fn(
&EngineState,
&mut Stack,
&ast::Expression,
) -> Result<Value, ShellError>,
starting_pos: usize,
) -> Result<Vec<Value>, ShellError> {
call.rest_iter_flattened(starting_pos, |expr| {
eval_expression(engine_state, stack, expr)
})
}
fn by_ir(
call: &ir::Call,
stack: &Stack,
starting_pos: usize,
) -> Result<Vec<Value>, ShellError> {
call.rest_iter_flattened(stack, starting_pos)
}
match &self.inner {
CallImpl::AstRef(call) => {
by_ast(call, engine_state, stack, eval_expression, starting_pos)
}
CallImpl::AstBox(call) => {
by_ast(call, engine_state, stack, eval_expression, starting_pos)
}
CallImpl::IrRef(call) => by_ir(call, stack, starting_pos),
CallImpl::IrBox(call) => by_ir(call, stack, starting_pos),
}
}
pub fn positional_nth<'a>(&'a self, stack: &'a Stack, index: usize) -> Option<&'a Expression> {
match &self.inner {
CallImpl::AstRef(call) => call.positional_nth(index),
CallImpl::AstBox(call) => call.positional_nth(index),
CallImpl::IrRef(call) => call.positional_ast(stack, index).map(|arc| arc.as_ref()),
CallImpl::IrBox(call) => call.positional_ast(stack, index).map(|arc| arc.as_ref()),
}
}
}
impl CallImpl<'_> {
pub fn to_owned(&self) -> CallImpl<'static> {
match self {
CallImpl::AstRef(call) => CallImpl::AstBox(Box::new((*call).clone())),
CallImpl::AstBox(call) => CallImpl::AstBox(call.clone()),
CallImpl::IrRef(call) => CallImpl::IrBox(Box::new((*call).clone())),
CallImpl::IrBox(call) => CallImpl::IrBox(call.clone()),
}
}
}
impl<'a> From<&'a ast::Call> for Call<'a> {
fn from(call: &'a ast::Call) -> Self {
Call {
head: call.head,
decl_id: call.decl_id,
inner: CallImpl::AstRef(call),
}
}
}
impl<'a> From<&'a ir::Call> for Call<'a> {
fn from(call: &'a ir::Call) -> Self {
Call {
head: call.head,
decl_id: call.decl_id,
inner: CallImpl::IrRef(call),
}
}
}