use cas_error::Error;
use cas_parser::parser::ast::call::Call;
use crate::{Compile, Compiler, InstructionKind};
impl Compile for Call {
fn compile(&self, compiler: &mut Compiler) -> Result<(), Error> {
self.args.iter().try_for_each(|arg| arg.compile(compiler))?;
let symbol_id = compiler.resolve_symbol(&self.name)?;
compiler.add_instr_with_spans(
InstructionKind::LoadVar(symbol_id),
vec![self.name.span.clone()],
);
let spans = self.outer_span()
.into_iter()
.chain(self.args.iter().map(|arg| arg.span()))
.collect();
if self.derivatives > 0 {
compiler.add_instr_with_spans(
InstructionKind::CallDerivative(self.derivatives, self.args.len()),
spans,
);
} else {
compiler.add_instr_with_spans(InstructionKind::Call(self.args.len()), spans);
}
Ok(())
}
}