use crate::{Environment, eval::apply::eval_apply, is_number_eq, value::Value};
pub(crate) fn builtin(args: &[Value], env: Environment) -> Result<Value, std::sync::Arc<str>> {
if let [obj, key, val] = args {
match (eval_apply(obj, env.clone())?, eval_apply(key, env.clone())?) {
(Value::Object(type_name, mut dict), Value::Atom(k)) => {
let _ = dict.insert(k, std::sync::Arc::new(eval_apply(val, env)?));
Ok(Value::Object(type_name, dict))
}
(Value::List(lst), Value::Number(idx)) if (idx as usize) > lst.len() => Err(std::sync::Arc::from(format!(
concat!(
"Error[ksl::builtin::Index]: ",
"Index `{}` out of bounds `{}`."
),
idx.trunc(),
lst.len()
))),
(Value::List(lst), Value::Number(idx)) if is_number_eq(idx, idx.trunc()) && idx > 0.0 => {
let mut new_vec = lst.to_vec();
new_vec[(idx as usize) - 1] = eval_apply(val, env)?;
Ok(Value::List(std::sync::Arc::from(new_vec)))
}
(Value::List(lst), Value::Number(idx)) if is_number_eq(idx, -1.0) => {
let len = lst.len();
let mut new_vec = lst.to_vec();
new_vec[len - 1] = eval_apply(val, env)?;
Ok(Value::List(std::sync::Arc::from(new_vec)))
}
(Value::List(_), Value::Number(idx)) => Err(std::sync::Arc::from(format!(
concat!(
"Error[ksl::builtin::Set]: ",
"Expected an integer greater than 0, but got {}."
),
idx
))),
(Value::Object(_, _) | Value::List(_), e) => Err(std::sync::Arc::from(format!(
concat!("Error[ksl::builtin::Set]: ", "Unexpected value: `{}`."),
e
))),
(e, _) => Err(std::sync::Arc::from(format!(
concat!("Error[ksl::builtin::Set]: ", "Unexpected value: `{}`."),
e
))),
}
} else {
Err(std::sync::Arc::from(format!(
concat!(
"Error[ksl::builtin::Set]: ",
"Expected 3 parameters, but {} were passed."
),
args.len()
)))
}
}