1 |
|
2 | (function() {
|
3 | var $debId, BlockEff, BlockPure, Builder, CAPTURE_SET, Cont, Eff, Empty, ExprEff, ExprPure, Par, Postfix, PurePrefix, VarUsage, assert, config, envBefore, envMerge, envPush, envThread, kit, root,
|
4 | extend = 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; },
|
5 | hasProp = {}.hasOwnProperty,
|
6 | slice = [].slice;
|
7 |
|
8 | kit = require("./kit");
|
9 |
|
10 | config = require("./config");
|
11 |
|
12 | root = module.exports = {};
|
13 |
|
14 | assert = require("assert");
|
15 |
|
16 | CAPTURE_SET = true;
|
17 |
|
18 | VarUsage = (function() {
|
19 | function VarUsage() {
|
20 | this.sets = {};
|
21 | this.upds = {};
|
22 | this.mods = {};
|
23 | this.uses = {};
|
24 | this.after = {};
|
25 | this.before = {};
|
26 | this.vars = {};
|
27 | }
|
28 |
|
29 | VarUsage.prototype.init = function() {
|
30 | var deps, i, ref, v, w;
|
31 | deps = {};
|
32 | ref = this.vars;
|
33 | for (i in ref) {
|
34 | v = ref[i];
|
35 | if (!(v && !this.refs[i])) {
|
36 | continue;
|
37 | }
|
38 | w = deps[i] = {};
|
39 | if (!this.after[i]) {
|
40 | w.fin = true;
|
41 | }
|
42 | if (this.uses[i]) {
|
43 | w.use = true;
|
44 | }
|
45 | if (this.sets[i]) {
|
46 | if (this.before[i]) {
|
47 | w.mod = true;
|
48 | } else {
|
49 | w.set = true;
|
50 | }
|
51 | }
|
52 | if (this.upds[i]) {
|
53 | w.use = w.mod = true;
|
54 | }
|
55 | }
|
56 | return deps;
|
57 | };
|
58 |
|
59 | VarUsage.prototype.setBefore = function(known) {
|
60 | var i, results;
|
61 | results = [];
|
62 | for (i in known) {
|
63 | this.before[i] = true;
|
64 | if (this.sets[i]) {
|
65 | this.sets[i] = false;
|
66 | results.push(this.upds[i] = true);
|
67 | } else {
|
68 | results.push(void 0);
|
69 | }
|
70 | }
|
71 | return results;
|
72 | };
|
73 |
|
74 | VarUsage.prototype.addAssign = function(name) {
|
75 | if (this.uses[name]) {
|
76 | this.upds[name] = true;
|
77 | } else {
|
78 | this.sets[name] = true;
|
79 | }
|
80 | this.mods[name] = true;
|
81 | this.mod = true;
|
82 | return this;
|
83 | };
|
84 |
|
85 | VarUsage.prototype.addUpd = function(name) {
|
86 | this.upds[name] = true;
|
87 | this.uses[name] = true;
|
88 | this.mods[name] = true;
|
89 | this.mod = true;
|
90 | this.upd = true;
|
91 | return this;
|
92 | };
|
93 |
|
94 | VarUsage.prototype.skipMods = function() {
|
95 | this._skipMods = true;
|
96 | return this;
|
97 | };
|
98 |
|
99 | VarUsage.prototype.addInner = function(d) {
|
100 | var i, ref, ref1, v;
|
101 | if (d.mod) {
|
102 | this.mod = true;
|
103 | }
|
104 | if (d.upd) {
|
105 | this.upd = true;
|
106 | }
|
107 | if (!this._skipMods) {
|
108 | ref = d.mods;
|
109 | for (i in ref) {
|
110 | v = ref[i];
|
111 | if (v) {
|
112 | this.mods[i] = true;
|
113 | }
|
114 | }
|
115 | }
|
116 | ref1 = d.uses;
|
117 | for (i in ref1) {
|
118 | v = ref1[i];
|
119 | if (v) {
|
120 | this.uses[i] = true;
|
121 | }
|
122 | }
|
123 | };
|
124 |
|
125 | VarUsage.prototype.addInnerDeep = function(d) {
|
126 | var i, ref, ref1, ref2, v;
|
127 | this.addInner(d);
|
128 | if (d.mod) {
|
129 | this.mod = true;
|
130 | }
|
131 | if (d.upd) {
|
132 | this.upd = true;
|
133 | }
|
134 | ref = d.upds;
|
135 | for (i in ref) {
|
136 | v = ref[i];
|
137 | if (v) {
|
138 | this.upds[i] = true;
|
139 | }
|
140 | }
|
141 | ref1 = d.uses;
|
142 | for (i in ref1) {
|
143 | v = ref1[i];
|
144 | if (v) {
|
145 | this.uses[i] = true;
|
146 | }
|
147 | }
|
148 | ref2 = d.sets;
|
149 | for (i in ref2) {
|
150 | v = ref2[i];
|
151 | if (v) {
|
152 | if (this.uses[i]) {
|
153 | this.upds[i] = true;
|
154 | this.upd = true;
|
155 | } else {
|
156 | this.sets[i] = true;
|
157 | }
|
158 | }
|
159 | }
|
160 | };
|
161 |
|
162 | VarUsage.prototype.threadOutMap = function(res) {
|
163 | var i, ref, v;
|
164 | if (res == null) {
|
165 | res = {};
|
166 | }
|
167 | ref = this.mods;
|
168 | for (i in ref) {
|
169 | v = ref[i];
|
170 | if (!(v)) {
|
171 | continue;
|
172 | }
|
173 | if (!this.after[i]) {
|
174 | continue;
|
175 | }
|
176 | if (!(this.vars[i] && !this.refs[i])) {
|
177 | continue;
|
178 | }
|
179 | res[i] = true;
|
180 | }
|
181 | return res;
|
182 | };
|
183 |
|
184 | return VarUsage;
|
185 |
|
186 | })();
|
187 |
|
188 | envPush = function(dst, src) {
|
189 | var i, v, w;
|
190 | for (i in src) {
|
191 | v = src[i];
|
192 | w = dst[i] != null ? dst[i] : dst[i] = {};
|
193 | if (v.mod || v.set && w.use) {
|
194 | w.mod = true;
|
195 | }
|
196 | if (v.use) {
|
197 | w.use = true;
|
198 | }
|
199 | if (v.set && !w.mod) {
|
200 | w.set = true;
|
201 | }
|
202 | if (v.fin) {
|
203 | w.fin = true;
|
204 | }
|
205 | if (v.thread && !v.fin) {
|
206 | w.thread = true;
|
207 | }
|
208 | }
|
209 | return dst;
|
210 | };
|
211 |
|
212 | envMerge = function(dst, src) {
|
213 | var i, v, w;
|
214 | for (i in src) {
|
215 | v = src[i];
|
216 | w = dst[i] != null ? dst[i] : dst[i] = {};
|
217 | if (v.use) {
|
218 | w.use = true;
|
219 | }
|
220 | if (v.mod) {
|
221 | w.mod = true;
|
222 | }
|
223 | if (v.fin) {
|
224 | w.fin = true;
|
225 | }
|
226 | if (v.thread && !v.fin) {
|
227 | w.thread = true;
|
228 | }
|
229 | }
|
230 | return dst;
|
231 | };
|
232 |
|
233 | envThread = function(env) {
|
234 | var i, v;
|
235 | return ((function() {
|
236 | var results;
|
237 | results = [];
|
238 | for (i in env) {
|
239 | v = env[i];
|
240 | if (v.thread && !v.fin) {
|
241 | results.push(i);
|
242 | }
|
243 | }
|
244 | return results;
|
245 | })()).sort().map(kit.id);
|
246 | };
|
247 |
|
248 | envBefore = function(f, s) {
|
249 | var i, v, w;
|
250 | for (i in s) {
|
251 | v = s[i];
|
252 | if ((w = f[i]) && w.use) {
|
253 | v.use = true;
|
254 | }
|
255 | }
|
256 | };
|
257 |
|
258 | root.VarUsage = VarUsage;
|
259 |
|
260 | $debId = 0;
|
261 |
|
262 | Builder = (function() {
|
263 | function Builder() {
|
264 | this.env = {};
|
265 | this.opts = {};
|
266 | this.$debId = $debId++;
|
267 | }
|
268 |
|
269 | Builder.prototype.setOpts = function(opts) {
|
270 | var i, o, v;
|
271 | o = this.opts;
|
272 | if (o != null) {
|
273 | for (i in opts) {
|
274 | v = opts[i];
|
275 | if (o[i] == null) {
|
276 | o[i] = v;
|
277 | }
|
278 | }
|
279 | } else {
|
280 | this.opts = opts;
|
281 | }
|
282 | return this;
|
283 | };
|
284 |
|
285 | Builder.prototype.needCoerce = function(v) {
|
286 | if (v == null) {
|
287 | v = true;
|
288 | }
|
289 | this._needCoerce = v;
|
290 | return this;
|
291 | };
|
292 |
|
293 | Builder.prototype.coerceObj = function() {
|
294 | if (!this._needCoerce) {
|
295 | return this;
|
296 | }
|
297 | return this.toExprBuilder()._coerceObj();
|
298 | };
|
299 |
|
300 | Builder.prototype._coerceObj = function() {
|
301 | return this;
|
302 | };
|
303 |
|
304 | Builder.prototype.noCoerceTry = function() {
|
305 | this._noCoerceTry = true;
|
306 | return this;
|
307 | };
|
308 |
|
309 | Builder.prototype.coerceTry = function() {
|
310 | var b;
|
311 | if (this._noCoerceTry && this.opts.coerce === "all") {
|
312 | return this;
|
313 | }
|
314 | b = this.capture().toBlockBuilder();
|
315 | return root.expr(kit.coerceThunk(b.block), b.eff).morph(b).noCoerceTry();
|
316 | };
|
317 |
|
318 | Builder.prototype.setBindVar = function(bv) {
|
319 | if (bv === false) {
|
320 | delete this._bindvar;
|
321 | return this;
|
322 | }
|
323 | if (bv != null) {
|
324 | this._bindVar = bv;
|
325 | }
|
326 | return this;
|
327 | };
|
328 |
|
329 | Builder.prototype.getBindVar = function() {
|
330 | return this._bindVar;
|
331 | };
|
332 |
|
333 | Builder.prototype.noCapture = function() {
|
334 | this._captured = true;
|
335 | return this;
|
336 | };
|
337 |
|
338 | Builder.prototype.capture = function() {
|
339 | if (this._captured) {
|
340 | return this;
|
341 | }
|
342 | this._captured = true;
|
343 | return this._capture();
|
344 | };
|
345 |
|
346 | Builder.prototype._capture = function() {
|
347 | return this;
|
348 | };
|
349 |
|
350 | Builder.prototype.morph = function(other, env) {
|
351 | var brk;
|
352 | this.setBindVar(other.getBindVar());
|
353 | assert.ok(other.opts);
|
354 | this.opts = other.opts;
|
355 | if (this._captured == null) {
|
356 | this._captured = other._captured;
|
357 | }
|
358 | if (env == null) {
|
359 | env = other.env;
|
360 | }
|
361 | if (env) {
|
362 | envPush(this.env, env);
|
363 | }
|
364 | brk = other.isBrk();
|
365 | if (brk != null) {
|
366 | this.setBrk(brk);
|
367 | }
|
368 | if (this._needCoerce == null) {
|
369 | this._needCoerce = other._needCoerce;
|
370 | }
|
371 | return this;
|
372 | };
|
373 |
|
374 | Builder.prototype.toBlockBuilder = function(eff) {
|
375 | var block, n;
|
376 | n = this.toExprBuilder(eff);
|
377 | block = [kit.ret(n.expr)];
|
378 | return (n.eff ? root.blockEff(block) : root.pureBlock(block)).morph(n);
|
379 | };
|
380 |
|
381 | Builder.prototype.toExprBuilder = function(eff) {
|
382 | var expr, n;
|
383 | assert.ok(this.toBlockBuilder !== Builder.prototype.toBlockBuilder);
|
384 | n = this.toBlockBuilder(eff);
|
385 | expr = kit.call(kit.fun([], n.block), []);
|
386 | return (n.eff ? root.exprEff(expr) : root.exprPure(expr)).morph(n);
|
387 | };
|
388 |
|
389 | Builder.prototype.toBlock = function(eff) {
|
390 | return this.capture().toBlockBuilder(eff).block;
|
391 | };
|
392 |
|
393 | Builder.prototype.toExpr = function(eff) {
|
394 | return this.capture().toExprBuilder(eff).expr;
|
395 | };
|
396 |
|
397 | Builder.prototype.pushEnv = function(env) {
|
398 | envPush(this.env, env);
|
399 | return this;
|
400 | };
|
401 |
|
402 | Builder.prototype.mergeEnv = function(env) {
|
403 | envPush(this.env, env.init());
|
404 | return this;
|
405 | };
|
406 |
|
407 | Builder.prototype.bindTo = function() {
|
408 | var base, block, bv, capt, captVars, closVars, cont, eff, env, exprs, i, j, k, l, len, len1, lhs, ref, ref1, ref2, thisCapt, threadIn, threadOut, threadVars, tvars, v, vars, w, x;
|
409 | vars = [];
|
410 | exprs = [];
|
411 | eff = false;
|
412 | assert.ok(arguments.length);
|
413 | for (k = 0, len = arguments.length; k < len; k++) {
|
414 | i = arguments[k];
|
415 | eff || (eff = i.eff);
|
416 | }
|
417 | env = {};
|
418 | threadVars = [];
|
419 | captVars = {};
|
420 | threadIn = {};
|
421 | lhs = {};
|
422 | for (x = l = 0, len1 = arguments.length; l < len1; x = ++l) {
|
423 | i = arguments[x];
|
424 | capt = i;
|
425 | if (arguments.length === 1) {
|
426 | capt = capt.coerceObj();
|
427 | }
|
428 | capt = capt.capture().toExprBuilder(eff);
|
429 | tvars = [];
|
430 | ref = capt.env;
|
431 | for (j in ref) {
|
432 | v = ref[j];
|
433 | w = env[j] != null ? env[j] : env[j] = {};
|
434 | if (v.use) {
|
435 | w.use = true;
|
436 | }
|
437 | if (v.mod || w.use && v.set) {
|
438 | w.mod = true;
|
439 | }
|
440 | if (v.set && !w.mod) {
|
441 | w.set = true;
|
442 | }
|
443 | if (v.fin) {
|
444 | w.fin = true;
|
445 | }
|
446 | if (v.thread && !v.fin) {
|
447 | w.thread = true;
|
448 | threadIn[j] = true;
|
449 | tvars.push(j);
|
450 | }
|
451 | }
|
452 | if (tvars.length !== 0) {
|
453 | assert.ok(arguments.length === 1);
|
454 | }
|
455 | threadVars.push.apply(threadVars, tvars);
|
456 | exprs.push(capt.expr);
|
457 | bv = capt.getBindVar();
|
458 | if (bv != null) {
|
459 | vars.push(bv);
|
460 | }
|
461 | vars.push.apply(vars, tvars.sort().map(kit.id));
|
462 | }
|
463 | threadOut = {};
|
464 | for (i in env) {
|
465 | v = env[i];
|
466 | w = (ref1 = this.env[i]) != null ? ref1 : {};
|
467 | if (!v.fin && (v.thread || v.mod || w.mod)) {
|
468 | threadOut[i] = true;
|
469 | }
|
470 | }
|
471 | thisCapt = this.addThread(threadOut).capture();
|
472 | block = thisCapt.toBlock();
|
473 | if (this.isBrk()) {
|
474 | thisCapt.setBrk(true);
|
475 | }
|
476 | closVars = [];
|
477 | ref2 = thisCapt.env;
|
478 | for (i in ref2) {
|
479 | v = ref2[i];
|
480 | if (!v.mod) {
|
481 | continue;
|
482 | }
|
483 | v.mod = false;
|
484 | if (!threadIn[i]) {
|
485 | closVars.push(i);
|
486 | }
|
487 | }
|
488 | closVars = closVars.sort().map(kit.id);
|
489 | if (closVars.length) {
|
490 | block = [kit.ret(kit.call(kit.fun(closVars, block), closVars))];
|
491 | }
|
492 | for (i in env) {
|
493 | v = env[i];
|
494 | if (!v.mod) {
|
495 | continue;
|
496 | }
|
497 | w = (base = thisCapt.env)[i] != null ? base[i] : base[i] = {
|
498 | fin: true
|
499 | };
|
500 | w.use = w.mod = true;
|
501 | }
|
502 | cont = kit.fun(vars, block);
|
503 | if (exprs.length === 0) {
|
504 | return thisCapt;
|
505 | }
|
506 | return root.exprEffCaptured(thisCapt.mkMap(exprs, cont)).needCoerce(false).morph(thisCapt);
|
507 | };
|
508 |
|
509 | Builder.prototype.setBrk = function(brk1) {
|
510 | this.brk = brk1;
|
511 | return this;
|
512 | };
|
513 |
|
514 | Builder.prototype.isBrk = function() {
|
515 | return this.brk;
|
516 | };
|
517 |
|
518 | Builder.prototype.addThread = function(thread) {
|
519 | var any, base, base1, env, i, ref, some, v, w;
|
520 | if (this.isBrk()) {
|
521 | for (i in thread) {
|
522 | v = thread[i];
|
523 | (w = (base = this.env)[i] != null ? base[i] : base[i] = {
|
524 | fin: true
|
525 | }).thread = true;
|
526 | }
|
527 | this;
|
528 | }
|
529 | if (!this._captured) {
|
530 | for (i in thread) {
|
531 | v = thread[i];
|
532 | if (v) {
|
533 | (w = (base1 = this.env)[i] != null ? base1[i] : base1[i] = {
|
534 | fin: true
|
535 | }).thread = true;
|
536 | }
|
537 | }
|
538 | return this;
|
539 | }
|
540 | env = {};
|
541 | if (thread != null) {
|
542 | for (i in thread) {
|
543 | v = thread[i];
|
544 | if (!(v)) {
|
545 | continue;
|
546 | }
|
547 | w = this.env[i];
|
548 | if ((w != null) && w.fin) {
|
549 | continue;
|
550 | }
|
551 | (env[i] = {}).thread = true;
|
552 | if ((w != null) && !w.thread) {
|
553 | any = true;
|
554 | }
|
555 | }
|
556 | ref = this.env;
|
557 | for (i in ref) {
|
558 | v = ref[i];
|
559 | if (!(!v.fin)) {
|
560 | continue;
|
561 | }
|
562 | w = env[i] != null ? env[i] : env[i] = {};
|
563 | if (v.thread && !thread[i]) {
|
564 | some = true;
|
565 | w.thread = true;
|
566 | }
|
567 | }
|
568 | }
|
569 | if (!any) {
|
570 | return this;
|
571 | }
|
572 | return root.purePostfix(this, root.pure([]).pushEnv(env)).morph(this);
|
573 | };
|
574 |
|
575 | return Builder;
|
576 |
|
577 | })();
|
578 |
|
579 | BlockPure = (function(superClass) {
|
580 | extend(BlockPure, superClass);
|
581 |
|
582 | function BlockPure(block1) {
|
583 | this.block = block1;
|
584 | BlockPure.__super__.constructor.call(this);
|
585 | }
|
586 |
|
587 | BlockPure.prototype.mkMap = function(args, fun) {
|
588 | return kit.mapply(args, fun);
|
589 | };
|
590 |
|
591 | BlockPure.prototype.pureBlock = function() {
|
592 | return this.block;
|
593 | };
|
594 |
|
595 | BlockPure.prototype.toPrefix = function() {
|
596 | return this;
|
597 | };
|
598 |
|
599 | BlockPure.prototype.toBlockBuilder = function(eff) {
|
600 | var block, expr, nocoerce;
|
601 | block = this.block;
|
602 | expr = this.ret;
|
603 | nocoerce = this.opts.coerce === "none";
|
604 | if (expr != null) {
|
605 | if (eff && nocoerce) {
|
606 | expr = kit.pure(expr);
|
607 | }
|
608 | block.push(kit.ret(expr));
|
609 | }
|
610 | if (!eff) {
|
611 | return this.needCoerce(!nocoerce);
|
612 | }
|
613 | if (!expr) {
|
614 | if (nocoerce) {
|
615 | block.push(kit.ret(kit.pure()));
|
616 | }
|
617 | }
|
618 | return root.blockEff(block).morph(this).needCoerce(!nocoerce);
|
619 | };
|
620 |
|
621 | BlockPure.prototype.prepend = function(other) {
|
622 | return (this.opts.bindAssoc === "left" ? root.postfixLeft(other, this) : root.purePostfix(other, this)).setOpts(this.opts);
|
623 | };
|
624 |
|
625 | BlockPure.prototype._append = function(other) {
|
626 | var ref;
|
627 | if (other.pureBlock != null) {
|
628 | (ref = this.block).push.apply(ref, other.pureBlock());
|
629 | envPush(this.env, other.env);
|
630 | this.setBindVar(other._bindVar);
|
631 | return this;
|
632 | }
|
633 | return root.purePrefix(this, other).setOpts(this.opts);
|
634 | };
|
635 |
|
636 | BlockPure.prototype.append = function(other) {
|
637 | return this._append(other);
|
638 | };
|
639 |
|
640 | BlockPure.prototype.coerceTry = function() {
|
641 | var w;
|
642 | w = this.opts.coerce;
|
643 | if (w !== "all") {
|
644 | return this;
|
645 | }
|
646 | return root.exprEff(kit.coerceThunk(this.block)).morph(this);
|
647 | };
|
648 |
|
649 | BlockPure.prototype._capture = function() {
|
650 | var ov;
|
651 | ov = envThread(this.env);
|
652 | if (ov.length) {
|
653 | assert.ok(!this.ret);
|
654 | this.ret = ov.length === 1 ? ov[0] : kit.arr(ov);
|
655 | }
|
656 | return this;
|
657 | };
|
658 |
|
659 | return BlockPure;
|
660 |
|
661 | })(Builder);
|
662 |
|
663 | root.pure = function(block) {
|
664 | return new BlockPure(block);
|
665 | };
|
666 |
|
667 | Empty = (function(superClass) {
|
668 | extend(Empty, superClass);
|
669 |
|
670 | function Empty() {
|
671 | Empty.__super__.constructor.call(this, []);
|
672 | }
|
673 |
|
674 | Empty.prototype.append = function(other) {
|
675 | return other;
|
676 | };
|
677 |
|
678 | Empty.prototype.prepend = function(other) {
|
679 | return other;
|
680 | };
|
681 |
|
682 | return Empty;
|
683 |
|
684 | })(BlockPure);
|
685 |
|
686 | root.empty = function() {
|
687 | return new Empty();
|
688 | };
|
689 |
|
690 | Eff = (function(superClass) {
|
691 | extend(Eff, superClass);
|
692 |
|
693 | function Eff() {
|
694 | Eff.__super__.constructor.call(this);
|
695 | }
|
696 |
|
697 | Eff.prototype.eff = true;
|
698 |
|
699 | Eff.prototype.mkMap = function(args, fun) {
|
700 | return kit.mbind(args, fun);
|
701 | };
|
702 |
|
703 | Eff.prototype.append = function(other) {
|
704 | var res;
|
705 | envBefore(this.env, other.env);
|
706 | if (other.prepend != null) {
|
707 | res = other.prepend(this);
|
708 | if (res != null) {
|
709 | return res;
|
710 | }
|
711 | }
|
712 | return this._append(other);
|
713 | };
|
714 |
|
715 | Eff.prototype._append = function(other) {
|
716 | if (!this._assocDone && this.opts.bindAssoc === "left" && other.eff) {
|
717 | this._assocDone = true;
|
718 | return root.postfixLeft(this, other).setOpts(other.opts);
|
719 | }
|
720 | return other.bindTo(this);
|
721 | };
|
722 |
|
723 | Eff.prototype._capture = function() {
|
724 | var fn, ov, val;
|
725 | ov = envThread(this.env);
|
726 | if (ov.length) {
|
727 | if (this.getBindVar() != null) {
|
728 | fn = "munshiftTo";
|
729 | val = kit.arr(ov);
|
730 | } else {
|
731 | fn = "mconst";
|
732 | val = ov.length === 1 ? ov[0] : kit.arr(ov);
|
733 | }
|
734 | return root.exprEff(kit.call(kit.mem(this.coerceObj().toExpr(), kit.id(fn)), [val])).morph(this).noCapture();
|
735 | }
|
736 | return this;
|
737 | };
|
738 |
|
739 | return Eff;
|
740 |
|
741 | })(Builder);
|
742 |
|
743 | Cont = (function(superClass) {
|
744 | extend(Cont, superClass);
|
745 |
|
746 | function Cont(inner1, cont1, isFinal) {
|
747 | this.inner = inner1;
|
748 | this.cont = cont1;
|
749 | Cont.__super__.constructor.call(this);
|
750 | if (this.isFinal == null) {
|
751 | this.isFinal = isFinal;
|
752 | }
|
753 | }
|
754 |
|
755 | Cont.prototype.addThread = function(thread) {
|
756 | this.cont = this.cont.addThread(thread);
|
757 | return this;
|
758 | };
|
759 |
|
760 | Cont.prototype.setBindVar = function(bv) {
|
761 | this.cont.setBindVar(bv);
|
762 | return this;
|
763 | };
|
764 |
|
765 | Cont.prototype.setBrk = function(bv) {
|
766 | this.cont.setBrk(bv);
|
767 | return this;
|
768 | };
|
769 |
|
770 | Cont.prototype.isBrk = function() {
|
771 | return this.cont.isBrk();
|
772 | };
|
773 |
|
774 | Cont.prototype.result = function() {
|
775 | return this.inner._append(this.cont).morph(this);
|
776 | };
|
777 |
|
778 | Cont.prototype.append = function(other) {
|
779 | this.cont = this.cont.append(other);
|
780 | if (this.isFinal()) {
|
781 | return this.result();
|
782 | } else {
|
783 | return this;
|
784 | }
|
785 | };
|
786 |
|
787 | Cont.prototype.toExprBuilder = function(eff) {
|
788 | return this.result().toExprBuilder(eff);
|
789 | };
|
790 |
|
791 | Cont.prototype.toBlockBuilder = function(eff) {
|
792 | return this.result().toBlockBuilder(eff);
|
793 | };
|
794 |
|
795 | Cont.prototype.capture = function() {
|
796 | return this.result();
|
797 | };
|
798 |
|
799 | return Cont;
|
800 |
|
801 | })(Eff);
|
802 |
|
803 | root.cont = function(inner, cont, isFinal) {
|
804 | return new Cont(inner, cont, isFinal);
|
805 | };
|
806 |
|
807 | Postfix = (function(superClass) {
|
808 | extend(Postfix, superClass);
|
809 |
|
810 | function Postfix(inner, cont, left) {
|
811 | this.left = left;
|
812 | Postfix.__super__.constructor.call(this, inner, cont);
|
813 | }
|
814 |
|
815 | Postfix.prototype.isFinal = function() {
|
816 | return !this.left && this.cont.eff;
|
817 | };
|
818 |
|
819 | return Postfix;
|
820 |
|
821 | })(Cont);
|
822 |
|
823 | root.purePostfix = function(inner, cont) {
|
824 | return new Postfix(inner, cont);
|
825 | };
|
826 |
|
827 | root.postfixLeft = function(inner, cont) {
|
828 | return new Postfix(inner, cont, true);
|
829 | };
|
830 |
|
831 | Par = (function(superClass) {
|
832 | extend(Par, superClass);
|
833 |
|
834 | function Par(args1, cont1) {
|
835 | this.args = args1;
|
836 | this.cont = cont1;
|
837 | Par.__super__.constructor.call(this);
|
838 | }
|
839 |
|
840 | Par.prototype.setBindVar = function(bv) {
|
841 | this.cont.setBindVar(bv);
|
842 | return this;
|
843 | };
|
844 |
|
845 | Par.prototype.addThread = function(thread) {
|
846 | if (this.isBrk()) {
|
847 | return this;
|
848 | }
|
849 | this.cont = this.cont.addThread(thread);
|
850 | return this;
|
851 | };
|
852 |
|
853 | Par.prototype.setBrk = function(bv) {
|
854 | this.cont.setBrk(bv);
|
855 | return this;
|
856 | };
|
857 |
|
858 | Par.prototype.isBrk = function() {
|
859 | return this.cont.isBrk();
|
860 | };
|
861 |
|
862 | Par.prototype.append = function(other) {
|
863 | if (this.cont.eff) {
|
864 | return Par.__super__.append.call(this, other);
|
865 | }
|
866 | this.cont = this.cont.append(other);
|
867 | return this;
|
868 | };
|
869 |
|
870 | Par.prototype.getDepVars = function() {
|
871 | return this.vars;
|
872 | };
|
873 |
|
874 | Par.prototype.toExprBuilder = function() {
|
875 | var ref;
|
876 | return (ref = this.cont).bindTo.apply(ref, this.args).morph(this).toExprBuilder();
|
877 | };
|
878 |
|
879 | Par.prototype.capture = function() {
|
880 | return this.toExprBuilder();
|
881 | };
|
882 |
|
883 | return Par;
|
884 |
|
885 | })(Eff);
|
886 |
|
887 | root.par = function(vars, args, cont) {
|
888 | return new Par(vars, args, cont);
|
889 | };
|
890 |
|
891 | PurePrefix = (function(superClass) {
|
892 | extend(PurePrefix, superClass);
|
893 |
|
894 | function PurePrefix(prefix1, cont1) {
|
895 | this.prefix = prefix1;
|
896 | this.cont = cont1;
|
897 | PurePrefix.__super__.constructor.call(this);
|
898 | assert.ok(this.cont.eff);
|
899 | assert.ok(!this.prefix.eff);
|
900 | return;
|
901 | }
|
902 |
|
903 | PurePrefix.prototype.addThread = function(thread) {
|
904 | if (this.isBrk()) {
|
905 | return this;
|
906 | }
|
907 | if (this.last != null) {
|
908 | this.last = this.last.addThread(thread);
|
909 | } else {
|
910 | this.cont = this.cont.addThread(thread);
|
911 | }
|
912 | return this;
|
913 | };
|
914 |
|
915 | PurePrefix.prototype.setBindVar = function(bv) {
|
916 | var ref;
|
917 | ((ref = this.last) != null ? ref : this.cont).setBindVar(bv);
|
918 | return this;
|
919 | };
|
920 |
|
921 | PurePrefix.prototype.setBrk = function(bv) {
|
922 | var ref;
|
923 | ((ref = this.last) != null ? ref : this.cont).setBrk(bv);
|
924 | return this;
|
925 | };
|
926 |
|
927 | PurePrefix.prototype.isBrk = function() {
|
928 | var ref;
|
929 | return ((ref = this.last) != null ? ref : this.cont).isBrk();
|
930 | };
|
931 |
|
932 | PurePrefix.prototype.prepend = function(other) {
|
933 | if (!other.eff) {
|
934 | this.prefix = other.append(this.prefix);
|
935 | return this;
|
936 | }
|
937 | return this.bindTo(other);
|
938 | };
|
939 |
|
940 | PurePrefix.prototype.append = function(other) {
|
941 | if (this.last) {
|
942 | this.cont = this.cont.append(this.last);
|
943 | }
|
944 | this.last = other;
|
945 | return this;
|
946 | };
|
947 |
|
948 | PurePrefix.prototype.toBlockBuilder = function() {
|
949 | return this.capture();
|
950 | };
|
951 |
|
952 | PurePrefix.prototype.capture = function() {
|
953 | var bv, cblock, cont, env, i, last, pblock, ref, ref1, threadOut, v, w;
|
954 | if (this.last) {
|
955 | last = this.last;
|
956 | } else {
|
957 | last = this.cont;
|
958 | }
|
959 | threadOut = {};
|
960 | ref = this.prefix.env;
|
961 | for (i in ref) {
|
962 | v = ref[i];
|
963 | if (v.mod && !v.fin) {
|
964 | threadOut[i] = true;
|
965 | }
|
966 | }
|
967 | last = last.addThread(threadOut);
|
968 | cont = last.capture();
|
969 | if (this.last) {
|
970 | bv = cont._bindVar;
|
971 | cont = this.cont.append(cont).capture();
|
972 | }
|
973 | env = {};
|
974 | envMerge(env = {}, this.prefix.env);
|
975 | envMerge(env, cont.env);
|
976 | pblock = this.prefix.toPrefix();
|
977 | cblock = cont.toBlockBuilder();
|
978 | ref1 = cblock.env;
|
979 | for (i in ref1) {
|
980 | v = ref1[i];
|
981 | if (!v.thread) {
|
982 | continue;
|
983 | }
|
984 | w = env[i] != null ? env[i] : env[i] = {
|
985 | fin: true
|
986 | };
|
987 | w.thread = true;
|
988 | }
|
989 | return root.blockEff(pblock.block.concat(cblock.block)).morph(this, env).needCoerce(cblock._needCoerce).setBindVar(cont.getBindVar()).noCapture().setBrk(this.isBrk());
|
990 | };
|
991 |
|
992 | return PurePrefix;
|
993 |
|
994 | })(Eff);
|
995 |
|
996 | root.purePrefix = function(prefix, cont) {
|
997 | return new PurePrefix(prefix, cont);
|
998 | };
|
999 |
|
1000 | BlockEff = (function(superClass) {
|
1001 | extend(BlockEff, superClass);
|
1002 |
|
1003 | function BlockEff(block1) {
|
1004 | this.block = block1;
|
1005 | BlockEff.__super__.constructor.call(this);
|
1006 | }
|
1007 |
|
1008 | BlockEff.prototype.toBlockBuilder = function() {
|
1009 | return this;
|
1010 | };
|
1011 |
|
1012 | return BlockEff;
|
1013 |
|
1014 | })(Eff);
|
1015 |
|
1016 | root.blockEff = function(block) {
|
1017 | return new BlockEff(block);
|
1018 | };
|
1019 |
|
1020 | root.block = function(eff, block) {
|
1021 | if (eff) {
|
1022 | return root.blockEff(block);
|
1023 | } else {
|
1024 | return root.pure(block);
|
1025 | }
|
1026 | };
|
1027 |
|
1028 | ExprEff = (function(superClass) {
|
1029 | extend(ExprEff, superClass);
|
1030 |
|
1031 | function ExprEff(expr1) {
|
1032 | this.expr = expr1;
|
1033 | ExprEff.__super__.constructor.call(this);
|
1034 | }
|
1035 |
|
1036 | ExprEff.prototype._coerceObj = function() {
|
1037 | var w;
|
1038 | w = this.opts.coerce;
|
1039 | if (w === "none") {
|
1040 | return this;
|
1041 | }
|
1042 | return root.exprEff(kit.coerceVal(this.expr)).morph(this);
|
1043 | };
|
1044 |
|
1045 | ExprEff.prototype.toExprBuilder = function() {
|
1046 | return this;
|
1047 | };
|
1048 |
|
1049 | return ExprEff;
|
1050 |
|
1051 | })(Eff);
|
1052 |
|
1053 | root.exprEffCoerce = function(expr) {
|
1054 | return (new ExprEff(expr)).needCoerce();
|
1055 | };
|
1056 |
|
1057 | root.exprEff = function(expr) {
|
1058 | return (new ExprEff(expr)).needCoerce(false);
|
1059 | };
|
1060 |
|
1061 | root.exprEffCaptured = function(expr) {
|
1062 | return (new ExprEff(expr)).noCapture();
|
1063 | };
|
1064 |
|
1065 | ExprPure = (function(superClass) {
|
1066 | extend(ExprPure, superClass);
|
1067 |
|
1068 | function ExprPure(expr1) {
|
1069 | this.expr = expr1;
|
1070 | ExprPure.__super__.constructor.call(this, [kit.ret(this.expr)]);
|
1071 | }
|
1072 |
|
1073 | ExprPure.prototype.toExprBuilder = function(eff) {
|
1074 | var expr;
|
1075 | if (!this.expr) {
|
1076 | return ExprPure.__super__.toExprBuilder.call(this, eff);
|
1077 | }
|
1078 | if (!eff) {
|
1079 | return this;
|
1080 | }
|
1081 | expr = this.opts.coerce === "none" ? kit.pure(this.expr) : this.expr;
|
1082 | return root.exprEff(expr).morph(this);
|
1083 | };
|
1084 |
|
1085 | ExprPure.prototype.toBlockBuilder = function(eff) {
|
1086 | var res;
|
1087 | res = root.pure([]);
|
1088 | res.ret = this.expr;
|
1089 | return res.morph(this).toBlockBuilder(eff);
|
1090 | };
|
1091 |
|
1092 | ExprPure.prototype.toPrefix = function() {
|
1093 | return root.pure([this._bindVar != null ? kit.varDecl(this._bindVar, this.expr) : kit.exprStmt(this.expr)]).morph(this).setBindVar(false);
|
1094 | };
|
1095 |
|
1096 | ExprPure.prototype._capture = function() {
|
1097 | var ov;
|
1098 | if (!this.expr) {
|
1099 | return ExprPure.__super__._capture.call(this);
|
1100 | }
|
1101 | ov = envThread(this.env);
|
1102 | if (ov.length) {
|
1103 | this.expr = kit.arr([this.expr].concat(slice.call(ov)));
|
1104 | this.block = [kit.ret(this.expr)];
|
1105 | }
|
1106 | return this;
|
1107 | };
|
1108 |
|
1109 | return ExprPure;
|
1110 |
|
1111 | })(BlockPure);
|
1112 |
|
1113 | root.exprPure = function(expr) {
|
1114 | return new ExprPure(expr);
|
1115 | };
|
1116 |
|
1117 | root.expr = function(expr, eff) {
|
1118 | if (eff) {
|
1119 | return root.exprEffCoerce(expr);
|
1120 | } else {
|
1121 | return root.exprPure(expr);
|
1122 | }
|
1123 | };
|
1124 |
|
1125 | }).call(this);
|