Lines
80 %
Functions
83.33 %
Branches
100 %
//! The `FUNCTION` special form: the function a symbol names or a lambda
//! expression.
use super::lambda;
use crate::ast::Expr;
use crate::compiler::native::designator_class::reject_non_function;
use crate::error::{Error, Result};
use crate::runtime::{Head, SymbolTable};
pub(super) fn function_form(symbols: &mut SymbolTable, args: &[Expr]) -> Result<Expr> {
if args.len() != 1 {
return Err(Error::Arity {
name: "FUNCTION".to_string(),
expected: 1,
actual: args.len(),
});
}
match &args[0] {
Expr::Symbol(name) => {
reject_non_function(symbols, name)?;
match symbols.resolve_head(name) {
Head::Native | Head::SpecialForm => Ok(Expr::quoted(Expr::Symbol(name.clone()))),
_ => symbols
.lookup(name)
.and_then(|s| s.function().cloned())
.ok_or_else(|| match SymbolTable::local_alias_origin(name) {
Some(_) => Error::undefined_symbol(name),
None => Error::Compile(format!("'{name}' has no function definition")),
}),
Expr::List(elems) if matches!(elems.first(), Some(Expr::Symbol(s)) if s == "LAMBDA") => {
lambda(symbols, &elems[1..])
_ => Err(Error::Compile(
"FUNCTION: argument must be a symbol or lambda expression".to_string(),
)),