Skip to main content

nu_protocol/
eval_base.rs

1//! Foundational [`Eval`] trait allowing dispatch between const-eval and regular evaluation
2use nu_path::expand_path;
3
4use crate::{
5    BlockId, Config, ENV_VARIABLE_ID, GetSpan, Range, Record, ShellError, Span, Value, VarId,
6    ast::{
7        Assignment, Bits, Boolean, Call, Comparison, Expr, Expression, ExternalArgument, ListItem,
8        Math, Operator, RecordItem, eval_operator,
9    },
10    debugger::DebugContext,
11};
12use std::{borrow::Cow, collections::HashMap, sync::Arc};
13
14/// To share implementations for regular eval and const eval
15pub trait Eval {
16    /// State that doesn't need to be mutated.
17    /// EngineState for regular eval and StateWorkingSet for const eval
18    type State<'a>: Copy + GetSpan;
19
20    /// State that needs to be mutated.
21    /// This is the stack for regular eval, and unused by const eval
22    type MutState;
23
24    fn eval<D: DebugContext>(
25        state: Self::State<'_>,
26        mut_state: &mut Self::MutState,
27        expr: &Expression,
28    ) -> Result<Value, ShellError> {
29        let expr_span = expr.span(&state);
30
31        match &expr.expr {
32            Expr::AttributeBlock(ab) => Self::eval::<D>(state, mut_state, &ab.item),
33            Expr::Bool(b) => Ok(Value::bool(*b, expr_span)),
34            Expr::Int(i) => Ok(Value::int(*i, expr_span)),
35            Expr::Float(f) => Ok(Value::float(*f, expr_span)),
36            Expr::Binary(b) => Ok(Value::binary(b.clone(), expr_span)),
37            Expr::Filepath(path, quoted) => {
38                if *quoted {
39                    Ok(Value::string(path, expr_span))
40                } else {
41                    let path = expand_path(path, true);
42                    Ok(Value::string(path.to_string_lossy(), expr_span))
43                }
44            }
45            Expr::Directory(path, quoted) => {
46                if path == "-" {
47                    Ok(Value::string("-", expr_span))
48                } else if *quoted {
49                    Ok(Value::string(path, expr_span))
50                } else {
51                    let path = expand_path(path, true);
52                    Ok(Value::string(path.to_string_lossy(), expr_span))
53                }
54            }
55            Expr::Var(var_id) => Self::eval_var(state, mut_state, *var_id, expr_span),
56            Expr::CellPath(cell_path) => Ok(Value::cell_path(cell_path.clone(), expr_span)),
57            Expr::FullCellPath(cell_path) => {
58                let value = Self::eval::<D>(state, mut_state, &cell_path.head)?;
59
60                // Cell paths are usually case-sensitive, but we give $env
61                // special treatment.
62                let tail = if cell_path.head.expr == Expr::Var(ENV_VARIABLE_ID) {
63                    let mut tail = cell_path.tail.clone();
64                    if let Some(pm) = tail.first_mut() {
65                        pm.make_insensitive();
66                    }
67                    Cow::Owned(tail)
68                } else {
69                    Cow::Borrowed(&cell_path.tail)
70                };
71                value.follow_cell_path(&tail).map(Cow::into_owned)
72            }
73            Expr::DateTime(dt) => Ok(Value::date(*dt, expr_span)),
74            Expr::List(list) => {
75                let mut output = vec![];
76                for item in list {
77                    match item {
78                        ListItem::Item(expr) => output.push(Self::eval::<D>(state, mut_state, expr)?),
79                        ListItem::Spread(_, expr) => match Self::eval::<D>(state, mut_state, expr)? {
80                            Value::List { vals, .. } => output.extend(vals),
81                            Value::Nothing { .. } => (),
82                            _ => return Err(ShellError::CannotSpreadAsList { span: expr_span }),
83                        },
84                    }
85                }
86                Ok(Value::list(output, expr_span))
87            }
88            Expr::Record(items) => {
89                let mut record = Record::new();
90                let mut col_names: HashMap<String, (Span, bool)> = HashMap::new();
91                for item in items {
92                    match item {
93                        RecordItem::Pair(col, val) => {
94                            // avoid duplicate cols
95                            let col_name = Self::eval::<D>(state, mut_state, col)?.coerce_into_string()?;
96                            let col_span = col.span(&state);
97                            if let Some(&(orig_span, from_spread)) = col_names.get(&col_name) {
98                                return Err(ShellError::ColumnDefinedTwice {
99                                    col_name,
100                                    second_use: col_span,
101                                    first_use: orig_span,
102                                    help: from_spread.then(|| {
103                                        ShellError::RECORD_SPREAD_DUPLICATE_HELP.into()
104                                    }),
105                                });
106                            } else {
107                                col_names.insert(col_name.clone(), (col_span, false));
108                                record.push(col_name, Self::eval::<D>(state, mut_state, val)?);
109                            }
110                        }
111                        RecordItem::Spread(_, inner) => {
112                            let inner_span = inner.span(&state);
113                            match Self::eval::<D>(state, mut_state, inner)? {
114                                Value::Record { val: inner_val, .. } => {
115                                    for (col_name, val) in inner_val.into_owned() {
116                                        if let Some(&(orig_span, _)) = col_names.get(&col_name) {
117                                            return Err(ShellError::ColumnDefinedTwice {
118                                                col_name,
119                                                second_use: inner_span,
120                                                first_use: orig_span,
121                                                help: Some(
122                                                    ShellError::RECORD_SPREAD_DUPLICATE_HELP.into(),
123                                                ),
124                                            });
125                                        } else {
126                                            col_names.insert(col_name.clone(), (inner_span, true));
127                                            record.push(col_name, val);
128                                        }
129                                    }
130                                }
131                                _ => {
132                                    return Err(ShellError::CannotSpreadAsRecord {
133                                        span: inner_span,
134                                    })
135                                }
136                            }
137                        }
138                    }
139                }
140
141                Ok(Value::record(record, expr_span))
142            }
143            Expr::Table(table) => {
144                let mut output_headers = vec![];
145                for expr in table.columns.as_ref() {
146                    let header = Self::eval::<D>(state, mut_state, expr)?.coerce_into_string()?;
147                    if let Some(idx) = output_headers
148                        .iter()
149                        .position(|existing| existing == &header)
150                    {
151                        let first_use = table.columns[idx].span(&state);
152                        return Err(ShellError::ColumnDefinedTwice {
153                            col_name: header,
154                            second_use: expr_span,
155                            first_use,
156                            help: None,
157                        });
158                    } else {
159                        output_headers.push(header);
160                    }
161                }
162
163                let mut output_rows = vec![];
164                for val in table.rows.as_ref() {
165                    let record = output_headers.iter().zip(val.as_ref()).map(|(col, expr)| {
166                        Self::eval::<D>(state, mut_state, expr).map(|val| (col.clone(), val))
167                    }).collect::<Result<_,_>>()?;
168
169                    output_rows.push(Value::record(
170                        record,
171                        expr_span,
172                    ));
173                }
174                Ok(Value::list(output_rows, expr_span))
175            }
176            Expr::Keyword(kw) => Self::eval::<D>(state, mut_state, &kw.expr),
177            Expr::String(s) | Expr::RawString(s) => Ok(Value::string(s.clone(), expr_span)),
178            Expr::Nothing => Ok(Value::nothing(expr_span)),
179            Expr::ValueWithUnit(value) => match Self::eval::<D>(state, mut_state, &value.expr)? {
180                Value::Int { val, .. } => value.unit.item.build_value(val, value.unit.span),
181                x => Err(ShellError::CantConvert {
182                    to_type: "unit value".into(),
183                    from_type: x.get_type().to_string(),
184                    span: value.expr.span(&state),
185                    help: None,
186                }),
187            },
188            Expr::Call(call) => Self::eval_call::<D>(state, mut_state, call, expr_span),
189            Expr::ExternalCall(head, args) => {
190                Self::eval_external_call(state, mut_state, head, args, expr_span)
191            }
192            Expr::Collect(var_id, expr) => {
193                Self::eval_collect::<D>(state, mut_state, *var_id, expr)
194            }
195            Expr::Subexpression(block_id) => {
196                Self::eval_subexpression::<D>(state, mut_state, *block_id, expr_span)
197            }
198            Expr::Range(range) => {
199                let from = if let Some(f) = &range.from {
200                    Self::eval::<D>(state, mut_state, f)?
201                } else {
202                    Value::nothing(expr_span)
203                };
204
205                let next = if let Some(s) = &range.next {
206                    Self::eval::<D>(state, mut_state, s)?
207                } else {
208                    Value::nothing(expr_span)
209                };
210
211                let to = if let Some(t) = &range.to {
212                    Self::eval::<D>(state, mut_state, t)?
213                } else {
214                    Value::nothing(expr_span)
215                };
216
217                Ok(Value::range(
218                    Range::new(from, next, to, range.operator.inclusion, expr_span)?,
219                    expr_span,
220                ))
221            }
222            Expr::UnaryNot(expr) => {
223                let lhs = Self::eval::<D>(state, mut_state, expr)?;
224                match lhs {
225                    Value::Bool { val, .. } => Ok(Value::bool(!val, expr_span)),
226                    other => Err(ShellError::TypeMismatch {
227                        err_message: format!("expected bool, found {}", other.get_type()),
228                        span: expr_span,
229                    }),
230                }
231            }
232            Expr::UnaryMinus(operand) => {
233                // Negation is multiplication by -1, as in the IR compiler.
234                let value = Self::eval::<D>(state, mut_state, operand)?;
235                value.mul(expr_span, &Value::int(-1, expr_span), expr_span)
236            }
237            Expr::BinaryOp(lhs, op, rhs) => {
238                let op_span = op.span(&state);
239                let op = eval_operator(op)?;
240
241                match op {
242                    Operator::Boolean(boolean) => {
243                        let lhs = Self::eval::<D>(state, mut_state, lhs)?;
244                        match boolean {
245                            Boolean::And => {
246                                if lhs.is_false() {
247                                    Ok(Value::bool(false, expr_span))
248                                } else {
249                                    let rhs = Self::eval::<D>(state, mut_state, rhs)?;
250                                    lhs.and(op_span, &rhs, expr_span)
251                                }
252                            }
253                            Boolean::Or => {
254                                if lhs.is_true() {
255                                    Ok(Value::bool(true, expr_span))
256                                } else {
257                                    let rhs = Self::eval::<D>(state, mut_state, rhs)?;
258                                    lhs.or(op_span, &rhs, expr_span)
259                                }
260                            }
261                            Boolean::Xor => {
262                                let rhs = Self::eval::<D>(state, mut_state, rhs)?;
263                                lhs.xor(op_span, &rhs, expr_span)
264                            }
265                        }
266                    }
267                    Operator::Math(math) => {
268                        let lhs = Self::eval::<D>(state, mut_state, lhs)?;
269                        let rhs = Self::eval::<D>(state, mut_state, rhs)?;
270
271                        match math {
272                            Math::Add => lhs.add(op_span, &rhs, expr_span),
273                            Math::Subtract => lhs.sub(op_span, &rhs, expr_span),
274                            Math::Multiply => lhs.mul(op_span, &rhs, expr_span),
275                            Math::Divide => lhs.div(op_span, &rhs, expr_span),
276                            Math::FloorDivide => lhs.floor_div(op_span, &rhs, expr_span),
277                            Math::Modulo => lhs.modulo(op_span, &rhs, expr_span),
278                            Math::Pow => lhs.pow(op_span, &rhs, expr_span),
279                            Math::Concatenate => lhs.concat(op_span, &rhs, expr_span),
280                        }
281                    }
282                    Operator::Comparison(comparison) => {
283                        let lhs = Self::eval::<D>(state, mut_state, lhs)?;
284                        let rhs = Self::eval::<D>(state, mut_state, rhs)?;
285                        match comparison {
286                            Comparison::LessThan => lhs.lt(op_span, &rhs, expr_span),
287                            Comparison::LessThanOrEqual => lhs.lte(op_span, &rhs, expr_span),
288                            Comparison::GreaterThan => lhs.gt(op_span, &rhs, expr_span),
289                            Comparison::GreaterThanOrEqual => lhs.gte(op_span, &rhs, expr_span),
290                            Comparison::Equal => lhs.eq(op_span, &rhs, expr_span),
291                            Comparison::NotEqual => lhs.ne(op_span, &rhs, expr_span),
292                            Comparison::In => lhs.r#in(op_span, &rhs, expr_span),
293                            Comparison::NotIn => lhs.not_in(op_span, &rhs, expr_span),
294                            Comparison::Has => lhs.has(op_span, &rhs, expr_span),
295                            Comparison::NotHas => lhs.not_has(op_span, &rhs, expr_span),
296                            Comparison::StartsWith => lhs.starts_with(op_span, &rhs, expr_span),
297                            Comparison::NotStartsWith => lhs.not_starts_with(op_span, &rhs, expr_span),
298                            Comparison::EndsWith => lhs.ends_with(op_span, &rhs, expr_span),
299                            Comparison::NotEndsWith => lhs.not_ends_with(op_span, &rhs, expr_span),
300                            Comparison::OlderThan => {
301                                Self::check_point_in_time(state, &rhs, expr_span)?;
302                                lhs.older_than(op_span, &rhs, expr_span)
303                            }
304                            Comparison::NewerThan => {
305                                Self::check_point_in_time(state, &rhs, expr_span)?;
306                                lhs.newer_than(op_span, &rhs, expr_span)
307                            }
308                            Comparison::RegexMatch => {
309                                Self::regex_match(state, op_span, &lhs, &rhs, false, expr_span)
310                            }
311                            Comparison::NotRegexMatch => {
312                                Self::regex_match(state, op_span, &lhs, &rhs, true, expr_span)
313                            }
314                        }
315                    }
316                    Operator::Bits(bits) => {
317                        let lhs = Self::eval::<D>(state, mut_state, lhs)?;
318                        let rhs = Self::eval::<D>(state, mut_state, rhs)?;
319                        match bits {
320                            Bits::BitAnd => lhs.bit_and(op_span, &rhs, expr_span),
321                            Bits::BitOr => lhs.bit_or(op_span, &rhs, expr_span),
322                            Bits::BitXor => lhs.bit_xor(op_span, &rhs, expr_span),
323                            Bits::ShiftLeft => lhs.bit_shl(op_span, &rhs, expr_span),
324                            Bits::ShiftRight => lhs.bit_shr(op_span, &rhs, expr_span),
325                        }
326                    }
327                    Operator::Assignment(assignment) => Self::eval_assignment::<D>(
328                        state, mut_state, lhs, rhs, assignment, op_span, expr_span
329                    ),
330                }
331            }
332            Expr::RowCondition(block_id) | Expr::Closure(block_id) => {
333                Self::eval_row_condition_or_closure(state, mut_state, *block_id, expr_span)
334            }
335            Expr::StringInterpolation(exprs) => {
336                let config = Self::get_config(state, mut_state);
337                let str = exprs
338                    .iter()
339                    .map(|expr| Self::eval::<D>(state, mut_state, expr).map(|v| v.to_expanded_string(", ", &config)))
340                    .collect::<Result<String, _>>()?;
341
342                Ok(Value::string(str, expr_span))
343            }
344            Expr::GlobInterpolation(exprs, quoted) => {
345                let config = Self::get_config(state, mut_state);
346                let str = exprs
347                    .iter()
348                    .map(|expr| Self::eval::<D>(state, mut_state, expr).map(|v| v.to_expanded_string(", ", &config)))
349                    .collect::<Result<String, _>>()?;
350
351                Ok(Value::glob(str, *quoted, expr_span))
352            }
353            Expr::Overlay(_) => Self::eval_overlay(state, expr_span),
354            Expr::GlobPattern(pattern, quoted) => {
355                // GlobPattern is similar to Filepath
356                // But we don't want to expand path during eval time, it's required for `nu_engine::glob_from` to run correctly
357                Ok(Value::glob(pattern, *quoted, expr_span))
358            }
359            Expr::MatchBlock(_) // match blocks are handled by `match`
360            | Expr::Block(_) // blocks are handled directly by core commands
361            | Expr::VarDecl(_)
362            | Expr::ImportPattern(_)
363            | Expr::Signature(_)
364            | Expr::Operator(_)
365            | Expr::Garbage => Self::unreachable(state, expr),
366        }
367    }
368
369    fn get_config(state: Self::State<'_>, mut_state: &mut Self::MutState) -> Arc<Config>;
370
371    fn eval_var(
372        state: Self::State<'_>,
373        mut_state: &mut Self::MutState,
374        var_id: VarId,
375        span: Span,
376    ) -> Result<Value, ShellError>;
377
378    fn eval_call<D: DebugContext>(
379        state: Self::State<'_>,
380        mut_state: &mut Self::MutState,
381        call: &Call,
382        span: Span,
383    ) -> Result<Value, ShellError>;
384
385    fn eval_external_call(
386        state: Self::State<'_>,
387        mut_state: &mut Self::MutState,
388        head: &Expression,
389        args: &[ExternalArgument],
390        span: Span,
391    ) -> Result<Value, ShellError>;
392
393    fn eval_collect<D: DebugContext>(
394        state: Self::State<'_>,
395        mut_state: &mut Self::MutState,
396        var_id: VarId,
397        expr: &Expression,
398    ) -> Result<Value, ShellError>;
399
400    fn eval_subexpression<D: DebugContext>(
401        state: Self::State<'_>,
402        mut_state: &mut Self::MutState,
403        block_id: BlockId,
404        span: Span,
405    ) -> Result<Value, ShellError>;
406
407    fn regex_match(
408        state: Self::State<'_>,
409        op_span: Span,
410        lhs: &Value,
411        rhs: &Value,
412        invert: bool,
413        expr_span: Span,
414    ) -> Result<Value, ShellError>;
415
416    /// Checks that this evaluator can compare against `rhs` with `older-than` or `newer-than`.
417    ///
418    /// A duration on the right is counted back from the current time. Const evaluation runs
419    /// while the script is parsed, so it can't read the clock and refuses a duration.
420    ///
421    /// # Arguments
422    ///
423    /// * `rhs` - The evaluated right operand
424    /// * `expr_span` - Span of the whole comparison, used in errors
425    ///
426    /// # Errors
427    ///
428    /// Returns `ShellError::NotAConstant` when const evaluation meets a duration.
429    fn check_point_in_time(
430        state: Self::State<'_>,
431        rhs: &Value,
432        expr_span: Span,
433    ) -> Result<(), ShellError>;
434
435    #[allow(clippy::too_many_arguments)]
436    fn eval_assignment<D: DebugContext>(
437        state: Self::State<'_>,
438        mut_state: &mut Self::MutState,
439        lhs: &Expression,
440        rhs: &Expression,
441        assignment: Assignment,
442        op_span: Span,
443        expr_span: Span,
444    ) -> Result<Value, ShellError>;
445
446    fn eval_row_condition_or_closure(
447        state: Self::State<'_>,
448        mut_state: &mut Self::MutState,
449        block_id: BlockId,
450        span: Span,
451    ) -> Result<Value, ShellError>;
452
453    fn eval_overlay(state: Self::State<'_>, span: Span) -> Result<Value, ShellError>;
454
455    /// For expressions that should never actually be evaluated
456    fn unreachable(state: Self::State<'_>, expr: &Expression) -> Result<Value, ShellError>;
457}