Skip to main content

rl_lang/interpreter/stdlib/types/
bool.rs

1use crate::interpreter::{
2    evaluator::Evaluator,
3    stdlib::common::{vb, verr, vok, vs},
4    values::Value,
5};
6
7pub fn std_is_bool(_: &mut Evaluator, value: Value) -> bool {
8    matches!(value, Value::Bool(_))
9}
10
11pub fn std_to_bool(_: &mut Evaluator, value: Value) -> Value {
12    let result = match value {
13        Value::Bool(b) => b,
14        Value::Integer(i) => i != 0,
15        Value::Byte(i) => i != 0,
16        Value::Float(f) => f != 0.0,
17        Value::Null => false,
18        Value::String(s) => !matches!(s.trim(), "false" | "0" | ""),
19
20        other => {
21            return verr!(vs!(format!(
22                "cannot parse \"{}\" as bool",
23                other.type_name()
24            )));
25        }
26    };
27
28    vok!(vb!(result))
29}