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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
use std::fmt;
use std::cmp;
use memory::*;
use dispatch::*;
use value::*;
use transduce::{Transducers, Process};
use meta;
#[derive(Copy, Clone)]
pub struct Handle {
pub unit: Unit,
}
pub static STATIC_NIL: Unit = Handle::NIL;
impl From<Unit> for Handle {
fn from(u: Unit) -> Self { Handle { unit: u } }
}
impl From<Value> for Handle {
fn from(v: Value) -> Self {
let ret = v.handle;
use std::mem::forget;
forget(v);
ret
}
}
impl Handle {
pub fn unit(self) -> Unit { self.unit }
pub fn segment(self) -> Segment { self.unit.segment() }
pub fn value(self) -> Value { Value { handle: self } }
pub const NIL: Unit = Unit { word: 0x07 };
pub const TRUE: Unit = Unit { word: !0x00usize };
pub const FALSE: Unit = Unit { word: !0x08usize };
pub fn nil() -> Handle { Handle::from(Handle::NIL) }
pub fn tru() -> Handle { Handle::from(Handle::TRUE) }
pub fn fals() -> Handle { Handle::from(Handle::FALSE) }
pub fn is_nil(self) -> bool { self.unit == Handle::NIL }
pub fn is_true(self) -> bool { self.unit == Handle::TRUE }
pub fn is_false(self) -> bool { self.unit == Handle::FALSE }
pub fn is_bool(self) -> bool { self.unit.u() | 0x8 == !0 }
pub fn is_flag(self) -> bool { self.unit.u() & 0x7 == 0x7 }
pub fn is_not(self) -> bool { self.unit.u() & 0xF == 0x7 }
pub fn is_so(self) -> bool { !self.is_not() }
fn tag(self) -> usize { self.unit.u() & 0xF }
pub fn is_imm_char(self) -> bool { self.tag() == 0x3 }
pub fn is_imm_int(self) -> bool { self.tag() == 0x1 }
pub fn is_imm_float(self) -> bool { self.tag() == 0x9 }
}
impl Handle {
pub fn is_ref(self) -> bool { self.unit.is_even() }
pub fn split(self) -> Handle {
if self.is_ref() { self.segment().alias() }
self
}
pub fn retire(self) {
if self.is_ref() && self.segment().unalias() == 0 {
self._tear_down()
}
}
pub fn prism(self) -> AnchoredLine { self.segment().line_at(0) }
pub fn _tear_down(self) {
mechanism::tear_down(self.prism())
}
pub fn _alias_components(self) {
mechanism::alias_components(self.prism())
}
pub fn unaliased(self) -> Handle {
if self.is_ref() {
let seg = self.segment();
if seg.is_aliased() {
self._alias_components();
let s = seg.carbon_copy();
self.retire();
s.unit().handle()
} else {
self
}
} else {
self
}
}
pub fn type_name(self) -> &'static str {
if self.is_ref() {
let prism = self.prism();
let p = prism[0];
mechanism::as_dispatch(&p).type_name()
} else {
if self.is_nil() { return "Nil" }
if self.is_bool() { return "Boolean" }
if self.is_imm_char() { return "Character" }
if self.is_imm_int() { return "Integral" }
if self.is_imm_float() { return "FloatPoint" }
unreachable!("Bad handle unit!: 0x{:016X}", self.unit.u())
}
}
pub fn logical_value(self) -> AnchoredLine {
if self.is_ref() {
mechanism::logical_value(self.prism())
} else { unimplemented!() }
}
pub fn eq(self, other: Handle) -> bool {
if self.unit == other.unit { return true; }
if self.is_ref() {
mechanism::eq(self.prism(), other.unit)
} else if other.is_ref() {
mechanism::eq(other.prism(), self.unit)
} else {
self.unit == other.unit
}
}
pub fn cmp(self, other: Handle) -> Option<cmp::Ordering> {
if self.is_ref() {
let prism = self.prism();
let p = prism[0];
mechanism::as_dispatch(&p).cmp(prism, other.unit)
} else if other.is_ref() {
let prism = other.prism();
let p = prism[0];
mechanism::as_dispatch(&p).cmp(prism, self.unit)
} else {
self.unit.partial_cmp(&other.unit)
}
}
pub fn hash(self) -> u32 {
if self.is_ref() {
mechanism::hash(self.prism())
} else {
use hash::hash_64;
hash_64(self.unit.u64(), 8)
}
}
pub fn meta(self) -> *const Handle {
meta::get_meta(self)
}
pub fn with_meta(self, m: Handle) -> (Handle, Handle) {
meta::with_meta(self, m)
}
pub fn assoc_meta(self, meta_key: Handle, meta_val: Handle) -> Handle {
meta::assoc_meta(self, meta_key, meta_val)
}
pub fn as_immediate(self) -> Handle {
if self.is_ref() {
let prism = self.logical_value();
if meta::is_prism(prism) {
meta::get_imm(prism)
} else {
Handle::nil()
}
} else {
self
}
}
pub fn invoke1(self, a: Handle) -> Handle {
if self.is_ref() {
let prism = self.prism();
let p = prism[0];
mechanism::as_dispatch(&p).invoke1(prism, a.unit()).handle()
} else { unimplemented!() }
}
}
impl fmt::Display for Handle {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if self.is_ref() {
let prism = self.prism();
let p = prism[0];
mechanism::as_dispatch(&p).edn(prism, f)
} else {
if self.is_flag() {
let name = if self.is_nil() { "nil" }
else if self.is_true() { "true" } else { "false" };
write!(f, "{}", name)
} else if self.is_imm_int() {
let x: i64 = self.unit.into();
write!(f, "{}", x >> 4)
} else if self.is_imm_float() {
unimplemented!()
} else if self.is_imm_char() {
use character;
character::display(self.unit, f)
} else {
unreachable!("Bad handle unit!: 0x{:016X}", self.unit.u())
}
}
}
}
impl fmt::Debug for Handle {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
meta::do_print_meta();
let res = fmt::Display::fmt(self, f);
meta::end_print_meta();
res
}
}
impl Handle {
pub fn empty(self) -> Handle {
if self.is_ref() {
let prism = self.prism();
let p = prism[0];
mechanism::as_dispatch(&p).empty(prism).handle()
} else { unimplemented!() }
}
pub fn peek(self) -> *const Handle {
if self.is_ref() {
let prism = self.prism();
let p = prism[0];
let elem = mechanism::as_dispatch(&p).peek(prism);
elem as *const Handle
} else { unimplemented!() }
}
pub fn count(self) -> u32 {
if self.is_ref() {
let prism = self.prism();
let p = prism[0];
mechanism::as_dispatch(&p).count(prism)
} else { unimplemented!() }
}
pub fn contains(self, k: Handle) -> bool {
if self.is_ref() {
let prism = self.prism();
let p = prism[0];
mechanism::as_dispatch(&p).contains(prism, k.unit())
} else { unimplemented!() }
}
pub fn conj(self, x: Handle) -> Handle {
if self.is_ref() {
let prism = self.prism();
let p = prism[0];
mechanism::as_dispatch(&p).conj(prism, x.unit).handle()
} else { unimplemented!() }
}
pub fn pop(self) -> (Handle, Handle) {
if self.is_ref() {
let prism = self.prism();
let p = prism[0];
let (c, v) = mechanism::as_dispatch(&p).pop(prism);
(c.handle(), v.handle())
} else { unimplemented!() }
}
pub fn assoc(self, k: Handle, v: Handle) -> Handle {
let (c, displaced) = self.assoc_out(k, v);
displaced.retire();
c
}
pub fn assoc_out(self, k: Handle, v: Handle) -> (Handle, Handle) {
if self.is_ref() {
let prism = self.prism();
let p = prism[0];
let (c, displaced) = mechanism::as_dispatch(&p).assoc(prism, k.unit, v.unit);
(c.handle(), displaced.handle())
} else {
unimplemented!("assoc_out on {}", self)
}
}
pub fn dissoc(self, k: Handle) -> Handle {
if self.is_ref() {
let prism = self.prism();
let p = prism[0];
let v = mechanism::as_dispatch(&p).dissoc(prism, k.unit);
v.handle()
} else { unimplemented!() }
}
pub fn get(self, k: Handle) -> *const Handle {
if self.is_ref() {
let prism = self.prism();
let p = prism[0];
let v = mechanism::as_dispatch(&p).get(prism, k.unit);
v as *const Handle
} else {
if self.is_nil() {
(& STATIC_NIL) as *const Unit as *const Handle
} else {
unimplemented!()
}
}
}
pub fn nth(self, idx: u32) -> *const Handle {
if self.is_ref() {
let prism = self.prism();
let p = prism[0];
let elem = mechanism::as_dispatch(&p).nth(prism, idx);
elem as *const Handle
} else { unimplemented!() }
}
pub fn nth_set(self, idx: u32, v: Handle) -> Handle {
if self.is_ref() {
let prism = self.prism();
let p = prism[0];
let res = mechanism::as_dispatch(&p).nth_set(prism, idx, v.unit);
res.handle()
} else { unimplemented!() }
}
pub fn swap_idx(self, i: u32, j: u32) -> Handle {
if self.is_ref() {
let prism = self.prism();
let p = prism[0];
let res = mechanism::as_dispatch(&p).swap_idx(prism, i, j);
res.handle()
} else { unimplemented!() }
}
pub fn reduce(self, stack: &mut [Box<dyn Process>]) -> Value {
if self.is_ref() {
let prism = self.prism();
let p = prism[0];
mechanism::as_dispatch(&p).reduce(prism, stack)
} else { unimplemented!() }
}
pub fn pour(self, xf: Transducers, sink: Handle) -> Handle {
struct Collect {
c: Handle,
}
impl Process for Collect {
fn ingest (&mut self, stack: &mut [Box<dyn Process>], v: Value) -> Option<Value> {
self.c = self.c.conj(v._consume());
None
}
fn ingest_kv(&mut self, stack: &mut [Box<dyn Process>], k: Value, v: Value)
-> Option<Value> {
self.c = self.c.assoc(k._consume(), v._consume());
None
}
fn last_call(&mut self, stack: &mut [Box<dyn Process>]) -> Value { self.c.value() }
}
let mut stack = {
let stack: Vec<Box<dyn Process>> = vec!(Box::new(Collect { c: sink }));
xf.apply(stack)
};
self.reduce(&mut stack)._consume()
}
pub fn as_i64(self) -> i64 {
if self.is_ref() {
use integral;
if let Some(prism) = integral::find_prism(self) {
integral::as_i64(prism)
} else {
unimplemented!("Can't turn {} into an integer.", self)
}
} else {
if self.is_imm_int() {
let x: i64 = self.unit.into();
(x >> 4)
} else {
unimplemented!("Can't turn {} into an integer.", self)
}
}
}
}
impl Handle {
pub fn find_prism(self, prism_unit: Unit) -> Option<AnchoredLine> {
if self.is_ref() {
let prism = self.prism();
if prism[0] == prism_unit {
Some(prism)
} else {
let prism = self.logical_value();
if prism[0] == prism_unit {
Some(prism)
} else {
None
}
}
} else {
None
}
}
pub fn is_integral(self) -> bool {
use integral;
integral::is_integral(self)
}
pub fn is_string(self) -> bool {
use string;
string::is_string(self)
}
pub fn is_symbol(self) -> bool {
use symbol;
symbol::is_symbol(self)
}
pub fn is_list(self) -> bool {
use list;
list::is_list(self)
}
pub fn is_vector(self) -> bool {
use vector;
vector::is_vector(self)
}
pub fn is_set(self) -> bool {
if self.is_ref() {
let prism = self.prism();
let p = prism[0];
mechanism::as_dispatch(&p).is_set(prism)
} else { false }
}
pub fn is_map(self) -> bool {
if self.is_ref() {
let prism = self.prism();
let p = prism[0];
mechanism::as_dispatch(&p).is_map(prism)
} else { false }
}
pub fn is_aggregate(self) -> bool {
if self.is_ref() {
let prism = self.prism();
let p = prism[0];
mechanism::as_dispatch(&p).is_aggregate(prism)
} else { false }
}
}
impl Handle {
pub fn has_namespace(self) -> bool {
use symbol;
if let Some(prism) = symbol::find_prism(self) {
symbol::has_namespace(prism)
} else {
unimplemented!()
}
}
}
impl Handle {
pub fn add(self, rhs: Handle) -> Handle {
if self.is_ref() {
let prism = self.prism();
let p = prism[0];
let sum = mechanism::as_dispatch(&p).add(prism, rhs.unit);
sum.handle()
} else { unimplemented!() }
}
pub fn sub(self, rhs: Handle) -> Handle {
if self.is_ref() {
let prism = self.prism();
let p = prism[0];
let diff = mechanism::as_dispatch(&p).subtract(prism, rhs.unit);
diff.handle()
} else { unimplemented!() }
}
pub fn mul(self, rhs: Handle) -> Handle {
if self.is_ref() {
let prism = self.prism();
let p = prism[0];
let product = mechanism::as_dispatch(&p).multiply(prism, rhs.unit);
product.handle()
} else { unimplemented!() }
}
pub fn div(self, rhs: Handle) -> Handle {
if self.is_ref() {
let prism = self.prism();
let p = prism[0];
let num = mechanism::as_dispatch(&p).divide(prism, rhs.unit);
num.handle()
} else { unimplemented!() }
}
pub fn rem(self, rhs: Handle) -> Handle {
if self.is_ref() {
let prism = self.prism();
let p = prism[0];
let num = mechanism::as_dispatch(&p).remainder(prism, rhs.unit);
num.handle()
} else { unimplemented!() }
}
pub fn modulus(self, rhs: Handle) -> Handle {
if self.is_ref() {
let prism = self.prism();
let p = prism[0];
let num = mechanism::as_dispatch(&p).modulus(prism, rhs.unit);
num.handle()
} else { unimplemented!() }
}
pub fn inc(self) -> Handle {
if self.is_ref() {
let prism = self.prism();
let p = prism[0];
let x = mechanism::as_dispatch(&p).inc(prism);
x.handle()
} else { unimplemented!() }
}
pub fn dec(self) -> Handle {
if self.is_ref() {
let prism = self.prism();
let p = prism[0];
let x = mechanism::as_dispatch(&p).dec(prism);
x.handle()
} else { unimplemented!() }
}
pub fn neg(self) -> Handle {
if self.is_ref() {
let prism = self.prism();
let p = prism[0];
let x = mechanism::as_dispatch(&p).neg(prism);
x.handle()
} else { unimplemented!() }
}
pub fn bitand(self, rhs: Handle) -> Handle { unimplemented!() }
pub fn bitor(self, rhs: Handle) -> Handle { unimplemented!() }
pub fn bitxor(self, rhs: Handle) -> Handle { unimplemented!() }
pub fn shl(self, rhs: u32) -> Handle { unimplemented!() }
pub fn shr(self, rhs: u32) -> Handle { unimplemented!() }
}
impl PartialEq for Handle {
fn eq(&self, other: &Handle) -> bool { (*self).eq(*other) }
}
impl PartialOrd for Handle {
fn partial_cmp(&self, other: &Handle) -> Option<cmp::Ordering> { self.cmp(*other) }
}