1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
//! Queries of the registry (looking up commands, variables, types and sugar bindings).
use super::*;
impl Registry {
/// Finds the ID of a stop command by name (effect and query commands have no IDs).
pub fn command_id(&self, name: &str) -> Option<CommandId> {
self.stops
.iter()
.position(|d| d.name == name)
.map(|i| CommandId(i as u32))
}
/// The declaration of a stop command.
///
/// # Panics
/// If the ID was not made by this registry.
pub fn command(&self, id: CommandId) -> &StopDecl {
&self.stops[id.0 as usize]
}
/// Finds the kind of a command by name (stop, effect, query).
pub fn command_kind(&self, name: &str) -> Option<CommandKind> {
self.find_command(name).map(|c| c.kind)
}
/// The registered packages (in registration order).
pub fn packages(&self) -> impl Iterator<Item = PackageDecl> + '_ {
self.packages.iter().map(|e| PackageDecl {
name: e.name.to_string(),
version: e.version,
persistent_version: e.persistent_version,
depends: e.depends.iter().map(|d| (*d).to_string()).collect(),
})
}
/// Variables declared by the host (in registration order).
pub fn vars(&self) -> &[VarDecl] {
&self.vars
}
/// The names of the registered commands (stop commands, then effect and query commands; each in registration order).
pub(crate) fn command_names(&self) -> impl Iterator<Item = &str> {
self.stops
.iter()
.map(|d| d.name.as_str())
.chain(self.natives.iter().map(|d| d.name.as_str()))
}
/// Looks up a command by name. For an old name (alias), returns the information of the official command (`via_alias`).
pub(crate) fn find_command(&self, name: &str) -> Option<CommandInfo> {
if let Some((_, target)) = self.aliases.iter().find(|(old, _)| old == name) {
let mut info = self.find_canonical(target)?;
info.via_alias = true;
if info.package.is_some() {
// When using an old name, `import` the old name's namespace
info.package = name.split_once('.').map(|(ns, _)| ns.to_string());
}
return Some(info);
}
self.find_canonical(name)
}
/// Looks up only commands with official names, without resolving aliases.
pub(super) fn find_canonical(&self, name: &str) -> Option<CommandInfo> {
if let Some(i) = self.stops.iter().position(|d| d.name == name) {
let d = &self.stops[i];
return Some(CommandInfo {
name: d.name.clone(),
via_alias: false,
kind: CommandKind::Stop,
args: d.args.clone(),
ret: d.ret.clone(),
package: d.package.clone(),
params: d.params.clone(),
defaults: d.defaults.clone(),
});
}
let i = self.natives.iter().position(|d| d.name == name)?;
let d = &self.natives[i];
Some(CommandInfo {
name: d.name.clone(),
via_alias: false,
kind: match d.kind {
NativeKind::Effect => CommandKind::Effect,
NativeKind::Query => CommandKind::Query,
},
args: d.args.clone(),
ret: d.ret.clone(),
package: Some(self.packages[d.package].name.to_string()),
params: d.params.clone(),
defaults: d.defaults.clone(),
})
}
/// The old names of registered commands and their official names (in registration order).
pub fn aliases(&self) -> impl Iterator<Item = (&str, &str)> {
self.aliases.iter().map(|(a, b)| (a.as_str(), b.as_str()))
}
/// The registered `struct`s and `enum`s (in registration order).
pub fn types(&self) -> &[TypeDecl] {
&self.types
}
/// The description of a type.
pub fn type_doc(&self, name: &str) -> Option<&str> {
let i = self.types.iter().position(|t| t.name == name)?;
self.type_docs[i].as_deref()
}
/// Whether the registry was made from a declaration file. If so, it can only be used for compiling (checking) scripts
/// and cannot run in a VM (`Vm::new` and `Vm::load` return errors).
pub fn is_decl_only(&self) -> bool {
self.decl_only
}
/// The sugar binding. `Some(Some(command name))` if bound, `Some(None)` if ignored, `None` if neither.
pub fn sugar_binding(&self, sugar: Sugar) -> Option<Option<&str>> {
match self.slot(sugar)? {
Binding::Command(id) => Some(Some(self.command(id).name.as_str())),
Binding::Ignored => Some(None),
}
}
/// The declarations of all commands (in name order; stop, effect and query).
pub fn commands(&self) -> Vec<CommandDecl> {
let aliases_of = |name: &str| -> Vec<String> {
let mut v: Vec<String> = self
.aliases
.iter()
.filter(|(_, t)| t == name)
.map(|(a, _)| a.clone())
.collect();
v.sort();
v
};
let mut out: Vec<CommandDecl> = Vec::new();
for d in &self.stops {
out.push(CommandDecl {
name: d.name.clone(),
kind: CommandKind::Stop,
args: d.args.clone(),
ret: d.ret.clone(),
params: d.params.clone(),
aliases: aliases_of(&d.name),
doc: d.doc.clone(),
package: d.package.clone(),
defaults: d.defaults.clone(),
});
}
for d in &self.natives {
out.push(CommandDecl {
name: d.name.clone(),
kind: match d.kind {
NativeKind::Effect => CommandKind::Effect,
NativeKind::Query => CommandKind::Query,
},
args: d.args.clone(),
ret: d.ret.clone(),
params: d.params.clone(),
aliases: aliases_of(&d.name),
doc: d.doc.clone(),
package: Some(self.packages[d.package].name.to_string()),
defaults: d.defaults.clone(),
});
}
out.sort_by(|a, b| a.name.cmp(&b.name));
out
}
/// The list of declarations of effect and query commands (in registration order).
pub(crate) fn natives(&self) -> &[NativeDecl] {
&self.natives
}
/// Finds the position of the declaration of an effect or query command by name.
pub(crate) fn native_index(&self, name: &str) -> Option<usize> {
self.natives.iter().position(|d| d.name == name)
}
/// The list of registration information of packages (in registration order; the same order as the state slices).
pub(crate) fn package_entries(&self) -> &[PackageEntry] {
&self.packages
}
/// The argument check of stop command `id` (`None` if none).
pub(crate) fn stop_check(&self, id: CommandId) -> Option<&StopCheck> {
self.stop_checks.get(id.0 as usize).and_then(Option::as_ref)
}
/// The return value check of stop command `id` (`None` if none).
pub(crate) fn stop_ret_check(&self, id: CommandId) -> Option<&RetCheck> {
self.stop_ret_checks
.get(id.0 as usize)
.and_then(Option::as_ref)
}
/// Finds the position of a host-declared variable by name.
pub(crate) fn var_index(&self, name: &str) -> Option<usize> {
self.vars.iter().position(|v| v.name == name)
}
/// Whether it is a standard package (`import std::name`).
pub(crate) fn is_std_package(&self, name: &str) -> bool {
self.packages.iter().any(|e| e.name == name && e.std)
}
/// Whether the name can be `import`ed (a package, a namespace in which the host declared variables or types, or the
/// namespace of an old command name).
pub(crate) fn has_namespace(&self, name: &str) -> bool {
self.namespaces().iter().any(|n| n == name)
}
/// The list of names that can be `import`ed (no duplicates; packages, then namespaces of variables).
pub(crate) fn namespaces(&self) -> Vec<String> {
let mut out: Vec<String> = self.packages.iter().map(|e| e.name.to_string()).collect();
// Namespaces of variables, types and old command names can also be `import`ed
let named = self
.vars
.iter()
.map(|v| v.name.as_str())
.chain(self.types.iter().map(|t| t.name.as_str()))
.chain(self.aliases.iter().map(|(old, _)| old.as_str()));
for name in named {
if let Some((ns, _)) = name.split_once('.')
&& !out.iter().any(|n| n == ns)
{
out.push(ns.to_string());
}
}
out
}
/// The names of the variables in namespace `ns` (`ns.variable`).
pub(crate) fn vars_in(&self, ns: &str) -> impl Iterator<Item = &VarDecl> {
let prefix = format!("{ns}.");
self.vars
.iter()
.filter(move |v| v.name.starts_with(&prefix))
}
/// What plain lines are bound to (`None` if not bound).
pub(crate) fn plain_binding(&self) -> Option<Binding> {
self.plain
}
/// What quote lines are bound to (`None` if not bound).
pub(crate) fn quote_binding(&self) -> Option<Binding> {
self.quote
}
/// What choices are bound to (`None` if not bound).
pub(crate) fn choice_binding(&self) -> Option<Binding> {
self.choice
}
}