use alloc::string::{String, ToString};
use crate::runtime::{Runtime, RuntimeError, Value};
pub(crate) fn chr(
rt: &mut Runtime<'_>,
values: &[Value],
_line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
let Some(code) = the_code_of(rt, values) else {
return Ok(Value::Text(String::new()));
};
if code <= 0 {
return Ok(Value::Text(String::new()));
}
match u32::try_from(code).ok().and_then(char::from_u32) {
Some(character) => Ok(Value::Text(character.to_string())),
None => Ok(Value::Text(String::new())),
}
}
fn the_code_of(rt: &mut Runtime<'_>, values: &[Value]) -> Option<i64> {
match values.first() {
Some(Value::Int(number)) => Some(*number),
Some(Value::Float(number)) => Some(*number as i64),
Some(other) => crate::v1::the_whole_number_at_the_start_of_a_text(&rt.the_text_of(other)),
None => None,
}
}