This repository has been archived by the owner on Aug 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadverbs.go
730 lines (704 loc) · 12.5 KB
/
adverbs.go
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
package goal
import (
"fmt"
"strings"
)
func fold2(ctx *Context, args []V) V {
f := args[1]
if !f.IsFunction() {
if f.IsI() {
return decode(f, args[0])
}
if f.IsF() {
return decode(f, args[0])
}
switch fv := f.bv.(type) {
case S:
return joinStrings(fv, args[0])
case *AB, *AI, *AF:
return decode(f, args[0])
default:
return panicType("F/x", "F", f)
}
}
if f.Rank(ctx) == 1 {
return converge(ctx, f, args[0])
}
x := args[0]
return foldfx(ctx, f, x)
}
func foldfx(ctx *Context, f, x V) V {
if f.kind == valVariadic {
switch f.variadic() {
case vAdd:
return fold2vAdd(x)
case vSubtract:
return fold2vSubtract(x)
case vMultiply:
return fold2vMultiply(x)
case vMax:
return fold2vMax(x)
case vMin:
return fold2vMin(x)
case vJoin:
return fold2vJoin(x)
}
}
switch xv := x.bv.(type) {
case *D:
return foldfx(ctx, f, NewV(xv.values))
case Array:
if xv.Len() == 0 {
switch xv.(type) {
case *AS:
return NewS("")
case *AV:
return x
case *AF:
return NewF(0)
default:
return NewI(0)
}
}
r := xv.VAt(0)
ctx.pushNoRC(V{})
f.IncrRC()
for i := 1; i < xv.Len(); i++ {
ctx.replaceTop(xv.VAt(i))
ctx.push(r)
r = f.applyN(ctx, 2)
}
f.DecrRC()
ctx.drop()
return r
default:
return x
}
}
func joinStrings(sep S, x V) V {
switch xv := x.bv.(type) {
case S:
return x
case *AS:
return NewS(strings.Join([]string(xv.elts), string(sep)))
default:
if x.Len() == 0 {
return NewS("")
}
return panicType("s/S", "S", x)
}
}
const maxConvergeIters = 1_000_000
func converge(ctx *Context, f, x V) V {
if dv, ok := f.bv.(*derivedVerb); ok && dv.Fun == vFold && dv.Arg.kind == valVariadic {
switch dv.Arg.variadic() {
case vJoin:
return convergeJoin(x)
}
}
n := 0
f.IncrRC()
first := x
first.IncrRC()
ctx.push(x)
for {
x.IncrRC()
r := f.applyN(ctx, 1)
x.DecrRC()
if r.IsPanic() {
f.DecrRC()
first.DecrRC()
ctx.drop()
return r
}
if r.Matches(x) || r.Matches(first) {
f.DecrRC()
first.DecrRC()
ctx.drop()
return x
}
ctx.replaceTop(r)
x = r
n++
if n > maxConvergeIters {
f.DecrRC()
first.DecrRC()
ctx.drop()
return panics("f/x : too many iterations")
}
}
}
func fold3(ctx *Context, args []V) V {
f := args[2]
if !f.IsFunction() {
return panicType("x F/y", "F", f)
}
rank := f.Rank(ctx)
if rank == 1 {
return doWhile(ctx, args)
}
if rank == 2 {
return foldxfy(ctx, args[1], f, args[0])
}
return panicRankN("x F/y", "F", rank, len(args)-1)
}
func foldxfy(ctx *Context, x, f, y V) V {
if f.kind == valVariadic {
switch f.variadic() {
case vAdd:
return fold3vAdd(x, y)
case vSubtract:
return fold3vSubtract(x, y)
case vMultiply:
return fold3vMultiply(x, y)
case vMax:
return fold3vMax(x, y)
case vMin:
return fold3vMin(x, y)
case vJoin:
return fold3vJoin(x, y)
}
}
switch yv := y.bv.(type) {
case *D:
return foldxfy(ctx, x, f, NewV(yv.values))
case Array:
r := x
if yv.Len() == 0 {
return r
}
f.IncrRC()
ctx.pushNoRC(V{})
for i := 0; i < yv.Len(); i++ {
ctx.replaceTop(yv.VAt(i))
ctx.push(r)
r = f.applyN(ctx, 2)
if r.IsPanic() {
f.DecrRC()
ctx.drop()
return r
}
}
f.DecrRC()
ctx.drop()
return r
default:
ctx.push(y)
ctx.push(x)
r := f.applyN(ctx, 2)
ctx.drop()
return r
}
}
func getIterLen(args []V) (int, error) {
mlen := -1
for _, x := range args {
switch xv := x.bv.(type) {
case countable:
switch {
case mlen < 0:
mlen = xv.Len()
case mlen != xv.Len():
return mlen, fmt.Errorf("length mismatch (%d vs %d)", mlen, xv.Len())
}
}
}
return mlen, nil
}
func foldN(ctx *Context, args []V) V {
f := args[len(args)-1]
if !f.IsFunction() {
return panicType("f/[x;y;...]", "f", f)
}
n := len(args) - 1
if f.Rank(ctx) != n {
return panicRankN("f/[x;y;...]", "f", f.Rank(ctx), n)
}
mlen, err := getIterLen(args[:len(args)-2])
if err != nil {
return Panicf("f/[x;y;...] : %v", err)
}
if mlen == -1 {
return ctx.ApplyN(f, args[:len(args)-1])
}
x := args[len(args)-2]
if mlen == 0 {
return x
}
f.IncrRC()
ctx.pushNoRC(V{})
r := x
for i := 0; i < mlen; i++ {
ctx.replaceTop(args[0].VAt(i))
for j := 1; j < len(args)-2; j++ {
ctx.push(args[j].VAt(i))
}
ctx.push(r)
r = f.applyN(ctx, n)
if r.IsPanic() {
f.DecrRC()
ctx.drop()
return r
}
}
f.DecrRC()
ctx.drop()
return r
}
func doWhile(ctx *Context, args []V) V {
f := args[2]
x := args[1]
y := args[0]
if x.IsI() {
return doTimes(ctx, x.I(), f, y)
}
if x.IsF() {
if !isI(x.F()) {
return Panicf("n f/y : non-integer n (%g)", x.F())
}
return doTimes(ctx, int64(x.F()), f, y)
}
if x.IsFunction() {
f.IncrRC()
x.IncrRC()
ctx.push(y)
for {
y.IncrRC()
cond := x.applyN(ctx, 1)
y.DecrRC()
if cond.IsPanic() {
x.DecrRC()
f.DecrRC()
ctx.drop()
return cond
}
if !cond.IsTrue() {
x.DecrRC()
f.DecrRC()
ctx.drop()
return y
}
y = f.applyN(ctx, 1)
if y.IsPanic() {
x.DecrRC()
f.DecrRC()
ctx.drop()
return y
}
ctx.replaceTop(y)
}
}
return panicType("x f/y", "x", x)
}
func doTimes(ctx *Context, n int64, f, y V) V {
f.IncrRC()
ctx.push(y)
for i := int64(0); i < n; i++ {
y = f.applyN(ctx, 1)
if y.IsPanic() {
f.DecrRC()
ctx.drop()
return y
}
ctx.replaceTop(y)
}
f.DecrRC()
ctx.drop()
return y
}
func scan2(ctx *Context, f, x V) V {
if !f.IsFunction() {
if f.IsI() {
return encode(f, x)
}
if f.IsF() {
return encode(f, x)
}
switch fv := f.bv.(type) {
case S:
return splitS(fv, x)
case *rx:
return splitRx(fv, x)
case *AB, *AI, *AF:
return encode(f, x)
default:
return panicType("f\\x", "f", f)
}
}
if f.Rank(ctx) != 2 {
return converges(ctx, f, x)
}
return scanfx(ctx, f, x)
}
func scanfx(ctx *Context, f, x V) V {
if f.kind == valVariadic {
switch f.variadic() {
case vAdd:
return scan2vAdd(x)
case vSubtract:
return scan2vSubtract(x)
case vMax:
return scan2vMax(x)
case vMin:
return scan2vMin(x)
}
}
switch xv := x.bv.(type) {
case *D:
return newDictValues(xv.keys, scanfx(ctx, f, NewV(xv.values)))
case Array:
if xv.Len() == 0 {
return x
}
r := make([]V, xv.Len())
next := xv.VAt(0)
r[0] = next
f.IncrRC()
ctx.pushNoRC(V{})
for i := 1; i < xv.Len(); i++ {
ctx.replaceTop(xv.VAt(i))
ctx.push(next)
next = f.applyN(ctx, 2)
if next.IsPanic() {
f.DecrRC()
ctx.drop()
return next
}
next.MarkImmutable()
r[i] = next
}
f.DecrRC()
ctx.drop()
return canonicalVs(r)
default:
return x
}
}
func converges(ctx *Context, f, x V) V {
n := 0
r := []V{}
f.IncrRC()
first := x
ctx.push(x)
for {
x.MarkImmutable()
r = append(r, x)
y := f.applyN(ctx, 1)
if y.IsPanic() {
f.DecrRC()
ctx.drop()
return y
}
if y.Matches(x) || y.Matches(first) {
f.DecrRC()
ctx.drop()
return canonicalVs(r)
}
ctx.replaceTop(y)
x = y
n++
if n > maxConvergeIters {
f.DecrRC()
ctx.drop()
return panics(`f\x : too many iterations`)
}
}
}
func splitS(sep S, x V) V {
r := splitN(-1, sep, x)
if r.IsPanic() {
return ppanic("s/x : x ", r)
}
return r
}
func scan3(ctx *Context, args []V) V {
f := args[2]
if !f.IsFunction() {
switch fv := f.bv.(type) {
case S:
return splitNS(args[1], fv, args[0])
default:
return panicType("x f'y", "f", f)
}
}
if f.Rank(ctx) == 1 {
return doWhiles(ctx, args)
}
y := args[0]
x := args[1]
return scanxfy(ctx, x, f, y)
}
func scanxfy(ctx *Context, x, f, y V) V {
if f.kind == valVariadic {
switch f.variadic() {
case vAdd:
return scan3vAdd(x, y)
case vSubtract:
return scan3vSubtract(x, y)
case vMax:
return scan3vMax(x, y)
case vMin:
return scan3vMin(x, y)
}
}
switch yv := y.bv.(type) {
case *D:
return newDictValues(yv.keys, scanxfy(ctx, x, f, NewV(yv.values)))
case Array:
if yv.Len() == 0 {
return y
}
f.IncrRC()
ctx.pushNoRC(V{})
r := make([]V, yv.Len())
for i := 0; i < yv.Len(); i++ {
ctx.replaceTop(yv.VAt(i))
ctx.push(x)
x = f.applyN(ctx, 2)
if x.IsPanic() {
f.DecrRC()
ctx.drop()
return x
}
x.MarkImmutable()
r[i] = x
}
f.DecrRC()
ctx.drop()
return canonicalVs(r)
default:
ctx.push(y)
ctx.push(x)
r := f.applyN(ctx, 2)
ctx.drop()
return r
}
}
func scanN(ctx *Context, args []V) V {
f := args[len(args)-1]
if !f.IsFunction() {
return panicType(`f[x;y;...]`, "f", f)
}
n := len(args) - 1
if f.Rank(ctx) != n {
return panicRankN(`f\[x;y;...]`, "f", f.Rank(ctx), n)
}
mlen, err := getIterLen(args[:len(args)-2])
if err != nil {
return Panicf("f\\[x;y;...] : %v", err)
}
if mlen == -1 {
return toArray(ctx.ApplyN(f, args[:len(args)-1]))
}
x := args[len(args)-2]
if mlen == 0 {
return protoV(args[len(args)-3])
}
f.IncrRC()
ctx.pushNoRC(V{})
r := make([]V, mlen)
for i := 0; i < mlen; i++ {
ctx.replaceTop(args[0].VAt(i))
for j := 1; j < len(args)-2; j++ {
ctx.push(args[j].VAt(i))
}
ctx.push(x)
x = f.applyN(ctx, n)
if x.IsPanic() {
f.DecrRC()
ctx.drop()
return x
}
x.MarkImmutable()
r[i] = x
}
f.DecrRC()
ctx.drop()
if d, ok := args[len(args)-3].bv.(*D); ok {
return newDictValues(d.keys, canonicalVs(r))
}
return canonicalVs(r)
}
func splitNS(x V, sep S, y V) V {
var n int
if x.IsI() {
n = int(x.I())
} else if x.IsF() {
if !isI(x.F()) {
return Panicf("i s/y : i non-integer (%g)", x.F())
}
n = int(x.F())
} else {
return panicType("i s/y", "i", x)
}
r := splitN(n, sep, y)
if r.IsPanic() {
return ppanic("i s/y : y ", r)
}
return r
}
func doWhiles(ctx *Context, args []V) V {
f := args[2]
x := args[1]
y := args[0]
if x.IsI() {
return dosTimes(ctx, x.I(), f, y)
}
if x.IsF() {
if !isI(x.F()) {
return Panicf("n f\\y : non-integer n (%g)", x.F())
}
return dosTimes(ctx, int64(x.F()), f, y)
}
if x.IsFunction() {
r := []V{y}
f.IncrRC()
x.IncrRC()
ctx.push(y)
for {
cond := x.applyN(ctx, 1)
if cond.IsPanic() {
f.DecrRC()
x.DecrRC()
ctx.drop()
return cond
}
if !cond.IsTrue() {
f.DecrRC()
x.DecrRC()
ctx.drop()
return canonicalVs(r)
}
y = f.applyN(ctx, 1)
if y.IsPanic() {
f.DecrRC()
x.DecrRC()
ctx.drop()
return y
}
y.MarkImmutable()
r = append(r, y)
ctx.replaceTop(y)
}
}
return panicType("x f\\y", "x", x)
}
func dosTimes(ctx *Context, n int64, f, y V) V {
r := make([]V, n+1)
y.MarkImmutable()
r[0] = y
f.IncrRC()
ctx.push(y)
for i := int64(1); i <= n; i++ {
y = f.applyN(ctx, 1)
if y.IsPanic() {
f.DecrRC()
ctx.drop()
return y
}
y.MarkImmutable()
r[i] = y
ctx.replaceTop(y)
}
f.DecrRC()
ctx.drop()
return canonicalVs(r)
}
func each2(ctx *Context, f, x V) V {
if !f.IsFunction() {
return panicType(`f'x`, "f", f)
}
switch xv := x.bv.(type) {
case *D:
return newDictValues(xv.keys, eachfx(ctx, f, xv.values))
case Array:
return eachfx(ctx, f, xv)
default:
return ctx.Apply(f, x)
}
}
func eachfx(ctx *Context, f V, x Array) V {
if f.kind == valVariadic {
switch f.variadic() {
case vShape:
return each2String(ctx, x)
case vTake:
return each2Length(x)
case vMultiply:
return each2First(x)
case vApply:
return each2Type(x)
}
}
r := make([]V, x.Len())
f.IncrRC()
ctx.pushNoRC(V{})
for i := range r {
ctx.replaceTop(x.VAt(i))
next := f.applyN(ctx, 1)
if next.IsPanic() {
f.DecrRC()
ctx.drop()
return next
}
next.MarkImmutable()
r[i] = next
}
f.DecrRC()
ctx.drop()
return canonicalVs(r)
}
func eachN(ctx *Context, args []V) V {
f := args[len(args)-1]
if !f.IsFunction() {
return panicType(`f'[x;y;...]`, "f", f)
}
n := len(args) - 1
if f.Rank(ctx) != n {
return panicRankN(`f'[x;y;...]`, "f", f.Rank(ctx), n)
}
mlen, err := getIterLen(args[:len(args)-1])
if err != nil {
return Panicf("f'[x;y;...] : %v", err)
}
if mlen == -1 {
return ctx.ApplyN(f, args[:len(args)-1])
}
y := args[0]
if n == 2 && f.kind == valVariadic && f.variadic() == vMatch {
return each3Match(args[1], y)
}
r := make([]V, mlen)
f.IncrRC()
ctx.pushNoRC(V{})
for i := range r {
ctx.replaceTop(y.VAt(i))
for j := 1; j < len(args)-1; j++ {
ctx.push(args[j].VAt(i))
}
next := f.applyN(ctx, n)
if next.IsPanic() {
f.DecrRC()
ctx.drop()
return next
}
next.MarkImmutable()
r[i] = next
}
f.DecrRC()
ctx.drop()
if d, ok := args[len(args)-2].bv.(*D); ok {
return newDictValues(d.keys, canonicalVs(r))
}
return canonicalVs(r)
}
func (x V) VAt(i int) V {
switch xv := x.bv.(type) {
case *D:
return xv.values.VAt(i)
case Array:
return xv.VAt(i)
default:
return x
}
}