cl-interpret/builtin: Add len
builtin as a quick hack to write more interesting programs.
This is temporary, I just want a way to get the length of a thing atm.
This commit is contained in:
parent
a233bb18bc
commit
b2733aa171
@ -10,6 +10,7 @@ use cl_ast::Sym;
|
||||
use std::{
|
||||
io::{stdout, Write},
|
||||
rc::Rc,
|
||||
slice,
|
||||
};
|
||||
|
||||
builtins! {
|
||||
@ -57,6 +58,19 @@ builtins! {
|
||||
println!("{}", *env);
|
||||
Ok(ConValue::Empty)
|
||||
}
|
||||
|
||||
pub fn len<env, _>(list) -> IResult<ConValue> {
|
||||
Ok(ConValue::Int(match list {
|
||||
ConValue::Empty => 0,
|
||||
ConValue::String(s) => s.chars().count() as _,
|
||||
ConValue::Ref(r) => return len.call(env, slice::from_ref(r.as_ref())),
|
||||
ConValue::Array(t) => t.len() as _,
|
||||
ConValue::Tuple(t) => t.len() as _,
|
||||
ConValue::RangeExc(start, end) => (end - start) as _,
|
||||
ConValue::RangeInc(start, end) => (end - start + 1) as _,
|
||||
_ => Err(Error::TypeError)?,
|
||||
}))
|
||||
}
|
||||
}
|
||||
builtins! {
|
||||
const BINARY;
|
||||
|
Loading…
Reference in New Issue
Block a user