mimium_lang/runtime/vm/
builtin.rs

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use crate::compiler::ExtFunTypeInfo;
use crate::interner::ToSymbol;
use crate::types::{PType, Type};
use crate::{function, numeric};

use super::{ExtFnInfo, Machine, ReturnCode};

fn probef(machine: &mut Machine) -> ReturnCode {
    let rv = machine.get_stack(0);
    let i = super::Machine::get_as::<f64>(rv);
    print!("{i}");
    machine.set_stack(0, rv);
    1
}

fn probelnf(machine: &mut Machine) -> ReturnCode {
    let rv = machine.get_stack(0);
    let i = super::Machine::get_as::<f64>(rv);
    println!("{} ", i);
    machine.set_stack(0, rv);
    1
}
fn min(machine: &mut Machine) -> ReturnCode {
    let lhs = super::Machine::get_as::<f64>(machine.get_stack(0));
    let rhs = super::Machine::get_as::<f64>(machine.get_stack(1));
    let res = lhs.min(rhs);
    machine.set_stack(0, super::Machine::to_value(res));
    1
}
fn max(machine: &mut Machine) -> ReturnCode {
    let lhs = super::Machine::get_as::<f64>(machine.get_stack(0));
    let rhs = super::Machine::get_as::<f64>(machine.get_stack(1));
    let res = lhs.max(rhs);
    machine.set_stack(0, super::Machine::to_value(res));
    1
}

pub fn get_builtin_fns() -> [ExtFnInfo; 4] {
    [
        (
            "probe".to_symbol(),
            probef,
            function!(vec![numeric!()], numeric!()),
        ),
        (
            "probeln".to_symbol(),
            probelnf,
            function!(vec![numeric!()], numeric!()),
        ),
        (
            "min".to_symbol(),
            min,
            function!(vec![numeric!(), numeric!()], numeric!()),
        ),
        (
            "max".to_symbol(),
            max,
            function!(vec![numeric!(), numeric!()], numeric!()),
        ),
    ]
}

pub fn get_builtin_fn_types() -> Vec<ExtFunTypeInfo> {
    get_builtin_fns()
        .iter()
        .map(|(name, _f, t)| ExtFunTypeInfo {
            name: *name,
            ty: *t,
        })
        .collect()
}