Skip to main content

rl_lang/interpreter/stdlib/array/
arr_remove.rs

1use crate::{
2    interpreter::{evaluator::Evaluator, values::Value},
3    utils::{errors::Error, span::Span},
4};
5
6pub fn std_arr_remove(
7    eval: &mut Evaluator,
8    array: Value,
9    index: i64,
10    span: Span,
11) -> Result<Value, Error> {
12    match array {
13        Value::Values { items, items_type } => {
14            if index as usize >= items.len() {
15                return Err(eval.err(format!("index out of bounds: {}", index), span));
16            }
17            let mut v = items;
18            v.remove(index as usize);
19            Ok(Value::Values {
20                items_type,
21                items: v,
22            })
23        }
24        _ => Err(eval.err("arr_remove() accepts only arrays".to_string(), span)),
25    }
26}