-rw-r--r--src/exec.rs38
-rw-r--r--src/main.rs1
-rw-r--r--src/parser/fun.rs2
3 files changed, 35 insertions, 6 deletions
diff --git a/src/exec.rs b/src/exec.rs
index 162a3a0..0ad25f5 100644
--- a/src/exec.rs
+++ b/src/exec.rs
@@ -362,6 +362,11 @@ impl From<bool> for Val<'_> {
Self::Int(value as i128)
}
}
+impl From<Array> for Val<'_> {
+ fn from(value: Array) -> Self {
+ Self::Array(value)
+ }
+}
impl std::fmt::Debug for Val<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
@@ -585,10 +590,9 @@ fn size_fn<'s>(f: &Function<'s>) -> Argc {
use Function::*;
match f {
Matches | Windows | IndexHashMap | HashMap | Append | Del
- | Fold(_) | Mask | Group | Index | Sub | Add | Mul | Div | Xor
- | Mod | Pow | Eq | Ne | BitAnd | Or | Ge | Le | Lt | Gt | In => {
- Argc::takes(2).into(1)
- }
+ | Fold(_) | Mask | Group | Index | First | Last | Sub | Add
+ | Mul | Div | Xor | Mod | Pow | Eq | Ne | BitAnd | Or | Ge
+ | Le | Lt | Gt | In => Argc::takes(2).into(1),
Python(x) => *x,
&Take(x) => Argc::takes(x as _).into(x as _),
With(x) => Argc::takes(1).into(x.argc().output),
@@ -1054,6 +1058,30 @@ impl<'s> Function<'s> {
exec_lambda(λ, &mut Context::inherits(c), &mut a)?;
stack.extend(a.drain(..));
}
+ Self::First => {
+ let array = pop!().assert_array(span)?;
+ stack.push(
+ each!(array.inner, |x| Ok(Val::from(x.get(0).cloned().ok_or_else(|| {
+ Error {
+ name: format!("array is empty"),
+ message: "here".to_string().spun(span),
+ ..Default::default()
+ }
+ })?)), Vec<_> => Result<Val>)?.spun(span),
+ );
+ }
+ Self::Last => {
+ let array = pop!().assert_array(span)?;
+ stack.push(
+ each!(array.inner, |x| Ok(Val::from(x.get(x.len()-1).cloned().ok_or_else(|| {
+ Error {
+ name: format!("array is empty"),
+ message: "here".to_string().spun(span),
+ ..Default::default()
+ }
+ })?)), Vec<_> => Result<Val>)?.spun(span),
+ );
+ }
Self::Index => {
let index = pop!().assert_array(span)?.assert_int(span)?;
let array = pop!().assert_array(span)?;
@@ -1345,7 +1373,7 @@ impl<'s> Function<'s> {
.spun(span),
);
}
- _ => (),
+ _ => do yeet Error::lazy(span, "unimplemented?"),
}
Ok(())
}
diff --git a/src/main.rs b/src/main.rs
index bae9eca..a417450 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,4 +1,5 @@
#![feature(
+ yeet_expr,
iter_intersperse,
iterator_try_reduce,
formatting_options,
diff --git a/src/parser/fun.rs b/src/parser/fun.rs
index 1da1bdb..5f95814 100644
--- a/src/parser/fun.rs
+++ b/src/parser/fun.rs
@@ -20,7 +20,7 @@ pub enum Function<'s> {
Python(Argc),
Matches,
Eq,
- Reverse,
+ // Reverse,
Zap(Option<u64>),
Del,
Debug,