mirror of
https://github.com/nushell/nushell.git
synced 2025-08-12 02:30:38 +02:00
Add ListItem
type for Expr::List
(#12529)
# Description This PR adds a `ListItem` enum to our set of AST types. It encodes the two possible expressions inside of list expression: a singular item or a spread. This is similar to the existing `RecordItem` enum. Adding `ListItem` allows us to remove the existing `Expr::Spread` case which was previously used for list spreads. As a consequence, this guarantees (via the type system) that spreads can only ever occur inside lists, records, or as command args. This PR also does a little bit of cleanup in relevant parser code.
This commit is contained in:
@ -1,7 +1,7 @@
|
||||
use crate::{
|
||||
ast::{
|
||||
eval_operator, Assignment, Bits, Boolean, Call, Comparison, Expr, Expression,
|
||||
ExternalArgument, Math, Operator, RecordItem,
|
||||
ExternalArgument, ListItem, Math, Operator, RecordItem,
|
||||
},
|
||||
debugger::DebugContext,
|
||||
Config, IntoInterruptiblePipelineData, Range, Record, ShellError, Span, Value, VarId,
|
||||
@ -40,15 +40,15 @@ pub trait Eval {
|
||||
value.follow_cell_path(&cell_path.tail, false)
|
||||
}
|
||||
Expr::DateTime(dt) => Ok(Value::date(*dt, expr.span)),
|
||||
Expr::List(x) => {
|
||||
Expr::List(list) => {
|
||||
let mut output = vec![];
|
||||
for expr in x {
|
||||
match &expr.expr {
|
||||
Expr::Spread(expr) => match Self::eval::<D>(state, mut_state, expr)? {
|
||||
Value::List { mut vals, .. } => output.append(&mut vals),
|
||||
for item in list {
|
||||
match item {
|
||||
ListItem::Item(expr) => output.push(Self::eval::<D>(state, mut_state, expr)?),
|
||||
ListItem::Spread(_, expr) => match Self::eval::<D>(state, mut_state, expr)? {
|
||||
Value::List { vals, .. } => output.extend(vals),
|
||||
_ => return Err(ShellError::CannotSpreadAsList { span: expr.span }),
|
||||
},
|
||||
_ => output.push(Self::eval::<D>(state, mut_state, expr)?),
|
||||
}
|
||||
}
|
||||
Ok(Value::list(output, expr.span))
|
||||
@ -295,7 +295,6 @@ pub trait Eval {
|
||||
| Expr::VarDecl(_)
|
||||
| Expr::ImportPattern(_)
|
||||
| Expr::Signature(_)
|
||||
| Expr::Spread(_)
|
||||
| Expr::Operator(_)
|
||||
| Expr::Garbage => Self::unreachable(expr),
|
||||
}
|
||||
|
Reference in New Issue
Block a user