Add record literal syntax (#326)

This commit is contained in:
JT
2021-11-11 12:14:00 +13:00
committed by GitHub
parent 586c6d9fa8
commit 568e566adf
9 changed files with 160 additions and 4 deletions

View File

@ -23,6 +23,7 @@ pub enum Expr {
Block(BlockId),
List(Vec<Expression>),
Table(Vec<Expression>, Vec<Vec<Expression>>),
Record(Vec<(Expression, Expression)>),
Keyword(Vec<u8>, Span, Box<Expression>),
ValueWithUnit(Box<Expression>, Spanned<Unit>),
Filepath(String),

View File

@ -170,6 +170,17 @@ impl Expression {
}
false
}
Expr::Record(fields) => {
for (field_name, field_value) in fields {
if field_name.has_in_variable(working_set) {
return true;
}
if field_value.has_in_variable(working_set) {
return true;
}
}
false
}
Expr::RowCondition(_, expr) => expr.has_in_variable(working_set),
Expr::Signature(_) => false,
Expr::String(_) => false,
@ -292,6 +303,12 @@ impl Expression {
right.replace_in_variable(working_set, new_var_id)
}
}
Expr::Record(fields) => {
for (field_name, field_value) in fields {
field_name.replace_in_variable(working_set, new_var_id);
field_value.replace_in_variable(working_set, new_var_id);
}
}
Expr::RowCondition(_, expr) => expr.replace_in_variable(working_set, new_var_id),
Expr::Signature(_) => {}
Expr::String(_) => {}

View File

@ -95,6 +95,17 @@ impl Value {
}
}
pub fn as_block(&self) -> Result<BlockId, ShellError> {
match self {
Value::Block { val, .. } => Ok(*val),
x => Err(ShellError::CantConvert(
"block".into(),
x.get_type().to_string(),
self.span()?,
)),
}
}
/// Get the span for the current value
pub fn span(&self) -> Result<Span, ShellError> {
match self {