ksl 0.1.30

KSL core library and interpreter
Documentation
//! # ksl::builtin::pend
//!
//! Built-in function `Append` and `Prepend`.

use crate::{Environment, eval::apply::eval_apply, value::Value};

pub(crate) fn builtin(args: &[Value], is_prepend: bool, env: Environment) -> Result<Value, std::sync::Arc<str>> {
    if let [lst, val] = args {
        let val = eval_apply(val, env.clone())?;

        match eval_apply(lst, env)? {
            Value::List(elements) => {
                let mut new_vec = Vec::with_capacity(elements.len() + 1);
                Ok(Value::List(std::sync::Arc::from({
                    if is_prepend {
                        new_vec.push(val);
                        new_vec.extend_from_slice(elements.as_ref());
                    } else {
                        new_vec.extend_from_slice(elements.as_ref());
                        new_vec.push(val);
                    }
                    new_vec
                })))
            }
            e => Err(std::sync::Arc::from(format!(
                concat!("Error[ksl::builtin::{}]: ", "Unexpected value: `{}`."),
                if is_prepend { "Prepend" } else { "Append" },
                e
            ))),
        }
    } else {
        Err(std::sync::Arc::from(format!(
            concat!(
                "Error[ksl::builtin::{}]: ",
                "Expected 2 parameters, but {} were passed."
            ),
            if is_prepend { "Prepend" } else { "Append" },
            args.len()
        )))
    }
}