rl_lang/interpreter/stdlib/array/
arr_push.rs1use crate::{
2 interpreter::{evaluator::Evaluator, values::Value},
3 utils::{errors::Error, span::Span},
4};
5
6pub fn std_arr_push(
7 eval: &mut Evaluator,
8 array: Value,
9 value: Value,
10 span: Span,
11) -> Result<Value, Error> {
12 match array {
13 Value::Values { items_type, items } => {
14 let val_type = Evaluator::infer_type(&value, false);
15 if !Evaluator::types_compatible(&val_type, &items_type) {
16 return Err(eval.err(
17 format!(
18 "type mismatch: array expects {:?}, cannot push {:?}",
19 items_type, val_type
20 ),
21 span,
22 ));
23 }
24 let mut v = items;
25 v.push(value);
26 Ok(Value::Values {
27 items_type,
28 items: v,
29 })
30 }
31 other => Err(eval.err(
32 format!("arr_push() accepts only arrays found {}", other.type_name()).to_string(),
33 span,
34 )),
35 }
36}