Lines
92.41 %
Functions
82.14 %
Branches
100 %
use crate::ast::{Expr, WasmType};
use crate::compiler::expr::format_form;
use crate::error::{Error, Result};
use crate::reserved::reject_reserved;
use crate::runtime::{Head, Symbol, SymbolKind, SymbolTable};
use super::super::context::CompileContext;
use super::super::emit::FunctionEmitter;
use super::super::expr::{compile_expr, compile_for_stack};
use super::SpecialFormSpec;
use super::binding::parse_macro_params;
use super::compile_eval::folded_form;
pub(super) const FORMS: &[SpecialFormSpec] = &[
SpecialFormSpec {
name: "DEFMACRO",
eval: defmacro,
compile: compile_defmacro_form,
stack: None,
effect: Some(compile_defmacro_for_effect),
},
name: "MACROEXPAND-1",
eval: macroexpand_1,
compile: compile_macroexpand_1_form,
stack: Some(compile_macroexpand_1_for_stack),
effect: Some(compile_macroexpand_1_for_effect),
name: "MACROEXPAND",
eval: macroexpand,
compile: compile_macroexpand_form,
stack: Some(compile_macroexpand_for_stack),
effect: Some(compile_macroexpand_for_effect),
];
pub(super) fn compile_defmacro_form(
ctx: &mut CompileContext,
emit: &mut FunctionEmitter,
symbols: &mut SymbolTable,
args: &[Expr],
) -> Result<()> {
let result = defmacro(symbols, args)?;
compile_expr(ctx, emit, symbols, &result)
}
fn compile_defmacro_for_effect(
_ctx: &mut CompileContext,
_emit: &mut FunctionEmitter,
defmacro(symbols, args).map(drop)
pub(super) fn compile_macroexpand_1_form(
let result = macroexpand_1(symbols, args)?;
pub(super) fn compile_macroexpand_form(
let result = macroexpand(symbols, args)?;
fn compile_macroexpand_1_for_stack(
) -> Result<WasmType> {
compile_for_stack(ctx, emit, symbols, &result)
fn compile_macroexpand_for_stack(
fn compile_macroexpand_1_for_effect(
macroexpand_1(symbols, args).map(drop)
fn compile_macroexpand_for_effect(
macroexpand(symbols, args).map(drop)
pub(super) fn defmacro(symbols: &mut SymbolTable, args: &[Expr]) -> Result<Expr> {
if args.len() < 3 {
return Err(Error::Compile(
"DEFMACRO requires a name, parameter list, and body".to_string(),
));
// Always compile-time-only — same reasoning as DEFUN.
super::reject_definition_under_runtime_guard(symbols, "DEFMACRO")?;
let name = match &args[0] {
Expr::Symbol(s) => reject_reserved(symbols, s).map(|()| s.clone())?,
other => {
return Err(Error::Compile(format!(
"DEFMACRO: expected symbol name, got {}",
format_form(other)
)));
};
super::reject_definition_after_exit(symbols, "DEFMACRO", &name, args)?;
let params = parse_macro_params(symbols, "DEFMACRO", &args[1])?;
if !params.aux.is_empty() {
"DEFMACRO: &aux is not yet supported".to_string(),
let (doc, body_idx) = match args.get(2) {
Some(Expr::String(s)) if args.len() > 3 => (Some(s.clone()), 3),
_ => (None, 2),
if body_idx >= args.len() {
return Err(Error::Compile("DEFMACRO: missing body".to_string()));
let body = Expr::body(&args[body_idx..]);
let lambda = Expr::Lambda(params, Box::new(body));
if symbols.lookup(&name).is_some() {
symbols.note_redefinition();
let mut sym = Symbol::new(&name, SymbolKind::Macro).with_function(lambda);
if let Some(d) = doc {
sym = sym.with_doc(d);
symbols.define(sym);
Ok(Expr::quoted(Expr::Symbol(name)))
pub(super) fn macroexpand_1(symbols: &mut SymbolTable, args: &[Expr]) -> Result<Expr> {
if args.len() != 1 {
return Err(Error::Arity {
name: "MACROEXPAND-1".to_string(),
expected: 1,
actual: args.len(),
});
let form = Expr::quoted(folded_form(symbols, "MACROEXPAND-1", &args[0])?);
macroexpand_1_impl(symbols, &form)
pub(super) fn macroexpand(symbols: &mut SymbolTable, args: &[Expr]) -> Result<Expr> {
name: "MACROEXPAND".to_string(),
let mut form = Expr::quoted(folded_form(symbols, "MACROEXPAND", &args[0])?);
loop {
let expanded = macroexpand_1_impl(symbols, &form)?;
if expanded == form {
break;
form = expanded;
Ok(form)
fn macroexpand_1_impl(symbols: &mut SymbolTable, form: &Expr) -> Result<Expr> {
match form {
Expr::Datum(inner) => match inner.as_ref() {
Expr::List(elems) if !elems.is_empty() => {
if let Some(name) = macro_head_name(&elems[0])
&& let Head::Macro(params, body) = symbols.resolve_head(name)
{
let (params, body) = (params.clone(), body.clone());
// `macroexpand-1` is one-step, but expanding the macro
// evaluates its body — which may itself call
// `macroexpand-1` on the same macro, recursing
// unboundedly. Bracket with the shared depth guard so a
// self-referential expander errors instead of
// overflowing the native stack.
symbols.enter_macro_expansion()?;
let expanded =
super::super::expr::expand_macro(symbols, ¶ms, &body, &elems[1..]);
symbols.exit_macro_expansion()?;
return expanded.map(Expr::quoted);
Ok(form.clone())
_ => Ok(form.clone()),
fn macro_head_name(expr: &Expr) -> Option<&str> {
match expr {
Expr::Symbol(name) => Some(name),
Expr::Quote(inner) => match inner.as_ref() {
_ => None,