Lines
100 %
Functions
Branches
//! The operand of a reflective form (`EVAL`, `MACROEXPAND`, `COMPILE`,
//! `DESCRIBE`) is a folded constant form on every compilation surface.
use crate::ast::Expr;
use crate::compiler::expr::fold_value;
use crate::compiler::special::binding::shared::{fully_macro_expanded, mentions_expanded};
use crate::error::{Error, Result};
use crate::runtime::SymbolTable;
/// The form `operand` folds to; a non-constant operand is a compile error,
/// never the operand returned as the result.
pub(in crate::compiler::special) fn folded_form(
symbols: &mut SymbolTable,
who: &str,
operand: &Expr,
) -> Result<Expr> {
let operand = &fully_macro_expanded(symbols, operand)?;
match fold_value(symbols, operand)? {
Some(Expr::Datum(inner)) => Ok(*inner),
Some(form) => Ok(form),
None => Err(Error::Compile(format!(
"{who}: the form is not a compile-time constant"
))),
}
/// The symbol name `operand` folds to.
pub(in crate::compiler::special) fn folded_symbol(
) -> Result<String> {
match folded_form(symbols, who, operand)? {
Expr::Symbol(name) => Ok(name),
_ => Err(Error::Compile(format!(
"{who}: argument must be a quoted symbol"
/// The form EVAL's operand folds to; EVAL sees only global definitions, so a
/// form mentioning a name a local binding holds here is a compile error.
pub(in crate::compiler::special) fn folded_eval_form(
let form = folded_form(symbols, "EVAL", operand)?;
for (name, _) in symbols.lexical_names() {
if mentions_expanded(symbols, &form, &name)? {
return Err(Error::Compile(format!(
"EVAL: {name} is bound locally here; EVAL sees only global definitions"
)));
Ok(form)