Skip to main content

sim_lib_lang_cl/
forms.rs

1use sim_kernel::Symbol;
2
3/// Shared organ a CL-lite surface form delegates to.
4#[derive(Clone, Copy, Debug, PartialEq, Eq)]
5pub enum ClLiteFormRole {
6    /// Form backed by the binding organ (e.g. `defun`, `let`).
7    Binding,
8    /// Form backed by the control organ (e.g. `handler-case`).
9    Control,
10    /// Form backed by the dispatch organ (e.g. `defgeneric`).
11    Dispatch,
12    /// Form backed by the namespace organ (e.g. `defpackage`).
13    Namespace,
14    /// Form backed by the mutation organ (e.g. `setq`, `setf`).
15    Mutation,
16}
17
18/// Specification of one CL-lite surface form and the organ it routes to.
19#[derive(Clone, Debug, PartialEq, Eq)]
20pub struct ClLiteFormSpec {
21    /// `cl`-qualified surface symbol of the form.
22    pub symbol: Symbol,
23    /// Organ role classifying the form.
24    pub role: ClLiteFormRole,
25    /// Symbol of the organ that provides the form's behavior.
26    pub organ: Symbol,
27    /// One-line description of the form.
28    pub doc: &'static str,
29}
30
31/// Returns the CL-lite surface forms and the organs they delegate to.
32pub fn cl_lite_form_specs() -> Vec<ClLiteFormSpec> {
33    vec![
34        binding_form(
35            "defun",
36            "Define a function through the shared binding organ.",
37        ),
38        binding_form(
39            "defmacro",
40            "Define a macro function through the binding organ.",
41        ),
42        binding_form(
43            "let",
44            "Enter a lexical binding frame through the binding organ.",
45        ),
46        mutation_form("setq", "Update a CL-lite variable through a mutation cell."),
47        control_form(
48            "handler-case",
49            "Signal to the nearest condition handler through the control organ.",
50        ),
51        control_form(
52            "restart-case",
53            "Expose and invoke restarts through the control organ.",
54        ),
55        dispatch_form(
56            "defgeneric",
57            "Declare a generic function backed by the dispatch organ.",
58        ),
59        dispatch_form(
60            "defmethod",
61            "Attach a primary method through the dispatch organ.",
62        ),
63        namespace_form(
64            "defpackage",
65            "Create a package through the namespace organ.",
66        ),
67        namespace_form(
68            "in-package",
69            "Resolve the current package through the namespace organ.",
70        ),
71        mutation_form(
72            "setf",
73            "Update generalized places through the mutation organ.",
74        ),
75    ]
76}
77
78fn binding_form(name: &'static str, doc: &'static str) -> ClLiteFormSpec {
79    ClLiteFormSpec {
80        symbol: Symbol::qualified("cl", name),
81        role: ClLiteFormRole::Binding,
82        organ: sim_lib_binding::binding_organ_symbol(),
83        doc,
84    }
85}
86
87fn control_form(name: &'static str, doc: &'static str) -> ClLiteFormSpec {
88    ClLiteFormSpec {
89        symbol: Symbol::qualified("cl", name),
90        role: ClLiteFormRole::Control,
91        organ: sim_lib_control::control_organ_symbol(),
92        doc,
93    }
94}
95
96fn dispatch_form(name: &'static str, doc: &'static str) -> ClLiteFormSpec {
97    ClLiteFormSpec {
98        symbol: Symbol::qualified("cl", name),
99        role: ClLiteFormRole::Dispatch,
100        organ: sim_lib_dispatch::dispatch_organ_symbol(),
101        doc,
102    }
103}
104
105fn namespace_form(name: &'static str, doc: &'static str) -> ClLiteFormSpec {
106    ClLiteFormSpec {
107        symbol: Symbol::qualified("cl", name),
108        role: ClLiteFormRole::Namespace,
109        organ: sim_lib_namespace::namespace_organ_symbol(),
110        doc,
111    }
112}
113
114fn mutation_form(name: &'static str, doc: &'static str) -> ClLiteFormSpec {
115    ClLiteFormSpec {
116        symbol: Symbol::qualified("cl", name),
117        role: ClLiteFormRole::Mutation,
118        organ: sim_lib_mutation::mutation_organ_symbol(),
119        doc,
120    }
121}