goodguide-gibbon 0.4.0 → 0.4.1

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.
@@ -71,12 +71,16 @@ module GoodGuide
71
71
  end
72
72
 
73
73
  private
74
- def inspect_expr(expr)
75
- obj = JS.default.gibbon[:TypeExpr].fromJSON(expr)
74
+ def inspect_js(type, js)
75
+ obj = JS.default.gibbon[type].fromJSON(js)
76
76
  inspect = obj[:inspect]
77
77
  inspect.methodcall(obj)
78
78
  end
79
79
 
80
+ def inspect_expr(expr)
81
+ inspect_js :TypeExpr, expr
82
+ end
83
+
80
84
  def semantic_error_message(e)
81
85
  case e['_tag']
82
86
  when 'match'
@@ -87,15 +91,17 @@ module GoodGuide
87
91
  type = inspect_expr e['type']
88
92
  "could not destructure #{type}"
89
93
  when 'lookup'
90
- query_name = e['query']['name']
91
- query_type = e['query']['type']
92
- query = "@#{query_type}(#{query_name})"
94
+ query = inspect_js :AST, e['query']
93
95
  id = e['id']
94
96
  error = e['error']
95
97
  "error analyzing #{query} on #{id}: #{error}"
96
98
  when 'circular'
97
99
  chain = e['crumbs'].join(' -> ')
98
100
  "circular dependency: #{chain}"
101
+ when 'func'
102
+ funcname = e['node']['name']
103
+ expr = inspect_expr e['node']
104
+ "no such function: #{funcname} (in #{expr})"
99
105
  else
100
106
  raise "unknown error type #{e['_tag']}"
101
107
  end
@@ -163,8 +169,8 @@ module GoodGuide
163
169
  @queries ||= {}
164
170
  end
165
171
 
166
- def self.query(name, &impl)
167
- queries[name.to_s] = impl
172
+ def self.query(type, &impl)
173
+ queries[type.to_s] = impl
168
174
  end
169
175
 
170
176
  def to_js
@@ -201,7 +207,7 @@ module GoodGuide
201
207
 
202
208
  def analyze_query(input_id, query, t)
203
209
  query_impl = get_query(query.type)
204
- analysis = instance_exec(input_id, query.name, t, &query_impl)
210
+ analysis = instance_exec(input_id, obj_to_ruby(query.args), t, &query_impl)
205
211
  analysis[:annotations]['_query_type'] = query.type
206
212
  analysis
207
213
  end
@@ -1,7 +1,7 @@
1
1
  module GoodGuide
2
2
  module Gibbon
3
3
  def self.version
4
- '0.4.0'
4
+ '0.4.1'
5
5
  end
6
6
  end
7
7
  end
@@ -121,6 +121,22 @@ Parsimmon.Parser = P(function(_, _super, Parser) {
121
121
  function success(stream, i, result) { return result; }
122
122
  };
123
123
 
124
+ function furthestFailure(onFailure, myI, myExpected) {
125
+ return function(stream, yourI, yourExpected) {
126
+ if (myI > yourI) return onFailure(stream, myI, myExpected);
127
+ else return onFailure.apply(this, arguments);
128
+ };
129
+ }
130
+
131
+ function furthestFailureSuccess(onSuccess, myFurthestFailureI, myFurthestExpected) {
132
+ return function(stream, i, result, yourFurthestFailureI, yourFurthestExpected) {
133
+ if (myFurthestFailureI > yourFurthestFailureI) {
134
+ return onSuccess(stream, i, result, myFurthestFailureI, myFurthestExpected);
135
+ }
136
+ else return onSuccess.apply(this, arguments);
137
+ };
138
+ }
139
+
124
140
  // -*- primitive combinators -*- //
125
141
  _.or = function(alternative) {
126
142
  var self = this;
@@ -128,8 +144,10 @@ Parsimmon.Parser = P(function(_, _super, Parser) {
128
144
  return Parser(function(stream, i, onSuccess, onFailure) {
129
145
  return self._(stream, i, onSuccess, failure);
130
146
 
131
- function failure(stream, newI) {
132
- return alternative._(stream, i, onSuccess, onFailure);
147
+ function failure(stream, newI, expected) {
148
+ var altSuccess = furthestFailureSuccess(onSuccess, newI, expected);
149
+ var altFailure = furthestFailure(onFailure, newI, expected);
150
+ return alternative._(stream, i, altSuccess, altFailure);
133
151
  }
134
152
  });
135
153
  };
@@ -140,9 +158,11 @@ Parsimmon.Parser = P(function(_, _super, Parser) {
140
158
  return Parser(function(stream, i, onSuccess, onFailure) {
141
159
  return self._(stream, i, success, onFailure);
142
160
 
143
- function success(stream, newI, result) {
161
+ function success(stream, newI, result, furthestFailureI, furthestExpected) {
144
162
  var nextParser = (next instanceof Parser ? next : next(result));
145
- return nextParser._(stream, newI, onSuccess, onFailure);
163
+ var nextSuccess = furthestFailureSuccess(onSuccess, furthestFailureI, furthestExpected);
164
+ var nextFailure = furthestFailure(onFailure, furthestFailureI, furthestExpected);
165
+ return nextParser._(stream, newI, nextSuccess, nextFailure);
146
166
  }
147
167
  });
148
168
  };
@@ -167,15 +187,22 @@ Parsimmon.Parser = P(function(_, _super, Parser) {
167
187
  return Parser(function(stream, i, onSuccess, onFailure) {
168
188
  var xs = [];
169
189
  while (self._(stream, i, success, failure));
170
- return onSuccess(stream, i, xs);
190
+ var furthestFailureI, furthestExpected;
191
+ return onSuccess(stream, i, xs, furthestFailureI, furthestExpected);
171
192
 
172
- function success(stream, newI, x) {
193
+ function success(stream, newI, x, successFurthestFailureI, successFurthestExpected) {
173
194
  i = newI;
174
195
  xs.push(x);
196
+ furthestFailureI = successFurthestFailureI;
197
+ furthestExpected = successFurthestExpected;
175
198
  return true;
176
199
  }
177
200
 
178
- function failure() {
201
+ function failure(stream, newI, expected) {
202
+ if (!(furthestFailureI > newI)) {
203
+ furthestFailureI = newI;
204
+ furthestExpected = expected;
205
+ }
179
206
  return false;
180
207
  }
181
208
  });
@@ -208,32 +235,32 @@ Parsimmon.Parser = P(function(_, _super, Parser) {
208
235
  return Parser(function(stream, i, onSuccess, onFailure) {
209
236
  var xs = [];
210
237
  var result = true;
211
- var failure;
238
+ var furthestFailureI, furthestExpected;
212
239
 
213
240
  for (var times = 0; times < min; times += 1) {
214
- result = self._(stream, i, success, firstFailure);
215
- if (!result) return onFailure(stream, i, failure);
241
+ result = self._(stream, i, success, failure);
242
+ if (!result) return onFailure(stream, furthestFailureI, furthestExpected);
216
243
  }
217
244
 
218
245
  for (; times < max && result; times += 1) {
219
- result = self._(stream, i, success, secondFailure);
246
+ result = self._(stream, i, success, failure);
220
247
  }
221
248
 
222
- return onSuccess(stream, i, xs);
249
+ return onSuccess(stream, i, xs, furthestFailureI, furthestExpected);
223
250
 
224
- function success(stream, newI, x) {
225
- xs.push(x);
251
+ function success(stream, newI, x, successFurthestFailureI, successFurthestExpected) {
226
252
  i = newI;
253
+ xs.push(x);
254
+ furthestFailureI = successFurthestFailureI;
255
+ furthestExpected = successFurthestExpected;
227
256
  return true;
228
257
  }
229
258
 
230
- function firstFailure(stream, newI, msg) {
231
- failure = msg;
232
- i = newI;
233
- return false;
234
- }
235
-
236
- function secondFailure(stream, newI, msg) {
259
+ function failure(stream, newI, expected) {
260
+ if (!(furthestFailureI > newI)) {
261
+ furthestFailureI = newI;
262
+ furthestExpected = expected;
263
+ }
237
264
  return false;
238
265
  }
239
266
  });
@@ -268,7 +295,7 @@ Parsimmon.Parser = P(function(_, _super, Parser) {
268
295
  var head = stream.slice(i, i+len);
269
296
 
270
297
  if (head === str) {
271
- return onSuccess(stream, i+len, head);
298
+ return onSuccess(stream, i+len, head, -1);
272
299
  }
273
300
  else {
274
301
  return onFailure(stream, i, expected);
@@ -286,7 +313,7 @@ Parsimmon.Parser = P(function(_, _super, Parser) {
286
313
 
287
314
  if (match) {
288
315
  var result = match[0];
289
- return onSuccess(stream, i+result.length, result);
316
+ return onSuccess(stream, i+result.length, result, -1);
290
317
  }
291
318
  else {
292
319
  return onFailure(stream, i, expected);
@@ -316,17 +343,17 @@ Parsimmon.Parser = P(function(_, _super, Parser) {
316
343
  var any = Parsimmon.any = Parser(function(stream, i, onSuccess, onFailure) {
317
344
  if (i >= stream.length) return onFailure(stream, i, 'any character');
318
345
 
319
- return onSuccess(stream, i+1, stream.charAt(i));
346
+ return onSuccess(stream, i+1, stream.charAt(i), -1);
320
347
  });
321
348
 
322
349
  var all = Parsimmon.all = Parser(function(stream, i, onSuccess, onFailure) {
323
- return onSuccess(stream, stream.length, stream.slice(i));
350
+ return onSuccess(stream, stream.length, stream.slice(i), -1);
324
351
  });
325
352
 
326
353
  var eof = Parsimmon.eof = Parser(function(stream, i, onSuccess, onFailure) {
327
354
  if (i < stream.length) return onFailure(stream, i, 'EOF');
328
355
 
329
- return onSuccess(stream, i, '');
356
+ return onSuccess(stream, i, '', -1);
330
357
  });
331
358
  });
332
359
  return Parsimmon;
@@ -862,7 +889,7 @@ Gibbon.AST = AST = (function(_super) {
862
889
  block: ['flow'],
863
890
  list: ['elements', 'squish'],
864
891
  defaulted: ['body', 'alternative'],
865
- query: ['type', 'name'],
892
+ query: ['type', 'args'],
866
893
  func: ['name', 'args'],
867
894
  pair: ['first', 'second'],
868
895
  flow: ['head', 'tail'],
@@ -897,11 +924,12 @@ Gibbon.AST = AST = (function(_super) {
897
924
  string: function(s) {
898
925
  return "'" + s + "'";
899
926
  },
900
- query: function(query, name) {
927
+ query: function(query, args) {
901
928
  if (query === 'access') {
902
- query = '';
929
+ return "@" + args[0];
930
+ } else {
931
+ return "." + query + " " + (args.join(' '));
903
932
  }
904
- return "@" + query + "(" + name + ")";
905
933
  },
906
934
  subst: function(flow) {
907
935
  return "(" + (flow.inspect()) + ")";
@@ -1082,11 +1110,12 @@ parse = Gibbon.parse = (function() {
1082
1110
  return AST.string(s);
1083
1111
  });
1084
1112
  accessorExpr = accessor.map(function(name) {
1085
- return AST.query('access', name);
1113
+ debugger;
1114
+ return AST.query('access', [name]);
1086
1115
  });
1087
1116
  queryExpr = query.then(function(q) {
1088
- return name.map(function(n) {
1089
- return AST.query(q, n);
1117
+ return name.many().map(function(args) {
1118
+ return AST.query(q, args);
1090
1119
  });
1091
1120
  });
1092
1121
  numericExpr = percentExpr.or(decimalExpr.or(fractionExpr.or(integerExpr)));
@@ -1381,7 +1410,7 @@ Gibbon.TypeExpr = TypeExpr = (function(_super) {
1381
1410
  return "%" + name;
1382
1411
  },
1383
1412
  query: function(input, _, query) {
1384
- return "@" + query.type + "(" + query.name + ")[" + (input.inspect()) + "]";
1413
+ return "" + (input.inspect()) + "[" + (query.inspect()) + "]";
1385
1414
  },
1386
1415
  destructure: function(constraint, name, argnum) {
1387
1416
  return "" + (constraint.inspect()) + "/" + name + "[" + argnum + "]";
@@ -1611,7 +1640,7 @@ analyze = Gibbon.analyze = (function() {
1611
1640
  Scope.prototype.lookup = function(nativeId, query, cb) {
1612
1641
  var lexical;
1613
1642
  if (query.type === 'access' && nativeId === this.context.globalID) {
1614
- lexical = this.lexicalLookup(query.name);
1643
+ lexical = this.lexicalLookup(query.args[0]);
1615
1644
  if (lexical) {
1616
1645
  return cb(lexical);
1617
1646
  } else {
@@ -1720,7 +1749,7 @@ analyze = Gibbon.analyze = (function() {
1720
1749
  },
1721
1750
  subst: function(subFlow) {
1722
1751
  push(TypeExpr.expr(flow), TypeExpr.expr(subFlow));
1723
- return _this.analyzeFlow(flow, global, push);
1752
+ return _this.analyzeFlow(subFlow, global, push);
1724
1753
  },
1725
1754
  list: function(elements) {
1726
1755
  var el, expected, rest, _i, _len, _results;
@@ -1950,8 +1979,9 @@ analyze = Gibbon.analyze = (function() {
1950
1979
  return Semantic.flow(flowType(this), toSemanticTree(head), tail && toSemanticTree(tail));
1951
1980
  },
1952
1981
  query: function(type, name) {
1982
+ var _this = this;
1953
1983
  DEBUG(function() {
1954
- if (!semanticAccessors.has(this)) {
1984
+ if (!semanticAccessors.has(_this)) {
1955
1985
  debugger;
1956
1986
  }
1957
1987
  });
@@ -1975,6 +2005,9 @@ analyze = Gibbon.analyze = (function() {
1975
2005
  block: function(flow) {
1976
2006
  return Semantic.block(toSemanticTree(flow));
1977
2007
  },
2008
+ subst: function(subFlow) {
2009
+ return toSemanticTree(subFlow);
2010
+ },
1978
2011
  list: function(elements, squish) {
1979
2012
  var e;
1980
2013
  return Semantic.list((function() {
@@ -2132,9 +2165,10 @@ analyze = Gibbon.analyze = (function() {
2132
2165
  DEBUG.log('initial crumbs', initialCrumbs);
2133
2166
  return contMap(initialCrumbs, solveEntry, function() {
2134
2167
  if (errors.length === 0) {
2135
- errors = null;
2168
+ return finish(null, semantics);
2169
+ } else {
2170
+ return finish(errors);
2136
2171
  }
2137
- return finish(errors, semantics);
2138
2172
  });
2139
2173
  };
2140
2174
  })();
@@ -2357,33 +2391,10 @@ Value = Gibbon.Value = Value = (function(_super) {
2357
2391
  })(Union);
2358
2392
 
2359
2393
  Promise = (function() {
2360
- function Promise(fn) {
2361
- this.fn = fn;
2394
+ function Promise(force) {
2395
+ this.force = force;
2362
2396
  }
2363
2397
 
2364
- Promise.prototype.force = function(failure, success) {
2365
- var onFailure, onSuccess,
2366
- _this = this;
2367
- if (this.result === true) {
2368
- return success(this.value, this.dependencies);
2369
- }
2370
- if (this.result === false) {
2371
- return failure(this.value);
2372
- }
2373
- onSuccess = function(val, deps) {
2374
- _this.result = true;
2375
- _this.value = val;
2376
- _this.dependencies = deps;
2377
- return success(val, deps);
2378
- };
2379
- onFailure = function(fail) {
2380
- _this.result = false;
2381
- _this.value = fail;
2382
- return failure(fail);
2383
- };
2384
- return this.fn.call(null, onFailure, onSuccess);
2385
- };
2386
-
2387
2398
  Promise.prototype.then = function(fn) {
2388
2399
  var _this = this;
2389
2400
  return new Promise(function(fail, cb) {
@@ -2744,15 +2755,486 @@ eval_ = Gibbon["eval"] = (function() {
2744
2755
  };
2745
2756
  })();
2746
2757
  // Generated by CoffeeScript 1.6.3
2758
+ var IR, _ref,
2759
+ __hasProp = {}.hasOwnProperty,
2760
+ __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; };
2761
+
2762
+ Gibbon.IR = IR = (function(_super) {
2763
+ var nameGen;
2764
+
2765
+ __extends(IR, _super);
2766
+
2767
+ function IR() {
2768
+ _ref = IR.__super__.constructor.apply(this, arguments);
2769
+ return _ref;
2770
+ }
2771
+
2772
+ IR.types({
2773
+ global: [],
2774
+ constant: ['value'],
2775
+ variable: ['name'],
2776
+ branch: ['cond', 'ifTrue', 'ifFalse'],
2777
+ bind: ['name', 'value', 'expr'],
2778
+ delist: ['expr', 'index'],
2779
+ depair: ['expr', 'key'],
2780
+ list: ['elements'],
2781
+ foldList: ['list', 'out', 'arg', 'accumArg', 'idxArg', 'body'],
2782
+ mapList: ['list', 'arg', 'idxArg', 'body'],
2783
+ zipLists: ['first', 'second'],
2784
+ filterList: ['list', 'arg', 'body'],
2785
+ squishList: ['list'],
2786
+ len: ['list'],
2787
+ pair: ['first', 'second'],
2788
+ block: ['name', 'body'],
2789
+ app: ['block', 'arg'],
2790
+ query: ['expr', 'annotations'],
2791
+ localQuery: ['expr', 'key'],
2792
+ fail: ['message'],
2793
+ binop: ['op', 'lhs', 'rhs'],
2794
+ extern: ['name', 'value'],
2795
+ rescue: ['expr', 'default']
2796
+ });
2797
+
2798
+ IR.makeVariable = function(name) {
2799
+ return IR.variable(nameGen(name));
2800
+ };
2801
+
2802
+ IR.prototype.branch = function(ifTrue, ifFalse) {
2803
+ return IR.branch(this, ifTrue, ifFalse);
2804
+ };
2805
+
2806
+ IR.prototype.delist = function(index) {
2807
+ return IR.delist(this, index);
2808
+ };
2809
+
2810
+ IR.prototype.depair = function(key) {
2811
+ return IR.depair(this, key);
2812
+ };
2813
+
2814
+ IR.prototype.query = function(annotations) {
2815
+ return IR.query(this, annotations);
2816
+ };
2817
+
2818
+ IR.prototype.localQuery = function(key) {
2819
+ return IR.localQuery(this, key);
2820
+ };
2821
+
2822
+ IR.prototype.binop = function(op, other) {
2823
+ return IR.binop(op, this, other);
2824
+ };
2825
+
2826
+ IR.prototype.extern = function(name) {
2827
+ return IR.extern(name, this);
2828
+ };
2829
+
2830
+ IR.prototype.app = function(arg) {
2831
+ return IR.app(this, arg);
2832
+ };
2833
+
2834
+ IR.prototype.squish = function() {
2835
+ return IR.squishList(this);
2836
+ };
2837
+
2838
+ IR.prototype.len = function() {
2839
+ return IR.len(this);
2840
+ };
2841
+
2842
+ IR.prototype.seq = function(name, f) {
2843
+ name = nameGen(name);
2844
+ return IR.bind(name, this, f(IR.variable(name)));
2845
+ };
2846
+
2847
+ IR.prototype.mapList = function(f) {
2848
+ var elName, ixName;
2849
+ elName = nameGen('el');
2850
+ ixName = nameGen('i');
2851
+ return IR.mapList(this, elName, ixName, f(IR.variable(elName), IR.variable(ixName)));
2852
+ };
2853
+
2854
+ IR.prototype.foldList = function(init, f) {
2855
+ var accumName, body, elName, ixName;
2856
+ elName = nameGen('el');
2857
+ ixName = nameGen('i');
2858
+ accumName = nameGen('next');
2859
+ body = f(IR.variable(elName), IR.variable(accumName), IR.variable(ixName));
2860
+ return IR.foldList(this, init, elName, accumName, ixName, body);
2861
+ };
2862
+
2863
+ IR.prototype.zipList = function(other) {
2864
+ return IR.zipLists(this, other);
2865
+ };
2866
+
2867
+ IR.prototype.filterList = function(f) {
2868
+ var name;
2869
+ name = nameGen('el');
2870
+ return IR.filterList(this, name, f(IR.variable(name)));
2871
+ };
2872
+
2873
+ IR.prototype.subtrees = function() {
2874
+ var double, single;
2875
+ single = function(x) {
2876
+ return [x];
2877
+ };
2878
+ double = function(x, y) {
2879
+ return [x, y];
2880
+ };
2881
+ return this.cases({
2882
+ branch: function(c, t, f) {
2883
+ return [c, t, f];
2884
+ },
2885
+ bind: function(n, v, e) {
2886
+ return [v, e];
2887
+ },
2888
+ delist: single,
2889
+ depair: single,
2890
+ list: function(e) {
2891
+ return e;
2892
+ },
2893
+ foldList: function(l, o, a, aa, ia, b) {
2894
+ return [l, o, b];
2895
+ },
2896
+ mapList: function(l, a, ia, b) {
2897
+ return [l, b];
2898
+ },
2899
+ zipLists: double,
2900
+ len: single,
2901
+ pair: double,
2902
+ block: function(n, b) {
2903
+ return [b];
2904
+ },
2905
+ app: double,
2906
+ query: single,
2907
+ localQuery: single,
2908
+ binop: function(op, l, r) {
2909
+ return [l, r];
2910
+ },
2911
+ extern: function(n, v) {
2912
+ return [v];
2913
+ },
2914
+ rescue: double,
2915
+ other: function() {
2916
+ return [];
2917
+ }
2918
+ });
2919
+ };
2920
+
2921
+ IR.prototype.map = function(f) {
2922
+ return this.cases({
2923
+ branch: function(c, tr, fa) {
2924
+ return IR.branch(f(c), f(tr), f(fa));
2925
+ },
2926
+ bind: function(n, v, e) {
2927
+ return IR.bind(n, f(v), f(e));
2928
+ },
2929
+ delist: function(e, i) {
2930
+ return IR.delist(f(e), i);
2931
+ },
2932
+ depair: function(e, k) {
2933
+ return IR.depair(f(e), k);
2934
+ },
2935
+ list: function(els) {
2936
+ var e;
2937
+ return IR.list((function() {
2938
+ var _i, _len, _results;
2939
+ _results = [];
2940
+ for (_i = 0, _len = els.length; _i < _len; _i++) {
2941
+ e = els[_i];
2942
+ _results.push(f(e));
2943
+ }
2944
+ return _results;
2945
+ })());
2946
+ },
2947
+ foldList: function(l, o, a, i, aa, b) {
2948
+ return IR.foldList(f(l), f(o), a, i, aa, f(b));
2949
+ },
2950
+ mapList: function(l, a, i, b) {
2951
+ return IR.mapList(f(l), i, a, f(b));
2952
+ },
2953
+ zipLists: function(l, r) {
2954
+ return IR.zipLists(f(l), f(r));
2955
+ },
2956
+ len: function(l) {
2957
+ return IR.len(f(l));
2958
+ },
2959
+ pair: function(x, y) {
2960
+ return IR.pair(f(x), f(y));
2961
+ },
2962
+ block: function(n, b) {
2963
+ return IR.block(n, f(b));
2964
+ },
2965
+ app: function(b, a) {
2966
+ return IR.app(f(b), f(a));
2967
+ },
2968
+ query: function(e, a) {
2969
+ return IR.query(f(e), a);
2970
+ },
2971
+ localQuery: function(e, k) {
2972
+ return IR.localQuery(f(e), k);
2973
+ },
2974
+ binop: function(o, l, r) {
2975
+ return IR.binop(o, f(l), f(r));
2976
+ },
2977
+ extern: function(n, v) {
2978
+ return IR.extern(n, f(v));
2979
+ },
2980
+ other: function() {
2981
+ return this;
2982
+ }
2983
+ });
2984
+ };
2985
+
2986
+ IR.prototype.replace = function(expr, other) {
2987
+ if (this.equals(expr)) {
2988
+ return other;
2989
+ }
2990
+ return this.map(function(x) {
2991
+ return x.replace(expr, other);
2992
+ });
2993
+ };
2994
+
2995
+ IR.prototype.subst = function(varName, expr) {
2996
+ if (this._tag === 'variable' && this.name === varName) {
2997
+ return expr;
2998
+ }
2999
+ return this.map(function(x) {
3000
+ return x.subst(varName, expr);
3001
+ });
3002
+ };
3003
+
3004
+ IR.prototype.equals = function(other) {
3005
+ if (this._tag !== other._tag) {
3006
+ return false;
3007
+ }
3008
+ return this.cases({
3009
+ branch: function(cond, ifTrue, ifFalse) {
3010
+ return cond.equals(other.cond) && ifTrue.equals(other.ifTrue) && ifFalse.equals(other.ifFalse);
3011
+ },
3012
+ bind: function(name, val, expr) {
3013
+ if (!val.equals(other.value)) {
3014
+ return false;
3015
+ }
3016
+ return expr.equals(other.replace(IR.variable(other.name), IR.variable(name)));
3017
+ },
3018
+ variable: function(name) {
3019
+ return name === other.name;
3020
+ },
3021
+ delist: function(expr, index) {
3022
+ return expr.equals(other.expr) && index.equals(other.index);
3023
+ },
3024
+ depair: function(expr, key) {
3025
+ return expr.equals(other.expr) && key.equals(other.key);
3026
+ },
3027
+ list: function(els) {
3028
+ var el, i, _i, _len;
3029
+ for (i = _i = 0, _len = els.length; _i < _len; i = ++_i) {
3030
+ el = els[i];
3031
+ if (!el.equals(other.elements[i])) {
3032
+ return false;
3033
+ }
3034
+ }
3035
+ return true;
3036
+ },
3037
+ pair: function(x, y) {
3038
+ return x.equals(other.first) && y.equals(other.second);
3039
+ },
3040
+ query: function(expr, annotations) {
3041
+ return JSON.stringify(annotations) === JSON.stringify(other.annotations) && expr.equals(other.expr);
3042
+ },
3043
+ localQuery: function(expr, key) {
3044
+ return key === other.key && expr.equals(other.expr);
3045
+ }
3046
+ });
3047
+ };
3048
+
3049
+ nameGen = (function() {
3050
+ var count;
3051
+ count = 0;
3052
+ return function(name) {
3053
+ return "" + name + (count += 1);
3054
+ };
3055
+ })();
3056
+
3057
+ IR.prototype.inspect = function(indent) {
3058
+ if (indent == null) {
3059
+ indent = 0;
3060
+ }
3061
+ return this.cases({
3062
+ global: function() {
3063
+ return '$';
3064
+ },
3065
+ branch: function(cond, ifTrue, ifFalse) {
3066
+ return "(IF " + (cond.inspect()) + " " + (ifTrue.inspect()) + " " + (ifFalse.inspect()) + ")";
3067
+ },
3068
+ bind: function(name, val, expr) {
3069
+ return "(" + name + "=" + (val.inspect()) + " " + (expr.inspect()) + ")";
3070
+ },
3071
+ variable: function(name) {
3072
+ return name;
3073
+ },
3074
+ delist: function(e, i) {
3075
+ return "([" + i + "] " + (e.inspect()) + ")";
3076
+ },
3077
+ depair: function(e, k) {
3078
+ return "([" + k + "] " + (e.inspect()) + ")";
3079
+ },
3080
+ list: function(els) {
3081
+ var e;
3082
+ return "(LIST " + (((function() {
3083
+ var _i, _len, _results;
3084
+ _results = [];
3085
+ for (_i = 0, _len = els.length; _i < _len; _i++) {
3086
+ e = els[_i];
3087
+ _results.push(e.inspect());
3088
+ }
3089
+ return _results;
3090
+ })()).join(' ')) + ")";
3091
+ },
3092
+ len: function(e) {
3093
+ return "(LEN " + (e.inspect()) + ")";
3094
+ },
3095
+ mapList: function(list, arg, i, body) {
3096
+ return "(MAP " + (list.inspect()) + " " + arg + " " + i + " " + (body.inspect()) + ")";
3097
+ },
3098
+ foldList: function(list, out, arg, next, i, body) {
3099
+ return "(FOLDR " + (list.inspect()) + " " + (out.inspect()) + " " + arg + " " + next + " " + i + " " + (body.inspect()) + ")";
3100
+ },
3101
+ zipLists: function(x, y) {
3102
+ return "(ZIP " + (x.inspect()) + " " + (y.inspect()) + ")";
3103
+ },
3104
+ pair: function(x, y) {
3105
+ return "(PAIR " + (x.inspect()) + " " + (y.inspect()) + ")";
3106
+ },
3107
+ query: function(e, a) {
3108
+ return "(QUERY " + (JSON.stringify(a)) + " " + (e.inspect()) + ")";
3109
+ },
3110
+ block: function(a, e) {
3111
+ return "(LAMBDA " + a + " " + (e.inspect()) + ")";
3112
+ },
3113
+ app: function(b, a) {
3114
+ return "(APPLY " + (b.inspect()) + " " + (a.inspect()) + ")";
3115
+ },
3116
+ localQuery: function(k) {
3117
+ return "(@" + k + " " + (e.inspect()) + ")";
3118
+ },
3119
+ fail: function(m) {
3120
+ return "(FAIL " + m + ")";
3121
+ },
3122
+ binop: function(op, l, r) {
3123
+ return "(`" + op + "` " + (l.inspect()) + " " + (r.inspect()) + ")";
3124
+ },
3125
+ extern: function(name, val) {
3126
+ return "(`" + name + "` " + (val.inspect()) + ")";
3127
+ },
3128
+ rescue: function(e, d) {
3129
+ return "(RESCUE " + (e.inspect()) + " " + (d.inspect()) + ")";
3130
+ },
3131
+ constant: function(v) {
3132
+ return '' + v;
3133
+ }
3134
+ });
3135
+ };
3136
+
3137
+ return IR;
3138
+
3139
+ })(Union);
3140
+
3141
+ Gibbon.compile = (function() {
3142
+ var compile;
3143
+ compile = function(semantic, input, context) {
3144
+ if (input == null) {
3145
+ input = IR.global();
3146
+ }
3147
+ if (context == null) {
3148
+ context = input;
3149
+ }
3150
+ return semantic.cases({
3151
+ definition: function(_, flow) {
3152
+ return compile(flow, input, context);
3153
+ },
3154
+ flow: function(_, head, tail) {
3155
+ if (tail) {
3156
+ return compile(head, compile(tail, input, context), context);
3157
+ } else {
3158
+ return compile(head, input, context);
3159
+ }
3160
+ },
3161
+ query: function(annotations) {
3162
+ return input.query(annotations);
3163
+ },
3164
+ localAccessor: function(key) {
3165
+ return input.localQuery(key);
3166
+ },
3167
+ func: function(name, args) {
3168
+ var arg, argTypes, compArgs;
3169
+ compArgs = (function() {
3170
+ var _i, _len, _results;
3171
+ _results = [];
3172
+ for (_i = 0, _len = args.length; _i < _len; _i++) {
3173
+ arg = args[_i];
3174
+ _results.push(compile(arg, context));
3175
+ }
3176
+ return _results;
3177
+ })();
3178
+ argTypes = (function() {
3179
+ var _i, _len, _results;
3180
+ _results = [];
3181
+ for (_i = 0, _len = args.length; _i < _len; _i++) {
3182
+ arg = args[_i];
3183
+ _results.push(arg.type);
3184
+ }
3185
+ return _results;
3186
+ })();
3187
+ return stdlib[name].compile(input, compArgs, this.type, argTypes);
3188
+ },
3189
+ literal: function(syntax) {
3190
+ return IR.constant(syntax.value);
3191
+ },
3192
+ list: function(elements) {
3193
+ var e;
3194
+ return IR.list((function() {
3195
+ var _i, _len, _results;
3196
+ _results = [];
3197
+ for (_i = 0, _len = elements.length; _i < _len; _i++) {
3198
+ e = elements[_i];
3199
+ _results.push(compile(e, context));
3200
+ }
3201
+ return _results;
3202
+ })());
3203
+ },
3204
+ pair: function(x, y) {
3205
+ return IR.pair(compile(x, context), compile(y, context));
3206
+ },
3207
+ block: function(body) {
3208
+ var arg;
3209
+ arg = IR.makeVariable('arg');
3210
+ return IR.block(arg.name, compile(body, arg, arg));
3211
+ },
3212
+ defaulted: function(body, alternative) {
3213
+ return IR.rescue(compile(body, context), compile(alternative, context));
3214
+ }
3215
+ });
3216
+ };
3217
+ return function(semantics) {
3218
+ var out;
3219
+ out = new Hash;
3220
+ semantics.each(function(k, v) {
3221
+ return out.set(k, compile(v));
3222
+ });
3223
+ return out;
3224
+ };
3225
+ })();
3226
+ // Generated by CoffeeScript 1.6.3
2747
3227
  var stdlib;
2748
3228
 
2749
3229
  stdlib = Gibbon.stdlib = (function() {
2750
- var allBooleans, anyBooleans, combine, iter, unit, vFalse, vTrue;
3230
+ var allBooleans, anyBooleans, combine, compEquals, iFalse, iTrue, iter, unit, vFalse, vTrue;
2751
3231
  unit = Promise.unit;
2752
3232
  iter = Promise.iter;
2753
3233
  combine = Promise.combine;
2754
3234
  vTrue = Value.boolean(true);
2755
3235
  vFalse = Value.boolean(false);
3236
+ iTrue = IR.constant(true);
3237
+ iFalse = IR.constant(false);
2756
3238
  anyBooleans = function(promises) {
2757
3239
  var out, step;
2758
3240
  step = function(cond, next) {
@@ -2781,6 +3263,35 @@ stdlib = Gibbon.stdlib = (function() {
2781
3263
  };
2782
3264
  return iter(promises, step, out);
2783
3265
  };
3266
+ compEquals = function(x, y, type) {
3267
+ var direct;
3268
+ direct = function() {
3269
+ return x.binop('===', y);
3270
+ };
3271
+ return type.cases({
3272
+ entity: direct,
3273
+ numeric: direct,
3274
+ string: direct,
3275
+ bool: direct,
3276
+ block: function() {
3277
+ return iFalse;
3278
+ },
3279
+ pair: function() {
3280
+ var eqx, eqy;
3281
+ eqx = compEquals(input.depair('first'), input.depair('second'), type.first);
3282
+ eqy = compEquals(input.depair('first'), input.depair('second'), type.first);
3283
+ return eqx.branch(eqy, iFalse);
3284
+ },
3285
+ list: function() {
3286
+ var eqEls, eqLength;
3287
+ eqLength = compEquals(x.len(), y.len(), Type.numeric());
3288
+ eqEls = x.zipList(y).foldList(iTrue, function(pair) {
3289
+ return compEquals(pair.depair('first'), pair.depair('second'), type.of);
3290
+ });
3291
+ return eqLength.branch(eqEls, iFalse);
3292
+ }
3293
+ });
3294
+ };
2784
3295
  return {
2785
3296
  "case": {
2786
3297
  type: parse.type('case [bool : %b] = % -> %b'),
@@ -2801,6 +3312,13 @@ stdlib = Gibbon.stdlib = (function() {
2801
3312
  };
2802
3313
  return iter(list.elements, step, out);
2803
3314
  });
3315
+ },
3316
+ compile: function(_, _arg) {
3317
+ var alts;
3318
+ alts = _arg[0];
3319
+ return alts.foldList(IR.fail('non-exhaustive cases'), function(el, next) {
3320
+ return el.depair('first').branch(el.depair('second'), next);
3321
+ });
2804
3322
  }
2805
3323
  },
2806
3324
  "case-eq": {
@@ -2826,12 +3344,36 @@ stdlib = Gibbon.stdlib = (function() {
2826
3344
  };
2827
3345
  return iter(list.elements, step, out);
2828
3346
  });
3347
+ },
3348
+ compile: function(input, _arg, _, _arg1) {
3349
+ var alts, argType, eqType;
3350
+ alts = _arg[0];
3351
+ argType = _arg1[0];
3352
+ eqType = argType.of.first;
3353
+ return alts.foldList(IR.fail('non-exhaustive cases'), function(el, next) {
3354
+ var first, second;
3355
+ first = el.depair('first');
3356
+ second = el.depair('second');
3357
+ return compEquals(input, first, eqType).branch(second, next);
3358
+ });
2829
3359
  }
2830
3360
  },
2831
3361
  "case-eq-default": {
2832
3362
  type: parse.type('case-eq-default %b [%a : %b] = %a -> %b'),
2833
3363
  impl: function(evalInput, evalDefault, evalList) {
2834
3364
  return stdlib['case-eq'].impl(evalInput, evalList).or(evalDefault);
3365
+ },
3366
+ compile: function(input, _arg, _, _arg1) {
3367
+ var alts, argType, default_, eqType;
3368
+ default_ = _arg[0], alts = _arg[1];
3369
+ argType = _arg1[0];
3370
+ eqType = argType.of.first;
3371
+ return alts.foldList(default_, function(el, next) {
3372
+ var first, second;
3373
+ first = el.depair('first');
3374
+ second = el.depair('second');
3375
+ return compEquals(input, first, eqType).branch(second, next);
3376
+ });
2835
3377
  }
2836
3378
  },
2837
3379
  "any-true?": {
@@ -2840,6 +3382,11 @@ stdlib = Gibbon.stdlib = (function() {
2840
3382
  return evalList.then(function(list) {
2841
3383
  return anyBooleans(list.elements);
2842
3384
  });
3385
+ },
3386
+ compile: function(list) {
3387
+ return list.foldList(iFalse, function(el, next) {
3388
+ return el.branch(iTrue, next);
3389
+ });
2843
3390
  }
2844
3391
  },
2845
3392
  "all-true?": {
@@ -2848,6 +3395,11 @@ stdlib = Gibbon.stdlib = (function() {
2848
3395
  return evalList.then(function(list) {
2849
3396
  return allBooleans(list.elements);
2850
3397
  });
3398
+ },
3399
+ compile: function(list) {
3400
+ return list.foldList(iTrue, function(el, next) {
3401
+ return el.branch(next, iFalse);
3402
+ });
2851
3403
  }
2852
3404
  },
2853
3405
  "any?": {
@@ -2868,6 +3420,11 @@ stdlib = Gibbon.stdlib = (function() {
2868
3420
  return _results;
2869
3421
  })());
2870
3422
  });
3423
+ },
3424
+ compile: function(list, block) {
3425
+ return list.foldList(iFalse, function(el, next) {
3426
+ return block.app(el).branch(iTrue, next);
3427
+ });
2871
3428
  }
2872
3429
  },
2873
3430
  "all?": {
@@ -2888,6 +3445,11 @@ stdlib = Gibbon.stdlib = (function() {
2888
3445
  return _results;
2889
3446
  })());
2890
3447
  });
3448
+ },
3449
+ compile: function(list, block) {
3450
+ return list.foldList(iTrue, function(el, next) {
3451
+ return block.app(el).branch(next, iFalse);
3452
+ });
2891
3453
  }
2892
3454
  },
2893
3455
  "include?": {
@@ -2911,6 +3473,13 @@ stdlib = Gibbon.stdlib = (function() {
2911
3473
  })();
2912
3474
  return anyBooleans(booleans);
2913
3475
  });
3476
+ },
3477
+ compile: function(list, needle, _, _arg) {
3478
+ var listType;
3479
+ listType = _arg[0];
3480
+ return list.foldList(iFalse, function(el, next) {
3481
+ return compEquals(el, needle, listType.of).branch(iTrue, next);
3482
+ });
2914
3483
  }
2915
3484
  },
2916
3485
  "empty?": {
@@ -2919,6 +3488,11 @@ stdlib = Gibbon.stdlib = (function() {
2919
3488
  return evalList.map(function(list) {
2920
3489
  return Value.boolean(list.length === 0);
2921
3490
  });
3491
+ },
3492
+ compile: function(list) {
3493
+ return list.foldList(iTrue, function() {
3494
+ return iFalse;
3495
+ });
2922
3496
  }
2923
3497
  },
2924
3498
  weight: {
@@ -2953,6 +3527,18 @@ stdlib = Gibbon.stdlib = (function() {
2953
3527
  });
2954
3528
  });
2955
3529
  });
3530
+ },
3531
+ compile: function(_, weights) {
3532
+ var ratio;
3533
+ return ratio = weights.foldList(IR.pair(IR.constant(0), IR.constant(0)), function(el, next) {
3534
+ var denominator, numerator, value, weight, weighted;
3535
+ value = el.depair('first');
3536
+ weight = el.depair('second');
3537
+ numerator = next.depair('first');
3538
+ denominator = next.depair('second');
3539
+ weighted = value.binop('*', weight);
3540
+ return IR.pair(numerator.binop('+', weighted), denominator.binop('+', weight));
3541
+ });
2956
3542
  }
2957
3543
  },
2958
3544
  filter: {
@@ -2985,6 +3571,11 @@ stdlib = Gibbon.stdlib = (function() {
2985
3571
  });
2986
3572
  });
2987
3573
  });
3574
+ },
3575
+ compile: function(input, block) {
3576
+ return input.filterList(function(el) {
3577
+ return block.app(el);
3578
+ });
2988
3579
  }
2989
3580
  },
2990
3581
  scale: {
@@ -3031,6 +3622,13 @@ stdlib = Gibbon.stdlib = (function() {
3031
3622
  })());
3032
3623
  });
3033
3624
  });
3625
+ },
3626
+ compile: function(list, _arg) {
3627
+ var block;
3628
+ block = _arg[0];
3629
+ return list.mapList(function(el) {
3630
+ return block.app(el);
3631
+ });
3034
3632
  }
3035
3633
  },
3036
3634
  count: {
@@ -3055,6 +3653,11 @@ stdlib = Gibbon.stdlib = (function() {
3055
3653
  return unit(Value.number(out));
3056
3654
  });
3057
3655
  });
3656
+ },
3657
+ compile: function(list) {
3658
+ return list.foldList(IR.constant(0), function(el, next) {
3659
+ return el.binop('+', next);
3660
+ });
3058
3661
  }
3059
3662
  },
3060
3663
  "case-sum": {
@@ -3062,20 +3665,22 @@ stdlib = Gibbon.stdlib = (function() {
3062
3665
  impl: function(_, evalList) {
3063
3666
  return evalList.then(function(list) {
3064
3667
  return combine(list.elements).then(function(elements) {
3065
- var el, numbers;
3668
+ var first, numbers, second;
3066
3669
  numbers = (function() {
3067
- var _i, _len, _results;
3670
+ var _i, _len, _ref, _results;
3068
3671
  _results = [];
3069
3672
  for (_i = 0, _len = elements.length; _i < _len; _i++) {
3070
- el = elements[_i];
3071
- _results.push(el.first.then(function(cond) {
3072
- if (!cond.value) {
3073
- return unit(0);
3074
- }
3075
- return el.second.map(function(v) {
3076
- return v.value;
3673
+ _ref = elements[_i], first = _ref.first, second = _ref.second;
3674
+ _results.push((function(first, second) {
3675
+ return first.then(function(cond) {
3676
+ if (!cond.value) {
3677
+ return unit(0);
3678
+ }
3679
+ return second.map(function(v) {
3680
+ return v.value;
3681
+ });
3077
3682
  });
3078
- }));
3683
+ })(first, second));
3079
3684
  }
3080
3685
  return _results;
3081
3686
  })();
@@ -3090,6 +3695,16 @@ stdlib = Gibbon.stdlib = (function() {
3090
3695
  });
3091
3696
  });
3092
3697
  });
3698
+ },
3699
+ compile: function(_, _arg) {
3700
+ var list;
3701
+ list = _arg[0];
3702
+ return list.foldList(IR.constant(0), function(el, next) {
3703
+ var cond, val;
3704
+ cond = el.depair('first');
3705
+ val = el.depair('second');
3706
+ return cond.branch(val.binop('+', next), next);
3707
+ });
3093
3708
  }
3094
3709
  },
3095
3710
  first: {
@@ -3098,6 +3713,9 @@ stdlib = Gibbon.stdlib = (function() {
3098
3713
  return list.then(function(val) {
3099
3714
  return val.elements[0];
3100
3715
  });
3716
+ },
3717
+ compile: function(list) {
3718
+ return list.delist(0);
3101
3719
  }
3102
3720
  },
3103
3721
  squish: {
@@ -3131,6 +3749,13 @@ stdlib = Gibbon.stdlib = (function() {
3131
3749
  })());
3132
3750
  });
3133
3751
  });
3752
+ },
3753
+ compile: function(list) {
3754
+ return list.filterList(function(el) {
3755
+ return IR.rescue(el.seq('v', function(_) {
3756
+ return iTrue;
3757
+ }), iFalse);
3758
+ });
3134
3759
  }
3135
3760
  },
3136
3761
  add: {
@@ -3141,6 +3766,11 @@ stdlib = Gibbon.stdlib = (function() {
3141
3766
  lhs = _arg[0], rhs = _arg[1];
3142
3767
  return Value.number(lhs.value + rhs.value);
3143
3768
  });
3769
+ },
3770
+ compile: function(input, _arg) {
3771
+ var num;
3772
+ num = _arg[0];
3773
+ return input.binop('+', num);
3144
3774
  }
3145
3775
  },
3146
3776
  sub: {
@@ -3151,18 +3781,29 @@ stdlib = Gibbon.stdlib = (function() {
3151
3781
  lhs = _arg[0], rhs = _arg[1];
3152
3782
  return Value.number(lhs.value - rhs.value);
3153
3783
  });
3784
+ },
3785
+ compile: function(input, _arg) {
3786
+ var num;
3787
+ num = _arg[0];
3788
+ return input.binop('+', num.extern('-'));
3154
3789
  }
3155
3790
  },
3156
3791
  id: {
3157
3792
  type: parse.type('id = %a -> %a'),
3158
3793
  impl: function(x) {
3159
3794
  return x;
3795
+ },
3796
+ compile: function(input) {
3797
+ return input;
3160
3798
  }
3161
3799
  },
3162
3800
  "else": {
3163
3801
  type: parse.type('else = % -> bool'),
3164
3802
  impl: function(_) {
3165
3803
  return unit(vTrue);
3804
+ },
3805
+ compile: function(_) {
3806
+ return iTrue;
3166
3807
  }
3167
3808
  },
3168
3809
  not: {
@@ -3171,6 +3812,9 @@ stdlib = Gibbon.stdlib = (function() {
3171
3812
  return evalBool.map(function(bool) {
3172
3813
  return Value.boolean(!bool.value);
3173
3814
  });
3815
+ },
3816
+ compile: function(input) {
3817
+ return input.extern('!');
3174
3818
  }
3175
3819
  },
3176
3820
  gt: {
@@ -3181,6 +3825,11 @@ stdlib = Gibbon.stdlib = (function() {
3181
3825
  lhs = _arg[0], rhs = _arg[1];
3182
3826
  return Value.boolean(lhs.value > rhs.value);
3183
3827
  });
3828
+ },
3829
+ compile: function(input, _arg) {
3830
+ var num;
3831
+ num = _arg[0];
3832
+ return input.binop('>', num);
3184
3833
  }
3185
3834
  },
3186
3835
  lt: {
@@ -3191,6 +3840,11 @@ stdlib = Gibbon.stdlib = (function() {
3191
3840
  lhs = _arg[0], rhs = _arg[1];
3192
3841
  return Value.boolean(lhs.value < rhs.value);
3193
3842
  });
3843
+ },
3844
+ compile: function(input, _arg) {
3845
+ var num;
3846
+ num = _arg[0];
3847
+ return input.binop('<', num);
3194
3848
  }
3195
3849
  },
3196
3850
  eq: {
@@ -3201,6 +3855,12 @@ stdlib = Gibbon.stdlib = (function() {
3201
3855
  lhs = _arg[0], rhs = _arg[1];
3202
3856
  return lhs.equals(rhs);
3203
3857
  });
3858
+ },
3859
+ compile: function(input, _arg, _, _arg1) {
3860
+ var obj, objType;
3861
+ obj = _arg[0];
3862
+ objType = _arg1[0];
3863
+ return compEquals(input, obj, objType);
3204
3864
  }
3205
3865
  }
3206
3866
  };
@@ -3260,11 +3920,12 @@ Gibbon.jsonConsumer = (function() {
3260
3920
  };
3261
3921
  return {
3262
3922
  analyzeQuery: function(id, query, t, callback) {
3923
+ debugger;
3263
3924
  switch (query.type) {
3264
3925
  case 'access':
3265
- return getType(id, query.name, t, callback);
3926
+ return getType(id, query.args[0], t, callback);
3266
3927
  case 'on':
3267
- return analyzeList(id, query.name, t, callback);
3928
+ return analyzeList(id, query.args[0], t, callback);
3268
3929
  default:
3269
3930
  return callback(new Error("unknown query `" + query.type + "'"));
3270
3931
  }