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
//! Execute a `CodeObject` by name using the given arguments

use super::*;

/// Execute a `CodeObject` by name using the given arguments
#[derive(Clone, Debug)]
pub struct Call {
    name: Variable,
    args: Vec<Expr>,
}

impl Call {
    pub fn new<T>(name: T) -> Self
    where
        T: Into<Variable>,
    {
        Self {
            args: vec![],
            name: name.into(),
        }
    }

    pub fn with_args<T>(name: T, args: Vec<Expr>) -> Self
    where
        T: Into<Variable>,
    {
        Self {
            args,
            name: name.into(),
        }
    }

    pub fn arg<T>(mut self, expr: T) -> Self
    where
        T: Into<Expr>,
    {
        self.args.push(expr.into());
        self
    }
}

impl HirLowering for Call {
    fn lower<'hir, 'lir>(&'hir self, runtime: &mut HirLoweringRuntime<'lir>)
    where
        'hir: 'lir,
    {
        // calling convention is pascal-style i.e. f(a, b)
        // will be lowered as:
        //  push a
        //  push b
        for arg in self.args.iter() {
            arg.lower(runtime);
        }

        runtime.emit(LirElement::call(&self.name, self.args.len() as u8));
    }
}