-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgen.rs
More file actions
304 lines (267 loc) · 7.9 KB
/
Copy pathgen.rs
File metadata and controls
304 lines (267 loc) · 7.9 KB
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
macro_rules! obj_ref_t {
( $ty:ty ) => {
Arc<RwLock<$ty>>
};
}
macro_rules! obj_ref {
( $obj:expr ) => {
Arc::new(RwLock::new($obj))
};
}
pub(crate) use obj_ref;
pub(crate) use obj_ref_t;
// Types ---------------------------------------------------------------
/// Generate an intrinsic type definition. This includes the type's
/// struct and impl as well as the TypeTrait, Send, and Sync impls.
///
/// Args:
///
/// $type_name: ident
/// The type name. E.g., `NilType`
///
/// $name: ident
/// The type's object name. E.g., `Nil`
macro_rules! type_and_impls {
( $type_name:ident, $name:ident ) => {
pub struct $type_name {
ns: Namespace,
}
unsafe impl Send for $type_name {}
unsafe impl Sync for $type_name {}
impl $type_name {
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
let name = new::str(stringify!($name));
let full_name = new::str(concat!("std.", stringify!($name)));
Self {
ns: Namespace::with_entries(&[
("$name", name),
("$full_name", full_name),
]),
}
}
pub fn with_attrs(attrs: &[(&str, ObjectRef)]) -> Self {
let mut type_obj = Self::new();
type_obj.ns.extend(attrs);
type_obj
}
pub fn add_attr(&mut self, name: &str, val: ObjectRef) {
self.ns.insert(name, val);
}
pub fn add_attrs(&mut self, attrs: &[(&str, ObjectRef)]) {
self.ns.extend(attrs);
}
}
impl TypeTrait for $type_name {
fn name(&self) -> &str {
stringify!($name)
}
fn full_name(&self) -> &str {
concat!("std.", stringify!($name))
}
fn ns(&self) -> &Namespace {
&self.ns
}
}
impl ObjectTrait for $type_name {
fn as_any(&self) -> &dyn Any {
self
}
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
fn class(&self) -> TypeRef {
TYPE_TYPE.clone()
}
fn type_obj(&self) -> ObjectRef {
TYPE_TYPE.clone()
}
fn ns(&self) -> &Namespace {
&self.ns
}
fn ns_mut(&mut self) -> &mut Namespace {
&mut self.ns
}
fn as_type(&self) -> Option<&dyn TypeTrait> {
Some(self)
}
}
};
}
/// Generate standard obj impls.
macro_rules! standard_object_impls {
( $name:ident ) => {
unsafe impl Send for $name {}
unsafe impl Sync for $name {}
};
}
/// Generate `ObjectTrait` header--i.e., the standard implementations of
/// the required `ObjectTrait` methods.
///
/// Args:
///
/// $class: ident
/// The singleton type instance. E.g. `NIL_TYPE`.
macro_rules! object_trait_header {
( $class:ident ) => {
fn as_any(&self) -> &dyn Any {
self
}
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
fn class(&self) -> TypeRef {
$class.clone()
}
fn type_obj(&self) -> ObjectRef {
$class.clone()
}
fn ns(&self) -> &Namespace {
&self.ns
}
fn ns_mut(&mut self) -> &mut Namespace {
&mut self.ns
}
fn as_type(&self) -> Option<&dyn TypeTrait> {
None
}
};
}
pub(crate) use object_trait_header;
pub(crate) use standard_object_impls;
pub(crate) use type_and_impls;
// Methods -------------------------------------------------------------
/// Make a class or instance method for an intrinsic type.
///
/// Args:
///
/// $name: &str
/// The method name.
///
/// $this_type: type
/// Method receiver type (AKA `this`).
///
/// $params:
/// Params The method's parameters.
///
/// $func: fn
/// The function that implements the method. Accepts 3 args: `this`
/// (ObjectRef), `args` (Args), and `vm` (&mut VM).
///
/// This is used for adding methods to intrinsic types. It reduces a bit
/// of tedium in the process of adding methods.
///
/// Note that, in general, both class and instance methods are added to
/// the type, e.g. `IntType` and shared among instance of the type. It's
/// possible to create instance-specific methods, but I'm not sure if
/// that's useful.
///
/// Returns a 2-tuple containing the method name and the intrinsic
/// function object itself. This makes it easy to add the method to the
/// type's namespace by calling `ns.add_obj(meth!(...))`.
macro_rules! meth {
( $name:literal, $this_type:expr, $params:expr, $doc:literal, $func:expr ) => {
(
$name,
new::intrinsic_func(
"std",
$name,
Some($this_type.clone()),
$params,
$doc,
$func,
),
)
};
}
/// This is similar to `meth!` but it creates a property instead of a
/// method and has no `$params` arg.
macro_rules! prop {
( $name:literal, $this_type:expr, $doc:literal, $func:expr ) => {
(
$name,
new::prop(new::intrinsic_func(
"std",
$name,
Some($this_type.clone()),
&[],
$doc,
$func,
)),
)
};
}
/// Get arg by index. If the index is out of bounds, return an error.
///
/// Args:
///
/// $args: Args
/// $index: usize
macro_rules! use_arg {
( $args:ident, $index:literal ) => {{
if $index < $args.len() {
$args[$index].read().unwrap()
} else {
// NOTE: This should never happen from user code.
return Err(RuntimeErr::index_out_of_bounds("Arg", $index));
}
}};
}
// The use_arg_<type> macros convert the supplied arg to a value of the
// given type if possible or return an Err if not.
macro_rules! use_arg_str {
( $func_name:ident, $arg_name:ident, $arg:ident ) => {{
if let Some(val) = $arg.get_str_val() {
val
} else {
let msg = format!(
"{}() expected {} to be a Str",
stringify!($func_name),
stringify!($arg_name)
);
return Ok(new::arg_err(msg, new::nil()));
}
}};
}
macro_rules! use_arg_map {
( $func_name:ident, $arg_name:ident, $arg:ident ) => {{
if let Some(val) = $arg.get_map_val() {
val.clone()
} else {
let msg = format!(
"{}() expected {} to be a Map",
stringify!($func_name),
stringify!($arg_name)
);
return Ok(new::arg_err(msg, new::nil()));
}
}};
}
macro_rules! use_arg_usize {
( $func_name:ident, $arg_name:ident, $args:ident, $index:literal ) => {{
if $index < $args.len() {
let arg = $args[$index].read().unwrap();
if let Some(val) = arg.get_usize_val() {
val
} else {
let msg = format!(
"{}() expected {} to be an index (usize)",
stringify!($func_name),
stringify!($arg_name)
);
return Ok(new::arg_err(msg, new::nil()));
}
} else {
// NOTE: This should never happen from user code.
let msg =
format!("{}() didn't receive enough args", stringify!($func_name));
return Err(RuntimeErr::index_out_of_bounds(msg, $index));
}
}};
}
pub(crate) use meth;
pub(crate) use prop;
pub(crate) use use_arg;
pub(crate) use use_arg_map;
pub(crate) use use_arg_str;
pub(crate) use use_arg_usize;