cas_compiler/expr/
call.rs

1use cas_error::Error;
2use cas_parser::parser::ast::call::Call;
3use crate::{Compile, Compiler, InstructionKind};
4
5impl Compile for Call {
6    fn compile(&self, compiler: &mut Compiler) -> Result<(), Error> {
7        self.args.iter().try_for_each(|arg| arg.compile(compiler))?;
8
9        // TODO: allow calling any receiever when it is supported
10        // load the function
11        let symbol_id = compiler.resolve_symbol(&self.name)?;
12        compiler.add_instr_with_spans(
13            InstructionKind::LoadVar(symbol_id),
14            vec![self.name.span.clone()],
15        );
16
17        let spans = self.outer_span()
18            .into_iter()
19            .chain(self.args.iter().map(|arg| arg.span()))
20            .collect();
21
22        if self.derivatives > 0 {
23            // compute derivative of function
24            compiler.add_instr_with_spans(
25                InstructionKind::CallDerivative(self.derivatives, self.args.len()),
26                spans,
27            );
28        } else {
29            // call the function
30            compiler.add_instr_with_spans(InstructionKind::Call(self.args.len()), spans);
31        }
32
33        Ok(())
34    }
35}