goodguide-gibbon 0.5.1 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/goodguide/gibbon/version.rb +1 -1
- data/lib/goodguide/gibbon.rb +60 -33
- data/vendor/gibbon/lib/gibbon.browser.dev.js +3145 -553
- data/vendor/gibbon/lib/gibbon.browser.js +3135 -541
- metadata +8 -8
@@ -359,14 +359,13 @@ Parsimmon.Parser = P(function(_, _super, Parser) {
|
|
359
359
|
return Parsimmon;
|
360
360
|
})()
|
361
361
|
// Generated by CoffeeScript 1.6.3
|
362
|
-
var Gibbon
|
363
|
-
|
364
|
-
Gibbon = {};
|
365
|
-
// Generated by CoffeeScript 1.6.3
|
366
|
-
var CompMap, DEBUG, Hash, Map, Union, allocate, asyncMap, catLists, contIter, contMap, genId, isArray, uniq, _ref,
|
362
|
+
var AST, CachingPromise, CompMap, CompiledCode, ContinuationTrace, Core, DEBUG, Dependency, Failure, Gibbon, Hash, JS, List, Map, ObjHash, Promise, RVal, Semantic, Step, Thunk, Trace, Type, TypeAST, TypeExpr, TypeLookup, UnitPromise, Value, VarTrace, Variant, allocate, analyze, applyOp1, applyOp2, asyncMap, catLists, contIter, contMap, equalArrays, eval_, genId, isArray, nameGen, parse, stdlib, uniq, _ref, _ref1, _ref10, _ref11, _ref12, _ref13, _ref14, _ref15, _ref16, _ref17, _ref18, _ref19, _ref2, _ref3, _ref4, _ref5, _ref6, _ref7, _ref8, _ref9,
|
367
363
|
__slice = [].slice,
|
368
364
|
__hasProp = {}.hasOwnProperty,
|
369
|
-
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }
|
365
|
+
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
|
366
|
+
__indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
|
367
|
+
|
368
|
+
Gibbon = {};
|
370
369
|
|
371
370
|
genId = (function() {
|
372
371
|
var id;
|
@@ -376,11 +375,29 @@ genId = (function() {
|
|
376
375
|
};
|
377
376
|
})();
|
378
377
|
|
378
|
+
Thunk = (function() {
|
379
|
+
Thunk.trampoline = function(t) {
|
380
|
+
while (t instanceof Thunk) {
|
381
|
+
t = t.run();
|
382
|
+
}
|
383
|
+
return t;
|
384
|
+
};
|
385
|
+
|
386
|
+
function Thunk(run) {
|
387
|
+
this.run = run;
|
388
|
+
}
|
389
|
+
|
390
|
+
return Thunk;
|
391
|
+
|
392
|
+
})();
|
393
|
+
|
379
394
|
DEBUG = function(f) {
|
380
395
|
return f();
|
381
396
|
};
|
382
397
|
|
383
|
-
DEBUG.log =
|
398
|
+
DEBUG.log = function() {
|
399
|
+
return console.log.apply(console, arguments);
|
400
|
+
};
|
384
401
|
|
385
402
|
DEBUG.assert = function(msg, cond) {
|
386
403
|
if (!cond) {
|
@@ -406,6 +423,53 @@ catLists = function(lists) {
|
|
406
423
|
return out;
|
407
424
|
};
|
408
425
|
|
426
|
+
equalArrays = function(a1, a2) {
|
427
|
+
var e, i, _i, _len;
|
428
|
+
for (i = _i = 0, _len = a1.length; _i < _len; i = ++_i) {
|
429
|
+
e = a1[i];
|
430
|
+
if (e !== a2[i]) {
|
431
|
+
return false;
|
432
|
+
}
|
433
|
+
}
|
434
|
+
return true;
|
435
|
+
};
|
436
|
+
|
437
|
+
applyOp1 = function(op, arg) {
|
438
|
+
switch (op) {
|
439
|
+
case '!':
|
440
|
+
return !arg;
|
441
|
+
case '-':
|
442
|
+
return -arg;
|
443
|
+
default:
|
444
|
+
throw "unknown operator " + op;
|
445
|
+
}
|
446
|
+
};
|
447
|
+
|
448
|
+
applyOp2 = function(op, l, r) {
|
449
|
+
switch (op) {
|
450
|
+
case '+':
|
451
|
+
return l + r;
|
452
|
+
case '*':
|
453
|
+
return l * r;
|
454
|
+
case '/':
|
455
|
+
return l / r;
|
456
|
+
case '%':
|
457
|
+
return l % r;
|
458
|
+
case '===':
|
459
|
+
return l === r;
|
460
|
+
case '<':
|
461
|
+
return l < r;
|
462
|
+
case '>':
|
463
|
+
return l > r;
|
464
|
+
case '<=':
|
465
|
+
return l <= r;
|
466
|
+
case '>=':
|
467
|
+
return l >= r;
|
468
|
+
default:
|
469
|
+
throw "unknown operator " + op;
|
470
|
+
}
|
471
|
+
};
|
472
|
+
|
409
473
|
uniq = function(list, eq) {
|
410
474
|
var el, out, u, _i, _j, _len, _len1;
|
411
475
|
if (eq == null) {
|
@@ -435,34 +499,34 @@ uniq = function(list, eq) {
|
|
435
499
|
|
436
500
|
allocate = Object.create || function(proto) {
|
437
501
|
var dummy;
|
438
|
-
dummy = function() {};
|
502
|
+
dummy = (function() {});
|
439
503
|
dummy.prototype = proto;
|
440
504
|
return new dummy;
|
441
505
|
};
|
442
506
|
|
443
507
|
asyncMap = function(list, mapper, cb) {
|
444
|
-
var el, i, output, response, seen, _i, _len
|
508
|
+
var el, i, output, response, retVal, seen, _i, _len;
|
445
509
|
if (list.length === 0) {
|
446
510
|
return cb(list);
|
447
511
|
}
|
448
512
|
seen = 0;
|
449
513
|
output = [];
|
514
|
+
retVal = null;
|
450
515
|
response = function(i) {
|
451
516
|
return function(el) {
|
452
517
|
seen += 1;
|
453
518
|
output[i] = el;
|
454
519
|
if (seen >= list.length) {
|
455
|
-
cb(output);
|
520
|
+
retVal = cb(output);
|
456
521
|
}
|
457
522
|
return null;
|
458
523
|
};
|
459
524
|
};
|
460
|
-
_results = [];
|
461
525
|
for (i = _i = 0, _len = list.length; _i < _len; i = ++_i) {
|
462
526
|
el = list[i];
|
463
|
-
|
527
|
+
mapper(el, response(i), i);
|
464
528
|
}
|
465
|
-
return
|
529
|
+
return retVal;
|
466
530
|
};
|
467
531
|
|
468
532
|
contMap = function(list, mapper, cb) {
|
@@ -492,10 +556,12 @@ contIter = function(list, f, cb) {
|
|
492
556
|
return loop_(0);
|
493
557
|
};
|
494
558
|
|
495
|
-
|
559
|
+
Variant = (function() {
|
496
560
|
var castJSON;
|
497
561
|
|
498
|
-
|
562
|
+
Variant.prototype.__isVariant__ = true;
|
563
|
+
|
564
|
+
Variant.specialize = function(tag, names) {
|
499
565
|
var klass, name, subclass, _i, _len;
|
500
566
|
klass = this;
|
501
567
|
subclass = function(values) {
|
@@ -517,19 +583,20 @@ Union = (function() {
|
|
517
583
|
};
|
518
584
|
};
|
519
585
|
|
520
|
-
|
586
|
+
Variant.variants = function(tagSpec) {
|
521
587
|
var names, tag, _results;
|
522
|
-
this.
|
588
|
+
this.prototype._type = this.name;
|
589
|
+
this.tags = tagSpec;
|
523
590
|
_results = [];
|
524
|
-
for (tag in
|
525
|
-
if (!__hasProp.call(
|
526
|
-
names =
|
591
|
+
for (tag in tagSpec) {
|
592
|
+
if (!__hasProp.call(tagSpec, tag)) continue;
|
593
|
+
names = tagSpec[tag];
|
527
594
|
_results.push(this[tag] = this.specialize(tag, names));
|
528
595
|
}
|
529
596
|
return _results;
|
530
597
|
};
|
531
598
|
|
532
|
-
function
|
599
|
+
function Variant(_names, _values) {
|
533
600
|
var i, name, _i, _len, _ref;
|
534
601
|
this._names = _names;
|
535
602
|
this._values = _values;
|
@@ -540,7 +607,7 @@ Union = (function() {
|
|
540
607
|
}
|
541
608
|
}
|
542
609
|
|
543
|
-
|
610
|
+
Variant.prototype.cases = function(cases) {
|
544
611
|
var fn;
|
545
612
|
fn = cases[this._tag] || cases.other;
|
546
613
|
if (!fn) {
|
@@ -565,11 +632,12 @@ Union = (function() {
|
|
565
632
|
}
|
566
633
|
};
|
567
634
|
|
568
|
-
|
635
|
+
Variant.prototype.asJSON = function() {
|
569
636
|
var name, out, _i, _len, _ref;
|
570
637
|
out = {
|
638
|
+
__isVariant__: true,
|
571
639
|
_tag: this._tag,
|
572
|
-
|
640
|
+
_type: this.constructor.name
|
573
641
|
};
|
574
642
|
_ref = this._names;
|
575
643
|
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
@@ -579,7 +647,7 @@ Union = (function() {
|
|
579
647
|
return out;
|
580
648
|
};
|
581
649
|
|
582
|
-
|
650
|
+
Variant.fromJSON = function(o) {
|
583
651
|
var constructor, e, name, names, vals;
|
584
652
|
if (isArray(o)) {
|
585
653
|
return (function() {
|
@@ -595,7 +663,7 @@ Union = (function() {
|
|
595
663
|
if (!(typeof o === 'object' && (o != null) && '_tag' in o)) {
|
596
664
|
return o;
|
597
665
|
}
|
598
|
-
constructor = Gibbon[o.
|
666
|
+
constructor = Gibbon[o._type];
|
599
667
|
names = constructor.tags[o._tag];
|
600
668
|
vals = (function() {
|
601
669
|
var _i, _len, _results;
|
@@ -609,14 +677,14 @@ Union = (function() {
|
|
609
677
|
return constructor[o._tag].apply(constructor, vals);
|
610
678
|
};
|
611
679
|
|
612
|
-
|
680
|
+
Variant.wrap = function(o) {
|
613
681
|
if (o instanceof this) {
|
614
682
|
return o;
|
615
683
|
}
|
616
684
|
return this.fromJSON(o);
|
617
685
|
};
|
618
686
|
|
619
|
-
return
|
687
|
+
return Variant;
|
620
688
|
|
621
689
|
})();
|
622
690
|
|
@@ -701,7 +769,7 @@ Map = (function() {
|
|
701
769
|
})();
|
702
770
|
|
703
771
|
Gibbon.Hash = Hash = (function(_super) {
|
704
|
-
var salt;
|
772
|
+
var salt, saltLen;
|
705
773
|
|
706
774
|
__extends(Hash, _super);
|
707
775
|
|
@@ -714,13 +782,15 @@ Gibbon.Hash = Hash = (function(_super) {
|
|
714
782
|
|
715
783
|
salt = '<key>';
|
716
784
|
|
785
|
+
saltLen = salt.length;
|
786
|
+
|
717
787
|
Hash.prototype.loadKey = function(k) {
|
718
788
|
return salt + k;
|
719
789
|
};
|
720
790
|
|
721
791
|
Hash.prototype.dumpKey = function(k) {
|
722
792
|
if (k.indexOf(salt) === 0) {
|
723
|
-
return k.slice(
|
793
|
+
return k.slice(saltLen);
|
724
794
|
}
|
725
795
|
};
|
726
796
|
|
@@ -742,10 +812,9 @@ Gibbon.Hash = Hash = (function(_super) {
|
|
742
812
|
for (k in this) {
|
743
813
|
if (!__hasProp.call(this, k)) continue;
|
744
814
|
v = this[k];
|
745
|
-
if (k.
|
746
|
-
|
815
|
+
if (k.slice(0, saltLen) === salt) {
|
816
|
+
_results.push(f(k.slice(saltLen), v));
|
747
817
|
}
|
748
|
-
_results.push(f(k.slice(salt.length), v));
|
749
818
|
}
|
750
819
|
return _results;
|
751
820
|
};
|
@@ -754,6 +823,45 @@ Gibbon.Hash = Hash = (function(_super) {
|
|
754
823
|
|
755
824
|
})(Map);
|
756
825
|
|
826
|
+
Gibbon.ObjHash = ObjHash = (function(_super) {
|
827
|
+
__extends(ObjHash, _super);
|
828
|
+
|
829
|
+
function ObjHash() {
|
830
|
+
_ref1 = ObjHash.__super__.constructor.apply(this, arguments);
|
831
|
+
return _ref1;
|
832
|
+
}
|
833
|
+
|
834
|
+
ObjHash.prototype.get = function(k) {
|
835
|
+
var _ref2;
|
836
|
+
return (_ref2 = this[k.hash()]) != null ? _ref2.value : void 0;
|
837
|
+
};
|
838
|
+
|
839
|
+
ObjHash.prototype.set = function(k, v) {
|
840
|
+
return this[k.hash()] = {
|
841
|
+
key: k,
|
842
|
+
value: v
|
843
|
+
};
|
844
|
+
};
|
845
|
+
|
846
|
+
ObjHash.prototype.has = function(k) {
|
847
|
+
return this.hasOwnProperty(k.hash());
|
848
|
+
};
|
849
|
+
|
850
|
+
ObjHash.prototype.each = function(f) {
|
851
|
+
var k, v, _results;
|
852
|
+
_results = [];
|
853
|
+
for (k in this) {
|
854
|
+
if (!__hasProp.call(this, k)) continue;
|
855
|
+
v = this[k];
|
856
|
+
_results.push(f(v.key, v.value));
|
857
|
+
}
|
858
|
+
return _results;
|
859
|
+
};
|
860
|
+
|
861
|
+
return ObjHash;
|
862
|
+
|
863
|
+
})(Hash);
|
864
|
+
|
757
865
|
CompMap = (function(_super) {
|
758
866
|
__extends(CompMap, _super);
|
759
867
|
|
@@ -776,10 +884,10 @@ CompMap = (function(_super) {
|
|
776
884
|
};
|
777
885
|
|
778
886
|
CompMap.prototype.keyIndex = function(k) {
|
779
|
-
var i, key, _i, _len,
|
780
|
-
|
781
|
-
for (i = _i = 0, _len =
|
782
|
-
key =
|
887
|
+
var i, key, _i, _len, _ref2;
|
888
|
+
_ref2 = this.keys;
|
889
|
+
for (i = _i = 0, _len = _ref2.length; _i < _len; i = ++_i) {
|
890
|
+
key = _ref2[i];
|
783
891
|
if (this.compare(key, k)) {
|
784
892
|
return i;
|
785
893
|
}
|
@@ -808,37 +916,37 @@ CompMap = (function(_super) {
|
|
808
916
|
};
|
809
917
|
|
810
918
|
CompMap.prototype.each = function(f) {
|
811
|
-
var i, k, _i, _len,
|
812
|
-
|
919
|
+
var i, k, _i, _len, _ref2, _results;
|
920
|
+
_ref2 = this.keys;
|
813
921
|
_results = [];
|
814
|
-
for (i = _i = 0, _len =
|
815
|
-
k =
|
922
|
+
for (i = _i = 0, _len = _ref2.length; _i < _len; i = ++_i) {
|
923
|
+
k = _ref2[i];
|
816
924
|
_results.push(f(k, this.values[i]));
|
817
925
|
}
|
818
926
|
return _results;
|
819
927
|
};
|
820
928
|
|
821
929
|
CompMap.prototype.eachAsync = function(f, cb) {
|
822
|
-
var i, k, keys, responder, seen, _i, _len
|
930
|
+
var i, k, keys, output, responder, seen, _i, _len;
|
823
931
|
keys = this.keys;
|
824
932
|
if (keys.length === 0) {
|
825
933
|
return;
|
826
934
|
}
|
935
|
+
output = null;
|
827
936
|
seen = 0;
|
828
937
|
responder = function(i) {
|
829
938
|
return function() {
|
830
939
|
seen += 1;
|
831
940
|
if (seen >= keys.length) {
|
832
|
-
return cb();
|
941
|
+
return output = cb();
|
833
942
|
}
|
834
943
|
};
|
835
944
|
};
|
836
|
-
_results = [];
|
837
945
|
for (i = _i = 0, _len = keys.length; _i < _len; i = ++_i) {
|
838
946
|
k = keys[i];
|
839
|
-
|
947
|
+
f(k, this.values[i], responder(i));
|
840
948
|
}
|
841
|
-
return
|
949
|
+
return output;
|
842
950
|
};
|
843
951
|
|
844
952
|
CompMap.prototype.fetch = function(k, f) {
|
@@ -871,10 +979,48 @@ CompMap = (function(_super) {
|
|
871
979
|
return CompMap;
|
872
980
|
|
873
981
|
})(Map);
|
874
|
-
|
875
|
-
|
876
|
-
|
877
|
-
|
982
|
+
|
983
|
+
List = (function(_super) {
|
984
|
+
__extends(List, _super);
|
985
|
+
|
986
|
+
function List() {
|
987
|
+
_ref2 = List.__super__.constructor.apply(this, arguments);
|
988
|
+
return _ref2;
|
989
|
+
}
|
990
|
+
|
991
|
+
List.variants({
|
992
|
+
empty: [],
|
993
|
+
cons: ['head', 'tail']
|
994
|
+
});
|
995
|
+
|
996
|
+
List.single = function(el) {
|
997
|
+
return List.empty().cons(el);
|
998
|
+
};
|
999
|
+
|
1000
|
+
List.prototype.cons = function(el) {
|
1001
|
+
return List.cons(el, this);
|
1002
|
+
};
|
1003
|
+
|
1004
|
+
List.prototype.toArray = function() {
|
1005
|
+
return this.mapArray(function(x) {
|
1006
|
+
return x;
|
1007
|
+
});
|
1008
|
+
};
|
1009
|
+
|
1010
|
+
List.prototype.mapArray = function(f) {
|
1011
|
+
var out, tr;
|
1012
|
+
out = [];
|
1013
|
+
tr = this;
|
1014
|
+
while (tr._tag === 'cons') {
|
1015
|
+
out.push(f(tr.head));
|
1016
|
+
tr = tr.tail;
|
1017
|
+
}
|
1018
|
+
return out;
|
1019
|
+
};
|
1020
|
+
|
1021
|
+
return List;
|
1022
|
+
|
1023
|
+
})(Variant);
|
878
1024
|
|
879
1025
|
Gibbon.AST = AST = (function(_super) {
|
880
1026
|
var inspectDefinitions;
|
@@ -882,11 +1028,11 @@ Gibbon.AST = AST = (function(_super) {
|
|
882
1028
|
__extends(AST, _super);
|
883
1029
|
|
884
1030
|
function AST() {
|
885
|
-
|
886
|
-
return
|
1031
|
+
_ref3 = AST.__super__.constructor.apply(this, arguments);
|
1032
|
+
return _ref3;
|
887
1033
|
}
|
888
1034
|
|
889
|
-
AST.
|
1035
|
+
AST.variants({
|
890
1036
|
integer: ['value'],
|
891
1037
|
decimal: ['value'],
|
892
1038
|
percent: ['value'],
|
@@ -1014,17 +1160,17 @@ Gibbon.AST = AST = (function(_super) {
|
|
1014
1160
|
|
1015
1161
|
return AST;
|
1016
1162
|
|
1017
|
-
})(
|
1163
|
+
})(Variant);
|
1018
1164
|
|
1019
1165
|
Gibbon.TypeAST = TypeAST = (function(_super) {
|
1020
1166
|
__extends(TypeAST, _super);
|
1021
1167
|
|
1022
1168
|
function TypeAST() {
|
1023
|
-
|
1024
|
-
return
|
1169
|
+
_ref4 = TypeAST.__super__.constructor.apply(this, arguments);
|
1170
|
+
return _ref4;
|
1025
1171
|
}
|
1026
1172
|
|
1027
|
-
TypeAST.
|
1173
|
+
TypeAST.variants({
|
1028
1174
|
concrete: ['name'],
|
1029
1175
|
variable: ['name'],
|
1030
1176
|
"native": ['id'],
|
@@ -1046,10 +1192,10 @@ Gibbon.TypeAST = TypeAST = (function(_super) {
|
|
1046
1192
|
|
1047
1193
|
return TypeAST;
|
1048
1194
|
|
1049
|
-
})(
|
1195
|
+
})(Variant);
|
1050
1196
|
|
1051
1197
|
parse = Gibbon.parse = (function() {
|
1052
|
-
var accessor, accessorExpr, arrow, arrowType, assertString, bang, blankLines, blockExpr, blockType, comma, commaSepFlows, comment, component, concrete, decimal, decimalExpr, defaulted, define, definition, expr, fail, flow, fraction, fractionExpr, frame, freeFrame, fullSignature, func, funcPlaceholder, identifier, innerFrame, integer, integerExpr, isString, label, labelVal, lbrace, lbrack, lexeme, lines, listExpr, listType, lparen, lsplat, metadata, multiline, name, nativeId, nativeType, nonDefaultedFlow, nonPairedFlow, numericExpr, pair, parenFlow, parenFrame, parenType, percent, percentExpr, program, query, queryExpr, rbrace, rbrack, regex, rparen, rsplat, signature, simpleType, singletonFlow, squishListExpr, str, string, stringExpr, substExpr, succeed, tag, tassign, type, typeVar, variable, whitespace, wildType, wildcard;
|
1198
|
+
var accessor, accessorExpr, arrow, arrowType, assertString, bang, blankLines, blockExpr, blockType, comma, commaSepFlows, comment, component, concrete, decimal, decimalExpr, defaulted, define, definition, expr, fail, flow, fraction, fractionExpr, frame, freeFrame, fullSignature, func, funcPlaceholder, identifier, innerFrame, integer, integerExpr, isString, label, labelVal, lbrace, lbrack, lexeme, lines, listExpr, listType, lparen, lsplat, metadata, multiline, name, nativeId, nativeType, nonDefaultedFlow, nonPairedFlow, numericExpr, pair, parenFlow, parenFrame, parenType, percent, percentExpr, program, query, queryArg, queryExpr, rbrace, rbrack, regex, rparen, rsplat, signature, simpleType, singletonFlow, squishListExpr, str, string, stringExpr, substExpr, succeed, tag, tassign, type, typeVar, variable, whitespace, wildType, wildcard;
|
1053
1199
|
tag = function(name, parser) {
|
1054
1200
|
return parser.tag(name);
|
1055
1201
|
};
|
@@ -1087,6 +1233,7 @@ parse = Gibbon.parse = (function() {
|
|
1087
1233
|
lsplat = multiline(string('[*'));
|
1088
1234
|
rsplat = lexeme(string('*]'));
|
1089
1235
|
query = lexeme(string('.').then(identifier));
|
1236
|
+
queryArg = lexeme(regex(/^[\w-]+/));
|
1090
1237
|
accessor = lexeme(string('@').then(identifier));
|
1091
1238
|
name = lexeme(identifier);
|
1092
1239
|
str = lexeme(string("'").then(regex(/^[^']*/)).skip(string("'")));
|
@@ -1109,8 +1256,8 @@ parse = Gibbon.parse = (function() {
|
|
1109
1256
|
return AST.percent(parseInt(p));
|
1110
1257
|
});
|
1111
1258
|
fractionExpr = fraction.map(function(f) {
|
1112
|
-
var denom, num,
|
1113
|
-
|
1259
|
+
var denom, num, _ref5;
|
1260
|
+
_ref5 = f.split('/'), num = _ref5[0], denom = _ref5[1];
|
1114
1261
|
return AST.fraction(num, denom);
|
1115
1262
|
});
|
1116
1263
|
stringExpr = str.map(function(s) {
|
@@ -1120,7 +1267,7 @@ parse = Gibbon.parse = (function() {
|
|
1120
1267
|
return AST.query('access', [name]);
|
1121
1268
|
});
|
1122
1269
|
queryExpr = query.then(function(q) {
|
1123
|
-
return
|
1270
|
+
return queryArg.many().map(function(args) {
|
1124
1271
|
return AST.query(q, args);
|
1125
1272
|
});
|
1126
1273
|
});
|
@@ -1271,21 +1418,16 @@ parse = Gibbon.parse = (function() {
|
|
1271
1418
|
};
|
1272
1419
|
return parse;
|
1273
1420
|
})();
|
1274
|
-
// Generated by CoffeeScript 1.6.3
|
1275
|
-
var Semantic, Type, TypeExpr, TypeLookup, analyze, _ref, _ref1, _ref2, _ref3,
|
1276
|
-
__hasProp = {}.hasOwnProperty,
|
1277
|
-
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
|
1278
|
-
__slice = [].slice;
|
1279
1421
|
|
1280
1422
|
Gibbon.Semantic = Semantic = (function(_super) {
|
1281
1423
|
__extends(Semantic, _super);
|
1282
1424
|
|
1283
1425
|
function Semantic() {
|
1284
|
-
|
1285
|
-
return
|
1426
|
+
_ref5 = Semantic.__super__.constructor.apply(this, arguments);
|
1427
|
+
return _ref5;
|
1286
1428
|
}
|
1287
1429
|
|
1288
|
-
Semantic.
|
1430
|
+
Semantic.variants({
|
1289
1431
|
definition: ['dependencies', 'flow'],
|
1290
1432
|
literal: ['syntax'],
|
1291
1433
|
query: ['annotations'],
|
@@ -1299,37 +1441,127 @@ Gibbon.Semantic = Semantic = (function(_super) {
|
|
1299
1441
|
defaulted: ['body', 'alternative']
|
1300
1442
|
});
|
1301
1443
|
|
1444
|
+
Semantic.prototype.inspect = function() {
|
1445
|
+
return this.cases({
|
1446
|
+
definition: function(deps, flow) {
|
1447
|
+
var d;
|
1448
|
+
return "<" + (((function() {
|
1449
|
+
var _i, _len, _results;
|
1450
|
+
_results = [];
|
1451
|
+
for (_i = 0, _len = deps.length; _i < _len; _i++) {
|
1452
|
+
d = deps[_i];
|
1453
|
+
_results.push(d.inspect());
|
1454
|
+
}
|
1455
|
+
return _results;
|
1456
|
+
})()).join(', ')) + "> " + (flow.inspect());
|
1457
|
+
},
|
1458
|
+
literal: function(syntax) {
|
1459
|
+
return syntax.inspect();
|
1460
|
+
},
|
1461
|
+
query: function(annotations) {
|
1462
|
+
return "(? " + (JSON.stringify(annotations)) + ")";
|
1463
|
+
},
|
1464
|
+
localAccessor: function(name) {
|
1465
|
+
return "@" + name;
|
1466
|
+
},
|
1467
|
+
pair: function(first, second) {
|
1468
|
+
return "(" + (first.inspect()) + " : " + (second.inspect()) + ")";
|
1469
|
+
},
|
1470
|
+
block: function(body) {
|
1471
|
+
return "{ " + (body.inspect()) + " }";
|
1472
|
+
},
|
1473
|
+
list: function(elements, squish) {
|
1474
|
+
var e, l, r;
|
1475
|
+
if (squish) {
|
1476
|
+
l = "[*";
|
1477
|
+
r = "*]";
|
1478
|
+
} else {
|
1479
|
+
l = "[";
|
1480
|
+
r = "]";
|
1481
|
+
}
|
1482
|
+
return "" + l + (((function() {
|
1483
|
+
var _i, _len, _results;
|
1484
|
+
_results = [];
|
1485
|
+
for (_i = 0, _len = elements.length; _i < _len; _i++) {
|
1486
|
+
e = elements[_i];
|
1487
|
+
_results.push(e.inspect());
|
1488
|
+
}
|
1489
|
+
return _results;
|
1490
|
+
})()).join(', ')) + r;
|
1491
|
+
},
|
1492
|
+
flow: function(type, head, tail) {
|
1493
|
+
if (tail) {
|
1494
|
+
return "" + (tail.inspect()) + " -> " + (head.inspect()) + " :: " + (type.inspect());
|
1495
|
+
} else {
|
1496
|
+
return "" + (head.inspect()) + " :: " + (type.inspect());
|
1497
|
+
}
|
1498
|
+
},
|
1499
|
+
func: function(name, args, scope) {
|
1500
|
+
var a;
|
1501
|
+
return "" + name + " " + (((function() {
|
1502
|
+
var _i, _len, _results;
|
1503
|
+
_results = [];
|
1504
|
+
for (_i = 0, _len = args.length; _i < _len; _i++) {
|
1505
|
+
a = args[_i];
|
1506
|
+
_results.push("(" + (a.inspect()) + ")");
|
1507
|
+
}
|
1508
|
+
return _results;
|
1509
|
+
})()).join(' '));
|
1510
|
+
},
|
1511
|
+
subst: function(flow) {
|
1512
|
+
return flow.inspect();
|
1513
|
+
},
|
1514
|
+
defaulted: function(body, alt) {
|
1515
|
+
return "" + (body.inspect()) + " | " + (alt.inspect());
|
1516
|
+
}
|
1517
|
+
});
|
1518
|
+
};
|
1519
|
+
|
1302
1520
|
return Semantic;
|
1303
1521
|
|
1304
|
-
})(
|
1522
|
+
})(Variant);
|
1305
1523
|
|
1306
1524
|
Gibbon.TypeLookup = TypeLookup = (function(_super) {
|
1307
1525
|
__extends(TypeLookup, _super);
|
1308
1526
|
|
1309
1527
|
function TypeLookup() {
|
1310
|
-
|
1311
|
-
return
|
1528
|
+
_ref6 = TypeLookup.__super__.constructor.apply(this, arguments);
|
1529
|
+
return _ref6;
|
1312
1530
|
}
|
1313
1531
|
|
1314
|
-
TypeLookup.
|
1532
|
+
TypeLookup.variants({
|
1315
1533
|
response: ['query', 'analysis'],
|
1316
1534
|
local: ['name'],
|
1317
1535
|
error: ['error']
|
1318
1536
|
});
|
1319
1537
|
|
1538
|
+
TypeLookup.prototype.inspect = function() {
|
1539
|
+
return this.cases({
|
1540
|
+
response: function(query, analysis) {
|
1541
|
+
return "" + (query.inspect()) + (JSON.stringify(analysis.annotations)) + "::" + (analysis.type.inspect());
|
1542
|
+
},
|
1543
|
+
local: function(name) {
|
1544
|
+
return "@" + name;
|
1545
|
+
},
|
1546
|
+
error: function(e) {
|
1547
|
+
return "!" + e;
|
1548
|
+
}
|
1549
|
+
});
|
1550
|
+
};
|
1551
|
+
|
1320
1552
|
return TypeLookup;
|
1321
1553
|
|
1322
|
-
})(
|
1554
|
+
})(Variant);
|
1323
1555
|
|
1324
1556
|
Gibbon.Type = Type = (function(_super) {
|
1325
1557
|
__extends(Type, _super);
|
1326
1558
|
|
1327
1559
|
function Type() {
|
1328
|
-
|
1329
|
-
return
|
1560
|
+
_ref7 = Type.__super__.constructor.apply(this, arguments);
|
1561
|
+
return _ref7;
|
1330
1562
|
}
|
1331
1563
|
|
1332
|
-
Type.
|
1564
|
+
Type.variants({
|
1333
1565
|
block: ['from', 'to'],
|
1334
1566
|
pair: ['first', 'second'],
|
1335
1567
|
list: ['of'],
|
@@ -1363,17 +1595,17 @@ Gibbon.Type = Type = (function(_super) {
|
|
1363
1595
|
|
1364
1596
|
return Type;
|
1365
1597
|
|
1366
|
-
})(
|
1598
|
+
})(Variant);
|
1367
1599
|
|
1368
1600
|
Gibbon.TypeExpr = TypeExpr = (function(_super) {
|
1369
1601
|
__extends(TypeExpr, _super);
|
1370
1602
|
|
1371
1603
|
function TypeExpr() {
|
1372
|
-
|
1373
|
-
return
|
1604
|
+
_ref8 = TypeExpr.__super__.constructor.apply(this, arguments);
|
1605
|
+
return _ref8;
|
1374
1606
|
}
|
1375
1607
|
|
1376
|
-
TypeExpr.
|
1608
|
+
TypeExpr.variants({
|
1377
1609
|
expr: ['expr'],
|
1378
1610
|
variable: ['name'],
|
1379
1611
|
query: ['input', 'scope', 'query'],
|
@@ -1412,6 +1644,18 @@ Gibbon.TypeExpr = TypeExpr = (function(_super) {
|
|
1412
1644
|
expr: function(e) {
|
1413
1645
|
return "<" + (e.inspect()) + ">";
|
1414
1646
|
},
|
1647
|
+
error: function(t, args) {
|
1648
|
+
var a;
|
1649
|
+
return "?" + t + "(" + (((function() {
|
1650
|
+
var _i, _len, _results;
|
1651
|
+
_results = [];
|
1652
|
+
for (_i = 0, _len = args.length; _i < _len; _i++) {
|
1653
|
+
a = args[_i];
|
1654
|
+
_results.push(a.inspect());
|
1655
|
+
}
|
1656
|
+
return _results;
|
1657
|
+
})()).join(' ')) + ")";
|
1658
|
+
},
|
1415
1659
|
variable: function(name) {
|
1416
1660
|
return "%" + name;
|
1417
1661
|
},
|
@@ -1440,6 +1684,18 @@ Gibbon.TypeExpr = TypeExpr = (function(_super) {
|
|
1440
1684
|
})();
|
1441
1685
|
return "(" + name + " " + (exprs.join(' ')) + ")";
|
1442
1686
|
},
|
1687
|
+
error: function(t, args) {
|
1688
|
+
var a;
|
1689
|
+
return "?" + t + "(" + (((function() {
|
1690
|
+
var _i, _len, _results;
|
1691
|
+
_results = [];
|
1692
|
+
for (_i = 0, _len = args.length; _i < _len; _i++) {
|
1693
|
+
a = args[_i];
|
1694
|
+
_results.push(a.inspect());
|
1695
|
+
}
|
1696
|
+
return _results;
|
1697
|
+
})()).join(' ')) + ")";
|
1698
|
+
},
|
1443
1699
|
any: function() {
|
1444
1700
|
return '*';
|
1445
1701
|
}
|
@@ -1537,11 +1793,11 @@ Gibbon.TypeExpr = TypeExpr = (function(_super) {
|
|
1537
1793
|
return TypeExpr["native"](t.id);
|
1538
1794
|
}
|
1539
1795
|
return TypeExpr.param(t._tag, (function() {
|
1540
|
-
var _i, _len,
|
1541
|
-
|
1796
|
+
var _i, _len, _ref9, _results;
|
1797
|
+
_ref9 = t._values;
|
1542
1798
|
_results = [];
|
1543
|
-
for (_i = 0, _len =
|
1544
|
-
st =
|
1799
|
+
for (_i = 0, _len = _ref9.length; _i < _len; _i++) {
|
1800
|
+
st = _ref9[_i];
|
1545
1801
|
_results.push(this.fromType(st));
|
1546
1802
|
}
|
1547
1803
|
return _results;
|
@@ -1599,7 +1855,7 @@ Gibbon.TypeExpr = TypeExpr = (function(_super) {
|
|
1599
1855
|
|
1600
1856
|
return TypeExpr;
|
1601
1857
|
|
1602
|
-
})(
|
1858
|
+
})(Variant);
|
1603
1859
|
|
1604
1860
|
analyze = Gibbon.analyze = (function() {
|
1605
1861
|
var generate, solve;
|
@@ -1614,9 +1870,9 @@ analyze = Gibbon.analyze = (function() {
|
|
1614
1870
|
NativeContext.prototype.query = function(id, query, cb) {
|
1615
1871
|
return this.externalLookup.call(null, id, query, Type, function(err, analysis) {
|
1616
1872
|
if (err) {
|
1617
|
-
return cb(TypeLookup.error(err));
|
1873
|
+
return Thunk.trampoline(cb(TypeLookup.error(err)));
|
1618
1874
|
} else {
|
1619
|
-
return cb(TypeLookup.response(query, analysis));
|
1875
|
+
return Thunk.trampoline(cb(TypeLookup.response(query, analysis)));
|
1620
1876
|
}
|
1621
1877
|
});
|
1622
1878
|
};
|
@@ -1688,10 +1944,10 @@ analyze = Gibbon.analyze = (function() {
|
|
1688
1944
|
};
|
1689
1945
|
|
1690
1946
|
Scope.prototype.analyze = function(push) {
|
1691
|
-
var def, frame, frameScope, global, key, _i, _len,
|
1692
|
-
|
1693
|
-
for (_i = 0, _len =
|
1694
|
-
def =
|
1947
|
+
var def, frame, frameScope, global, key, _i, _len, _ref9;
|
1948
|
+
_ref9 = this.frame.definitions;
|
1949
|
+
for (_i = 0, _len = _ref9.length; _i < _len; _i++) {
|
1950
|
+
def = _ref9[_i];
|
1695
1951
|
frameScope = this.extend(def.name, def.frame);
|
1696
1952
|
frameScope.analyze(push);
|
1697
1953
|
this.bindings.set(def.name, frameScope);
|
@@ -1759,19 +2015,14 @@ analyze = Gibbon.analyze = (function() {
|
|
1759
2015
|
return _this.analyzeFlow(subFlow, global, push);
|
1760
2016
|
},
|
1761
2017
|
list: function(elements) {
|
1762
|
-
var el,
|
1763
|
-
|
1764
|
-
|
1765
|
-
return;
|
1766
|
-
}
|
1767
|
-
expected = elements[0], rest = 2 <= elements.length ? __slice.call(elements, 1) : [];
|
1768
|
-
push(TypeExpr.expr(flow), TypeExpr.param('list', [TypeExpr.expr(expected)]));
|
1769
|
-
_this.analyzeFlow(expected, global, push);
|
2018
|
+
var el, elVar, _i, _len, _results;
|
2019
|
+
elVar = TypeExpr.variable('el');
|
2020
|
+
push(TypeExpr.expr(flow), TypeExpr.param('list', [elVar]));
|
1770
2021
|
_results = [];
|
1771
|
-
for (_i = 0, _len =
|
1772
|
-
el =
|
2022
|
+
for (_i = 0, _len = elements.length; _i < _len; _i++) {
|
2023
|
+
el = elements[_i];
|
1773
2024
|
_this.analyzeFlow(el, global, push);
|
1774
|
-
_results.push(push(TypeExpr.expr(el),
|
2025
|
+
_results.push(push(TypeExpr.expr(el), elVar));
|
1775
2026
|
}
|
1776
2027
|
return _results;
|
1777
2028
|
},
|
@@ -1812,16 +2063,16 @@ analyze = Gibbon.analyze = (function() {
|
|
1812
2063
|
};
|
1813
2064
|
})();
|
1814
2065
|
solve = (function() {
|
1815
|
-
var TypeError, consume,
|
2066
|
+
var TypeError, consume, _ref9;
|
1816
2067
|
TypeError = (function(_super) {
|
1817
2068
|
__extends(TypeError, _super);
|
1818
2069
|
|
1819
2070
|
function TypeError() {
|
1820
|
-
|
1821
|
-
return
|
2071
|
+
_ref9 = TypeError.__super__.constructor.apply(this, arguments);
|
2072
|
+
return _ref9;
|
1822
2073
|
}
|
1823
2074
|
|
1824
|
-
TypeError.
|
2075
|
+
TypeError.variants({
|
1825
2076
|
match: ['lhs', 'rhs'],
|
1826
2077
|
destructure: ['type'],
|
1827
2078
|
lookup: ['query', 'id', 'error'],
|
@@ -1829,9 +2080,29 @@ analyze = Gibbon.analyze = (function() {
|
|
1829
2080
|
func: ['node']
|
1830
2081
|
});
|
1831
2082
|
|
2083
|
+
TypeError.prototype.inspect = function() {
|
2084
|
+
return this.cases({
|
2085
|
+
match: function(lhs, rhs) {
|
2086
|
+
return "could not match " + (lhs.inspect()) + " with " + (rhs.inspect());
|
2087
|
+
},
|
2088
|
+
destructure: function(type) {
|
2089
|
+
return "failure to destructure " + (type.inspect());
|
2090
|
+
},
|
2091
|
+
lookup: function(query, id, error) {
|
2092
|
+
return "error looking up " + (query.inspect()) + " on " + id + ": " + error;
|
2093
|
+
},
|
2094
|
+
circular: function(crumbs) {
|
2095
|
+
return "circular reference: " + (crumbs.join(' -> '));
|
2096
|
+
},
|
2097
|
+
func: function(node) {
|
2098
|
+
return "no such function " + node.name + " (in " + (node.inspect()) + ")";
|
2099
|
+
}
|
2100
|
+
});
|
2101
|
+
};
|
2102
|
+
|
1832
2103
|
return TypeError;
|
1833
2104
|
|
1834
|
-
})(
|
2105
|
+
})(Variant);
|
1835
2106
|
DEBUG.logConstraint = function(prefix, lhs, rhs) {
|
1836
2107
|
return console.log(prefix, lhs.inspect(), '=', rhs.inspect());
|
1837
2108
|
};
|
@@ -1841,14 +2112,16 @@ analyze = Gibbon.analyze = (function() {
|
|
1841
2112
|
return array.push([x, y]);
|
1842
2113
|
};
|
1843
2114
|
loop_ = function() {
|
1844
|
-
var lhs, rhs,
|
2115
|
+
var lhs, rhs, _ref10;
|
1845
2116
|
if (!(array.length > 0)) {
|
1846
2117
|
return next();
|
1847
2118
|
}
|
1848
|
-
|
1849
|
-
return body(lhs, rhs, push,
|
2119
|
+
_ref10 = array.pop(), lhs = _ref10[0], rhs = _ref10[1];
|
2120
|
+
return body(lhs, rhs, push, function() {
|
2121
|
+
return new Thunk(loop_);
|
2122
|
+
});
|
1850
2123
|
};
|
1851
|
-
return loop_();
|
2124
|
+
return Thunk.trampoline(loop_());
|
1852
2125
|
};
|
1853
2126
|
return function(constraintMap, finish) {
|
1854
2127
|
var errors, frameTypes, initialCrumbs, k, locks, semantics, solutions, solveEntry;
|
@@ -1860,22 +2133,22 @@ analyze = Gibbon.analyze = (function() {
|
|
1860
2133
|
frameTypes = new Hash;
|
1861
2134
|
locks = new Hash;
|
1862
2135
|
solveEntry = function(crumbs, solved) {
|
1863
|
-
var constraints, dependencies, done, error, frame, fullSubstitute, key, nextCrumbs, semanticAccessors, simplify, substitute,
|
2136
|
+
var constraints, dependencies, done, error, frame, fullSubstitute, key, nextCrumbs, semanticAccessors, simplify, substitute, _ref10;
|
1864
2137
|
key = crumbs[crumbs.length - 1];
|
1865
2138
|
if (semantics.has(key)) {
|
1866
2139
|
return solved();
|
1867
2140
|
}
|
1868
2141
|
nextCrumbs = crumbs.concat([key]);
|
1869
|
-
|
2142
|
+
_ref10 = constraintMap.get(key), frame = _ref10.frame, constraints = _ref10.constraints;
|
1870
2143
|
DEBUG.log('locking', key);
|
1871
2144
|
locks.set(key, true);
|
1872
2145
|
semanticAccessors = new CompMap;
|
1873
2146
|
dependencies = [];
|
1874
2147
|
DEBUG(function() {
|
1875
|
-
var lhs, rhs, _i, _len,
|
2148
|
+
var lhs, rhs, _i, _len, _ref11, _results;
|
1876
2149
|
_results = [];
|
1877
2150
|
for (_i = 0, _len = constraints.length; _i < _len; _i++) {
|
1878
|
-
|
2151
|
+
_ref11 = constraints[_i], lhs = _ref11[0], rhs = _ref11[1];
|
1879
2152
|
_results.push(DEBUG.logConstraint('-> ', lhs, rhs));
|
1880
2153
|
}
|
1881
2154
|
return _results;
|
@@ -1948,8 +2221,12 @@ analyze = Gibbon.analyze = (function() {
|
|
1948
2221
|
return cb(error('circular', nextCrumbs));
|
1949
2222
|
}
|
1950
2223
|
localFrame = constraintMap.get(key).frame;
|
1951
|
-
return
|
1952
|
-
return
|
2224
|
+
return new Thunk(function() {
|
2225
|
+
return solveEntry(nextCrumbs, function() {
|
2226
|
+
return new Thunk(function() {
|
2227
|
+
return cb(frameTypes.get(key));
|
2228
|
+
});
|
2229
|
+
});
|
1953
2230
|
});
|
1954
2231
|
}
|
1955
2232
|
});
|
@@ -1986,12 +2263,9 @@ analyze = Gibbon.analyze = (function() {
|
|
1986
2263
|
return Semantic.flow(flowType(TypeExpr.expr(this)), toSemanticTree(head), tail && toSemanticTree(tail));
|
1987
2264
|
},
|
1988
2265
|
query: function(type, name) {
|
1989
|
-
|
1990
|
-
|
1991
|
-
|
1992
|
-
debugger;
|
1993
|
-
}
|
1994
|
-
});
|
2266
|
+
if (!(errors.length || semanticAccessors.has(this))) {
|
2267
|
+
throw "unsolved with no errors!";
|
2268
|
+
}
|
1995
2269
|
return semanticAccessors.get(this);
|
1996
2270
|
},
|
1997
2271
|
func: function(name, args) {
|
@@ -2136,22 +2410,22 @@ analyze = Gibbon.analyze = (function() {
|
|
2136
2410
|
param: function() {
|
2137
2411
|
return rhs.cases({
|
2138
2412
|
param: function() {
|
2139
|
-
var constraint, i, _i, _len,
|
2413
|
+
var constraint, i, _i, _len, _ref11;
|
2140
2414
|
if (lhs.name !== rhs.name) {
|
2141
2415
|
return matchError();
|
2142
2416
|
}
|
2143
|
-
|
2144
|
-
for (i = _i = 0, _len =
|
2145
|
-
constraint =
|
2417
|
+
_ref11 = lhs.constraints;
|
2418
|
+
for (i = _i = 0, _len = _ref11.length; _i < _len; i = ++_i) {
|
2419
|
+
constraint = _ref11[i];
|
2146
2420
|
push(constraint, rhs.constraints[i]);
|
2147
2421
|
}
|
2148
2422
|
return next();
|
2149
2423
|
},
|
2150
2424
|
query: function() {
|
2151
|
-
var c, i, _i, _len,
|
2152
|
-
|
2153
|
-
for (i = _i = 0, _len =
|
2154
|
-
c =
|
2425
|
+
var c, i, _i, _len, _ref11;
|
2426
|
+
_ref11 = lhs.constraints;
|
2427
|
+
for (i = _i = 0, _len = _ref11.length; _i < _len; i = ++_i) {
|
2428
|
+
c = _ref11[i];
|
2155
2429
|
push(c, TypeExpr.destructure(rhs, lhs.type(i)));
|
2156
2430
|
}
|
2157
2431
|
return next();
|
@@ -2166,11 +2440,11 @@ analyze = Gibbon.analyze = (function() {
|
|
2166
2440
|
});
|
2167
2441
|
};
|
2168
2442
|
initialCrumbs = (function() {
|
2169
|
-
var _i, _len,
|
2170
|
-
|
2443
|
+
var _i, _len, _ref10, _results;
|
2444
|
+
_ref10 = constraintMap.keys();
|
2171
2445
|
_results = [];
|
2172
|
-
for (_i = 0, _len =
|
2173
|
-
k =
|
2446
|
+
for (_i = 0, _len = _ref10.length; _i < _len; _i++) {
|
2447
|
+
k = _ref10[_i];
|
2174
2448
|
_results.push([k]);
|
2175
2449
|
}
|
2176
2450
|
return _results;
|
@@ -2193,20 +2467,16 @@ analyze = Gibbon.analyze = (function() {
|
|
2193
2467
|
return solve(constraints, cb);
|
2194
2468
|
};
|
2195
2469
|
})();
|
2196
|
-
// Generated by CoffeeScript 1.6.3
|
2197
|
-
var CachingPromise, Dependency, Failure, Promise, Thunk, UnitPromise, Value, eval_, _ref, _ref1, _ref2, _ref3,
|
2198
|
-
__hasProp = {}.hasOwnProperty,
|
2199
|
-
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
|
2200
2470
|
|
2201
2471
|
Value = Gibbon.Value = Value = (function(_super) {
|
2202
2472
|
__extends(Value, _super);
|
2203
2473
|
|
2204
2474
|
function Value() {
|
2205
|
-
|
2206
|
-
return
|
2475
|
+
_ref9 = Value.__super__.constructor.apply(this, arguments);
|
2476
|
+
return _ref9;
|
2207
2477
|
}
|
2208
2478
|
|
2209
|
-
Value.
|
2479
|
+
Value.variants({
|
2210
2480
|
string: ['value'],
|
2211
2481
|
number: ['value'],
|
2212
2482
|
boolean: ['value'],
|
@@ -2247,6 +2517,121 @@ Value = Gibbon.Value = Value = (function(_super) {
|
|
2247
2517
|
throw new Error('invalid value: ' + o);
|
2248
2518
|
};
|
2249
2519
|
|
2520
|
+
Value.interpret = function(o, type) {
|
2521
|
+
var _this = this;
|
2522
|
+
return type.cases({
|
2523
|
+
pair: function(first, second) {
|
2524
|
+
if (!('first' in o && 'second' in o)) {
|
2525
|
+
throw 'bad value';
|
2526
|
+
}
|
2527
|
+
return Value.pair(_this.interpret(o.first, first), _this.interpret(o.second, second));
|
2528
|
+
},
|
2529
|
+
list: function(listOf) {
|
2530
|
+
var e;
|
2531
|
+
if (!isArray(o)) {
|
2532
|
+
throw 'bad value';
|
2533
|
+
}
|
2534
|
+
return Value.list((function() {
|
2535
|
+
var _i, _len, _results;
|
2536
|
+
_results = [];
|
2537
|
+
for (_i = 0, _len = o.length; _i < _len; _i++) {
|
2538
|
+
e = o[_i];
|
2539
|
+
_results.push(this.interpret(e, listOf));
|
2540
|
+
}
|
2541
|
+
return _results;
|
2542
|
+
}).call(_this));
|
2543
|
+
},
|
2544
|
+
entity: function(type) {
|
2545
|
+
if (typeof o !== 'number') {
|
2546
|
+
throw 'bad value';
|
2547
|
+
}
|
2548
|
+
return Value.entity(type, o);
|
2549
|
+
},
|
2550
|
+
numeric: function() {
|
2551
|
+
if (typeof o !== 'number') {
|
2552
|
+
throw 'bad value';
|
2553
|
+
}
|
2554
|
+
return Value.number(o);
|
2555
|
+
},
|
2556
|
+
string: function() {
|
2557
|
+
if (typeof o !== 'string') {
|
2558
|
+
throw 'bad value';
|
2559
|
+
}
|
2560
|
+
return Value.string(o);
|
2561
|
+
},
|
2562
|
+
bool: function() {
|
2563
|
+
if (typeof o !== 'boolean') {
|
2564
|
+
throw 'bad value';
|
2565
|
+
}
|
2566
|
+
return Value.boolean(o);
|
2567
|
+
},
|
2568
|
+
other: function() {
|
2569
|
+
throw "could not return object of type " + (type.inspect());
|
2570
|
+
}
|
2571
|
+
});
|
2572
|
+
};
|
2573
|
+
|
2574
|
+
Value.prototype.asPrimitive = function() {
|
2575
|
+
return this.cases({
|
2576
|
+
string: function(v) {
|
2577
|
+
return v;
|
2578
|
+
},
|
2579
|
+
number: function(v) {
|
2580
|
+
return v;
|
2581
|
+
},
|
2582
|
+
boolean: function(v) {
|
2583
|
+
return v;
|
2584
|
+
},
|
2585
|
+
block: function(v) {
|
2586
|
+
return v;
|
2587
|
+
},
|
2588
|
+
list: function(els) {
|
2589
|
+
var e, _i, _len, _results;
|
2590
|
+
_results = [];
|
2591
|
+
for (_i = 0, _len = els.length; _i < _len; _i++) {
|
2592
|
+
e = els[_i];
|
2593
|
+
_results.push(e.asPrimitive());
|
2594
|
+
}
|
2595
|
+
return _results;
|
2596
|
+
},
|
2597
|
+
pair: function(first, second) {
|
2598
|
+
return {
|
2599
|
+
first: first.asPrimitive(),
|
2600
|
+
second: second.asPrimitive()
|
2601
|
+
};
|
2602
|
+
},
|
2603
|
+
entity: function(_, id) {
|
2604
|
+
return id;
|
2605
|
+
}
|
2606
|
+
});
|
2607
|
+
};
|
2608
|
+
|
2609
|
+
Value.prototype.inspect = function() {
|
2610
|
+
return this.cases({
|
2611
|
+
list: function(els) {
|
2612
|
+
var e;
|
2613
|
+
return "(list " + (((function() {
|
2614
|
+
var _i, _len, _results;
|
2615
|
+
_results = [];
|
2616
|
+
for (_i = 0, _len = els.length; _i < _len; _i++) {
|
2617
|
+
e = els[_i];
|
2618
|
+
_results.push(e.inspect());
|
2619
|
+
}
|
2620
|
+
return _results;
|
2621
|
+
})()).join(' ')) + ")";
|
2622
|
+
},
|
2623
|
+
pair: function(first, second) {
|
2624
|
+
return "(pair " + (first.inspect()) + " " + (second.inspect()) + ")";
|
2625
|
+
},
|
2626
|
+
entity: function(id) {
|
2627
|
+
return "(entity " + id + ")";
|
2628
|
+
},
|
2629
|
+
other: function() {
|
2630
|
+
return '' + this.asPrimitive();
|
2631
|
+
}
|
2632
|
+
});
|
2633
|
+
};
|
2634
|
+
|
2250
2635
|
Value.prototype.promise = function() {
|
2251
2636
|
return Promise.unit(this.map(function(x) {
|
2252
2637
|
return x.promise();
|
@@ -2346,23 +2731,7 @@ Value = Gibbon.Value = Value = (function(_super) {
|
|
2346
2731
|
|
2347
2732
|
return Value;
|
2348
2733
|
|
2349
|
-
})(
|
2350
|
-
|
2351
|
-
Thunk = (function() {
|
2352
|
-
Thunk.trampoline = function(t) {
|
2353
|
-
while (t instanceof Thunk) {
|
2354
|
-
t = t.run();
|
2355
|
-
}
|
2356
|
-
return t;
|
2357
|
-
};
|
2358
|
-
|
2359
|
-
function Thunk(run) {
|
2360
|
-
this.run = run;
|
2361
|
-
}
|
2362
|
-
|
2363
|
-
return Thunk;
|
2364
|
-
|
2365
|
-
})();
|
2734
|
+
})(Variant);
|
2366
2735
|
|
2367
2736
|
Promise = (function() {
|
2368
2737
|
function Promise(forceRaw) {
|
@@ -2541,8 +2910,8 @@ UnitPromise = (function(_super) {
|
|
2541
2910
|
__extends(UnitPromise, _super);
|
2542
2911
|
|
2543
2912
|
function UnitPromise() {
|
2544
|
-
|
2545
|
-
return
|
2913
|
+
_ref10 = UnitPromise.__super__.constructor.apply(this, arguments);
|
2914
|
+
return _ref10;
|
2546
2915
|
}
|
2547
2916
|
|
2548
2917
|
UnitPromise.prototype.cache = function() {
|
@@ -2592,11 +2961,11 @@ Gibbon.Failure = Failure = (function(_super) {
|
|
2592
2961
|
__extends(Failure, _super);
|
2593
2962
|
|
2594
2963
|
function Failure() {
|
2595
|
-
|
2596
|
-
return
|
2964
|
+
_ref11 = Failure.__super__.constructor.apply(this, arguments);
|
2965
|
+
return _ref11;
|
2597
2966
|
}
|
2598
2967
|
|
2599
|
-
Failure.
|
2968
|
+
Failure.variants({
|
2600
2969
|
query: ['id', 'annotations'],
|
2601
2970
|
composite: ['failures'],
|
2602
2971
|
message: ['message']
|
@@ -2633,17 +3002,17 @@ Gibbon.Failure = Failure = (function(_super) {
|
|
2633
3002
|
|
2634
3003
|
return Failure;
|
2635
3004
|
|
2636
|
-
})(
|
3005
|
+
})(Variant);
|
2637
3006
|
|
2638
3007
|
Gibbon.Dependency = Dependency = (function(_super) {
|
2639
3008
|
__extends(Dependency, _super);
|
2640
3009
|
|
2641
3010
|
function Dependency() {
|
2642
|
-
|
2643
|
-
return
|
3011
|
+
_ref12 = Dependency.__super__.constructor.apply(this, arguments);
|
3012
|
+
return _ref12;
|
2644
3013
|
}
|
2645
3014
|
|
2646
|
-
Dependency.
|
3015
|
+
Dependency.variants({
|
2647
3016
|
query: ['entity', 'query'],
|
2648
3017
|
lexical: ['key'],
|
2649
3018
|
failure: ['failure']
|
@@ -2658,7 +3027,7 @@ Gibbon.Dependency = Dependency = (function(_super) {
|
|
2658
3027
|
if (entity.id !== other.entity.id) {
|
2659
3028
|
return false;
|
2660
3029
|
}
|
2661
|
-
if (query
|
3030
|
+
if (JSON.stringify(query) !== JSON.stringify(other.query)) {
|
2662
3031
|
return false;
|
2663
3032
|
}
|
2664
3033
|
return true;
|
@@ -2674,8 +3043,8 @@ Gibbon.Dependency = Dependency = (function(_super) {
|
|
2674
3043
|
|
2675
3044
|
Dependency.prototype.inspect = function() {
|
2676
3045
|
return this.cases({
|
2677
|
-
|
2678
|
-
return "" + (
|
3046
|
+
query: function(entity, query) {
|
3047
|
+
return "" + (JSON.stringify(query)) + "<" + entity + ">";
|
2679
3048
|
},
|
2680
3049
|
lexical: function(definition) {
|
2681
3050
|
return "@" + definition.name;
|
@@ -2688,7 +3057,7 @@ Gibbon.Dependency = Dependency = (function(_super) {
|
|
2688
3057
|
|
2689
3058
|
return Dependency;
|
2690
3059
|
|
2691
|
-
})(
|
3060
|
+
})(Variant);
|
2692
3061
|
|
2693
3062
|
eval_ = Gibbon["eval"] = (function() {
|
2694
3063
|
var Context, Scope, combine, unit, vFalse, vTrue;
|
@@ -2707,10 +3076,10 @@ eval_ = Gibbon["eval"] = (function() {
|
|
2707
3076
|
Context.prototype.evalAll = function() {
|
2708
3077
|
var results, toForce,
|
2709
3078
|
_this = this;
|
2710
|
-
this.definitions.each(function(key,
|
3079
|
+
this.definitions.each(function(key, expr) {
|
2711
3080
|
var lazy;
|
2712
3081
|
lazy = Promise.lazy(function() {
|
2713
|
-
return _this.rootScope["eval"](
|
3082
|
+
return _this.rootScope["eval"](expr).then(function(x) {
|
2714
3083
|
return x.resolve();
|
2715
3084
|
});
|
2716
3085
|
});
|
@@ -2730,8 +3099,8 @@ eval_ = Gibbon["eval"] = (function() {
|
|
2730
3099
|
}));
|
2731
3100
|
});
|
2732
3101
|
return combine(toForce).then(function() {
|
2733
|
-
var deps, val,
|
2734
|
-
|
3102
|
+
var deps, val, _ref13;
|
3103
|
+
_ref13 = results.get('/'), val = _ref13[0], deps = _ref13[1];
|
2735
3104
|
if (val) {
|
2736
3105
|
return Promise.unit(results);
|
2737
3106
|
} else {
|
@@ -2791,10 +3160,10 @@ eval_ = Gibbon["eval"] = (function() {
|
|
2791
3160
|
};
|
2792
3161
|
|
2793
3162
|
Scope.prototype["let"] = function(bindings) {
|
2794
|
-
var ext, k, v, _i, _len,
|
3163
|
+
var ext, k, v, _i, _len, _ref13;
|
2795
3164
|
ext = new Hash;
|
2796
3165
|
for (_i = 0, _len = bindings.length; _i < _len; _i++) {
|
2797
|
-
|
3166
|
+
_ref13 = bindings[_i], k = _ref13[0], v = _ref13[1];
|
2798
3167
|
ext.set(k, v.cache());
|
2799
3168
|
}
|
2800
3169
|
return this.extend(ext);
|
@@ -2850,9 +3219,9 @@ eval_ = Gibbon["eval"] = (function() {
|
|
2850
3219
|
return this.get(varName);
|
2851
3220
|
};
|
2852
3221
|
|
2853
|
-
Scope.prototype["eval"] = function(
|
3222
|
+
Scope.prototype["eval"] = function(expr) {
|
2854
3223
|
var _this = this;
|
2855
|
-
return
|
3224
|
+
return expr.cases({
|
2856
3225
|
global: function() {
|
2857
3226
|
return _this.get('@');
|
2858
3227
|
},
|
@@ -2872,16 +3241,14 @@ eval_ = Gibbon["eval"] = (function() {
|
|
2872
3241
|
});
|
2873
3242
|
},
|
2874
3243
|
bind: function(name, val, expr) {
|
2875
|
-
return _this["eval"](val)
|
2876
|
-
return _this["let"]([[name, v]])["eval"](expr);
|
2877
|
-
});
|
3244
|
+
return _this["let"]([[name, _this["eval"](val)]])["eval"](expr);
|
2878
3245
|
},
|
2879
3246
|
delist: function(expr, index) {
|
2880
3247
|
var evaled;
|
2881
3248
|
evaled = [_this["eval"](expr), _this["eval"](index)];
|
2882
3249
|
return combine(evaled).then(function(_arg) {
|
2883
|
-
var elements, value,
|
2884
|
-
(
|
3250
|
+
var elements, value, _ref13, _ref14;
|
3251
|
+
(_ref13 = _arg[0], elements = _ref13.elements), (_ref14 = _arg[1], value = _ref14.value);
|
2885
3252
|
return elements[value];
|
2886
3253
|
});
|
2887
3254
|
},
|
@@ -2910,7 +3277,13 @@ eval_ = Gibbon["eval"] = (function() {
|
|
2910
3277
|
if (i >= elements.length) {
|
2911
3278
|
return _this["eval"](out);
|
2912
3279
|
}
|
2913
|
-
return _this["let"]([
|
3280
|
+
return _this["let"]([
|
3281
|
+
[idxArg, unit(Value.number(i))], [arg, elements[i]], [
|
3282
|
+
accumArg, Promise.lazy(function() {
|
3283
|
+
return _loop(i + 1);
|
3284
|
+
})
|
3285
|
+
]
|
3286
|
+
])["eval"](body);
|
2914
3287
|
};
|
2915
3288
|
return _loop(0);
|
2916
3289
|
});
|
@@ -2938,11 +3311,11 @@ eval_ = Gibbon["eval"] = (function() {
|
|
2938
3311
|
var el, first, i, pairs, second;
|
2939
3312
|
first = _arg[0], second = _arg[1];
|
2940
3313
|
pairs = (function() {
|
2941
|
-
var _i, _len,
|
2942
|
-
|
3314
|
+
var _i, _len, _ref13, _results;
|
3315
|
+
_ref13 = first.elements;
|
2943
3316
|
_results = [];
|
2944
|
-
for (i = _i = 0, _len =
|
2945
|
-
el =
|
3317
|
+
for (i = _i = 0, _len = _ref13.length; _i < _len; i = ++_i) {
|
3318
|
+
el = _ref13[i];
|
2946
3319
|
_results.push(Value.pair(el, second.elements[i]));
|
2947
3320
|
}
|
2948
3321
|
return _results;
|
@@ -2989,14 +3362,25 @@ eval_ = Gibbon["eval"] = (function() {
|
|
2989
3362
|
promise = elements[_i];
|
2990
3363
|
_results.push(promise.then(function(e) {
|
2991
3364
|
return e.resolve().map(function(e) {
|
2992
|
-
return
|
3365
|
+
return e.promise();
|
2993
3366
|
});
|
2994
|
-
}).or(unit(
|
3367
|
+
}).or(unit(null)));
|
2995
3368
|
}
|
2996
3369
|
return _results;
|
2997
3370
|
}).call(_this);
|
2998
|
-
return combine(wrapped).map(function(
|
2999
|
-
|
3371
|
+
return combine(wrapped).map(function(withNulls) {
|
3372
|
+
var e;
|
3373
|
+
return Value.list((function() {
|
3374
|
+
var _i, _len, _results;
|
3375
|
+
_results = [];
|
3376
|
+
for (_i = 0, _len = withNulls.length; _i < _len; _i++) {
|
3377
|
+
e = withNulls[_i];
|
3378
|
+
if (e) {
|
3379
|
+
_results.push(e);
|
3380
|
+
}
|
3381
|
+
}
|
3382
|
+
return _results;
|
3383
|
+
})());
|
3000
3384
|
});
|
3001
3385
|
});
|
3002
3386
|
},
|
@@ -3032,18 +3416,18 @@ eval_ = Gibbon["eval"] = (function() {
|
|
3032
3416
|
fail: function(message) {
|
3033
3417
|
return Promise.fail(Failure.message(message));
|
3034
3418
|
},
|
3035
|
-
|
3419
|
+
op1: function(name, value) {
|
3420
|
+
return _this["eval"](value).map(function(v) {
|
3421
|
+
return EXTERN[name](v.value);
|
3422
|
+
});
|
3423
|
+
},
|
3424
|
+
op2: function(op, lhs, rhs) {
|
3036
3425
|
return combine([_this["eval"](lhs), _this["eval"](rhs)]).map(function(_arg) {
|
3037
3426
|
var l, r;
|
3038
3427
|
l = _arg[0], r = _arg[1];
|
3039
3428
|
return OPERATORS[op](l.value, r.value);
|
3040
3429
|
});
|
3041
3430
|
},
|
3042
|
-
extern: function(name, value) {
|
3043
|
-
return _this["eval"](value).map(function(v) {
|
3044
|
-
return EXTERN[name](v.value);
|
3045
|
-
});
|
3046
|
-
},
|
3047
3431
|
rescue: function(expr, default_) {
|
3048
3432
|
return _this["eval"](expr).or(_this["eval"](default_));
|
3049
3433
|
}
|
@@ -3054,46 +3438,25 @@ eval_ = Gibbon["eval"] = (function() {
|
|
3054
3438
|
|
3055
3439
|
})();
|
3056
3440
|
return function(semantics, table, id, client, finish) {
|
3057
|
-
|
3058
|
-
optimize = Gibbon.optimize, compile = Gibbon.compile;
|
3059
|
-
allCompiled = new Hash;
|
3060
|
-
semantics.each(function(key, semantic) {
|
3061
|
-
var compiled;
|
3062
|
-
compiled = optimize(compile(semantic));
|
3063
|
-
return allCompiled.set(key, compiled);
|
3064
|
-
});
|
3065
|
-
entity = Value.entity(table, id);
|
3066
|
-
onFailure = function(fail) {
|
3067
|
-
return finish(fail);
|
3068
|
-
};
|
3069
|
-
onSuccess = function(vals) {
|
3070
|
-
DEBUG.log("final stack size: " + (DEBUG.getStackSize()));
|
3071
|
-
return finish(null, vals);
|
3072
|
-
};
|
3073
|
-
return new Context(client, entity, allCompiled).evalAll().force(onFailure, onSuccess);
|
3441
|
+
return Gibbon.compile(semantics).run('/', id, client, finish);
|
3074
3442
|
};
|
3075
3443
|
})();
|
3076
|
-
// Generated by CoffeeScript 1.6.3
|
3077
|
-
var IR, _ref,
|
3078
|
-
__hasProp = {}.hasOwnProperty,
|
3079
|
-
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
|
3080
3444
|
|
3081
|
-
Gibbon.
|
3445
|
+
Gibbon.Core = Core = (function(_super) {
|
3082
3446
|
var nameGen;
|
3083
3447
|
|
3084
|
-
__extends(
|
3448
|
+
__extends(Core, _super);
|
3085
3449
|
|
3086
|
-
function
|
3087
|
-
|
3088
|
-
return
|
3450
|
+
function Core() {
|
3451
|
+
_ref13 = Core.__super__.constructor.apply(this, arguments);
|
3452
|
+
return _ref13;
|
3089
3453
|
}
|
3090
3454
|
|
3091
|
-
|
3455
|
+
Core.variants({
|
3092
3456
|
global: [],
|
3093
3457
|
constant: ['value'],
|
3094
3458
|
variable: ['name'],
|
3095
3459
|
branch: ['cond', 'ifTrue', 'ifFalse'],
|
3096
|
-
bind: ['name', 'value', 'expr'],
|
3097
3460
|
delist: ['expr', 'index'],
|
3098
3461
|
depair: ['expr', 'key'],
|
3099
3462
|
list: ['elements'],
|
@@ -3109,52 +3472,58 @@ Gibbon.IR = IR = (function(_super) {
|
|
3109
3472
|
query: ['expr', 'annotations'],
|
3110
3473
|
localQuery: ['key'],
|
3111
3474
|
fail: ['message'],
|
3112
|
-
|
3113
|
-
|
3114
|
-
rescue: ['expr', 'default']
|
3475
|
+
op1: ['op', 'arg'],
|
3476
|
+
op2: ['op', 'lhs', 'rhs'],
|
3477
|
+
rescue: ['expr', 'default'],
|
3478
|
+
next: ['cont', 'args'],
|
3479
|
+
bind: ['name', 'value', 'expr']
|
3115
3480
|
});
|
3116
3481
|
|
3117
|
-
|
3118
|
-
return
|
3482
|
+
Core.makeVariable = function(name) {
|
3483
|
+
return Core.variable(nameGen(name));
|
3484
|
+
};
|
3485
|
+
|
3486
|
+
Core.prototype.branch = function(ifTrue, ifFalse) {
|
3487
|
+
return Core.branch(this, ifTrue, ifFalse);
|
3119
3488
|
};
|
3120
3489
|
|
3121
|
-
|
3122
|
-
return
|
3490
|
+
Core.prototype.delist = function(index) {
|
3491
|
+
return Core.delist(this, index);
|
3123
3492
|
};
|
3124
3493
|
|
3125
|
-
|
3126
|
-
return
|
3494
|
+
Core.prototype.depair = function(key) {
|
3495
|
+
return Core.depair(this, key);
|
3127
3496
|
};
|
3128
3497
|
|
3129
|
-
|
3130
|
-
return
|
3498
|
+
Core.prototype.query = function(annotations) {
|
3499
|
+
return Core.query(this, annotations);
|
3131
3500
|
};
|
3132
3501
|
|
3133
|
-
|
3134
|
-
return
|
3502
|
+
Core.prototype.op1 = function(op) {
|
3503
|
+
return Core.op1(op, this);
|
3135
3504
|
};
|
3136
3505
|
|
3137
|
-
|
3138
|
-
return
|
3506
|
+
Core.prototype.op2 = function(op, other) {
|
3507
|
+
return Core.op2(op, this, other);
|
3139
3508
|
};
|
3140
3509
|
|
3141
|
-
|
3142
|
-
return
|
3510
|
+
Core.prototype.app = function(arg) {
|
3511
|
+
return Core.app(this, arg);
|
3143
3512
|
};
|
3144
3513
|
|
3145
|
-
|
3146
|
-
return
|
3514
|
+
Core.prototype.squish = function() {
|
3515
|
+
return Core.squishList(this);
|
3147
3516
|
};
|
3148
3517
|
|
3149
|
-
|
3150
|
-
return
|
3518
|
+
Core.prototype.len = function() {
|
3519
|
+
return Core.len(this);
|
3151
3520
|
};
|
3152
3521
|
|
3153
|
-
|
3154
|
-
return
|
3522
|
+
Core.prototype.rescue = function(alt) {
|
3523
|
+
return Core.rescue(this, alt);
|
3155
3524
|
};
|
3156
3525
|
|
3157
|
-
|
3526
|
+
Core.prototype.isAsync = function() {
|
3158
3527
|
return this.cases({
|
3159
3528
|
query: function() {
|
3160
3529
|
return true;
|
@@ -3163,10 +3532,10 @@ Gibbon.IR = IR = (function(_super) {
|
|
3163
3532
|
return true;
|
3164
3533
|
},
|
3165
3534
|
other: function() {
|
3166
|
-
var sub, _i, _len,
|
3167
|
-
|
3168
|
-
for (_i = 0, _len =
|
3169
|
-
sub =
|
3535
|
+
var sub, _i, _len, _ref14;
|
3536
|
+
_ref14 = this.subtrees();
|
3537
|
+
for (_i = 0, _len = _ref14.length; _i < _len; _i++) {
|
3538
|
+
sub = _ref14[_i];
|
3170
3539
|
if (sub.isAsync()) {
|
3171
3540
|
return true;
|
3172
3541
|
}
|
@@ -3176,7 +3545,7 @@ Gibbon.IR = IR = (function(_super) {
|
|
3176
3545
|
});
|
3177
3546
|
};
|
3178
3547
|
|
3179
|
-
|
3548
|
+
Core.prototype.isSimple = function() {
|
3180
3549
|
return this.cases({
|
3181
3550
|
variable: function() {
|
3182
3551
|
return true;
|
@@ -3187,50 +3556,125 @@ Gibbon.IR = IR = (function(_super) {
|
|
3187
3556
|
global: function() {
|
3188
3557
|
return true;
|
3189
3558
|
},
|
3190
|
-
|
3191
|
-
return
|
3559
|
+
other: function() {
|
3560
|
+
return false;
|
3561
|
+
}
|
3562
|
+
});
|
3563
|
+
};
|
3564
|
+
|
3565
|
+
Core.prototype.alwaysSucceeds = function() {
|
3566
|
+
return this.cases({
|
3567
|
+
query: function() {
|
3568
|
+
return false;
|
3569
|
+
},
|
3570
|
+
localQuery: function() {
|
3571
|
+
return false;
|
3572
|
+
},
|
3573
|
+
fail: function() {
|
3574
|
+
return false;
|
3575
|
+
},
|
3576
|
+
rescue: function(e, default_) {
|
3577
|
+
return e.alwaysSucceeds() || default_.alwaysSucceeds();
|
3578
|
+
},
|
3579
|
+
squishList: function() {
|
3580
|
+
return true;
|
3581
|
+
},
|
3582
|
+
other: function() {
|
3583
|
+
var subtree, _i, _len, _ref14;
|
3584
|
+
_ref14 = this.subtrees();
|
3585
|
+
for (_i = 0, _len = _ref14.length; _i < _len; _i++) {
|
3586
|
+
subtree = _ref14[_i];
|
3587
|
+
console.log("checking alwaysSucceeds " + (subtree.inspect()));
|
3588
|
+
if (!subtree.alwaysSucceeds()) {
|
3589
|
+
return false;
|
3590
|
+
}
|
3591
|
+
}
|
3592
|
+
return true;
|
3593
|
+
}
|
3594
|
+
});
|
3595
|
+
};
|
3596
|
+
|
3597
|
+
Core.prototype.alwaysFails = function() {
|
3598
|
+
return this.cases({
|
3599
|
+
query: function() {
|
3600
|
+
return false;
|
3601
|
+
},
|
3602
|
+
localQuery: function() {
|
3603
|
+
return false;
|
3604
|
+
},
|
3605
|
+
fail: function() {
|
3606
|
+
return true;
|
3607
|
+
},
|
3608
|
+
rescue: function(e, default_) {
|
3609
|
+
return e.alwaysFails() && default_.alwaysFails();
|
3192
3610
|
},
|
3193
|
-
|
3194
|
-
return
|
3611
|
+
squishList: function() {
|
3612
|
+
return false;
|
3195
3613
|
},
|
3196
3614
|
other: function() {
|
3615
|
+
var subtree, _i, _len, _ref14;
|
3616
|
+
_ref14 = this.subtrees();
|
3617
|
+
for (_i = 0, _len = _ref14.length; _i < _len; _i++) {
|
3618
|
+
subtree = _ref14[_i];
|
3619
|
+
console.log("checking alwaysFails " + (subtree.inspect()));
|
3620
|
+
if (subtree.alwaysFails()) {
|
3621
|
+
return true;
|
3622
|
+
}
|
3623
|
+
}
|
3197
3624
|
return false;
|
3198
3625
|
}
|
3199
3626
|
});
|
3200
3627
|
};
|
3201
3628
|
|
3202
|
-
|
3629
|
+
Core.prototype.seq = function(name, f) {
|
3203
3630
|
name = nameGen(name);
|
3204
|
-
return
|
3631
|
+
return Core.bind(name, this, f(Core.variable(name)));
|
3205
3632
|
};
|
3206
3633
|
|
3207
|
-
|
3634
|
+
Core.prototype.mapList = function(f) {
|
3208
3635
|
var elName, ixName;
|
3209
3636
|
elName = nameGen('el');
|
3210
3637
|
ixName = nameGen('i');
|
3211
|
-
return
|
3638
|
+
return Core.mapList(this, elName, ixName, f(Core.variable(elName), Core.variable(ixName)));
|
3212
3639
|
};
|
3213
3640
|
|
3214
|
-
|
3641
|
+
Core.prototype.foldList = function(init, f) {
|
3215
3642
|
var accumName, body, elName, ixName;
|
3216
3643
|
elName = nameGen('el');
|
3217
3644
|
ixName = nameGen('i');
|
3218
3645
|
accumName = nameGen('next');
|
3219
|
-
body = f(
|
3220
|
-
return
|
3646
|
+
body = f(Core.variable(elName), Core.variable(accumName), Core.variable(ixName));
|
3647
|
+
return Core.foldList(this, init, elName, accumName, ixName, body);
|
3221
3648
|
};
|
3222
3649
|
|
3223
|
-
|
3224
|
-
return
|
3650
|
+
Core.prototype.zipList = function(other) {
|
3651
|
+
return Core.zipLists(this, other);
|
3225
3652
|
};
|
3226
3653
|
|
3227
|
-
|
3654
|
+
Core.prototype.filterList = function(f) {
|
3228
3655
|
var name;
|
3229
3656
|
name = nameGen('el');
|
3230
|
-
return
|
3657
|
+
return Core.filterList(this, name, f(Core.variable(name)));
|
3658
|
+
};
|
3659
|
+
|
3660
|
+
Core.prototype.variables = function() {
|
3661
|
+
var s;
|
3662
|
+
if (this._tag === 'variable') {
|
3663
|
+
return [this];
|
3664
|
+
}
|
3665
|
+
return catLists((function() {
|
3666
|
+
var _i, _len, _ref14, _results;
|
3667
|
+
_ref14 = this.subtrees();
|
3668
|
+
_results = [];
|
3669
|
+
for (_i = 0, _len = _ref14.length; _i < _len; _i++) {
|
3670
|
+
s = _ref14[_i];
|
3671
|
+
_results.push(s.variables());
|
3672
|
+
}
|
3673
|
+
return _results;
|
3674
|
+
}).call(this));
|
3231
3675
|
};
|
3232
3676
|
|
3233
|
-
|
3677
|
+
Core.prototype.subtrees = function() {
|
3234
3678
|
var double, single;
|
3235
3679
|
single = function(x) {
|
3236
3680
|
return [x];
|
@@ -3264,12 +3708,12 @@ Gibbon.IR = IR = (function(_super) {
|
|
3264
3708
|
},
|
3265
3709
|
app: double,
|
3266
3710
|
query: single,
|
3267
|
-
|
3268
|
-
return [l, r];
|
3269
|
-
},
|
3270
|
-
extern: function(n, v) {
|
3711
|
+
op1: function(op, v) {
|
3271
3712
|
return [v];
|
3272
3713
|
},
|
3714
|
+
op2: function(op, l, r) {
|
3715
|
+
return [l, r];
|
3716
|
+
},
|
3273
3717
|
rescue: double,
|
3274
3718
|
other: function() {
|
3275
3719
|
return [];
|
@@ -3277,23 +3721,23 @@ Gibbon.IR = IR = (function(_super) {
|
|
3277
3721
|
});
|
3278
3722
|
};
|
3279
3723
|
|
3280
|
-
|
3724
|
+
Core.prototype.map = function(f) {
|
3281
3725
|
return this.cases({
|
3282
3726
|
branch: function(c, tr, fa) {
|
3283
|
-
return
|
3727
|
+
return Core.branch(f(c), f(tr), f(fa));
|
3284
3728
|
},
|
3285
3729
|
bind: function(n, v, e) {
|
3286
|
-
return
|
3730
|
+
return Core.bind(n, f(v), f(e));
|
3287
3731
|
},
|
3288
3732
|
delist: function(e, i) {
|
3289
|
-
return
|
3733
|
+
return Core.delist(f(e), i);
|
3290
3734
|
},
|
3291
3735
|
depair: function(e, k) {
|
3292
|
-
return
|
3736
|
+
return Core.depair(f(e), k);
|
3293
3737
|
},
|
3294
3738
|
list: function(els) {
|
3295
3739
|
var e;
|
3296
|
-
return
|
3740
|
+
return Core.list((function() {
|
3297
3741
|
var _i, _len, _results;
|
3298
3742
|
_results = [];
|
3299
3743
|
for (_i = 0, _len = els.length; _i < _len; _i++) {
|
@@ -3304,34 +3748,40 @@ Gibbon.IR = IR = (function(_super) {
|
|
3304
3748
|
})());
|
3305
3749
|
},
|
3306
3750
|
foldList: function(l, o, a, i, aa, b) {
|
3307
|
-
return
|
3751
|
+
return Core.foldList(f(l), f(o), a, i, aa, f(b));
|
3308
3752
|
},
|
3309
3753
|
mapList: function(l, a, i, b) {
|
3310
|
-
return
|
3754
|
+
return Core.mapList(f(l), i, a, f(b));
|
3311
3755
|
},
|
3312
3756
|
zipLists: function(l, r) {
|
3313
|
-
return
|
3757
|
+
return Core.zipLists(f(l), f(r));
|
3758
|
+
},
|
3759
|
+
filterList: function(l, a, b) {
|
3760
|
+
return Core.filterList(f(l), a, f(b));
|
3314
3761
|
},
|
3315
3762
|
len: function(l) {
|
3316
|
-
return
|
3763
|
+
return Core.len(f(l));
|
3317
3764
|
},
|
3318
3765
|
pair: function(x, y) {
|
3319
|
-
return
|
3766
|
+
return Core.pair(f(x), f(y));
|
3320
3767
|
},
|
3321
3768
|
block: function(n, b) {
|
3322
|
-
return
|
3769
|
+
return Core.block(n, f(b));
|
3323
3770
|
},
|
3324
3771
|
app: function(b, a) {
|
3325
|
-
return
|
3772
|
+
return Core.app(f(b), f(a));
|
3326
3773
|
},
|
3327
3774
|
query: function(e, a) {
|
3328
|
-
return
|
3775
|
+
return Core.query(f(e), a);
|
3776
|
+
},
|
3777
|
+
op1: function(o, v) {
|
3778
|
+
return Core.op1(o, f(v));
|
3329
3779
|
},
|
3330
|
-
|
3331
|
-
return
|
3780
|
+
op2: function(o, l, r) {
|
3781
|
+
return Core.op2(o, f(l), f(r));
|
3332
3782
|
},
|
3333
|
-
|
3334
|
-
return
|
3783
|
+
squishList: function(l) {
|
3784
|
+
return Core.squishList(f(l));
|
3335
3785
|
},
|
3336
3786
|
other: function() {
|
3337
3787
|
return this;
|
@@ -3339,7 +3789,7 @@ Gibbon.IR = IR = (function(_super) {
|
|
3339
3789
|
});
|
3340
3790
|
};
|
3341
3791
|
|
3342
|
-
|
3792
|
+
Core.prototype.replace = function(expr, other) {
|
3343
3793
|
if (this.equals(expr)) {
|
3344
3794
|
return other;
|
3345
3795
|
}
|
@@ -3348,7 +3798,7 @@ Gibbon.IR = IR = (function(_super) {
|
|
3348
3798
|
});
|
3349
3799
|
};
|
3350
3800
|
|
3351
|
-
|
3801
|
+
Core.prototype.subst = function(varName, expr) {
|
3352
3802
|
if (this._tag === 'variable' && this.name === varName) {
|
3353
3803
|
return expr;
|
3354
3804
|
}
|
@@ -3357,7 +3807,58 @@ Gibbon.IR = IR = (function(_super) {
|
|
3357
3807
|
});
|
3358
3808
|
};
|
3359
3809
|
|
3360
|
-
|
3810
|
+
Core.prototype.isStrictIn = function(needle) {
|
3811
|
+
if (this.equals(needle)) {
|
3812
|
+
return true;
|
3813
|
+
}
|
3814
|
+
return this.cases({
|
3815
|
+
branch: function(cond, ifTrue, ifFalse) {
|
3816
|
+
return cond.isStrictIn(needle) || (ifTrue.isStrictIn(needle) && ifFalse.isStrictIn(needle));
|
3817
|
+
},
|
3818
|
+
other: function() {
|
3819
|
+
var subtree, _i, _len, _ref14;
|
3820
|
+
_ref14 = this.subtrees();
|
3821
|
+
for (_i = 0, _len = _ref14.length; _i < _len; _i++) {
|
3822
|
+
subtree = _ref14[_i];
|
3823
|
+
if (subtree.isStrictIn(needle)) {
|
3824
|
+
return true;
|
3825
|
+
}
|
3826
|
+
}
|
3827
|
+
return false;
|
3828
|
+
}
|
3829
|
+
});
|
3830
|
+
};
|
3831
|
+
|
3832
|
+
Core.prototype.contains = function(needle) {
|
3833
|
+
var subtree, _i, _len, _ref14;
|
3834
|
+
if (this.equals(needle)) {
|
3835
|
+
return true;
|
3836
|
+
}
|
3837
|
+
_ref14 = this.subtrees();
|
3838
|
+
for (_i = 0, _len = _ref14.length; _i < _len; _i++) {
|
3839
|
+
subtree = _ref14[_i];
|
3840
|
+
if (subtree.contains(needle)) {
|
3841
|
+
return true;
|
3842
|
+
}
|
3843
|
+
}
|
3844
|
+
return false;
|
3845
|
+
};
|
3846
|
+
|
3847
|
+
Core.prototype.containsInNonTailPosition = function(needle) {
|
3848
|
+
if (this.equals(needle)) {
|
3849
|
+
return false;
|
3850
|
+
}
|
3851
|
+
return this.cases({
|
3852
|
+
branch: function(cond, ifTrue, ifFalse) {
|
3853
|
+
return cond.contains(needle) || ifTrue.containsInNonTailPosition(needle) || ifFalse.containsInNonTailPosition(needle);
|
3854
|
+
},
|
3855
|
+
other: function() {
|
3856
|
+
return this.contains(needle);
|
3857
|
+
}
|
3858
|
+
});
|
3859
|
+
};
|
3860
|
+
|
3861
|
+
Core.prototype.equals = function(other) {
|
3361
3862
|
if (this._tag !== other._tag) {
|
3362
3863
|
return false;
|
3363
3864
|
}
|
@@ -3379,7 +3880,7 @@ Gibbon.IR = IR = (function(_super) {
|
|
3379
3880
|
if (!val.equals(other.value)) {
|
3380
3881
|
return false;
|
3381
3882
|
}
|
3382
|
-
subst = other.expr.subst(other.name,
|
3883
|
+
subst = other.expr.subst(other.name, Core.variable(name));
|
3383
3884
|
return expr.equals(subst);
|
3384
3885
|
},
|
3385
3886
|
delist: function(expr, index) {
|
@@ -3406,7 +3907,7 @@ Gibbon.IR = IR = (function(_super) {
|
|
3406
3907
|
if (!out.equals(other.out)) {
|
3407
3908
|
return false;
|
3408
3909
|
}
|
3409
|
-
subst = other.body.subst(other.arg,
|
3910
|
+
subst = other.body.subst(other.arg, Core.variable(arg)).subst(other.accumArg, Core.variable(accumArg)).subst(other.idxArg, Core.variable(idxArg));
|
3410
3911
|
return body.equals(subst);
|
3411
3912
|
},
|
3412
3913
|
mapList: function(list, arg, idxArg, body) {
|
@@ -3414,7 +3915,7 @@ Gibbon.IR = IR = (function(_super) {
|
|
3414
3915
|
if (!list.equals(other.list)) {
|
3415
3916
|
return false;
|
3416
3917
|
}
|
3417
|
-
subst = other.body.subst(other.arg,
|
3918
|
+
subst = other.body.subst(other.arg, Core.variable(arg)).subst(other.idxArg, Core.variable(idxArg));
|
3418
3919
|
return body.equals(subst);
|
3419
3920
|
},
|
3420
3921
|
zipLists: function(first, second) {
|
@@ -3424,7 +3925,7 @@ Gibbon.IR = IR = (function(_super) {
|
|
3424
3925
|
if (!list.equals(other.list)) {
|
3425
3926
|
return false;
|
3426
3927
|
}
|
3427
|
-
return body.equals(other.body.subst(other.arg,
|
3928
|
+
return body.equals(other.body.subst(other.arg, Core.variable(arg)));
|
3428
3929
|
},
|
3429
3930
|
squishList: function(list) {
|
3430
3931
|
return list.equals(other.list);
|
@@ -3450,11 +3951,11 @@ Gibbon.IR = IR = (function(_super) {
|
|
3450
3951
|
fail: function() {
|
3451
3952
|
return false;
|
3452
3953
|
},
|
3453
|
-
|
3954
|
+
op2: function(op, lhs, rhs) {
|
3454
3955
|
return op === other.op && lhs.equals(other.lhs) && rhs.equals(other.rhs);
|
3455
3956
|
},
|
3456
|
-
|
3457
|
-
return
|
3957
|
+
op1: function(op, arg) {
|
3958
|
+
return op === other.op && arg.equals(other.arg);
|
3458
3959
|
},
|
3459
3960
|
rescue: function(expr, default_) {
|
3460
3961
|
return expr.equals(other.expr) && default_.equals(other["default"]);
|
@@ -3470,7 +3971,7 @@ Gibbon.IR = IR = (function(_super) {
|
|
3470
3971
|
};
|
3471
3972
|
})();
|
3472
3973
|
|
3473
|
-
|
3974
|
+
Core.prototype.inspect = function(indent) {
|
3474
3975
|
if (indent == null) {
|
3475
3976
|
indent = 0;
|
3476
3977
|
}
|
@@ -3541,50 +4042,155 @@ Gibbon.IR = IR = (function(_super) {
|
|
3541
4042
|
fail: function(m) {
|
3542
4043
|
return "(FAIL " + m + ")";
|
3543
4044
|
},
|
3544
|
-
|
3545
|
-
return "(`" + op + "` " + (
|
4045
|
+
op1: function(op, arg) {
|
4046
|
+
return "(`" + op + "` " + (arg.inspect()) + ")";
|
3546
4047
|
},
|
3547
|
-
|
3548
|
-
return "(`" +
|
4048
|
+
op2: function(op, l, r) {
|
4049
|
+
return "(`" + op + "` " + (l.inspect()) + " " + (r.inspect()) + ")";
|
3549
4050
|
},
|
3550
4051
|
rescue: function(e, d) {
|
3551
4052
|
return "(RESCUE " + (e.inspect()) + " " + (d.inspect()) + ")";
|
3552
4053
|
},
|
3553
4054
|
constant: function(v) {
|
3554
4055
|
return '' + v;
|
4056
|
+
},
|
4057
|
+
next: function() {
|
4058
|
+
return 'NEXT';
|
3555
4059
|
}
|
3556
4060
|
});
|
3557
4061
|
};
|
3558
4062
|
|
3559
|
-
|
4063
|
+
Core.prototype.hash = function() {
|
4064
|
+
var mkVar, r,
|
4065
|
+
_this = this;
|
4066
|
+
mkVar = (function() {
|
4067
|
+
var i;
|
4068
|
+
i = 0;
|
4069
|
+
return function() {
|
4070
|
+
return Core.variable("" + (i += 1));
|
4071
|
+
};
|
4072
|
+
})();
|
4073
|
+
return this.__hash__ || (this.__hash__ = (r = function(el) {
|
4074
|
+
return el.cases({
|
4075
|
+
global: function() {
|
4076
|
+
return '$';
|
4077
|
+
},
|
4078
|
+
branch: function(cond, ifTrue, ifFalse) {
|
4079
|
+
return "(IF " + (r(cond)) + " " + (r(ifTrue)) + " " + (r(ifFalse)) + ")";
|
4080
|
+
},
|
4081
|
+
bind: function(name, val, expr) {
|
4082
|
+
expr = expr.subst(name, mkVar());
|
4083
|
+
return "(LET " + (r(val)) + " " + (r(expr)) + ")";
|
4084
|
+
},
|
4085
|
+
variable: function(name) {
|
4086
|
+
return name;
|
4087
|
+
},
|
4088
|
+
delist: function(e, i) {
|
4089
|
+
return "([" + (r(i)) + "] " + (r(e)) + ")";
|
4090
|
+
},
|
4091
|
+
depair: function(e, k) {
|
4092
|
+
return "([" + k + "] " + (r(e)) + ")";
|
4093
|
+
},
|
4094
|
+
list: function(els) {
|
4095
|
+
var e;
|
4096
|
+
return "(LIST " + (((function() {
|
4097
|
+
var _i, _len, _results;
|
4098
|
+
_results = [];
|
4099
|
+
for (_i = 0, _len = els.length; _i < _len; _i++) {
|
4100
|
+
e = els[_i];
|
4101
|
+
_results.push(r(e));
|
4102
|
+
}
|
4103
|
+
return _results;
|
4104
|
+
})()).join(' ')) + ")";
|
4105
|
+
},
|
4106
|
+
len: function(e) {
|
4107
|
+
return "(LEN " + (r(e)) + ")";
|
4108
|
+
},
|
4109
|
+
mapList: function(list, arg, i, body) {
|
4110
|
+
body = body.subst(arg, mkVar()).subst(i, mkVar());
|
4111
|
+
return "(MAP " + (r(list)) + " " + (r(body)) + ")";
|
4112
|
+
},
|
4113
|
+
foldList: function(list, out, arg, next, i, body) {
|
4114
|
+
body = body.subst(arg, mkVar()).subst(next, mkVar()).subst(i, mkVar());
|
4115
|
+
return "(FOLDR " + (r(list)) + " " + (r(out)) + " " + (r(body)) + ")";
|
4116
|
+
},
|
4117
|
+
filterList: function(list, arg, body) {
|
4118
|
+
body = body.subst(arg, mkVar());
|
4119
|
+
return "(FILTER " + (r(list)) + " " + (r(body)) + ")";
|
4120
|
+
},
|
4121
|
+
zipLists: function(x, y) {
|
4122
|
+
return "(ZIP " + (r(x)) + " " + (r(y)) + ")";
|
4123
|
+
},
|
4124
|
+
squishList: function(l) {
|
4125
|
+
return "(SQUISH " + (r(l)) + ")";
|
4126
|
+
},
|
4127
|
+
pair: function(x, y) {
|
4128
|
+
return "(PAIR " + (r(x)) + " " + (r(y)) + ")";
|
4129
|
+
},
|
4130
|
+
query: function(e, a) {
|
4131
|
+
return "(QUERY " + (JSON.stringify(a)) + " " + (r(e)) + ")";
|
4132
|
+
},
|
4133
|
+
block: function(arg, body) {
|
4134
|
+
body = body.subst(arg, mkVar());
|
4135
|
+
return "(LAMBDA " + (r(body)) + ")";
|
4136
|
+
},
|
4137
|
+
app: function(b, a) {
|
4138
|
+
return "(APPLY " + (r(b)) + " " + (r(a)) + ")";
|
4139
|
+
},
|
4140
|
+
localQuery: function(k) {
|
4141
|
+
return "@" + k;
|
4142
|
+
},
|
4143
|
+
fail: function(m) {
|
4144
|
+
return "(FAIL)";
|
4145
|
+
},
|
4146
|
+
op1: function(op, arg) {
|
4147
|
+
return "(OP1 " + op + " " + (r(arg)) + ")";
|
4148
|
+
},
|
4149
|
+
op2: function(op, lhs, rhs) {
|
4150
|
+
return "(OP2 " + op + " " + (r(lhs)) + " " + (r(rhs)) + ")";
|
4151
|
+
},
|
4152
|
+
rescue: function(e, d) {
|
4153
|
+
return "(RESCUE " + (r(e)) + " " + (r(d)) + ")";
|
4154
|
+
},
|
4155
|
+
constant: function(v) {
|
4156
|
+
return '' + v;
|
4157
|
+
},
|
4158
|
+
next: function() {
|
4159
|
+
return 'NEXT';
|
4160
|
+
}
|
4161
|
+
});
|
4162
|
+
})(this));
|
4163
|
+
};
|
4164
|
+
|
4165
|
+
return Core;
|
3560
4166
|
|
3561
|
-
})(
|
4167
|
+
})(Variant);
|
3562
4168
|
|
3563
|
-
Gibbon.
|
3564
|
-
var
|
3565
|
-
|
4169
|
+
Gibbon.translate = (function() {
|
4170
|
+
var translate;
|
4171
|
+
translate = function(semantic, input, context) {
|
3566
4172
|
if (input == null) {
|
3567
|
-
input =
|
4173
|
+
input = Core.global();
|
3568
4174
|
}
|
3569
4175
|
if (context == null) {
|
3570
4176
|
context = input;
|
3571
4177
|
}
|
3572
4178
|
return semantic.cases({
|
3573
4179
|
definition: function(_, flow) {
|
3574
|
-
return
|
4180
|
+
return translate(flow, input, context);
|
3575
4181
|
},
|
3576
4182
|
flow: function(_, head, tail) {
|
3577
4183
|
if (tail) {
|
3578
|
-
return
|
4184
|
+
return translate(head, translate(tail, input, context), context);
|
3579
4185
|
} else {
|
3580
|
-
return
|
4186
|
+
return translate(head, input, context);
|
3581
4187
|
}
|
3582
4188
|
},
|
3583
4189
|
query: function(annotations) {
|
3584
4190
|
return input.query(annotations);
|
3585
4191
|
},
|
3586
4192
|
localAccessor: function(key) {
|
3587
|
-
return
|
4193
|
+
return Core.localQuery(key);
|
3588
4194
|
},
|
3589
4195
|
func: function(name, args, tvars) {
|
3590
4196
|
var arg, compArgs;
|
@@ -3593,23 +4199,23 @@ Gibbon.compile = (function() {
|
|
3593
4199
|
_results = [];
|
3594
4200
|
for (_i = 0, _len = args.length; _i < _len; _i++) {
|
3595
4201
|
arg = args[_i];
|
3596
|
-
_results.push(
|
4202
|
+
_results.push(translate(arg, context));
|
3597
4203
|
}
|
3598
4204
|
return _results;
|
3599
4205
|
})();
|
3600
4206
|
return stdlib[name].compile(input, compArgs, tvars);
|
3601
4207
|
},
|
3602
4208
|
literal: function(syntax) {
|
3603
|
-
return
|
4209
|
+
return Core.constant(syntax.value);
|
3604
4210
|
},
|
3605
4211
|
list: function(elements, squish) {
|
3606
4212
|
var e, list;
|
3607
|
-
list =
|
4213
|
+
list = Core.list((function() {
|
3608
4214
|
var _i, _len, _results;
|
3609
4215
|
_results = [];
|
3610
4216
|
for (_i = 0, _len = elements.length; _i < _len; _i++) {
|
3611
4217
|
e = elements[_i];
|
3612
|
-
_results.push(
|
4218
|
+
_results.push(translate(e, context));
|
3613
4219
|
}
|
3614
4220
|
return _results;
|
3615
4221
|
})());
|
@@ -3620,28 +4226,25 @@ Gibbon.compile = (function() {
|
|
3620
4226
|
}
|
3621
4227
|
},
|
3622
4228
|
pair: function(x, y) {
|
3623
|
-
return
|
4229
|
+
return Core.pair(translate(x, context), translate(y, context));
|
3624
4230
|
},
|
3625
4231
|
block: function(body) {
|
3626
4232
|
var arg;
|
3627
|
-
arg =
|
3628
|
-
return
|
4233
|
+
arg = Core.makeVariable('arg');
|
4234
|
+
return Core.block(arg.name, translate(body, arg, arg));
|
3629
4235
|
},
|
3630
4236
|
defaulted: function(body, alternative) {
|
3631
|
-
return
|
4237
|
+
return Core.rescue(translate(body, context), translate(alternative, context));
|
3632
4238
|
}
|
3633
4239
|
});
|
3634
4240
|
};
|
3635
4241
|
return function(semantic) {
|
3636
|
-
return
|
4242
|
+
return translate(semantic);
|
3637
4243
|
};
|
3638
4244
|
})();
|
3639
|
-
|
4245
|
+
|
3640
4246
|
Gibbon.optimize = (function() {
|
3641
4247
|
var insertBindings, partialEval;
|
3642
|
-
insertBindings = function(expr) {
|
3643
|
-
return expr;
|
3644
|
-
};
|
3645
4248
|
partialEval = function(expr) {
|
3646
4249
|
return expr.cases({
|
3647
4250
|
depair: function(expr, key) {
|
@@ -3695,7 +4298,7 @@ Gibbon.optimize = (function() {
|
|
3695
4298
|
return this.branch(ifTrue, ifFalse);
|
3696
4299
|
}
|
3697
4300
|
},
|
3698
|
-
|
4301
|
+
op1: function(name, arg) {
|
3699
4302
|
if (name === '!') {
|
3700
4303
|
DEBUG.log("eliminating if negation in condition " + (this.inspect()));
|
3701
4304
|
return partialEval(arg.branch(ifFalse, ifTrue));
|
@@ -3711,6 +4314,7 @@ Gibbon.optimize = (function() {
|
|
3711
4314
|
app: function(block, arg) {
|
3712
4315
|
return partialEval(block).cases({
|
3713
4316
|
block: function(argName, body) {
|
4317
|
+
DEBUG.log("inlining application of " + (this.inspect()) + " to " + (arg.inspect()));
|
3714
4318
|
return partialEval(body.subst(argName, arg));
|
3715
4319
|
},
|
3716
4320
|
other: function() {
|
@@ -3726,7 +4330,7 @@ Gibbon.optimize = (function() {
|
|
3726
4330
|
for (i = _i = 0, _len = elements.length; _i < _len; i = ++_i) {
|
3727
4331
|
el = elements[i];
|
3728
4332
|
DEBUG.log(("subst " + arg + "=" + (el.inspect()) + " ") + ("" + ixName + "=" + i + " " + (body.inspect())));
|
3729
|
-
partialEval(body.subst(arg, el).subst(ixName,
|
4333
|
+
partialEval(body.subst(arg, el).subst(ixName, Core.constant(i)));
|
3730
4334
|
}
|
3731
4335
|
mapped = (function() {
|
3732
4336
|
var _j, _len1, _results;
|
@@ -3737,10 +4341,16 @@ Gibbon.optimize = (function() {
|
|
3737
4341
|
}
|
3738
4342
|
return _results;
|
3739
4343
|
})();
|
3740
|
-
return partialEval(
|
4344
|
+
return partialEval(Core.list(mapped));
|
4345
|
+
},
|
4346
|
+
mapList: function(innerList, innerArg, innerIdxArg, innerBody) {
|
4347
|
+
var newBody;
|
4348
|
+
DEBUG.log("fusing MAP of " + (this.inspect()));
|
4349
|
+
newBody = body.subst(arg, innerBody).subst(ixName, Core.variable(innerIdxArg));
|
4350
|
+
return Core.mapList(innerList, innerArg, innerIdxArg, newBody);
|
3741
4351
|
},
|
3742
4352
|
other: function() {
|
3743
|
-
return
|
4353
|
+
return Core.mapList(this, arg, ixName, partialEval(body));
|
3744
4354
|
}
|
3745
4355
|
});
|
3746
4356
|
},
|
@@ -3756,12 +4366,18 @@ Gibbon.optimize = (function() {
|
|
3756
4366
|
}
|
3757
4367
|
next = _loop(i + 1);
|
3758
4368
|
DEBUG.log(("subst " + arg + "=" + (elements[i].inspect()) + " ") + ("" + accum + "=" + (next.inspect()) + " ") + ("" + ixName + "=i " + (body.inspect())));
|
3759
|
-
return body.subst(arg, elements[i]).subst(accum, next).subst(ixName,
|
4369
|
+
return body.subst(arg, elements[i]).subst(accum, next).subst(ixName, Core.constant(i));
|
3760
4370
|
};
|
3761
4371
|
return partialEval(_loop(0));
|
3762
4372
|
},
|
4373
|
+
mapList: function(innerList, innerArg, innerIdxArg, mapBody) {
|
4374
|
+
var newBody;
|
4375
|
+
DEBUG.log("fusing FOLDR of " + (this.inspect()));
|
4376
|
+
newBody = body.subst(arg, mapBody).subst(ixName, Core.variable(innerIdxArg));
|
4377
|
+
return Core.foldList(innerList, out, innerArg, accum, innerIdxArg, newBody);
|
4378
|
+
},
|
3763
4379
|
other: function() {
|
3764
|
-
return
|
4380
|
+
return Core.foldList(this, partialEval(out), arg, accum, ixName, partialEval(body));
|
3765
4381
|
}
|
3766
4382
|
});
|
3767
4383
|
},
|
@@ -3769,7 +4385,7 @@ Gibbon.optimize = (function() {
|
|
3769
4385
|
return partialEval(list).cases({
|
3770
4386
|
list: function(elements) {
|
3771
4387
|
DEBUG.log("optimizing out len from " + (this.inspect()));
|
3772
|
-
return
|
4388
|
+
return Core.constant(elements.length);
|
3773
4389
|
},
|
3774
4390
|
mapList: function(list) {
|
3775
4391
|
DEBUG.log("optimizing MAP out of len");
|
@@ -3789,146 +4405,58 @@ Gibbon.optimize = (function() {
|
|
3789
4405
|
l = partialEval(l);
|
3790
4406
|
r = partialEval(r);
|
3791
4407
|
if (!(l._tag === 'list' && r._tag === 'list')) {
|
3792
|
-
return
|
4408
|
+
return Core.zipLists(l, r);
|
3793
4409
|
}
|
3794
4410
|
elements = (function() {
|
3795
4411
|
var _i, _len, _results;
|
3796
4412
|
_results = [];
|
3797
4413
|
for (i = _i = 0, _len = l.length; _i < _len; i = ++_i) {
|
3798
4414
|
e = l[i];
|
3799
|
-
_results.push(
|
4415
|
+
_results.push(Core.pair(e, r[i]));
|
3800
4416
|
}
|
3801
4417
|
return _results;
|
3802
4418
|
})();
|
3803
|
-
return partialEval(
|
4419
|
+
return partialEval(Core.list(elements));
|
3804
4420
|
},
|
3805
|
-
|
4421
|
+
op1: function(op, arg) {
|
3806
4422
|
return partialEval(arg).cases({
|
3807
4423
|
constant: function(value) {
|
3808
|
-
|
3809
|
-
return IR.constant(Math[RegExp.$1](value));
|
3810
|
-
}
|
3811
|
-
return IR.constant((function() {
|
3812
|
-
switch (op) {
|
3813
|
-
case '!':
|
3814
|
-
return !value;
|
3815
|
-
case '-':
|
3816
|
-
return -value;
|
3817
|
-
default:
|
3818
|
-
throw "unknown operator " + op;
|
3819
|
-
}
|
3820
|
-
})());
|
4424
|
+
return Core.constant(applyOp1(op, value));
|
3821
4425
|
},
|
3822
4426
|
other: function() {
|
3823
|
-
return
|
4427
|
+
return Core.op1(op, this);
|
3824
4428
|
}
|
3825
4429
|
});
|
3826
4430
|
},
|
3827
|
-
|
3828
|
-
var l, r,
|
4431
|
+
op2: function(op, left, right) {
|
4432
|
+
var checkIdent, identFold, l, r, _ref14;
|
3829
4433
|
left = partialEval(left);
|
3830
4434
|
right = partialEval(right);
|
4435
|
+
checkIdent = function(opTest, val, ident, identVal) {
|
4436
|
+
return op === opTest && val._tag === 'constant' && val.value === ident && identVal;
|
4437
|
+
};
|
4438
|
+
identFold = checkIdent('*', right, 1, left) || checkIdent('*', left, 1, right) || checkIdent('*', right, 0, Core.constant(0)) || checkIdent('*', left, 0, Core.constant(0)) || checkIdent('+', left, 0, right) || checkIdent('+', right, 0, left) || checkIdent('/', right, 1, left);
|
4439
|
+
if (identFold) {
|
4440
|
+
DEBUG.log("identity-folding " + (this.inspect()) + " to " + (identFold.inspect()));
|
4441
|
+
return identFold;
|
4442
|
+
}
|
3831
4443
|
if (!(left._tag === 'constant' && right._tag === 'constant')) {
|
3832
|
-
return
|
4444
|
+
return Core.op2(op, left, right);
|
3833
4445
|
}
|
3834
|
-
DEBUG.log("constant-folding " + (left.
|
3835
|
-
|
3836
|
-
return
|
3837
|
-
switch (op) {
|
3838
|
-
case '+':
|
3839
|
-
return l + r;
|
3840
|
-
case '*':
|
3841
|
-
return l * r;
|
3842
|
-
case '/':
|
3843
|
-
return l / r;
|
3844
|
-
case '%':
|
3845
|
-
return l % r;
|
3846
|
-
case '===':
|
3847
|
-
return l === r;
|
3848
|
-
case '<':
|
3849
|
-
return l < r;
|
3850
|
-
case '>':
|
3851
|
-
return l > r;
|
3852
|
-
case '<=':
|
3853
|
-
return l <= r;
|
3854
|
-
case '>=':
|
3855
|
-
return l >= r;
|
3856
|
-
}
|
3857
|
-
})());
|
4446
|
+
DEBUG.log("constant-folding " + (left.op2(op, right).inspect()));
|
4447
|
+
_ref14 = [left.value, right.value], l = _ref14[0], r = _ref14[1];
|
4448
|
+
return Core.constant(applyOp2(op, left.value, right.value));
|
3858
4449
|
},
|
3859
4450
|
rescue: function(expr, default_) {
|
3860
|
-
var alwaysFails, alwaysSucceeds;
|
3861
4451
|
expr = partialEval(expr);
|
3862
|
-
|
3863
|
-
return e.cases({
|
3864
|
-
query: function() {
|
3865
|
-
return false;
|
3866
|
-
},
|
3867
|
-
localQuery: function() {
|
3868
|
-
return false;
|
3869
|
-
},
|
3870
|
-
fail: function() {
|
3871
|
-
return false;
|
3872
|
-
},
|
3873
|
-
rescue: function() {
|
3874
|
-
return true;
|
3875
|
-
},
|
3876
|
-
squishList: function() {
|
3877
|
-
return true;
|
3878
|
-
},
|
3879
|
-
other: function() {
|
3880
|
-
var subtree, _i, _len, _ref;
|
3881
|
-
_ref = this.subtrees();
|
3882
|
-
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
3883
|
-
subtree = _ref[_i];
|
3884
|
-
console.log("checking alwaysSucceeds " + (subtree.inspect()));
|
3885
|
-
if (!alwaysSucceeds(subtree)) {
|
3886
|
-
return false;
|
3887
|
-
}
|
3888
|
-
}
|
3889
|
-
return true;
|
3890
|
-
}
|
3891
|
-
});
|
3892
|
-
};
|
3893
|
-
alwaysFails = function(e) {
|
3894
|
-
return e.cases({
|
3895
|
-
query: function() {
|
3896
|
-
return false;
|
3897
|
-
},
|
3898
|
-
localQuery: function() {
|
3899
|
-
return false;
|
3900
|
-
},
|
3901
|
-
fail: function() {
|
3902
|
-
return true;
|
3903
|
-
},
|
3904
|
-
rescue: function() {
|
3905
|
-
return false;
|
3906
|
-
},
|
3907
|
-
squishList: function() {
|
3908
|
-
return false;
|
3909
|
-
},
|
3910
|
-
other: function() {
|
3911
|
-
var subtree, _i, _len, _ref;
|
3912
|
-
_ref = this.subtrees();
|
3913
|
-
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
3914
|
-
subtree = _ref[_i];
|
3915
|
-
console.log("checking alwaysFails " + (subtree.inspect()));
|
3916
|
-
if (alwaysFails(subtree)) {
|
3917
|
-
return true;
|
3918
|
-
}
|
3919
|
-
}
|
3920
|
-
return false;
|
3921
|
-
}
|
3922
|
-
});
|
3923
|
-
};
|
3924
|
-
if (alwaysSucceeds(expr)) {
|
4452
|
+
if (expr.alwaysSucceeds()) {
|
3925
4453
|
DEBUG.log("eliminating dead default branch of " + (this.inspect()));
|
3926
4454
|
return expr;
|
3927
|
-
} else if (alwaysFails(
|
4455
|
+
} else if (expr.alwaysFails()) {
|
3928
4456
|
DEBUG.log("eliminating dead main branch of " + (this.inspect()));
|
3929
4457
|
return partialEval(default_);
|
3930
4458
|
} else {
|
3931
|
-
return
|
4459
|
+
return Core.rescue(expr, partialEval(default_));
|
3932
4460
|
}
|
3933
4461
|
},
|
3934
4462
|
other: function() {
|
@@ -3936,6 +4464,79 @@ Gibbon.optimize = (function() {
|
|
3936
4464
|
}
|
3937
4465
|
});
|
3938
4466
|
};
|
4467
|
+
insertBindings = (function() {
|
4468
|
+
var findLastCommon, genSubstitutions, makeCrumbs, simplify;
|
4469
|
+
makeCrumbs = function(trace) {
|
4470
|
+
return trace.mapArray(function(e) {
|
4471
|
+
return e.hash();
|
4472
|
+
}).reverse();
|
4473
|
+
};
|
4474
|
+
genSubstitutions = function(expr) {
|
4475
|
+
var newTrace, queue, sub, substitutions, trace, _i, _len, _ref14, _ref15;
|
4476
|
+
substitutions = new ObjHash;
|
4477
|
+
queue = [[expr, List.empty()]];
|
4478
|
+
while (queue.length) {
|
4479
|
+
_ref14 = queue.shift(), expr = _ref14[0], trace = _ref14[1];
|
4480
|
+
if (expr.isSimple()) {
|
4481
|
+
continue;
|
4482
|
+
}
|
4483
|
+
if (substitutions.has(expr)) {
|
4484
|
+
substitutions.get(expr).push(makeCrumbs(trace));
|
4485
|
+
} else {
|
4486
|
+
substitutions.set(expr, [makeCrumbs(trace)]);
|
4487
|
+
newTrace = trace.cons(expr);
|
4488
|
+
_ref15 = expr.subtrees();
|
4489
|
+
for (_i = 0, _len = _ref15.length; _i < _len; _i++) {
|
4490
|
+
sub = _ref15[_i];
|
4491
|
+
queue.push([sub, newTrace]);
|
4492
|
+
}
|
4493
|
+
}
|
4494
|
+
}
|
4495
|
+
return substitutions;
|
4496
|
+
};
|
4497
|
+
findLastCommon = function(_arg) {
|
4498
|
+
var i, last, refCrumb, reference, rest, testCrumbs, _i, _j, _len, _len1;
|
4499
|
+
reference = _arg[0], rest = 2 <= _arg.length ? __slice.call(_arg, 1) : [];
|
4500
|
+
last = null;
|
4501
|
+
for (i = _i = 0, _len = reference.length; _i < _len; i = ++_i) {
|
4502
|
+
refCrumb = reference[i];
|
4503
|
+
for (_j = 0, _len1 = rest.length; _j < _len1; _j++) {
|
4504
|
+
testCrumbs = rest[_j];
|
4505
|
+
if (refCrumb !== testCrumbs[i]) {
|
4506
|
+
return last;
|
4507
|
+
}
|
4508
|
+
}
|
4509
|
+
last = refCrumb;
|
4510
|
+
}
|
4511
|
+
return refCrumb;
|
4512
|
+
};
|
4513
|
+
simplify = function(expr, substitutions) {
|
4514
|
+
substitutions.each(function(subExpr, crumbs) {
|
4515
|
+
var lastCommon, name, recurse;
|
4516
|
+
if (!(crumbs.length >= 2)) {
|
4517
|
+
return;
|
4518
|
+
}
|
4519
|
+
lastCommon = findLastCommon(crumbs);
|
4520
|
+
name = nameGen('b');
|
4521
|
+
DEBUG.log("eliminating common expression " + (expr.inspect()) + " as " + name);
|
4522
|
+
return expr = (recurse = function(expr) {
|
4523
|
+
var hash;
|
4524
|
+
hash = expr.hash();
|
4525
|
+
if (hash === lastCommon) {
|
4526
|
+
return Core.bind(name, subExpr, expr.map(recurse));
|
4527
|
+
} else if (hash === subExpr.hash()) {
|
4528
|
+
return Core.variable(name);
|
4529
|
+
} else {
|
4530
|
+
return expr.map(recurse);
|
4531
|
+
}
|
4532
|
+
})(expr);
|
4533
|
+
});
|
4534
|
+
return expr;
|
4535
|
+
};
|
4536
|
+
return function(expr) {
|
4537
|
+
return simplify(expr, genSubstitutions(expr));
|
4538
|
+
};
|
4539
|
+
})();
|
3939
4540
|
return function(expr) {
|
3940
4541
|
var out;
|
3941
4542
|
DEBUG.log("=== OPTIMIZING " + (expr.inspect()));
|
@@ -3944,66 +4545,1921 @@ Gibbon.optimize = (function() {
|
|
3944
4545
|
return out;
|
3945
4546
|
};
|
3946
4547
|
})();
|
3947
|
-
// Generated by CoffeeScript 1.6.3
|
3948
|
-
var stdlib;
|
3949
4548
|
|
3950
|
-
|
3951
|
-
|
3952
|
-
|
3953
|
-
|
3954
|
-
|
3955
|
-
|
3956
|
-
|
3957
|
-
|
3958
|
-
|
3959
|
-
|
3960
|
-
|
3961
|
-
return
|
3962
|
-
|
3963
|
-
|
3964
|
-
|
3965
|
-
|
3966
|
-
|
3967
|
-
|
4549
|
+
nameGen = (function(i) {
|
4550
|
+
return function(name) {
|
4551
|
+
return "" + name + (i += 1);
|
4552
|
+
};
|
4553
|
+
})(0);
|
4554
|
+
|
4555
|
+
Step = (function(_super) {
|
4556
|
+
__extends(Step, _super);
|
4557
|
+
|
4558
|
+
function Step() {
|
4559
|
+
_ref14 = Step.__super__.constructor.apply(this, arguments);
|
4560
|
+
return _ref14;
|
4561
|
+
}
|
4562
|
+
|
4563
|
+
Step.variants({
|
4564
|
+
"let": ['lval', 'value', 'expr'],
|
4565
|
+
fork: ['forks'],
|
4566
|
+
each: ['length', 'cont'],
|
4567
|
+
letCont: ['name', 'args', 'body', 'expr'],
|
4568
|
+
letJoin: ['name', 'order', 'cont', 'expr'],
|
4569
|
+
next: ['cont', 'args'],
|
4570
|
+
app: ['fn', 'args', 'rescue', 'next'],
|
4571
|
+
query: ['annotations', 'arg', 'rescue', 'next'],
|
4572
|
+
localQuery: ['key', 'rescue', 'next'],
|
4573
|
+
"if": ['cond', 'trueCont', 'falseCont']
|
4574
|
+
});
|
4575
|
+
|
4576
|
+
Step.prototype.inspect = function() {
|
4577
|
+
return this.cases({
|
4578
|
+
"let": function(varName, value, expr) {
|
4579
|
+
return "(LET " + varName + " " + (value.inspect()) + " " + (expr.inspect()) + ")";
|
3968
4580
|
},
|
3969
|
-
|
3970
|
-
|
3971
|
-
eqx = compEquals(input.depair('first'), input.depair('second'), type.first);
|
3972
|
-
eqy = compEquals(input.depair('first'), input.depair('second'), type.first);
|
3973
|
-
return eqx.branch(eqy, iFalse);
|
4581
|
+
letCont: function(name, args, body, expr) {
|
4582
|
+
return "(LETC " + name + " \\" + (args.join(',')) + " " + (body.inspect()) + " " + (expr.inspect()) + ")";
|
3974
4583
|
},
|
3975
|
-
|
3976
|
-
var
|
3977
|
-
|
3978
|
-
|
3979
|
-
|
3980
|
-
|
3981
|
-
|
4584
|
+
fork: function(forks) {
|
4585
|
+
var f;
|
4586
|
+
return "(FORK " + (((function() {
|
4587
|
+
var _i, _len, _results;
|
4588
|
+
_results = [];
|
4589
|
+
for (_i = 0, _len = forks.length; _i < _len; _i++) {
|
4590
|
+
f = forks[_i];
|
4591
|
+
_results.push("->" + f);
|
4592
|
+
}
|
4593
|
+
return _results;
|
4594
|
+
})()).join(' ')) + ")";
|
4595
|
+
},
|
4596
|
+
each: function(list, cont) {
|
4597
|
+
return "(EACH " + list + " ->" + cont + ")";
|
4598
|
+
},
|
4599
|
+
letJoin: function(name, order, cont, expr) {
|
4600
|
+
return "(LETJ " + name + "/" + order + " ->" + cont + " " + (expr.inspect()) + ")";
|
4601
|
+
},
|
4602
|
+
letMap: function(name, list, joinName, arg, body, expr) {
|
4603
|
+
return "(LETM " + name + " " + list + " " + joinName + "<- \\" + arg + " " + (body.inspect()) + " " + (expr.inspect()) + ")";
|
4604
|
+
},
|
4605
|
+
next: function(cont, args) {
|
4606
|
+
return "(->" + cont + " " + (args.join(' ')) + ")";
|
4607
|
+
},
|
4608
|
+
app: function(fn, args, rescue, next) {
|
4609
|
+
return "(" + fn + " " + (args.join(' ')) + " !->" + rescue + " ->" + next + ")";
|
4610
|
+
},
|
4611
|
+
query: function(annotations, arg, rescue, next) {
|
4612
|
+
return "(Q " + (JSON.stringify(annotations)) + " !->" + rescue + " ->" + next + ")";
|
4613
|
+
},
|
4614
|
+
localQuery: function(key, rescue, next) {
|
4615
|
+
return "(@" + key + " !->" + rescue + ", ->" + next + ")";
|
4616
|
+
},
|
4617
|
+
"if": function(cond, trueCont, falseCont) {
|
4618
|
+
return "(IF " + cond + " ->" + trueCont + " ->" + falseCont + ")";
|
3982
4619
|
}
|
3983
4620
|
});
|
3984
4621
|
};
|
3985
|
-
|
3986
|
-
|
3987
|
-
|
3988
|
-
|
3989
|
-
|
3990
|
-
|
3991
|
-
|
3992
|
-
|
3993
|
-
|
3994
|
-
|
3995
|
-
|
3996
|
-
|
3997
|
-
|
3998
|
-
|
4622
|
+
|
4623
|
+
Step.prototype.inspectLines = function(indent) {
|
4624
|
+
var i;
|
4625
|
+
if (indent == null) {
|
4626
|
+
indent = 0;
|
4627
|
+
}
|
4628
|
+
i = new Array(indent + 1).join(' ');
|
4629
|
+
return this.cases({
|
4630
|
+
"let": function(varName, value, expr) {
|
4631
|
+
return "" + i + "let " + varName + " = " + (value.inspect()) + "\n" + (expr.inspectLines(indent));
|
4632
|
+
},
|
4633
|
+
letCont: function(name, args, body, expr) {
|
4634
|
+
return "" + i + name + " " + (args.join(' ')) + ":\n" + (body.inspectLines(indent + 1)) + "\n" + (expr.inspectLines(indent));
|
4635
|
+
},
|
4636
|
+
letJoin: function(name, order, cont, expr) {
|
4637
|
+
return "" + i + name + ": join/" + order + " ->" + cont + "\n" + (expr.inspectLines(indent));
|
4638
|
+
},
|
4639
|
+
fork: function(conts) {
|
4640
|
+
var c;
|
4641
|
+
return "" + i + "fork " + (((function() {
|
4642
|
+
var _i, _len, _results;
|
4643
|
+
_results = [];
|
4644
|
+
for (_i = 0, _len = conts.length; _i < _len; _i++) {
|
4645
|
+
c = conts[_i];
|
4646
|
+
_results.push("->" + c);
|
4647
|
+
}
|
4648
|
+
return _results;
|
4649
|
+
})()).join(' '));
|
4650
|
+
},
|
4651
|
+
each: function(length, cont) {
|
4652
|
+
return "" + i + "each/" + length + " ->" + cont;
|
4653
|
+
},
|
4654
|
+
map: function(name, list, arg, joinName, body, expr) {
|
4655
|
+
return "" + i + name + " = map " + list + " \\" + joinName + ":\n" + (body.inspectLines(indent + 1)) + "\n" + (expr.inspectLines(indent));
|
4656
|
+
},
|
4657
|
+
next: function(name, args) {
|
4658
|
+
return "" + i + "go " + name + " " + (args.join(' '));
|
4659
|
+
},
|
4660
|
+
app: function(fn, args, rescue, next) {
|
4661
|
+
return "" + i + "app " + fn + " " + (args.join(' ')) + " !->" + rescue + " ->" + next;
|
4662
|
+
},
|
4663
|
+
query: function(annotations, arg, rescue, next) {
|
4664
|
+
return "" + i + "query " + (JSON.stringify(annotations)) + " " + arg + " !->" + rescue + " ->" + next;
|
4665
|
+
},
|
4666
|
+
localQuery: function(key, rescue, next) {
|
4667
|
+
return "" + i + "@" + key + " !->" + rescue + ", ->" + next;
|
4668
|
+
},
|
4669
|
+
"if": function(cond, trueCont, falseCont) {
|
4670
|
+
return "" + i + "if " + cond + " ->" + trueCont + " else ->" + falseCont;
|
4671
|
+
}
|
4672
|
+
});
|
4673
|
+
};
|
4674
|
+
|
4675
|
+
Step.prototype.search = function(f) {
|
4676
|
+
if (f(this)) {
|
4677
|
+
return true;
|
4678
|
+
}
|
4679
|
+
return this.cases({
|
4680
|
+
"let": function(_, __, expr) {
|
4681
|
+
return expr.search(f);
|
4682
|
+
},
|
4683
|
+
project: function(_, __, ___, expr) {
|
4684
|
+
return expr.search(f);
|
4685
|
+
},
|
4686
|
+
letCont: function(_, __, body, expr) {
|
4687
|
+
return body.search(f) || expr.search(f);
|
4688
|
+
},
|
4689
|
+
letJoin: function(_, __, ___, expr) {
|
4690
|
+
return expr.search(f);
|
4691
|
+
},
|
4692
|
+
other: function() {
|
4693
|
+
return false;
|
4694
|
+
}
|
4695
|
+
});
|
4696
|
+
};
|
4697
|
+
|
4698
|
+
Step.prototype.map = function(f) {
|
4699
|
+
return this.cases({
|
4700
|
+
"let": function(lval, value, expr) {
|
4701
|
+
return Step["let"](lval, value.mapSteps(f), f(expr));
|
4702
|
+
},
|
4703
|
+
project: function(lval, value, index, expr) {
|
4704
|
+
return Step.project(lval, value, index, f(expr));
|
4705
|
+
},
|
4706
|
+
letCont: function(name, args, body, expr) {
|
4707
|
+
return Step.letCont(name, args, f(body), f(expr));
|
4708
|
+
},
|
4709
|
+
other: function() {
|
4710
|
+
return this;
|
4711
|
+
}
|
4712
|
+
});
|
4713
|
+
};
|
4714
|
+
|
4715
|
+
Step.prototype.hasFree = function(varName) {
|
4716
|
+
return this.search(function(step) {
|
4717
|
+
return step.cases({
|
4718
|
+
"let": function(_, value, __) {
|
4719
|
+
return value.hasFree(varName);
|
4720
|
+
},
|
4721
|
+
next: function(cont, args) {
|
4722
|
+
return varName === cont || __indexOf.call(args, varName) >= 0;
|
4723
|
+
},
|
4724
|
+
app: function(fn, args, rescue, next) {
|
4725
|
+
return (varName === fn || varName === rescue || varName === next) || __indexOf.call(args, varName) >= 0;
|
4726
|
+
},
|
4727
|
+
query: function(annotations, arg, rescue, next) {
|
4728
|
+
return varName === arg || varName === rescue || varName === next;
|
4729
|
+
},
|
4730
|
+
localQuery: function(key, rescue, next) {
|
4731
|
+
return varName === rescue || varName === next;
|
4732
|
+
},
|
4733
|
+
"if": function(cond, trueCont, falseCont) {
|
4734
|
+
return varName === cond || varName === trueCont || varName === falseCont;
|
4735
|
+
},
|
4736
|
+
letJoin: function(name, order, cont) {
|
4737
|
+
return varName === order || varName === cont;
|
4738
|
+
},
|
4739
|
+
fork: function(conts) {
|
4740
|
+
return __indexOf.call(conts, varName) >= 0;
|
4741
|
+
},
|
4742
|
+
each: function(name, cont) {
|
4743
|
+
return varName === name || varName === cont;
|
4744
|
+
},
|
4745
|
+
other: function() {
|
4746
|
+
return false;
|
4747
|
+
}
|
4748
|
+
});
|
4749
|
+
});
|
4750
|
+
};
|
4751
|
+
|
4752
|
+
Step.prototype.genTrace = function(contName) {
|
4753
|
+
return this.cases({
|
4754
|
+
"let": function(_, __, e) {
|
4755
|
+
return e.genTrace(contName);
|
4756
|
+
},
|
4757
|
+
letCont: function(_, __, body, expr) {
|
4758
|
+
return body.genTrace(contName).concat(expr.genTrace(contName));
|
4759
|
+
},
|
4760
|
+
letJoin: function(_, __, cont, e) {
|
4761
|
+
var calls;
|
4762
|
+
calls = e.genTrace(contName);
|
4763
|
+
if (cont === contName) {
|
4764
|
+
return [ContinuationTrace.simple(this)].concat(calls);
|
4765
|
+
} else {
|
4766
|
+
return calls;
|
4767
|
+
}
|
4768
|
+
},
|
4769
|
+
app: function(_, __, rescue, next) {
|
4770
|
+
if (contName === rescue) {
|
4771
|
+
return [ContinuationTrace.key(this, 'rescue')];
|
4772
|
+
} else if (contName === next) {
|
4773
|
+
return [ContinuationTrace.key(this, 'next')];
|
4774
|
+
} else {
|
4775
|
+
return [];
|
4776
|
+
}
|
4777
|
+
},
|
4778
|
+
query: function(_, __, rescue, next) {
|
4779
|
+
if (contName === rescue) {
|
4780
|
+
return [ContinuationTrace.key(this, 'rescue')];
|
4781
|
+
} else if (contName === next) {
|
4782
|
+
return [ContinuationTrace.key(this, 'next')];
|
4783
|
+
} else {
|
4784
|
+
return [];
|
4785
|
+
}
|
4786
|
+
},
|
4787
|
+
localQuery: function(_, rescue, next) {
|
4788
|
+
if (contName === rescue) {
|
4789
|
+
return [ContinuationTrace.key(this, 'rescue')];
|
4790
|
+
} else if (contName === next) {
|
4791
|
+
return [ContinuationTrace.key(this, 'next')];
|
4792
|
+
} else {
|
4793
|
+
return [];
|
4794
|
+
}
|
4795
|
+
},
|
4796
|
+
next: function(name, _) {
|
4797
|
+
if (contName === name) {
|
4798
|
+
return [ContinuationTrace.simple(this)];
|
4799
|
+
} else {
|
4800
|
+
return [];
|
4801
|
+
}
|
4802
|
+
},
|
4803
|
+
fork: function(forks) {
|
4804
|
+
var fork, i, _i, _len;
|
4805
|
+
for (i = _i = 0, _len = forks.length; _i < _len; i = ++_i) {
|
4806
|
+
fork = forks[i];
|
4807
|
+
if (contName === fork) {
|
4808
|
+
return [ContinuationTrace.index(this, 'forks', i)];
|
4809
|
+
}
|
4810
|
+
}
|
4811
|
+
return [];
|
4812
|
+
},
|
4813
|
+
each: function(length, cont) {
|
4814
|
+
if (contName === cont) {
|
4815
|
+
return [ContinuationTrace.key(this, 'cont')];
|
4816
|
+
} else {
|
4817
|
+
return [];
|
4818
|
+
}
|
4819
|
+
},
|
4820
|
+
"if": function(_, trueCont, falseCont) {
|
4821
|
+
if (contName === trueCont) {
|
4822
|
+
return [ContinuationTrace.key(this, 'trueCont')];
|
4823
|
+
} else if (contName === falseCont) {
|
4824
|
+
return [ContinuationTrace.key(this, 'falseCont')];
|
4825
|
+
} else {
|
4826
|
+
return [];
|
4827
|
+
}
|
4828
|
+
}
|
4829
|
+
});
|
4830
|
+
};
|
4831
|
+
|
4832
|
+
Step.prototype.subst = function(varName, target) {
|
4833
|
+
return this.cases({
|
4834
|
+
"let": function(lval, value, expr) {
|
4835
|
+
value = value.subst(varName, target);
|
4836
|
+
if (varName === lval) {
|
4837
|
+
return Step["let"](lval, value, expr);
|
4838
|
+
}
|
4839
|
+
return Step["let"](lval, value, expr.subst(varName, target));
|
4840
|
+
},
|
4841
|
+
letJoin: function(name, order, cont, expr) {
|
4842
|
+
if (order === varName) {
|
4843
|
+
order = target;
|
4844
|
+
}
|
4845
|
+
if (cont === varName) {
|
4846
|
+
cont = target;
|
4847
|
+
}
|
4848
|
+
expr = expr.subst(varName, target);
|
4849
|
+
return Step.letJoin(name, order, cont, expr);
|
4850
|
+
},
|
4851
|
+
next: function(cont, args) {
|
4852
|
+
var arg, newArgs;
|
4853
|
+
if (cont === varName) {
|
4854
|
+
cont = target;
|
4855
|
+
}
|
4856
|
+
newArgs = (function() {
|
4857
|
+
var _i, _len, _results;
|
4858
|
+
_results = [];
|
4859
|
+
for (_i = 0, _len = args.length; _i < _len; _i++) {
|
4860
|
+
arg = args[_i];
|
4861
|
+
if (arg === varName) {
|
4862
|
+
_results.push(target);
|
4863
|
+
} else {
|
4864
|
+
_results.push(arg);
|
4865
|
+
}
|
4866
|
+
}
|
4867
|
+
return _results;
|
4868
|
+
})();
|
4869
|
+
return Step.next(cont, newArgs);
|
4870
|
+
},
|
4871
|
+
app: function(fn, args, rescue, next) {
|
4872
|
+
var arg;
|
4873
|
+
if (fn === varName) {
|
4874
|
+
fn = target;
|
4875
|
+
}
|
4876
|
+
if (rescue === varName) {
|
4877
|
+
rescue = target;
|
4878
|
+
}
|
4879
|
+
if (next === varName) {
|
4880
|
+
next = target;
|
4881
|
+
}
|
4882
|
+
args = (function() {
|
4883
|
+
var _i, _len, _results;
|
4884
|
+
_results = [];
|
4885
|
+
for (_i = 0, _len = args.length; _i < _len; _i++) {
|
4886
|
+
arg = args[_i];
|
4887
|
+
if (arg === varName) {
|
4888
|
+
_results.push(target);
|
4889
|
+
} else {
|
4890
|
+
_results.push(arg);
|
4891
|
+
}
|
4892
|
+
}
|
4893
|
+
return _results;
|
4894
|
+
})();
|
4895
|
+
return Step.app(fn, args, rescue, next);
|
4896
|
+
},
|
4897
|
+
query: function(annotations, arg, rescue, next) {
|
4898
|
+
if (arg === varName) {
|
4899
|
+
arg = target;
|
4900
|
+
}
|
4901
|
+
if (rescue === varName) {
|
4902
|
+
rescue = target;
|
4903
|
+
}
|
4904
|
+
if (next === varName) {
|
4905
|
+
next = target;
|
4906
|
+
}
|
4907
|
+
return Step.query(annotations, arg, rescue, next);
|
4908
|
+
},
|
4909
|
+
localQuery: function(key, rescue, next) {
|
4910
|
+
if (rescue === varName) {
|
4911
|
+
rescue = target;
|
4912
|
+
}
|
4913
|
+
if (next === varName) {
|
4914
|
+
next = target;
|
4915
|
+
}
|
4916
|
+
return Step.localQuery(key, rescue, next);
|
4917
|
+
},
|
4918
|
+
"if": function(cond, trueCont, falseCont) {
|
4919
|
+
if (cond === varName) {
|
4920
|
+
cond = target;
|
4921
|
+
}
|
4922
|
+
if (trueCont === varName) {
|
4923
|
+
trueCont = target;
|
4924
|
+
}
|
4925
|
+
if (falseCont === varName) {
|
4926
|
+
falseCont = target;
|
4927
|
+
}
|
4928
|
+
return Step["if"](cond, trueCont, falseCont);
|
4929
|
+
},
|
4930
|
+
each: function(name, cont) {
|
4931
|
+
if (name === varName) {
|
4932
|
+
name = target;
|
4933
|
+
}
|
4934
|
+
if (cont === varName) {
|
4935
|
+
cont = target;
|
4936
|
+
}
|
4937
|
+
return Step.each(name, cont);
|
4938
|
+
},
|
4939
|
+
other: function() {
|
4940
|
+
return this.map(function(x) {
|
4941
|
+
return x.subst(varName, target);
|
4942
|
+
});
|
4943
|
+
}
|
4944
|
+
});
|
4945
|
+
};
|
4946
|
+
|
4947
|
+
Step.prototype.substAll = function(varNames, targets) {
|
4948
|
+
var i, replaced, varName, _i, _len;
|
4949
|
+
replaced = this;
|
4950
|
+
for (i = _i = 0, _len = varNames.length; _i < _len; i = ++_i) {
|
4951
|
+
varName = varNames[i];
|
4952
|
+
replaced = replaced.subst(varName, targets[i]);
|
4953
|
+
}
|
4954
|
+
return replaced;
|
4955
|
+
};
|
4956
|
+
|
4957
|
+
Step.makeCont = function(arity, fBody, fExpr) {
|
4958
|
+
var contName, varNames;
|
4959
|
+
contName = nameGen('k');
|
4960
|
+
varNames = (function() {
|
4961
|
+
var _i, _results;
|
4962
|
+
_results = [];
|
4963
|
+
for (_i = 0; 0 <= arity ? _i < arity : _i > arity; 0 <= arity ? _i++ : _i--) {
|
4964
|
+
_results.push(nameGen('t'));
|
4965
|
+
}
|
4966
|
+
return _results;
|
4967
|
+
})();
|
4968
|
+
return Step.letCont(contName, varNames, fBody.apply(null, varNames), fExpr(contName));
|
4969
|
+
};
|
4970
|
+
|
4971
|
+
Step.makeVar = function(val, f) {
|
4972
|
+
var varName;
|
4973
|
+
varName = nameGen('t');
|
4974
|
+
return Step["let"](varName, val, f(varName));
|
4975
|
+
};
|
4976
|
+
|
4977
|
+
Step.makeJoin = function(order, outCont, f) {
|
4978
|
+
var joinName, mkBody;
|
4979
|
+
joinName = nameGen('j');
|
4980
|
+
mkBody = function(index) {
|
4981
|
+
return f(index, function(mappedVal) {
|
4982
|
+
return Step.next(joinName, [index, mappedVal]);
|
4983
|
+
});
|
4984
|
+
};
|
4985
|
+
return Step.makeCont(1, mkBody, function(bodyCont) {
|
4986
|
+
return Step.letJoin(joinName, order, outCont, Step.each(order, bodyCont));
|
4987
|
+
});
|
4988
|
+
};
|
4989
|
+
|
4990
|
+
Step.prototype.extendTrace = function(trace) {
|
4991
|
+
return this.cases;
|
4992
|
+
};
|
4993
|
+
|
4994
|
+
Step.prototype.walk = function(f) {
|
4995
|
+
var recurse;
|
4996
|
+
return (recurse = function(step, trace) {
|
4997
|
+
return f(step, trace, function(newStep) {
|
4998
|
+
var newTrace;
|
4999
|
+
newTrace = trace.extendWith(step);
|
5000
|
+
return recurse(newStep, newTrace);
|
5001
|
+
});
|
5002
|
+
})(this, Trace.empty());
|
5003
|
+
};
|
5004
|
+
|
5005
|
+
return Step;
|
5006
|
+
|
5007
|
+
})(Variant);
|
5008
|
+
|
5009
|
+
VarTrace = (function(_super) {
|
5010
|
+
__extends(VarTrace, _super);
|
5011
|
+
|
5012
|
+
function VarTrace() {
|
5013
|
+
_ref15 = VarTrace.__super__.constructor.apply(this, arguments);
|
5014
|
+
return _ref15;
|
5015
|
+
}
|
5016
|
+
|
5017
|
+
VarTrace.variants({
|
5018
|
+
value: ['val'],
|
5019
|
+
continued: ['continuation', 'index']
|
5020
|
+
});
|
5021
|
+
|
5022
|
+
VarTrace.prototype.equals = function(other) {
|
5023
|
+
if (this._tag !== other._tag) {
|
5024
|
+
return false;
|
5025
|
+
}
|
5026
|
+
return this.cases({
|
5027
|
+
value: function(val) {
|
5028
|
+
return val.equals(other.val);
|
5029
|
+
},
|
5030
|
+
continued: function(continuation, index) {
|
5031
|
+
return continuation === other.continuation && index === other.index;
|
5032
|
+
}
|
5033
|
+
});
|
5034
|
+
};
|
5035
|
+
|
5036
|
+
return VarTrace;
|
5037
|
+
|
5038
|
+
})(Variant);
|
5039
|
+
|
5040
|
+
ContinuationTrace = (function(_super) {
|
5041
|
+
__extends(ContinuationTrace, _super);
|
5042
|
+
|
5043
|
+
function ContinuationTrace() {
|
5044
|
+
_ref16 = ContinuationTrace.__super__.constructor.apply(this, arguments);
|
5045
|
+
return _ref16;
|
5046
|
+
}
|
5047
|
+
|
5048
|
+
ContinuationTrace.variants({
|
5049
|
+
simple: ['node'],
|
5050
|
+
key: ['node', 'key'],
|
5051
|
+
index: ['node', 'key', 'index']
|
5052
|
+
});
|
5053
|
+
|
5054
|
+
return ContinuationTrace;
|
5055
|
+
|
5056
|
+
})(Variant);
|
5057
|
+
|
5058
|
+
Trace = (function(_super) {
|
5059
|
+
__extends(Trace, _super);
|
5060
|
+
|
5061
|
+
function Trace() {
|
5062
|
+
_ref17 = Trace.__super__.constructor.apply(this, arguments);
|
5063
|
+
return _ref17;
|
5064
|
+
}
|
5065
|
+
|
5066
|
+
Trace.variants({
|
5067
|
+
empty: [],
|
5068
|
+
contTrace: ['parent', 'name', 'calls'],
|
5069
|
+
varTrace: ['parent', 'name', 'traces']
|
5070
|
+
});
|
5071
|
+
|
5072
|
+
Trace.prototype.boundNames = function() {
|
5073
|
+
return this.cases({
|
5074
|
+
empty: function() {
|
5075
|
+
return [];
|
5076
|
+
},
|
5077
|
+
contTrace: function(parent, name, _, __) {
|
5078
|
+
return parent.boundNames().concat([name]);
|
5079
|
+
},
|
5080
|
+
varTrace: function(parent, name, _) {
|
5081
|
+
return parent.boundNames().concat([name]);
|
5082
|
+
}
|
5083
|
+
});
|
5084
|
+
};
|
5085
|
+
|
5086
|
+
Trace.prototype.extendWith = function(step) {
|
5087
|
+
var _this = this;
|
5088
|
+
return step.cases({
|
5089
|
+
"let": function(lval, value, _) {
|
5090
|
+
return _this.traceVar(lval, VarTrace.value(value));
|
5091
|
+
},
|
5092
|
+
letCont: function(contName, argNames, body, expr) {
|
5093
|
+
var i, name, traced, _i, _len;
|
5094
|
+
traced = _this.traceCont(contName, step.genTrace(contName));
|
5095
|
+
for (i = _i = 0, _len = argNames.length; _i < _len; i = ++_i) {
|
5096
|
+
name = argNames[i];
|
5097
|
+
traced = traced.traceVar(name, VarTrace.continued(contName, i));
|
5098
|
+
}
|
5099
|
+
return traced;
|
5100
|
+
},
|
5101
|
+
letJoin: function(name) {
|
5102
|
+
return _this.traceCont(step.genTrace(name));
|
5103
|
+
},
|
5104
|
+
other: function() {
|
5105
|
+
return _this;
|
5106
|
+
}
|
5107
|
+
});
|
5108
|
+
};
|
5109
|
+
|
5110
|
+
Trace.prototype.inspect = function() {
|
5111
|
+
return "<" + (this.boundNames().join(' ')) + ">";
|
5112
|
+
};
|
5113
|
+
|
5114
|
+
Trace.prototype.traceCont = function(name, traces) {
|
5115
|
+
return Trace.contTrace(this, name, traces);
|
5116
|
+
};
|
5117
|
+
|
5118
|
+
Trace.prototype.traceVar = function(name, trace) {
|
5119
|
+
return Trace.varTrace(this, name, trace);
|
5120
|
+
};
|
5121
|
+
|
5122
|
+
Trace.prototype.findVarTrace = function(needle) {
|
5123
|
+
return this.cases({
|
5124
|
+
empty: function() {
|
5125
|
+
return null;
|
5126
|
+
},
|
5127
|
+
contTrace: function(parent) {
|
5128
|
+
return parent.findVarTrace(needle);
|
5129
|
+
},
|
5130
|
+
varTrace: function(parent, name, trace) {
|
5131
|
+
if (needle.equals(trace)) {
|
5132
|
+
return name;
|
5133
|
+
} else {
|
5134
|
+
return parent.findVarTrace(needle);
|
5135
|
+
}
|
5136
|
+
}
|
5137
|
+
});
|
5138
|
+
};
|
5139
|
+
|
5140
|
+
Trace.prototype.getVar = function(needle) {
|
5141
|
+
return this.cases({
|
5142
|
+
empty: function() {
|
5143
|
+
throw "no such variable " + needle;
|
5144
|
+
},
|
5145
|
+
contTrace: function(parent) {
|
5146
|
+
return parent.getVar(needle);
|
5147
|
+
},
|
5148
|
+
varTrace: function(parent, name, subst) {
|
5149
|
+
if (name === needle) {
|
5150
|
+
return subst;
|
5151
|
+
} else {
|
5152
|
+
return parent.getVar(needle);
|
5153
|
+
}
|
5154
|
+
}
|
5155
|
+
});
|
5156
|
+
};
|
5157
|
+
|
5158
|
+
Trace.prototype.getCont = function(needle) {
|
5159
|
+
return this.cases({
|
5160
|
+
empty: function() {
|
5161
|
+
throw "no such continuation " + needle;
|
5162
|
+
},
|
5163
|
+
contTrace: function(parent, name, traces) {
|
5164
|
+
if (name === needle) {
|
5165
|
+
return traces;
|
5166
|
+
} else {
|
5167
|
+
return parent.getCont(needle);
|
5168
|
+
}
|
5169
|
+
},
|
5170
|
+
varTrace: function(parent) {
|
5171
|
+
return parent.getCont(needle);
|
5172
|
+
}
|
5173
|
+
});
|
5174
|
+
};
|
5175
|
+
|
5176
|
+
return Trace;
|
5177
|
+
|
5178
|
+
})(Variant);
|
5179
|
+
|
5180
|
+
RVal = (function(_super) {
|
5181
|
+
__extends(RVal, _super);
|
5182
|
+
|
5183
|
+
function RVal() {
|
5184
|
+
_ref18 = RVal.__super__.constructor.apply(this, arguments);
|
5185
|
+
return _ref18;
|
5186
|
+
}
|
5187
|
+
|
5188
|
+
RVal.variants({
|
5189
|
+
constant: ['value'],
|
5190
|
+
global: [],
|
5191
|
+
lambda: ['args', 'rescue', 'next', 'body'],
|
5192
|
+
prim: ['arity', 'name', 'args'],
|
5193
|
+
list: ['elements'],
|
5194
|
+
project: ['val', 'index'],
|
5195
|
+
pair: ['first', 'second'],
|
5196
|
+
depair: ['val', 'key'],
|
5197
|
+
compact: ['val']
|
5198
|
+
});
|
5199
|
+
|
5200
|
+
RVal.prototype.equals = function(other) {
|
5201
|
+
if (this._tag !== other._tag) {
|
5202
|
+
return false;
|
5203
|
+
}
|
5204
|
+
return this.cases({
|
5205
|
+
constant: function(val) {
|
5206
|
+
return val === other.value;
|
5207
|
+
},
|
5208
|
+
global: function() {
|
5209
|
+
return true;
|
5210
|
+
},
|
5211
|
+
lambda: function() {
|
5212
|
+
return false;
|
5213
|
+
},
|
5214
|
+
prim: function(arity, name, args) {
|
5215
|
+
return arity === other.arity && name === other.name && equalArrays(args, other.args);
|
5216
|
+
},
|
5217
|
+
list: function(elements) {
|
5218
|
+
return equalArrays(elements, other.elements);
|
5219
|
+
},
|
5220
|
+
project: function(val, index) {
|
5221
|
+
return val === other.val && index === other.index;
|
5222
|
+
},
|
5223
|
+
pair: function(first, second) {
|
5224
|
+
return first === other.first && second === other.second;
|
5225
|
+
},
|
5226
|
+
depair: function(val, key) {
|
5227
|
+
return val === other.val;
|
5228
|
+
},
|
5229
|
+
compact: function(val) {
|
5230
|
+
return val === other.val;
|
5231
|
+
}
|
5232
|
+
});
|
5233
|
+
};
|
5234
|
+
|
5235
|
+
RVal.prototype.hasFree = function(varName) {
|
5236
|
+
return this.cases({
|
5237
|
+
lambda: function(args, rescue, next, body) {
|
5238
|
+
return __indexOf.call(args, varName) < 0 && (varName !== rescue && varName !== next) && body.hasFree(varName);
|
5239
|
+
},
|
5240
|
+
prim: function(_, __, args) {
|
5241
|
+
return __indexOf.call(args, varName) >= 0;
|
5242
|
+
},
|
5243
|
+
list: function(els) {
|
5244
|
+
return __indexOf.call(els, varName) >= 0;
|
5245
|
+
},
|
5246
|
+
project: function(val, index) {
|
5247
|
+
return varName === val || varName === index;
|
5248
|
+
},
|
5249
|
+
pair: function(first, second) {
|
5250
|
+
return varName === first || varName === second;
|
5251
|
+
},
|
5252
|
+
depair: function(val, key) {
|
5253
|
+
return varName === val;
|
5254
|
+
},
|
5255
|
+
compact: function(val) {
|
5256
|
+
return val === varName;
|
5257
|
+
},
|
5258
|
+
other: function() {
|
5259
|
+
return false;
|
5260
|
+
}
|
5261
|
+
});
|
5262
|
+
};
|
5263
|
+
|
5264
|
+
RVal.prototype.subst = function(varName, target) {
|
5265
|
+
return this.cases({
|
5266
|
+
prim: function(arity, name, args) {
|
5267
|
+
var arg, newArgs;
|
5268
|
+
newArgs = (function() {
|
5269
|
+
var _i, _len, _results;
|
5270
|
+
_results = [];
|
5271
|
+
for (_i = 0, _len = args.length; _i < _len; _i++) {
|
5272
|
+
arg = args[_i];
|
5273
|
+
if (arg === varName) {
|
5274
|
+
_results.push(target);
|
5275
|
+
} else {
|
5276
|
+
_results.push(arg);
|
5277
|
+
}
|
5278
|
+
}
|
5279
|
+
return _results;
|
5280
|
+
})();
|
5281
|
+
return RVal.prim(arity, name, newArgs);
|
5282
|
+
},
|
5283
|
+
list: function(els) {
|
5284
|
+
var el, newEls;
|
5285
|
+
newEls = (function() {
|
5286
|
+
var _i, _len, _results;
|
5287
|
+
_results = [];
|
5288
|
+
for (_i = 0, _len = els.length; _i < _len; _i++) {
|
5289
|
+
el = els[_i];
|
5290
|
+
if (el === varName) {
|
5291
|
+
_results.push(target);
|
5292
|
+
} else {
|
5293
|
+
_results.push(el);
|
5294
|
+
}
|
5295
|
+
}
|
5296
|
+
return _results;
|
5297
|
+
})();
|
5298
|
+
return RVal.list(newEls);
|
5299
|
+
},
|
5300
|
+
project: function(val, index) {
|
5301
|
+
if (val === varName) {
|
5302
|
+
val = target;
|
5303
|
+
}
|
5304
|
+
if (index === varName) {
|
5305
|
+
index = target;
|
5306
|
+
}
|
5307
|
+
return RVal.project(val, index);
|
5308
|
+
},
|
5309
|
+
compact: function(val) {
|
5310
|
+
if (val === varName) {
|
5311
|
+
val = target;
|
5312
|
+
}
|
5313
|
+
return RVal.compact(val);
|
5314
|
+
},
|
5315
|
+
pair: function(first, second) {
|
5316
|
+
if (first === varName) {
|
5317
|
+
first = target;
|
5318
|
+
}
|
5319
|
+
if (second === varName) {
|
5320
|
+
second = target;
|
5321
|
+
}
|
5322
|
+
return RVal.pair(first, second);
|
5323
|
+
},
|
5324
|
+
depair: function(val, key) {
|
5325
|
+
if (val === varName) {
|
5326
|
+
val = target;
|
5327
|
+
}
|
5328
|
+
return RVal.depair(val, key);
|
5329
|
+
},
|
5330
|
+
lambda: function() {
|
5331
|
+
throw 'TODO';
|
5332
|
+
},
|
5333
|
+
other: function() {
|
5334
|
+
return this;
|
5335
|
+
}
|
5336
|
+
});
|
5337
|
+
};
|
5338
|
+
|
5339
|
+
RVal.prototype.mapSteps = function() {
|
5340
|
+
return this.cases({
|
5341
|
+
lambda: function(a, r, n, b) {
|
5342
|
+
return RVal.lambda(a, r, n, f(b));
|
5343
|
+
},
|
5344
|
+
other: function() {
|
5345
|
+
return this;
|
5346
|
+
}
|
5347
|
+
});
|
5348
|
+
};
|
5349
|
+
|
5350
|
+
RVal.makeLambda = function(argNames, f) {
|
5351
|
+
var body, nextName, rescueName;
|
5352
|
+
rescueName = nameGen('rescue');
|
5353
|
+
nextName = nameGen('next');
|
5354
|
+
body = f(rescueName, nextName);
|
5355
|
+
return RVal.lambda(argNames, rescueName, nextName, body);
|
5356
|
+
};
|
5357
|
+
|
5358
|
+
RVal.prototype.inspect = function() {
|
5359
|
+
return this.cases({
|
5360
|
+
constant: function(value) {
|
5361
|
+
return "" + value;
|
5362
|
+
},
|
5363
|
+
global: function() {
|
5364
|
+
return '$';
|
5365
|
+
},
|
5366
|
+
prim: function(arity, name, args) {
|
5367
|
+
return "(`" + name + "`/" + arity + " " + (args.join(' ')) + ")";
|
5368
|
+
},
|
5369
|
+
project: function(val, index) {
|
5370
|
+
return "" + val + "[" + index + "]";
|
5371
|
+
},
|
5372
|
+
lambda: function(args, rescue, next, body) {
|
5373
|
+
return "(\\" + (args.join(' ')) + " =>" + rescue + " =>" + next + " " + (body.inspect()) + ")";
|
5374
|
+
},
|
5375
|
+
compact: function(val) {
|
5376
|
+
return "(COMPACT " + val + ")";
|
5377
|
+
},
|
5378
|
+
pair: function(first, second) {
|
5379
|
+
return "(PAIR " + first + " " + second + ")";
|
5380
|
+
},
|
5381
|
+
depair: function(val, key) {
|
5382
|
+
return "" + val + "." + key;
|
5383
|
+
},
|
5384
|
+
list: function(els) {
|
5385
|
+
return "[" + (els.join(', ')) + "]";
|
5386
|
+
}
|
5387
|
+
});
|
5388
|
+
};
|
5389
|
+
|
5390
|
+
return RVal;
|
5391
|
+
|
5392
|
+
})(Variant);
|
5393
|
+
|
5394
|
+
Gibbon.sequence = (function() {
|
5395
|
+
var bindExprs, sequence, sequenceTail;
|
5396
|
+
bindExprs = function(exprs, rescue, f) {
|
5397
|
+
var asyncExprs, asyncVars, bindAsyncVars, bound, collectionName, contName, expr, forkBodies, forks, i, idx, joinName, name, recurse, syncExprs, syncStep, syncVars, _, _i, _len;
|
5398
|
+
syncExprs = [];
|
5399
|
+
asyncExprs = [];
|
5400
|
+
bound = [];
|
5401
|
+
for (idx = _i = 0, _len = exprs.length; _i < _len; idx = ++_i) {
|
5402
|
+
expr = exprs[idx];
|
5403
|
+
if (expr.isAsync()) {
|
5404
|
+
asyncExprs.push([idx, expr]);
|
5405
|
+
} else {
|
5406
|
+
syncExprs.push([idx, expr]);
|
5407
|
+
}
|
5408
|
+
}
|
5409
|
+
if (asyncExprs.length === 1) {
|
5410
|
+
syncExprs.push(asyncExprs[0]);
|
5411
|
+
asyncExprs = [];
|
5412
|
+
}
|
5413
|
+
syncVars = (function() {
|
5414
|
+
var _j, _len1, _ref19, _results;
|
5415
|
+
_results = [];
|
5416
|
+
for (_j = 0, _len1 = syncExprs.length; _j < _len1; _j++) {
|
5417
|
+
_ref19 = syncExprs[_j], idx = _ref19[0], expr = _ref19[1];
|
5418
|
+
name = nameGen('t');
|
5419
|
+
bound[idx] = name;
|
5420
|
+
_results.push(name);
|
5421
|
+
}
|
5422
|
+
return _results;
|
5423
|
+
})();
|
5424
|
+
asyncVars = (function() {
|
5425
|
+
var _j, _len1, _ref19, _results;
|
5426
|
+
_results = [];
|
5427
|
+
for (_j = 0, _len1 = asyncExprs.length; _j < _len1; _j++) {
|
5428
|
+
_ref19 = asyncExprs[_j], idx = _ref19[0], expr = _ref19[1];
|
5429
|
+
_results.push(bound[idx] = nameGen('t'));
|
5430
|
+
}
|
5431
|
+
return _results;
|
5432
|
+
})();
|
5433
|
+
syncStep = (recurse = function(i) {
|
5434
|
+
var expr_, idx_, _ref19;
|
5435
|
+
if (i >= syncExprs.length) {
|
5436
|
+
return f(bound);
|
5437
|
+
}
|
5438
|
+
_ref19 = syncExprs[i], idx_ = _ref19[0], expr_ = _ref19[1];
|
5439
|
+
return sequence(expr_, rescue, function(boundExpr) {
|
5440
|
+
bound[idx_] = boundExpr;
|
5441
|
+
return recurse(i + 1);
|
5442
|
+
});
|
5443
|
+
})(0);
|
5444
|
+
if (!asyncExprs.length) {
|
5445
|
+
return syncStep;
|
5446
|
+
}
|
5447
|
+
joinName = nameGen('j');
|
5448
|
+
contName = nameGen('k');
|
5449
|
+
forkBodies = (function() {
|
5450
|
+
var _j, _len1, _ref19, _results;
|
5451
|
+
_results = [];
|
5452
|
+
for (i = _j = 0, _len1 = asyncExprs.length; _j < _len1; i = ++_j) {
|
5453
|
+
_ref19 = asyncExprs[i], _ = _ref19[0], expr = _ref19[1];
|
5454
|
+
_results.push(Step.makeVar(RVal.constant(i), function(boundI) {
|
5455
|
+
return sequence(expr, rescue, function(boundExpr) {
|
5456
|
+
return Step.next(joinName, [boundI, boundExpr]);
|
5457
|
+
});
|
5458
|
+
}));
|
5459
|
+
}
|
5460
|
+
return _results;
|
5461
|
+
})();
|
5462
|
+
collectionName = nameGen('c');
|
5463
|
+
bindAsyncVars = (recurse = function(i) {
|
5464
|
+
if (i >= asyncVars.length) {
|
5465
|
+
return syncStep;
|
5466
|
+
}
|
5467
|
+
return sequence(Core.constant(i), rescue, function(boundIndex) {
|
5468
|
+
var projection;
|
5469
|
+
projection = RVal.project(collectionName, boundIndex);
|
5470
|
+
return Step["let"](asyncVars[i], projection, recurse(i + 1));
|
5471
|
+
});
|
5472
|
+
})(0);
|
5473
|
+
forks = (function() {
|
5474
|
+
var _j, _len1, _results;
|
5475
|
+
_results = [];
|
5476
|
+
for (_j = 0, _len1 = forkBodies.length; _j < _len1; _j++) {
|
5477
|
+
_ = forkBodies[_j];
|
5478
|
+
_results.push(nameGen('f'));
|
5479
|
+
}
|
5480
|
+
return _results;
|
5481
|
+
})();
|
5482
|
+
return Step.letCont(contName, [collectionName], bindAsyncVars, Step.makeVar(RVal.constant(asyncExprs.length), function(order) {
|
5483
|
+
return Step.letJoin(joinName, order, contName, (recurse = function(i) {
|
5484
|
+
if (i >= forkBodies.length) {
|
5485
|
+
return Step.fork(forks);
|
5486
|
+
} else {
|
5487
|
+
return Step.letCont(forks[i], [], forkBodies[i], recurse(i + 1));
|
5488
|
+
}
|
5489
|
+
})(0));
|
5490
|
+
}));
|
5491
|
+
};
|
5492
|
+
sequence = function(core, rescue, bind) {
|
5493
|
+
return core.cases({
|
5494
|
+
variable: function(name) {
|
5495
|
+
return bind(name);
|
5496
|
+
},
|
5497
|
+
constant: function(val) {
|
5498
|
+
return Step.makeVar(RVal.constant(val), bind);
|
5499
|
+
},
|
5500
|
+
global: function() {
|
5501
|
+
return Step.makeVar(RVal.global(), bind);
|
5502
|
+
},
|
5503
|
+
bind: function(varName, valExpr, expr) {
|
5504
|
+
return sequence(valExpr, rescue, function(bound) {
|
5505
|
+
return sequence(expr.subst(varName, Core.variable(bound)), rescue, bind);
|
5506
|
+
});
|
5507
|
+
},
|
5508
|
+
block: function(argName, body) {
|
5509
|
+
return bind(RVal.makeLambda(argName, function(rescue, next) {
|
5510
|
+
return sequenceTail(body, rescue, next);
|
5511
|
+
}));
|
5512
|
+
},
|
5513
|
+
app: function(block, arg) {
|
5514
|
+
return Step.makeCont(1, bind, function(cont) {
|
5515
|
+
return bindExprs([block, arg], rescue, function(_arg) {
|
5516
|
+
var boundArg, boundBlock;
|
5517
|
+
boundBlock = _arg[0], boundArg = _arg[1];
|
5518
|
+
return Step.app(boundBlock, [boundArg], rescue, cont);
|
5519
|
+
});
|
5520
|
+
});
|
5521
|
+
},
|
5522
|
+
len: function(expr) {
|
5523
|
+
return sequence(expr, rescue, function(boundExpr) {
|
5524
|
+
return Step.makeVar(RVal.prim(1, 'length', [boundExpr]), bind);
|
5525
|
+
});
|
5526
|
+
},
|
5527
|
+
op1: function(op, arg) {
|
5528
|
+
return sequence(arg, rescue, function(boundArg) {
|
5529
|
+
return Step.makeVar(RVal.prim(1, op, [boundArg]), bind);
|
5530
|
+
});
|
5531
|
+
},
|
5532
|
+
op2: function(op, lhs, rhs) {
|
5533
|
+
return bindExprs([lhs, rhs], rescue, function(_arg) {
|
5534
|
+
var l, r;
|
5535
|
+
l = _arg[0], r = _arg[1];
|
5536
|
+
return Step.makeVar(RVal.prim(2, op, [l, r]), bind);
|
5537
|
+
});
|
5538
|
+
},
|
5539
|
+
query: function(expr, annotations) {
|
5540
|
+
return sequence(expr, rescue, function(boundExpr) {
|
5541
|
+
return Step.makeCont(1, bind, function(outCont) {
|
5542
|
+
return Step.query(annotations, boundExpr, rescue, outCont);
|
5543
|
+
});
|
5544
|
+
});
|
5545
|
+
},
|
5546
|
+
localQuery: function(key) {
|
5547
|
+
return Step.makeCont(1, bind, function(outCont) {
|
5548
|
+
return Step.localQuery(key, rescue, outCont);
|
5549
|
+
});
|
5550
|
+
},
|
5551
|
+
branch: function(cond, ifTrue, ifFalse) {
|
5552
|
+
return sequence(cond, rescue, function(boundCond) {
|
5553
|
+
return Step.makeCont(1, bind, function(joinCont) {
|
5554
|
+
var falseBody, trueBody;
|
5555
|
+
trueBody = sequenceTail(ifTrue, rescue, joinCont);
|
5556
|
+
falseBody = sequenceTail(ifFalse, rescue, joinCont);
|
5557
|
+
return Step.makeCont(0, (function() {
|
5558
|
+
return trueBody;
|
5559
|
+
}), function(trueCont) {
|
5560
|
+
return Step.makeCont(0, (function() {
|
5561
|
+
return falseBody;
|
5562
|
+
}), function(falseCont) {
|
5563
|
+
return Step["if"](boundCond, trueCont, falseCont);
|
5564
|
+
});
|
5565
|
+
});
|
5566
|
+
});
|
5567
|
+
});
|
5568
|
+
},
|
5569
|
+
fail: function(message) {
|
5570
|
+
return Step.makeVar(RVal.constant(message), function(boundMessage) {
|
5571
|
+
return Step.next(rescue, [boundMessage]);
|
5572
|
+
});
|
5573
|
+
},
|
5574
|
+
pair: function(first, second) {
|
5575
|
+
return bindExprs([first, second], rescue, function(_arg) {
|
5576
|
+
var f, s;
|
5577
|
+
f = _arg[0], s = _arg[1];
|
5578
|
+
return Step.makeVar(RVal.pair(f, s), bind);
|
5579
|
+
});
|
5580
|
+
},
|
5581
|
+
list: function(elements) {
|
5582
|
+
return bindExprs(elements, rescue, function(boundEls) {
|
5583
|
+
return Step.makeVar(RVal.list(boundEls), bind);
|
5584
|
+
});
|
5585
|
+
},
|
5586
|
+
squishList: function(list) {
|
5587
|
+
return list.cases({
|
5588
|
+
list: function(elements) {
|
5589
|
+
var NULL, e, rescued;
|
5590
|
+
NULL = Core.constant(null);
|
5591
|
+
rescued = (function() {
|
5592
|
+
var _i, _len, _results;
|
5593
|
+
_results = [];
|
5594
|
+
for (_i = 0, _len = elements.length; _i < _len; _i++) {
|
5595
|
+
e = elements[_i];
|
5596
|
+
_results.push(e.rescue(NULL));
|
5597
|
+
}
|
5598
|
+
return _results;
|
5599
|
+
})();
|
5600
|
+
return bindExprs(rescued, rescue, function(boundExprs) {
|
5601
|
+
return Step.makeVar(RVal.list(boundExprs), function(boundList) {
|
5602
|
+
return Step.makeVar(RVal.compact(boundList), bind);
|
5603
|
+
});
|
5604
|
+
});
|
5605
|
+
},
|
5606
|
+
other: function() {
|
5607
|
+
return sequence(list, rescue, function(boundList) {
|
5608
|
+
return Step.makeVar(RVal.compact(boundList), bind);
|
5609
|
+
});
|
5610
|
+
}
|
5611
|
+
});
|
5612
|
+
},
|
5613
|
+
mapList: function(list, arg, idxArg, body) {
|
5614
|
+
var joinName;
|
5615
|
+
joinName = nameGen('j');
|
5616
|
+
return sequence(list, rescue, function(boundList) {
|
5617
|
+
return Step.makeVar(RVal.prim(1, 'length', [boundList]), function(boundLen) {
|
5618
|
+
return Step.makeCont(1, bind, function(outCont) {
|
5619
|
+
return Step.makeJoin(boundLen, outCont, function(index, bindJoin) {
|
5620
|
+
var delistExpr, substBody;
|
5621
|
+
delistExpr = Core.variable(boundList).delist(Core.variable(index));
|
5622
|
+
substBody = body.subst(arg, delistExpr).subst(idxArg, Core.variable(index));
|
5623
|
+
return sequence(substBody, rescue, bindJoin);
|
5624
|
+
});
|
5625
|
+
});
|
5626
|
+
});
|
5627
|
+
});
|
5628
|
+
},
|
5629
|
+
filterList: function(list, arg, body) {
|
5630
|
+
var joinName, nullName;
|
5631
|
+
joinName = nameGen('j');
|
5632
|
+
nullName = nameGen('NULL');
|
5633
|
+
return sequence(list, rescue, function(boundList) {
|
5634
|
+
return Step["let"](nullName, RVal.constant(null), Step.makeVar(RVal.prim(1, 'length', [boundList]), function(boundLen) {
|
5635
|
+
var mkCompact;
|
5636
|
+
mkCompact = function(v) {
|
5637
|
+
return Step.makeVar(RVal.compact(v), bind);
|
5638
|
+
};
|
5639
|
+
return Step.makeCont(1, mkCompact, function(outCont) {
|
5640
|
+
return Step.makeJoin(boundLen, outCont, function(index, bindJoin) {
|
5641
|
+
var delistExpr, substBody;
|
5642
|
+
delistExpr = Core.variable(boundList).delist(Core.variable(index));
|
5643
|
+
substBody = body.subst(arg, delistExpr);
|
5644
|
+
return sequence(substBody, rescue, function(mappedBool) {
|
5645
|
+
var mkDelete, mkKeep;
|
5646
|
+
mkDelete = function() {
|
5647
|
+
return bindJoin(nullName);
|
5648
|
+
};
|
5649
|
+
mkKeep = function() {
|
5650
|
+
return sequence(delistExpr, rescue, bindJoin);
|
5651
|
+
};
|
5652
|
+
return Step.makeCont(0, mkDelete, function(deleteCont) {
|
5653
|
+
return Step.makeCont(0, mkKeep, function(keepCont) {
|
5654
|
+
return Step["if"](mappedBool, keepCont, deleteCont);
|
5655
|
+
});
|
5656
|
+
});
|
5657
|
+
});
|
5658
|
+
});
|
5659
|
+
});
|
5660
|
+
}));
|
5661
|
+
});
|
5662
|
+
},
|
5663
|
+
delist: function(expr, idxExpr) {
|
5664
|
+
return bindExprs([expr, idxExpr], rescue, function(_arg) {
|
5665
|
+
var boundExpr, boundIdx, e;
|
5666
|
+
boundExpr = _arg[0], boundIdx = _arg[1];
|
5667
|
+
e = nameGen('e');
|
5668
|
+
return Step["let"](e, RVal.project(boundExpr, boundIdx), bind(e));
|
5669
|
+
});
|
5670
|
+
},
|
5671
|
+
depair: function(expr, key) {
|
5672
|
+
return sequence(expr, rescue, function(boundExpr) {
|
5673
|
+
return Step.makeVar(RVal.depair(boundExpr, key), bind);
|
5674
|
+
});
|
5675
|
+
},
|
5676
|
+
rescue: function(expr, default_) {
|
5677
|
+
return Step.makeCont(1, bind, function(joinCont) {
|
5678
|
+
var defaultBody;
|
5679
|
+
defaultBody = sequenceTail(default_, rescue, joinCont);
|
5680
|
+
return Step.makeCont(0, (function() {
|
5681
|
+
return defaultBody;
|
5682
|
+
}), function(innerRescue) {
|
5683
|
+
return sequenceTail(expr, innerRescue, joinCont);
|
5684
|
+
});
|
5685
|
+
});
|
5686
|
+
},
|
5687
|
+
foldList: function(list, out, arg, accumArg, idxArg, body) {
|
5688
|
+
var v;
|
5689
|
+
v = Core.variable;
|
5690
|
+
if (body.isStrictIn(Core.variable(accumArg))) {
|
5691
|
+
return sequence(list, rescue, function(boundList) {
|
5692
|
+
return sequence(out, rescue, function(boundOut) {
|
5693
|
+
var decrIdx, lenExpr, loopName;
|
5694
|
+
loopName = nameGen('loop');
|
5695
|
+
decrIdx = Core.variable(idxArg).op2('-', Core.constant(1));
|
5696
|
+
lenExpr = Core.variable(boundList).len();
|
5697
|
+
return sequence(lenExpr, rescue, function(boundLen) {
|
5698
|
+
var escapeCond, loopBody;
|
5699
|
+
escapeCond = Core.variable(idxArg).op2('===', Core.constant(0));
|
5700
|
+
loopBody = sequence(escapeCond, rescue, function(boundEscape) {
|
5701
|
+
var continueBody, escapeBody;
|
5702
|
+
escapeBody = function() {
|
5703
|
+
return bind(accumArg);
|
5704
|
+
};
|
5705
|
+
continueBody = function() {
|
5706
|
+
return sequence(decrIdx, rescue, function(nextIdx) {
|
5707
|
+
var substBody;
|
5708
|
+
substBody = body.subst(arg, Core.delist(v(boundList), v(nextIdx))).subst(idxArg, Core.variable(nextIdx));
|
5709
|
+
return sequence(substBody, rescue, function(nextAccum) {
|
5710
|
+
return Step.next(loopName, [nextIdx, nextAccum]);
|
5711
|
+
});
|
5712
|
+
});
|
5713
|
+
};
|
5714
|
+
return Step.makeCont(0, escapeBody, function(escapeCont) {
|
5715
|
+
return Step.makeCont(0, continueBody, function(continueCont) {
|
5716
|
+
return Step["if"](boundEscape, escapeCont, continueCont);
|
5717
|
+
});
|
5718
|
+
});
|
5719
|
+
});
|
5720
|
+
return Step.letCont(loopName, [idxArg, accumArg], loopBody, Step.next(loopName, [boundLen, boundOut]));
|
5721
|
+
});
|
5722
|
+
});
|
5723
|
+
});
|
5724
|
+
} else if (!body.containsInNonTailPosition(Core.variable(accumArg))) {
|
5725
|
+
return sequence(list, rescue, function(boundList) {
|
5726
|
+
var lenExpr;
|
5727
|
+
lenExpr = Core.variable(boundList).len();
|
5728
|
+
return sequence(lenExpr, rescue, function(boundLen) {
|
5729
|
+
return Step.makeCont(1, bind, function(outCont) {
|
5730
|
+
var escapeBody, loopBody, loopName, processBody, testExpr;
|
5731
|
+
loopName = nameGen('l');
|
5732
|
+
escapeBody = function() {
|
5733
|
+
return sequenceTail(out, rescue, outCont);
|
5734
|
+
};
|
5735
|
+
processBody = function() {
|
5736
|
+
return sequence(v(idxArg).op2('+', Core.constant(1)), rescue, function(incr) {
|
5737
|
+
var substBody;
|
5738
|
+
substBody = body.subst(arg, Core.delist(v(boundList), v(idxArg))).subst(accumArg, Core.next(loopName, [incr]));
|
5739
|
+
return sequenceTail(substBody, rescue, outCont);
|
5740
|
+
});
|
5741
|
+
};
|
5742
|
+
testExpr = v(idxArg).op2('<', v(boundLen));
|
5743
|
+
loopBody = Step.makeCont(0, escapeBody, function(escape) {
|
5744
|
+
return Step.makeCont(0, processBody, function(process) {
|
5745
|
+
return sequence(testExpr, rescue, function(test) {
|
5746
|
+
return Step["if"](test, process, escape);
|
5747
|
+
});
|
5748
|
+
});
|
5749
|
+
});
|
5750
|
+
return Step.letCont(loopName, [idxArg], loopBody, Step.makeVar(RVal.constant(0), function(zero) {
|
5751
|
+
return Step.next(loopName, [zero]);
|
5752
|
+
}));
|
5753
|
+
});
|
5754
|
+
});
|
5755
|
+
});
|
5756
|
+
} else {
|
5757
|
+
debugger;
|
5758
|
+
body.isStrictIn(accumArg);
|
5759
|
+
throw 'TODO';
|
5760
|
+
}
|
5761
|
+
}
|
5762
|
+
});
|
5763
|
+
};
|
5764
|
+
sequenceTail = function(core, rescue, next) {
|
5765
|
+
return core.cases({
|
5766
|
+
next: function(cont, args) {
|
5767
|
+
return Step.next(cont, args);
|
5768
|
+
},
|
5769
|
+
other: function() {
|
5770
|
+
return sequence(core, rescue, function(bound) {
|
5771
|
+
return Step.next(next, [bound]);
|
5772
|
+
});
|
5773
|
+
}
|
5774
|
+
});
|
5775
|
+
};
|
5776
|
+
return function(core) {
|
5777
|
+
return sequenceTail(core, 'FAIL', 'RETURN');
|
5778
|
+
};
|
5779
|
+
})();
|
5780
|
+
|
5781
|
+
Gibbon.reduce = (function() {
|
5782
|
+
var betaReduce, reduceWithTrace;
|
5783
|
+
betaReduce = function(name, params, body, expr) {
|
5784
|
+
return expr.cases({
|
5785
|
+
next: function(contName, args) {
|
5786
|
+
if (name === contName) {
|
5787
|
+
DEBUG.log("beta-reducing " + contName + "(" + (params.join(' ')) + ") with " + (args.join(',')));
|
5788
|
+
return body.substAll(params, args);
|
5789
|
+
} else {
|
5790
|
+
return this;
|
5791
|
+
}
|
5792
|
+
},
|
5793
|
+
other: function() {
|
5794
|
+
return this.map(function(x) {
|
5795
|
+
return betaReduce(name, params, body, x);
|
5796
|
+
});
|
5797
|
+
}
|
5798
|
+
});
|
5799
|
+
};
|
5800
|
+
reduceWithTrace = function(step, trace) {
|
5801
|
+
return step.cases({
|
5802
|
+
"let": function(lval, value, expr) {
|
5803
|
+
var checkDup, goAbort, goConst, goSubst;
|
5804
|
+
checkDup = function(val) {
|
5805
|
+
var dupVar;
|
5806
|
+
dupVar = trace.findVarTrace(value);
|
5807
|
+
if (dupVar) {
|
5808
|
+
DEBUG.log("replacing duplicate variable " + lval + " with " + dupVar);
|
5809
|
+
return reduceWithTrace(expr.subst(lval, dupVar), trace);
|
5810
|
+
}
|
5811
|
+
};
|
5812
|
+
goSubst = function(varName) {
|
5813
|
+
return reduceWithTrace(expr.subst(lval, varName), trace);
|
5814
|
+
};
|
5815
|
+
goConst = function(c) {
|
5816
|
+
var constVal, dup, newTrace;
|
5817
|
+
constVal = RVal.constant(c);
|
5818
|
+
if ((dup = checkDup(constVal))) {
|
5819
|
+
return dup;
|
5820
|
+
}
|
5821
|
+
newTrace = trace.traceVar(lval, VarTrace.value(constVal));
|
5822
|
+
return Step["let"](lval, constVal, reduceWithTrace(expr, newTrace));
|
5823
|
+
};
|
5824
|
+
goAbort = function() {
|
5825
|
+
var dup, reduced;
|
5826
|
+
if ((dup = checkDup(value))) {
|
5827
|
+
return dup;
|
5828
|
+
}
|
5829
|
+
reduced = reduceWithTrace(expr, trace.extendWith(step));
|
5830
|
+
if (reduced.hasFree(lval)) {
|
5831
|
+
return Step["let"](lval, value, reduced);
|
5832
|
+
} else {
|
5833
|
+
DEBUG.log("removing dead variable " + lval);
|
5834
|
+
return reduced;
|
5835
|
+
}
|
5836
|
+
};
|
5837
|
+
return value.cases({
|
5838
|
+
prim: function(arity, op, args) {
|
5839
|
+
var checkIdent, constFold, identFold, left, leftTrace, leftVal, right, rightTrace, rightVal, vTrace;
|
5840
|
+
if (arity === 1) {
|
5841
|
+
vTrace = trace.getVar(args[0]);
|
5842
|
+
if (vTrace._tag === 'value' && vTrace.val._tag === 'constant') {
|
5843
|
+
return goConst(applyOp1(op, vTrace.val.value));
|
5844
|
+
} else {
|
5845
|
+
return goAbort();
|
5846
|
+
}
|
5847
|
+
} else if (arity === 2) {
|
5848
|
+
left = args[0], right = args[1];
|
5849
|
+
leftTrace = trace.getVar(left);
|
5850
|
+
rightTrace = trace.getVar(right);
|
5851
|
+
leftVal = leftTrace._tag === 'value' && leftTrace.val;
|
5852
|
+
rightVal = rightTrace._tag === 'value' && rightTrace.val;
|
5853
|
+
checkIdent = function(opTest, val, ident, identVal) {
|
5854
|
+
return op === opTest && val && val._tag === 'constant' && val.value === ident && identVal();
|
5855
|
+
};
|
5856
|
+
identFold = checkIdent('*', leftVal, 0, function() {
|
5857
|
+
return goConst(0);
|
5858
|
+
}) || checkIdent('*', rightVal, 0, function() {
|
5859
|
+
return goConst(0);
|
5860
|
+
}) || checkIdent('*', leftVal, 1, function() {
|
5861
|
+
return goSubst(right);
|
5862
|
+
}) || checkIdent('*', rightVal, 1, function() {
|
5863
|
+
return goSubst(left);
|
5864
|
+
}) || checkIdent('+', leftVal, 0, function() {
|
5865
|
+
return goSubst(right);
|
5866
|
+
}) || checkIdent('+', rightVal, 0, function() {
|
5867
|
+
return goSubst(left);
|
5868
|
+
}) || checkIdent('/', rightVal, 1, function() {
|
5869
|
+
return goSubst(left);
|
5870
|
+
});
|
5871
|
+
if (identFold) {
|
5872
|
+
DEBUG.log("identity-folding " + (this.inspect()) + " to " + identFold);
|
5873
|
+
return identFold;
|
5874
|
+
}
|
5875
|
+
if (leftVal && leftVal._tag === 'constant' && rightVal && rightVal._tag === 'constant') {
|
5876
|
+
constFold = applyOp2(op, leftVal.value, rightVal.value);
|
5877
|
+
DEBUG.log("constant-folding " + (this.inspect()) + " to " + constFold);
|
5878
|
+
return goConst(constFold);
|
5879
|
+
}
|
5880
|
+
return goAbort();
|
5881
|
+
}
|
5882
|
+
},
|
5883
|
+
other: function() {
|
5884
|
+
return goAbort();
|
5885
|
+
}
|
5886
|
+
});
|
5887
|
+
},
|
5888
|
+
letCont: function(contName, argNames, body, expr) {
|
5889
|
+
var extended, reducedBody, tryBeta;
|
5890
|
+
extended = trace.extendWith(this);
|
5891
|
+
tryBeta = function() {
|
5892
|
+
var betaReduced, reduced;
|
5893
|
+
if (reducedBody.hasFree(contName)) {
|
5894
|
+
DEBUG.log("skipping beta-reduction for recursive " + contName);
|
5895
|
+
betaReduced = expr;
|
5896
|
+
} else {
|
5897
|
+
betaReduced = betaReduce(contName, argNames, reducedBody, expr);
|
5898
|
+
}
|
5899
|
+
reduced = reduceWithTrace(betaReduced, extended);
|
5900
|
+
if (!reduced.hasFree(contName)) {
|
5901
|
+
DEBUG.log("removing dead continuation " + contName);
|
5902
|
+
return reduced;
|
5903
|
+
}
|
5904
|
+
return Step.letCont(contName, argNames, reducedBody, reduced);
|
5905
|
+
};
|
5906
|
+
reducedBody = reduceWithTrace(body, extended);
|
5907
|
+
return reducedBody.cases({
|
5908
|
+
next: function(innerName, innerArgs) {
|
5909
|
+
if (equalArrays(innerArgs, argNames)) {
|
5910
|
+
DEBUG.log("eta-reducing " + contName + " with " + innerName);
|
5911
|
+
return reduceWithTrace(expr.subst(contName, innerName), trace);
|
5912
|
+
} else {
|
5913
|
+
return tryBeta();
|
5914
|
+
}
|
5915
|
+
},
|
5916
|
+
other: function() {
|
5917
|
+
return tryBeta();
|
5918
|
+
}
|
5919
|
+
});
|
5920
|
+
},
|
5921
|
+
other: function() {
|
5922
|
+
var _this = this;
|
5923
|
+
return this.map(function(x) {
|
5924
|
+
return reduceWithTrace(x, trace.extendWith(_this));
|
5925
|
+
});
|
5926
|
+
}
|
5927
|
+
});
|
5928
|
+
};
|
5929
|
+
return function(step) {
|
5930
|
+
var reduced;
|
5931
|
+
DEBUG.log("before:");
|
5932
|
+
DEBUG.log(step.inspectLines());
|
5933
|
+
DEBUG.log();
|
5934
|
+
reduced = reduceWithTrace(step, Trace.empty());
|
5935
|
+
DEBUG.log("reduced to:");
|
5936
|
+
DEBUG.log(reduced.inspectLines());
|
5937
|
+
return reduced;
|
5938
|
+
};
|
5939
|
+
})();
|
5940
|
+
|
5941
|
+
Gibbon.JS = JS = (function(_super) {
|
5942
|
+
var inspectString, oldBlock, validIdent;
|
5943
|
+
|
5944
|
+
__extends(JS, _super);
|
5945
|
+
|
5946
|
+
function JS() {
|
5947
|
+
_ref19 = JS.__super__.constructor.apply(this, arguments);
|
5948
|
+
return _ref19;
|
5949
|
+
}
|
5950
|
+
|
5951
|
+
JS.variants({
|
5952
|
+
func: ['name', 'args', 'block'],
|
5953
|
+
json: ['obj'],
|
5954
|
+
block: ['statements'],
|
5955
|
+
ident: ['name'],
|
5956
|
+
funcall: ['callee', 'args'],
|
5957
|
+
literal: ['value'],
|
5958
|
+
array: ['elements'],
|
5959
|
+
object: ['keys', 'vals'],
|
5960
|
+
access: ['expr', 'name'],
|
5961
|
+
bind: ['lhs', 'rhs'],
|
5962
|
+
ternary: ['cond', 'ifTrue', 'ifFalse'],
|
5963
|
+
"if": ['cond', 'ifTrue', 'ifFalse'],
|
5964
|
+
"return": ['expr'],
|
5965
|
+
operator: ['operator', 'lhs', 'rhs'],
|
5966
|
+
forLoop: ['len', 'arg', 'body'],
|
5967
|
+
whileLoop: ['cond', 'body'],
|
5968
|
+
varDecl: ['name']
|
5969
|
+
});
|
5970
|
+
|
5971
|
+
oldBlock = JS.block;
|
5972
|
+
|
5973
|
+
JS.block = function(s) {
|
5974
|
+
if (!isArray(s)) {
|
5975
|
+
throw 'lol';
|
5976
|
+
}
|
5977
|
+
return oldBlock(s);
|
5978
|
+
};
|
5979
|
+
|
5980
|
+
JS.prototype.toFunction = function() {
|
5981
|
+
return this.cases({
|
5982
|
+
func: function(name, args, block) {
|
5983
|
+
return (function(func, args, ctor) {
|
5984
|
+
ctor.prototype = func.prototype;
|
5985
|
+
var child = new ctor, result = func.apply(child, args);
|
5986
|
+
return Object(result) === result ? result : child;
|
5987
|
+
})(Function, __slice.call(args).concat([block.toJS()]), function(){});
|
5988
|
+
},
|
5989
|
+
other: function() {
|
5990
|
+
return JS.func('tmp', [], this).toFunction();
|
5991
|
+
}
|
5992
|
+
});
|
5993
|
+
};
|
5994
|
+
|
5995
|
+
JS.trap = function(cond) {
|
5996
|
+
return JS["if"](cond, JS["return"](null), null);
|
5997
|
+
};
|
5998
|
+
|
5999
|
+
JS.iife = function(statements) {
|
6000
|
+
return JS.funcall(JS.func(null, [], JS.block(statements)), []);
|
6001
|
+
};
|
6002
|
+
|
6003
|
+
JS.tailcall = function(fn, args) {
|
6004
|
+
return JS["return"](JS.funcall(fn, args));
|
6005
|
+
};
|
6006
|
+
|
6007
|
+
JS.prototype.op = function(o, other) {
|
6008
|
+
return JS.operator(o, this, other);
|
6009
|
+
};
|
6010
|
+
|
6011
|
+
JS.prototype.eq = function(other) {
|
6012
|
+
return this.op('===', other);
|
6013
|
+
};
|
6014
|
+
|
6015
|
+
JS.prototype.lt = function(other) {
|
6016
|
+
return this.op('<', other);
|
6017
|
+
};
|
6018
|
+
|
6019
|
+
JS.prototype.funcall = function(args) {
|
6020
|
+
return JS.funcall(this, args);
|
6021
|
+
};
|
6022
|
+
|
6023
|
+
JS.prototype.tailcall = function(args) {
|
6024
|
+
return JS.tailcall(this, args);
|
6025
|
+
};
|
6026
|
+
|
6027
|
+
JS.prototype.methodcall = function(name, args) {
|
6028
|
+
return this.access(JS.literal(name)).funcall(args);
|
6029
|
+
};
|
6030
|
+
|
6031
|
+
JS.prototype.access = function(key) {
|
6032
|
+
return JS.access(this, key);
|
6033
|
+
};
|
6034
|
+
|
6035
|
+
JS.trampoline = function(varName) {
|
6036
|
+
var cond, v;
|
6037
|
+
v = JS.ident(varName);
|
6038
|
+
cond = JS.ident('typeof').funcall([v]).op('===', JS.literal('function'));
|
6039
|
+
return JS.whileLoop(cond, JS.bind(v, v.funcall([])));
|
6040
|
+
};
|
6041
|
+
|
6042
|
+
inspectString = function(str) {
|
6043
|
+
var escaped;
|
6044
|
+
escaped = str.replace("\\", "\\\\").replace('"', '\\"').replace("\n", "\\n");
|
6045
|
+
return "\"" + escaped + "\"";
|
6046
|
+
};
|
6047
|
+
|
6048
|
+
JS.prototype.inspect = function() {
|
6049
|
+
return "[JS " + (this.toJS()) + "]";
|
6050
|
+
};
|
6051
|
+
|
6052
|
+
validIdent = /^[\w$][\w\d$]*$/;
|
6053
|
+
|
6054
|
+
JS.prototype.toJS = function(indent, block) {
|
6055
|
+
var I, i;
|
6056
|
+
if (indent == null) {
|
6057
|
+
indent = 0;
|
6058
|
+
}
|
6059
|
+
i = Array(indent + 1).join(' ');
|
6060
|
+
I = block ? i : '';
|
6061
|
+
return this.cases({
|
6062
|
+
func: function(name, args, block) {
|
6063
|
+
var header;
|
6064
|
+
header = name ? "function " + name : "function";
|
6065
|
+
return "" + I + header + "(" + (args.join(', ')) + ") " + (block.toJS(indent));
|
6066
|
+
},
|
6067
|
+
json: function(obj) {
|
6068
|
+
return JSON.stringify(obj);
|
6069
|
+
},
|
6070
|
+
ident: function(name) {
|
6071
|
+
return name;
|
6072
|
+
},
|
6073
|
+
array: function(els) {
|
6074
|
+
var e;
|
6075
|
+
return "" + I + "[" + (((function() {
|
6076
|
+
var _i, _len, _results;
|
6077
|
+
_results = [];
|
6078
|
+
for (_i = 0, _len = els.length; _i < _len; _i++) {
|
6079
|
+
e = els[_i];
|
6080
|
+
_results.push(e.toJS(indent));
|
6081
|
+
}
|
6082
|
+
return _results;
|
6083
|
+
})()).join(', ')) + "]";
|
6084
|
+
},
|
6085
|
+
object: function(keys, vals) {
|
6086
|
+
var k, pairs;
|
6087
|
+
pairs = (function() {
|
6088
|
+
var _i, _len, _results;
|
6089
|
+
_results = [];
|
6090
|
+
for (i = _i = 0, _len = keys.length; _i < _len; i = ++_i) {
|
6091
|
+
k = keys[i];
|
6092
|
+
_results.push("" + (inspectString(k)) + ": " + (vals[i].toJS()));
|
6093
|
+
}
|
6094
|
+
return _results;
|
6095
|
+
})();
|
6096
|
+
return "" + I + "{ " + (pairs.join(', ')) + " }";
|
6097
|
+
},
|
6098
|
+
funcall: function(callee, args) {
|
6099
|
+
var a;
|
6100
|
+
args = ((function() {
|
6101
|
+
var _i, _len, _results;
|
6102
|
+
_results = [];
|
6103
|
+
for (_i = 0, _len = args.length; _i < _len; _i++) {
|
6104
|
+
a = args[_i];
|
6105
|
+
_results.push(a.toJS(indent));
|
6106
|
+
}
|
6107
|
+
return _results;
|
6108
|
+
})()).join(', ');
|
6109
|
+
return "" + I + (callee.toJS(indent)) + "(" + args + ")";
|
6110
|
+
},
|
6111
|
+
literal: function(value) {
|
6112
|
+
if (typeof value === 'string') {
|
6113
|
+
return inspectString(value);
|
6114
|
+
}
|
6115
|
+
return '' + value;
|
6116
|
+
},
|
6117
|
+
"if": function(cond, ifTrue, ifFalse) {
|
6118
|
+
var elseBranch;
|
6119
|
+
elseBranch = "";
|
6120
|
+
if (ifFalse) {
|
6121
|
+
elseBranch = "\n" + i + "else " + (ifFalse.toJS(indent));
|
6122
|
+
}
|
6123
|
+
return "" + I + "if (" + (cond.toJS(indent, true)) + ") " + (ifTrue.toJS(indent)) + elseBranch;
|
6124
|
+
},
|
6125
|
+
block: function(statements) {
|
6126
|
+
var s;
|
6127
|
+
return "" + I + "{\n" + (((function() {
|
6128
|
+
var _i, _len, _results;
|
6129
|
+
_results = [];
|
6130
|
+
for (_i = 0, _len = statements.length; _i < _len; _i++) {
|
6131
|
+
s = statements[_i];
|
6132
|
+
_results.push(s.toJS(indent + 1, true) + ';');
|
6133
|
+
}
|
6134
|
+
return _results;
|
6135
|
+
})()).join('\n')) + "\n" + i + "}";
|
6136
|
+
},
|
6137
|
+
forLoop: function(len, arg, body) {
|
6138
|
+
arg = arg.toJS(indent);
|
6139
|
+
len = len.toJS(indent);
|
6140
|
+
body = body.toJS(indent);
|
6141
|
+
return "" + i + "for (var " + arg + "=0; " + arg + "<" + len + "; " + arg + "+=1) " + body;
|
6142
|
+
},
|
6143
|
+
whileLoop: function(cond, body) {
|
6144
|
+
return "" + I + "while (" + (cond.toJS(indent)) + ") " + (body.toJS(indent));
|
6145
|
+
},
|
6146
|
+
varDecl: function(name) {
|
6147
|
+
return "" + i + "var " + name;
|
6148
|
+
},
|
6149
|
+
access: function(expr, name) {
|
6150
|
+
var access;
|
6151
|
+
access = name._tag === 'literal' && typeof name.value === 'string' && validIdent.test(name.value) ? "." + name.value : "[" + (name.toJS(indent)) + "]";
|
6152
|
+
return "" + (expr.toJS(indent)) + access;
|
6153
|
+
},
|
6154
|
+
bind: function(lhs, rhs) {
|
6155
|
+
return "" + i + (lhs.toJS(indent)) + " = " + (rhs.toJS(indent));
|
6156
|
+
},
|
6157
|
+
"return": function(expr) {
|
6158
|
+
if (expr) {
|
6159
|
+
return "" + I + "return " + (expr.toJS(indent));
|
6160
|
+
} else {
|
6161
|
+
return "" + I + "return";
|
6162
|
+
}
|
6163
|
+
},
|
6164
|
+
operator: function(op, lhs, rhs) {
|
6165
|
+
return "(" + (lhs.toJS(indent)) + ")" + op + "(" + (rhs.toJS(indent)) + ")";
|
6166
|
+
}
|
6167
|
+
});
|
6168
|
+
};
|
6169
|
+
|
6170
|
+
return JS;
|
6171
|
+
|
6172
|
+
})(Variant);
|
6173
|
+
|
6174
|
+
Gibbon.codegen = (function() {
|
6175
|
+
var generate, inlinePrim;
|
6176
|
+
inlinePrim = {
|
6177
|
+
2: {
|
6178
|
+
'+': function(_arg) {
|
6179
|
+
var l, r;
|
6180
|
+
l = _arg[0], r = _arg[1];
|
6181
|
+
return l.op('+', r);
|
6182
|
+
},
|
6183
|
+
'*': function(_arg) {
|
6184
|
+
var l, r;
|
6185
|
+
l = _arg[0], r = _arg[1];
|
6186
|
+
return l.op('*', r);
|
6187
|
+
},
|
6188
|
+
'-': function(_arg) {
|
6189
|
+
var l, r;
|
6190
|
+
l = _arg[0], r = _arg[1];
|
6191
|
+
return l.op('-', r);
|
6192
|
+
},
|
6193
|
+
'/': function(_arg) {
|
6194
|
+
var l, r;
|
6195
|
+
l = _arg[0], r = _arg[1];
|
6196
|
+
return l.op('/', r);
|
6197
|
+
},
|
6198
|
+
'<': function(_arg) {
|
6199
|
+
var l, r;
|
6200
|
+
l = _arg[0], r = _arg[1];
|
6201
|
+
return l.op('<', r);
|
6202
|
+
},
|
6203
|
+
'<=': function(_arg) {
|
6204
|
+
var l, r;
|
6205
|
+
l = _arg[0], r = _arg[1];
|
6206
|
+
return l.op('<=', r);
|
6207
|
+
},
|
6208
|
+
'>': function(_arg) {
|
6209
|
+
var l, r;
|
6210
|
+
l = _arg[0], r = _arg[1];
|
6211
|
+
return l.op('>', r);
|
6212
|
+
},
|
6213
|
+
'>=': function(_arg) {
|
6214
|
+
var l, r;
|
6215
|
+
l = _arg[0], r = _arg[1];
|
6216
|
+
return l.op('>=', r);
|
6217
|
+
},
|
6218
|
+
'===': function(_arg) {
|
6219
|
+
var l, r;
|
6220
|
+
l = _arg[0], r = _arg[1];
|
6221
|
+
return l.op('===', r);
|
6222
|
+
}
|
6223
|
+
},
|
6224
|
+
1: {
|
6225
|
+
length: function(_arg) {
|
6226
|
+
var l;
|
6227
|
+
l = _arg[0];
|
6228
|
+
return l.access(JS.literal('length'));
|
6229
|
+
},
|
6230
|
+
'!': function(_arg) {
|
6231
|
+
var a;
|
6232
|
+
a = _arg[0];
|
6233
|
+
return JS.funcall(JS.ident('!'), [a]);
|
6234
|
+
},
|
6235
|
+
'-': function(_arg) {
|
6236
|
+
var a;
|
6237
|
+
a = _arg[0];
|
6238
|
+
return JS.funcall(JS.ident('-'), [a]);
|
6239
|
+
}
|
6240
|
+
}
|
6241
|
+
};
|
6242
|
+
generate = function(term, trace, push) {
|
6243
|
+
var extended, varToJS;
|
6244
|
+
varToJS = function(varName) {
|
6245
|
+
return trace.getVar(varName).cases({
|
6246
|
+
continued: function() {
|
6247
|
+
return JS.ident(varName);
|
6248
|
+
},
|
6249
|
+
value: function(val) {
|
6250
|
+
return val.cases({
|
6251
|
+
constant: function(v) {
|
6252
|
+
return JS.literal(v);
|
6253
|
+
},
|
6254
|
+
global: function() {
|
6255
|
+
return JS.ident('$');
|
6256
|
+
},
|
6257
|
+
lambda: function() {
|
6258
|
+
throw 'TODO';
|
6259
|
+
},
|
6260
|
+
prim: function(arity, name, args) {
|
6261
|
+
var a, jsArgs;
|
6262
|
+
jsArgs = (function() {
|
6263
|
+
var _i, _len, _results;
|
6264
|
+
_results = [];
|
6265
|
+
for (_i = 0, _len = args.length; _i < _len; _i++) {
|
6266
|
+
a = args[_i];
|
6267
|
+
_results.push(varToJS(a));
|
6268
|
+
}
|
6269
|
+
return _results;
|
6270
|
+
})();
|
6271
|
+
return inlinePrim[arity][name](jsArgs);
|
6272
|
+
},
|
6273
|
+
list: function(els) {
|
6274
|
+
var e;
|
6275
|
+
return JS.array((function() {
|
6276
|
+
var _i, _len, _results;
|
6277
|
+
_results = [];
|
6278
|
+
for (_i = 0, _len = els.length; _i < _len; _i++) {
|
6279
|
+
e = els[_i];
|
6280
|
+
_results.push(varToJS(e));
|
6281
|
+
}
|
6282
|
+
return _results;
|
6283
|
+
})());
|
6284
|
+
},
|
6285
|
+
pair: function(first, second) {
|
6286
|
+
return JS.object(['first', 'second'], [varToJS(first), varToJS(second)]);
|
6287
|
+
},
|
6288
|
+
depair: function(val, key) {
|
6289
|
+
return varToJS(val).access(JS.literal(key));
|
6290
|
+
},
|
6291
|
+
project: function(val, index) {
|
6292
|
+
return varToJS(val).access(varToJS(index));
|
6293
|
+
},
|
6294
|
+
compact: function() {
|
6295
|
+
return JS.ident(varName);
|
6296
|
+
}
|
6297
|
+
});
|
6298
|
+
}
|
6299
|
+
});
|
6300
|
+
};
|
6301
|
+
extended = trace.extendWith(term);
|
6302
|
+
return term.cases({
|
6303
|
+
"let": function(name, val, expr) {
|
6304
|
+
var _compact, _i, _len, _push;
|
6305
|
+
if (val._tag === 'compact') {
|
6306
|
+
val = varToJS(val.val);
|
6307
|
+
_i = JS.ident('_i');
|
6308
|
+
_compact = JS.ident('_compact');
|
6309
|
+
_len = JS.ident('_len');
|
6310
|
+
_push = JS.access(_compact, JS.literal('push'));
|
6311
|
+
push(JS.varDecl(name));
|
6312
|
+
push(JS.bind(JS.ident(name), JS.iife([JS.varDecl('_compact'), JS.varDecl('_len'), JS.bind(_len, JS.access(val, JS.literal('length'))), JS.bind(_compact, JS.array([])), JS.forLoop(_len, _i, JS.block([JS["if"](JS.access(val, _i).op('!=', JS.literal(null)), JS.funcall(_push, [JS.access(val, _i)]), null)])), JS["return"](_compact)])));
|
6313
|
+
}
|
6314
|
+
return generate(expr, extended, push);
|
6315
|
+
},
|
6316
|
+
letCont: function(name, args, body, expr) {
|
6317
|
+
var bodyStatements;
|
6318
|
+
bodyStatements = [];
|
6319
|
+
generate(body, extended, function(s) {
|
6320
|
+
return bodyStatements.push(s);
|
6321
|
+
});
|
6322
|
+
push(JS.func(name, args, JS.block(bodyStatements)));
|
6323
|
+
return generate(expr, extended, push);
|
6324
|
+
},
|
6325
|
+
letJoin: function(name, order, cont, expr) {
|
6326
|
+
var counterName, resultsName;
|
6327
|
+
counterName = "" + name + "$counter";
|
6328
|
+
resultsName = "" + name + "$results";
|
6329
|
+
push(JS.varDecl(counterName));
|
6330
|
+
push(JS.varDecl(resultsName));
|
6331
|
+
push(JS.bind(JS.ident(counterName), varToJS(order)));
|
6332
|
+
push(JS.bind(JS.ident(resultsName), JS.array([])));
|
6333
|
+
push(JS.func(name, ['idx', 'el'], JS.block([JS.bind(JS.access(JS.ident(resultsName), JS.ident('idx')), JS.ident('el')), JS.trap(JS.ident(counterName).op('-=', JS.literal(1)).op('>', JS.literal(0))), JS.ident(cont).tailcall([JS.ident(resultsName)])])));
|
6334
|
+
return generate(expr, extended, push);
|
6335
|
+
},
|
6336
|
+
fork: function(forks) {
|
6337
|
+
var cont, head, last, thunk, _i, _j, _len;
|
6338
|
+
head = 2 <= forks.length ? __slice.call(forks, 0, _i = forks.length - 1) : (_i = 0, []), last = forks[_i++];
|
6339
|
+
thunk = nameGen('_thunk');
|
6340
|
+
push(JS.varDecl(thunk));
|
6341
|
+
for (_j = 0, _len = head.length; _j < _len; _j++) {
|
6342
|
+
cont = head[_j];
|
6343
|
+
push(JS.bind(JS.ident(thunk), JS.ident(cont).funcall([])));
|
6344
|
+
push(JS.trampoline(thunk));
|
6345
|
+
}
|
6346
|
+
return push(JS.ident(last).tailcall([]));
|
6347
|
+
},
|
6348
|
+
each: function(length, cont) {
|
6349
|
+
var minus1, thunk, _i;
|
6350
|
+
_i = JS.ident('_i');
|
6351
|
+
thunk = nameGen('_thunk');
|
6352
|
+
push(JS.varDecl(thunk));
|
6353
|
+
minus1 = varToJS(length).op('+', JS.literal(-1));
|
6354
|
+
push(JS.forLoop(minus1, _i, JS.block([JS.bind(JS.ident(thunk), JS.ident(cont).funcall([_i])), JS.trampoline(thunk)])));
|
6355
|
+
return push(JS.ident(cont).tailcall([minus1]));
|
6356
|
+
},
|
6357
|
+
next: function(cont, args) {
|
6358
|
+
var a;
|
6359
|
+
return push(JS.ident(cont).tailcall((function() {
|
6360
|
+
var _i, _len, _results;
|
6361
|
+
_results = [];
|
6362
|
+
for (_i = 0, _len = args.length; _i < _len; _i++) {
|
6363
|
+
a = args[_i];
|
6364
|
+
_results.push(varToJS(a));
|
6365
|
+
}
|
6366
|
+
return _results;
|
6367
|
+
})()));
|
6368
|
+
},
|
6369
|
+
app: function(fn, args, rescue, next) {
|
6370
|
+
var a;
|
6371
|
+
args = (function() {
|
6372
|
+
var _i, _len, _results;
|
6373
|
+
_results = [];
|
6374
|
+
for (_i = 0, _len = args.length; _i < _len; _i++) {
|
6375
|
+
a = args[_i];
|
6376
|
+
_results.push(varToJS(a));
|
6377
|
+
}
|
6378
|
+
return _results;
|
6379
|
+
})();
|
6380
|
+
return push(varToJS(fn).tailcall(__slice.call(args).concat([JS.ident(rescue)], [JS.ident(next)])));
|
6381
|
+
},
|
6382
|
+
query: function(annotations, arg, rescue, next) {
|
6383
|
+
return push(JS.ident('QUERY').tailcall([varToJS(arg), JS.json(annotations), JS.ident(rescue), JS.ident(next)]));
|
6384
|
+
},
|
6385
|
+
localQuery: function(key, rescue, next) {
|
6386
|
+
return push(JS.ident('QUERY').tailcall([JS.literal(null), JS.literal(key), JS.ident(rescue), JS.ident(next)]));
|
6387
|
+
},
|
6388
|
+
"if": function(cond, trueCont, falseCont) {
|
6389
|
+
return push(JS["if"](varToJS(cond), JS.ident(trueCont).tailcall([]), JS.ident(falseCont).tailcall([])));
|
6390
|
+
}
|
6391
|
+
});
|
6392
|
+
};
|
6393
|
+
return function(term) {
|
6394
|
+
var out, statements;
|
6395
|
+
statements = [];
|
6396
|
+
generate(term, Trace.empty(), function(s) {
|
6397
|
+
return statements.push(s);
|
6398
|
+
});
|
6399
|
+
out = JS.func('compiled', ['$', 'QUERY', 'FAIL', 'RETURN'], JS.block(statements));
|
6400
|
+
DEBUG.log('GENERATED:');
|
6401
|
+
DEBUG.log(out.toJS());
|
6402
|
+
return out;
|
6403
|
+
};
|
6404
|
+
})();
|
6405
|
+
|
6406
|
+
stdlib = Gibbon.stdlib = (function() {
|
6407
|
+
var FALSE, TRUE, equals;
|
6408
|
+
TRUE = Core.constant(true);
|
6409
|
+
FALSE = Core.constant(false);
|
6410
|
+
equals = function(x, y, type) {
|
6411
|
+
var direct;
|
6412
|
+
direct = function() {
|
6413
|
+
return x.op2('===', y);
|
6414
|
+
};
|
6415
|
+
return type.cases({
|
6416
|
+
entity: direct,
|
6417
|
+
numeric: direct,
|
6418
|
+
string: direct,
|
6419
|
+
bool: direct,
|
6420
|
+
block: function() {
|
6421
|
+
return FALSE;
|
6422
|
+
},
|
6423
|
+
pair: function() {
|
6424
|
+
var eq1, eq2;
|
6425
|
+
eq1 = equals(x.depair('first'), y.depair('first'), type.first);
|
6426
|
+
eq2 = equals(x.depair('first'), y.depair('second'), type.second);
|
6427
|
+
return eq1.branch(eq2, FALSE);
|
6428
|
+
},
|
6429
|
+
list: function() {
|
6430
|
+
var eqEls, eqLength;
|
6431
|
+
eqLength = equals(x.len(), y.len(), Type.numeric());
|
6432
|
+
eqEls = x.zipList(y).foldList(TRUE, function(pair, next) {
|
6433
|
+
var eq;
|
6434
|
+
eq = equals(pair.depair('first'), pair.depair('second'), type.of);
|
6435
|
+
return next.branch(FALSE, eq);
|
6436
|
+
});
|
6437
|
+
return eqLength.branch(eqEls, FALSE);
|
6438
|
+
}
|
6439
|
+
});
|
6440
|
+
};
|
6441
|
+
return {
|
6442
|
+
"case": {
|
6443
|
+
type: parse.type('case [bool : %b] = % -> %b'),
|
6444
|
+
compile: function(_, _arg) {
|
6445
|
+
var alts;
|
6446
|
+
alts = _arg[0];
|
6447
|
+
return alts.foldList(Core.fail('non-exhaustive cases'), function(el, next) {
|
6448
|
+
return el.depair('first').branch(el.depair('second'), next);
|
6449
|
+
});
|
6450
|
+
}
|
6451
|
+
},
|
6452
|
+
"case-eq": {
|
6453
|
+
type: parse.type('case-eq [%a : %b] = %a -> %b'),
|
6454
|
+
compile: function(input, _arg, tvars) {
|
3999
6455
|
var alts, eqType;
|
4000
6456
|
alts = _arg[0];
|
4001
6457
|
eqType = tvars.get('a');
|
4002
|
-
return alts.foldList(
|
6458
|
+
return alts.foldList(Core.fail('non-exhaustive cases'), function(el, next) {
|
4003
6459
|
var first, second;
|
4004
6460
|
first = el.depair('first');
|
4005
6461
|
second = el.depair('second');
|
4006
|
-
return
|
6462
|
+
return equals(input, first, eqType).branch(second, next);
|
4007
6463
|
});
|
4008
6464
|
}
|
4009
6465
|
},
|
@@ -4017,23 +6473,23 @@ stdlib = Gibbon.stdlib = (function() {
|
|
4017
6473
|
var first, second;
|
4018
6474
|
first = el.depair('first');
|
4019
6475
|
second = el.depair('second');
|
4020
|
-
return
|
6476
|
+
return equals(input, first, eqType).branch(second, next);
|
4021
6477
|
});
|
4022
6478
|
}
|
4023
6479
|
},
|
4024
6480
|
"any-true?": {
|
4025
6481
|
type: parse.type('any-true? = [bool] -> bool'),
|
4026
6482
|
compile: function(list) {
|
4027
|
-
return list.foldList(
|
4028
|
-
return el.branch(
|
6483
|
+
return list.foldList(FALSE, function(el, next) {
|
6484
|
+
return el.branch(TRUE, next);
|
4029
6485
|
});
|
4030
6486
|
}
|
4031
6487
|
},
|
4032
6488
|
"all-true?": {
|
4033
6489
|
type: parse.type('all-true? = [bool] -> bool'),
|
4034
6490
|
compile: function(list) {
|
4035
|
-
return list.foldList(
|
4036
|
-
return el.branch(next,
|
6491
|
+
return list.foldList(TRUE, function(el, next) {
|
6492
|
+
return el.branch(next, FALSE);
|
4037
6493
|
});
|
4038
6494
|
}
|
4039
6495
|
},
|
@@ -4042,8 +6498,8 @@ stdlib = Gibbon.stdlib = (function() {
|
|
4042
6498
|
compile: function(list, _arg) {
|
4043
6499
|
var block;
|
4044
6500
|
block = _arg[0];
|
4045
|
-
return list.foldList(
|
4046
|
-
return block.app(el).branch(
|
6501
|
+
return list.foldList(FALSE, function(el, next) {
|
6502
|
+
return block.app(el).branch(TRUE, next);
|
4047
6503
|
});
|
4048
6504
|
}
|
4049
6505
|
},
|
@@ -4052,8 +6508,8 @@ stdlib = Gibbon.stdlib = (function() {
|
|
4052
6508
|
compile: function(list, _arg) {
|
4053
6509
|
var block;
|
4054
6510
|
block = _arg[0];
|
4055
|
-
return list.foldList(
|
4056
|
-
return block.app(el).branch(next,
|
6511
|
+
return list.foldList(TRUE, function(el, next) {
|
6512
|
+
return block.app(el).branch(next, FALSE);
|
4057
6513
|
});
|
4058
6514
|
}
|
4059
6515
|
},
|
@@ -4063,17 +6519,15 @@ stdlib = Gibbon.stdlib = (function() {
|
|
4063
6519
|
var elType, needle;
|
4064
6520
|
needle = _arg[0];
|
4065
6521
|
elType = tvars.get('a');
|
4066
|
-
return list.foldList(
|
4067
|
-
return
|
6522
|
+
return list.foldList(FALSE, function(el, next) {
|
6523
|
+
return next.branch(TRUE, equals(el, needle, elType));
|
4068
6524
|
});
|
4069
6525
|
}
|
4070
6526
|
},
|
4071
6527
|
"empty?": {
|
4072
6528
|
type: parse.type('empty? = [%] -> bool'),
|
4073
6529
|
compile: function(list) {
|
4074
|
-
return list.
|
4075
|
-
return iFalse;
|
4076
|
-
});
|
6530
|
+
return list.len().op2('===', Core.constant(0));
|
4077
6531
|
}
|
4078
6532
|
},
|
4079
6533
|
weight: {
|
@@ -4081,16 +6535,16 @@ stdlib = Gibbon.stdlib = (function() {
|
|
4081
6535
|
compile: function(_, _arg) {
|
4082
6536
|
var ratio, weights;
|
4083
6537
|
weights = _arg[0];
|
4084
|
-
ratio = weights.foldList(
|
6538
|
+
ratio = weights.foldList(Core.pair(Core.constant(0), Core.constant(0)), function(el, next) {
|
4085
6539
|
var denominator, numerator, value, weight, weighted;
|
4086
6540
|
value = el.depair('first');
|
4087
6541
|
weight = el.depair('second');
|
4088
6542
|
numerator = next.depair('first');
|
4089
6543
|
denominator = next.depair('second');
|
4090
|
-
weighted = value.
|
4091
|
-
return
|
6544
|
+
weighted = value.op2('*', weight);
|
6545
|
+
return Core.pair(numerator.op2('+', weighted), denominator.op2('+', weight));
|
4092
6546
|
});
|
4093
|
-
return ratio.depair('first').
|
6547
|
+
return ratio.depair('first').op2('/', ratio.depair('second'));
|
4094
6548
|
}
|
4095
6549
|
},
|
4096
6550
|
filter: {
|
@@ -4112,13 +6566,13 @@ stdlib = Gibbon.stdlib = (function() {
|
|
4112
6566
|
domHigh = dom.depair('second');
|
4113
6567
|
rangeLow = range.depair('first');
|
4114
6568
|
rangeHigh = range.depair('second');
|
4115
|
-
input = input.
|
4116
|
-
input = input.
|
4117
|
-
domSize = domHigh.
|
4118
|
-
rangeSize = rangeHigh.
|
4119
|
-
translated = input.
|
4120
|
-
scaled = translated.
|
4121
|
-
retranslated = scaled.
|
6569
|
+
input = input.op2('<', domLow).branch(domLow, input);
|
6570
|
+
input = input.op2('>', domHigh).branch(domHigh, input);
|
6571
|
+
domSize = domHigh.op2('+', domLow.op1('-'));
|
6572
|
+
rangeSize = rangeHigh.op2('+', rangeLow.op1('-'));
|
6573
|
+
translated = input.op2('+', domLow.op1('-'));
|
6574
|
+
scaled = translated.op2('*', rangeSize.op2('/', domSize));
|
6575
|
+
retranslated = scaled.op2('+', rangeLow);
|
4122
6576
|
return retranslated;
|
4123
6577
|
}
|
4124
6578
|
},
|
@@ -4141,8 +6595,8 @@ stdlib = Gibbon.stdlib = (function() {
|
|
4141
6595
|
sum: {
|
4142
6596
|
type: parse.type('sum = [numeric] -> numeric'),
|
4143
6597
|
compile: function(list) {
|
4144
|
-
return list.foldList(
|
4145
|
-
return el.
|
6598
|
+
return list.foldList(Core.constant(0), function(el, next) {
|
6599
|
+
return el.op2('+', next);
|
4146
6600
|
});
|
4147
6601
|
}
|
4148
6602
|
},
|
@@ -4151,24 +6605,18 @@ stdlib = Gibbon.stdlib = (function() {
|
|
4151
6605
|
compile: function(_, _arg) {
|
4152
6606
|
var list;
|
4153
6607
|
list = _arg[0];
|
4154
|
-
return list.foldList(
|
6608
|
+
return list.foldList(Core.constant(0), function(el, next) {
|
4155
6609
|
var cond, val;
|
4156
6610
|
cond = el.depair('first');
|
4157
6611
|
val = el.depair('second');
|
4158
|
-
return cond.branch(val.
|
6612
|
+
return cond.branch(val.op2('+', next), next);
|
4159
6613
|
});
|
4160
6614
|
}
|
4161
6615
|
},
|
4162
6616
|
first: {
|
4163
6617
|
type: parse.type('first = [%a] -> %a'),
|
4164
6618
|
compile: function(list) {
|
4165
|
-
return list.delist(
|
4166
|
-
}
|
4167
|
-
},
|
4168
|
-
squish: {
|
4169
|
-
type: parse.type('squish = [%a] -> [%a]'),
|
4170
|
-
compile: function(list) {
|
4171
|
-
return list.squishList();
|
6619
|
+
return list.delist(Core.constant(0));
|
4172
6620
|
}
|
4173
6621
|
},
|
4174
6622
|
add: {
|
@@ -4176,7 +6624,7 @@ stdlib = Gibbon.stdlib = (function() {
|
|
4176
6624
|
compile: function(input, _arg) {
|
4177
6625
|
var num;
|
4178
6626
|
num = _arg[0];
|
4179
|
-
return input.
|
6627
|
+
return input.op2('+', num);
|
4180
6628
|
}
|
4181
6629
|
},
|
4182
6630
|
sub: {
|
@@ -4184,7 +6632,7 @@ stdlib = Gibbon.stdlib = (function() {
|
|
4184
6632
|
compile: function(input, _arg) {
|
4185
6633
|
var num;
|
4186
6634
|
num = _arg[0];
|
4187
|
-
return input.
|
6635
|
+
return input.op2('+', num.op1('-'));
|
4188
6636
|
}
|
4189
6637
|
},
|
4190
6638
|
id: {
|
@@ -4196,13 +6644,13 @@ stdlib = Gibbon.stdlib = (function() {
|
|
4196
6644
|
"else": {
|
4197
6645
|
type: parse.type('else = % -> bool'),
|
4198
6646
|
compile: function(_) {
|
4199
|
-
return
|
6647
|
+
return TRUE;
|
4200
6648
|
}
|
4201
6649
|
},
|
4202
6650
|
not: {
|
4203
6651
|
type: parse.type('not = bool -> bool'),
|
4204
6652
|
compile: function(input) {
|
4205
|
-
return input.
|
6653
|
+
return input.op1('!');
|
4206
6654
|
}
|
4207
6655
|
},
|
4208
6656
|
gt: {
|
@@ -4210,7 +6658,7 @@ stdlib = Gibbon.stdlib = (function() {
|
|
4210
6658
|
compile: function(input, _arg) {
|
4211
6659
|
var num;
|
4212
6660
|
num = _arg[0];
|
4213
|
-
return input.
|
6661
|
+
return input.op2('>', num);
|
4214
6662
|
}
|
4215
6663
|
},
|
4216
6664
|
lt: {
|
@@ -4218,7 +6666,7 @@ stdlib = Gibbon.stdlib = (function() {
|
|
4218
6666
|
compile: function(input, _arg) {
|
4219
6667
|
var num;
|
4220
6668
|
num = _arg[0];
|
4221
|
-
return input.
|
6669
|
+
return input.op2('<', num);
|
4222
6670
|
}
|
4223
6671
|
},
|
4224
6672
|
eq: {
|
@@ -4226,12 +6674,156 @@ stdlib = Gibbon.stdlib = (function() {
|
|
4226
6674
|
compile: function(input, _arg, tvars) {
|
4227
6675
|
var obj;
|
4228
6676
|
obj = _arg[0];
|
4229
|
-
return
|
6677
|
+
return equals(input, obj, tvars.get('a'));
|
6678
|
+
}
|
6679
|
+
},
|
6680
|
+
neq: {
|
6681
|
+
type: parse.type('neq %a = %a -> bool'),
|
6682
|
+
compile: function(input, _arg, tvars) {
|
6683
|
+
var obj;
|
6684
|
+
obj = _arg[0];
|
6685
|
+
return equals(input, obj, tvars.get('a')).op1('!');
|
4230
6686
|
}
|
4231
6687
|
}
|
4232
6688
|
};
|
4233
6689
|
})();
|
4234
|
-
|
6690
|
+
|
6691
|
+
Gibbon.CompiledCode = CompiledCode = (function() {
|
6692
|
+
var idFor, trampoline;
|
6693
|
+
|
6694
|
+
idFor = function(key) {
|
6695
|
+
return 'local' + key.replace(/\//g, '$$$$').replace(/-/g, '$$_');
|
6696
|
+
};
|
6697
|
+
|
6698
|
+
trampoline = function(x) {
|
6699
|
+
while (typeof x === 'function') {
|
6700
|
+
x = x();
|
6701
|
+
}
|
6702
|
+
return x;
|
6703
|
+
};
|
6704
|
+
|
6705
|
+
function CompiledCode(semantics, blocks) {
|
6706
|
+
var _this = this;
|
6707
|
+
this.semantics = semantics;
|
6708
|
+
this.blocks = blocks;
|
6709
|
+
this.functions = new Hash;
|
6710
|
+
this.blocks.each(function(k, v) {
|
6711
|
+
var func;
|
6712
|
+
func = new Function('$', 'QUERY', 'FAIL', 'RETURN', v);
|
6713
|
+
return _this.functions.set(k, func);
|
6714
|
+
});
|
6715
|
+
}
|
6716
|
+
|
6717
|
+
CompiledCode.prototype.functionFor = function(key) {
|
6718
|
+
return this.functions.get(key);
|
6719
|
+
};
|
6720
|
+
|
6721
|
+
CompiledCode.prototype.run = function(rootKey, input, client, cb) {
|
6722
|
+
var FAIL, RETURN, dependencies, failures, mkQuery, results, runKey, thunk,
|
6723
|
+
_this = this;
|
6724
|
+
results = new Hash;
|
6725
|
+
dependencies = new Hash;
|
6726
|
+
failures = new Hash;
|
6727
|
+
runKey = function(key, fail, succeed) {
|
6728
|
+
var deps, onFailure, onSuccess, query;
|
6729
|
+
DEBUG.log('evaluating key', key);
|
6730
|
+
if (results.has(key)) {
|
6731
|
+
return succeed(results.get(key));
|
6732
|
+
}
|
6733
|
+
if (failures.has(key)) {
|
6734
|
+
return fail(failures.get(key));
|
6735
|
+
}
|
6736
|
+
deps = dependencies.set(key, []);
|
6737
|
+
query = mkQuery(function(d) {
|
6738
|
+
return deps.push(d);
|
6739
|
+
});
|
6740
|
+
onSuccess = function(data) {
|
6741
|
+
results.set(key, data);
|
6742
|
+
return succeed(data);
|
6743
|
+
};
|
6744
|
+
onFailure = function(err) {
|
6745
|
+
failures.set(key, err);
|
6746
|
+
return fail(err);
|
6747
|
+
};
|
6748
|
+
return _this.functionFor(key)(input, query, onFailure, onSuccess);
|
6749
|
+
};
|
6750
|
+
mkQuery = function(pushDep) {
|
6751
|
+
return function(id, annotations, onFailure, onSuccess) {
|
6752
|
+
var isSynchronous, out;
|
6753
|
+
if (id == null) {
|
6754
|
+
pushDep(Dependency.lexical(annotations));
|
6755
|
+
return runKey(annotations, onFailure, onSuccess);
|
6756
|
+
}
|
6757
|
+
isSynchronous = true;
|
6758
|
+
out = client.performQuery(id, annotations, function(err, data) {
|
6759
|
+
if (err) {
|
6760
|
+
pushDep(Dependency.failure(err));
|
6761
|
+
if (isSynchronous) {
|
6762
|
+
return function() {
|
6763
|
+
return onFailure(err);
|
6764
|
+
};
|
6765
|
+
} else {
|
6766
|
+
return trampoline(onFailure(err));
|
6767
|
+
}
|
6768
|
+
} else {
|
6769
|
+
pushDep(Dependency.query(id, annotations));
|
6770
|
+
if (isSynchronous) {
|
6771
|
+
return function() {
|
6772
|
+
return onSuccess(data);
|
6773
|
+
};
|
6774
|
+
} else {
|
6775
|
+
return trampoline(onSuccess(data));
|
6776
|
+
}
|
6777
|
+
}
|
6778
|
+
});
|
6779
|
+
isSynchronous = false;
|
6780
|
+
return out;
|
6781
|
+
};
|
6782
|
+
};
|
6783
|
+
FAIL = function(f) {
|
6784
|
+
return cb(f);
|
6785
|
+
};
|
6786
|
+
RETURN = function(d) {
|
6787
|
+
return cb(null, d);
|
6788
|
+
};
|
6789
|
+
thunk = runKey(rootKey, cb, function() {
|
6790
|
+
var out;
|
6791
|
+
out = new Hash;
|
6792
|
+
results.each(function(k, v) {
|
6793
|
+
var deps, value;
|
6794
|
+
value = Value.interpret(v, _this.outputType(k));
|
6795
|
+
deps = dependencies.get(k);
|
6796
|
+
deps = uniq(deps, function(x, y) {
|
6797
|
+
return x.equals(y);
|
6798
|
+
});
|
6799
|
+
return out.set(k, [value, deps]);
|
6800
|
+
});
|
6801
|
+
return cb(null, out);
|
6802
|
+
});
|
6803
|
+
return trampoline(thunk);
|
6804
|
+
};
|
6805
|
+
|
6806
|
+
CompiledCode.prototype.outputType = function(key) {
|
6807
|
+
if (key == null) {
|
6808
|
+
key = '/';
|
6809
|
+
}
|
6810
|
+
return this.semantics.get(key).flow.type;
|
6811
|
+
};
|
6812
|
+
|
6813
|
+
return CompiledCode;
|
6814
|
+
|
6815
|
+
})();
|
6816
|
+
|
6817
|
+
Gibbon.compile = function(semantics) {
|
6818
|
+
var codegen, compiled, optimize, reduce, sequence, translate;
|
6819
|
+
codegen = Gibbon.codegen, reduce = Gibbon.reduce, sequence = Gibbon.sequence, optimize = Gibbon.optimize, translate = Gibbon.translate;
|
6820
|
+
compiled = new Hash;
|
6821
|
+
semantics.each(function(k, v) {
|
6822
|
+
return compiled.set(k, codegen(reduce(sequence(optimize(translate(v))))).block.toJS());
|
6823
|
+
});
|
6824
|
+
return new CompiledCode(semantics, compiled);
|
6825
|
+
};
|
6826
|
+
|
4235
6827
|
Gibbon.jsonConsumer = (function() {
|
4236
6828
|
return function(tables) {
|
4237
6829
|
var analyzeList, getType, getValue, listLookup, lists;
|
@@ -4252,7 +6844,7 @@ Gibbon.jsonConsumer = (function() {
|
|
4252
6844
|
}
|
4253
6845
|
});
|
4254
6846
|
};
|
4255
|
-
getValue = function(id, annotations,
|
6847
|
+
getValue = function(id, annotations, callback) {
|
4256
6848
|
var entity, values;
|
4257
6849
|
if (!tables.hasOwnProperty(annotations.table)) {
|
4258
6850
|
throw new Error("no such type " + annotations.table);
|
@@ -4265,7 +6857,7 @@ Gibbon.jsonConsumer = (function() {
|
|
4265
6857
|
if (!(entity.hasOwnProperty(annotations.name) && (entity[annotations.name] != null))) {
|
4266
6858
|
return callback(Failure.query(id, annotations));
|
4267
6859
|
}
|
4268
|
-
return callback(null,
|
6860
|
+
return callback(null, entity[annotations.name]);
|
4269
6861
|
};
|
4270
6862
|
lists = tables._lists || {};
|
4271
6863
|
analyzeList = function(id, listName, t, callback) {
|
@@ -4284,13 +6876,13 @@ Gibbon.jsonConsumer = (function() {
|
|
4284
6876
|
}
|
4285
6877
|
});
|
4286
6878
|
};
|
4287
|
-
listLookup = function(id, listName,
|
6879
|
+
listLookup = function(id, listName, callback) {
|
4288
6880
|
var list;
|
4289
6881
|
list = lists[listName].values;
|
4290
6882
|
if (list.indexOf(id) >= 0) {
|
4291
|
-
return callback(null,
|
6883
|
+
return callback(null, true);
|
4292
6884
|
} else {
|
4293
|
-
return callback(null,
|
6885
|
+
return callback(null, false);
|
4294
6886
|
}
|
4295
6887
|
};
|
4296
6888
|
return {
|
@@ -4304,11 +6896,11 @@ Gibbon.jsonConsumer = (function() {
|
|
4304
6896
|
return callback(new Error("unknown query `" + query.type + "'"));
|
4305
6897
|
}
|
4306
6898
|
},
|
4307
|
-
performQuery: function(id, annotations,
|
6899
|
+
performQuery: function(id, annotations, callback) {
|
4308
6900
|
if ('list' in annotations) {
|
4309
|
-
return listLookup(id, annotations.list,
|
6901
|
+
return listLookup(id, annotations.list, callback);
|
4310
6902
|
} else {
|
4311
|
-
return getValue(id, annotations,
|
6903
|
+
return getValue(id, annotations, callback);
|
4312
6904
|
}
|
4313
6905
|
}
|
4314
6906
|
};
|