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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
use std::fmt;
use std::cmp::Ordering;
use memory::*;
use dispatch::*;
use value::Value;
use transduce::{Transducers, Process};
use handle::Handle;
use handle::STATIC_NIL;
use ::hash_map;
pub struct Meta_ { }
pub fn prism_unit() -> Unit { mechanism::prism::<Meta_>() }
pub fn is_prism(prism: AnchoredLine) -> bool { prism[0] == prism_unit() }
pub fn find_prism(h: Handle) -> Option<AnchoredLine> {
if h.is_ref() {
let prism = h.prism();
let p = prism[0];
mechanism::as_dispatch(&p).meta_value(prism)
} else {
None
}
}
pub fn has_meta(h: Handle) -> bool { find_prism(h).is_some() }
pub fn get_meta(v: Handle) -> *const Handle {
if let Some(m_prism) = find_prism(v) {
m_prism.offset(2).line().star() as *const Handle
} else {
(& STATIC_NIL) as *const Unit as *const Handle
}
}
pub fn imm_with_meta(imm: Handle, m: Handle) -> Handle {
assert!(!imm.is_ref());
let s = Segment::new(4 );
s.set(0, prism_unit());
s.set(1, 1.into());
s.set(2, m.unit());
s.set(3, imm.unit());
let h = s.unit().handle();
h
}
pub fn get_imm(prism: AnchoredLine) -> Handle {
assert!(is_prism(prism));
assert_eq!(prism[1], Unit::from(1));
prism[3].handle()
}
pub fn shim_with_meta(v: Handle, m: Handle) -> Handle {
assert!(v.is_ref());
let seg = v.segment();
let h = {
let cap = seg.capacity();
let s = Segment::new(cap + 3);
seg.at(0..cap).to_offset(s, 3);
s.set(0, prism_unit());
s.set(1, 0.into());
s.set(2, m.unit());
s.unit().handle()
};
if seg.is_aliased() {
v._alias_components();
v.retire();
} else {
assert_eq!(seg.unalias(), 0);
Segment::free(seg);
}
h
}
pub fn with_meta(v: Handle, m: Handle) -> (Handle, Handle) {
if v.is_ref() {
if let Some(m_prism) = find_prism(v) {
let w = v.unaliased();
let mp = m_prism.with_seg(w.segment());
let curr_meta = mp[2].handle();
mp.set(2, m.unit());
(w, curr_meta)
} else {
(shim_with_meta(v, m), Handle::nil())
}
} else {
(imm_with_meta(v, m), Handle::nil())
}
}
pub fn assoc_meta(v: Handle, meta_key: Handle, meta_val: Handle) -> Handle {
if v.is_ref() {
if let Some(m_prism) = find_prism(v) {
let w = v.unaliased();
let mp = m_prism.with_seg(w.segment());
let curr_meta = {
let curr_meta = mp[2].handle();
if curr_meta.is_nil() { hash_map()._consume() } else { curr_meta }
};
let (m, old_value) = curr_meta.assoc_out(meta_key, meta_val);
assert!(old_value.is_nil());
mp.set(2, m.unit());
w
} else {
let m = hash_map().assoc(meta_key.value(), meta_val.value());
shim_with_meta(v, m._consume())
}
} else {
let m = hash_map().assoc(meta_key.value(), meta_val.value());
imm_with_meta(v, m._consume())
}
}
impl Dispatch for Meta_ {
fn tear_down(&self, prism: AnchoredLine) {
assert_eq!(0, prism.segment().anchor().aliases());
prism[2].handle().retire();
if prism[1].u() == 0 {
mechanism::tear_down(prism.offset(3))
} else {
Segment::free(prism.segment());
}
}
fn alias_components(&self, prism: AnchoredLine) {
prism[2].handle().split();
if prism[1].u() == 0 {
mechanism::alias_components(prism.offset(3))
}
}
fn meta_value(&self, prism: AnchoredLine) -> Option<AnchoredLine> { Some(prism) }
fn logical_value(&self, prism: AnchoredLine) -> AnchoredLine {
if prism[1].u() == 0 {
mechanism::logical_value(prism.offset(3))
} else {
prism
}
}
}
impl Identification for Meta_ {
fn type_name(&self) -> &'static str { "Meta" }
}
use std::cell::Cell;
thread_local! {
pub static PRINT_META: Cell<u32> = Cell::new(0);
}
pub fn do_print_meta() {
PRINT_META.with(|c| {
let x = c.get();
c.set(x + 1)
});
}
pub fn end_print_meta() {
PRINT_META.with(|c| {
let x = c.get();
assert_ne!(x, 0);
c.set(x - 1)
});
}
impl Notation for Meta_ {
fn edn(&self, prism: AnchoredLine, f: &mut fmt::Formatter) -> fmt::Result {
let do_print = PRINT_META.with(|c| c.get());
if do_print > 0 {
write!(f, "^{} ", prism[2].handle())?;
}
if prism[1].u() == 0 {
let next_prism = prism.offset(3);
let p = next_prism[0];
mechanism::as_dispatch(&p).edn(next_prism, f)
} else {
log!("Meta edn, printing imm 0x{:016X}", prism[3].u());
write!(f, "{}", prism[3].handle())
}
}
}
impl Distinguish for Meta_ {
fn hash(&self, prism: AnchoredLine) -> u32 {
if prism[1].u() == 0 {
mechanism::hash(prism.offset(3))
} else {
prism[3].handle().hash()
}
}
fn eq(&self, prism: AnchoredLine, other: Unit) -> bool {
if prism[1].u() == 0 {
mechanism::eq(prism.offset(3), other)
} else {
prism[3].handle().eq(other.handle())
}
}
fn cmp(&self, prism: AnchoredLine, other: Unit) -> Option<Ordering> {
if prism[1].u() == 0 {
let next_prism = prism.offset(3);
let p = next_prism[0];
mechanism::as_dispatch(&p).cmp(next_prism, other)
} else {
prism[3].handle().cmp(other.handle())
}
}
}
impl Aggregate for Meta_ {
fn is_aggregate(&self, prism: AnchoredLine) -> bool {
if prism[1].u() == 0 {
let next_prism = prism.offset(3);
let p = next_prism[0];
mechanism::as_dispatch(&p).is_aggregate(next_prism)
} else {
false
}
}
fn count(&self, prism: AnchoredLine) -> u32 {
if prism[1].u() == 0 {
let next_prism = prism.offset(3);
let p = next_prism[0];
mechanism::as_dispatch(&p).count(next_prism)
} else {
unimplemented!()
}
}
fn empty(&self, prism: AnchoredLine) -> Unit {
if prism[1].u() == 0 {
let next_prism = prism.offset(3);
let p = next_prism[0];
mechanism::as_dispatch(&p).empty(next_prism)
} else {
unimplemented!()
}
}
fn conj(&self, prism: AnchoredLine, x: Unit) -> Unit {
if prism[1].u() == 0 {
let next_prism = prism.offset(3);
let p = next_prism[0];
mechanism::as_dispatch(&p).conj(next_prism, x)
} else {
unimplemented!()
}
}
fn peek(&self, prism: AnchoredLine) -> *const Unit {
if prism[1].u() == 0 {
let next_prism = prism.offset(3);
let p = next_prism[0];
mechanism::as_dispatch(&p).peek(next_prism)
} else {
unimplemented!()
}
}
fn pop(&self, prism: AnchoredLine) -> (Unit, Unit) {
if prism[1].u() == 0 {
let next_prism = prism.offset(3);
let p = next_prism[0];
mechanism::as_dispatch(&p).pop(next_prism)
} else {
unimplemented!()
}
}
fn get(&self, prism: AnchoredLine, k: Unit) -> *const Unit {
if prism[1].u() == 0 {
let next_prism = prism.offset(3);
let p = next_prism[0];
mechanism::as_dispatch(&p).get(next_prism, k)
} else {
unimplemented!()
}
}
fn reduce(&self, prism: AnchoredLine, process: &mut [Box<dyn Process>]) -> Value {
if prism[1].u() == 0 {
let next_prism = prism.offset(3);
let p = next_prism[0];
mechanism::as_dispatch(&p).reduce(next_prism, process)
} else {
unimplemented!()
}
}
}
impl Sequential for Meta_ {
fn is_sequential(&self, prism: AnchoredLine) -> bool {
if prism[1].u() == 0 {
let next_prism = prism.offset(3);
let p = next_prism[0];
mechanism::as_dispatch(&p).is_sequential(next_prism)
} else {
false
}
}
fn nth(&self, prism: AnchoredLine, idx: u32) -> *const Unit {
if prism[1].u() == 0 {
let next_prism = prism.offset(3);
let p = next_prism[0];
mechanism::as_dispatch(&p).nth(next_prism, idx)
} else {
unimplemented!()
}
}
}
impl Associative for Meta_ {
fn is_map(&self, prism: AnchoredLine) -> bool {
if prism[1].u() == 0 {
let next_prism = prism.offset(3);
let p = next_prism[0];
mechanism::as_dispatch(&p).is_map(next_prism)
} else {
false
}
}
fn is_set(&self, prism: AnchoredLine) -> bool {
if prism[1].u() == 0 {
let next_prism = prism.offset(3);
let p = next_prism[0];
mechanism::as_dispatch(&p).is_set(next_prism)
} else {
false
}
}
fn contains(&self, prism: AnchoredLine, x: Unit) -> bool {
if prism[1].u() == 0 {
let next_prism = prism.offset(3);
let p = next_prism[0];
mechanism::as_dispatch(&p).contains(next_prism, x)
} else {
false
}
}
fn assoc(&self, prism: AnchoredLine, k: Unit, v: Unit) -> (Unit, Unit) {
if prism[1].u() == 0 {
let next_prism = prism.offset(3);
let p = next_prism[0];
mechanism::as_dispatch(&p).assoc(next_prism, k, v)
} else {
unimplemented!()
}
}
fn dissoc(&self, prism: AnchoredLine, k: Unit) -> Unit {
if prism[1].u() == 0 {
let next_prism = prism.offset(3);
let p = next_prism[0];
mechanism::as_dispatch(&p).dissoc(next_prism, k)
} else {
unimplemented!()
}
}
}
impl Reversible for Meta_ {
fn reverse(&self, prism: AnchoredLine) -> Unit {
if prism[1].u() == 0 {
let next_prism = prism.offset(3);
let p = next_prism[0];
mechanism::as_dispatch(&p).reverse(next_prism)
} else {
unimplemented!()
}
}
}
impl Sorted for Meta_ {
fn subrange(&self, prism: AnchoredLine, start: Unit, end: Unit) -> Unit {
if prism[1].u() == 0 {
let next_prism = prism.offset(3);
let p = next_prism[0];
mechanism::as_dispatch(&p).subrange(next_prism, start, end)
} else {
unimplemented!()
}
}
}
impl Numeral for Meta_ {
fn is_numeral(&self, prism: AnchoredLine) -> bool {
if prism[1].u() == 0 {
let next_prism = prism.offset(3);
let p = next_prism[0];
mechanism::as_dispatch(&p).is_numeral(next_prism)
} else {
false
}
}
}
impl Callable for Meta_ {
fn invoke0(&self, prism: AnchoredLine) -> Unit {
if prism[1].u() == 0 {
let next_prism = prism.offset(3);
let p = next_prism[0];
mechanism::as_dispatch(&p).invoke0(next_prism)
} else {
unimplemented!()
}
}
fn invoke1(&self, prism: AnchoredLine, a: Unit) -> Unit {
if prism[1].u() == 0 {
let next_prism = prism.offset(3);
let p = next_prism[0];
mechanism::as_dispatch(&p).invoke1(next_prism, a)
} else {
unimplemented!()
}
}
}
#[cfg(test)]
mod tests {
use super::*;
}