Lines
88.68 %
Functions
95.45 %
Branches
100 %
//! Quasiquote expansion into the standard backquote form.
//!
//! A template with no unquote is one quoted datum. Otherwise a list becomes
//! `(list …)`, a `,@x` element splits it into nested two-list `append`s
//! and a dotted pair becomes `(cons car cdr)`. The form is then compiled or
//! evaluated like any other, so an unquoted call or runtime value is computed.
//! Inside a macro body an unquoted parameter holds a FORM, to be substituted
//! as it is, so [`quasiquote_datum`] builds the expansion as a datum instead.
use crate::ast::Expr;
use crate::error::{Error, Result};
use crate::runtime::SymbolTable;
use super::eval::eval_value;
use super::format::format_expr;
fn call(head: &str, args: Vec<Expr>) -> Expr {
let mut form = vec![Expr::Symbol(head.to_string())];
form.extend(args);
Expr::List(form)
}
fn has_unquote(expr: &Expr) -> bool {
match expr {
Expr::Unquote(_) | Expr::UnquoteSplicing(_) => true,
Expr::Quote(inner) => has_unquote(inner),
Expr::List(items) => items.iter().any(has_unquote),
Expr::Cons(car, cdr) => has_unquote(car) || has_unquote(cdr),
_ => false,
pub(in crate::compiler) fn quasiquote_form(template: &Expr) -> Result<Expr> {
match template {
_ if !has_unquote(template) => Ok(Expr::quoted(template.clone())),
Expr::Unquote(inner) => Ok(inner.as_ref().clone()),
Expr::Quote(inner) => Ok(call(
"LIST",
vec![
Expr::quoted(Expr::Symbol("QUOTE".to_string())),
quasiquote_form(inner)?,
],
)),
Expr::List(items) => list_form(items),
Expr::Cons(car, cdr) => Ok(call(
"CONS",
vec![quasiquote_form(car)?, quasiquote_form(cdr)?],
_ => Err(Error::Compile(
"unquote-splicing outside of list in quasiquote".to_string(),
fn list_form(items: &[Expr]) -> Result<Expr> {
let mut segments: Vec<Expr> = Vec::new();
let mut cells: Vec<Expr> = Vec::new();
let mut spliced = false;
for item in items {
let Expr::UnquoteSplicing(inner) = item else {
cells.push(quasiquote_form(item)?);
continue;
};
reject_non_list(inner)?;
spliced = true;
if !cells.is_empty() {
segments.push(call("LIST", std::mem::take(&mut cells)));
segments.push(inner.as_ref().clone());
if !spliced {
return Ok(call("LIST", cells));
segments.push(call("LIST", cells));
let last = segments.pop().unwrap_or(Expr::Nil);
Ok(segments
.into_iter()
.rev()
.fold(last, |tail, head| call("APPEND", vec![head, tail])))
fn reject_non_list(spliced: &Expr) -> Result<()> {
match spliced {
Expr::Number(_) | Expr::String(_) | Expr::T | Expr::Keyword(_) => Err(not_a_list(spliced)),
_ => Ok(()),
/// The expansion of `template` as a quoted datum, each `,form` replaced by
/// its evaluated value and each `,@form` spliced.
pub(in crate::compiler) fn quasiquote_datum(
symbols: &mut SymbolTable,
template: &Expr,
) -> Result<Expr> {
let data = datum(symbols, template)?;
crate::compiler::datum::bounded(symbols, Expr::quoted(data))
fn datum(symbols: &mut SymbolTable, expr: &Expr) -> Result<Expr> {
Expr::Quote(inner) if has_unquote(inner) => Ok(Expr::List(vec![
Expr::Symbol("QUOTE".to_string()),
datum(symbols, inner)?,
])),
Expr::Unquote(inner) => unquoted(symbols, inner),
Expr::UnquoteSplicing(_) => Err(Error::Compile(
Expr::List(elems) => {
let mut resolved = Vec::new();
for elem in elems {
match elem {
Expr::UnquoteSplicing(inner) => {
let spliced = eval_value(symbols, inner)?;
resolved.extend(splice_elements(spliced)?);
_ => resolved.push(datum(symbols, elem)?),
Ok(Expr::List(resolved))
Expr::Cons(car, cdr) => Ok(Expr::cons(datum(symbols, car)?, datum(symbols, cdr)?)),
_ => Ok(expr.clone()),
/// An unquoted form is evaluated and its value becomes code.
fn unquoted(symbols: &mut SymbolTable, inner: &Expr) -> Result<Expr> {
eval_value(symbols, inner).map(Expr::into_code)
fn splice_elements(spliced: Expr) -> Result<Vec<Expr>> {
Expr::Nil => Ok(Vec::new()),
Expr::List(elems) => Ok(elems),
Expr::Datum(inner) => match *inner {
other => Err(not_a_list(&other)),
},
fn not_a_list(value: &Expr) -> Error {
Error::Compile(format!(
"unquote-splicing requires a list, got {}",
format_expr(value)
))
#[cfg(test)]
#[path = "quasiquote_tests.rs"]
mod tests;