goodguide-gibbon 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,2860 @@
1
+ var Gibbon = (function(undefined) {
2
+ var Parsimmon = (function(undefined) {
3
+ var P = (function(prototype, ownProperty, undefined) {
4
+ // helper functions that also help minification
5
+ function isObject(o) { return typeof o === 'object'; }
6
+ function isFunction(f) { return typeof f === 'function'; }
7
+
8
+ // used to extend the prototypes of superclasses (which might not
9
+ // have `.Bare`s)
10
+ function SuperclassBare() {}
11
+
12
+ function P(_superclass /* = Object */, definition) {
13
+ // handle the case where no superclass is given
14
+ if (definition === undefined) {
15
+ definition = _superclass;
16
+ _superclass = Object;
17
+ }
18
+
19
+ // C is the class to be returned.
20
+ //
21
+ // It delegates to instantiating an instance of `Bare`, so that it
22
+ // will always return a new instance regardless of the calling
23
+ // context.
24
+ //
25
+ // TODO: the Chrome inspector shows all created objects as `C`
26
+ // rather than `Object`. Setting the .name property seems to
27
+ // have no effect. Is there a way to override this behavior?
28
+ function C() {
29
+ var self = new Bare;
30
+ if (isFunction(self.init)) self.init.apply(self, arguments);
31
+ return self;
32
+ }
33
+
34
+ // C.Bare is a class with a noop constructor. Its prototype is the
35
+ // same as C, so that instances of C.Bare are also instances of C.
36
+ // New objects can be allocated without initialization by calling
37
+ // `new MyClass.Bare`.
38
+ function Bare() {}
39
+ C.Bare = Bare;
40
+
41
+ // Set up the prototype of the new class.
42
+ var _super = SuperclassBare[prototype] = _superclass[prototype];
43
+ var proto = Bare[prototype] = C[prototype] = new SuperclassBare;
44
+
45
+ // other variables, as a minifier optimization
46
+ var extensions;
47
+
48
+
49
+ // set the constructor property on the prototype, for convenience
50
+ proto.constructor = C;
51
+
52
+ C.mixin = function(def) {
53
+ Bare[prototype] = C[prototype] = P(C, def)[prototype];
54
+ return C;
55
+ }
56
+
57
+ return (C.open = function(def) {
58
+ extensions = {};
59
+
60
+ if (isFunction(def)) {
61
+ // call the defining function with all the arguments you need
62
+ // extensions captures the return value.
63
+ extensions = def.call(C, proto, _super, C, _superclass);
64
+ }
65
+ else if (isObject(def)) {
66
+ // if you passed an object instead, we'll take it
67
+ extensions = def;
68
+ }
69
+
70
+ // ...and extend it
71
+ if (isObject(extensions)) {
72
+ for (var ext in extensions) {
73
+ if (ownProperty.call(extensions, ext)) {
74
+ proto[ext] = extensions[ext];
75
+ }
76
+ }
77
+ }
78
+
79
+ // if there's no init, we assume we're inheriting a non-pjs class, so
80
+ // we default to applying the superclass's constructor.
81
+ if (!isFunction(proto.init)) {
82
+ proto.init = _superclass;
83
+ }
84
+
85
+ return C;
86
+ })(definition);
87
+ }
88
+
89
+ // ship it
90
+ return P;
91
+
92
+ // as a minifier optimization, we've closured in a few helper functions
93
+ // and the string 'prototype' (C[p] is much shorter than C.prototype)
94
+ })('prototype', ({}).hasOwnProperty);
95
+ var Parsimmon = {};
96
+
97
+ Parsimmon.Parser = P(function(_, _super, Parser) {
98
+ "use strict";
99
+ // The Parser object is a wrapper for a parser function.
100
+ // Externally, you use one to parse a string by calling
101
+ // var result = SomeParser.parse('Me Me Me! Parse Me!');
102
+ // You should never call the constructor, rather you should
103
+ // construct your Parser from the base parsers and the
104
+ // parser combinator methods.
105
+
106
+ function parseError(stream, i, expected) {
107
+ if (i === stream.length) {
108
+ var message = 'expected ' + expected + ', got the end of the string';
109
+ }
110
+ else {
111
+ var prefix = (i > 0 ? "'..." : "'");
112
+ var suffix = (stream.length - i > 12 ? "...'" : "'");
113
+ var message = 'expected ' + expected + ' at character ' + i + ', got '
114
+ + prefix + stream.slice(i, i+12) + suffix;
115
+ }
116
+ throw 'Parse Error: ' + message + "\n parsing: '" + stream + "'";
117
+ }
118
+
119
+ _.init = function(body) { this._ = body; };
120
+
121
+ _.parse = function(stream) {
122
+ return this.skip(eof)._(stream, 0, success, parseError);
123
+
124
+ function success(stream, i, result) { return result; }
125
+ };
126
+
127
+ // -*- primitive combinators -*- //
128
+ _.or = function(alternative) {
129
+ var self = this;
130
+
131
+ return Parser(function(stream, i, onSuccess, onFailure) {
132
+ return self._(stream, i, onSuccess, failure);
133
+
134
+ function failure(stream, newI) {
135
+ return alternative._(stream, i, onSuccess, onFailure);
136
+ }
137
+ });
138
+ };
139
+
140
+ _.then = function(next) {
141
+ var self = this;
142
+
143
+ return Parser(function(stream, i, onSuccess, onFailure) {
144
+ return self._(stream, i, success, onFailure);
145
+
146
+ function success(stream, newI, result) {
147
+ var nextParser = (next instanceof Parser ? next : next(result));
148
+ return nextParser._(stream, newI, onSuccess, onFailure);
149
+ }
150
+ });
151
+ };
152
+
153
+ // -*- optimized iterative combinators -*- //
154
+ // equivalent to:
155
+ // _.many = function() {
156
+ // return this.times(0, Infinity);
157
+ // };
158
+ // or, more explicitly:
159
+ // _.many = function() {
160
+ // var self = this;
161
+ // return self.then(function(x) {
162
+ // return self.many().then(function(xs) {
163
+ // return [x].concat(xs);
164
+ // });
165
+ // }).or(succeed([]));
166
+ // };
167
+ _.many = function() {
168
+ var self = this;
169
+
170
+ return Parser(function(stream, i, onSuccess, onFailure) {
171
+ var xs = [];
172
+ while (self._(stream, i, success, failure));
173
+ return onSuccess(stream, i, xs);
174
+
175
+ function success(stream, newI, x) {
176
+ i = newI;
177
+ xs.push(x);
178
+ return true;
179
+ }
180
+
181
+ function failure() {
182
+ return false;
183
+ }
184
+ });
185
+ };
186
+
187
+ // equivalent to:
188
+ // _.times = function(min, max) {
189
+ // if (arguments.length < 2) max = min;
190
+ // var self = this;
191
+ // if (min > 0) {
192
+ // return self.then(function(x) {
193
+ // return self.times(min - 1, max - 1).then(function(xs) {
194
+ // return [x].concat(xs);
195
+ // });
196
+ // });
197
+ // }
198
+ // else if (max > 0) {
199
+ // return self.then(function(x) {
200
+ // return self.times(0, max - 1).then(function(xs) {
201
+ // return [x].concat(xs);
202
+ // });
203
+ // }).or(succeed([]));
204
+ // }
205
+ // else return succeed([]);
206
+ // };
207
+ _.times = function(min, max) {
208
+ if (arguments.length < 2) max = min;
209
+ var self = this;
210
+
211
+ return Parser(function(stream, i, onSuccess, onFailure) {
212
+ var xs = [];
213
+ var result = true;
214
+ var failure;
215
+
216
+ for (var times = 0; times < min; times += 1) {
217
+ result = self._(stream, i, success, firstFailure);
218
+ if (!result) return onFailure(stream, i, failure);
219
+ }
220
+
221
+ for (; times < max && result; times += 1) {
222
+ result = self._(stream, i, success, secondFailure);
223
+ }
224
+
225
+ return onSuccess(stream, i, xs);
226
+
227
+ function success(stream, newI, x) {
228
+ xs.push(x);
229
+ i = newI;
230
+ return true;
231
+ }
232
+
233
+ function firstFailure(stream, newI, msg) {
234
+ failure = msg;
235
+ i = newI;
236
+ return false;
237
+ }
238
+
239
+ function secondFailure(stream, newI, msg) {
240
+ return false;
241
+ }
242
+ });
243
+ };
244
+
245
+ // -*- higher-level combinators -*- //
246
+ _.result = function(res) { return this.then(succeed(res)); };
247
+ _.atMost = function(n) { return this.times(0, n); };
248
+ _.atLeast = function(n) {
249
+ var self = this;
250
+ return self.times(n).then(function(start) {
251
+ return self.many().map(function(end) {
252
+ return start.concat(end);
253
+ });
254
+ });
255
+ };
256
+
257
+ _.map = function(fn) {
258
+ return this.then(function(result) { return succeed(fn(result)); });
259
+ };
260
+
261
+ _.skip = function(two) {
262
+ return this.then(function(result) { return two.result(result); });
263
+ };
264
+
265
+ // -*- primitive parsers -*- //
266
+ var string = Parsimmon.string = function(str) {
267
+ var len = str.length;
268
+ var expected = "'"+str+"'";
269
+
270
+ return Parser(function(stream, i, onSuccess, onFailure) {
271
+ var head = stream.slice(i, i+len);
272
+
273
+ if (head === str) {
274
+ return onSuccess(stream, i+len, head);
275
+ }
276
+ else {
277
+ return onFailure(stream, i, expected);
278
+ }
279
+ });
280
+ };
281
+
282
+ var regex = Parsimmon.regex = function(re) {
283
+ if (re.source[0] !== '^') throw 'regex '+re+' must be anchored';
284
+
285
+ var expected = ''+re;
286
+
287
+ return Parser(function(stream, i, onSuccess, onFailure) {
288
+ var match = re.exec(stream.slice(i));
289
+
290
+ if (match) {
291
+ var result = match[0];
292
+ return onSuccess(stream, i+result.length, result);
293
+ }
294
+ else {
295
+ return onFailure(stream, i, expected);
296
+ }
297
+ });
298
+ };
299
+
300
+ var succeed = Parsimmon.succeed = function(result) {
301
+ return Parser(function(stream, i, onSuccess) {
302
+ return onSuccess(stream, i, result);
303
+ });
304
+ };
305
+
306
+ var fail = Parsimmon.fail = function(expected) {
307
+ return Parser(function(stream, i, _, onFailure) {
308
+ return onFailure(stream, i, expected);
309
+ });
310
+ };
311
+
312
+ var letter = Parsimmon.letter = regex(/^[a-z]/i);
313
+ var letters = Parsimmon.letters = regex(/^[a-z]*/i);
314
+ var digit = Parsimmon.digit = regex(/^[0-9]/);
315
+ var digits = Parsimmon.digits = regex(/^[0-9]*/);
316
+ var whitespace = Parsimmon.whitespace = regex(/^\s+/);
317
+ var optWhitespace = Parsimmon.optWhitespace = regex(/^\s*/);
318
+
319
+ var any = Parsimmon.any = Parser(function(stream, i, onSuccess, onFailure) {
320
+ if (i >= stream.length) return onFailure(stream, i, 'any character');
321
+
322
+ return onSuccess(stream, i+1, stream.charAt(i));
323
+ });
324
+
325
+ var all = Parsimmon.all = Parser(function(stream, i, onSuccess, onFailure) {
326
+ return onSuccess(stream, stream.length, stream.slice(i));
327
+ });
328
+
329
+ var eof = Parsimmon.eof = Parser(function(stream, i, onSuccess, onFailure) {
330
+ if (i < stream.length) return onFailure(stream, i, 'EOF');
331
+
332
+ return onSuccess(stream, i, '');
333
+ });
334
+ });
335
+ return Parsimmon;
336
+ })()
337
+ // Generated by CoffeeScript 1.6.3
338
+ var Gibbon;
339
+
340
+ Gibbon = {};
341
+ // Generated by CoffeeScript 1.6.3
342
+ var CompMap, Hash, Map, Union, allocate, asyncMap, catLists, contIter, contMap, genId, isArray, uniq, _ref,
343
+ __slice = [].slice,
344
+ __hasProp = {}.hasOwnProperty,
345
+ __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; };
346
+
347
+ genId = (function() {
348
+ var id;
349
+ id = 0;
350
+ return function() {
351
+ return id += 1;
352
+ };
353
+ })();
354
+
355
+ isArray = Array.isArray || function(arg) {
356
+ return Object.prototype.toString.call(arg) === '[object Array]';
357
+ };
358
+
359
+ catLists = function(lists) {
360
+ var l, out, _i, _len;
361
+ out = [];
362
+ for (_i = 0, _len = lists.length; _i < _len; _i++) {
363
+ l = lists[_i];
364
+ out = out.concat(l);
365
+ }
366
+ return out;
367
+ };
368
+
369
+ uniq = function(list, eq) {
370
+ var el, out, u, _i, _j, _len, _len1;
371
+ if (eq == null) {
372
+ eq = (function(x, y) {
373
+ return x === y;
374
+ });
375
+ }
376
+ if (list.length === 0) {
377
+ return list;
378
+ }
379
+ out = [];
380
+ for (_i = 0, _len = list.length; _i < _len; _i++) {
381
+ el = list[_i];
382
+ for (_j = 0, _len1 = out.length; _j < _len1; _j++) {
383
+ u = out[_j];
384
+ if (!eq(el, u)) {
385
+ out.push(el);
386
+ break;
387
+ }
388
+ }
389
+ if (out.length === 0) {
390
+ out.push(el);
391
+ }
392
+ }
393
+ return out;
394
+ };
395
+
396
+ allocate = Object.create || function(proto) {
397
+ var dummy;
398
+ dummy = function() {};
399
+ dummy.prototype = proto;
400
+ return new dummy;
401
+ };
402
+
403
+ asyncMap = function(list, mapper, cb) {
404
+ var el, i, output, response, seen, _i, _len, _results;
405
+ if (list.length === 0) {
406
+ return cb(list);
407
+ }
408
+ seen = 0;
409
+ output = [];
410
+ response = function(i) {
411
+ return function(el) {
412
+ seen += 1;
413
+ output[i] = el;
414
+ if (seen >= list.length) {
415
+ return cb(output);
416
+ }
417
+ };
418
+ };
419
+ _results = [];
420
+ for (i = _i = 0, _len = list.length; _i < _len; i = ++_i) {
421
+ el = list[i];
422
+ _results.push(mapper(el, response(i), i));
423
+ }
424
+ return _results;
425
+ };
426
+
427
+ contMap = function(list, mapper, cb) {
428
+ var accum, step;
429
+ accum = [];
430
+ step = function(el, i, next) {
431
+ return mapper(el, function(result) {
432
+ accum[i] = result;
433
+ return next();
434
+ });
435
+ };
436
+ return contIter(list, step, function() {
437
+ return cb(accum);
438
+ });
439
+ };
440
+
441
+ contIter = function(list, f, cb) {
442
+ var loop_;
443
+ loop_ = function(i) {
444
+ if (i >= list.length) {
445
+ return cb();
446
+ }
447
+ return f(list[i], i, function() {
448
+ return loop_(i + 1);
449
+ });
450
+ };
451
+ return loop_(0);
452
+ };
453
+
454
+ Union = (function() {
455
+ var castJSON;
456
+
457
+ Union.specialize = function(tag, names) {
458
+ var klass, name, subclass, _i, _len;
459
+ klass = this;
460
+ subclass = function(values) {
461
+ if (values.length !== names.length) {
462
+ throw new TypeError("wrong number of arguments: " + values.length + " for " + names.length);
463
+ }
464
+ return klass.call(this, names, values);
465
+ };
466
+ subclass.prototype = allocate(klass.prototype);
467
+ subclass.prototype._tag = tag;
468
+ for (_i = 0, _len = names.length; _i < _len; _i++) {
469
+ name = names[_i];
470
+ subclass.prototype[name] = null;
471
+ }
472
+ return function() {
473
+ var values;
474
+ values = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
475
+ return new subclass(values);
476
+ };
477
+ };
478
+
479
+ Union.types = function(tags) {
480
+ var names, tag, _results;
481
+ this.tags = tags;
482
+ _results = [];
483
+ for (tag in tags) {
484
+ if (!__hasProp.call(tags, tag)) continue;
485
+ names = tags[tag];
486
+ _results.push(this[tag] = this.specialize(tag, names));
487
+ }
488
+ return _results;
489
+ };
490
+
491
+ function Union(_names, _values) {
492
+ var i, name, _i, _len, _ref;
493
+ this._names = _names;
494
+ this._values = _values;
495
+ _ref = this._names;
496
+ for (i = _i = 0, _len = _ref.length; _i < _len; i = ++_i) {
497
+ name = _ref[i];
498
+ this[name] = this._values[i];
499
+ }
500
+ }
501
+
502
+ Union.prototype.cases = function(cases) {
503
+ var fn;
504
+ fn = cases[this._tag] || cases.other;
505
+ if (!fn) {
506
+ throw new Error("non-exhaustive cases: missing " + this._tag);
507
+ }
508
+ return fn.apply(this, this._values);
509
+ };
510
+
511
+ castJSON = function(val) {
512
+ var v, _i, _len, _results;
513
+ if (typeof (val != null ? val.asJSON : void 0) === 'function') {
514
+ return val.asJSON();
515
+ } else if (isArray(val)) {
516
+ _results = [];
517
+ for (_i = 0, _len = val.length; _i < _len; _i++) {
518
+ v = val[_i];
519
+ _results.push(castJSON(v));
520
+ }
521
+ return _results;
522
+ } else {
523
+ return val;
524
+ }
525
+ };
526
+
527
+ Union.prototype.asJSON = function() {
528
+ var name, out, _i, _len, _ref;
529
+ out = {
530
+ _tag: this._tag,
531
+ _union: this.constructor.name
532
+ };
533
+ _ref = this._names;
534
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
535
+ name = _ref[_i];
536
+ out[name] = castJSON(this[name]);
537
+ }
538
+ return out;
539
+ };
540
+
541
+ Union.fromJSON = function(o) {
542
+ var constructor, e, name, names, vals;
543
+ if (isArray(o)) {
544
+ return (function() {
545
+ var _i, _len, _results;
546
+ _results = [];
547
+ for (_i = 0, _len = o.length; _i < _len; _i++) {
548
+ e = o[_i];
549
+ _results.push(this.fromJSON(e));
550
+ }
551
+ return _results;
552
+ }).call(this);
553
+ }
554
+ if (!(typeof o === 'object' && (o != null) && '_tag' in o)) {
555
+ return o;
556
+ }
557
+ constructor = Gibbon[o._union];
558
+ names = constructor.tags[o._tag];
559
+ vals = (function() {
560
+ var _i, _len, _results;
561
+ _results = [];
562
+ for (_i = 0, _len = names.length; _i < _len; _i++) {
563
+ name = names[_i];
564
+ _results.push(constructor.fromJSON(o[name]));
565
+ }
566
+ return _results;
567
+ })();
568
+ return constructor[o._tag].apply(constructor, vals);
569
+ };
570
+
571
+ Union.wrap = function(o) {
572
+ if (o instanceof this) {
573
+ return o;
574
+ }
575
+ return this.fromJSON(o);
576
+ };
577
+
578
+ return Union;
579
+
580
+ })();
581
+
582
+ Map = (function() {
583
+ function Map() {}
584
+
585
+ Map.prototype.has = function() {
586
+ throw 'abstract';
587
+ };
588
+
589
+ Map.prototype.get = function() {
590
+ throw 'abstract';
591
+ };
592
+
593
+ Map.prototype.set = function() {
594
+ throw 'abstract';
595
+ };
596
+
597
+ Map.prototype.each = function() {
598
+ throw 'abstract';
599
+ };
600
+
601
+ Map.prototype.fetch = function(k, f) {
602
+ if (this.has(k)) {
603
+ return this.get(k);
604
+ } else {
605
+ return f();
606
+ }
607
+ };
608
+
609
+ Map.prototype.merge = function() {
610
+ var k, obj, objs, v, _i, _len, _results;
611
+ objs = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
612
+ _results = [];
613
+ for (_i = 0, _len = objs.length; _i < _len; _i++) {
614
+ obj = objs[_i];
615
+ _results.push((function() {
616
+ var _results1;
617
+ _results1 = [];
618
+ for (k in obj) {
619
+ if (!__hasProp.call(obj, k)) continue;
620
+ v = obj[k];
621
+ _results1.push(this.set(k, v));
622
+ }
623
+ return _results1;
624
+ }).call(this));
625
+ }
626
+ return _results;
627
+ };
628
+
629
+ Map.prototype.modify = function(k, f) {
630
+ return this.set(k, f(this.get(k)));
631
+ };
632
+
633
+ Map.prototype.cache = function(k, f) {
634
+ var _this = this;
635
+ return this.fetch(k, function() {
636
+ return _this.modify(k, f);
637
+ });
638
+ };
639
+
640
+ Map.prototype.keys = function() {
641
+ var out;
642
+ out = [];
643
+ this.each(function(k, v) {
644
+ return out.push(k);
645
+ });
646
+ return out;
647
+ };
648
+
649
+ Map.prototype.values = function() {
650
+ var out;
651
+ out = [];
652
+ this.each(function(k, v) {
653
+ return out.push(v);
654
+ });
655
+ return out;
656
+ };
657
+
658
+ return Map;
659
+
660
+ })();
661
+
662
+ Gibbon.Hash = Hash = (function(_super) {
663
+ var salt;
664
+
665
+ __extends(Hash, _super);
666
+
667
+ function Hash() {
668
+ _ref = Hash.__super__.constructor.apply(this, arguments);
669
+ return _ref;
670
+ }
671
+
672
+ salt = '<key>';
673
+
674
+ Hash.prototype.loadKey = function(k) {
675
+ return salt + k;
676
+ };
677
+
678
+ Hash.prototype.dumpKey = function(k) {
679
+ if (k.indexOf(salt) === 0) {
680
+ return k.slice(salt.length);
681
+ }
682
+ };
683
+
684
+ Hash.prototype.get = function(k) {
685
+ return this[salt + k];
686
+ };
687
+
688
+ Hash.prototype.set = function(k, v) {
689
+ return this[salt + k] = v;
690
+ };
691
+
692
+ Hash.prototype.has = function(k) {
693
+ return this.hasOwnProperty(salt + k);
694
+ };
695
+
696
+ Hash.prototype.each = function(f) {
697
+ var k, v, _results;
698
+ _results = [];
699
+ for (k in this) {
700
+ if (!__hasProp.call(this, k)) continue;
701
+ v = this[k];
702
+ if (k.indexOf(salt) !== 0) {
703
+ continue;
704
+ }
705
+ _results.push(f(k.slice(salt.length), v));
706
+ }
707
+ return _results;
708
+ };
709
+
710
+ return Hash;
711
+
712
+ })(Map);
713
+
714
+ CompMap = (function(_super) {
715
+ __extends(CompMap, _super);
716
+
717
+ function CompMap(fn) {
718
+ if (fn == null) {
719
+ fn = (function(x, y) {
720
+ return x === y;
721
+ });
722
+ }
723
+ CompMap.__super__.constructor.apply(this, arguments);
724
+ this.compare = fn;
725
+ this.keys = [];
726
+ this.values = [];
727
+ }
728
+
729
+ CompMap.prototype.addBinding = function(k, v) {
730
+ this.keys.push(k);
731
+ this.values.push(v);
732
+ return v;
733
+ };
734
+
735
+ CompMap.prototype.keyIndex = function(k) {
736
+ var i, key, _i, _len, _ref1;
737
+ _ref1 = this.keys;
738
+ for (i = _i = 0, _len = _ref1.length; _i < _len; i = ++_i) {
739
+ key = _ref1[i];
740
+ if (this.compare(key, k)) {
741
+ return i;
742
+ }
743
+ }
744
+ return null;
745
+ };
746
+
747
+ CompMap.prototype.has = function(k) {
748
+ return this.keyIndex(k) != null;
749
+ };
750
+
751
+ CompMap.prototype.get = function(k) {
752
+ var index;
753
+ index = this.keyIndex(k);
754
+ return (index != null) && this.values[index];
755
+ };
756
+
757
+ CompMap.prototype.set = function(k, v) {
758
+ var index;
759
+ index = this.keyIndex(k);
760
+ if (index != null) {
761
+ return this.values[index] = v;
762
+ } else {
763
+ return this.addBinding(k, v);
764
+ }
765
+ };
766
+
767
+ CompMap.prototype.each = function(f) {
768
+ var i, k, _i, _len, _ref1, _results;
769
+ _ref1 = this.keys;
770
+ _results = [];
771
+ for (i = _i = 0, _len = _ref1.length; _i < _len; i = ++_i) {
772
+ k = _ref1[i];
773
+ _results.push(f(k, this.values[i]));
774
+ }
775
+ return _results;
776
+ };
777
+
778
+ CompMap.prototype.eachAsync = function(f, cb) {
779
+ var i, k, keys, responder, seen, _i, _len, _results;
780
+ keys = this.keys;
781
+ if (keys.length === 0) {
782
+ return;
783
+ }
784
+ seen = 0;
785
+ responder = function(i) {
786
+ return function() {
787
+ seen += 1;
788
+ if (seen >= keys.length) {
789
+ return cb();
790
+ }
791
+ };
792
+ };
793
+ _results = [];
794
+ for (i = _i = 0, _len = keys.length; _i < _len; i = ++_i) {
795
+ k = keys[i];
796
+ _results.push(f(k, this.values[i], responder(i)));
797
+ }
798
+ return _results;
799
+ };
800
+
801
+ CompMap.prototype.fetch = function(k, f) {
802
+ var index;
803
+ index = this.keyIndex(k);
804
+ if (index != null) {
805
+ return this.values[index];
806
+ } else {
807
+ return f();
808
+ }
809
+ };
810
+
811
+ CompMap.prototype.fetchAsync = function(k, f, cb) {
812
+ var index;
813
+ index = this.keyIndex(k);
814
+ if (index != null) {
815
+ return cb(this.values[index]);
816
+ } else {
817
+ return f(cb);
818
+ }
819
+ };
820
+
821
+ CompMap.prototype.cache = function(k, f) {
822
+ var _this = this;
823
+ return this.fetch(k, function() {
824
+ return _this.addBinding(k, f());
825
+ });
826
+ };
827
+
828
+ return CompMap;
829
+
830
+ })(Map);
831
+ // Generated by CoffeeScript 1.6.3
832
+ var AST, TypeAST, parse, _ref, _ref1,
833
+ __hasProp = {}.hasOwnProperty,
834
+ __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; };
835
+
836
+ Gibbon.AST = AST = (function(_super) {
837
+ var inspectDefinitions;
838
+
839
+ __extends(AST, _super);
840
+
841
+ function AST() {
842
+ _ref = AST.__super__.constructor.apply(this, arguments);
843
+ return _ref;
844
+ }
845
+
846
+ AST.types({
847
+ integer: ['value'],
848
+ decimal: ['value'],
849
+ percent: ['value'],
850
+ fraction: ['numerator', 'denominator'],
851
+ string: ['value'],
852
+ accessor: ['name'],
853
+ subst: ['flow'],
854
+ block: ['flow'],
855
+ list: ['elements'],
856
+ func: ['name', 'args'],
857
+ pair: ['first', 'second'],
858
+ flow: ['head', 'tail'],
859
+ metadata: ['key', 'text'],
860
+ definition: ['metadata', 'name', 'frame'],
861
+ frame: ['definitions', 'flow'],
862
+ program: ['definitions']
863
+ });
864
+
865
+ inspectDefinitions = function(defs) {
866
+ var def, out, _i, _len;
867
+ out = [];
868
+ for (_i = 0, _len = defs.length; _i < _len; _i++) {
869
+ def = defs[_i];
870
+ out.push(def.inspect());
871
+ out.push("\n");
872
+ }
873
+ return out.join('');
874
+ };
875
+
876
+ AST.prototype.inspect = function() {
877
+ var id;
878
+ id = function(x) {
879
+ return x;
880
+ };
881
+ return this.cases({
882
+ integer: id,
883
+ decimal: id,
884
+ percent: id,
885
+ fraction: function(num, denom) {
886
+ return "" + num + "/" + denom;
887
+ },
888
+ string: function(s) {
889
+ return "'" + s + "'";
890
+ },
891
+ accessor: function(name) {
892
+ return "@" + name;
893
+ },
894
+ subst: function(flow) {
895
+ return "(" + (flow.inspect()) + ")";
896
+ },
897
+ block: function(flow) {
898
+ return "{ " + (flow.inspect()) + " }";
899
+ },
900
+ list: function(els) {
901
+ var el;
902
+ return "[ " + (((function() {
903
+ var _i, _len, _results;
904
+ _results = [];
905
+ for (_i = 0, _len = els.length; _i < _len; _i++) {
906
+ el = els[_i];
907
+ _results.push(el.inspect());
908
+ }
909
+ return _results;
910
+ })()).join(', ')) + " ]";
911
+ },
912
+ func: function(name, args) {
913
+ var arg;
914
+ args = (function() {
915
+ var _i, _len, _results;
916
+ _results = [];
917
+ for (_i = 0, _len = args.length; _i < _len; _i++) {
918
+ arg = args[_i];
919
+ _results.push(arg.inspect());
920
+ }
921
+ return _results;
922
+ })();
923
+ return "" + name + " " + (args.join(' '));
924
+ },
925
+ pair: function(first, second) {
926
+ return "" + (first.inspect()) + " : " + (second.inspect());
927
+ },
928
+ flow: function(head, tail) {
929
+ if (tail) {
930
+ tail = "" + (tail.inspect()) + " -> ";
931
+ } else {
932
+ tail = '';
933
+ }
934
+ return "" + tail + (head.inspect());
935
+ },
936
+ metadata: function(key, text) {
937
+ return "" + key + ": " + text;
938
+ },
939
+ definition: function(metadata, name, frame) {
940
+ var m, out, _i, _len;
941
+ out = [];
942
+ for (_i = 0, _len = metadata.length; _i < _len; _i++) {
943
+ m = metadata[_i];
944
+ out.push(m.inspect());
945
+ out.push("\n");
946
+ }
947
+ out.push("" + name + " := ");
948
+ out.push(frame.inspect());
949
+ return out.join('');
950
+ },
951
+ frame: function(definitions, flow) {
952
+ var out;
953
+ out = [];
954
+ out.push("(");
955
+ out.push(inspectDefinitions(definitions));
956
+ out.push(flow.inspect());
957
+ out.push(")");
958
+ return out.join('');
959
+ },
960
+ program: function(definitions) {
961
+ return inspectDefinitions(definitions);
962
+ }
963
+ });
964
+ };
965
+
966
+ return AST;
967
+
968
+ })(Union);
969
+
970
+ Gibbon.TypeAST = TypeAST = (function(_super) {
971
+ __extends(TypeAST, _super);
972
+
973
+ function TypeAST() {
974
+ _ref1 = TypeAST.__super__.constructor.apply(this, arguments);
975
+ return _ref1;
976
+ }
977
+
978
+ TypeAST.types({
979
+ concrete: ['name'],
980
+ variable: ['name'],
981
+ "native": ['id'],
982
+ wildcard: [],
983
+ list: ['of'],
984
+ func: ['input', 'args', 'output'],
985
+ block: ['of'],
986
+ pair: ['first', 'second'],
987
+ arrow: ['from', 'to']
988
+ });
989
+
990
+ return TypeAST;
991
+
992
+ })(Union);
993
+
994
+ parse = Gibbon.parse = (function() {
995
+ var accessor, accessorExpr, arrow, arrowType, assertString, bang, blockExpr, blockType, comma, comment, component, concrete, decimal, decimalExpr, define, definition, expr, fail, flow, fraction, fractionExpr, frame, freeFrame, fullSignature, func, funcPlaceholder, identifier, ignore, integer, integerExpr, isString, label, labelVal, lbrace, lbrack, lexeme, listExpr, listType, lparen, metadata, name, nativeId, nativeType, nonPairedFlow, numericExpr, pair, parenFlow, parenFrame, parenType, percent, percentExpr, program, rbrace, rbrack, regex, rparen, signature, simpleType, singletonFlow, str, string, stringExpr, substExpr, succeed, tassign, type, typeVar, variable, whitespace, wildType, wildcard;
996
+ string = Parsimmon.string, regex = Parsimmon.regex, succeed = Parsimmon.succeed, fail = Parsimmon.fail;
997
+ Parsimmon.Parser.prototype.expected = function(message) {
998
+ var _this = this;
999
+ return Parsimmon.Parser(function(stream, i, onSuccess) {
1000
+ return _this._(stream, i, onSuccess, function(newStream, _, defMessage) {
1001
+ var at;
1002
+ at = stream.slice(0, 10) || 'EOF';
1003
+ throw new Error("Parse Error: expected " + (message || defMessage) + " (at '" + at + "')");
1004
+ });
1005
+ });
1006
+ };
1007
+ Parsimmon.Parser.prototype.named = function(name) {
1008
+ var _this = this;
1009
+ return Parsimmon.Parser(function(stream, i, onSuccess, onFailure) {
1010
+ return _this._(stream, i, onSuccess, function() {
1011
+ return onFailure(stream, i, name);
1012
+ });
1013
+ });
1014
+ };
1015
+ whitespace = regex(/^[\n\s]+/);
1016
+ comment = regex(/^#.*?(\n|$)/);
1017
+ ignore = (whitespace.or(comment)).many();
1018
+ lexeme = function(p) {
1019
+ return p.skip(ignore);
1020
+ };
1021
+ identifier = regex(/^[a-z][\w-]*/i);
1022
+ arrow = lexeme(string('->'));
1023
+ define = lexeme(string(':='));
1024
+ pair = lexeme(string(':'));
1025
+ lbrace = lexeme(string('{'));
1026
+ rbrace = lexeme(string('}'));
1027
+ lbrack = lexeme(string('['));
1028
+ rbrack = lexeme(string(']'));
1029
+ lparen = lexeme(string('('));
1030
+ rparen = lexeme(string(')'));
1031
+ comma = lexeme(string(','));
1032
+ name = lexeme(identifier);
1033
+ str = lexeme(string("'").then(regex(/^[^']*/)).skip(string("'").expected()));
1034
+ accessor = lexeme(string('@').then(identifier.expected()));
1035
+ fraction = lexeme(regex(/^\d+\/\d+/));
1036
+ decimal = lexeme(regex(/^\d+\.\d+/));
1037
+ percent = lexeme(regex(/^\d+%/));
1038
+ integer = lexeme(regex(/^\d+/));
1039
+ label = lexeme(identifier.skip(string(':')));
1040
+ labelVal = lexeme(regex(/^[^\n]*/));
1041
+ variable = lexeme(string('%').then(identifier));
1042
+ wildcard = lexeme(string('%'));
1043
+ funcPlaceholder = lexeme(string('&'));
1044
+ integerExpr = integer.map(function(i) {
1045
+ return AST.integer(parseInt(i));
1046
+ });
1047
+ decimalExpr = decimal.map(function(d) {
1048
+ return AST.decimal(parseFloat(d));
1049
+ });
1050
+ percentExpr = percent.map(function(p) {
1051
+ return AST.percent(parseInt(p));
1052
+ });
1053
+ fractionExpr = fraction.map(function(f) {
1054
+ var denom, num, _ref2;
1055
+ _ref2 = f.split('/'), num = _ref2[0], denom = _ref2[1];
1056
+ return AST.fraction(num, denom);
1057
+ });
1058
+ stringExpr = str.map(function(s) {
1059
+ return AST.string(s);
1060
+ });
1061
+ numericExpr = percentExpr.or(decimalExpr.or(fractionExpr.or(integerExpr)));
1062
+ accessorExpr = accessor.map(function(name) {
1063
+ return AST.accessor(name);
1064
+ });
1065
+ parenFlow = lparen.then(function() {
1066
+ return flow.expected().skip(rparen.expected());
1067
+ });
1068
+ substExpr = parenFlow.map(function(fl) {
1069
+ return AST.subst(fl);
1070
+ });
1071
+ listExpr = lbrack.then(function() {
1072
+ return flow.skip(comma).many().then(function(els) {
1073
+ return flow.or(succeed(null)).then(function(final) {
1074
+ if (final) {
1075
+ els.push(final);
1076
+ }
1077
+ return rbrack.expected().result(AST.list(els));
1078
+ });
1079
+ });
1080
+ });
1081
+ blockExpr = lbrace.then(function() {
1082
+ return flow.skip(rbrace.expected());
1083
+ }).map(AST.block);
1084
+ expr = accessorExpr.or(substExpr.or(listExpr.or(stringExpr.or(blockExpr.or(numericExpr)))));
1085
+ singletonFlow = expr.map(function(e) {
1086
+ return AST.flow(e, null);
1087
+ });
1088
+ func = name.then(function(funcName) {
1089
+ return parenFlow.or(singletonFlow).many().map(function(args) {
1090
+ return AST.func(funcName, args);
1091
+ });
1092
+ });
1093
+ component = expr.or(func);
1094
+ nonPairedFlow = component.then(function(first) {
1095
+ return arrow.then(component).many().map(function(rest) {
1096
+ var comp, cursor, _i, _len;
1097
+ cursor = AST.flow(first, null);
1098
+ for (_i = 0, _len = rest.length; _i < _len; _i++) {
1099
+ comp = rest[_i];
1100
+ cursor = AST.flow(comp, cursor);
1101
+ }
1102
+ return cursor;
1103
+ });
1104
+ });
1105
+ flow = nonPairedFlow.then(function(first) {
1106
+ return pair.then(flow).map(function(second) {
1107
+ return AST.flow(AST.pair(first, second), null);
1108
+ }).or(succeed(first));
1109
+ });
1110
+ metadata = label.then(function(key) {
1111
+ return labelVal.map(function(text) {
1112
+ return AST.metadata(key, text);
1113
+ });
1114
+ });
1115
+ definition = metadata.many().then(function(md) {
1116
+ return name.then(function(n) {
1117
+ return define.expected().then(frame.expected()).map(function(fl) {
1118
+ return AST.definition(md, n, fl);
1119
+ });
1120
+ });
1121
+ });
1122
+ parenFrame = lparen.then(definition.many()).then(function(defs) {
1123
+ return flow.expected().skip(rparen.expected()).map(function(fl) {
1124
+ return AST.frame(defs, fl);
1125
+ });
1126
+ });
1127
+ freeFrame = flow.map(function(fl) {
1128
+ return AST.frame([], fl);
1129
+ });
1130
+ frame = parenFrame.or(freeFrame);
1131
+ program = ignore.then(definition.many().map(function(defs) {
1132
+ return AST.program(defs);
1133
+ }));
1134
+ tassign = lexeme(string('='));
1135
+ bang = lexeme(string('!'));
1136
+ nativeId = lexeme(regex(/^\w+/));
1137
+ concrete = name.map(function(n) {
1138
+ return TypeAST.concrete(n);
1139
+ });
1140
+ typeVar = variable.map(function(v) {
1141
+ return TypeAST.variable(v);
1142
+ });
1143
+ wildType = wildcard.result(TypeAST.wildcard());
1144
+ listType = lbrack.then(function() {
1145
+ return type.skip(rbrack.expected()).map(function(t) {
1146
+ return TypeAST.list(t);
1147
+ });
1148
+ });
1149
+ parenType = lparen.then(function() {
1150
+ return type.skip(rparen.expected());
1151
+ });
1152
+ blockType = lbrace.then(function() {
1153
+ return arrowType.skip(rbrace.expected()).map(function(t) {
1154
+ return TypeAST.block(t);
1155
+ });
1156
+ });
1157
+ nativeType = bang.then(nativeId).map(TypeAST["native"]);
1158
+ simpleType = (typeVar.or(wildType.or(listType.or(parenType.or(blockType.or(concrete.or(nativeType))))))).named('a simple type');
1159
+ type = simpleType.then(function(first) {
1160
+ return pair.then(type.expected()).map(function(second) {
1161
+ return TypeAST.pair(first, second);
1162
+ }).or(succeed(first));
1163
+ });
1164
+ arrowType = type.then(function(first) {
1165
+ return arrow.then(type.expected()).map(function(second) {
1166
+ return TypeAST.arrow(first, second);
1167
+ });
1168
+ });
1169
+ signature = name.then(function(n) {
1170
+ return type.many().then(function(argTypes) {
1171
+ return tassign.then(arrowType).map(function(ftype) {
1172
+ return TypeAST.func(ftype.from, argTypes, ftype.to);
1173
+ });
1174
+ });
1175
+ });
1176
+ fullSignature = ignore.then(signature);
1177
+ isString = function(s) {
1178
+ return typeof s === 'string' || s instanceof String;
1179
+ };
1180
+ assertString = function(s) {
1181
+ if (!isString(s)) {
1182
+ throw 'can only parse strings';
1183
+ }
1184
+ };
1185
+ parse = function(str) {
1186
+ assertString(str);
1187
+ return program.parse(str);
1188
+ };
1189
+ parse.type = function(str) {
1190
+ assertString(str);
1191
+ return fullSignature.parse(str);
1192
+ };
1193
+ parse.unitType = function(str) {
1194
+ assertString(str);
1195
+ return type.parse(str);
1196
+ };
1197
+ return parse;
1198
+ })();
1199
+ // Generated by CoffeeScript 1.6.3
1200
+ var stdlib;
1201
+
1202
+ stdlib = Gibbon.stdlib = (function() {
1203
+ return {
1204
+ "case": {
1205
+ type: parse.type('case [bool : %b] = % -> %b'),
1206
+ impl: function(_, evalList) {
1207
+ return evalList.then(function(list) {
1208
+ var out, step;
1209
+ step = function(pair, next) {
1210
+ return pair.first.then(function(cond) {
1211
+ if (cond.value) {
1212
+ return pair.second;
1213
+ } else {
1214
+ return next();
1215
+ }
1216
+ });
1217
+ };
1218
+ out = function() {
1219
+ throw new Error('Runtime Error: non-exhaustive cases');
1220
+ };
1221
+ return Promise.iter(list.elements, step, out);
1222
+ });
1223
+ }
1224
+ },
1225
+ weight: {
1226
+ type: parse.type('weight [numeric : numeric] = % -> numeric'),
1227
+ impl: function(_, eWeights) {
1228
+ return eWeights.then(function(weights) {
1229
+ return Promise.combine(weights.elements).then(function(pairs) {
1230
+ var denominator, numerator, p, process;
1231
+ numerator = 0;
1232
+ denominator = 0;
1233
+ process = function(pair) {
1234
+ return Promise.combine([pair.first, pair.second]).after(function(_arg) {
1235
+ var first, second;
1236
+ first = _arg[0], second = _arg[1];
1237
+ numerator += first.value * second.value;
1238
+ return denominator += second.value;
1239
+ });
1240
+ };
1241
+ return Promise.combine((function() {
1242
+ var _i, _len, _results;
1243
+ _results = [];
1244
+ for (_i = 0, _len = pairs.length; _i < _len; _i++) {
1245
+ p = pairs[_i];
1246
+ _results.push(process(p));
1247
+ }
1248
+ return _results;
1249
+ })()).then(function() {
1250
+ if (denominator === 0) {
1251
+ return Promise.fail('cannot divide by zero');
1252
+ }
1253
+ debugger;
1254
+ return Promise.unit(Value.number(numerator / denominator));
1255
+ });
1256
+ });
1257
+ });
1258
+ }
1259
+ },
1260
+ filter: {
1261
+ type: parse.type('filter { %a -> bool } = [%a] -> [%a]'),
1262
+ impl: function(evalInput, evalBlock) {
1263
+ return evalBlock.then(function(block) {
1264
+ var fn;
1265
+ fn = block.fn;
1266
+ return evalInput.then(function(list) {
1267
+ var check, e, out;
1268
+ out = [];
1269
+ check = function(thunk) {
1270
+ return fn(thunk).after(function(bool) {
1271
+ if (bool.value) {
1272
+ return out.push(thunk);
1273
+ }
1274
+ });
1275
+ };
1276
+ return Promise.combine((function() {
1277
+ var _i, _len, _ref, _results;
1278
+ _ref = list.elements;
1279
+ _results = [];
1280
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
1281
+ e = _ref[_i];
1282
+ _results.push(check(e));
1283
+ }
1284
+ return _results;
1285
+ })()).map(function() {
1286
+ return Value.list(out);
1287
+ });
1288
+ });
1289
+ });
1290
+ }
1291
+ },
1292
+ scale: {
1293
+ type: parse.type('scale (numeric:numeric) (numeric:numeric) = numeric -> numeric'),
1294
+ impl: function(eInput, eDomain, eRange) {
1295
+ return Promise.combine([eInput, eDomain, eRange]).then(function(_arg) {
1296
+ var bounds, dom, input, range;
1297
+ input = _arg[0], dom = _arg[1], range = _arg[2];
1298
+ bounds = Promise.combine([dom.first, dom.second, range.first, range.second]);
1299
+ return bounds.map(function(_arg1) {
1300
+ var domHigh, domLow, domSize, rangeHigh, rangeLow, rangeSize, retranslated, scaled, translated;
1301
+ domLow = _arg1[0], domHigh = _arg1[1], rangeLow = _arg1[2], rangeHigh = _arg1[3];
1302
+ if (input.value < domLow.value) {
1303
+ input = domLow;
1304
+ } else if (input.value > domHigh.value) {
1305
+ input = domHigh;
1306
+ }
1307
+ domSize = domHigh.value - domLow.value;
1308
+ rangeSize = rangeHigh.value - rangeLow.value;
1309
+ translated = input.value - domLow.value;
1310
+ scaled = translated * rangeSize / domSize;
1311
+ retranslated = scaled + rangeLow.value;
1312
+ return Value.number(retranslated);
1313
+ });
1314
+ });
1315
+ }
1316
+ },
1317
+ map: {
1318
+ type: parse.type('map { %a -> %b } = [%a] -> [%b]'),
1319
+ impl: function(evalList, evalBlock) {
1320
+ return evalList.then(function(list) {
1321
+ return evalBlock.map(function(block) {
1322
+ var e, fn;
1323
+ fn = block.fn;
1324
+ return Value.list(Promise.combine((function() {
1325
+ var _i, _len, _ref, _results;
1326
+ _ref = list.elements;
1327
+ _results = [];
1328
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
1329
+ e = _ref[_i];
1330
+ _results.push(fn(e));
1331
+ }
1332
+ return _results;
1333
+ })()));
1334
+ });
1335
+ });
1336
+ }
1337
+ },
1338
+ count: {
1339
+ type: parse.type('count = [%a] -> numeric'),
1340
+ impl: function(evalList) {
1341
+ return evalList.map(function(list) {
1342
+ return Value.number(list.elements.length);
1343
+ });
1344
+ }
1345
+ },
1346
+ sum: {
1347
+ type: parse.type('sum = [numeric] -> numeric'),
1348
+ impl: function(list) {
1349
+ return list.then(function(val) {
1350
+ return Promise.combine(val.elements).then(function(vals) {
1351
+ var e, out, _i, _len;
1352
+ out = 0;
1353
+ for (_i = 0, _len = vals.length; _i < _len; _i++) {
1354
+ e = vals[_i];
1355
+ out += e.value;
1356
+ }
1357
+ return Promise.unit(Value.number(out));
1358
+ });
1359
+ });
1360
+ }
1361
+ },
1362
+ first: {
1363
+ type: parse.type('first = [%a] -> %a'),
1364
+ impl: function(list) {
1365
+ return list.then(function(val) {
1366
+ return val.elements[0];
1367
+ });
1368
+ }
1369
+ },
1370
+ add: {
1371
+ type: parse.type('add numeric = numeric -> numeric'),
1372
+ impl: function(input, num) {
1373
+ return Promise.combine([input, num]).map(function(_arg) {
1374
+ var lhs, rhs;
1375
+ lhs = _arg[0], rhs = _arg[1];
1376
+ return Value.number(lhs.value + rhs.value);
1377
+ });
1378
+ }
1379
+ },
1380
+ sub: {
1381
+ type: parse.type('sub numeric = numeric -> numeric'),
1382
+ impl: function(input, num) {
1383
+ return Promise.combine([input, num]).map(function(_arg) {
1384
+ var lhs, rhs;
1385
+ lhs = _arg[0], rhs = _arg[1];
1386
+ return Value.number(lhs.value - rhs.value);
1387
+ });
1388
+ }
1389
+ },
1390
+ id: {
1391
+ type: parse.type('id = %a -> %a'),
1392
+ impl: function(x) {
1393
+ return x;
1394
+ }
1395
+ },
1396
+ "else": {
1397
+ type: parse.type('else = % -> bool'),
1398
+ impl: function(_) {
1399
+ return Promise.unit(Value.boolean(true));
1400
+ }
1401
+ },
1402
+ gt: {
1403
+ type: parse.type('gt numeric = numeric -> bool'),
1404
+ impl: function(input, num) {
1405
+ return Promise.combine([input, num]).map(function(_arg) {
1406
+ var lhs, rhs;
1407
+ lhs = _arg[0], rhs = _arg[1];
1408
+ return Value.boolean(lhs.value > rhs.value);
1409
+ });
1410
+ }
1411
+ },
1412
+ lt: {
1413
+ type: parse.type('lt numeric = numeric -> bool'),
1414
+ impl: function(input, num) {
1415
+ return Promise.combine([input, num]).map(function(_arg) {
1416
+ var lhs, rhs;
1417
+ lhs = _arg[0], rhs = _arg[1];
1418
+ return Value.boolean(lhs.value < rhs.value);
1419
+ });
1420
+ }
1421
+ },
1422
+ eq: {
1423
+ type: parse.type('eq %a = %a -> bool'),
1424
+ impl: function(input, obj) {
1425
+ return Promise.combine([input, obj]).then(function(_arg) {
1426
+ var lhs, rhs;
1427
+ lhs = _arg[0], rhs = _arg[1];
1428
+ return lhs.equals(rhs);
1429
+ });
1430
+ }
1431
+ }
1432
+ };
1433
+ })();
1434
+ // Generated by CoffeeScript 1.6.3
1435
+ var Semantic, Type, TypeLookup, analyze, _ref, _ref1, _ref2,
1436
+ __hasProp = {}.hasOwnProperty,
1437
+ __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; },
1438
+ __slice = [].slice;
1439
+
1440
+ Gibbon.Semantic = Semantic = (function(_super) {
1441
+ __extends(Semantic, _super);
1442
+
1443
+ function Semantic() {
1444
+ _ref = Semantic.__super__.constructor.apply(this, arguments);
1445
+ return _ref;
1446
+ }
1447
+
1448
+ Semantic.types({
1449
+ definition: ['dependencies', 'flow'],
1450
+ literal: ['syntax'],
1451
+ accessor: ['annotations'],
1452
+ localAccessor: ['name'],
1453
+ pair: ['first', 'second'],
1454
+ block: ['body'],
1455
+ list: ['elements'],
1456
+ flow: ['type', 'head', 'tail'],
1457
+ func: ['name', 'args'],
1458
+ subst: ['flow']
1459
+ });
1460
+
1461
+ return Semantic;
1462
+
1463
+ })(Union);
1464
+
1465
+ Gibbon.TypeLookup = TypeLookup = (function(_super) {
1466
+ __extends(TypeLookup, _super);
1467
+
1468
+ function TypeLookup() {
1469
+ _ref1 = TypeLookup.__super__.constructor.apply(this, arguments);
1470
+ return _ref1;
1471
+ }
1472
+
1473
+ TypeLookup.types({
1474
+ response: ['id', 'name', 'annotations'],
1475
+ local: ['name']
1476
+ });
1477
+
1478
+ return TypeLookup;
1479
+
1480
+ })(Union);
1481
+
1482
+ Gibbon.Type = Type = (function(_super) {
1483
+ __extends(Type, _super);
1484
+
1485
+ function Type() {
1486
+ _ref2 = Type.__super__.constructor.apply(this, arguments);
1487
+ return _ref2;
1488
+ }
1489
+
1490
+ Type.types({
1491
+ block: ['from', 'to'],
1492
+ pair: ['first', 'second'],
1493
+ list: ['of'],
1494
+ entity: ['id'],
1495
+ numeric: [],
1496
+ string: [],
1497
+ bool: [],
1498
+ abstract: ['expr']
1499
+ });
1500
+
1501
+ Type.prototype.inspect = function() {
1502
+ var v, vals;
1503
+ if (this._tag === 'entity') {
1504
+ return "(entity " + this.id + ")";
1505
+ }
1506
+ vals = this._values;
1507
+ if (!vals.length) {
1508
+ return this._tag;
1509
+ }
1510
+ vals = (function() {
1511
+ var _i, _len, _results;
1512
+ _results = [];
1513
+ for (_i = 0, _len = vals.length; _i < _len; _i++) {
1514
+ v = vals[_i];
1515
+ _results.push(v.inspect());
1516
+ }
1517
+ return _results;
1518
+ })();
1519
+ return "(" + this._tag + " " + (vals.join(' ')) + ")";
1520
+ };
1521
+
1522
+ return Type;
1523
+
1524
+ })(Union);
1525
+
1526
+ analyze = Gibbon.analyze = (function() {
1527
+ var TypeExpr, generate, solve, _ref3;
1528
+ TypeExpr = (function(_super) {
1529
+ __extends(TypeExpr, _super);
1530
+
1531
+ function TypeExpr() {
1532
+ _ref3 = TypeExpr.__super__.constructor.apply(this, arguments);
1533
+ return _ref3;
1534
+ }
1535
+
1536
+ TypeExpr.types({
1537
+ expr: ['expr'],
1538
+ variable: ['name'],
1539
+ access: ['input', 'scope', 'accessor'],
1540
+ destructure: ['constraint', 'name', 'argnum'],
1541
+ "native": ['id'],
1542
+ param: ['name', 'constraints'],
1543
+ any: []
1544
+ });
1545
+
1546
+ TypeExpr.prototype.realize = function() {
1547
+ return this.cases({
1548
+ "native": function(id) {
1549
+ return Type.entity(id);
1550
+ },
1551
+ param: function(name, args) {
1552
+ var arg;
1553
+ return Type[name].apply(Type, (function() {
1554
+ var _i, _len, _results;
1555
+ _results = [];
1556
+ for (_i = 0, _len = args.length; _i < _len; _i++) {
1557
+ arg = args[_i];
1558
+ _results.push(arg.realize());
1559
+ }
1560
+ return _results;
1561
+ })());
1562
+ },
1563
+ other: function() {
1564
+ return Type.abstract(this);
1565
+ }
1566
+ });
1567
+ };
1568
+
1569
+ TypeExpr.prototype.inspect = function() {
1570
+ return this.cases({
1571
+ expr: function(e) {
1572
+ return "<" + (e.inspect()) + ">";
1573
+ },
1574
+ variable: function(name) {
1575
+ return "%" + name;
1576
+ },
1577
+ access: function(input, _, accessor) {
1578
+ return "@" + accessor.name + "[" + (input.inspect()) + "]";
1579
+ },
1580
+ destructure: function(constraint, name, argnum) {
1581
+ return "" + (constraint.inspect()) + "/" + name + "[" + argnum + "]";
1582
+ },
1583
+ "native": function(id) {
1584
+ return "!" + id;
1585
+ },
1586
+ param: function(name, exprs) {
1587
+ var expr;
1588
+ if (!exprs.length) {
1589
+ return "(" + name + ")";
1590
+ }
1591
+ exprs = (function() {
1592
+ var _i, _len, _results;
1593
+ _results = [];
1594
+ for (_i = 0, _len = exprs.length; _i < _len; _i++) {
1595
+ expr = exprs[_i];
1596
+ _results.push(expr.inspect());
1597
+ }
1598
+ return _results;
1599
+ })();
1600
+ return "(" + name + " " + (exprs.join(' ')) + ")";
1601
+ },
1602
+ any: function() {
1603
+ return '*';
1604
+ }
1605
+ });
1606
+ };
1607
+
1608
+ TypeExpr.prototype.equals = function(other) {
1609
+ var _this = this;
1610
+ if (this._tag !== other._tag) {
1611
+ return false;
1612
+ }
1613
+ return this.cases({
1614
+ expr: function(e) {
1615
+ return e === other.expr;
1616
+ },
1617
+ access: function(input, scope, accessor) {
1618
+ if (scope !== other.scope) {
1619
+ return false;
1620
+ }
1621
+ if (accessor !== other.accessor) {
1622
+ return false;
1623
+ }
1624
+ return input.equals(other.input);
1625
+ },
1626
+ "native": function(id) {
1627
+ return id === other.id;
1628
+ },
1629
+ param: function(name, constraints) {
1630
+ var constraint, i, _i, _len;
1631
+ if (name !== other.name) {
1632
+ return false;
1633
+ }
1634
+ for (i = _i = 0, _len = constraints.length; _i < _len; i = ++_i) {
1635
+ constraint = constraints[i];
1636
+ if (!constraint.equals(other.constraints[i])) {
1637
+ return false;
1638
+ }
1639
+ }
1640
+ return true;
1641
+ },
1642
+ destructure: function(constraint, name, argnum) {
1643
+ if (name !== other.name) {
1644
+ return false;
1645
+ }
1646
+ if (argnum !== other.argnum) {
1647
+ return false;
1648
+ }
1649
+ return constraint.equals(other.constraint);
1650
+ },
1651
+ other: function() {
1652
+ return _this === other;
1653
+ }
1654
+ });
1655
+ };
1656
+
1657
+ TypeExpr.fromAST = function(typeAST, scope) {
1658
+ var e, r;
1659
+ r = function(ast) {
1660
+ return TypeExpr.fromAST(ast, scope);
1661
+ };
1662
+ e = TypeExpr;
1663
+ return typeAST.cases({
1664
+ concrete: function(name) {
1665
+ if (typeof Type[name] !== 'function') {
1666
+ throw new Error("unknown type " + name);
1667
+ }
1668
+ return e.param(name, []);
1669
+ },
1670
+ variable: function(name) {
1671
+ return scope.cache(name, function() {
1672
+ return e.variable(name);
1673
+ });
1674
+ },
1675
+ "native": function(id) {
1676
+ return e["native"](id);
1677
+ },
1678
+ wildcard: function() {
1679
+ return e.any();
1680
+ },
1681
+ list: function(el) {
1682
+ return e.param('list', [r(el)]);
1683
+ },
1684
+ block: function(el) {
1685
+ return e.param('block', [r(el.from), r(el.to)]);
1686
+ },
1687
+ pair: function(first, second) {
1688
+ return e.param('pair', [r(first), r(second)]);
1689
+ }
1690
+ });
1691
+ };
1692
+
1693
+ TypeExpr.prototype.map = function(f) {
1694
+ return this.cases({
1695
+ param: function(name, params) {
1696
+ var p;
1697
+ return TypeExpr.param(name, (function() {
1698
+ var _i, _len, _results;
1699
+ _results = [];
1700
+ for (_i = 0, _len = params.length; _i < _len; _i++) {
1701
+ p = params[_i];
1702
+ _results.push(f(p));
1703
+ }
1704
+ return _results;
1705
+ })());
1706
+ },
1707
+ access: function(input, scope, accessor) {
1708
+ return TypeExpr.access(f(input), scope, accessor);
1709
+ },
1710
+ destructure: function(constraint, name, argnum) {
1711
+ return TypeExpr.destructure(f(constraint), name, argnum);
1712
+ },
1713
+ other: function() {
1714
+ return this;
1715
+ }
1716
+ });
1717
+ };
1718
+
1719
+ TypeExpr.prototype.mapAsync = function(f, cb) {
1720
+ return this.cases({
1721
+ param: function(name, params) {
1722
+ return asyncMap(params, f, function(ps) {
1723
+ return cb(TypeExpr.param(name, ps));
1724
+ });
1725
+ },
1726
+ access: function(input, scope, accessor) {
1727
+ return f(input, function(i) {
1728
+ return cb(TypeExpr.access(i, scope, accessor));
1729
+ });
1730
+ },
1731
+ destructure: function(param, name, argnum) {
1732
+ return f(param, function(p) {
1733
+ return cb(TypeExpr.destructure(p, name, argnum));
1734
+ });
1735
+ },
1736
+ other: function() {
1737
+ return cb(this);
1738
+ }
1739
+ });
1740
+ };
1741
+
1742
+ return TypeExpr;
1743
+
1744
+ })(Union);
1745
+ generate = (function() {
1746
+ var NativeContext, Scope;
1747
+ NativeContext = (function() {
1748
+ function NativeContext(globalID, externalLookup) {
1749
+ this.globalID = globalID;
1750
+ this.externalLookup = externalLookup;
1751
+ }
1752
+
1753
+ NativeContext.prototype.lookup = function(id, name, cb) {
1754
+ return this.externalLookup.call(null, id, name, function(err, response) {
1755
+ if (err) {
1756
+ throw err;
1757
+ }
1758
+ return cb(TypeLookup.response(id, name, response));
1759
+ });
1760
+ };
1761
+
1762
+ return NativeContext;
1763
+
1764
+ })();
1765
+ Scope = (function() {
1766
+ Scope.global = function(context, definitions) {
1767
+ return new Scope(null, [], definitions, context);
1768
+ };
1769
+
1770
+ function Scope(parent, breadcrumbs, definitions, context) {
1771
+ this.parent = parent;
1772
+ this.breadcrumbs = breadcrumbs;
1773
+ this.definitions = definitions;
1774
+ this.context = context;
1775
+ this.bindings = new Hash;
1776
+ }
1777
+
1778
+ Scope.prototype.extend = function(name, definitions) {
1779
+ return new Scope(this, this.breadcrumbs.concat([name]), definitions, this.context);
1780
+ };
1781
+
1782
+ Scope.prototype.lookup = function(nativeId, name, cb) {
1783
+ var lexical;
1784
+ if (nativeId === this.context.globalID) {
1785
+ lexical = this.lexicalLookup(name);
1786
+ if (lexical) {
1787
+ return cb(lexical);
1788
+ } else {
1789
+ return this.context.lookup(nativeId, name, cb);
1790
+ }
1791
+ } else {
1792
+ return this.context.lookup(nativeId, name, cb);
1793
+ }
1794
+ };
1795
+
1796
+ Scope.prototype.lexicalLookup = function(name) {
1797
+ var local;
1798
+ local = this.lookupLocal(name);
1799
+ if (local) {
1800
+ return local;
1801
+ }
1802
+ if (this.parent) {
1803
+ return this.parent.lexicalLookup(name);
1804
+ }
1805
+ };
1806
+
1807
+ Scope.prototype.lookupLocal = function(name) {
1808
+ if (!this.bindings.has(name)) {
1809
+ return null;
1810
+ }
1811
+ return TypeLookup.local(this.keyFor(name));
1812
+ };
1813
+
1814
+ Scope.prototype.keyFor = function(name) {
1815
+ return this.breadcrumbs.concat([name]).join('/');
1816
+ };
1817
+
1818
+ Scope.prototype.analyzeDefinitions = function(push) {
1819
+ var def, frameScope, global, _i, _len, _ref4, _results,
1820
+ _this = this;
1821
+ global = TypeExpr["native"](this.context.globalID);
1822
+ _ref4 = this.definitions;
1823
+ _results = [];
1824
+ for (_i = 0, _len = _ref4.length; _i < _len; _i++) {
1825
+ def = _ref4[_i];
1826
+ this.bindings.set(def.name, def);
1827
+ frameScope = this.extend(def.name, def.frame.definitions);
1828
+ frameScope.analyzeDefinitions(push);
1829
+ _results.push((function(def) {
1830
+ var key, pusher;
1831
+ key = _this.keyFor(def.name);
1832
+ pusher = function(lhs, rhs) {
1833
+ return push(key, def, [lhs, rhs]);
1834
+ };
1835
+ return frameScope.analyzeFlow(def.frame.flow, global, pusher);
1836
+ })(def));
1837
+ }
1838
+ return _results;
1839
+ };
1840
+
1841
+ Scope.prototype.analyzeFlow = function(flow, global, push) {
1842
+ var _this = this;
1843
+ if (flow.tail) {
1844
+ this.analyzeFlow(flow.tail, global, push);
1845
+ }
1846
+ return flow.head.cases({
1847
+ accessor: function(name) {
1848
+ var input;
1849
+ input = flow.tail ? TypeExpr.expr(flow.tail) : global;
1850
+ return push(TypeExpr.expr(flow), TypeExpr.access(input, _this, flow.head));
1851
+ },
1852
+ pair: function(first, second) {
1853
+ _this.analyzeFlow(first, global, push);
1854
+ _this.analyzeFlow(second, global, push);
1855
+ return push(TypeExpr.expr(flow), TypeExpr.param('pair', [TypeExpr.expr(first), TypeExpr.expr(second)]));
1856
+ },
1857
+ func: function(name, args) {
1858
+ var arg, ast, func, i, input, scope, _i, _len, _results;
1859
+ func = stdlib[name];
1860
+ ast = func.type;
1861
+ scope = new Hash;
1862
+ input = TypeExpr.fromAST(ast.input, scope);
1863
+ if (flow.tail) {
1864
+ push(TypeExpr.expr(flow.tail), input);
1865
+ } else {
1866
+ push(input, global);
1867
+ }
1868
+ push(TypeExpr.expr(flow), TypeExpr.fromAST(ast.output, scope));
1869
+ _results = [];
1870
+ for (i = _i = 0, _len = args.length; _i < _len; i = ++_i) {
1871
+ arg = args[i];
1872
+ push(TypeExpr.expr(arg), TypeExpr.fromAST(ast.args[i], scope));
1873
+ _results.push(_this.analyzeFlow(arg, global, push));
1874
+ }
1875
+ return _results;
1876
+ },
1877
+ integer: function() {
1878
+ return push(TypeExpr.expr(flow), TypeExpr.param('numeric', []));
1879
+ },
1880
+ decimal: function() {
1881
+ return push(TypeExpr.expr(flow), TypeExpr.param('numeric', []));
1882
+ },
1883
+ string: function() {
1884
+ return push(TypeExpr.expr(flow), TypeExpr.param('string', []));
1885
+ },
1886
+ subst: function(subFlow) {
1887
+ push(TypeExpr.expr(flow), TypeExpr.expr(subFlow));
1888
+ return _this.analyzeFlow(flow, global, push);
1889
+ },
1890
+ list: function(elements) {
1891
+ var el, expected, rest, _i, _len, _results;
1892
+ if (elements.length === 0) {
1893
+ push(TypeExpr.expr(flow), TypeExpr.param('list', [TypeExpr.variable('el')]));
1894
+ return;
1895
+ }
1896
+ expected = elements[0], rest = 2 <= elements.length ? __slice.call(elements, 1) : [];
1897
+ push(TypeExpr.expr(flow), TypeExpr.param('list', [TypeExpr.expr(expected)]));
1898
+ _this.analyzeFlow(expected, global, push);
1899
+ _results = [];
1900
+ for (_i = 0, _len = rest.length; _i < _len; _i++) {
1901
+ el = rest[_i];
1902
+ _this.analyzeFlow(el, global, push);
1903
+ _results.push(push(TypeExpr.expr(el), TypeExpr.expr(expected)));
1904
+ }
1905
+ return _results;
1906
+ },
1907
+ block: function(subFlow) {
1908
+ var input;
1909
+ input = TypeExpr.variable('.input');
1910
+ push(TypeExpr.expr(flow), TypeExpr.param('block', [input, TypeExpr.expr(subFlow)]));
1911
+ return _this.analyzeFlow(subFlow, input, push);
1912
+ }
1913
+ });
1914
+ };
1915
+
1916
+ return Scope;
1917
+
1918
+ })();
1919
+ return function(globalID, externalLookup, program) {
1920
+ var constraintMap, context, scope;
1921
+ context = new NativeContext(globalID, externalLookup);
1922
+ scope = Scope.global(context, program.definitions);
1923
+ constraintMap = new Hash;
1924
+ scope.analyzeDefinitions(function(key, def, constraint) {
1925
+ var entry;
1926
+ entry = constraintMap.cache(key, function() {
1927
+ return {
1928
+ definition: def,
1929
+ constraints: []
1930
+ };
1931
+ });
1932
+ return entry.constraints.push(constraint);
1933
+ });
1934
+ return constraintMap;
1935
+ };
1936
+ })();
1937
+ solve = (function() {
1938
+ var TypeError, consume, logConstraint, _ref4;
1939
+ TypeError = (function(_super) {
1940
+ __extends(TypeError, _super);
1941
+
1942
+ function TypeError() {
1943
+ _ref4 = TypeError.__super__.constructor.apply(this, arguments);
1944
+ return _ref4;
1945
+ }
1946
+
1947
+ TypeError.types({
1948
+ match: ['lhs', 'rhs'],
1949
+ infinite: ['type'],
1950
+ destructure: ['type'],
1951
+ access: ['type'],
1952
+ circular: ['definition']
1953
+ });
1954
+
1955
+ return TypeError;
1956
+
1957
+ })(Union);
1958
+ logConstraint = function(prefix, lhs, rhs) {
1959
+ return console.log(prefix, lhs.inspect(), '=', rhs.inspect());
1960
+ };
1961
+ consume = function(array, next, body) {
1962
+ var loop_, push;
1963
+ push = function(x, y) {
1964
+ return array.push([x, y]);
1965
+ };
1966
+ loop_ = function() {
1967
+ var lhs, rhs, _ref5;
1968
+ if (!(array.length > 0)) {
1969
+ return next();
1970
+ }
1971
+ _ref5 = array.pop(), lhs = _ref5[0], rhs = _ref5[1];
1972
+ return body(lhs, rhs, push, loop_);
1973
+ };
1974
+ return loop_();
1975
+ };
1976
+ return function(constraintMap, finish) {
1977
+ var definitionTypes, errors, semantics, solutions, solveEntry;
1978
+ errors = [];
1979
+ solutions = new CompMap(function(x, y) {
1980
+ return x.equals(y);
1981
+ });
1982
+ semantics = new Hash;
1983
+ definitionTypes = new Hash;
1984
+ solveEntry = function(key, solved) {
1985
+ var constraints, definition, dependencies, done, error, fullSubstitute, lhs, rhs, semanticAccessors, simplify, substitute, _i, _len, _ref5, _ref6;
1986
+ if (semantics.has(key)) {
1987
+ return solved();
1988
+ }
1989
+ _ref5 = constraintMap.get(key), definition = _ref5.definition, constraints = _ref5.constraints;
1990
+ console.log('locking', definition.inspect());
1991
+ definition.lock = true;
1992
+ semanticAccessors = new CompMap;
1993
+ dependencies = [];
1994
+ for (_i = 0, _len = constraints.length; _i < _len; _i++) {
1995
+ _ref6 = constraints[_i], lhs = _ref6[0], rhs = _ref6[1];
1996
+ logConstraint('-> ', lhs, rhs);
1997
+ }
1998
+ substitute = function(texpr, cb) {
1999
+ return solutions.fetchAsync(texpr, (function() {
2000
+ return texpr.mapAsync(substitute, cb);
2001
+ }), cb);
2002
+ };
2003
+ fullSubstitute = function(texpr, cb) {
2004
+ return substitute(texpr, function(substituted) {
2005
+ return simplify(substituted, function(result) {
2006
+ if (!texpr.equals(result)) {
2007
+ logConstraint('%> ', texpr, result);
2008
+ }
2009
+ return cb(result);
2010
+ });
2011
+ });
2012
+ };
2013
+ error = function() {
2014
+ var args, type;
2015
+ type = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
2016
+ errors.push(TypeError[type].apply(TypeError, args));
2017
+ return TypeExpr.any();
2018
+ };
2019
+ simplify = function(expr, cb) {
2020
+ return expr.cases({
2021
+ destructure: function(constraint, name, argnum) {
2022
+ var destructured;
2023
+ destructured = this;
2024
+ return simplify(constraint, function(x) {
2025
+ return cb(x.cases({
2026
+ param: function(paramName, paramArgs) {
2027
+ if (paramName === name) {
2028
+ return paramArgs[argnum];
2029
+ } else {
2030
+ return error('destructure', this);
2031
+ }
2032
+ },
2033
+ other: function() {
2034
+ return error('destructure', this);
2035
+ }
2036
+ }));
2037
+ });
2038
+ },
2039
+ access: function(input, scope, accessor) {
2040
+ return simplify(input, function(x) {
2041
+ return x.cases({
2042
+ "native": function(id) {
2043
+ accessor.source = id;
2044
+ return scope.lookup(id, accessor.name, function(lookup) {
2045
+ dependencies.push(lookup);
2046
+ return lookup.cases({
2047
+ response: function(_, __, response) {
2048
+ semanticAccessors.set(accessor, Semantic.accessor(response.annotations));
2049
+ return cb(TypeExpr.fromAST(Gibbon.parse.unitType(response.type)));
2050
+ },
2051
+ local: function(key) {
2052
+ var localDef;
2053
+ semanticAccessors.set(accessor, Semantic.localAccessor(key));
2054
+ localDef = constraintMap.get(key).definition;
2055
+ if ('lock' in localDef) {
2056
+ return cb(error('circular', localDef));
2057
+ } else {
2058
+ return solveEntry(key, function() {
2059
+ return cb(definitionTypes.get(key));
2060
+ });
2061
+ }
2062
+ }
2063
+ });
2064
+ });
2065
+ },
2066
+ other: function() {
2067
+ return cb(expr);
2068
+ }
2069
+ });
2070
+ });
2071
+ },
2072
+ other: function() {
2073
+ return this.mapAsync(simplify, cb);
2074
+ }
2075
+ });
2076
+ };
2077
+ done = function() {
2078
+ var flowType, toSemanticTree;
2079
+ flowType = function(expr) {
2080
+ if (!solutions.has(TypeExpr.expr(expr))) {
2081
+ solutions.get(TypeExpr.expr(expr));
2082
+ throw 'unsolved!';
2083
+ }
2084
+ return solutions.get(TypeExpr.expr(expr)).realize();
2085
+ };
2086
+ toSemanticTree = function(expr) {
2087
+ return expr.cases({
2088
+ definition: function(name, metadata, frame) {
2089
+ return Semantic.definition(dependencies, toSemanticTree(frame.flow));
2090
+ },
2091
+ flow: function(head, tail) {
2092
+ return Semantic.flow(flowType(this), toSemanticTree(head), tail && toSemanticTree(tail));
2093
+ },
2094
+ accessor: function(name) {
2095
+ if (!semanticAccessors.has(this)) {
2096
+ debugger;
2097
+ }
2098
+ return semanticAccessors.get(this);
2099
+ },
2100
+ func: function(name, args) {
2101
+ var a;
2102
+ return Semantic.func(name, (function() {
2103
+ var _j, _len1, _results;
2104
+ _results = [];
2105
+ for (_j = 0, _len1 = args.length; _j < _len1; _j++) {
2106
+ a = args[_j];
2107
+ _results.push(toSemanticTree(a));
2108
+ }
2109
+ return _results;
2110
+ })());
2111
+ },
2112
+ pair: function(first, second) {
2113
+ return Semantic.pair(toSemanticTree(first), toSemanticTree(second));
2114
+ },
2115
+ block: function(flow) {
2116
+ return Semantic.block(toSemanticTree(flow));
2117
+ },
2118
+ list: function(elements) {
2119
+ var e;
2120
+ return Semantic.list((function() {
2121
+ var _j, _len1, _results;
2122
+ _results = [];
2123
+ for (_j = 0, _len1 = elements.length; _j < _len1; _j++) {
2124
+ e = elements[_j];
2125
+ _results.push(toSemanticTree(e));
2126
+ }
2127
+ return _results;
2128
+ })());
2129
+ },
2130
+ integer: function() {
2131
+ return Semantic.literal(this);
2132
+ },
2133
+ decimal: function() {
2134
+ return Semantic.literal(this);
2135
+ },
2136
+ percent: function() {
2137
+ return Semantic.literal(this);
2138
+ },
2139
+ fraction: function() {
2140
+ return Semantic.literal(this);
2141
+ },
2142
+ string: function() {
2143
+ return Semantic.literal(this);
2144
+ }
2145
+ });
2146
+ };
2147
+ solutions.each(function(k, v) {
2148
+ return logConstraint('=> ', k, v);
2149
+ });
2150
+ console.log('setting key: ' + key);
2151
+ semantics.set(key, toSemanticTree(definition));
2152
+ definitionTypes.set(key, solutions.get(TypeExpr.expr(definition.frame.flow)));
2153
+ console.log('unlocking', definition);
2154
+ delete definition.lock;
2155
+ return solved();
2156
+ };
2157
+ return consume(constraints.reverse(), done, function(lhs, rhs, push, next) {
2158
+ var log, matchError, skip, solveFor, swap;
2159
+ logConstraint(':> ', lhs, rhs);
2160
+ if (lhs.equals(rhs)) {
2161
+ return next();
2162
+ }
2163
+ solveFor = function() {
2164
+ return fullSubstitute(rhs, function(rhs) {
2165
+ var mapper;
2166
+ if (solutions.has(lhs)) {
2167
+ push(solutions.get(lhs), rhs);
2168
+ return next();
2169
+ } else {
2170
+ logConstraint('>> ', lhs, rhs);
2171
+ solutions.set(lhs, rhs);
2172
+ mapper = function(k, texpr, cb) {
2173
+ return fullSubstitute(texpr, (function(s) {
2174
+ solutions.set(k, s);
2175
+ return cb();
2176
+ }));
2177
+ };
2178
+ return solutions.eachAsync(mapper, next);
2179
+ }
2180
+ });
2181
+ };
2182
+ log = function() {
2183
+ logConstraint('?> ', lhs, rhs);
2184
+ return next();
2185
+ };
2186
+ skip = function() {
2187
+ return next();
2188
+ };
2189
+ swap = function() {
2190
+ push(rhs, lhs);
2191
+ return next();
2192
+ };
2193
+ matchError = function() {
2194
+ error('match', lhs, rhs);
2195
+ return next();
2196
+ };
2197
+ return lhs.cases({
2198
+ expr: solveFor,
2199
+ variable: solveFor,
2200
+ any: skip,
2201
+ access: function() {
2202
+ return rhs.cases({
2203
+ expr: swap,
2204
+ variable: swap,
2205
+ param: swap,
2206
+ other: log
2207
+ });
2208
+ },
2209
+ "native": function(id) {
2210
+ return rhs.cases({
2211
+ variable: swap,
2212
+ expr: swap,
2213
+ "native": function(otherId) {
2214
+ if (id === otherId) {
2215
+ return skip();
2216
+ } else {
2217
+ return matchError();
2218
+ }
2219
+ },
2220
+ other: matchError
2221
+ });
2222
+ },
2223
+ param: function() {
2224
+ return rhs.cases({
2225
+ param: function() {
2226
+ var constraint, i, _j, _len1, _ref7;
2227
+ if (lhs.name !== rhs.name) {
2228
+ return matchError();
2229
+ }
2230
+ _ref7 = lhs.constraints;
2231
+ for (i = _j = 0, _len1 = _ref7.length; _j < _len1; i = ++_j) {
2232
+ constraint = _ref7[i];
2233
+ push(constraint, rhs.constraints[i]);
2234
+ }
2235
+ return next();
2236
+ },
2237
+ access: function() {
2238
+ var c, i, _j, _len1, _ref7;
2239
+ _ref7 = lhs.constraints;
2240
+ for (i = _j = 0, _len1 = _ref7.length; _j < _len1; i = ++_j) {
2241
+ c = _ref7[i];
2242
+ push(c, TypeExpr.destructure(rhs, lhs.name, i));
2243
+ }
2244
+ return next();
2245
+ },
2246
+ expr: swap,
2247
+ variable: swap,
2248
+ other: matchError
2249
+ });
2250
+ },
2251
+ other: log
2252
+ });
2253
+ });
2254
+ };
2255
+ return contMap(constraintMap.keys(), solveEntry, function() {
2256
+ if (errors.length === 0) {
2257
+ errors = null;
2258
+ }
2259
+ return finish(errors, semantics);
2260
+ });
2261
+ };
2262
+ })();
2263
+ return function(program, globalID, external, cb) {
2264
+ var constraints;
2265
+ if (!(program instanceof AST)) {
2266
+ program = AST.fromJSON(program);
2267
+ }
2268
+ console.log();
2269
+ console.log(program.inspect());
2270
+ constraints = generate(globalID, external.getType, program);
2271
+ return solve(constraints, cb);
2272
+ };
2273
+ })();
2274
+ // Generated by CoffeeScript 1.6.3
2275
+ var Dependency, Promise, Value, eval_, _ref, _ref1,
2276
+ __hasProp = {}.hasOwnProperty,
2277
+ __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; },
2278
+ __slice = [].slice;
2279
+
2280
+ Value = Gibbon.Value = Value = (function(_super) {
2281
+ var thunksAreEqual;
2282
+
2283
+ __extends(Value, _super);
2284
+
2285
+ function Value() {
2286
+ _ref = Value.__super__.constructor.apply(this, arguments);
2287
+ return _ref;
2288
+ }
2289
+
2290
+ Value.types({
2291
+ string: ['value'],
2292
+ number: ['value'],
2293
+ boolean: ['value'],
2294
+ block: ['fn'],
2295
+ list: ['elements'],
2296
+ pair: ['first', 'second'],
2297
+ entity: ['type', 'id']
2298
+ });
2299
+
2300
+ Value.fromJSON = function(o) {
2301
+ var e;
2302
+ if (typeof o === 'boolean') {
2303
+ return Value.boolean(o);
2304
+ }
2305
+ if (typeof o === 'number') {
2306
+ return Value.number(o);
2307
+ }
2308
+ if (typeof o === 'string') {
2309
+ return Value.string(o);
2310
+ }
2311
+ if (isArray(o)) {
2312
+ return Value.list((function() {
2313
+ var _i, _len, _results;
2314
+ _results = [];
2315
+ for (_i = 0, _len = o.length; _i < _len; _i++) {
2316
+ e = o[_i];
2317
+ _results.push(Value.fromJSON(e));
2318
+ }
2319
+ return _results;
2320
+ })());
2321
+ }
2322
+ switch (o._tag) {
2323
+ case 'entity':
2324
+ return Value.entity(o.type, o.id);
2325
+ case 'pair':
2326
+ return Value.pair(o.first, o.second);
2327
+ }
2328
+ throw new Error('invalid value: ' + o);
2329
+ };
2330
+
2331
+ Value.prototype.promise = function() {
2332
+ return Promise.unit(this.map(function(x) {
2333
+ return x.promise();
2334
+ }));
2335
+ };
2336
+
2337
+ thunksAreEqual = function(list1, list2) {
2338
+ var i, out, step, x, zipped;
2339
+ if (list1.length !== list2.length) {
2340
+ return Promise.unit(Value.boolean(false));
2341
+ }
2342
+ zipped = (function() {
2343
+ var _i, _len, _results;
2344
+ _results = [];
2345
+ for (i = _i = 0, _len = list1.length; _i < _len; i = ++_i) {
2346
+ x = list1[i];
2347
+ _results.push(Promise.combine(x, list2[i]));
2348
+ }
2349
+ return _results;
2350
+ })();
2351
+ step = function(_arg, next) {
2352
+ var x, y;
2353
+ x = _arg[0], y = _arg[1];
2354
+ return x.equals(y).then(function(isEqual) {
2355
+ if (isEqual.value) {
2356
+ return next();
2357
+ } else {
2358
+ return Promise.unit(Value.boolean(false));
2359
+ }
2360
+ });
2361
+ };
2362
+ out = function() {
2363
+ return Promise.unit(Value.boolean(true));
2364
+ };
2365
+ return Promise.iter(zipped, step, out);
2366
+ };
2367
+
2368
+ Value.prototype.equals = function(other) {
2369
+ var wrap;
2370
+ wrap = function(b) {
2371
+ return Promise.unit(Value.boolean(b));
2372
+ };
2373
+ return this.cases({
2374
+ string: function(val) {
2375
+ return wrap(val === other.value);
2376
+ },
2377
+ number: function(val) {
2378
+ return wrap(val === other.value);
2379
+ },
2380
+ boolean: function(val) {
2381
+ return wrap(val === other.value);
2382
+ },
2383
+ block: function() {
2384
+ return wrap(false);
2385
+ },
2386
+ list: function(els) {
2387
+ return thunksAreEqual(els, other.elements);
2388
+ },
2389
+ pair: function(evalFirst, evalSecond) {
2390
+ return thunksAreEqual([evalFirst, evalSecond], [other.first, other.second]);
2391
+ }
2392
+ });
2393
+ };
2394
+
2395
+ Value.prototype.map = function(f) {
2396
+ return this.cases({
2397
+ list: function(els) {
2398
+ var x;
2399
+ return Value.list((function() {
2400
+ var _i, _len, _results;
2401
+ _results = [];
2402
+ for (_i = 0, _len = els.length; _i < _len; _i++) {
2403
+ x = els[_i];
2404
+ _results.push(f(x));
2405
+ }
2406
+ return _results;
2407
+ })());
2408
+ },
2409
+ pair: function(first, second) {
2410
+ return Value.pair(f(first), f(second));
2411
+ },
2412
+ other: function() {
2413
+ return this;
2414
+ }
2415
+ });
2416
+ };
2417
+
2418
+ Value.prototype.mapAsync = function(f, cb) {
2419
+ return this.cases({
2420
+ list: function(els) {
2421
+ return asyncMap(els, f, function(mapped) {
2422
+ return Value.list(mapped);
2423
+ });
2424
+ },
2425
+ pair: function(first, second) {
2426
+ return asyncMap([first, second], f, function(_arg) {
2427
+ var first, second;
2428
+ first = _arg[0], second = _arg[1];
2429
+ return Value.pair(first, second);
2430
+ });
2431
+ },
2432
+ other: function() {
2433
+ return cb(this);
2434
+ }
2435
+ });
2436
+ };
2437
+
2438
+ return Value;
2439
+
2440
+ })(Union);
2441
+
2442
+ Promise = (function() {
2443
+ function Promise(fn) {
2444
+ this.fn = fn;
2445
+ }
2446
+
2447
+ Promise.prototype.force = function(failure, success) {
2448
+ var onFailure, onSuccess,
2449
+ _this = this;
2450
+ if (this.result === true) {
2451
+ return success(this.value, this.dependencies);
2452
+ }
2453
+ if (this.result === false) {
2454
+ return failure(this.value);
2455
+ }
2456
+ onSuccess = function(val, deps) {
2457
+ _this.result = true;
2458
+ _this.value = val;
2459
+ _this.dependencies = deps;
2460
+ return success(val, deps);
2461
+ };
2462
+ onFailure = function(fail) {
2463
+ _this.result = false;
2464
+ _this.value = fail;
2465
+ return failure(fail);
2466
+ };
2467
+ return this.fn.call(null, onFailure, onSuccess);
2468
+ };
2469
+
2470
+ Promise.prototype.then = function(fn) {
2471
+ var _this = this;
2472
+ return new Promise(function(fail, cb) {
2473
+ return _this.force(fail, function(val, deps) {
2474
+ return fn(val, deps).depends(deps).force(fail, cb);
2475
+ });
2476
+ });
2477
+ };
2478
+
2479
+ Promise.prototype.depends = function(deps) {
2480
+ var _this = this;
2481
+ return new Promise(function(fail, cc) {
2482
+ return _this.force(fail, function(val, prevDeps) {
2483
+ return cc(val, prevDeps.concat(deps));
2484
+ });
2485
+ });
2486
+ };
2487
+
2488
+ Promise.prototype.map = function(f) {
2489
+ return this.then(function(v, d) {
2490
+ return Promise.unit(f(v, d));
2491
+ });
2492
+ };
2493
+
2494
+ Promise.prototype.mapDependencies = function(f) {
2495
+ var _this = this;
2496
+ return new Promise(function(fail, cc) {
2497
+ return _this.force(fail, function(val, deps) {
2498
+ return cc(val, f(deps));
2499
+ });
2500
+ });
2501
+ };
2502
+
2503
+ Promise.prototype.after = function(f) {
2504
+ var _this = this;
2505
+ return new Promise(function(fail, cc) {
2506
+ return _this.force(fail, function(val, deps) {
2507
+ f(val, deps);
2508
+ return cc(val, deps);
2509
+ });
2510
+ });
2511
+ };
2512
+
2513
+ Promise.unit = function(e) {
2514
+ if (!(e instanceof Gibbon.Value)) {
2515
+ throw new Error('only make thunks of Value for now');
2516
+ }
2517
+ return new Promise(function(_, f) {
2518
+ return f(e, []);
2519
+ });
2520
+ };
2521
+
2522
+ Promise.fail = function(fail) {
2523
+ return new Promise(function(f, _) {
2524
+ return f(fail);
2525
+ });
2526
+ };
2527
+
2528
+ Promise.lazy = function(f) {
2529
+ return new Promise(function(fail, cc) {
2530
+ return f().force(fail, cc);
2531
+ });
2532
+ };
2533
+
2534
+ Promise.combine = function(thunks) {
2535
+ return new Promise(function(fail, cb) {
2536
+ var mapper;
2537
+ mapper = function(thunk, push) {
2538
+ return thunk.force(fail, function(val, deps) {
2539
+ return push([val, deps]);
2540
+ });
2541
+ };
2542
+ return asyncMap(thunks, mapper, function(vals) {
2543
+ var outDeps, outVal, v;
2544
+ outVal = (function() {
2545
+ var _i, _len, _results;
2546
+ _results = [];
2547
+ for (_i = 0, _len = vals.length; _i < _len; _i++) {
2548
+ v = vals[_i];
2549
+ _results.push(v[0]);
2550
+ }
2551
+ return _results;
2552
+ })();
2553
+ outDeps = catLists((function() {
2554
+ var _i, _len, _results;
2555
+ _results = [];
2556
+ for (_i = 0, _len = vals.length; _i < _len; _i++) {
2557
+ v = vals[_i];
2558
+ _results.push(v[1]);
2559
+ }
2560
+ return _results;
2561
+ })());
2562
+ return cb(outVal, outDeps);
2563
+ });
2564
+ });
2565
+ };
2566
+
2567
+ Promise.iter = function(list, step, out) {
2568
+ var _loop;
2569
+ _loop = function(i) {
2570
+ if (i >= list.length) {
2571
+ return out();
2572
+ }
2573
+ return list[i].then(function(val) {
2574
+ return step(val, function() {
2575
+ return _loop(i + 1);
2576
+ });
2577
+ });
2578
+ };
2579
+ return _loop(0);
2580
+ };
2581
+
2582
+ return Promise;
2583
+
2584
+ })();
2585
+
2586
+ Dependency = (function(_super) {
2587
+ __extends(Dependency, _super);
2588
+
2589
+ function Dependency() {
2590
+ _ref1 = Dependency.__super__.constructor.apply(this, arguments);
2591
+ return _ref1;
2592
+ }
2593
+
2594
+ Dependency.types({
2595
+ lookup: ['entity', 'accessor'],
2596
+ lexical: ['key']
2597
+ });
2598
+
2599
+ Dependency.prototype.equals = function(other) {
2600
+ if (this.tag !== other.tag) {
2601
+ return false;
2602
+ }
2603
+ return this.cases({
2604
+ lookup: function(entity, accessor) {
2605
+ if (entity.id !== other.entity.id) {
2606
+ return false;
2607
+ }
2608
+ if (accessor.name !== other.accessor.name) {
2609
+ return false;
2610
+ }
2611
+ return true;
2612
+ },
2613
+ lexical: function(key) {
2614
+ return key === other.key;
2615
+ }
2616
+ });
2617
+ };
2618
+
2619
+ Dependency.prototype.inspect = function() {
2620
+ return this.cases({
2621
+ lookup: function(entity, accessor) {
2622
+ return "" + (accessor.inspect()) + "<" + entity.id + ">";
2623
+ },
2624
+ lexical: function(definition) {
2625
+ return "@" + definition.name;
2626
+ }
2627
+ });
2628
+ };
2629
+
2630
+ return Dependency;
2631
+
2632
+ })(Union);
2633
+
2634
+ eval_ = Gibbon["eval"] = (function() {
2635
+ var Context, Scope, evalAll;
2636
+ Context = (function() {
2637
+ var CACHE;
2638
+
2639
+ CACHE = new Hash;
2640
+
2641
+ function Context(global, client) {
2642
+ this.global = global;
2643
+ this.client = client;
2644
+ this.globalPromise = Promise.unit(this.global);
2645
+ }
2646
+
2647
+ Context.prototype.lookup = function(entity, accessor) {
2648
+ var annotations, id,
2649
+ _this = this;
2650
+ annotations = accessor.annotations;
2651
+ id = entity.id;
2652
+ return CACHE.cache("" + id + "/" + (JSON.stringify(annotations)), function() {
2653
+ return new Promise(function(fail, cc) {
2654
+ return _this.client.getValue(id, annotations, function(err, val) {
2655
+ var dependency;
2656
+ if (err) {
2657
+ return fail(err);
2658
+ }
2659
+ dependency = Dependency.lookup(entity, accessor);
2660
+ return Value.fromJSON(val).promise().depends([dependency]);
2661
+ });
2662
+ });
2663
+ });
2664
+ };
2665
+
2666
+ return Context;
2667
+
2668
+ })();
2669
+ Scope = (function() {
2670
+ Scope.global = function(context) {
2671
+ return new Scope(null, [], context);
2672
+ };
2673
+
2674
+ function Scope(parent, definitions, context) {
2675
+ var def, _i, _len, _ref2;
2676
+ this.parent = parent;
2677
+ this.definitions = definitions;
2678
+ this.context = context;
2679
+ this.bindings = new Hash;
2680
+ _ref2 = this.definitions;
2681
+ for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
2682
+ def = _ref2[_i];
2683
+ this.setupBinding(def);
2684
+ }
2685
+ }
2686
+
2687
+ Scope.prototype.setupBinding = function(def) {};
2688
+
2689
+ Scope.prototype.extend = function(definitions) {
2690
+ return new Scope(this, definitions, this.context);
2691
+ };
2692
+
2693
+ Scope.prototype.allResults = function() {};
2694
+
2695
+ return Scope;
2696
+
2697
+ })();
2698
+ evalAll = function(semantics, entity, client) {
2699
+ var evalFlow, globalPromise, resultThunks, results;
2700
+ evalFlow = function(flow, global) {
2701
+ var input;
2702
+ input = flow.tail ? evalFlow(flow.tail, global) : global;
2703
+ return flow.head.cases({
2704
+ accessor: function(annotations) {
2705
+ return input.then(function(entity) {
2706
+ var _this = this;
2707
+ return new Promise(function(fail, cc) {
2708
+ return client.getValue(entity.id, annotations, function(err, val) {
2709
+ var dependency;
2710
+ if (err) {
2711
+ return fail(err);
2712
+ }
2713
+ dependency = Dependency.lookup(entity, flow.head);
2714
+ return Value.fromJSON(val).promise().depends([dependency]).force(fail, cc);
2715
+ });
2716
+ });
2717
+ });
2718
+ },
2719
+ localAccessor: function(key) {
2720
+ return new Promise(function(fail, cc) {
2721
+ return resultThunks.get(key).force(fail, function(val, otherDeps) {
2722
+ return cc(val, [Dependency.lexical(key)]);
2723
+ });
2724
+ });
2725
+ },
2726
+ func: function(name, args) {
2727
+ var a, func;
2728
+ func = stdlib[name].impl;
2729
+ args = (function() {
2730
+ var _i, _len, _results;
2731
+ _results = [];
2732
+ for (_i = 0, _len = args.length; _i < _len; _i++) {
2733
+ a = args[_i];
2734
+ _results.push(evalFlow(a, global));
2735
+ }
2736
+ return _results;
2737
+ })();
2738
+ return func.apply(null, [input].concat(__slice.call(args)));
2739
+ },
2740
+ literal: function(syntax) {
2741
+ return syntax.cases({
2742
+ integer: function(value) {
2743
+ return Promise.unit(Value.number(value));
2744
+ },
2745
+ decimal: function(value) {
2746
+ return Promise.unit(Value.number(value));
2747
+ },
2748
+ string: function(value) {
2749
+ return Promise.unit(Value.string(value));
2750
+ }
2751
+ });
2752
+ },
2753
+ pair: function(first, second) {
2754
+ var eFirst, eSecond;
2755
+ eFirst = evalFlow(first, global);
2756
+ eSecond = evalFlow(second, global);
2757
+ return Promise.unit(Value.pair(eFirst, eSecond));
2758
+ },
2759
+ list: function(elements) {
2760
+ var e;
2761
+ elements = (function() {
2762
+ var _i, _len, _results;
2763
+ _results = [];
2764
+ for (_i = 0, _len = elements.length; _i < _len; _i++) {
2765
+ e = elements[_i];
2766
+ _results.push(evalFlow(e, global));
2767
+ }
2768
+ return _results;
2769
+ })();
2770
+ return Promise.unit(Value.list(elements));
2771
+ },
2772
+ block: function(body) {
2773
+ return Promise.unit(Value.block(function(inputPromise) {
2774
+ return evalFlow(body, inputPromise);
2775
+ }));
2776
+ }
2777
+ });
2778
+ };
2779
+ resultThunks = new Hash;
2780
+ results = new Hash;
2781
+ globalPromise = Promise.unit(entity);
2782
+ semantics.each(function(key, definition) {
2783
+ return resultThunks.set(key, Promise.lazy(function() {
2784
+ return evalFlow(definition.flow, globalPromise).after(function(val, deps) {
2785
+ deps = uniq(deps, function(x, y) {
2786
+ return x.equals(y);
2787
+ });
2788
+ return results.set(key, [val, deps]);
2789
+ });
2790
+ }));
2791
+ });
2792
+ return new Promise(function(fail, cc) {
2793
+ return Promise.combine(resultThunks.values()).force(fail, function(_, deps) {
2794
+ return cc(results, deps);
2795
+ });
2796
+ });
2797
+ };
2798
+ return function(semantics, table, id, client, finish) {
2799
+ var entity, key, onFailure, onSuccess, _i, _len, _ref2;
2800
+ _ref2 = semantics.keys();
2801
+ for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
2802
+ key = _ref2[_i];
2803
+ semantics.modify(key, function(val) {
2804
+ if (val instanceof Semantic) {
2805
+ return val;
2806
+ }
2807
+ return Semantic.fromJSON(val);
2808
+ });
2809
+ }
2810
+ entity = Value.entity(table, id);
2811
+ onFailure = function(fail) {
2812
+ return finish(fail);
2813
+ };
2814
+ onSuccess = function(vals) {
2815
+ return finish(null, vals);
2816
+ };
2817
+ return evalAll(semantics, entity, client).force(onFailure, onSuccess);
2818
+ };
2819
+ })();
2820
+ // Generated by CoffeeScript 1.6.3
2821
+ Gibbon.jsonConsumer = (function() {
2822
+ return function(tables) {
2823
+ return {
2824
+ getType: function(id, accessorName, callback) {
2825
+ var fields;
2826
+ if (!tables.hasOwnProperty(id)) {
2827
+ return callback(new Error("no such type " + type));
2828
+ }
2829
+ fields = tables[id].fields;
2830
+ if (!fields.hasOwnProperty(accessorName)) {
2831
+ return callback(new Error("" + type + " has no field " + accessorName));
2832
+ }
2833
+ return callback(null, {
2834
+ type: fields[accessorName],
2835
+ annotations: {
2836
+ name: accessorName,
2837
+ table: id
2838
+ }
2839
+ });
2840
+ },
2841
+ getValue: function(id, annotations, callback) {
2842
+ var entity, values;
2843
+ if (!tables.hasOwnProperty(annotations.table)) {
2844
+ return callback(new Error("no such type " + annotations.table));
2845
+ }
2846
+ values = tables[annotations.table].values;
2847
+ if (!values[id]) {
2848
+ return callback(new Error("no entity with id " + id));
2849
+ }
2850
+ entity = values[id];
2851
+ if (!entity.hasOwnProperty(annotations.name)) {
2852
+ return callback(null, null);
2853
+ }
2854
+ return callback(null, entity[annotations.name]);
2855
+ }
2856
+ };
2857
+ };
2858
+ })();
2859
+ return Gibbon;
2860
+ })();