-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathjo_clojure.cpp
6696 lines (6249 loc) · 218 KB
/
jo_clojure.cpp
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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// TODO: arbitrary precision numbers... really? do I need to support this?
#define _SCL_SECURE 0
#define _HAS_ITERATOR_DEBUGGING 0
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <math.h>
#include <atomic>
#include <thread>
//#define WITH_TELEMETRY
#ifdef WITH_TELEMETRY
#pragma comment(lib,"rad_tm_win64.lib")
#include "tm/rad_tm.h"
#else
#define tmProfileThread sizeof
#endif
// get current thread id
static std::atomic<size_t> thread_uid(0);
static thread_local size_t thread_id = thread_uid.fetch_add(1, std::memory_order_relaxed);
#include "debugbreak.h"
#include "pdqsort.h"
#include "jo_stdcpp.h"
#include "jo_clojure_persistent.h"
//#define debugf printf
#ifndef debugf
#define debugf sizeof
#endif
#define warnf printf
#ifndef warnf
#define warnf sizeof
#endif
// should be first static in entire program to get actual program start.
static double time_program_start = jo_time();
static std::atomic<size_t> atom_retries(0);
static std::atomic<size_t> stm_retries(0);
static const auto processor_count = std::thread::hardware_concurrency();
enum {
// hard coded nodes
TX_R_LOCK = -3,
TX_HOLD_NODE = -2, // Set to this value if in the middle of a transaction commit. Don't touch while in this state!
INV_NODE = -1,
NIL_NODE = 0,
ZERO_NODE,
ONE_NODE,
INT_0_NODE = ZERO_NODE,
INT_256_NODE = INT_0_NODE + 256,
FALSE_NODE,
TRUE_NODE,
QUOTE_NODE,
UNQUOTE_NODE,
UNQUOTE_SPLICE_NODE,
QUASIQUOTE_NODE,
DEREF_NODE,
FN_NODE,
EMPTY_LIST_NODE,
EMPTY_VECTOR_NODE,
EMPTY_MAP_NODE,
EMPTY_SET_NODE,
EMPTY_QUEUE_NODE,
TAP_LIST_NODE,
STAR_1_NODE,
STAR_2_NODE,
STAR_3_NODE,
PCT_NODE,
PCT1_NODE,
PCT2_NODE,
PCT3_NODE,
PCT4_NODE,
PCT5_NODE,
PCT6_NODE,
PCT7_NODE,
PCT8_NODE,
K_ELSE_NODE,
K_WHEN_NODE,
K_WHILE_NODE,
K_LET_NODE,
K_META_NODE,
K_VALIDATOR_NODE,
K_ERROR_HANDLER_NODE,
K_ERROR_MODE_NODE,
K_CONTINUE_NODE,
K_FAIL_NODE,
K_DEFAULT_NODE,
K_PC_NODE,
K_ALL_NODE,
K_BY_NODE,
K_AUTO_DEREF_NODE,
START_USER_NODES,
// node types
NODE_NIL = 0,
NODE_EXCEPTION,
NODE_BOOL,
NODE_INT,
NODE_FLOAT,
NODE_STRING,
NODE_SYMBOL,
NODE_KEYWORD,
//NODE_REGEX,
NODE_LIST,
NODE_LAZY_LIST,
NODE_VECTOR,
NODE_MATRIX,
NODE_HASH_SET,
NODE_HASH_MAP,
NODE_QUEUE,
NODE_NATIVE_FUNC,
NODE_FUNC,
NODE_VAR,
NODE_DELAY,
NODE_FILE,
NODE_DIR,
NODE_ATOM,
NODE_AGENT,
NODE_FUTURE,
NODE_PROMISE,
NODE_RECUR,
NODE_REDUCED,
NODE_GIF, // jo_gif library
NODE_CANVAS,
NODE_ARRAY,
#ifndef NO_MYSQL
NODE_MYSQL,
#endif
// node flags
NODE_FLAG_MACRO = 1<<0,
NODE_FLAG_STRING = 1<<1, // string or symbol or keyword
NODE_FLAG_LAZY = 1<<2, // unused
NODE_FLAG_LITERAL = 1<<3,
NODE_FLAG_LITERAL_ARGS = 1<<4,
NODE_FLAG_PRERESOLVE = 1<<5,
NODE_FLAG_AUTO_DEREF = 1<<6,
NODE_FLAG_FOREVER = 1<<7, // never release this node
NODE_FLAG_GARBAGE = 1<<8, // this node is garbage
NODE_FLAG_CHAR = 1<<9, // char
};
struct node_t;
struct lazy_list_iterator_t;
typedef jo_shared_ptr_t<jo_object> object_ptr_t;
struct transaction_t;
typedef jo_alloc_t<transaction_t> transaction_alloc_t;
transaction_alloc_t transaction_alloc;
typedef jo_shared_ptr_t<transaction_t> transaction_ptr_t;
static transaction_ptr_t new_transaction() { return transaction_ptr_t(transaction_alloc.emplace()); }
struct env_t;
typedef jo_alloc_t<env_t> env_alloc_t;
env_alloc_t env_alloc;
typedef jo_shared_ptr_t<env_t> env_ptr_t;
static env_ptr_t new_env(env_ptr_t parent) { return env_ptr_t(env_alloc.emplace(parent)); }
typedef std::function<node_idx_t(env_ptr_t, list_ptr_t)> native_func_t;
typedef jo_shared_ptr<native_func_t> native_func_ptr_t;
typedef node_idx_t (*native_function_t)(env_ptr_t env, list_ptr_t args);
static inline node_t *get_node(node_idx_unsafe_t idx);
static inline int get_node_type(node_idx_t idx);
static inline int get_node_type(const node_t *n);
static inline int get_node_flags(node_idx_t idx);
static inline jo_string get_node_string(node_idx_t idx);
static inline node_idx_t get_node_var(node_idx_t idx);
static inline bool get_node_bool(node_idx_t idx);
static inline list_ptr_t get_node_list(node_idx_t idx);
static inline vector_ptr_t get_node_vector(node_idx_t idx);
static inline hash_map_ptr_t get_node_map(node_idx_t idx);
static inline hash_set_ptr_t get_node_hash_set(node_idx_t idx);
static inline long long get_node_int(node_idx_t idx);
static inline double get_node_float(node_idx_t idx);
static inline vector_ptr_t get_node_func_args(node_idx_t idx);
static inline list_ptr_t get_node_func_body(node_idx_t idx);
static inline node_idx_t get_node_lazy_fn(node_idx_t idx);
static inline node_idx_t get_node_lazy_fn(const node_t *n);
static inline FILE *get_node_file(node_idx_t idx);
static inline void *get_node_dir(node_idx_t idx);
static inline atomic_node_idx_t &get_node_atom(node_idx_t idx);
static inline env_ptr_t get_node_env(node_idx_t idx);
static inline env_ptr_t get_node_env(const node_t *n);
static node_idx_t new_node_symbol(const jo_string &s, int flags=0);
static node_idx_t new_node_exception(const jo_string &s, int flags=0);
static node_idx_t new_node_list(list_ptr_t nodes, int flags = 0);
static node_idx_t new_node_string(const jo_string &s, int flags = 0);
static node_idx_t new_node_hash_map(hash_map_ptr_t nodes, int flags = 0);
static node_idx_t new_node_hash_set(hash_set_ptr_t nodes, int flags = 0);
static node_idx_t new_node_vector(vector_ptr_t nodes, int flags = 0);
static node_idx_t new_node_matrix(matrix_ptr_t nodes, int flags = 0);
static node_idx_t new_node_queue(queue_ptr_t nodes, int flags = 0);
static node_idx_t new_node_lazy_list(env_ptr_t env, node_idx_t lazy_fn, int flags = 0);
static node_idx_t new_node_var(const jo_string &name, node_idx_t value, int flags = 0);
static node_idx_t node_add(node_idx_t n1i, node_idx_t n2i);
static node_idx_t node_mul(node_idx_t n1i, node_idx_t n2i);
static void node_let(env_ptr_t env, node_idx_t n1i, node_idx_t n2i);
static node_idx_t eval_node(env_ptr_t env, node_idx_t root);
static node_idx_t eval_node_list(env_ptr_t env, list_ptr_t list);
static node_idx_t eval_list(env_ptr_t env, list_ptr_t list, int list_flags=0);
#define list_va(...) new_list()->push_front_inplace(__VA_ARGS__)
#define eval_va(env, ...) eval_list(env, list_va(__VA_ARGS__))
static vector_ptr_t vector_va(node_idx_t a);
static vector_ptr_t vector_va(node_idx_t a, node_idx_t b);
static vector_ptr_t vector_va(node_idx_t a, node_idx_t b, node_idx_t c);
static vector_ptr_t vector_va(node_idx_t a, node_idx_t b, node_idx_t c, node_idx_t d);
static vector_ptr_t vector_va(node_idx_t a, node_idx_t b, node_idx_t c, node_idx_t d, node_idx_t e);
static vector_ptr_t vector_va(node_idx_t a, node_idx_t b, node_idx_t c, node_idx_t d, node_idx_t e, node_idx_t f);
static void print_node(node_idx_t node, int depth = 0, bool same_line=false);
static void print_node_type(node_idx_t node);
static void print_node_list(list_ptr_t nodes, int depth = 0);
static void print_node_vector(vector_ptr_t nodes, int depth = 0);
static void print_node_map(hash_map_ptr_t nodes, int depth = 0);
static void print_node_set(hash_map_ptr_t nodes, int depth = 0);
struct transaction_t {
struct tx_t {
node_idx_t old_val;
node_idx_t new_val;
tx_t() : old_val(INV_NODE), new_val(INV_NODE) {}
tx_t(node_idx_t o, node_idx_t n) : old_val(o), new_val(n) {}
};
//typedef node_idx_t atom_idx_t;
typedef node_idx_unsafe_t atom_idx_t;
typedef jo_hash_map<atom_idx_t, tx_t> tx_map_t;
tx_map_t tx_map;
double start_time;
int num_retries;
transaction_t() : tx_map(), start_time(jo_time() - time_program_start), num_retries() {}
node_idx_t read(atom_idx_t atom_idx) {
auto it = tx_map.find(atom_idx);
if(it.third) {
tx_t &tx = it.second;
return tx.new_val != INV_NODE ? tx.new_val : tx.old_val;
}
debugf("stm read %lld\n", atom_idx);
auto &atom = get_node_atom(atom_idx);
node_idx_t old_val = atom.load();
int count = 0;
while(old_val <= TX_HOLD_NODE) {
jo_yield_backoff(&count);
old_val = atom.load();
}
tx_map.assoc(atom_idx, tx_t(old_val, INV_NODE));
return old_val;
}
void write(atom_idx_t atom_idx, node_idx_t new_val) {
debugf("stm write %lld %lld\n", atom_idx, (long long)new_val);
tx_t &tx = tx_map.get(atom_idx);
tx.new_val = new_val;
}
// return false if failed and tx must be retried
bool commit() {
if(!tx_map.size()) {
return true;
}
const tx_map_t::entry_t **tx_list = (const tx_map_t::entry_t **)jo_alloca(tx_map.size() * sizeof(*tx_list));
int tx_list_size = 0;
for(auto tx = tx_map.begin(); tx; tx++) {
tx_list[tx_list_size++] = tx.get();
}
pdqsort(tx_list, tx_list + tx_list_size, [](const tx_map_t::entry_t *a, const tx_map_t::entry_t *b) {
return a->first < b->first;
});
// Transition all values to hold
for(auto txp = tx_list; txp != tx_list + tx_list_size; txp++) {
auto tx = *txp;
// Write stomp?
if(tx->second.old_val != INV_NODE) {
auto &atom = get_node_atom(tx->first);
long long lock_type = tx->second.new_val != INV_NODE ? TX_HOLD_NODE : TX_R_LOCK;
int num_retry = 0;
compex_retry:
// use strong here since the cost of a spurious failure can be significant
if(!atom.compare_exchange_strong(tx->second.old_val, lock_type)) {
// Don't abort if its just a read-lock. Instead retry.
if(atom.load() == TX_R_LOCK) {
jo_yield_backoff(&num_retry);
goto compex_retry;
}
// restore old values... we failed
for(auto tx2p = tx_list; tx2p != txp; tx2p++) {
auto tx2 = *tx2p;
// Write stomp? ignore these... nothing to restore.
if(tx2->second.old_val != INV_NODE) {
auto &atom2 = get_node_atom(tx2->first);
atom2.store(tx2->second.old_val);
}
}
tx_map.clear();
++num_retries;
return false;
}
}
}
// Set new values / restore reads from hold status
for(auto txp = tx_list; txp != tx_list + tx_list_size; txp++) {
auto tx = *txp;
node_idx_t store_val = tx->second.new_val != INV_NODE ? tx->second.new_val : tx->second.old_val;
auto &atom = get_node_atom(tx->first);
// If we don't have an old value, cause we only stored, grab one real quick so we can lock it.
if(tx->second.old_val == INV_NODE) {
node_idx_t old_val = atom.load();
do {
int count = 0;
while(old_val <= TX_HOLD_NODE) {
jo_yield_backoff(&count);
old_val = atom.load();
}
} while(!atom.compare_exchange_weak(old_val, store_val));
} else {
atom.store(store_val);
}
}
// TX success!
tx_map.clear();
return true;
}
};
struct env_t {
// for iterating them all, otherwise unused.
list_ptr_t vars;
//hash_map_ptr_t fast_map;
jo_hash_map<node_idx_t, node_idx_t> fast_map;
env_ptr_t parent;
transaction_ptr_t tx;
env_t() : vars(new_list()), fast_map(), parent(), tx() {}
env_t(env_ptr_t p) : vars(new_list()), fast_map(), parent(p) {
if(p) {
tx = p->tx;
}
}
void begin_transaction() {
if(!tx) {
tx = new_transaction();
}
}
bool end_transaction() {
bool ret = tx->commit();
if(ret) {
tx = parent ? parent->tx : nullptr;
} else {
stm_retries++;
}
return ret;
}
node_idx_t get(node_idx_t name) const {
auto it = fast_map.find(name, node_sym_eq);
if(it.third) {
return it.second;
}
if(parent.ptr) {
return parent.ptr->get(name);
}
return INV_NODE;
}
node_idx_t get(const char * name) const {
return get(new_node_symbol(name));
}
void remove(node_idx_t name) {
auto it = fast_map.find(name, node_sym_eq);
if(it.third) {
//vars = vars->erase(it.second);
fast_map.dissoc(it.first, node_sym_eq);
return;
}
if(parent.ptr) {
parent->remove(name);
}
}
void set(node_idx_t name, node_idx_t value) {
//node_idx_t idx = new_node_var(name, value, NODE_FLAG_FOREVER);
//vars = vars->push_front(idx);
fast_map.assoc(name, value, node_sym_eq);
assert(fast_map.contains(name, node_sym_eq));
}
void set(const char *name, node_idx_t value) {
//node_idx_t idx = new_node_var(name, value, NODE_FLAG_FOREVER);
//vars = vars->push_front(idx);
node_idx_t sym = new_node_symbol(name, NODE_FLAG_FOREVER);
fast_map.assoc(sym, value, node_sym_eq);
assert(fast_map.contains(sym, node_sym_eq));
}
// sets the map only, but cannot iterate it. for dotimes and stuffs.
void set_temp(node_idx_t name, node_idx_t value) {
fast_map.assoc(name, value, node_sym_eq);
assert(fast_map.contains(name, node_sym_eq));
}
void print_map(int depth = 0) {
printf("%*s{", depth, "");
for(auto it = fast_map.begin(); it; it++) {
print_node(it->first, depth);
printf(" = ");
print_node(it->second, depth);
printf(",\n");
}
printf("%*s}\n", depth, "");
}
};
struct lazy_list_iterator_t {
env_ptr_t env;
node_idx_t cur;
node_idx_t val;
jo_vector<node_idx_t> next_list;
node_idx_unsafe_t next_idx;
lazy_list_iterator_t(const node_t *node) : env(), cur(), val(NIL_NODE), next_list(), next_idx() {
if(get_node_type(node) == NODE_LAZY_LIST) {
env = get_node_env(node);
cur = eval_node(env, get_node_lazy_fn(node));
if(!done()) {
val = get_node_list(cur)->first_value();
}
}
}
lazy_list_iterator_t(node_idx_t node_idx) : env(), cur(node_idx), val(NIL_NODE), next_list(), next_idx() {
if(get_node_type(cur) == NODE_LAZY_LIST) {
env = get_node_env(cur);
cur = eval_node(env, get_node_lazy_fn(cur));
if(!done()) {
val = get_node_list(cur)->first_value();
}
}
}
bool done() const {
return next_idx >= (node_idx_unsafe_t)next_list.size() && !get_node_list(cur);
}
node_idx_t next() {
if(next_idx < next_list.size()) {
val = next_list[next_idx++];
return val;
}
if(done()) {
return INV_NODE;
}
cur = eval_list(env, get_node_list(cur)->rest());
if(!done()) {
val = get_node_list(cur)->first_value();
} else {
val = INV_NODE;
}
return val;
}
node_idx_t next_fn() {
if(done()) {
return NIL_NODE;
}
return new_node_list(get_node_list(cur)->rest());
}
node_idx_t next_fn(long long n) {
for(long long i = 0; i < n; i++) {
next();
}
if(done()) {
return NIL_NODE;
}
return new_node_list(get_node_list(cur)->rest());
}
node_idx_t nth(long long n) {
node_idx_t res = val;
while(n-- > 0 && !done()) {
next();
}
return val;
}
operator bool() const {
return !done();
}
list_ptr_t all(long long n = INT_MAX) {
list_ptr_t res = new_list();
while(!done() && n-- > 0) {
res->push_back_inplace(val);
next();
}
return res;
}
// fetch the next N values, and put them into next_list
void prefetch(long long n) {
// already prefetched at least n values?
if(next_list.size() > n || done()) {
return;
}
jo_vector<node_idx_t> res;
while(n-- > 0 && !done()) {
res.push_back(val);
next();
}
next_list = std::move(res);
next_idx = 0;
}
};
struct node_t {
std::atomic<int> ref_count;
int type;
int flags;
jo_string t_string;
list_ptr_t t_list;
object_ptr_t t_object;
node_idx_t t_meta;
native_func_ptr_t t_native_function;
atomic_node_idx_t t_atom;
env_ptr_t t_env;
struct {
vector_ptr_t args;
list_ptr_t body;
} t_func;
// var, delay, lazy_fn, reduced
node_idx_t t_extra;
union {
bool t_bool;
// most implementations combine these as "number", but at the moment that sounds silly
long long t_int;
double t_float;
FILE *t_file;
void *t_dir;
native_function_t t_nfunc_raw;
volatile unsigned long long t_thread_id;
};
inline list_ptr_t &as_list() { return t_list; }
inline vector_ptr_t &as_vector() { return t_object.cast<vector_t>(); }
inline matrix_ptr_t &as_matrix() { return t_object.cast<matrix_t>(); }
inline hash_map_ptr_t &as_hash_map() { return t_object.cast<hash_map_t>(); }
inline hash_set_ptr_t &as_hash_set() { return t_object.cast<hash_set_t>(); }
inline queue_ptr_t &as_queue() { return t_object.cast<queue_t>(); }
inline const vector_ptr_t &as_vector() const { return t_object.cast<vector_t>(); }
inline const matrix_ptr_t &as_matrix() const { return t_object.cast<matrix_t>(); }
inline const hash_map_ptr_t &as_hash_map() const { return t_object.cast<hash_map_t>(); }
inline const hash_set_ptr_t &as_hash_set() const { return t_object.cast<hash_set_t>(); }
inline const queue_ptr_t &as_queue() const { return t_object.cast<queue_t>(); }
node_t()
: ref_count()
, type(NODE_NIL)
, flags(0)
, t_string()
, t_list()
, t_object()
, t_native_function()
, t_atom()
, t_env()
, t_func()
, t_extra()
, t_int(0)
{
}
// copy constructor
node_t(const node_t &other)
: ref_count()
, type(other.type)
, flags(other.flags)
, t_string(other.t_string)
, t_list(other.t_list)
, t_object(other.t_object)
, t_native_function(other.t_native_function)
, t_env(other.t_env)
, t_func(other.t_func)
, t_extra(other.t_extra)
, t_int(other.t_int)
, t_atom(other.t_atom)
{
}
// move constructor
node_t(node_t &&other)
: type(other.type)
, flags(other.flags)
, t_string(std::move(other.t_string))
, t_list(std::move(other.t_list))
, t_object(std::move(other.t_object))
, t_native_function(other.t_native_function)
, t_env(std::move(other.t_env))
, t_func(std::move(other.t_func))
, t_extra(other.t_extra)
, t_int(other.t_int)
, t_atom(other.t_atom)
{
}
// move assignment operator
node_t &operator=(node_t &&other) {
type = other.type;
flags = other.flags;
t_string = std::move(other.t_string);
t_list = std::move(other.t_list);
t_object = std::move(other.t_object);
t_native_function = other.t_native_function;
t_atom.store(other.t_atom.load());
t_env = std::move(other.t_env);
t_func = std::move(other.t_func);
t_extra = other.t_extra;
t_int = other.t_int;
return *this;
}
void release() {
ref_count = 0;
type = NODE_NIL;
flags = NODE_FLAG_GARBAGE;
t_list = nullptr;
t_object = nullptr;
t_native_function = nullptr;
t_atom.store(0);
t_env = nullptr;
t_func.args = nullptr;
t_func.body = nullptr;
t_extra = 0;
t_int = 0;
}
inline bool is_symbol() const { return type == NODE_SYMBOL; }
inline bool is_keyword() const { return type == NODE_KEYWORD; }
inline bool is_list() const { return type == NODE_LIST; }
inline bool is_vector() const { return type == NODE_VECTOR; }
inline bool is_matrix() const { return type == NODE_MATRIX; }
inline bool is_hash_map() const { return type == NODE_HASH_MAP; }
inline bool is_hash_set() const { return type == NODE_HASH_SET; }
inline bool is_queue() const { return type == NODE_QUEUE; }
inline bool is_lazy_list() const { return type == NODE_LAZY_LIST; }
inline bool is_string() const { return type == NODE_STRING; }
inline bool is_func() const { return type == NODE_FUNC; }
inline bool is_native_func() const { return type == NODE_NATIVE_FUNC; }
inline bool is_macro() const { return flags & NODE_FLAG_MACRO;}
inline bool is_float() const { return type == NODE_FLOAT; }
inline bool is_int() const { return type == NODE_INT; }
inline bool is_file() const { return type == NODE_FILE; }
inline bool is_dir() const { return type == NODE_DIR; }
inline bool is_atom() const { return type == NODE_ATOM; }
inline bool is_future() const { return type == NODE_FUTURE; }
inline bool is_promise() const { return type == NODE_PROMISE; }
inline bool is_seq() const { return is_list() || is_lazy_list() || is_hash_map() || is_hash_set() || is_vector() || is_string(); }
inline bool can_eval() const { return is_symbol() || is_keyword() || is_list() || is_vector() || is_hash_map() || is_hash_set() || is_func() || is_native_func(); }
// first, more?
typedef jo_pair<node_idx_t, bool> seq_first_t;
seq_first_t seq_first() const {
if(is_list()) return seq_first_t(t_list->first_value(), !t_list->empty());
if(is_vector()) return seq_first_t(as_vector()->first_value(), !as_vector()->empty());
if(is_hash_map()) {
if(as_hash_map()->empty()) return seq_first_t(NIL_NODE, false);
auto e = as_hash_map()->first();
return seq_first_t(new_node_vector(vector_va(e.first, e.second)), true);
}
if(is_hash_set()) {
if(as_hash_set()->empty()) return seq_first_t(NIL_NODE, false);
return seq_first_t(as_hash_set()->first_value(), true);
}
if(is_lazy_list()) {
lazy_list_iterator_t lit(this);
if(lit.done()) return seq_first_t(NIL_NODE, false);
return seq_first_t(lit.val, true);
}
if(is_string()) {
if(!t_string.length()) return seq_first_t(NIL_NODE, false);
return seq_first_t(new_node_int(t_string.c_str()[0], NODE_FLAG_CHAR), true);
}
return seq_first_t(NIL_NODE, false);
}
// second
typedef jo_pair<node_idx_t, bool> seq_second_t;
seq_second_t seq_second() const {
if(is_list()) return seq_second_t(t_list->second_value(), t_list->size() >= 2);
if(is_vector()) return seq_second_t(as_vector()->nth(1), as_vector()->size() >= 2);
if(is_hash_map()) {
if(as_hash_map()->size() < 2) return seq_second_t(NIL_NODE, false);
auto e = as_hash_map()->second();
return seq_second_t(new_node_vector(vector_va(e.first, e.second)), true);
}
if(is_hash_set()) {
if(as_hash_set()->size() < 2) return seq_second_t(NIL_NODE, false);
return seq_second_t(as_hash_set()->second_value(), true);
}
if(is_lazy_list()) {
lazy_list_iterator_t lit(this);
if(lit.done()) return seq_second_t(NIL_NODE, false);
lit.next();
if(lit.done()) return seq_second_t(NIL_NODE, false);
return seq_second_t(lit.val, true);
}
if(is_string()) {
if(t_string.length() < 2) return seq_second_t(NIL_NODE, false);
return seq_second_t(new_node_int(t_string.c_str()[1], NODE_FLAG_CHAR), true);
}
return seq_second_t(NIL_NODE, false);
}
typedef jo_pair<node_idx_t, bool> seq_rest_t;
seq_rest_t seq_rest() const {
if(is_list()) {
if(t_list->empty()) return seq_rest_t(NIL_NODE, false);
return seq_rest_t(new_node_list(t_list->rest()), true);
}
if(is_vector()) {
if(as_vector()->empty()) return seq_rest_t(NIL_NODE, false);
return seq_rest_t(new_node_vector(as_vector()->rest()), true);
}
if(is_hash_map()) {
if(as_hash_map()->empty()) return seq_rest_t(NIL_NODE, false);
return seq_rest_t(new_node_hash_map(as_hash_map()->rest()), true);
}
if(is_hash_set()) {
if(as_hash_set()->empty()) return seq_rest_t(NIL_NODE, false);
return seq_rest_t(new_node_hash_set(as_hash_set()->rest()), true);
}
if(is_lazy_list()) {
lazy_list_iterator_t lit(this);
if(lit.done()) return seq_rest_t(NIL_NODE, false);
return seq_rest_t(new_node_lazy_list(t_env, lit.next_fn()), true);
}
if(is_string()) {
if(!t_string.length()) return seq_rest_t(NIL_NODE, false);
return seq_rest_t(new_node_string(t_string.substr(1)), true);
}
return seq_rest_t(NIL_NODE, false);
}
// first, rest, more?
typedef jo_tuple<node_idx_t, node_idx_t, bool> seq_first_rest_t;
seq_first_rest_t seq_first_rest() const {
if(is_list()) {
if(t_list->empty()) return seq_first_rest_t(NIL_NODE, NIL_NODE, false);
return seq_first_rest_t(t_list->first_value(), new_node_list(t_list->rest()), true);
}
if(is_vector()) {
if(as_vector()->empty()) return seq_first_rest_t(NIL_NODE, NIL_NODE, false);
return seq_first_rest_t(as_vector()->first_value(), new_node_vector(as_vector()->rest()), true);
}
if(is_hash_map()) {
if(as_hash_map()->empty()) return seq_first_rest_t(NIL_NODE, NIL_NODE, false);
auto e = as_hash_map()->first();
return seq_first_rest_t(new_node_vector(vector_va(e.first, e.second)), new_node_hash_map(as_hash_map()->rest()), true);
}
if(is_hash_set()) {
if(as_hash_set()->empty()) return seq_first_rest_t(NIL_NODE, NIL_NODE, false);
return seq_first_rest_t(as_hash_set()->first_value(), new_node_hash_set(as_hash_set()->rest()), true);
}
if(is_lazy_list()) {
lazy_list_iterator_t lit(this);
if(lit.done()) return seq_first_rest_t(NIL_NODE, NIL_NODE, false);
return seq_first_rest_t(lit.val, new_node_lazy_list(t_env, lit.next_fn()), true);
}
if(is_string() && t_string.length()) {
if(!t_string.length()) return seq_first_rest_t(NIL_NODE, NIL_NODE, false);
return seq_first_rest_t(INT_0_NODE + t_string.c_str()[0], new_node_string(t_string.substr(1)), true);
}
return seq_first_rest_t(NIL_NODE, NIL_NODE, false);
}
bool seq_empty() const {
if(is_list()) return t_list->empty();
if(is_vector()) return as_vector()->empty();
if(is_hash_map()) return as_hash_map()->size() == 0;
if(is_hash_set()) return as_hash_set()->size() == 0;
if(is_lazy_list()) {
lazy_list_iterator_t lit(this);
return lit.done();
}
if(is_string() && t_string.length()) return false;
return true;
}
typedef jo_pair<node_idx_t, bool> seq_take_t;
seq_take_t seq_take(size_t n) const {
if(is_list()) {
if(t_list->empty()) return seq_take_t(NIL_NODE, false);
return seq_take_t(new_node_list(t_list->take(n)), true);
}
if(is_vector()) {
if(as_vector()->empty()) return seq_take_t(NIL_NODE, false);
return seq_take_t(new_node_vector(as_vector()->take(n)), true);
}
if(is_hash_map()) {
if(as_hash_map()->empty()) return seq_take_t(NIL_NODE, false);
return seq_take_t(new_node_hash_map(as_hash_map()->take(n)), true);
}
if(is_hash_set()) {
if(as_hash_set()->empty()) return seq_take_t(NIL_NODE, false);
return seq_take_t(new_node_hash_set(as_hash_set()->take(n)), true);
}
if(is_lazy_list()) {
lazy_list_iterator_t lit(this);
if(lit.done()) return seq_take_t(NIL_NODE, false);
return seq_take_t(new_node_list(lit.all(n)), true);
}
if(is_string()) {
if(!t_string.length()) return seq_take_t(NIL_NODE, false);
return seq_take_t(new_node_string(t_string.substr(0, n)), true);
}
return seq_take_t(NIL_NODE, false);
}
node_idx_t seq_drop(size_t n) const {
if(is_list()) return new_node_list(t_list->drop(n));
if(is_vector()) return new_node_vector(as_vector()->drop(n));
if(is_hash_map()) return new_node_hash_map(as_hash_map()->drop(n));
if(is_hash_set()) return new_node_hash_set(as_hash_set()->drop(n));
if(is_lazy_list()) {
lazy_list_iterator_t lit(this);
return new_node_list(lit.all(n));
}
if(is_string() && t_string.length()) return new_node_string(t_string.substr(n));
return NIL_NODE;
}
size_t seq_size() const {
if(is_list()) return t_list->size();
if(is_vector()) return as_vector()->size();
if(is_hash_map()) return as_hash_map()->size();
if(is_hash_set()) return as_hash_set()->size();
if(is_lazy_list()) {
lazy_list_iterator_t lit(this);
return lit.all()->size();
}
if(is_string() && t_string.length()) return t_string.length();
return NIL_NODE;
}
void seq_push_back(node_idx_t x) {
if(is_list()) {
t_list = t_list->push_back(x);
} else if(is_vector()) {
t_object = as_vector()->push_back(x).cast<jo_object>();
} else if(is_hash_map()) {
warnf("map.push_back: not implemented");
} else if(is_hash_set()) {
warnf("hash_set.push_back: not implemented");
} else if(is_lazy_list()) {
warnf("seq_push_back: not implemented for lazy lists");
} else if(is_string()) {
t_string += get_node_string(x);
}
}
bool as_bool() const {
switch(type) {
case NODE_BOOL: return t_bool;
case NODE_INT: return t_int != 0;
case NODE_FLOAT: return t_float != 0.0;
case NODE_SYMBOL:
case NODE_KEYWORD:
case NODE_STRING:
if(t_string == "nil") return false;
return t_string.length() > 0;
case NODE_LIST:
case NODE_LAZY_LIST:
case NODE_VECTOR:
case NODE_MATRIX:
case NODE_HASH_SET:
case NODE_HASH_MAP: return true; // TODO
case NODE_NIL:
default:
return false;
}
}
long long as_int() const {
switch(type) {
case NODE_BOOL: return t_bool;
case NODE_INT: return t_int;
case NODE_FLOAT: return (long long)t_float;
case NODE_FUNC:
case NODE_VAR:
case NODE_SYMBOL:
case NODE_KEYWORD:
case NODE_STRING: return atoi(t_string.c_str());
}
return 0;
}
double as_float() const {
switch(type) {
case NODE_BOOL: return t_bool;
case NODE_INT: return t_int;
case NODE_FLOAT: return t_float;
case NODE_FUNC:
case NODE_VAR:
case NODE_SYMBOL:
case NODE_KEYWORD:
case NODE_STRING: return atof(t_string.c_str());
}
return 0;
}
jo_string as_string(int pretty = 0) const {
switch(type) {
case NODE_NIL: return "nil";
case NODE_BOOL: return t_bool ? "true" : "false";
case NODE_INT:
// only letter? TODO
if((flags & NODE_FLAG_CHAR) && jo_isletter(t_int)) {
if(pretty >= 2) {
if(t_int == 32) return "\\space";
if(t_int == 9) return "\\tab";
if(t_int == 10) return "\\newline";
if(t_int == 13) return "\\return";
return "\\" + jo_string(t_int);
}
return jo_string(t_int);
}
return va("%lld", t_int);
case NODE_FLOAT: return va("%g", t_float);
case NODE_LIST:
{
jo_string s;
s = '(';
if(t_list.ptr) for(list_t::iterator it(t_list); it;) {
s += get_node(*it)->as_string(3);
++it;
if(it) {
s += " ";
}
}
s += ')';
return s;
}
case NODE_VECTOR:
{
jo_string s;
s = '[';
if(as_vector().ptr) for(auto it = as_vector()->begin(); it;) {
s += get_node(*it)->as_string(3);
++it;
if(it) {
s += " ";
}
}
s += ']';
return s;
}
case NODE_MATRIX:
{
matrix_ptr_t M = as_matrix();
jo_string s;
s = va("(matrix %d %d [", M->width, M->height);
for(int j = 0; j < M->height; ++j) {
for(int i = 0; i < M->width; ++i) {
s += get_node(M->get(i,j))->as_string(3);
if(i < M->width - 1) s += " ";
}
if(j < M->height - 1) s += ", ";
}
s += "])";
return s;
}
case NODE_HASH_MAP: