1use crate::{
2 DeclId, FromValue, ShellError, Span, Value,
3 ast::{self, Expression},
4 ir,
5};
6
7use super::{EngineState, Stack, StateWorkingSet};
8
9#[derive(Debug, Clone)]
13pub struct Call<'a> {
14 pub head: Span,
15 pub decl_id: DeclId,
16 pub inner: CallImpl<'a>,
17}
18
19#[derive(Debug, Clone)]
20pub enum CallImpl<'a> {
21 AstRef(&'a ast::Call),
22 AstBox(Box<ast::Call>),
23 IrRef(&'a ir::Call),
24 IrBox(Box<ir::Call>),
25}
26
27impl Call<'_> {
28 pub fn to_owned(&self) -> Call<'static> {
31 Call {
32 head: self.head,
33 decl_id: self.decl_id,
34 inner: self.inner.to_owned(),
35 }
36 }
37
38 pub fn has_flag_const(
40 &self,
41 working_set: &StateWorkingSet,
42 stack: &Stack,
43 flag_name: &str,
44 ) -> Result<bool, ShellError> {
45 match &self.inner {
46 CallImpl::AstRef(call) => call.has_flag_const(working_set, flag_name),
47 CallImpl::AstBox(call) => call.has_flag_const(working_set, flag_name),
48 CallImpl::IrRef(call) => ir_has_flag_const(call, stack, flag_name),
49 CallImpl::IrBox(call) => ir_has_flag_const(call, stack, flag_name),
50 }
51 }
52
53 pub fn get_flag_const<T: FromValue>(
55 &self,
56 working_set: &StateWorkingSet,
57 stack: &Stack,
58 name: &str,
59 ) -> Result<Option<T>, ShellError> {
60 match &self.inner {
61 CallImpl::AstRef(call) => call.get_flag_const(working_set, name),
62 CallImpl::AstBox(call) => call.get_flag_const(working_set, name),
63 CallImpl::IrRef(call) => ir_get_flag_const(call, stack, name),
64 CallImpl::IrBox(call) => ir_get_flag_const(call, stack, name),
65 }
66 }
67
68 pub fn req_const<T: FromValue>(
70 &self,
71 working_set: &StateWorkingSet,
72 stack: &Stack,
73 pos: usize,
74 ) -> Result<T, ShellError> {
75 match &self.inner {
76 CallImpl::AstRef(call) => call.req_const(working_set, pos),
77 CallImpl::AstBox(call) => call.req_const(working_set, pos),
78 CallImpl::IrRef(call) => ir_req_const(call, stack, self.head, pos),
79 CallImpl::IrBox(call) => ir_req_const(call, stack, self.head, pos),
80 }
81 }
82
83 pub fn rest_const<T: FromValue>(
85 &self,
86 working_set: &StateWorkingSet,
87 stack: &Stack,
88 starting_pos: usize,
89 ) -> Result<Vec<T>, ShellError> {
90 match &self.inner {
91 CallImpl::AstRef(call) => call.rest_const(working_set, starting_pos),
92 CallImpl::AstBox(call) => call.rest_const(working_set, starting_pos),
93 CallImpl::IrRef(call) => ir_rest_const(call, stack, starting_pos),
94 CallImpl::IrBox(call) => ir_rest_const(call, stack, starting_pos),
95 }
96 }
97
98 pub fn arguments_span(&self) -> Span {
100 match &self.inner {
101 CallImpl::AstRef(call) => call.arguments_span(),
102 CallImpl::AstBox(call) => call.arguments_span(),
103 CallImpl::IrRef(call) => call.arguments_span(),
104 CallImpl::IrBox(call) => call.arguments_span(),
105 }
106 }
107
108 pub fn span(&self) -> Span {
110 match &self.inner {
111 CallImpl::AstRef(call) => call.span(),
112 CallImpl::AstBox(call) => call.span(),
113 CallImpl::IrRef(call) => call.span(),
114 CallImpl::IrBox(call) => call.span(),
115 }
116 }
117
118 pub fn get_parser_info<'a>(&'a self, stack: &'a Stack, name: &str) -> Option<&'a Expression> {
120 match &self.inner {
121 CallImpl::AstRef(call) => call.get_parser_info(name),
122 CallImpl::AstBox(call) => call.get_parser_info(name),
123 CallImpl::IrRef(call) => call.get_parser_info(stack, name),
124 CallImpl::IrBox(call) => call.get_parser_info(stack, name),
125 }
126 }
127
128 pub fn rest_iter_flattened(
131 &self,
132 engine_state: &EngineState,
133 stack: &mut Stack,
134 eval_expression: fn(
135 &EngineState,
136 &mut Stack,
137 &ast::Expression,
138 ) -> Result<Value, ShellError>,
139 starting_pos: usize,
140 ) -> Result<Vec<Value>, ShellError> {
141 fn by_ast(
142 call: &ast::Call,
143 engine_state: &EngineState,
144 stack: &mut Stack,
145 eval_expression: fn(
146 &EngineState,
147 &mut Stack,
148 &ast::Expression,
149 ) -> Result<Value, ShellError>,
150 starting_pos: usize,
151 ) -> Result<Vec<Value>, ShellError> {
152 call.rest_iter_flattened(starting_pos, |expr| {
153 eval_expression(engine_state, stack, expr)
154 })
155 }
156
157 fn by_ir(
158 call: &ir::Call,
159 stack: &Stack,
160 starting_pos: usize,
161 ) -> Result<Vec<Value>, ShellError> {
162 call.rest_iter_flattened(stack, starting_pos)
163 }
164
165 match &self.inner {
166 CallImpl::AstRef(call) => {
167 by_ast(call, engine_state, stack, eval_expression, starting_pos)
168 }
169 CallImpl::AstBox(call) => {
170 by_ast(call, engine_state, stack, eval_expression, starting_pos)
171 }
172 CallImpl::IrRef(call) => by_ir(call, stack, starting_pos),
173 CallImpl::IrBox(call) => by_ir(call, stack, starting_pos),
174 }
175 }
176}
177
178impl CallImpl<'_> {
179 pub fn to_owned(&self) -> CallImpl<'static> {
180 match self {
181 CallImpl::AstRef(call) => CallImpl::AstBox(Box::new((*call).clone())),
182 CallImpl::AstBox(call) => CallImpl::AstBox(call.clone()),
183 CallImpl::IrRef(call) => CallImpl::IrBox(Box::new((*call).clone())),
184 CallImpl::IrBox(call) => CallImpl::IrBox(call.clone()),
185 }
186 }
187}
188
189fn ir_has_flag_const(call: &ir::Call, stack: &Stack, flag_name: &str) -> Result<bool, ShellError> {
190 Ok(call
191 .named_iter(stack)
192 .find(|(name, _)| name.item == flag_name)
193 .is_some_and(|(_, value)| !matches!(value, Some(Value::Bool { val: false, .. }))))
194}
195
196fn ir_get_flag_const<T: FromValue>(
197 call: &ir::Call,
198 stack: &Stack,
199 name: &str,
200) -> Result<Option<T>, ShellError> {
201 if let Some(val) = call.get_named_arg(stack, name) {
202 T::from_value(val.clone()).map(Some)
203 } else {
204 Ok(None)
205 }
206}
207
208fn ir_req_const<T: FromValue>(
209 call: &ir::Call,
210 stack: &Stack,
211 head: Span,
212 pos: usize,
213) -> Result<T, ShellError> {
214 let maybe_val = call.positional_nth(stack, pos).cloned();
215 let val = maybe_val.ok_or_else(|| {
216 let max_idx = call.positional_len(stack).checked_sub(1);
217 match max_idx {
218 None => ShellError::AccessEmptyContent { span: head },
219 Some(max_idx) => ShellError::AccessBeyondEnd {
220 max_idx,
221 span: head,
222 },
223 }
224 })?;
225 T::from_value(val)
226}
227
228fn ir_rest_const<T: FromValue>(
229 call: &ir::Call,
230 stack: &Stack,
231 starting_pos: usize,
232) -> Result<Vec<T>, ShellError> {
233 call.rest_iter_flattened(stack, starting_pos)?
234 .into_iter()
235 .map(T::from_value)
236 .collect()
237}
238
239impl<'a> From<&'a ast::Call> for Call<'a> {
240 fn from(call: &'a ast::Call) -> Self {
241 Call {
242 head: call.head,
243 decl_id: call.decl_id,
244 inner: CallImpl::AstRef(call),
245 }
246 }
247}
248
249impl<'a> From<&'a ir::Call> for Call<'a> {
250 fn from(call: &'a ir::Call) -> Self {
251 Call {
252 head: call.head,
253 decl_id: call.decl_id,
254 inner: CallImpl::IrRef(call),
255 }
256 }
257}