From bd0983c4dda6c833753eee331173af9807ec164f Mon Sep 17 00:00:00 2001 From: Erin Date: Mon, 25 Apr 2022 14:56:35 +0200 Subject: [PATCH] Implemented key extraction --- ablescript/src/ast.rs | 1 + ablescript/src/interpret.rs | 9 ++++++++- ablescript/src/parser.rs | 4 ++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/ablescript/src/ast.rs b/ablescript/src/ast.rs index 50f9ceb..94dd46c 100644 --- a/ablescript/src/ast.rs +++ b/ablescript/src/ast.rs @@ -160,6 +160,7 @@ pub enum Expr { index: Box>, }, Len(Box>), + Keys(Box>), Variable(String), } diff --git a/ablescript/src/interpret.rs b/ablescript/src/interpret.rs index f666a55..ffd5bd3 100644 --- a/ablescript/src/interpret.rs +++ b/ablescript/src/interpret.rs @@ -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) => { diff --git a/ablescript/src/parser.rs b/ablescript/src/parser.rs index 5934e0a..3b82712 100644 --- a/ablescript/src/parser.rs +++ b/ablescript/src/parser.rs @@ -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)?), } })