Lines
97.44 %
Functions
100 %
Branches
//! A macro place of SETF is the place its expansion names (CLHS 5.1.2.7).
use crate::ast::Expr;
use crate::error::{Error, Result};
use crate::runtime::{Head, SymbolTable};
use super::fully_macro_expanded;
/// The place/value pairs `args` of a SETF with each macro place replaced by
/// its expansion.
pub(in crate::compiler) fn expand_macro_places(
symbols: &SymbolTable,
args: &[Expr],
) -> Result<Vec<Expr>> {
args.iter()
.enumerate()
.map(
|(at, arg)| match at % 2 == 0 && is_macro_use(symbols, arg) {
true => fully_macro_expanded(symbols, arg),
false => Ok(arg.clone()),
},
)
.collect()
}
/// The call `((SETF f) operands… value)` a compound place `(f operands…)`
/// stores through; the place's subforms run before the value, as in Common
/// Lisp. `None` for a place that is not a form.
pub(in crate::compiler) fn compound_setf_call(
place: &Expr,
value: &Expr,
) -> Result<Option<Expr>> {
let Expr::List(parts) = place else {
return Ok(None);
};
let Some((head, operands)) = parts.split_first() else {
let Expr::Symbol(accessor) = head else {
return Err(Error::Compile(
"Invalid place in SETF - expected function call".to_string(),
));
let setter = format!("(SETF {accessor})");
if !symbols.contains(&setter) {
return Err(Error::Compile(format!(
"No setf method defined for accessor '{accessor}'"
)));
let call = std::iter::once(Expr::Symbol(setter))
.chain(operands.iter().cloned())
.chain(std::iter::once(value.clone()));
Ok(Some(Expr::List(call.collect())))
fn is_macro_use(symbols: &SymbolTable, form: &Expr) -> bool {
matches!(form, Expr::List(items) if matches!(items.first(),
Some(Expr::Symbol(head)) if matches!(symbols.resolve_head(head), Head::Macro(..))))
#[cfg(test)]
#[path = "setf_place_tests.rs"]
mod tests;