Lines
100 %
Functions
Branches
//! Which names may stand as a function designator, `'x` or `#'x`.
use crate::error::{Error, Result};
use crate::runtime::{Head, SymbolTable};
/// Special forms that are Common Lisp functions: their name is a designator.
const FUNCTION_FORMS: &[&str] = &[
"FUNCALL",
"APPLY",
"EVAL",
"ERROR",
"COMPILE",
"DESCRIBE",
"APROPOS",
"MACROEXPAND",
"MACROEXPAND-1",
];
/// Special forms that are Common Lisp macros.
const MACRO_FORMS: &[&str] = &[
"AND",
"OR",
"COND",
"CASE",
"ECASE",
"DO",
"DO*",
"DOLIST",
"DEFUN",
"DEFVAR",
"DEFPARAMETER",
"DEFMACRO",
"DEFSTRUCT",
"SETF",
"HANDLER-CASE",
"LAMBDA",
/// Macros here that Common Lisp defines as functions, with no runtime
/// function to designate yet.
const FUNCTION_MACROS: &[&str] = &["MAPCAR", "MAPC", "1+", "1-"];
pub(in crate::compiler) fn is_function_form(name: &str) -> bool {
names(FUNCTION_FORMS, name)
}
pub(in crate::compiler) fn is_function_macro(name: &str) -> bool {
names(FUNCTION_MACROS, name)
/// A Common Lisp function this compiler implements as a special form or macro.
pub(in crate::compiler) fn is_function_name(name: &str) -> bool {
is_function_form(name) || is_function_macro(name)
fn names(list: &[&str], name: &str) -> bool {
list.iter().any(|known| known.eq_ignore_ascii_case(name))
/// The one classification of a name used as a function designator, `'x` or
/// `#'x`: a function is one; a special operator or a macro is not.
pub(in crate::compiler) fn reject_non_function(symbols: &SymbolTable, name: &str) -> Result<()> {
match symbols.resolve_head(name) {
Head::SpecialForm if is_function_form(name) => Ok(()),
Head::SpecialForm if names(MACRO_FORMS, name) => {
Err(Error::Compile(format!("{name} is a macro, not a function")))
Head::SpecialForm => Err(Error::Compile(format!(
"{name} is a special operator, not a function"
))),
Head::Macro(..) if names(FUNCTION_MACROS, name) => Err(Error::Compile(format!(
"{name} cannot be used as a function value yet"
Head::Macro(..) => Err(Error::Compile(format!("{name} is a macro, not a function"))),
_ => Ok(()),