Implemented key extraction

This commit is contained in:
Erin 2022-04-25 14:56:35 +02:00 committed by ondra05
parent f99aad0152
commit bd0983c4dd
3 changed files with 13 additions and 1 deletions

View file

@ -160,6 +160,7 @@ pub enum Expr {
index: Box<Spanned<Expr>>, index: Box<Spanned<Expr>>,
}, },
Len(Box<Spanned<Expr>>), Len(Box<Spanned<Expr>>),
Keys(Box<Spanned<Expr>>),
Variable(String), Variable(String),
} }

View file

@ -195,7 +195,14 @@ impl ExecEnv {
.unwrap_or(Value::Nul) .unwrap_or(Value::Nul)
} }
Len(expr) => Value::Int(self.eval_expr(expr)?.length()), Len(expr) => Value::Int(self.eval_expr(expr)?.length()),
Keys(expr) => Value::Cart(
self.eval_expr(expr)?
.into_cart()
.into_keys()
.enumerate()
.map(|(i, k)| (Value::Int(i as isize + 1), ValueRef::new(k)))
.collect(),
),
// TODO: not too happy with constructing an artificial // TODO: not too happy with constructing an artificial
// Ident here. // Ident here.
Variable(name) => { Variable(name) => {

View file

@ -294,6 +294,10 @@ impl<'source> Parser<'source> {
} }
None => break Expr::Len(Box::new(expr)), None => break Expr::Len(Box::new(expr)),
}, },
Token::GreaterThan if buf.is_none() => {
self.require(Token::RightBracket)?;
break Expr::Keys(Box::new(expr));
}
token => buf = Some(self.parse_expr(token, &mut buf)?), token => buf = Some(self.parse_expr(token, &mut buf)?),
} }
}) })