summaryrefslogtreecommitdiff
path: root/matrix-stdlib/src/io.rs
blob: 288e99e5ca795516f8fa6f0ab4313155c78a99c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
use matrix::{value::Value, self, vm::Vm, Result};
use matrix_macros::native_func;

#[native_func(1, true)]
fn print(_vm: &mut Vm, args: Vec<Value>) -> Result<Value> {
    let [values] = args.try_into().unwrap();
    if let Value::List(list) = values {
        for (i, value) in list.iter().enumerate() {
            print!("{}", value.boring_print());
            if i != 0 {
                print!(" ");
            }
        }
    }
    Ok(Value::Nil)
}

#[native_func(1, true)]
fn println(_vm: &mut Vm, args: Vec<Value>) -> Result<Value> {
    let [values] = args.try_into().unwrap();
    if let Value::List(list) = values {
        for (i, value) in list.iter().enumerate() {
            print!("{}", value.boring_print());
            if i != 0 {
                print!(" ");
            }
        }
    }
    print!("\n");
    Ok(Value::Nil)
}

pub fn load(vm: &mut Vm) {
    vm.load_native_fn(print());
    vm.load_native_fn(println());
}