Lines
99.15 %
Functions
93.18 %
Branches
100 %
use crate::ast::{Expr, LambdaParams, WasmType};
use crate::compiler::expr::format_form;
use crate::error::{Error, Result};
use crate::reserved::reject_reserved;
use crate::runtime::{Symbol, SymbolKind, SymbolTable};
use super::super::context::CompileContext;
use super::super::emit::FunctionEmitter;
use super::super::expr::{compile_body, compile_body_for_stack, compile_for_effect};
use super::SpecialFormSpec;
use super::binding::{eval_body, parse_param_list, rebind_params};
pub(super) const FORMS: &[SpecialFormSpec] = &[SpecialFormSpec {
name: "LABELS",
eval: labels_form,
compile: compile_labels,
stack: Some(compile_labels_for_stack),
effect: Some(compile_labels_for_effect),
}];
struct LabelDef {
name: String,
params: Vec<String>,
body: Expr,
}
fn parse_labels_defs<'a>(
symbols: &SymbolTable,
args: &'a [Expr],
) -> Result<(Vec<LabelDef>, &'a [Expr])> {
if args.len() < 2 {
return Err(Error::Compile(
"LABELS requires a definitions list and at least one body form".to_string(),
));
let defs_list = args[0].as_list().ok_or_else(|| {
Error::Compile(format!(
"LABELS: expected definitions list, got {}",
format_form(&args[0])
))
})?;
let defs = defs_list
.iter()
.map(|def| {
let elems = def.as_list().ok_or_else(|| {
"LABELS: expected function definition, got {}",
format_form(def)
if elems.len() < 3 {
"LABELS: each definition needs a name, params, and body".to_string(),
let name = elems[0]
.as_symbol()
.ok_or_else(|| {
"LABELS: expected function name, got {}",
format_form(&elems[0])
})?
.to_string();
reject_reserved(symbols, &name)?;
let params = parse_param_list(symbols, "LABELS", &elems[1])?;
let (params, body) = rebind_params(
symbols,
LambdaParams::simple(params),
Expr::body(&elems[2..]),
)?;
Ok(LabelDef {
name,
params: params.required,
body,
})
.collect::<Result<Vec<_>>>()?;
Ok((defs, &args[1..]))
/// Runs `body` with the LABELS functions bound in a scope of `symbols`, so a
/// definition the body makes outlives the form; `block_depth` is the BLOCK
/// labels open at the form, which their bodies see (ISS-179).
fn within_labels<T>(
symbols: &mut SymbolTable,
defs: Vec<LabelDef>,
block_depth: usize,
body: impl FnOnce(&mut SymbolTable) -> Result<T>,
) -> Result<T> {
symbols.push_scope();
let names: Vec<String> = defs.iter().map(|def| def.name.clone()).collect();
for def in defs {
let lambda = Expr::Lambda(LambdaParams::simple(def.params), Box::new(def.body));
let mut symbol = Symbol::new(&def.name, SymbolKind::Function)
.with_function(lambda)
.local_function();
symbol.set_block_depth(block_depth);
symbols.define_scoped(symbol);
let env = symbols.lexical_names();
for name in &names {
if let Some(symbol) = symbols.lookup_mut(name) {
symbol.set_lexical_env(env.clone());
symbols.define_local_alias(name);
let result = body(symbols);
symbols.pop_scope()?;
result
pub(super) fn labels_form(symbols: &mut SymbolTable, args: &[Expr]) -> Result<Expr> {
let (defs, body) = parse_labels_defs(symbols, args)?;
within_labels(symbols, defs, 0, |symbols| eval_body(symbols, body))
pub(super) fn compile_labels(
ctx: &mut CompileContext,
emit: &mut FunctionEmitter,
args: &[Expr],
) -> Result<()> {
within_labels(symbols, defs, ctx.block_label_count(), |symbols| {
compile_body(ctx, emit, symbols, body)
pub(super) fn compile_labels_for_stack(
) -> Result<WasmType> {
compile_body_for_stack(ctx, emit, symbols, body)
/// Unlike `compile_labels` (value position — its body's TAIL form is
/// serialized to the RPC output stream via `compile_body`/`compile_expr`),
/// LABELS in effect position must never serialize: every body form,
/// including the tail, is compiled purely for its effects and dropped —
/// same contract `PROGN` gives its own body. Without this, a LABELS in a
/// loop body (`(do (...) (...) (funcall-through-labels ...))`) would push
/// one output entity per iteration until the output buffer is exhausted.
pub(super) fn compile_labels_for_effect(
body.iter()
.try_for_each(|form| compile_for_effect(ctx, emit, symbols, form))