Lines
100 %
Functions
Branches
//! Argument parsing for `BLOCK` and `RETURN-FROM`.
use crate::ast::Expr;
use crate::compiler::expr::format_form;
use crate::error::{Error, Result};
const BLOCK: &str = "block";
const RETURN_FROM: &str = "return-from";
pub(in crate::compiler::special) const NIL_LABEL: &str = "NIL";
/// The block name `expr` spells: a symbol, or `nil`, the label of every
/// loop's implicit block.
pub(in crate::compiler::special) fn label(expr: &Expr) -> Option<&str> {
match expr {
Expr::Symbol(name) => Some(name),
Expr::Nil => Some(NIL_LABEL),
_ => None,
}
pub(super) fn parse_block(args: &[Expr]) -> Result<(&str, &[Expr])> {
if args.is_empty() {
return Err(Error::Arity {
name: BLOCK.to_string(),
expected: 1,
actual: 0,
});
let Some(name) = label(&args[0]) else {
return Err(Error::Type {
expected: "symbol for BLOCK label".to_string(),
actual: format_form(&args[0]),
};
Ok((name, &args[1..]))
pub(super) fn parse_return_from(args: &[Expr]) -> Result<(&str, &Expr)> {
if !(1..=2).contains(&args.len()) {
name: RETURN_FROM.to_string(),
expected: 2,
actual: args.len(),
expected: "symbol for RETURN-FROM target".to_string(),
Ok((name, args.get(1).unwrap_or(&Expr::Nil)))