Skip to main content

rl_lang/interpreter/stdlib/types/
bin.rs

1use crate::interpreter::{
2    evaluator::Evaluator,
3    stdlib::common::{verr, vok, vs},
4    values::Value,
5};
6
7pub fn func(_: &mut Evaluator, value: Value) -> Value {
8    let result = match value {
9        Value::Byte(v) => format!("{:b}", v),
10        Value::Integer(v) => format!("{:b}", v),
11        Value::Bool(v) => {
12            if v {
13                "1".to_string()
14            } else {
15                "0".to_string()
16            }
17        }
18        Value::Char(v) => format!("{:b}", v as u32),
19        Value::String(s) => s.bytes().map(|b| format!("{:b}", b)).collect::<String>(),
20
21        other => {
22            return verr!(vs!(format!(
23                "cannot parse \"{}\" as binary",
24                other.type_name()
25            )));
26        }
27    };
28
29    vok!(vs!(result))
30}