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:
Ian Manske
2024-04-18 11:21:05 +00:00
committed by GitHub
parent 57b0c722c6
commit 6ccd547d81
8 changed files with 179 additions and 133 deletions

View File

@ -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),
}