summaryrefslogtreecommitdiff
path: root/matrix-lang/src/value/function.rs
blob: 38d8b0bcb1df444c5fcb151c89242a838026237b (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
37
38
39
40
41
42
43
use crate::prelude::*;
use std::fmt::{Debug, Display};

pub struct Function {
    pub name: Rc<str>,
    pub arity: usize,
    pub variadic: bool,
    pub fun: InnerFunction
}

#[derive(Clone)]
pub enum InnerFunction {
    Compiled(Rc<Chunk>),
    Native(Rc<dyn Fn((&mut Vm, &mut StackFrame), Vec<Value>) -> Result<Value>>),
}

impl Debug for Function {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        use InnerFunction as F;
        match self.fun {
            F::Compiled(_) => {
                write!(f, "[Function {}]", self.name)
            },
            F::Native(_) => {
                write!(f, "[Builtin {}]", self.name)
            }
        }
    }
}

impl Display for Function {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        use InnerFunction as F;
        match self.fun {
            F::Compiled(_) => {
                write!(f, "[Function {}]", self.name)
            },
            F::Native(_) => {
                write!(f, "[Builtin {}]", self.name)
            }
        }
    }
}