1
//! Which names may stand as a function designator, `'x` or `#'x`.
2

            
3
use crate::error::{Error, Result};
4
use crate::runtime::{Head, SymbolTable};
5

            
6
/// Special forms that are Common Lisp functions: their name is a designator.
7
const FUNCTION_FORMS: &[&str] = &[
8
    "FUNCALL",
9
    "APPLY",
10
    "EVAL",
11
    "ERROR",
12
    "COMPILE",
13
    "DESCRIBE",
14
    "APROPOS",
15
    "MACROEXPAND",
16
    "MACROEXPAND-1",
17
];
18

            
19
/// Special forms that are Common Lisp macros.
20
const MACRO_FORMS: &[&str] = &[
21
    "AND",
22
    "OR",
23
    "COND",
24
    "CASE",
25
    "ECASE",
26
    "DO",
27
    "DO*",
28
    "DOLIST",
29
    "DEFUN",
30
    "DEFVAR",
31
    "DEFPARAMETER",
32
    "DEFMACRO",
33
    "DEFSTRUCT",
34
    "SETF",
35
    "HANDLER-CASE",
36
    "LAMBDA",
37
];
38

            
39
/// Macros here that Common Lisp defines as functions, with no runtime
40
/// function to designate yet.
41
const FUNCTION_MACROS: &[&str] = &["MAPCAR", "MAPC", "1+", "1-"];
42

            
43
18496
pub(in crate::compiler) fn is_function_form(name: &str) -> bool {
44
18496
    names(FUNCTION_FORMS, name)
45
18496
}
46

            
47
8584
pub(in crate::compiler) fn is_function_macro(name: &str) -> bool {
48
8584
    names(FUNCTION_MACROS, name)
49
8584
}
50

            
51
/// A Common Lisp function this compiler implements as a special form or macro.
52
5412
pub(in crate::compiler) fn is_function_name(name: &str) -> bool {
53
5412
    is_function_form(name) || is_function_macro(name)
54
5412
}
55

            
56
37296
fn names(list: &[&str], name: &str) -> bool {
57
186464
    list.iter().any(|known| known.eq_ignore_ascii_case(name))
58
37296
}
59

            
60
/// The one classification of a name used as a function designator, `'x` or
61
/// `#'x`: a function is one; a special operator or a macro is not.
62
222458
pub(in crate::compiler) fn reject_non_function(symbols: &SymbolTable, name: &str) -> Result<()> {
63
222458
    match symbols.resolve_head(name) {
64
11567
        Head::SpecialForm if is_function_form(name) => Ok(()),
65
3763
        Head::SpecialForm if names(MACRO_FORMS, name) => {
66
1665
            Err(Error::Compile(format!("{name} is a macro, not a function")))
67
        }
68
2098
        Head::SpecialForm => Err(Error::Compile(format!(
69
2098
            "{name} is a special operator, not a function"
70
2098
        ))),
71
6453
        Head::Macro(..) if names(FUNCTION_MACROS, name) => Err(Error::Compile(format!(
72
3954
            "{name} cannot be used as a function value yet"
73
3954
        ))),
74
2499
        Head::Macro(..) => Err(Error::Compile(format!("{name} is a macro, not a function"))),
75
204438
        _ => Ok(()),
76
    }
77
222458
}