/
Builtins.cs
777 lines (710 loc) · 28.1 KB
/
Builtins.cs
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
using Niecza;
using System;
using System.Collections.Generic;
public class Builtins {
public static IP6 NominalCheck(string name, DynMetaObject mo, Variable v) {
IP6 r = v.Fetch();
if (!r.mo.HasMRO(mo))
throw new NieczaException("Nominal type check failed for " + name +
" needed " + mo.name + " got " + r.mo.name);
return r;
}
public static void AssignV(Variable lhs, IP6 rhs) {
if (!lhs.islist) {
lhs.Store(rhs);
} else {
Frame n = lhs.Fetch().InvokeMethod(Kernel.GetInferiorRoot(),
"LISTSTORE",
new Variable[2] { lhs, Kernel.NewROScalar(rhs) }, null);
Kernel.RunInferior(n);
}
}
public static string LaxSubstring(string str, int from) {
if (from <= 0)
return str;
if (from >= str.Length)
return "";
return str.Substring(from);
}
public static string LaxSubstring2(string str, int from, int l) {
if (from <= 0) from = 0;
if (from >= str.Length) from = str.Length;
if (l >= str.Length - from) l = str.Length - from;
if (l < 0) l = 0;
return str.Substring(from, l);
}
public static Variable NumericEq(Variable v1, Variable v2) {
IP6 o1 = NominalCheck("$x", Kernel.AnyMO, v1);
IP6 o2 = NominalCheck("$y", Kernel.AnyMO, v2);
if (o1.mo.mro_raw_Numeric.Get(v1) == o2.mo.mro_raw_Numeric.Get(v2)) {
return Kernel.TrueV;
} else {
return Kernel.FalseV;
}
}
public static Variable NumericLt(Variable v1, Variable v2) {
IP6 o1 = NominalCheck("$x", Kernel.AnyMO, v1);
IP6 o2 = NominalCheck("$y", Kernel.AnyMO, v2);
if (o1.mo.mro_raw_Numeric.Get(v1) < o2.mo.mro_raw_Numeric.Get(v2)) {
return Kernel.TrueV;
} else {
return Kernel.FalseV;
}
}
public static Variable NumericNe(Variable v1, Variable v2) {
IP6 o1 = NominalCheck("$x", Kernel.AnyMO, v1);
IP6 o2 = NominalCheck("$y", Kernel.AnyMO, v2);
if (o1.mo.mro_raw_Numeric.Get(v1) != o2.mo.mro_raw_Numeric.Get(v2)) {
return Kernel.TrueV;
} else {
return Kernel.FalseV;
}
}
public static Variable NumericLe(Variable v1, Variable v2) {
IP6 o1 = NominalCheck("$x", Kernel.AnyMO, v1);
IP6 o2 = NominalCheck("$y", Kernel.AnyMO, v2);
if (o1.mo.mro_raw_Numeric.Get(v1) <= o2.mo.mro_raw_Numeric.Get(v2)) {
return Kernel.TrueV;
} else {
return Kernel.FalseV;
}
}
public static Variable NumericGt(Variable v1, Variable v2) {
IP6 o1 = NominalCheck("$x", Kernel.AnyMO, v1);
IP6 o2 = NominalCheck("$y", Kernel.AnyMO, v2);
if (o1.mo.mro_raw_Numeric.Get(v1) > o2.mo.mro_raw_Numeric.Get(v2)) {
return Kernel.TrueV;
} else {
return Kernel.FalseV;
}
}
public static Variable NumericGe(Variable v1, Variable v2) {
IP6 o1 = NominalCheck("$x", Kernel.AnyMO, v1);
IP6 o2 = NominalCheck("$y", Kernel.AnyMO, v2);
if (o1.mo.mro_raw_Numeric.Get(v1) >= o2.mo.mro_raw_Numeric.Get(v2)) {
return Kernel.TrueV;
} else {
return Kernel.FalseV;
}
}
public static Variable StringEq(Variable v1, Variable v2) {
IP6 o1 = NominalCheck("$x", Kernel.AnyMO, v1);
IP6 o2 = NominalCheck("$y", Kernel.AnyMO, v2);
if (o1.mo.mro_raw_Str.Get(v1) == o2.mo.mro_raw_Str.Get(v2)) {
return Kernel.TrueV;
} else {
return Kernel.FalseV;
}
}
public static Variable StringNe(Variable v1, Variable v2) {
IP6 o1 = NominalCheck("$x", Kernel.AnyMO, v1);
IP6 o2 = NominalCheck("$y", Kernel.AnyMO, v2);
if (o1.mo.mro_raw_Str.Get(v1) != o2.mo.mro_raw_Str.Get(v2)) {
return Kernel.TrueV;
} else {
return Kernel.FalseV;
}
}
public static Variable StringLt(Variable v1, Variable v2) {
IP6 o1 = NominalCheck("$x", Kernel.AnyMO, v1);
IP6 o2 = NominalCheck("$y", Kernel.AnyMO, v2);
if (string.CompareOrdinal(o1.mo.mro_raw_Str.Get(v1),
o2.mo.mro_raw_Str.Get(v2)) < 0) {
return Kernel.TrueV;
} else {
return Kernel.FalseV;
}
}
public static Variable StringLe(Variable v1, Variable v2) {
IP6 o1 = NominalCheck("$x", Kernel.AnyMO, v1);
IP6 o2 = NominalCheck("$y", Kernel.AnyMO, v2);
if (string.CompareOrdinal(o1.mo.mro_raw_Str.Get(v1),
o2.mo.mro_raw_Str.Get(v2)) <= 0) {
return Kernel.TrueV;
} else {
return Kernel.FalseV;
}
}
public static Variable StringGe(Variable v1, Variable v2) {
IP6 o1 = NominalCheck("$x", Kernel.AnyMO, v1);
IP6 o2 = NominalCheck("$y", Kernel.AnyMO, v2);
if (string.CompareOrdinal(o1.mo.mro_raw_Str.Get(v1),
o2.mo.mro_raw_Str.Get(v2)) >= 0) {
return Kernel.TrueV;
} else {
return Kernel.FalseV;
}
}
public static Variable StringGt(Variable v1, Variable v2) {
IP6 o1 = NominalCheck("$x", Kernel.AnyMO, v1);
IP6 o2 = NominalCheck("$y", Kernel.AnyMO, v2);
if (string.CompareOrdinal(o1.mo.mro_raw_Str.Get(v1),
o2.mo.mro_raw_Str.Get(v2)) > 0) {
return Kernel.TrueV;
} else {
return Kernel.FalseV;
}
}
public static Variable Substr3(Variable v1, Variable v2, Variable v3) {
IP6 o1 = NominalCheck("$string", Kernel.AnyMO, v1);
IP6 o2 = NominalCheck("$start", Kernel.AnyMO, v2);
IP6 o3 = NominalCheck("$chars", Kernel.AnyMO, v3);
string r1 = o1.mo.mro_raw_Str.Get(v1);
int r2 = (int)o2.mo.mro_raw_Numeric.Get(v2);
int r3 = (int)o3.mo.mro_raw_Numeric.Get(v3);
return Kernel.BoxAnyMO<string>(LaxSubstring2(r1, r2, r3), Kernel.StrMO);
}
public static Variable Plus(Variable v1, Variable v2) {
IP6 o1 = NominalCheck("$x", Kernel.AnyMO, v1);
IP6 o2 = NominalCheck("$y", Kernel.AnyMO, v2);
double r1 = o1.mo.mro_raw_Numeric.Get(v1);
double r2 = o2.mo.mro_raw_Numeric.Get(v2);
return Kernel.BoxAnyMO<double>(r1 + r2, Kernel.NumMO);
}
public static Variable Minus(Variable v1, Variable v2) {
IP6 o1 = NominalCheck("$x", Kernel.AnyMO, v1);
IP6 o2 = NominalCheck("$y", Kernel.AnyMO, v2);
double r1 = o1.mo.mro_raw_Numeric.Get(v1);
double r2 = o2.mo.mro_raw_Numeric.Get(v2);
return Kernel.BoxAnyMO<double>(r1 - r2, Kernel.NumMO);
}
public static Variable Mul(Variable v1, Variable v2) {
IP6 o1 = NominalCheck("$x", Kernel.AnyMO, v1);
IP6 o2 = NominalCheck("$y", Kernel.AnyMO, v2);
double r1 = o1.mo.mro_raw_Numeric.Get(v1);
double r2 = o2.mo.mro_raw_Numeric.Get(v2);
return Kernel.BoxAnyMO<double>(r1 * r2, Kernel.NumMO);
}
public static Variable Divide(Variable v1, Variable v2) {
IP6 o1 = NominalCheck("$x", Kernel.AnyMO, v1);
IP6 o2 = NominalCheck("$y", Kernel.AnyMO, v2);
double r1 = o1.mo.mro_raw_Numeric.Get(v1);
double r2 = o2.mo.mro_raw_Numeric.Get(v2);
return Kernel.BoxAnyMO<double>(r1 / r2, Kernel.NumMO);
}
public static Variable NumAnd(Variable v1, Variable v2) {
IP6 o1 = NominalCheck("$x", Kernel.AnyMO, v1);
IP6 o2 = NominalCheck("$y", Kernel.AnyMO, v2);
int r1 = (int)o1.mo.mro_raw_Numeric.Get(v1);
int r2 = (int)o2.mo.mro_raw_Numeric.Get(v2);
return Kernel.BoxAnyMO<double>((double)(r1 & r2), Kernel.NumMO);
}
public static Variable NumOr(Variable v1, Variable v2) {
IP6 o1 = NominalCheck("$x", Kernel.AnyMO, v1);
IP6 o2 = NominalCheck("$y", Kernel.AnyMO, v2);
int r1 = (int)o1.mo.mro_raw_Numeric.Get(v1);
int r2 = (int)o2.mo.mro_raw_Numeric.Get(v2);
return Kernel.BoxAnyMO<double>((double)(r1 | r2), Kernel.NumMO);
}
public static Variable NumXor(Variable v1, Variable v2) {
IP6 o1 = NominalCheck("$x", Kernel.AnyMO, v1);
IP6 o2 = NominalCheck("$y", Kernel.AnyMO, v2);
int r1 = (int)o1.mo.mro_raw_Numeric.Get(v1);
int r2 = (int)o2.mo.mro_raw_Numeric.Get(v2);
return Kernel.BoxAnyMO<double>((double)(r1 ^ r2), Kernel.NumMO);
}
public static Variable NumLShift(Variable v1, Variable v2) {
IP6 o1 = NominalCheck("$x", Kernel.AnyMO, v1);
IP6 o2 = NominalCheck("$y", Kernel.AnyMO, v2);
int r1 = (int)o1.mo.mro_raw_Numeric.Get(v1);
int r2 = (int)o2.mo.mro_raw_Numeric.Get(v2);
return Kernel.BoxAnyMO<double>((double)(r1 << r2), Kernel.NumMO);
}
public static Variable NumRShift(Variable v1, Variable v2) {
IP6 o1 = NominalCheck("$x", Kernel.AnyMO, v1);
IP6 o2 = NominalCheck("$y", Kernel.AnyMO, v2);
int r1 = (int)o1.mo.mro_raw_Numeric.Get(v1);
int r2 = (int)o2.mo.mro_raw_Numeric.Get(v2);
return Kernel.BoxAnyMO<double>((double)(r1 >> r2), Kernel.NumMO);
}
public static Variable NumCompl(Variable v1) {
IP6 o1 = NominalCheck("$x", Kernel.AnyMO, v1);
int r1 = (int)o1.mo.mro_raw_Numeric.Get(v1);
return Kernel.BoxAnyMO<double>((double)(~r1), Kernel.NumMO);
}
public static Variable PostIncrement(Variable v) {
IP6 o1 = NominalCheck("$x", Kernel.AnyMO, v);
AssignV(v, o1.mo.mro_succ.Get(v));
return Kernel.NewROScalar(o1);
}
public static Variable Not(Variable v) {
IP6 o1 = NominalCheck("$x", Kernel.AnyMO, v);
bool r = o1.mo.mro_raw_Bool.Get(v);
return r ? Kernel.FalseV : Kernel.TrueV;
}
public static Variable Chars(Variable v) {
IP6 o1 = NominalCheck("$x", Kernel.AnyMO, v);
string r = o1.mo.mro_raw_Str.Get(v);
return Kernel.BoxAnyMO((double)r.Length, Kernel.NumMO);
}
public static Variable Negate(Variable v) {
IP6 o1 = NominalCheck("$x", Kernel.AnyMO, v);
double r = o1.mo.mro_raw_Numeric.Get(v);
return Kernel.BoxAnyMO<double>(-r, Kernel.NumMO);
}
public static Variable Ord(Variable v) {
IP6 o1 = NominalCheck("$x", Kernel.AnyMO, v);
string r = o1.mo.mro_raw_Str.Get(v);
// XXX Failure
if (r.Length == 0) return Kernel.NewROScalar(Kernel.AnyP);
return Kernel.BoxAnyMO((double)r[0], Kernel.NumMO);
}
public static Variable Chr(Variable v) {
IP6 o1 = NominalCheck("$x", Kernel.AnyMO, v);
double r = o1.mo.mro_raw_Numeric.Get(v);
return Kernel.BoxAnyMO(new string((char)r, 1), Kernel.StrMO);
}
// used in cclass.t; maybe worth exposing
public static Variable UniCat(Variable v) {
IP6 o1 = NominalCheck("$x", Kernel.AnyMO, v);
char c = (char) o1.mo.mro_raw_Numeric.Get(v);
int ix = (int) char.GetUnicodeCategory(c);
return Kernel.BoxAnyMO((double)ix, Kernel.NumMO);
}
public static Variable Make(Frame fr, Variable v) {
if (fr.info.name == "SAFE make")
fr = fr.caller;
Cursor c = (Cursor) Kernel.StatusHelper(fr, "$*/", 0).Fetch();
c.Make(v);
return v;
}
public static VarDeque HashIterRaw(int mode, Variable v) {
IP6 o = NominalCheck("$x", Kernel.AnyMO, v);
VarHash d = Kernel.UnboxAny<VarHash>(o);
VarDeque lv = new VarDeque();
foreach (KeyValuePair<string,Variable> kv in d) {
switch (mode) {
case 0:
lv.Push(Kernel.BoxAnyMO<string>(kv.Key, Kernel.StrMO));
break;
case 1:
lv.Push(kv.Value);
break;
case 2:
lv.Push(Kernel.BoxAnyMO<string>(kv.Key, Kernel.StrMO));
lv.Push(kv.Value);
break;
case 3:
DynObject p = new DynObject(Kernel.PairMO);
p.slots[0] = Kernel.BoxAnyMO<string>(kv.Key, Kernel.StrMO);
p.slots[1] = kv.Value;
lv.Push(Kernel.NewROScalar(p));
break;
}
}
return lv;
}
public static Variable HashIter(int mode, Variable v) {
VarDeque lv = HashIterRaw(mode, v);
DynObject l = new DynObject(Kernel.ListMO);
l.slots[0] = lv;
l.slots[1] = new VarDeque();
return Kernel.NewRWListVar(l);
}
public static Variable GetModTime(string path) {
long t = System.IO.File.GetLastWriteTimeUtc(path).Ticks;
double d = ((double)(t - 621355968000000000L)) / 10000000.0;
return Kernel.BoxAnyMO(d, Kernel.NumMO);
}
public static Variable GetNow() {
long t = System.DateTime.UtcNow.Ticks;
double d = ((double)(t - 621355968000000000L)) / 10000000.0;
return Kernel.BoxAnyMO(d, Kernel.NumMO);
}
public static bool FileOrDirExists(string path) {
return System.IO.File.Exists(path) || System.IO.Directory.Exists(path);
}
// This is wrong in the long term. The backend should be linked; it
// should generate collectable assemblies; app-domains should be made
// available through MetaServices.
private static AppDomain subDomain;
public static Variable RunCLRSubtask(Variable filename, Variable args) {
List<string> la = new List<string>();
VarDeque iter = new VarDeque(args);
string sfn = filename.Fetch().mo.mro_raw_Str.Get(filename);
//Console.WriteLine("App name {0}", sfn);
while (Kernel.IterHasFlat(iter, true)) {
Variable v = iter.Shift();
la.Add(v.Fetch().mo.mro_raw_Str.Get(v));
//Console.WriteLine("Arg {0}", la[la.Count-1]);
}
if (subDomain == null) {
AppDomainSetup ads = new AppDomainSetup();
ads.ApplicationBase = System.IO.Path.GetDirectoryName(sfn);
subDomain = AppDomain.CreateDomain("zyg", null, ads);
}
int ret = subDomain.ExecuteAssembly(sfn, null, la.ToArray());
return Kernel.BoxAnyMO((double) ret, Kernel.NumMO);
}
public static void RunSubtask(string file, string args) {
System.Diagnostics.Process.Start(file, args).WaitForExit();
}
public static Variable ArrayConstructor(Variable bits) {
VarDeque rest = new VarDeque(bits);
VarDeque items = new VarDeque();
while (Kernel.IterHasFlat(rest, true))
items.Push(Kernel.NewRWScalar(Kernel.AnyMO, rest.Shift().Fetch()));
IP6 l = new DynObject(Kernel.ArrayMO);
l.SetSlot("rest", rest);
l.SetSlot("items", items);
return Kernel.NewROScalar(l);
}
public static int GetArity(IP6 fcni) {
if (!fcni.Isa(Kernel.SubMO))
return 1; // can't introspect fake subs (?)
SubInfo si = (SubInfo) fcni.GetSlot("info");
int[] sig = si.sig_i;
if (sig == null)
return 1;
int arity = 0;
for (int i = 0; i < sig.Length; i += SubInfo.SIG_I_RECORD) {
int fl = sig[i + SubInfo.SIG_I_FLAGS];
if ((fl & (SubInfo.SIG_F_SLURPY_CAP | SubInfo.SIG_F_SLURPY_POS |
SubInfo.SIG_F_SLURPY_PCL)) != 0)
return int.MaxValue;
if ((fl & SubInfo.SIG_F_POSITIONAL) == 0) continue;
arity++;
}
return arity;
}
class ItemSource {
protected ItemSource() {}
public static ItemSource Empty = new ItemSource();
public virtual bool TryGet(out Variable[] r, bool block) {
r = null;
return true;
}
protected static int TryOne(VarDeque items, bool block) {
if (block) {
return Kernel.IterHasFlat(items, true) ? +1 : -1;
} else {
again:
if (items.Count() == 0) return -1;
Variable v = items[0];
IP6 i = v.Fetch();
if (i.mo.HasMRO(Kernel.IterCursorMO))
return 0;
if (v.islist) {
items.Shift();
items.UnshiftD(i.mo.mro_raw_iterator.Get(v));
goto again;
}
return +1;
}
}
}
class BatchSource: ItemSource {
int arity;
VarDeque items;
public BatchSource(int count, VarDeque items) {
this.arity = count;
this.items = items;
}
public override bool TryGet(out Variable[] r, bool block) {
r = null;
List<Variable> pen = new List<Variable>();
while (pen.Count < arity) {
switch (TryOne(items, block)) {
case -1: goto nomore;
case 0:
for (int i = pen.Count - 1; i >= 0; i--)
items.Unshift(pen[i]);
return false;
case +1: pen.Add(items.Shift()); break;
}
}
nomore:
if (pen.Count != 0)
r = pen.ToArray();
return true;
}
}
class ZipSource : ItemSource {
VarDeque[] sources;
public ZipSource(Variable[] pcl) {
sources = new VarDeque[pcl.Length];
for (int i = 0; i < pcl.Length; i++)
sources[i] = new VarDeque(pcl[i]);
}
public override bool TryGet(out Variable[] r, bool block) {
r = null;
for (int i = 0; i < sources.Length; i++)
switch (TryOne(sources[i], block)) {
case -1: return true;
case 0: return false;
}
r = new Variable[sources.Length];
for (int i = 0; i < sources.Length; i++)
r[i] = sources[i].Shift();
return true;
}
}
class CrossSource: ItemSource {
VarDeque[] basic;
VarDeque[] iter;
Variable[] basic_top;
Variable[] iter_top;
// 0=init 1=end i=advance wheel i-2
int state;
public CrossSource(Variable[] pcl) {
basic = new VarDeque[pcl.Length];
iter = new VarDeque[pcl.Length];
basic_top = new Variable[pcl.Length];
iter_top = new Variable[pcl.Length];
for (int i = 0; i < pcl.Length; i++) {
iter[i] = new VarDeque(pcl[i]);
}
}
public override bool TryGet(out Variable[] r, bool block) {
r = null;
if (state == 0) {
// Make sure all the lists are non-empty.
for (int i = 0; i < iter.Length; i++) {
switch (TryOne(iter[i], block)) {
case -1: return true;
case 0: return false;
case 1: break;
}
}
for (int i = 0; i < iter.Length; i++) {
iter_top[i] = iter[i].Shift();
if (i != 0) {
basic[i] = new VarDeque(iter[i]);
basic_top[i] = iter_top[i];
}
}
}
else if (state == 1) {
return true;
}
else {
again:
int wheel = state - 2;
switch (TryOne(iter[wheel], block)) {
case 0: return false;
case +1:
iter_top[wheel] = iter[wheel].Shift();
break;
case -1:
if (wheel == 0) return true;
iter[wheel] = new VarDeque(basic[wheel]);
iter_top[wheel] = basic_top[wheel];
state--;
goto again;
}
}
r = new Variable[iter_top.Length];
for (int i = 0; i < iter_top.Length; i++)
r[i] = iter_top[i];
state = iter_top.Length + 1;
return true;
}
}
private static SubInfo CommonMEMap_I = new SubInfo("KERNEL map", null,
CommonMEMap_C, null, null, new int[] {
2, 3, SubInfo.ON_NEXT, 0, 0,
2, 3, SubInfo.ON_REDO, 1, 0,
2, 3, SubInfo.ON_LAST, 3, 0,
}, new string[] { "" }, 0, null, null);
private static Frame CommonMEMap_C(Frame th) {
ItemSource src = (ItemSource) th.lex0;
VarDeque outq = (VarDeque) th.lex1;
IP6 fnc = (IP6) th.lex2;
int tailmode = th.lexi0;
switch (th.ip) {
case 0:
Variable[] pen;
if (!src.TryGet(out pen, tailmode != 0)) {
DynObject thunk = new DynObject(Kernel.GatherIteratorMO);
th.lex = new Dictionary<string,object>();
th.lex["!return"] = null;
th.MarkSharedChain();
thunk.slots[0] = Kernel.NewRWScalar(Kernel.AnyMO, th);
thunk.slots[1] = Kernel.NewRWScalar(Kernel.AnyMO, Kernel.AnyP);
DynObject lst = new DynObject(Kernel.ListMO);
lst.slots[0] = outq;
lst.slots[1] = new VarDeque(Kernel.NewROScalar(thunk));
th.caller.resultSlot = Kernel.NewRWListVar(lst);
th.lexi0 = 1;
return th.caller;
}
if (pen == null) {
if (tailmode != 0)
return Kernel.Take(th, Kernel.NewROScalar(Kernel.EMPTYP));
DynObject lst = new DynObject(Kernel.ListMO);
lst.slots[0] = outq;
lst.slots[1] = new VarDeque();
th.caller.resultSlot = Kernel.NewRWListVar(lst);
return th.caller;
}
th.lex3 = pen;
th.ip = 1;
goto case 1;
case 1:
th.ip = 2;
if (fnc != null)
return fnc.Invoke(th, (Variable[])th.lex3, null);
else {
th.resultSlot = Kernel.NewRWListVar(
Kernel.BoxRaw((Variable[])th.lex3,
Kernel.ParcelMO));
goto case 2;
}
case 2:
if (tailmode != 0) {
th.ip = 0;
return Kernel.Take(th, (Variable)th.resultSlot);
} else {
outq.Push((Variable) th.resultSlot);
th.ip = 0;
goto case 0;
}
case 3:
th.lex0 = src = ItemSource.Empty;
th.ip = 0;
goto case 0;
default:
return Kernel.Die(th, "Invalid IP");
}
}
public static Frame MEMap(Frame th, Variable[] lst) {
VarDeque iter = new VarDeque(lst);
Variable fcn = iter.Shift();
IP6 fcni = fcn.Fetch();
int arity = GetArity(fcni);
Frame fr = th.MakeChild(null, CommonMEMap_I);
fr.lexi0 = 0;
fr.lex0 = new BatchSource(arity, iter);
fr.lex1 = new VarDeque();
fr.lex2 = fcni;
return fr;
}
static IP6 ExtractWith(bool with, ref Variable[] pcl) {
if (!with) return null;
Variable[] opcl = pcl;
pcl = new Variable[pcl.Length - 1];
for (int j = 0; j < pcl.Length; j++)
pcl[j] = opcl[j+1];
return opcl[0].Fetch();
}
public static Frame MEZip(Frame th, bool with, Variable[] pcl) {
Frame fr = th.MakeChild(null, CommonMEMap_I);
fr.lexi0 = 0;
fr.lex2 = ExtractWith(with, ref pcl);
fr.lex0 = new ZipSource(pcl);
fr.lex1 = new VarDeque();
return fr;
}
public static Frame MECross(Frame th, bool with, Variable[] pcl) {
Frame fr = th.MakeChild(null, CommonMEMap_I);
fr.lexi0 = 0;
fr.lex2 = ExtractWith(with, ref pcl);
fr.lex0 = new CrossSource(pcl);
fr.lex1 = new VarDeque();
return fr;
}
private static SubInfo CommonGrep_I = new SubInfo("KERNEL grep", null,
CommonGrep_C, null, null, new int[] {
2, 3, SubInfo.ON_NEXT, 0, 0,
2, 3, SubInfo.ON_REDO, 1, 0,
2, 3, SubInfo.ON_LAST, 3, 0,
}, new string[] { "" }, 0, null, null);
private static Frame CommonGrep_C(Frame th) {
VarDeque src = (VarDeque) th.lex0;
VarDeque outq = (VarDeque) th.lex1;
Variable flt = (Variable) th.lex2;
int tailmode = th.lexi0;
switch (th.ip) {
case 0:
Variable pen = null;
while (pen == null) {
if (tailmode != 0) {
if (!Kernel.IterHasFlat(src, false)) break;
} else {
if (src.Count() == 0) break;
if (src[0].Fetch().mo.HasMRO(Kernel.IterCursorMO)) {
DynObject thunk = new DynObject(Kernel.GatherIteratorMO);
th.lex = new Dictionary<string,object>();
th.lex["!return"] = null;
th.MarkSharedChain();
thunk.slots[0] = Kernel.NewRWScalar(Kernel.AnyMO, th);
thunk.slots[1] = Kernel.NewRWScalar(Kernel.AnyMO, Kernel.AnyP);
DynObject lst = new DynObject(Kernel.ListMO);
lst.slots[0] = outq;
lst.slots[1] = new VarDeque(Kernel.NewROScalar(thunk));
th.caller.resultSlot = Kernel.NewRWListVar(lst);
th.lexi0 = 1;
return th.caller;
}
}
pen = src.Shift();
}
if (pen == null) {
if (tailmode != 0)
return Kernel.Take(th, Kernel.NewROScalar(Kernel.EMPTYP));
DynObject lst = new DynObject(Kernel.ListMO);
lst.slots[0] = outq;
lst.slots[1] = new VarDeque();
th.caller.resultSlot = Kernel.NewRWListVar(lst);
return th.caller;
}
th.lex3 = pen;
th.ip = 1;
goto case 1;
case 1:
th.ip = 2;
return flt.Fetch().InvokeMethod(th, "ACCEPTS",
new Variable[] { flt, (Variable)th.lex3 }, null);
case 2:
Variable r = (Variable) th.resultSlot;
if (!r.Fetch().mo.mro_raw_Bool.Get(r)) {
th.ip = 0;
goto case 0;
}
if (tailmode != 0) {
th.ip = 0;
return Kernel.Take(th, (Variable)th.lex3);
} else {
outq.Push((Variable) th.lex3);
th.ip = 0;
goto case 0;
}
case 3:
th.lex0 = src = new VarDeque();
th.ip = 0;
goto case 0;
default:
return Kernel.Die(th, "Invalid IP");
}
}
public static Frame MEGrep(Frame th, Variable[] lst) {
VarDeque iter = new VarDeque(lst);
Variable fcn = iter.Shift();
iter = Kernel.IterFlatten(iter);
Frame fr = th.MakeChild(null, CommonGrep_I);
fr.lexi0 = 0;
fr.lex0 = iter;
fr.lex1 = new VarDeque();
fr.lex2 = fcn;
return fr;
}
public static Frame CallNext(Frame th, IP6 cap) {
Frame to = th;
while (to != null && to.curDisp == null)
to = to.caller;
if (to == null || to.curDisp.next == null)
return Kernel.Die(th, "No next function to call!");
DispatchEnt de = to.curDisp.next;
DynObject o = cap as DynObject;
Frame nf = th.MakeChild(de.outer, de.info);
Variable[] p = to.pos;
VarHash n = to.named;
if (o != null) {
p = (Variable[]) o.slots[0];
n = o.slots[1] as VarHash;
}
nf = nf.info.Binder(nf, p, n);
nf.curDisp = de;
return nf;
}
}