-
Notifications
You must be signed in to change notification settings - Fork 11
/
verbs.c
2966 lines (2924 loc) · 94.5 KB
/
verbs.c
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
#include "def.h"
#include "proto.h"
#include "accessors.h"
#include "vary.h"
int _equal_container(const VP x,const VP y) {
ITERV(x,{ IF_RET(_equal(ELl(x,_i),ELl(y,_i))==0, 0); });
return 1;
}
int _equal(const VP x,const VP y) {
// this is the most common call in the code
// TODO _equal() needs to handle comparison tolerance and type conversion
// TODO _equal should use the new VARY_*() macros, except for general lists
// XRAY_log("_equal\n"); XRAY_emit(x); XRAY_emit(y);
// if the list is a container for one item, we probably want to match the inner one
if(x==NULL||y==NULL) return 0;
VP a=x,b=y;
if(LIST(a) && SCALAR(a)) a=ELl(a,0);
if(LIST(b) && SCALAR(b)) b=ELl(b,0);
IF_RET(a->n != b->n, 0);
if(CONTAINER(a) && CONTAINER(b)) return _equal_container(a,b);
if(a->t == b->t) {
return memcmp(BUF(a),BUF(b),a->itemsz*a->n)==0;
} else if (COMPARABLE(a)&&COMPARABLE(b)) {
int typerr=-1;
VARY_EACHBOTH(a,b,({ if (_x != _y) return 0; }),typerr);
if(typerr>-1) return 0;
else return 1;
} else
return 0;
}
VP equal(const VP x,const VP y) {
return xb(_equal(x,y));
}
MEMO_make(CLONE);
VP clone0(const VP obj) {
if(IS_EXC(obj)) return obj;
XRAY_log("clone0 %p\n",obj);XRAY_emit(obj);
int i, objn=obj->n;
MEMO_check(CLONE,obj,({ XRAY_log("found memoized %p\n", memo_val); return xref(memo_val); }),i);
if(i==MEMO_sz) { return EXC(Tt(stack),__func__,0,0); }
VP res=ALLOC_LIKE(obj);
MEMO_set(CLONE,obj,res,i);
if(objn) {
if(CONTAINER(obj)) {
res->n=objn;
for(i=0;i<objn;i++) {
VP elem=LIST_item(obj,i);
if(elem==0) { res->n--; continue; };
if(IS_t(elem) || IS_1(elem) || IS_2(elem)) EL(res,VP,i)=xref(elem);
else EL(res,VP,i)=clone0(elem);
}
} else
res=appendbuf(res,BUF(obj),objn);
}
return res;
}
VP clone(const VP obj) {
// TODO keep a counter of clone events for performance reasons - these represent a concrete
// loss over mutable systems
XRAY_log("clone\n");XRAY_emit(obj);
MEMO_clear(CLONE);
VP res=clone0(obj);
XRAY_log("clone returning %p\n",res);XRAY_emit(res);
return res;
}
VP wherepred_(const VP x,const VP y,int removematches) {
VP where=matchany(x,y);
if(IS_EXC(where)) return where;
if(LIST(where)) where=list2vec(where);
VP invw;
if(removematches) invw=not(where);
else invw=where;
VP wherec=condense(invw);
if(IS_EXC(wherec)) return wherec;
XRAY_log("except indices to apply for result\n");XRAY_emit(wherec);
VP res=apply(x,wherec);
xfree(wherec);
xfree(where);
if(removematches) xfree(invw);
if(TABLE(x) && DICT(res)) {
// tables when indexed with one int, like x@0, return
// a single row as a dict. For our purposes, we want that
// to remain a list.
VP k=KEYS(res), v=xl(VALS(res)), t=dict(k,v);
xfree(res); xfree(v);
return t;
} else
return res;
}
// VALUE MANIPULATION
inline VP appendbuf(VP x,const buf_t buf,const size_t nelem) {
int newn;buf_t dest;
//XRAY_log("appendbuf %d\n", nelem);XRAY_emit(x);
newn=x->n+nelem;
x=XREALLOC(x,newn);
// XRAY_log("after realloc"); XRAY_emit(x);
dest=ELi(x,x->n);
memmove(dest,buf,x->itemsz * nelem);
x->n=newn;
// XRAY_log("appendbuf newn %d\n", newn); XRAY_emitRAW(dest, x->itemsz * newn);
return x;
}
VP append(VP x,VP y) {
// append all items of y to x. if x is a general list, append pointer to y, and increase refcount.
// XRAY_log("append %p %p\n",x,y); XRAY_emit(x); XRAY_emit(y);
if(IS_EXC(x)) return x;
if(!CONTAINER(x) && !(x->t==y->t)) { return EXC(Tt(type),"append x must be container or types must match", x, y); }
if(TABLE(x)) return catenate_table(x,y);
if(IS_x(x)) {
if(VALS(x)) xfree(VALS(x)); VALS(x)=y; return x;
}
if(IS_d(x)) {
// XRAY_log("append d %p\n", x); XRAY_emit(x);
ASSERT(y->n % 2 == 0, "append to a dict with ['key;value]");
VP k=KEYS(x),v=VALS(x),y1,y2; int i;
y1=ELl(y,0);
y2=ELl(y,1);
if(k==NULL) { // create dict
if(0 && SCALAR(y1)) k=ALLOC_LIKE_SZ(y1, 4);
else k=xl0();
v=xl0(); xref(k); xref(v);
EL(x,VP,0)=k; EL(x,VP,1)=v;
x->n=2; i=-1; // not found so insert below
} else i=_find1(k,y1);
if(i==-1) {
xref(y1); xref(y2); EL(x,VP,0)=append(k,y1); EL(x,VP,1)=append(v,y2);
} else {
xref(y2); ELl(v,i)=y2;
}
return x;
}
if(CONTAINER(x)) {
xref(y);
x=XREALLOC(x,x->n+1);
EL(x,VP,x->n)=y; x->n++;
} else {
// XRAY_log("append disaster xn %d xc %d xi %d xsz %d yn %d yc %d yi %d ysz %d\n",x->n,x->cap,x->itemsz,x->sz,y->n,y->cap,y->itemsz,y->sz);
// XRAY_emit(info(x));XRAY_emit(x);XRAY_emit(info(y));XRAY_emit(y);
// dest = BUF(x) + (x->n*x->itemsz);
x=XREALLOC(x,x->n + y->n);
memmove(ELsz(x,x->itemsz,x->n),BUF(y),y->n*y->itemsz);
x->n+=y->n;
}
return x;
}
VP appendfree(VP x,VP y) {
append(x,y); xfree(y); return x;
}
VP upsert(VP x,VP y) {
if(_find1(x,y)==-1) append(x,y); return x;
}
int _upsertidx(VP x,VP y) {
int idx = _find1(x,y);
if(idx>-1) return idx;
append(x,y); return x->n-1;
}
VP amend_dict_dict(VP x,VP y) {
// NB. amend supports callbacks as values but we
// arent going to do that here - possibly needs rethink
VP k=KEYS(y), v=VALS(y);
int i=0, kn=k->n, vn=v->n;
for(; i<kn; i++) x=assign(x,ELl(k,i),ELl(v,i));
return x;
}
VP amend(VP x,VP y) {
XRAY_log("amend\n");XRAY_emit(x);XRAY_emit(y);
// TODO amend should create a new structure rather than modifying one in-place
if(!SIMPLE(x) && !CONTAINER(x)) return EXC(Tt(type),"amend x must be a simple or container type",x,y);
if(DICT(x) && DICT(y)) return amend_dict_dict(x,y);
if(!CONTAINER(y) || y->n!=2) return EXC(Tt(type),"amend y should be [indices,replacements]",x,y);
VP idx=ELl(y,0), val=ELl(y,1), acc=ALLOC_LIKE_SZ(x, idx->n), idxv, tmp=0; int i,fail=-1;
if(CALLABLE(idx)) {
idx=matcheasy(x,idx);
RETURN_IF_EXC(idx);
idx=condense(idx);
}
for(i=0;i<idx->n;i++) {
idxv=apply_simple_(idx,i); // TODO really need a fast 0 alloc version of apply(simple,int)!
if(UNLIKELY(CALLABLE(val))) {
XRAY_log("amend calling val cb\n");XRAY_emit(val);
XRAY_in();
tmp=apply(val,apply(x,idxv));
XRAY_out();
} else {
// handle the case of assigning one vector (ie a string) to a given index
// or the case of assigning many indices to one value
if (SCALAR(idx) || SCALAR(val)) tmp=xref(val);
else { XRAY_in(); tmp=apply_simple_(val,i); XRAY_out(); }
}
RETURN_IF_EXC(tmp);
if(IS_EXC(tmp)) { xfree(idxv); return tmp; }
if(UNLIKELY(!CONTAINER(x) && tmp->t!=x->t)) {
if(NUM(x) && NUM(tmp)) tmp=make(tmp,x);
else return EXC(Tt(value),"amend value type does not match x",x,tmp);
}
assign(x,idxv,tmp);
xfree(idxv); xfree(tmp);
}
XRAY_log("amend returning\n");XRAY_emit(x);
return x;
}
VP assign_table(VP x,VP k,VP val) {
if(NUMSTRICT(k)) return EXC(Tt(nyi),"table numeric assign not yet implemented",x,k);
XRAY_log("assign_table\n");XRAY_emit(x);XRAY_emit(k);XRAY_emit(val);
int i=TABLE_col_num_for_name(x,k);
int nr=TABLE_nrows(x);
if(LEN(val)!=nr) return EXC(Tt(value),"table assign value length doesnt match table rows",x,val);
VP oldcol=TABLE_col(x,i);
TABLE_col(x,i)=clone(val);
xfree(oldcol);
return x;
}
inline VP assign(VP x,VP k,VP val) {
// XRAY_log("assign\n");XRAY_emit(x);XRAY_emit(k);XRAY_emit(val);
if (LIST(k) && k->n) {
int i=0;VP res=x;
if(IS_t(LIST_item(k,0)) && AS_t(LIST_item(k,0),0)==TINULL) {
// .name - try to find a matching key somewhere up the stack
// and update it with xreplace
XRAY_log("assign uplevel\n",i);
VP newk=LIST_item(k,1), tmp=resolvekey(x,newk,1);
if(!IS_EXC(tmp)) { XRAY_log("assign uplevel found ctx\n"); XRAY_emit(tmp); return xreplace(tmp,val); }
else { XRAY_log("assign uplevel could not set\n"); return EXC(Tt(assign),"assign could not set uplevel",x,k); }
}
for(;i<k->n-1;i++) {
// XRAY_log("assign at depth %d\n",i);
ARG_MUTATING(x);
res=apply(res,ELl(k,i));
XRAY_emit(res);
if(UNLIKELY(IS_EXC(res)) || res->n==0) return res;
}
// XRAY_log("assign at depth setting\n");
assign(res,ELl(k,k->n-1),val);
return res; // TODO: should this return the inner context affected? seems wrong
}
if(TABLE(x)) return assign_table(x,k,val);
else if(DICT(x)) {
xref(k); xref(val); return append(x,xln(2,k,val));
} else if(SIMPLE(x) && NUM(k)) {
// XRAY_log("assign");XRAY_emit(x);XRAY_emit(k);XRAY_emit(val);
if(val->n == 0) return EXC(Tt(value),"assignment with empty values not supported on simple types",x,val);
if(x->t != val->t) return EXC(Tt(type),"assign value and target types don't match",x,val);
int typerr=-1, i=NUM_val(k);
ARG_MUTATING(x);
if (i>=x->n) { XREALLOC(x,i+1); x->n=i+1; }
VARY_EACHRIGHT_NOFLOAT(x,k,({
EL(x,typeof(_x),_y) = EL(val,typeof(_x),_y%val->n); // TODO assign should create new return value
}),typerr);
// XRAY_log("assign num returning");XRAY_emit(x);
return x;
} else if(LIST(x) && NUM(k)) {
int i=NUM_val(k);
ARG_MUTATING(x);
if(i>=x->n) { XREALLOC(x,i+1); x->n=i+1; }
if(i<x->n && ELl(x,i)) xfree(ELl(x,i)); // if we're assigning over something that exists in a container, free it
xref(val);
EL(x,VP,i)=val;
return x;
}
return EXC(Tt(type),"assign: bad types",x,0);
}
VP assigns(VP x,const char* key,VP val) {
return assign(x,xfroms(key),val);
}
VP behead(const VP x) {
// XRAY_log("behead\n");XRAY_emit(x);
CLASS_dispatch(NULL, behead, x, NULL);
return drop_(x,1);
}
VP capacity(VP x) {
return xin(1,x->cap);
}
VP catenate_table(VP table, VP row) {
XRAY_log("catenate_table\n"); XRAY_emit(table); XRAY_emit(row);
// XXX i must be something something wrong here
int trows, tcols;
if(table==NULL || LEN(table)==0 || LEN(KEYS(table))==0) {
XRAY_log("table seems to be blank; creating table from this row");
if(DICT(row)) return make_table(MUTATE_CLONE(KEYS(row)),MUTATE_CLONE(VALS(row)));
else return EXC(Tt(value),"can't catenate an empty table with that",table,row);
} else {
trows=TABLE_nrows(table); tcols=TABLE_ncols(table);
}
if(TABLE(row)) {
VP tmp; int i;
XRAY_log("catenating table\n");
for(i=0; i<trows; i++) {
tmp=table_row_dict_(row, i);
ARG_MUTATING(table);
table=catenate_table(MUTATE_CLONE(table),tmp);
xfree(tmp);
}
return table;
}
if(DICT(row)) {
VP rk=KEYS(row);
ASSERT(LIST(KEYS(row))&&LIST(VALS(row)),"catenate_table: row keys or vals not list");
VP fullrow;
if(tcols != 0 && !_equal(rk,KEYS(table))) { // mismatching columns on existing table!
XRAY_log("row keys/table keys mismatch, loading old row defaults\n");
VP lastrow=table_row_dict_(table,trows-1);
fullrow=catenate(lastrow,row);
XRAY_emit(fullrow);
} else fullrow=MUTATE_CLONE(row);
rk=KEYS(fullrow);
VP rv=VALS(fullrow), rktmp, rvtmp, vec; int i=0;
for(; i<rk->n; i++) {
rktmp=ELl(rk,i); rvtmp=ELl(rv,i);
XRAY_log("catenate col\n");
XRAY_emit(rktmp);
XRAY_emit(rvtmp);
XRAY_emit(KEYS(table));
XRAY_emit(VALS(table));
XRAY_emit(table);
vec=TABLE_col_named(table,rktmp);
XRAY_log("vec %p\n",vec);XRAY_emit(vec);
ARG_MUTATING(table);
if(vec==NULL) {
// NB. unknown columns are added, but old rows
// will get the same value as this one!
KEYS(table)=append(KEYS(table),rktmp);
VALS(table)=append(VALS(table),take_(rvtmp,trows+1));
XRAY_log("new keys and vals:\n");
XRAY_emit(KEYS(table));
XRAY_emit(VALS(table));
} else {
vec=append(vec,rvtmp);
XRAY_log("new vec\n");
XRAY_emit(vec);
}
}
return table;
} else if (LIST(row)) {
int i=0;
row=MUTATE_CLONE(row);
table=MUTATE_CLONE(table);
// this could either be a single row, i.e., a list comprised of simple types,
// or a list of lists, meaning a list of individual lists of simple types. we'll
// check if the first member is a list to determine which we think it is.
if(LIST_of_lists(row) && LEN(LIST_first(row)) == tcols) {
XRAY_log("LOL\n");XRAY_emit(row);
// TODO probably important not to recurse here
for(; i<row->n; i++) table=catenate_table(table,MUTATE_CLONE(LIST_item(row,i)));
return table;
} else {
VP colval;
if(row->n != tcols) return EXC(Tt(value),"table definition doesn't match list",table,row);
if(LEN(VALS(table))==0) { // we have not yet setup the values records
for(i=0; i<LEN(row); i++) {
colval=LIST_item(row,i);
xref(colval);
// if any of the values of this dictionary are not scalar, we'll need
// to make that column into a general list, or indexing row values will
// become very confusing indeed
if(!SCALAR(colval)) EL(row,VP,i)=xl(colval);
// EL(row,VP,i)=xl(colval);
}
VALS(table) = xref(row);
} else {
for(; i<row->n; i++) {
TABLE_col(table,i)=append(TABLE_col(table,i),ELl(row,i));
}
}
return table;
}
}
return EXC(Tt(type), "tables can't be joined with that type",table,row);
}
VP catenate(VP x,VP y) {
VP res=0;
XRAY_log("catenate\n");XRAY_emit(x);XRAY_emit(y);
int n = x->n + y->n;
if(!CONTAINER(x) && x->tag==0 && x->t==y->t) {
XRAY_log("catenate2 - like simple objects\n");
res=ALLOC_LIKE_SZ(x, n);
appendbuf(res, BUF(x), x->n); appendbuf(res, BUF(y), y->n);
} else {
CLASS_dispatch(NULL, catenate, x, y);
XRAY_log("catenate3 - container as x, or unlike objects\n");
if(TABLE(x)) { return catenate_table(MUTATE_CLONE(x),y); }
if(DICT(x)) { return dict(MUTATE_CLONE(x),y); }
else if(LIST(x) && LEN(x)==0) { return xl(y); }
else if(LIST(x)) { res=append(MUTATE_CLONE(x),y); }
else {
XRAY_log("catenate4 - create 2-item list with both items in it\n");
res=xlsz(2);
res=append(res,x);
res=append(res,y);
}
}
//res=list2vec(res);
XRAY_log("catenate result");XRAY_emit(res);
return res;
}
int _contains(VP x, VP y) {
return _find1(x,y)==-1 ? 0 : 1;
}
VP contains(VP x, VP y) {
return xi(_contains(x,y));
}
VP condense(VP x) {
// equivalent to k's & / where - condenses non-zeroes into their positions (ugh english)
// always returns an int vector for now; we generally rely on ints too much for this
int typerr=-1; int j; VP acc=xi0();
// XRAY_log("condense\n");XRAY_emit(x);
VARY_EACH(x,({ if(_x) { j=_i; FOR(0,_x,appendbuf(acc,(buf_t)&j,1)); } }),typerr);
// XRAY_log("condense returning\n");XRAY_emit(acc);
return acc;
}
VP curtail(VP x) {
// XRAY_log("curtail\n");XRAY_emit(x);
return drop_(x,-1);
}
VP deal(VP range, VP amt) {
XRAY_log("deal\n");XRAY_emit(range);XRAY_emit(amt);
IF_EXC(!LIST(range) && !NUM(range),Tt(type),"deal: left arg must be numeric", range, amt);
IF_EXC(!IS_i(amt) || !SCALAR(amt),Tt(type),"deal: single right arg must be int", range, amt);
int typerr=-1;
if(LIST(range)) {
int i, rn=range->n, amtt=NUM_val(amt);
VP acc=xlsz(amtt);
for(i=0; i<amtt; i++) acc=append(acc,ELl(range,rand()%rn));
return acc;
} else {
VP acc=NULL;
VARY_EL(amt, 0, ({ typeof(_x)amt=_x; acc=ALLOC_LIKE_SZ(range,_x); // TODO rethink deal in terms of more types
VARY_EL_NOFLOAT(range, 0, ({
FOR(0, amt, ({ if(_x==0)_x=1; EL(acc, typeof(_x), _i)=rand()%_x; }));
acc->n=amt;
}), typerr);}), typerr);
return acc;
}
}
VP del_list_(VP x,int i) {
if(!LIST(x) || i >= LEN(x)) return x;
ARG_MUTATING(x);
if(ELl(x,i)!=NULL) xfree(ELl(x,i));
memmove(ELi(x,i),ELi(x,i+1),(sizeof(VP)*(LEN(x)-i-1)));
x->n--;
return x;
}
VP del_dict(VP x,VP y) {
int i=_find1(x,y);
if(i==-1) return x;
KEYS(x)=del_list_(KEYS(x),i);
VALS(x)=del_list_(VALS(x),i);
return x;
}
VP del_ctx(VP x,VP y) {
XRAY_log("del_ctx\n");XRAY_emit(x);XRAY_emit(y);
KEYS(x)=del_dict(KEYS(x),y);
XRAY_log("del_ctx returning\n");XRAY_emit(x);
return x;
}
VP del(VP x,VP y) {
XRAY_log("del\n");XRAY_emit(x);XRAY_emit(y);
if(IS_EXC(x) || IS_EXC(y)) return x;
CLASS_dispatch(NULL, del, x, y);
if(IS_d(x) && IS_t(y)) return del_dict(x,y);
if(IS_x(x) && IS_t(y)) return del_ctx(x,y);
if(IS_l(x) && NUM(y) && !IS_c(y)) return del_list_(x,NUM_val(y));
return EXC(Tt(nyi),"del not yet implemented for this type",x,y);
}
VP dict(VP x,VP y) {
XRAY_log("dict\n");XRAY_emit(x);XRAY_emit(y);
if(DICT(x)) {
ARG_MUTATING(x);
if(DICT(y)) {
ASSERT(LIST(KEYS(x)) && LIST(VALS(x)),"dict() x keys or vals not list");
int n = KEYS(y)->n;
FOR(0,n,({ assign(x,DICT_key_n(y,_i),DICT_val_n(y,_i)); }));
return x;
}
if(KEYS(x)->n > VALS(x)->n) { // dangling dictionary detection
append(VALS(x),y);
} else {
if(LIST(y) && y->n==2) {
append(KEYS(x),first(y)); append(VALS(x),last(y));
} else {
append(KEYS(x),y);
}
}
XRAY_log("dict already dict returning\n");XRAY_emit(x);
return x;
} else {
if(x->n > 1 && LIST_of_lists(y)) return make_table(x,y);
if(x->n > 1 && x->n != y->n) return EXC(Tt(value),"can't create dict from unlike vectors",x,y);
if(LIST(x) && LIST(y) && LEN(x)==0 && LEN(y)==0) {
XRAY_log("dict empty dict\n");
return xdn(2,x,y);
}
VP d=xd0();
if(LIKELY(SCALAR(x))) {
d=assign(d,DISCLOSE(x),DISCLOSE(y));
} else {
int i;VP tmp1,tmp2;
for(i=0;i<x->n;i++) {
tmp1=apply_simple_(x, i);
tmp2=apply_simple_(y, i);
d=assign(d,tmp1,tmp2);
}
}
d->n=2;
XRAY_log("dict new returning\n");XRAY_emit(d);
return d;
}
}
VP drop_(const VP x,int i) {
VP res;
int st, end;
if(!x || x->n==0) return x;
if(i<0) { st = 0; end=x->n+i; }
else { st = i; end=x->n; }
// XRAY_log("drop_(,%d) %d %d %d\n", i, x->n, st, end); XRAY_emit(x);
res=ALLOC_LIKE_SZ(x,end-st);
// XRAY_emit(info(res));
if(end-st > 0) {
appendbuf(res, ELi(x,st), end-st);
if(LIST(x)) for(i=st;i<end;i++) {
xref(ELl(res,i));
}
}
XRAY_log("drop_ result\n"); XRAY_emit(res);
return res;
}
VP drop(const VP x,const VP y) {
CLASS_dispatch(NULL, drop, x, y);
VP res=0;
int typerr=-1;
// XRAY_log("drop args\n"); XRAY_emit(x); XRAY_emit(y);
IF_RET(!NUM(y) || !SCALAR(y), EXC(Tt(type),"drop y arg must be single numeric",x,y));
if(TABLE(x)) return each(VALS(x),proj(2,x2(&drop),0,y));
VARY_EL(y, 0, ({ return drop_(x,_x); }), typerr);
return res;
}
VP emit(const VP x,const VP tag) {
VP pair=xln(2,tag,x);
XRAY_emit(pair);
show(pair);
xfree(pair);
return x;
}
VP enlist(const VP x) { // if x isnt a list, return [x]. else x.
if(x==0) return xl0();
if(LIST(x))return x;
return xln(1,x);
}
VP except(const VP x,const VP y) {
XRAY_log("except\n");XRAY_emit(x);XRAY_emit(y);
CLASS_dispatch(NULL, except, x, y);
return wherepred_(x,y,1);
}
VP extract(const VP data,const VP parts) {
if(IS_EXC(data)) return data;
if(IS_EXC(parts)) return parts;
if(!LIST(data)) { return EXC(Tt(type),"extract x must be list",data,parts); }
if(XXL_CUR_CTX==NULL) return EXC(Tt(context),"extract could not find current context",data,parts);
int pl=MIN(LEN(data),LEN(parts)), i=0, cursor=0;
VP res=xlsz(5); // arb
VP acc=xd0(), px, dx;
for(i=0; i<pl; i++) {
px=apply_simple_(parts,i);
dx=LIST_item(data,cursor);
if(CALLABLE(px)) {
VP tmp=apply(px,dx);
res=append(res,tmp);
xfree(tmp);
} else if(IS_t(px) && AS_t(px,0)!=TINULL) {
acc=assign(acc,px,dx);
}
xfree(px); // apply_simple_ always allocs
cursor++;
}
for(; cursor<LEN(data); cursor++) {
res=append(res,LIST_item(data, cursor));
}
return xln(2,acc,res);
}
VP extractas(const VP data,const VP parts) {
VP res=extract(data,parts);
VP vals=ELl(res,0), rest=ELl(res,1);
int kl=LEN(KEYS(vals)), i;
for(i=0; i<kl; i++) {
set_is(DICT_key_n(vals,i),DICT_val_n(vals,i));
}
xref(rest);
xfree(vals);
xfree(res);
return rest;
}
int _findbuf(const VP x, const buf_t y) { // returns index or -1 on not found
// XRAY_log("findbuf\n");XRAY_emit(x);
if(LISTDICT(x)) { ITERV(x,{
IF_RET(_findbuf(ELl(x,_i),y)!=-1,_i);
}); } else {
ITERV(x,{ IF_RET(memcmp(ELi(x,_i),y,x->itemsz)==0,_i); });
}
return -1;
}
inline int _find1(const VP x, const VP y) { // returns index or -1 on not found
// probably the most common, core call in the code. worth trying to optimize.
// XRAY_log("_find1\n",x,y); XRAY_emit(x); XRAY_emit(y);
ASSERT(x && (LISTDICT(x) || x->t==y->t), "_find1(): x must be list, or types must match");
VP scan;
if(UNLIKELY(DICT(x))) scan=KEYS(x);
else scan=x;
int sn=LEN(scan), yn=LEN(y), i;
if(LIST(scan)) {
int yt=y->t; VP item;
for(i=0; i<sn; i++) {
item=ELl(scan,i);
if(item && ( (item->t == yt && item->n == yn) ||
(LIST(item) && LIST_item(item,0) && LIST_item(item,0)->t == yt) ))
if (_equal(item,y)==1) return i;
}
return -1;
} else {
if(sn<yn) return -1;
for(i=0; i<sn-(yn-1); i++) {
if(memcmp(ELi(x,i), ELi(y,0), yn * x->itemsz)==0)
return i;
}
}
return -1; // The code of this function reminds me of Armenia, or some war torn place
}
VP find1(const VP x, const VP y) {
if(!x || !(LISTDICT(x) || x->t==y->t))
return EXC(Tt(type),"find x must be list, or types must match",x,y);
return xi(_find1(x,y));
}
VP first(const VP x) {
CLASS_dispatch(NULL, first, x, NULL);
VP i,r;
if(DICT(x)) { return EXC(Tt(type),"dict first/head doesn't make sense",x,0); }
if(LIST(x)) { return xref(ELl(x,0)); }
else return apply_simple_(x,0);
}
int _flat(const VP x) { // returns 1 if vector, or a list composed of vectors (and not other lists)
// XRAY_log("flat\n");XRAY_emit(x);
if(!CONTAINER(x)) return 1;
else return 0; // lists are never flat
}
VP flatten0(VP x, int cont) {
int i,t=-1;VP item,res=NULL;
XRAY_log("flatten\n");XRAY_emit(x);
if(!LIST(x))return x;
if(x->n) {
for(i=0;i<x->n;i++) {
item=ELl(x,i);
if(item==NULL) continue;
if(LIST(item) && cont) item=flatten0(item,cont-1);
if(!res) {
t=item->t; res=ALLOC_LIKE(item);
} else if(item->t!=t) {
XRAY_log("gave up on");XRAY_emit(item);
xfree(res); return x;
}
res=append(res,item);
}
XRAY_log("flatten returning\n");XRAY_emit(res);
return res;
} else return xl0();
}
VP flatten(VP x) {
CLASS_dispatch(NULL, flat, x, NULL);
return flatten0(x, 1);
}
VP from(const VP x,const VP y) {
if(IS_EXC(x) || IS_EXC(y)) return EXC(Tt(value),"from can't use those values",x,y);
CLASS_dispatch(NULL, from, y, x);
if(CALLABLE(x)) return wherepred_(y,x,0);
if(CALLABLE(y) && _arity(y)==2 && LIST(x) && LEN(x)==2) return apply2(y,ELl(x,0),ELl(x,1));
return apply(y,x);
}
VP identity(VP x) {
return x;
}
VP itemsz(VP x) {
return xi(x->itemsz);
}
VP info(VP x) {
CLASS_dispatch(NULL, info, x, NULL);
VP res; type_info_t t;
t=typeinfo(x->t);
res=xd0();
res=assign(res,Tt(typenum),xi(x->t));
res=assign(res,Tt(type),xfroms(t.name));
res=assign(res,Tt(rc),xi(x->rc));
res=assign(res,Tt(len),len(x));
res=assign(res,Tt(capacity),capacity(x));
res=assign(res,Tt(itemsz),itemsz(x));
res=assign(res,Tt(alloced),xi(x->alloc));
res=assign(res,Tt(baseptr),xj((long long)BUF(x)));
res=assign(res,Tt(dataptr),xj((long long)BUF(x)));
if(DICT(x)) {
if(KEYS(x)) res=assign(res,Tt(keyinfo),info(KEYS(x)));
if(VALS(x)) res=assign(res,Tt(valinfo),info(VALS(x)));
}
if(TABLE(x)) {
res=assign(res,Tt(keyinfo),info(KEYS(x)));
res=assign(res,Tt(valinfo),each(VALS(x),x1(&info)));
}
return res;
}
VP join(const VP list,const VP sep) {
VP acc=NULL,x; int ln=LEN(list), i;
if(IS_EXC(list) || ln==0) return list;
if(SCALAR(list)) return apply_simple_(list,0);
CLASS_dispatch(NULL, join, list, sep);
for(i=0;i<ln-1;i++) {
x=apply_simple_(list,i);
if(!acc) acc=ALLOC_LIKE(x);
acc=append(acc,x);
acc=append(acc,sep);
xfree(x);
}
x=apply_simple_(list,i);
acc=append(acc,x);
xfree(x);
return acc;
}
VP last(const VP x) {
CLASS_dispatch(NULL, last, x, NULL);
VP i,r;
if(DICT(x)) { return EXC(Tt(type),"dict last/tail doesn't make sense",x,0); }
if(LIST(x)) { return xref(ELl(x,LEN(x)-1)); }
else return apply_simple_(x,_len(x)-1);
}
int _len(VP x) {
int n = LEN(x);
if(TABLE(x)) n=TABLE_nrows(x);
if(DICT(x)) {
n=KEYS(x) ? LEN(KEYS(x)) : 0;
}
return n;
}
VP len(VP x) {
CLASS_dispatch(NULL, len, x, NULL);
return xi(_len(x));
}
VP list(const VP x) { // convert x to general list
if(x==0) return xl0();
if(LIST(x))return x;
return split(x,xi0());
}
VP make_table(VP keys,VP vals) {
VP res, newvals; int i;
XRAY_log("make_table\n");XRAY_emit(keys);XRAY_emit(vals);
// approach: create empty table, then apply all these row values to it.
// catenate_table already handles lists-of-lists correctly.
res=xasz(2);
EL(res,VP,0)=MUTATE_CLONE(keys);
int sz=vals && LEN(vals) ? LEN(ELl(vals,0)) : 0;
EL(res,VP,1)=xlsz(sz);
res->n=2;
return catenate_table(res, MUTATE_CLONE(vals));
}
VP make_many(VP x,VP y) {
int i, j;
int xn=LEN(x), yn=LEN(y);
VP chars=xfroms("ist");
VP funs=xln(3,
proj(2,&base,0,xi(10)),
proj(1,&str,0,0),
proj(2,&make,0,Tt(tag))
);
VP d=dict(chars,funs), row=xlsz(yn);
for(j=0; j<yn; j++) {
char ch=AS_c(y,j);
if(ch=='_') continue;
VP tmpc=xc(ch); VP fun=DICT_find(d,tmpc); xfree(tmpc);
VP item=apply_simple_(x,j);
if(fun!=NULL) { VP newitem=apply(fun, item); if (newitem!=item) { xfree(item); item=newitem; } }
row=append(row,item); xfree(item);
}
return row;
}
VP make(VP x, VP y) {
// TODO make() should short cut matching kind casts
// TODO make() should be smarter about lists in x
XRAY_log("make\n");XRAY_emit(x);XRAY_emit(y);
if(IS_EXC(x)) return x;
if(IS_EXC(y)) return y;
VP res=0; type_t typenum=-1; tag_t typetag=-1;
// right arg is tag naming a type, use that.. otherwise use y's type
if(IS_t(y)) {
if(LEN(y)==0) typetag=TINULL;
else typetag=AS_t(y,0);
} else typenum=y->t;
// convenience mode: ["123","abc","xyz"] $ "ist" -> [123,"abc",'xyz]
if(LIST(x) && IS_c(y)) return make_many(x,y);
if(IS_c(x) && (typetag==TINULL || typetag==Ti(tag))) {
return xt(_tagnum(x));
}
if(typetag==Ti(table)) {
if(!DICT(x)) return EXC(Tt(type), "can only create table from dict", x, y);
return make_table(KEYS(x),VALS(x));
}
#include"cast.h"
// XRAY_emitRAW(buf,BUFSZ);
if(res) return res;
if(typetag!=-1) {
ARG_MUTATING(x); x->tag=typetag;
CLASS_dispatch(NULL, make, x, NULL);
}
return x;
}
VP pin(VP x, VP y) {
// TODO cast() should short cut matching kind casts
XRAY_log("pin\n");XRAY_emit(x);XRAY_emit(y);
tag_t newt = 0;
if(IS_t(x))
newt=AS_t(x,0);
else if(x->tag != 0)
newt=x->tag;
if (newt) {
/*
if (newt==Ti(Pointer)) {
VP ret=NULL;
if (sizeof(void*)==4 && IS_i(y)) { ret=(VP)AS_i(y,0); }
if (sizeof(void*)==8 && IS_j(y)) { ret=(VP)AS_j(y,0); }
xref(ret); return ret;
}
*/
ARG_MUTATING(y);
y->tag=newt;
CLASS_dispatch(NULL, make, y, NULL);
return y;
} else
return EXC(Tt(type),"pin x arg should be tag name or tagged value",x,y);
}
VP reverse(const VP x) {
CLASS_dispatch(NULL, reverse, x, NULL);
if(!(SIMPLE(x)||CONTAINER(x))) return EXC(Tt(type),"reverse arg must be simple or container",x,NULL);
int i,typerr=-1; VP acc=ALLOC_LIKE(x);
for(i=x->n-1;i>=0;i--) appendbuf(acc,ELi(x,i),1);
return acc;
}
VP shift_(const VP x,int i) {
// XRAY_log("shift_ %d\n",i);XRAY_emit(x);
int n=x->n;
if(i<0) return catenate(take_(x,i%n),drop_(x,i%n));
else return catenate(drop_(x,i%n),take_(x,i%n));
}
VP shift(const VP x,const VP y) {
XRAY_log("shift\n");XRAY_emit(x);XRAY_emit(y);
if(!SIMPLE(x)) return EXC(Tt(type),"shr x must be a simple type",x,y);
if(!NUM(y)) return EXC(Tt(type),"shr y must be numeric",x,y);
int typerr=-1;
VARY_EL(y,0,({return shift_(x,_x);}),typerr);
return NULL;
}
VP show(const VP x) {
char* p;
XRAY_log("show\n");XRAY_emit(x);
if(x==NULL) { printf("null\n"); return x; }
if(IS_c(x)) p=sfromxA(x);
else p=reprA(x);
printf("%s\n",p); free(p);
return x;
}
VP xsplice(const VP x,const VP idx,const VP replace) {
int i, first=AS_i(idx,0), last=first+idx->n;
VP acc;
XRAY_log("xsplice (%d len) %d..%d",x->n, first,last);XRAY_emit(x);XRAY_emit(idx);XRAY_emit(replace);
if(first==0 && last==x->n) return replace;
acc=xl0();
if(LIST(x)) {
for(i=0;i<first;i++)
acc=append(acc,ELl(x,i));
append(acc,replace);
for(i=last;i<x->n;i++)
acc=append(acc,ELl(x,i));
} else {
if(first > 0) acc=append(acc, take_(x, first));
acc=append(acc, replace);
if (last < x->n) acc=append(acc, drop_(x, last));
XRAY_log("xsplice calling over\n");XRAY_emit(acc);
return over(acc, x2(&catenate));
}
XRAY_log("xsplice returning\n"); XRAY_emit(acc);
return acc;
}
VP split(const VP x,const VP tok) {
XRAY_log("split\n");XRAY_emit(x);XRAY_emit(tok);
int typerr=-1;
if(IS_EXC(x)) return x;
CLASS_dispatch(NULL, split, x, tok);
// special case for empty or null tok.. split vector into list
if(tok->n==0) {
VP tmp, tmp2;
tmp=xl0();
if(LIST(x)) return x;
VARY_EACHLIST(x,({
// XRAY_log("in split vary_each %c\n",_x);
tmp2=ALLOC_LIKE_SZ(x, 1);
tmp2=appendbuf(tmp2,(buf_t)&_x,1);
tmp=append(tmp,tmp2);
}),typerr);
IF_RET(typerr>-1, EXC(Tt(type),"can't split that type", x, tok));
XRAY_log("split returning\n");XRAY_emit(tmp);
return tmp;
} else if(x->t == tok->t) {
int j, i=0, last=0, tokn=tok->n;
VP rest=x, acc=0;
for(; i<x->n; i++) {
for(j=0; j<tokn; j++) {
if(!_equalm(x,i+j,tok,j)) break;
if(j==tokn-1) { // we made it to the end of the entire token!
if(!acc)acc=xlsz(x->n/1);
acc=append(acc,take_(rest,i-last));
rest=drop_(rest,i+(tokn)-last);
last=i+(tokn);
}
}
}
if(acc){ acc=append(acc,rest); return acc; }
else return x;
} else if(LIST(tok) && LIST_item(tok,0)->t == x->t) {
// "/add?item=mongoose&sex=male" split "?" :: {split "&" :: {split "="}}
// "/add?item=mongoose&sex=male" split ["?", "&", "="]
VP res, newres, item; int i;
if(!LIST(x)) res=xl(x);
else res=x;
for(i=0; i<LEN(tok); i++) {
item=LIST_item(tok,i);
newres=each(res,proj(2,&split,0,item));
XRAY_log("split-deep loop\n");XRAY_emit(newres);
if(newres!=res) xfree(res);
if(IS_EXC(newres)) { return newres; }
if(ENLISTED(newres)) { res=xref(LIST_item(newres,0)); xfree(newres); }
else res=newres;
}
return res;
} else
return EXC(Tt(nyi),"split with that type of data not yet implemented",x,tok);
}
VP take_(const VP x, const int i) {
VP res;
int xn=_len(x), st, end, j;
if(!x || xn==0) return x;
if(i<0) { st=ABS((xn+i)%xn); end=ABS(i)+st; } else { st=0; end=i; }
res=ALLOC_LIKE_SZ(x,end-st);
if(LIKELY(SIMPLE(x))) {
FOR(st,end,({ res=appendbuf(res,ELi(x,_i % xn),1); }));
} else {
if(!LIST(x)&&!TABLE(x))
return EXC(Tt(nyi),"take not yet implemented for that type",x,xi(i));
VP tmp;
FOR(st,end,({ tmp=apply_simple_(x,_i%xn); res=append(res,tmp); xfree(tmp); }));
}
XRAY_log("take_ returning\n");XRAY_emit(res);
return res;
}
VP take(const VP x, const VP y) {
int typerr=-1;
CLASS_dispatch(NULL, take, x, y);
size_t st, end; //TODO slice() support more than 32bit indices
XRAY_log("take args\n"); XRAY_emit(x); XRAY_emit(y);
IF_RET(!NUM(y) || !SCALAR(y), EXC(Tt(type),"take y arg must be single numeric",x,y));
VARY_EL(y, 0, ({ return take_(x,_x); }), typerr);
return NULL;
}
VP table_row_list_(VP tbl, int row) {
int tc=TABLE_ncols(tbl), i;
VP lst=xlsz(tc), res;
for(i=0; i<tc; i++) {
res=apply_simple_(TABLE_col(tbl,i), row);
lst=append(lst,DISCLOSE(res));
}
XRAY_log("table_row_list_ #%d returning\n");
XRAY_emit(lst);
return lst;
}
VP table_row_dict_(VP tbl, int row) {
return dict(KEYS(tbl), table_row_list_(tbl, row));
}
VP type(VP x) {
if(x==0 || x->t<0 || x->t>MAX_TYPE) return Tt(null);
if(IS_EXC(x)) return xt(_tagnums("exception"));
type_info_t t=typeinfo(x->t);
return xt(_tagnums(t.name));
}
// APPLICATION, ITERATION AND ADVERBS
VP applyexpr(VP parent, VP code, VP xarg, VP yarg) {
XRAY_log("applyexpr (code %p, xarg %p, yarg %p):\n", code, xarg, yarg);XRAY_emit(code);XRAY_emit(xarg);XRAY_emit(yarg);
// if(!LIST(code))return EXC(Tt(code),"expr code not list",code,xarg);
if(SIMPLE(code) || !LIST(code)) return code;
char ch; int i;
VP left=xarg,item=0,oldleft=0,old_cur_ctx;
tag_t tag, tcom=Ti(comment), texc=Ti(exception), texpr=Ti(expr), tlam=Ti(lambda),
tlistexpr=Ti(listexpr), tname=Ti(name), toper=Ti(oper),
traw=Ti(raw), tstr=Ti(string), tws=Ti(ws);