Implemented key extraction

pull/10/head
ondra05 2022-04-25 14:56:35 +02:00
parent ebd61780f6
commit 4b7aa07d06
3 changed files with 13 additions and 1 deletions

View File

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

View File

@ -195,7 +195,14 @@ impl ExecEnv {
.unwrap_or(Value::Nul)
}
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
// Ident here.
Variable(name) => {

View File

@ -294,6 +294,10 @@ impl<'source> Parser<'source> {
}
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)?),
}
})