Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50663 views
1
// Generated by CoffeeScript 1.12.6
2
(function() {
3
var Access, Arr, Assign, Base, Block, BooleanLiteral, Call, Class, Code, CodeFragment, Comment, Existence, Expansion, ExportAllDeclaration, ExportDeclaration, ExportDefaultDeclaration, ExportNamedDeclaration, ExportSpecifier, ExportSpecifierList, Extends, For, IdentifierLiteral, If, ImportClause, ImportDeclaration, ImportDefaultSpecifier, ImportNamespaceSpecifier, ImportSpecifier, ImportSpecifierList, In, Index, InfinityLiteral, JS_FORBIDDEN, LEVEL_ACCESS, LEVEL_COND, LEVEL_LIST, LEVEL_OP, LEVEL_PAREN, LEVEL_TOP, Literal, ModuleDeclaration, ModuleSpecifier, ModuleSpecifierList, NEGATE, NO, NaNLiteral, NullLiteral, NumberLiteral, Obj, Op, Param, Parens, PassthroughLiteral, PropertyName, Range, RegexLiteral, RegexWithInterpolations, Return, SIMPLENUM, Scope, Slice, Splat, StatementLiteral, StringLiteral, StringWithInterpolations, SuperCall, Switch, TAB, THIS, TaggedTemplateCall, ThisLiteral, Throw, Try, UTILITIES, UndefinedLiteral, Value, While, YES, YieldReturn, addLocationDataFn, compact, del, ends, extend, flatten, fragmentsToText, isComplexOrAssignable, isLiteralArguments, isLiteralThis, isUnassignable, locationDataToString, merge, multident, ref1, ref2, some, starts, throwSyntaxError, unfoldSoak, utility,
4
extend1 = 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
indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; },
7
slice = [].slice;
8
9
Error.stackTraceLimit = 2e308;
10
11
Scope = require('./scope').Scope;
12
13
ref1 = require('./lexer'), isUnassignable = ref1.isUnassignable, JS_FORBIDDEN = ref1.JS_FORBIDDEN;
14
15
ref2 = require('./helpers'), compact = ref2.compact, flatten = ref2.flatten, extend = ref2.extend, merge = ref2.merge, del = ref2.del, starts = ref2.starts, ends = ref2.ends, some = ref2.some, addLocationDataFn = ref2.addLocationDataFn, locationDataToString = ref2.locationDataToString, throwSyntaxError = ref2.throwSyntaxError;
16
17
exports.extend = extend;
18
19
exports.addLocationDataFn = addLocationDataFn;
20
21
YES = function() {
22
return true;
23
};
24
25
NO = function() {
26
return false;
27
};
28
29
THIS = function() {
30
return this;
31
};
32
33
NEGATE = function() {
34
this.negated = !this.negated;
35
return this;
36
};
37
38
exports.CodeFragment = CodeFragment = (function() {
39
function CodeFragment(parent, code) {
40
var ref3;
41
this.code = "" + code;
42
this.locationData = parent != null ? parent.locationData : void 0;
43
this.type = (parent != null ? (ref3 = parent.constructor) != null ? ref3.name : void 0 : void 0) || 'unknown';
44
}
45
46
CodeFragment.prototype.toString = function() {
47
return "" + this.code + (this.locationData ? ": " + locationDataToString(this.locationData) : '');
48
};
49
50
return CodeFragment;
51
52
})();
53
54
fragmentsToText = function(fragments) {
55
var fragment;
56
return ((function() {
57
var j, len1, results;
58
results = [];
59
for (j = 0, len1 = fragments.length; j < len1; j++) {
60
fragment = fragments[j];
61
results.push(fragment.code);
62
}
63
return results;
64
})()).join('');
65
};
66
67
exports.Base = Base = (function() {
68
function Base() {}
69
70
Base.prototype.compile = function(o, lvl) {
71
return fragmentsToText(this.compileToFragments(o, lvl));
72
};
73
74
Base.prototype.compileToFragments = function(o, lvl) {
75
var node;
76
o = extend({}, o);
77
if (lvl) {
78
o.level = lvl;
79
}
80
node = this.unfoldSoak(o) || this;
81
node.tab = o.indent;
82
if (o.level === LEVEL_TOP || !node.isStatement(o)) {
83
return node.compileNode(o);
84
} else {
85
return node.compileClosure(o);
86
}
87
};
88
89
Base.prototype.compileClosure = function(o) {
90
var args, argumentsNode, func, jumpNode, meth, parts, ref3;
91
if (jumpNode = this.jumps()) {
92
jumpNode.error('cannot use a pure statement in an expression');
93
}
94
o.sharedScope = true;
95
func = new Code([], Block.wrap([this]));
96
args = [];
97
if ((argumentsNode = this.contains(isLiteralArguments)) || this.contains(isLiteralThis)) {
98
args = [new ThisLiteral];
99
if (argumentsNode) {
100
meth = 'apply';
101
args.push(new IdentifierLiteral('arguments'));
102
} else {
103
meth = 'call';
104
}
105
func = new Value(func, [new Access(new PropertyName(meth))]);
106
}
107
parts = (new Call(func, args)).compileNode(o);
108
if (func.isGenerator || ((ref3 = func.base) != null ? ref3.isGenerator : void 0)) {
109
parts.unshift(this.makeCode("(yield* "));
110
parts.push(this.makeCode(")"));
111
}
112
return parts;
113
};
114
115
Base.prototype.cache = function(o, level, isComplex) {
116
var complex, ref, sub;
117
complex = isComplex != null ? isComplex(this) : this.isComplex();
118
if (complex) {
119
ref = new IdentifierLiteral(o.scope.freeVariable('ref'));
120
sub = new Assign(ref, this);
121
if (level) {
122
return [sub.compileToFragments(o, level), [this.makeCode(ref.value)]];
123
} else {
124
return [sub, ref];
125
}
126
} else {
127
ref = level ? this.compileToFragments(o, level) : this;
128
return [ref, ref];
129
}
130
};
131
132
Base.prototype.cacheToCodeFragments = function(cacheValues) {
133
return [fragmentsToText(cacheValues[0]), fragmentsToText(cacheValues[1])];
134
};
135
136
Base.prototype.makeReturn = function(res) {
137
var me;
138
me = this.unwrapAll();
139
if (res) {
140
return new Call(new Literal(res + ".push"), [me]);
141
} else {
142
return new Return(me);
143
}
144
};
145
146
Base.prototype.contains = function(pred) {
147
var node;
148
node = void 0;
149
this.traverseChildren(false, function(n) {
150
if (pred(n)) {
151
node = n;
152
return false;
153
}
154
});
155
return node;
156
};
157
158
Base.prototype.lastNonComment = function(list) {
159
var i;
160
i = list.length;
161
while (i--) {
162
if (!(list[i] instanceof Comment)) {
163
return list[i];
164
}
165
}
166
return null;
167
};
168
169
Base.prototype.toString = function(idt, name) {
170
var tree;
171
if (idt == null) {
172
idt = '';
173
}
174
if (name == null) {
175
name = this.constructor.name;
176
}
177
tree = '\n' + idt + name;
178
if (this.soak) {
179
tree += '?';
180
}
181
this.eachChild(function(node) {
182
return tree += node.toString(idt + TAB);
183
});
184
return tree;
185
};
186
187
Base.prototype.eachChild = function(func) {
188
var attr, child, j, k, len1, len2, ref3, ref4;
189
if (!this.children) {
190
return this;
191
}
192
ref3 = this.children;
193
for (j = 0, len1 = ref3.length; j < len1; j++) {
194
attr = ref3[j];
195
if (this[attr]) {
196
ref4 = flatten([this[attr]]);
197
for (k = 0, len2 = ref4.length; k < len2; k++) {
198
child = ref4[k];
199
if (func(child) === false) {
200
return this;
201
}
202
}
203
}
204
}
205
return this;
206
};
207
208
Base.prototype.traverseChildren = function(crossScope, func) {
209
return this.eachChild(function(child) {
210
var recur;
211
recur = func(child);
212
if (recur !== false) {
213
return child.traverseChildren(crossScope, func);
214
}
215
});
216
};
217
218
Base.prototype.invert = function() {
219
return new Op('!', this);
220
};
221
222
Base.prototype.unwrapAll = function() {
223
var node;
224
node = this;
225
while (node !== (node = node.unwrap())) {
226
continue;
227
}
228
return node;
229
};
230
231
Base.prototype.children = [];
232
233
Base.prototype.isStatement = NO;
234
235
Base.prototype.jumps = NO;
236
237
Base.prototype.isComplex = YES;
238
239
Base.prototype.isChainable = NO;
240
241
Base.prototype.isAssignable = NO;
242
243
Base.prototype.isNumber = NO;
244
245
Base.prototype.unwrap = THIS;
246
247
Base.prototype.unfoldSoak = NO;
248
249
Base.prototype.assigns = NO;
250
251
Base.prototype.updateLocationDataIfMissing = function(locationData) {
252
if (this.locationData) {
253
return this;
254
}
255
this.locationData = locationData;
256
return this.eachChild(function(child) {
257
return child.updateLocationDataIfMissing(locationData);
258
});
259
};
260
261
Base.prototype.error = function(message) {
262
return throwSyntaxError(message, this.locationData);
263
};
264
265
Base.prototype.makeCode = function(code) {
266
return new CodeFragment(this, code);
267
};
268
269
Base.prototype.wrapInBraces = function(fragments) {
270
return [].concat(this.makeCode('('), fragments, this.makeCode(')'));
271
};
272
273
Base.prototype.joinFragmentArrays = function(fragmentsList, joinStr) {
274
var answer, fragments, i, j, len1;
275
answer = [];
276
for (i = j = 0, len1 = fragmentsList.length; j < len1; i = ++j) {
277
fragments = fragmentsList[i];
278
if (i) {
279
answer.push(this.makeCode(joinStr));
280
}
281
answer = answer.concat(fragments);
282
}
283
return answer;
284
};
285
286
return Base;
287
288
})();
289
290
exports.Block = Block = (function(superClass1) {
291
extend1(Block, superClass1);
292
293
function Block(nodes) {
294
this.expressions = compact(flatten(nodes || []));
295
}
296
297
Block.prototype.children = ['expressions'];
298
299
Block.prototype.push = function(node) {
300
this.expressions.push(node);
301
return this;
302
};
303
304
Block.prototype.pop = function() {
305
return this.expressions.pop();
306
};
307
308
Block.prototype.unshift = function(node) {
309
this.expressions.unshift(node);
310
return this;
311
};
312
313
Block.prototype.unwrap = function() {
314
if (this.expressions.length === 1) {
315
return this.expressions[0];
316
} else {
317
return this;
318
}
319
};
320
321
Block.prototype.isEmpty = function() {
322
return !this.expressions.length;
323
};
324
325
Block.prototype.isStatement = function(o) {
326
var exp, j, len1, ref3;
327
ref3 = this.expressions;
328
for (j = 0, len1 = ref3.length; j < len1; j++) {
329
exp = ref3[j];
330
if (exp.isStatement(o)) {
331
return true;
332
}
333
}
334
return false;
335
};
336
337
Block.prototype.jumps = function(o) {
338
var exp, j, jumpNode, len1, ref3;
339
ref3 = this.expressions;
340
for (j = 0, len1 = ref3.length; j < len1; j++) {
341
exp = ref3[j];
342
if (jumpNode = exp.jumps(o)) {
343
return jumpNode;
344
}
345
}
346
};
347
348
Block.prototype.makeReturn = function(res) {
349
var expr, len;
350
len = this.expressions.length;
351
while (len--) {
352
expr = this.expressions[len];
353
if (!(expr instanceof Comment)) {
354
this.expressions[len] = expr.makeReturn(res);
355
if (expr instanceof Return && !expr.expression) {
356
this.expressions.splice(len, 1);
357
}
358
break;
359
}
360
}
361
return this;
362
};
363
364
Block.prototype.compileToFragments = function(o, level) {
365
if (o == null) {
366
o = {};
367
}
368
if (o.scope) {
369
return Block.__super__.compileToFragments.call(this, o, level);
370
} else {
371
return this.compileRoot(o);
372
}
373
};
374
375
Block.prototype.compileNode = function(o) {
376
var answer, compiledNodes, fragments, index, j, len1, node, ref3, top;
377
this.tab = o.indent;
378
top = o.level === LEVEL_TOP;
379
compiledNodes = [];
380
ref3 = this.expressions;
381
for (index = j = 0, len1 = ref3.length; j < len1; index = ++j) {
382
node = ref3[index];
383
node = node.unwrapAll();
384
node = node.unfoldSoak(o) || node;
385
if (node instanceof Block) {
386
compiledNodes.push(node.compileNode(o));
387
} else if (top) {
388
node.front = true;
389
fragments = node.compileToFragments(o);
390
if (!node.isStatement(o)) {
391
fragments.unshift(this.makeCode("" + this.tab));
392
fragments.push(this.makeCode(";"));
393
}
394
compiledNodes.push(fragments);
395
} else {
396
compiledNodes.push(node.compileToFragments(o, LEVEL_LIST));
397
}
398
}
399
if (top) {
400
if (this.spaced) {
401
return [].concat(this.joinFragmentArrays(compiledNodes, '\n\n'), this.makeCode("\n"));
402
} else {
403
return this.joinFragmentArrays(compiledNodes, '\n');
404
}
405
}
406
if (compiledNodes.length) {
407
answer = this.joinFragmentArrays(compiledNodes, ', ');
408
} else {
409
answer = [this.makeCode("void 0")];
410
}
411
if (compiledNodes.length > 1 && o.level >= LEVEL_LIST) {
412
return this.wrapInBraces(answer);
413
} else {
414
return answer;
415
}
416
};
417
418
Block.prototype.compileRoot = function(o) {
419
var exp, fragments, i, j, len1, name, prelude, preludeExps, ref3, ref4, rest;
420
o.indent = o.bare ? '' : TAB;
421
o.level = LEVEL_TOP;
422
this.spaced = true;
423
o.scope = new Scope(null, this, null, (ref3 = o.referencedVars) != null ? ref3 : []);
424
ref4 = o.locals || [];
425
for (j = 0, len1 = ref4.length; j < len1; j++) {
426
name = ref4[j];
427
o.scope.parameter(name);
428
}
429
prelude = [];
430
if (!o.bare) {
431
preludeExps = (function() {
432
var k, len2, ref5, results;
433
ref5 = this.expressions;
434
results = [];
435
for (i = k = 0, len2 = ref5.length; k < len2; i = ++k) {
436
exp = ref5[i];
437
if (!(exp.unwrap() instanceof Comment)) {
438
break;
439
}
440
results.push(exp);
441
}
442
return results;
443
}).call(this);
444
rest = this.expressions.slice(preludeExps.length);
445
this.expressions = preludeExps;
446
if (preludeExps.length) {
447
prelude = this.compileNode(merge(o, {
448
indent: ''
449
}));
450
prelude.push(this.makeCode("\n"));
451
}
452
this.expressions = rest;
453
}
454
fragments = this.compileWithDeclarations(o);
455
if (o.bare) {
456
return fragments;
457
}
458
return [].concat(prelude, this.makeCode("(function() {\n"), fragments, this.makeCode("\n}).call(this);\n"));
459
};
460
461
Block.prototype.compileWithDeclarations = function(o) {
462
var assigns, declars, exp, fragments, i, j, len1, post, ref3, ref4, ref5, rest, scope, spaced;
463
fragments = [];
464
post = [];
465
ref3 = this.expressions;
466
for (i = j = 0, len1 = ref3.length; j < len1; i = ++j) {
467
exp = ref3[i];
468
exp = exp.unwrap();
469
if (!(exp instanceof Comment || exp instanceof Literal)) {
470
break;
471
}
472
}
473
o = merge(o, {
474
level: LEVEL_TOP
475
});
476
if (i) {
477
rest = this.expressions.splice(i, 9e9);
478
ref4 = [this.spaced, false], spaced = ref4[0], this.spaced = ref4[1];
479
ref5 = [this.compileNode(o), spaced], fragments = ref5[0], this.spaced = ref5[1];
480
this.expressions = rest;
481
}
482
post = this.compileNode(o);
483
scope = o.scope;
484
if (scope.expressions === this) {
485
declars = o.scope.hasDeclarations();
486
assigns = scope.hasAssignments;
487
if (declars || assigns) {
488
if (i) {
489
fragments.push(this.makeCode('\n'));
490
}
491
fragments.push(this.makeCode(this.tab + "var "));
492
if (declars) {
493
fragments.push(this.makeCode(scope.declaredVariables().join(', ')));
494
}
495
if (assigns) {
496
if (declars) {
497
fragments.push(this.makeCode(",\n" + (this.tab + TAB)));
498
}
499
fragments.push(this.makeCode(scope.assignedVariables().join(",\n" + (this.tab + TAB))));
500
}
501
fragments.push(this.makeCode(";\n" + (this.spaced ? '\n' : '')));
502
} else if (fragments.length && post.length) {
503
fragments.push(this.makeCode("\n"));
504
}
505
}
506
return fragments.concat(post);
507
};
508
509
Block.wrap = function(nodes) {
510
if (nodes.length === 1 && nodes[0] instanceof Block) {
511
return nodes[0];
512
}
513
return new Block(nodes);
514
};
515
516
return Block;
517
518
})(Base);
519
520
exports.Literal = Literal = (function(superClass1) {
521
extend1(Literal, superClass1);
522
523
function Literal(value1) {
524
this.value = value1;
525
}
526
527
Literal.prototype.isComplex = NO;
528
529
Literal.prototype.assigns = function(name) {
530
return name === this.value;
531
};
532
533
Literal.prototype.compileNode = function(o) {
534
return [this.makeCode(this.value)];
535
};
536
537
Literal.prototype.toString = function() {
538
return " " + (this.isStatement() ? Literal.__super__.toString.apply(this, arguments) : this.constructor.name) + ": " + this.value;
539
};
540
541
return Literal;
542
543
})(Base);
544
545
exports.NumberLiteral = NumberLiteral = (function(superClass1) {
546
extend1(NumberLiteral, superClass1);
547
548
function NumberLiteral() {
549
return NumberLiteral.__super__.constructor.apply(this, arguments);
550
}
551
552
return NumberLiteral;
553
554
})(Literal);
555
556
exports.InfinityLiteral = InfinityLiteral = (function(superClass1) {
557
extend1(InfinityLiteral, superClass1);
558
559
function InfinityLiteral() {
560
return InfinityLiteral.__super__.constructor.apply(this, arguments);
561
}
562
563
InfinityLiteral.prototype.compileNode = function() {
564
return [this.makeCode('2e308')];
565
};
566
567
return InfinityLiteral;
568
569
})(NumberLiteral);
570
571
exports.NaNLiteral = NaNLiteral = (function(superClass1) {
572
extend1(NaNLiteral, superClass1);
573
574
function NaNLiteral() {
575
NaNLiteral.__super__.constructor.call(this, 'NaN');
576
}
577
578
NaNLiteral.prototype.compileNode = function(o) {
579
var code;
580
code = [this.makeCode('0/0')];
581
if (o.level >= LEVEL_OP) {
582
return this.wrapInBraces(code);
583
} else {
584
return code;
585
}
586
};
587
588
return NaNLiteral;
589
590
})(NumberLiteral);
591
592
exports.StringLiteral = StringLiteral = (function(superClass1) {
593
extend1(StringLiteral, superClass1);
594
595
function StringLiteral() {
596
return StringLiteral.__super__.constructor.apply(this, arguments);
597
}
598
599
return StringLiteral;
600
601
})(Literal);
602
603
exports.RegexLiteral = RegexLiteral = (function(superClass1) {
604
extend1(RegexLiteral, superClass1);
605
606
function RegexLiteral() {
607
return RegexLiteral.__super__.constructor.apply(this, arguments);
608
}
609
610
return RegexLiteral;
611
612
})(Literal);
613
614
exports.PassthroughLiteral = PassthroughLiteral = (function(superClass1) {
615
extend1(PassthroughLiteral, superClass1);
616
617
function PassthroughLiteral() {
618
return PassthroughLiteral.__super__.constructor.apply(this, arguments);
619
}
620
621
return PassthroughLiteral;
622
623
})(Literal);
624
625
exports.IdentifierLiteral = IdentifierLiteral = (function(superClass1) {
626
extend1(IdentifierLiteral, superClass1);
627
628
function IdentifierLiteral() {
629
return IdentifierLiteral.__super__.constructor.apply(this, arguments);
630
}
631
632
IdentifierLiteral.prototype.isAssignable = YES;
633
634
return IdentifierLiteral;
635
636
})(Literal);
637
638
exports.PropertyName = PropertyName = (function(superClass1) {
639
extend1(PropertyName, superClass1);
640
641
function PropertyName() {
642
return PropertyName.__super__.constructor.apply(this, arguments);
643
}
644
645
PropertyName.prototype.isAssignable = YES;
646
647
return PropertyName;
648
649
})(Literal);
650
651
exports.StatementLiteral = StatementLiteral = (function(superClass1) {
652
extend1(StatementLiteral, superClass1);
653
654
function StatementLiteral() {
655
return StatementLiteral.__super__.constructor.apply(this, arguments);
656
}
657
658
StatementLiteral.prototype.isStatement = YES;
659
660
StatementLiteral.prototype.makeReturn = THIS;
661
662
StatementLiteral.prototype.jumps = function(o) {
663
if (this.value === 'break' && !((o != null ? o.loop : void 0) || (o != null ? o.block : void 0))) {
664
return this;
665
}
666
if (this.value === 'continue' && !(o != null ? o.loop : void 0)) {
667
return this;
668
}
669
};
670
671
StatementLiteral.prototype.compileNode = function(o) {
672
return [this.makeCode("" + this.tab + this.value + ";")];
673
};
674
675
return StatementLiteral;
676
677
})(Literal);
678
679
exports.ThisLiteral = ThisLiteral = (function(superClass1) {
680
extend1(ThisLiteral, superClass1);
681
682
function ThisLiteral() {
683
ThisLiteral.__super__.constructor.call(this, 'this');
684
}
685
686
ThisLiteral.prototype.compileNode = function(o) {
687
var code, ref3;
688
code = ((ref3 = o.scope.method) != null ? ref3.bound : void 0) ? o.scope.method.context : this.value;
689
return [this.makeCode(code)];
690
};
691
692
return ThisLiteral;
693
694
})(Literal);
695
696
exports.UndefinedLiteral = UndefinedLiteral = (function(superClass1) {
697
extend1(UndefinedLiteral, superClass1);
698
699
function UndefinedLiteral() {
700
UndefinedLiteral.__super__.constructor.call(this, 'undefined');
701
}
702
703
UndefinedLiteral.prototype.compileNode = function(o) {
704
return [this.makeCode(o.level >= LEVEL_ACCESS ? '(void 0)' : 'void 0')];
705
};
706
707
return UndefinedLiteral;
708
709
})(Literal);
710
711
exports.NullLiteral = NullLiteral = (function(superClass1) {
712
extend1(NullLiteral, superClass1);
713
714
function NullLiteral() {
715
NullLiteral.__super__.constructor.call(this, 'null');
716
}
717
718
return NullLiteral;
719
720
})(Literal);
721
722
exports.BooleanLiteral = BooleanLiteral = (function(superClass1) {
723
extend1(BooleanLiteral, superClass1);
724
725
function BooleanLiteral() {
726
return BooleanLiteral.__super__.constructor.apply(this, arguments);
727
}
728
729
return BooleanLiteral;
730
731
})(Literal);
732
733
exports.Return = Return = (function(superClass1) {
734
extend1(Return, superClass1);
735
736
function Return(expression) {
737
this.expression = expression;
738
}
739
740
Return.prototype.children = ['expression'];
741
742
Return.prototype.isStatement = YES;
743
744
Return.prototype.makeReturn = THIS;
745
746
Return.prototype.jumps = THIS;
747
748
Return.prototype.compileToFragments = function(o, level) {
749
var expr, ref3;
750
expr = (ref3 = this.expression) != null ? ref3.makeReturn() : void 0;
751
if (expr && !(expr instanceof Return)) {
752
return expr.compileToFragments(o, level);
753
} else {
754
return Return.__super__.compileToFragments.call(this, o, level);
755
}
756
};
757
758
Return.prototype.compileNode = function(o) {
759
var answer;
760
answer = [];
761
answer.push(this.makeCode(this.tab + ("return" + (this.expression ? " " : ""))));
762
if (this.expression) {
763
answer = answer.concat(this.expression.compileToFragments(o, LEVEL_PAREN));
764
}
765
answer.push(this.makeCode(";"));
766
return answer;
767
};
768
769
return Return;
770
771
})(Base);
772
773
exports.YieldReturn = YieldReturn = (function(superClass1) {
774
extend1(YieldReturn, superClass1);
775
776
function YieldReturn() {
777
return YieldReturn.__super__.constructor.apply(this, arguments);
778
}
779
780
YieldReturn.prototype.compileNode = function(o) {
781
if (o.scope.parent == null) {
782
this.error('yield can only occur inside functions');
783
}
784
return YieldReturn.__super__.compileNode.apply(this, arguments);
785
};
786
787
return YieldReturn;
788
789
})(Return);
790
791
exports.Value = Value = (function(superClass1) {
792
extend1(Value, superClass1);
793
794
function Value(base, props, tag) {
795
if (!props && base instanceof Value) {
796
return base;
797
}
798
this.base = base;
799
this.properties = props || [];
800
if (tag) {
801
this[tag] = true;
802
}
803
return this;
804
}
805
806
Value.prototype.children = ['base', 'properties'];
807
808
Value.prototype.add = function(props) {
809
this.properties = this.properties.concat(props);
810
return this;
811
};
812
813
Value.prototype.hasProperties = function() {
814
return !!this.properties.length;
815
};
816
817
Value.prototype.bareLiteral = function(type) {
818
return !this.properties.length && this.base instanceof type;
819
};
820
821
Value.prototype.isArray = function() {
822
return this.bareLiteral(Arr);
823
};
824
825
Value.prototype.isRange = function() {
826
return this.bareLiteral(Range);
827
};
828
829
Value.prototype.isComplex = function() {
830
return this.hasProperties() || this.base.isComplex();
831
};
832
833
Value.prototype.isAssignable = function() {
834
return this.hasProperties() || this.base.isAssignable();
835
};
836
837
Value.prototype.isNumber = function() {
838
return this.bareLiteral(NumberLiteral);
839
};
840
841
Value.prototype.isString = function() {
842
return this.bareLiteral(StringLiteral);
843
};
844
845
Value.prototype.isRegex = function() {
846
return this.bareLiteral(RegexLiteral);
847
};
848
849
Value.prototype.isUndefined = function() {
850
return this.bareLiteral(UndefinedLiteral);
851
};
852
853
Value.prototype.isNull = function() {
854
return this.bareLiteral(NullLiteral);
855
};
856
857
Value.prototype.isBoolean = function() {
858
return this.bareLiteral(BooleanLiteral);
859
};
860
861
Value.prototype.isAtomic = function() {
862
var j, len1, node, ref3;
863
ref3 = this.properties.concat(this.base);
864
for (j = 0, len1 = ref3.length; j < len1; j++) {
865
node = ref3[j];
866
if (node.soak || node instanceof Call) {
867
return false;
868
}
869
}
870
return true;
871
};
872
873
Value.prototype.isNotCallable = function() {
874
return this.isNumber() || this.isString() || this.isRegex() || this.isArray() || this.isRange() || this.isSplice() || this.isObject() || this.isUndefined() || this.isNull() || this.isBoolean();
875
};
876
877
Value.prototype.isStatement = function(o) {
878
return !this.properties.length && this.base.isStatement(o);
879
};
880
881
Value.prototype.assigns = function(name) {
882
return !this.properties.length && this.base.assigns(name);
883
};
884
885
Value.prototype.jumps = function(o) {
886
return !this.properties.length && this.base.jumps(o);
887
};
888
889
Value.prototype.isObject = function(onlyGenerated) {
890
if (this.properties.length) {
891
return false;
892
}
893
return (this.base instanceof Obj) && (!onlyGenerated || this.base.generated);
894
};
895
896
Value.prototype.isSplice = function() {
897
var lastProp, ref3;
898
ref3 = this.properties, lastProp = ref3[ref3.length - 1];
899
return lastProp instanceof Slice;
900
};
901
902
Value.prototype.looksStatic = function(className) {
903
var ref3;
904
return this.base.value === className && this.properties.length === 1 && ((ref3 = this.properties[0].name) != null ? ref3.value : void 0) !== 'prototype';
905
};
906
907
Value.prototype.unwrap = function() {
908
if (this.properties.length) {
909
return this;
910
} else {
911
return this.base;
912
}
913
};
914
915
Value.prototype.cacheReference = function(o) {
916
var base, bref, name, nref, ref3;
917
ref3 = this.properties, name = ref3[ref3.length - 1];
918
if (this.properties.length < 2 && !this.base.isComplex() && !(name != null ? name.isComplex() : void 0)) {
919
return [this, this];
920
}
921
base = new Value(this.base, this.properties.slice(0, -1));
922
if (base.isComplex()) {
923
bref = new IdentifierLiteral(o.scope.freeVariable('base'));
924
base = new Value(new Parens(new Assign(bref, base)));
925
}
926
if (!name) {
927
return [base, bref];
928
}
929
if (name.isComplex()) {
930
nref = new IdentifierLiteral(o.scope.freeVariable('name'));
931
name = new Index(new Assign(nref, name.index));
932
nref = new Index(nref);
933
}
934
return [base.add(name), new Value(bref || base.base, [nref || name])];
935
};
936
937
Value.prototype.compileNode = function(o) {
938
var fragments, j, len1, prop, props;
939
this.base.front = this.front;
940
props = this.properties;
941
fragments = this.base.compileToFragments(o, (props.length ? LEVEL_ACCESS : null));
942
if (props.length && SIMPLENUM.test(fragmentsToText(fragments))) {
943
fragments.push(this.makeCode('.'));
944
}
945
for (j = 0, len1 = props.length; j < len1; j++) {
946
prop = props[j];
947
fragments.push.apply(fragments, prop.compileToFragments(o));
948
}
949
return fragments;
950
};
951
952
Value.prototype.unfoldSoak = function(o) {
953
return this.unfoldedSoak != null ? this.unfoldedSoak : this.unfoldedSoak = (function(_this) {
954
return function() {
955
var fst, i, ifn, j, len1, prop, ref, ref3, ref4, snd;
956
if (ifn = _this.base.unfoldSoak(o)) {
957
(ref3 = ifn.body.properties).push.apply(ref3, _this.properties);
958
return ifn;
959
}
960
ref4 = _this.properties;
961
for (i = j = 0, len1 = ref4.length; j < len1; i = ++j) {
962
prop = ref4[i];
963
if (!prop.soak) {
964
continue;
965
}
966
prop.soak = false;
967
fst = new Value(_this.base, _this.properties.slice(0, i));
968
snd = new Value(_this.base, _this.properties.slice(i));
969
if (fst.isComplex()) {
970
ref = new IdentifierLiteral(o.scope.freeVariable('ref'));
971
fst = new Parens(new Assign(ref, fst));
972
snd.base = ref;
973
}
974
return new If(new Existence(fst), snd, {
975
soak: true
976
});
977
}
978
return false;
979
};
980
})(this)();
981
};
982
983
return Value;
984
985
})(Base);
986
987
exports.Comment = Comment = (function(superClass1) {
988
extend1(Comment, superClass1);
989
990
function Comment(comment1) {
991
this.comment = comment1;
992
}
993
994
Comment.prototype.isStatement = YES;
995
996
Comment.prototype.makeReturn = THIS;
997
998
Comment.prototype.compileNode = function(o, level) {
999
var code, comment;
1000
comment = this.comment.replace(/^(\s*)#(?=\s)/gm, "$1 *");
1001
code = "/*" + (multident(comment, this.tab)) + (indexOf.call(comment, '\n') >= 0 ? "\n" + this.tab : '') + " */";
1002
if ((level || o.level) === LEVEL_TOP) {
1003
code = o.indent + code;
1004
}
1005
return [this.makeCode("\n"), this.makeCode(code)];
1006
};
1007
1008
return Comment;
1009
1010
})(Base);
1011
1012
exports.Call = Call = (function(superClass1) {
1013
extend1(Call, superClass1);
1014
1015
function Call(variable1, args1, soak1) {
1016
this.variable = variable1;
1017
this.args = args1 != null ? args1 : [];
1018
this.soak = soak1;
1019
this.isNew = false;
1020
if (this.variable instanceof Value && this.variable.isNotCallable()) {
1021
this.variable.error("literal is not a function");
1022
}
1023
}
1024
1025
Call.prototype.children = ['variable', 'args'];
1026
1027
Call.prototype.updateLocationDataIfMissing = function(locationData) {
1028
var base, ref3;
1029
if (this.locationData && this.needsUpdatedStartLocation) {
1030
this.locationData.first_line = locationData.first_line;
1031
this.locationData.first_column = locationData.first_column;
1032
base = ((ref3 = this.variable) != null ? ref3.base : void 0) || this.variable;
1033
if (base.needsUpdatedStartLocation) {
1034
this.variable.locationData.first_line = locationData.first_line;
1035
this.variable.locationData.first_column = locationData.first_column;
1036
base.updateLocationDataIfMissing(locationData);
1037
}
1038
delete this.needsUpdatedStartLocation;
1039
}
1040
return Call.__super__.updateLocationDataIfMissing.apply(this, arguments);
1041
};
1042
1043
Call.prototype.newInstance = function() {
1044
var base, ref3;
1045
base = ((ref3 = this.variable) != null ? ref3.base : void 0) || this.variable;
1046
if (base instanceof Call && !base.isNew) {
1047
base.newInstance();
1048
} else {
1049
this.isNew = true;
1050
}
1051
this.needsUpdatedStartLocation = true;
1052
return this;
1053
};
1054
1055
Call.prototype.unfoldSoak = function(o) {
1056
var call, ifn, j, left, len1, list, ref3, ref4, rite;
1057
if (this.soak) {
1058
if (this instanceof SuperCall) {
1059
left = new Literal(this.superReference(o));
1060
rite = new Value(left);
1061
} else {
1062
if (ifn = unfoldSoak(o, this, 'variable')) {
1063
return ifn;
1064
}
1065
ref3 = new Value(this.variable).cacheReference(o), left = ref3[0], rite = ref3[1];
1066
}
1067
rite = new Call(rite, this.args);
1068
rite.isNew = this.isNew;
1069
left = new Literal("typeof " + (left.compile(o)) + " === \"function\"");
1070
return new If(left, new Value(rite), {
1071
soak: true
1072
});
1073
}
1074
call = this;
1075
list = [];
1076
while (true) {
1077
if (call.variable instanceof Call) {
1078
list.push(call);
1079
call = call.variable;
1080
continue;
1081
}
1082
if (!(call.variable instanceof Value)) {
1083
break;
1084
}
1085
list.push(call);
1086
if (!((call = call.variable.base) instanceof Call)) {
1087
break;
1088
}
1089
}
1090
ref4 = list.reverse();
1091
for (j = 0, len1 = ref4.length; j < len1; j++) {
1092
call = ref4[j];
1093
if (ifn) {
1094
if (call.variable instanceof Call) {
1095
call.variable = ifn;
1096
} else {
1097
call.variable.base = ifn;
1098
}
1099
}
1100
ifn = unfoldSoak(o, call, 'variable');
1101
}
1102
return ifn;
1103
};
1104
1105
Call.prototype.compileNode = function(o) {
1106
var arg, argIndex, compiledArgs, compiledArray, fragments, j, len1, preface, ref3, ref4;
1107
if ((ref3 = this.variable) != null) {
1108
ref3.front = this.front;
1109
}
1110
compiledArray = Splat.compileSplattedArray(o, this.args, true);
1111
if (compiledArray.length) {
1112
return this.compileSplat(o, compiledArray);
1113
}
1114
compiledArgs = [];
1115
ref4 = this.args;
1116
for (argIndex = j = 0, len1 = ref4.length; j < len1; argIndex = ++j) {
1117
arg = ref4[argIndex];
1118
if (argIndex) {
1119
compiledArgs.push(this.makeCode(", "));
1120
}
1121
compiledArgs.push.apply(compiledArgs, arg.compileToFragments(o, LEVEL_LIST));
1122
}
1123
fragments = [];
1124
if (this instanceof SuperCall) {
1125
preface = this.superReference(o) + (".call(" + (this.superThis(o)));
1126
if (compiledArgs.length) {
1127
preface += ", ";
1128
}
1129
fragments.push(this.makeCode(preface));
1130
} else {
1131
if (this.isNew) {
1132
fragments.push(this.makeCode('new '));
1133
}
1134
fragments.push.apply(fragments, this.variable.compileToFragments(o, LEVEL_ACCESS));
1135
fragments.push(this.makeCode("("));
1136
}
1137
fragments.push.apply(fragments, compiledArgs);
1138
fragments.push(this.makeCode(")"));
1139
return fragments;
1140
};
1141
1142
Call.prototype.compileSplat = function(o, splatArgs) {
1143
var answer, base, fun, idt, name, ref;
1144
if (this instanceof SuperCall) {
1145
return [].concat(this.makeCode((this.superReference(o)) + ".apply(" + (this.superThis(o)) + ", "), splatArgs, this.makeCode(")"));
1146
}
1147
if (this.isNew) {
1148
idt = this.tab + TAB;
1149
return [].concat(this.makeCode("(function(func, args, ctor) {\n" + idt + "ctor.prototype = func.prototype;\n" + idt + "var child = new ctor, result = func.apply(child, args);\n" + idt + "return Object(result) === result ? result : child;\n" + this.tab + "})("), this.variable.compileToFragments(o, LEVEL_LIST), this.makeCode(", "), splatArgs, this.makeCode(", function(){})"));
1150
}
1151
answer = [];
1152
base = new Value(this.variable);
1153
if ((name = base.properties.pop()) && base.isComplex()) {
1154
ref = o.scope.freeVariable('ref');
1155
answer = answer.concat(this.makeCode("(" + ref + " = "), base.compileToFragments(o, LEVEL_LIST), this.makeCode(")"), name.compileToFragments(o));
1156
} else {
1157
fun = base.compileToFragments(o, LEVEL_ACCESS);
1158
if (SIMPLENUM.test(fragmentsToText(fun))) {
1159
fun = this.wrapInBraces(fun);
1160
}
1161
if (name) {
1162
ref = fragmentsToText(fun);
1163
fun.push.apply(fun, name.compileToFragments(o));
1164
} else {
1165
ref = 'null';
1166
}
1167
answer = answer.concat(fun);
1168
}
1169
return answer = answer.concat(this.makeCode(".apply(" + ref + ", "), splatArgs, this.makeCode(")"));
1170
};
1171
1172
return Call;
1173
1174
})(Base);
1175
1176
exports.SuperCall = SuperCall = (function(superClass1) {
1177
extend1(SuperCall, superClass1);
1178
1179
function SuperCall(args) {
1180
SuperCall.__super__.constructor.call(this, null, args != null ? args : [new Splat(new IdentifierLiteral('arguments'))]);
1181
this.isBare = args != null;
1182
}
1183
1184
SuperCall.prototype.superReference = function(o) {
1185
var accesses, base, bref, klass, method, name, nref, variable;
1186
method = o.scope.namedMethod();
1187
if (method != null ? method.klass : void 0) {
1188
klass = method.klass, name = method.name, variable = method.variable;
1189
if (klass.isComplex()) {
1190
bref = new IdentifierLiteral(o.scope.parent.freeVariable('base'));
1191
base = new Value(new Parens(new Assign(bref, klass)));
1192
variable.base = base;
1193
variable.properties.splice(0, klass.properties.length);
1194
}
1195
if (name.isComplex() || (name instanceof Index && name.index.isAssignable())) {
1196
nref = new IdentifierLiteral(o.scope.parent.freeVariable('name'));
1197
name = new Index(new Assign(nref, name.index));
1198
variable.properties.pop();
1199
variable.properties.push(name);
1200
}
1201
accesses = [new Access(new PropertyName('__super__'))];
1202
if (method["static"]) {
1203
accesses.push(new Access(new PropertyName('constructor')));
1204
}
1205
accesses.push(nref != null ? new Index(nref) : name);
1206
return (new Value(bref != null ? bref : klass, accesses)).compile(o);
1207
} else if (method != null ? method.ctor : void 0) {
1208
return method.name + ".__super__.constructor";
1209
} else {
1210
return this.error('cannot call super outside of an instance method.');
1211
}
1212
};
1213
1214
SuperCall.prototype.superThis = function(o) {
1215
var method;
1216
method = o.scope.method;
1217
return (method && !method.klass && method.context) || "this";
1218
};
1219
1220
return SuperCall;
1221
1222
})(Call);
1223
1224
exports.RegexWithInterpolations = RegexWithInterpolations = (function(superClass1) {
1225
extend1(RegexWithInterpolations, superClass1);
1226
1227
function RegexWithInterpolations(args) {
1228
if (args == null) {
1229
args = [];
1230
}
1231
RegexWithInterpolations.__super__.constructor.call(this, new Value(new IdentifierLiteral('RegExp')), args, false);
1232
}
1233
1234
return RegexWithInterpolations;
1235
1236
})(Call);
1237
1238
exports.TaggedTemplateCall = TaggedTemplateCall = (function(superClass1) {
1239
extend1(TaggedTemplateCall, superClass1);
1240
1241
function TaggedTemplateCall(variable, arg, soak) {
1242
if (arg instanceof StringLiteral) {
1243
arg = new StringWithInterpolations(Block.wrap([new Value(arg)]));
1244
}
1245
TaggedTemplateCall.__super__.constructor.call(this, variable, [arg], soak);
1246
}
1247
1248
TaggedTemplateCall.prototype.compileNode = function(o) {
1249
o.inTaggedTemplateCall = true;
1250
return this.variable.compileToFragments(o, LEVEL_ACCESS).concat(this.args[0].compileToFragments(o, LEVEL_LIST));
1251
};
1252
1253
return TaggedTemplateCall;
1254
1255
})(Call);
1256
1257
exports.Extends = Extends = (function(superClass1) {
1258
extend1(Extends, superClass1);
1259
1260
function Extends(child1, parent1) {
1261
this.child = child1;
1262
this.parent = parent1;
1263
}
1264
1265
Extends.prototype.children = ['child', 'parent'];
1266
1267
Extends.prototype.compileToFragments = function(o) {
1268
return new Call(new Value(new Literal(utility('extend', o))), [this.child, this.parent]).compileToFragments(o);
1269
};
1270
1271
return Extends;
1272
1273
})(Base);
1274
1275
exports.Access = Access = (function(superClass1) {
1276
extend1(Access, superClass1);
1277
1278
function Access(name1, tag) {
1279
this.name = name1;
1280
this.soak = tag === 'soak';
1281
}
1282
1283
Access.prototype.children = ['name'];
1284
1285
Access.prototype.compileToFragments = function(o) {
1286
var name, node, ref3;
1287
name = this.name.compileToFragments(o);
1288
node = this.name.unwrap();
1289
if (node instanceof PropertyName) {
1290
if (ref3 = node.value, indexOf.call(JS_FORBIDDEN, ref3) >= 0) {
1291
return [this.makeCode('["')].concat(slice.call(name), [this.makeCode('"]')]);
1292
} else {
1293
return [this.makeCode('.')].concat(slice.call(name));
1294
}
1295
} else {
1296
return [this.makeCode('[')].concat(slice.call(name), [this.makeCode(']')]);
1297
}
1298
};
1299
1300
Access.prototype.isComplex = NO;
1301
1302
return Access;
1303
1304
})(Base);
1305
1306
exports.Index = Index = (function(superClass1) {
1307
extend1(Index, superClass1);
1308
1309
function Index(index1) {
1310
this.index = index1;
1311
}
1312
1313
Index.prototype.children = ['index'];
1314
1315
Index.prototype.compileToFragments = function(o) {
1316
return [].concat(this.makeCode("["), this.index.compileToFragments(o, LEVEL_PAREN), this.makeCode("]"));
1317
};
1318
1319
Index.prototype.isComplex = function() {
1320
return this.index.isComplex();
1321
};
1322
1323
return Index;
1324
1325
})(Base);
1326
1327
exports.Range = Range = (function(superClass1) {
1328
extend1(Range, superClass1);
1329
1330
Range.prototype.children = ['from', 'to'];
1331
1332
function Range(from1, to1, tag) {
1333
this.from = from1;
1334
this.to = to1;
1335
this.exclusive = tag === 'exclusive';
1336
this.equals = this.exclusive ? '' : '=';
1337
}
1338
1339
Range.prototype.compileVariables = function(o) {
1340
var isComplex, ref3, ref4, ref5, step;
1341
o = merge(o, {
1342
top: true
1343
});
1344
isComplex = del(o, 'isComplex');
1345
ref3 = this.cacheToCodeFragments(this.from.cache(o, LEVEL_LIST, isComplex)), this.fromC = ref3[0], this.fromVar = ref3[1];
1346
ref4 = this.cacheToCodeFragments(this.to.cache(o, LEVEL_LIST, isComplex)), this.toC = ref4[0], this.toVar = ref4[1];
1347
if (step = del(o, 'step')) {
1348
ref5 = this.cacheToCodeFragments(step.cache(o, LEVEL_LIST, isComplex)), this.step = ref5[0], this.stepVar = ref5[1];
1349
}
1350
this.fromNum = this.from.isNumber() ? Number(this.fromVar) : null;
1351
this.toNum = this.to.isNumber() ? Number(this.toVar) : null;
1352
return this.stepNum = (step != null ? step.isNumber() : void 0) ? Number(this.stepVar) : null;
1353
};
1354
1355
Range.prototype.compileNode = function(o) {
1356
var cond, condPart, from, gt, idx, idxName, known, lt, namedIndex, ref3, ref4, stepPart, to, varPart;
1357
if (!this.fromVar) {
1358
this.compileVariables(o);
1359
}
1360
if (!o.index) {
1361
return this.compileArray(o);
1362
}
1363
known = (this.fromNum != null) && (this.toNum != null);
1364
idx = del(o, 'index');
1365
idxName = del(o, 'name');
1366
namedIndex = idxName && idxName !== idx;
1367
varPart = idx + " = " + this.fromC;
1368
if (this.toC !== this.toVar) {
1369
varPart += ", " + this.toC;
1370
}
1371
if (this.step !== this.stepVar) {
1372
varPart += ", " + this.step;
1373
}
1374
ref3 = [idx + " <" + this.equals, idx + " >" + this.equals], lt = ref3[0], gt = ref3[1];
1375
condPart = this.stepNum != null ? this.stepNum > 0 ? lt + " " + this.toVar : gt + " " + this.toVar : known ? ((ref4 = [this.fromNum, this.toNum], from = ref4[0], to = ref4[1], ref4), from <= to ? lt + " " + to : gt + " " + to) : (cond = this.stepVar ? this.stepVar + " > 0" : this.fromVar + " <= " + this.toVar, cond + " ? " + lt + " " + this.toVar + " : " + gt + " " + this.toVar);
1376
stepPart = this.stepVar ? idx + " += " + this.stepVar : known ? namedIndex ? from <= to ? "++" + idx : "--" + idx : from <= to ? idx + "++" : idx + "--" : namedIndex ? cond + " ? ++" + idx + " : --" + idx : cond + " ? " + idx + "++ : " + idx + "--";
1377
if (namedIndex) {
1378
varPart = idxName + " = " + varPart;
1379
}
1380
if (namedIndex) {
1381
stepPart = idxName + " = " + stepPart;
1382
}
1383
return [this.makeCode(varPart + "; " + condPart + "; " + stepPart)];
1384
};
1385
1386
Range.prototype.compileArray = function(o) {
1387
var args, body, cond, hasArgs, i, idt, j, known, post, pre, range, ref3, ref4, result, results, vars;
1388
known = (this.fromNum != null) && (this.toNum != null);
1389
if (known && Math.abs(this.fromNum - this.toNum) <= 20) {
1390
range = (function() {
1391
results = [];
1392
for (var j = ref3 = this.fromNum, ref4 = this.toNum; ref3 <= ref4 ? j <= ref4 : j >= ref4; ref3 <= ref4 ? j++ : j--){ results.push(j); }
1393
return results;
1394
}).apply(this);
1395
if (this.exclusive) {
1396
range.pop();
1397
}
1398
return [this.makeCode("[" + (range.join(', ')) + "]")];
1399
}
1400
idt = this.tab + TAB;
1401
i = o.scope.freeVariable('i', {
1402
single: true
1403
});
1404
result = o.scope.freeVariable('results');
1405
pre = "\n" + idt + result + " = [];";
1406
if (known) {
1407
o.index = i;
1408
body = fragmentsToText(this.compileNode(o));
1409
} else {
1410
vars = (i + " = " + this.fromC) + (this.toC !== this.toVar ? ", " + this.toC : '');
1411
cond = this.fromVar + " <= " + this.toVar;
1412
body = "var " + vars + "; " + cond + " ? " + i + " <" + this.equals + " " + this.toVar + " : " + i + " >" + this.equals + " " + this.toVar + "; " + cond + " ? " + i + "++ : " + i + "--";
1413
}
1414
post = "{ " + result + ".push(" + i + "); }\n" + idt + "return " + result + ";\n" + o.indent;
1415
hasArgs = function(node) {
1416
return node != null ? node.contains(isLiteralArguments) : void 0;
1417
};
1418
if (hasArgs(this.from) || hasArgs(this.to)) {
1419
args = ', arguments';
1420
}
1421
return [this.makeCode("(function() {" + pre + "\n" + idt + "for (" + body + ")" + post + "}).apply(this" + (args != null ? args : '') + ")")];
1422
};
1423
1424
return Range;
1425
1426
})(Base);
1427
1428
exports.Slice = Slice = (function(superClass1) {
1429
extend1(Slice, superClass1);
1430
1431
Slice.prototype.children = ['range'];
1432
1433
function Slice(range1) {
1434
this.range = range1;
1435
Slice.__super__.constructor.call(this);
1436
}
1437
1438
Slice.prototype.compileNode = function(o) {
1439
var compiled, compiledText, from, fromCompiled, ref3, to, toStr;
1440
ref3 = this.range, to = ref3.to, from = ref3.from;
1441
fromCompiled = from && from.compileToFragments(o, LEVEL_PAREN) || [this.makeCode('0')];
1442
if (to) {
1443
compiled = to.compileToFragments(o, LEVEL_PAREN);
1444
compiledText = fragmentsToText(compiled);
1445
if (!(!this.range.exclusive && +compiledText === -1)) {
1446
toStr = ', ' + (this.range.exclusive ? compiledText : to.isNumber() ? "" + (+compiledText + 1) : (compiled = to.compileToFragments(o, LEVEL_ACCESS), "+" + (fragmentsToText(compiled)) + " + 1 || 9e9"));
1447
}
1448
}
1449
return [this.makeCode(".slice(" + (fragmentsToText(fromCompiled)) + (toStr || '') + ")")];
1450
};
1451
1452
return Slice;
1453
1454
})(Base);
1455
1456
exports.Obj = Obj = (function(superClass1) {
1457
extend1(Obj, superClass1);
1458
1459
function Obj(props, generated) {
1460
this.generated = generated != null ? generated : false;
1461
this.objects = this.properties = props || [];
1462
}
1463
1464
Obj.prototype.children = ['properties'];
1465
1466
Obj.prototype.compileNode = function(o) {
1467
var answer, dynamicIndex, hasDynamic, i, idt, indent, j, join, k, key, l, lastNoncom, len1, len2, len3, node, oref, prop, props, ref3, value;
1468
props = this.properties;
1469
if (this.generated) {
1470
for (j = 0, len1 = props.length; j < len1; j++) {
1471
node = props[j];
1472
if (node instanceof Value) {
1473
node.error('cannot have an implicit value in an implicit object');
1474
}
1475
}
1476
}
1477
for (dynamicIndex = k = 0, len2 = props.length; k < len2; dynamicIndex = ++k) {
1478
prop = props[dynamicIndex];
1479
if ((prop.variable || prop).base instanceof Parens) {
1480
break;
1481
}
1482
}
1483
hasDynamic = dynamicIndex < props.length;
1484
idt = o.indent += TAB;
1485
lastNoncom = this.lastNonComment(this.properties);
1486
answer = [];
1487
if (hasDynamic) {
1488
oref = o.scope.freeVariable('obj');
1489
answer.push(this.makeCode("(\n" + idt + oref + " = "));
1490
}
1491
answer.push(this.makeCode("{" + (props.length === 0 || dynamicIndex === 0 ? '}' : '\n')));
1492
for (i = l = 0, len3 = props.length; l < len3; i = ++l) {
1493
prop = props[i];
1494
if (i === dynamicIndex) {
1495
if (i !== 0) {
1496
answer.push(this.makeCode("\n" + idt + "}"));
1497
}
1498
answer.push(this.makeCode(',\n'));
1499
}
1500
join = i === props.length - 1 || i === dynamicIndex - 1 ? '' : prop === lastNoncom || prop instanceof Comment ? '\n' : ',\n';
1501
indent = prop instanceof Comment ? '' : idt;
1502
if (hasDynamic && i < dynamicIndex) {
1503
indent += TAB;
1504
}
1505
if (prop instanceof Assign) {
1506
if (prop.context !== 'object') {
1507
prop.operatorToken.error("unexpected " + prop.operatorToken.value);
1508
}
1509
if (prop.variable instanceof Value && prop.variable.hasProperties()) {
1510
prop.variable.error('invalid object key');
1511
}
1512
}
1513
if (prop instanceof Value && prop["this"]) {
1514
prop = new Assign(prop.properties[0].name, prop, 'object');
1515
}
1516
if (!(prop instanceof Comment)) {
1517
if (i < dynamicIndex) {
1518
if (!(prop instanceof Assign)) {
1519
prop = new Assign(prop, prop, 'object');
1520
}
1521
} else {
1522
if (prop instanceof Assign) {
1523
key = prop.variable;
1524
value = prop.value;
1525
} else {
1526
ref3 = prop.base.cache(o), key = ref3[0], value = ref3[1];
1527
if (key instanceof IdentifierLiteral) {
1528
key = new PropertyName(key.value);
1529
}
1530
}
1531
prop = new Assign(new Value(new IdentifierLiteral(oref), [new Access(key)]), value);
1532
}
1533
}
1534
if (indent) {
1535
answer.push(this.makeCode(indent));
1536
}
1537
answer.push.apply(answer, prop.compileToFragments(o, LEVEL_TOP));
1538
if (join) {
1539
answer.push(this.makeCode(join));
1540
}
1541
}
1542
if (hasDynamic) {
1543
answer.push(this.makeCode(",\n" + idt + oref + "\n" + this.tab + ")"));
1544
} else {
1545
if (props.length !== 0) {
1546
answer.push(this.makeCode("\n" + this.tab + "}"));
1547
}
1548
}
1549
if (this.front && !hasDynamic) {
1550
return this.wrapInBraces(answer);
1551
} else {
1552
return answer;
1553
}
1554
};
1555
1556
Obj.prototype.assigns = function(name) {
1557
var j, len1, prop, ref3;
1558
ref3 = this.properties;
1559
for (j = 0, len1 = ref3.length; j < len1; j++) {
1560
prop = ref3[j];
1561
if (prop.assigns(name)) {
1562
return true;
1563
}
1564
}
1565
return false;
1566
};
1567
1568
return Obj;
1569
1570
})(Base);
1571
1572
exports.Arr = Arr = (function(superClass1) {
1573
extend1(Arr, superClass1);
1574
1575
function Arr(objs) {
1576
this.objects = objs || [];
1577
}
1578
1579
Arr.prototype.children = ['objects'];
1580
1581
Arr.prototype.compileNode = function(o) {
1582
var answer, compiledObjs, fragments, index, j, len1, obj;
1583
if (!this.objects.length) {
1584
return [this.makeCode('[]')];
1585
}
1586
o.indent += TAB;
1587
answer = Splat.compileSplattedArray(o, this.objects);
1588
if (answer.length) {
1589
return answer;
1590
}
1591
answer = [];
1592
compiledObjs = (function() {
1593
var j, len1, ref3, results;
1594
ref3 = this.objects;
1595
results = [];
1596
for (j = 0, len1 = ref3.length; j < len1; j++) {
1597
obj = ref3[j];
1598
results.push(obj.compileToFragments(o, LEVEL_LIST));
1599
}
1600
return results;
1601
}).call(this);
1602
for (index = j = 0, len1 = compiledObjs.length; j < len1; index = ++j) {
1603
fragments = compiledObjs[index];
1604
if (index) {
1605
answer.push(this.makeCode(", "));
1606
}
1607
answer.push.apply(answer, fragments);
1608
}
1609
if (fragmentsToText(answer).indexOf('\n') >= 0) {
1610
answer.unshift(this.makeCode("[\n" + o.indent));
1611
answer.push(this.makeCode("\n" + this.tab + "]"));
1612
} else {
1613
answer.unshift(this.makeCode("["));
1614
answer.push(this.makeCode("]"));
1615
}
1616
return answer;
1617
};
1618
1619
Arr.prototype.assigns = function(name) {
1620
var j, len1, obj, ref3;
1621
ref3 = this.objects;
1622
for (j = 0, len1 = ref3.length; j < len1; j++) {
1623
obj = ref3[j];
1624
if (obj.assigns(name)) {
1625
return true;
1626
}
1627
}
1628
return false;
1629
};
1630
1631
return Arr;
1632
1633
})(Base);
1634
1635
exports.Class = Class = (function(superClass1) {
1636
extend1(Class, superClass1);
1637
1638
function Class(variable1, parent1, body1) {
1639
this.variable = variable1;
1640
this.parent = parent1;
1641
this.body = body1 != null ? body1 : new Block;
1642
this.boundFuncs = [];
1643
this.body.classBody = true;
1644
}
1645
1646
Class.prototype.children = ['variable', 'parent', 'body'];
1647
1648
Class.prototype.defaultClassVariableName = '_Class';
1649
1650
Class.prototype.determineName = function() {
1651
var message, name, node, ref3, tail;
1652
if (!this.variable) {
1653
return this.defaultClassVariableName;
1654
}
1655
ref3 = this.variable.properties, tail = ref3[ref3.length - 1];
1656
node = tail ? tail instanceof Access && tail.name : this.variable.base;
1657
if (!(node instanceof IdentifierLiteral || node instanceof PropertyName)) {
1658
return this.defaultClassVariableName;
1659
}
1660
name = node.value;
1661
if (!tail) {
1662
message = isUnassignable(name);
1663
if (message) {
1664
this.variable.error(message);
1665
}
1666
}
1667
if (indexOf.call(JS_FORBIDDEN, name) >= 0) {
1668
return "_" + name;
1669
} else {
1670
return name;
1671
}
1672
};
1673
1674
Class.prototype.setContext = function(name) {
1675
return this.body.traverseChildren(false, function(node) {
1676
if (node.classBody) {
1677
return false;
1678
}
1679
if (node instanceof ThisLiteral) {
1680
return node.value = name;
1681
} else if (node instanceof Code) {
1682
if (node.bound) {
1683
return node.context = name;
1684
}
1685
}
1686
});
1687
};
1688
1689
Class.prototype.addBoundFunctions = function(o) {
1690
var bvar, j, len1, lhs, ref3;
1691
ref3 = this.boundFuncs;
1692
for (j = 0, len1 = ref3.length; j < len1; j++) {
1693
bvar = ref3[j];
1694
lhs = (new Value(new ThisLiteral, [new Access(bvar)])).compile(o);
1695
this.ctor.body.unshift(new Literal(lhs + " = " + (utility('bind', o)) + "(" + lhs + ", this)"));
1696
}
1697
};
1698
1699
Class.prototype.addProperties = function(node, name, o) {
1700
var acc, assign, base, exprs, func, props;
1701
props = node.base.properties.slice(0);
1702
exprs = (function() {
1703
var results;
1704
results = [];
1705
while (assign = props.shift()) {
1706
if (assign instanceof Assign) {
1707
base = assign.variable.base;
1708
delete assign.context;
1709
func = assign.value;
1710
if (base.value === 'constructor') {
1711
if (this.ctor) {
1712
assign.error('cannot define more than one constructor in a class');
1713
}
1714
if (func.bound) {
1715
assign.error('cannot define a constructor as a bound function');
1716
}
1717
if (func instanceof Code) {
1718
assign = this.ctor = func;
1719
} else {
1720
this.externalCtor = o.classScope.freeVariable('ctor');
1721
assign = new Assign(new IdentifierLiteral(this.externalCtor), func);
1722
}
1723
} else {
1724
if (assign.variable["this"]) {
1725
func["static"] = true;
1726
} else {
1727
acc = base.isComplex() ? new Index(base) : new Access(base);
1728
assign.variable = new Value(new IdentifierLiteral(name), [new Access(new PropertyName('prototype')), acc]);
1729
if (func instanceof Code && func.bound) {
1730
this.boundFuncs.push(base);
1731
func.bound = false;
1732
}
1733
}
1734
}
1735
}
1736
results.push(assign);
1737
}
1738
return results;
1739
}).call(this);
1740
return compact(exprs);
1741
};
1742
1743
Class.prototype.walkBody = function(name, o) {
1744
return this.traverseChildren(false, (function(_this) {
1745
return function(child) {
1746
var cont, exps, i, j, len1, node, ref3;
1747
cont = true;
1748
if (child instanceof Class) {
1749
return false;
1750
}
1751
if (child instanceof Block) {
1752
ref3 = exps = child.expressions;
1753
for (i = j = 0, len1 = ref3.length; j < len1; i = ++j) {
1754
node = ref3[i];
1755
if (node instanceof Assign && node.variable.looksStatic(name)) {
1756
node.value["static"] = true;
1757
} else if (node instanceof Value && node.isObject(true)) {
1758
cont = false;
1759
exps[i] = _this.addProperties(node, name, o);
1760
}
1761
}
1762
child.expressions = exps = flatten(exps);
1763
}
1764
return cont && !(child instanceof Class);
1765
};
1766
})(this));
1767
};
1768
1769
Class.prototype.hoistDirectivePrologue = function() {
1770
var expressions, index, node;
1771
index = 0;
1772
expressions = this.body.expressions;
1773
while ((node = expressions[index]) && node instanceof Comment || node instanceof Value && node.isString()) {
1774
++index;
1775
}
1776
return this.directives = expressions.splice(0, index);
1777
};
1778
1779
Class.prototype.ensureConstructor = function(name) {
1780
if (!this.ctor) {
1781
this.ctor = new Code;
1782
if (this.externalCtor) {
1783
this.ctor.body.push(new Literal(this.externalCtor + ".apply(this, arguments)"));
1784
} else if (this.parent) {
1785
this.ctor.body.push(new Literal(name + ".__super__.constructor.apply(this, arguments)"));
1786
}
1787
this.ctor.body.makeReturn();
1788
this.body.expressions.unshift(this.ctor);
1789
}
1790
this.ctor.ctor = this.ctor.name = name;
1791
this.ctor.klass = null;
1792
return this.ctor.noReturn = true;
1793
};
1794
1795
Class.prototype.compileNode = function(o) {
1796
var args, argumentsNode, func, jumpNode, klass, lname, name, ref3, superClass;
1797
if (jumpNode = this.body.jumps()) {
1798
jumpNode.error('Class bodies cannot contain pure statements');
1799
}
1800
if (argumentsNode = this.body.contains(isLiteralArguments)) {
1801
argumentsNode.error("Class bodies shouldn't reference arguments");
1802
}
1803
name = this.determineName();
1804
lname = new IdentifierLiteral(name);
1805
func = new Code([], Block.wrap([this.body]));
1806
args = [];
1807
o.classScope = func.makeScope(o.scope);
1808
this.hoistDirectivePrologue();
1809
this.setContext(name);
1810
this.walkBody(name, o);
1811
this.ensureConstructor(name);
1812
this.addBoundFunctions(o);
1813
this.body.spaced = true;
1814
this.body.expressions.push(lname);
1815
if (this.parent) {
1816
superClass = new IdentifierLiteral(o.classScope.freeVariable('superClass', {
1817
reserve: false
1818
}));
1819
this.body.expressions.unshift(new Extends(lname, superClass));
1820
func.params.push(new Param(superClass));
1821
args.push(this.parent);
1822
}
1823
(ref3 = this.body.expressions).unshift.apply(ref3, this.directives);
1824
klass = new Parens(new Call(func, args));
1825
if (this.variable) {
1826
klass = new Assign(this.variable, klass, null, {
1827
moduleDeclaration: this.moduleDeclaration
1828
});
1829
}
1830
return klass.compileToFragments(o);
1831
};
1832
1833
return Class;
1834
1835
})(Base);
1836
1837
exports.ModuleDeclaration = ModuleDeclaration = (function(superClass1) {
1838
extend1(ModuleDeclaration, superClass1);
1839
1840
function ModuleDeclaration(clause, source1) {
1841
this.clause = clause;
1842
this.source = source1;
1843
this.checkSource();
1844
}
1845
1846
ModuleDeclaration.prototype.children = ['clause', 'source'];
1847
1848
ModuleDeclaration.prototype.isStatement = YES;
1849
1850
ModuleDeclaration.prototype.jumps = THIS;
1851
1852
ModuleDeclaration.prototype.makeReturn = THIS;
1853
1854
ModuleDeclaration.prototype.checkSource = function() {
1855
if ((this.source != null) && this.source instanceof StringWithInterpolations) {
1856
return this.source.error('the name of the module to be imported from must be an uninterpolated string');
1857
}
1858
};
1859
1860
ModuleDeclaration.prototype.checkScope = function(o, moduleDeclarationType) {
1861
if (o.indent.length !== 0) {
1862
return this.error(moduleDeclarationType + " statements must be at top-level scope");
1863
}
1864
};
1865
1866
return ModuleDeclaration;
1867
1868
})(Base);
1869
1870
exports.ImportDeclaration = ImportDeclaration = (function(superClass1) {
1871
extend1(ImportDeclaration, superClass1);
1872
1873
function ImportDeclaration() {
1874
return ImportDeclaration.__super__.constructor.apply(this, arguments);
1875
}
1876
1877
ImportDeclaration.prototype.compileNode = function(o) {
1878
var code, ref3;
1879
this.checkScope(o, 'import');
1880
o.importedSymbols = [];
1881
code = [];
1882
code.push(this.makeCode(this.tab + "import "));
1883
if (this.clause != null) {
1884
code.push.apply(code, this.clause.compileNode(o));
1885
}
1886
if (((ref3 = this.source) != null ? ref3.value : void 0) != null) {
1887
if (this.clause !== null) {
1888
code.push(this.makeCode(' from '));
1889
}
1890
code.push(this.makeCode(this.source.value));
1891
}
1892
code.push(this.makeCode(';'));
1893
return code;
1894
};
1895
1896
return ImportDeclaration;
1897
1898
})(ModuleDeclaration);
1899
1900
exports.ImportClause = ImportClause = (function(superClass1) {
1901
extend1(ImportClause, superClass1);
1902
1903
function ImportClause(defaultBinding, namedImports) {
1904
this.defaultBinding = defaultBinding;
1905
this.namedImports = namedImports;
1906
}
1907
1908
ImportClause.prototype.children = ['defaultBinding', 'namedImports'];
1909
1910
ImportClause.prototype.compileNode = function(o) {
1911
var code;
1912
code = [];
1913
if (this.defaultBinding != null) {
1914
code.push.apply(code, this.defaultBinding.compileNode(o));
1915
if (this.namedImports != null) {
1916
code.push(this.makeCode(', '));
1917
}
1918
}
1919
if (this.namedImports != null) {
1920
code.push.apply(code, this.namedImports.compileNode(o));
1921
}
1922
return code;
1923
};
1924
1925
return ImportClause;
1926
1927
})(Base);
1928
1929
exports.ExportDeclaration = ExportDeclaration = (function(superClass1) {
1930
extend1(ExportDeclaration, superClass1);
1931
1932
function ExportDeclaration() {
1933
return ExportDeclaration.__super__.constructor.apply(this, arguments);
1934
}
1935
1936
ExportDeclaration.prototype.compileNode = function(o) {
1937
var code, ref3;
1938
this.checkScope(o, 'export');
1939
code = [];
1940
code.push(this.makeCode(this.tab + "export "));
1941
if (this instanceof ExportDefaultDeclaration) {
1942
code.push(this.makeCode('default '));
1943
}
1944
if (!(this instanceof ExportDefaultDeclaration) && (this.clause instanceof Assign || this.clause instanceof Class)) {
1945
if (this.clause instanceof Class && !this.clause.variable) {
1946
this.clause.error('anonymous classes cannot be exported');
1947
}
1948
code.push(this.makeCode('var '));
1949
this.clause.moduleDeclaration = 'export';
1950
}
1951
if ((this.clause.body != null) && this.clause.body instanceof Block) {
1952
code = code.concat(this.clause.compileToFragments(o, LEVEL_TOP));
1953
} else {
1954
code = code.concat(this.clause.compileNode(o));
1955
}
1956
if (((ref3 = this.source) != null ? ref3.value : void 0) != null) {
1957
code.push(this.makeCode(" from " + this.source.value));
1958
}
1959
code.push(this.makeCode(';'));
1960
return code;
1961
};
1962
1963
return ExportDeclaration;
1964
1965
})(ModuleDeclaration);
1966
1967
exports.ExportNamedDeclaration = ExportNamedDeclaration = (function(superClass1) {
1968
extend1(ExportNamedDeclaration, superClass1);
1969
1970
function ExportNamedDeclaration() {
1971
return ExportNamedDeclaration.__super__.constructor.apply(this, arguments);
1972
}
1973
1974
return ExportNamedDeclaration;
1975
1976
})(ExportDeclaration);
1977
1978
exports.ExportDefaultDeclaration = ExportDefaultDeclaration = (function(superClass1) {
1979
extend1(ExportDefaultDeclaration, superClass1);
1980
1981
function ExportDefaultDeclaration() {
1982
return ExportDefaultDeclaration.__super__.constructor.apply(this, arguments);
1983
}
1984
1985
return ExportDefaultDeclaration;
1986
1987
})(ExportDeclaration);
1988
1989
exports.ExportAllDeclaration = ExportAllDeclaration = (function(superClass1) {
1990
extend1(ExportAllDeclaration, superClass1);
1991
1992
function ExportAllDeclaration() {
1993
return ExportAllDeclaration.__super__.constructor.apply(this, arguments);
1994
}
1995
1996
return ExportAllDeclaration;
1997
1998
})(ExportDeclaration);
1999
2000
exports.ModuleSpecifierList = ModuleSpecifierList = (function(superClass1) {
2001
extend1(ModuleSpecifierList, superClass1);
2002
2003
function ModuleSpecifierList(specifiers) {
2004
this.specifiers = specifiers;
2005
}
2006
2007
ModuleSpecifierList.prototype.children = ['specifiers'];
2008
2009
ModuleSpecifierList.prototype.compileNode = function(o) {
2010
var code, compiledList, fragments, index, j, len1, specifier;
2011
code = [];
2012
o.indent += TAB;
2013
compiledList = (function() {
2014
var j, len1, ref3, results;
2015
ref3 = this.specifiers;
2016
results = [];
2017
for (j = 0, len1 = ref3.length; j < len1; j++) {
2018
specifier = ref3[j];
2019
results.push(specifier.compileToFragments(o, LEVEL_LIST));
2020
}
2021
return results;
2022
}).call(this);
2023
if (this.specifiers.length !== 0) {
2024
code.push(this.makeCode("{\n" + o.indent));
2025
for (index = j = 0, len1 = compiledList.length; j < len1; index = ++j) {
2026
fragments = compiledList[index];
2027
if (index) {
2028
code.push(this.makeCode(",\n" + o.indent));
2029
}
2030
code.push.apply(code, fragments);
2031
}
2032
code.push(this.makeCode("\n}"));
2033
} else {
2034
code.push(this.makeCode('{}'));
2035
}
2036
return code;
2037
};
2038
2039
return ModuleSpecifierList;
2040
2041
})(Base);
2042
2043
exports.ImportSpecifierList = ImportSpecifierList = (function(superClass1) {
2044
extend1(ImportSpecifierList, superClass1);
2045
2046
function ImportSpecifierList() {
2047
return ImportSpecifierList.__super__.constructor.apply(this, arguments);
2048
}
2049
2050
return ImportSpecifierList;
2051
2052
})(ModuleSpecifierList);
2053
2054
exports.ExportSpecifierList = ExportSpecifierList = (function(superClass1) {
2055
extend1(ExportSpecifierList, superClass1);
2056
2057
function ExportSpecifierList() {
2058
return ExportSpecifierList.__super__.constructor.apply(this, arguments);
2059
}
2060
2061
return ExportSpecifierList;
2062
2063
})(ModuleSpecifierList);
2064
2065
exports.ModuleSpecifier = ModuleSpecifier = (function(superClass1) {
2066
extend1(ModuleSpecifier, superClass1);
2067
2068
function ModuleSpecifier(original, alias, moduleDeclarationType1) {
2069
this.original = original;
2070
this.alias = alias;
2071
this.moduleDeclarationType = moduleDeclarationType1;
2072
this.identifier = this.alias != null ? this.alias.value : this.original.value;
2073
}
2074
2075
ModuleSpecifier.prototype.children = ['original', 'alias'];
2076
2077
ModuleSpecifier.prototype.compileNode = function(o) {
2078
var code;
2079
o.scope.find(this.identifier, this.moduleDeclarationType);
2080
code = [];
2081
code.push(this.makeCode(this.original.value));
2082
if (this.alias != null) {
2083
code.push(this.makeCode(" as " + this.alias.value));
2084
}
2085
return code;
2086
};
2087
2088
return ModuleSpecifier;
2089
2090
})(Base);
2091
2092
exports.ImportSpecifier = ImportSpecifier = (function(superClass1) {
2093
extend1(ImportSpecifier, superClass1);
2094
2095
function ImportSpecifier(imported, local) {
2096
ImportSpecifier.__super__.constructor.call(this, imported, local, 'import');
2097
}
2098
2099
ImportSpecifier.prototype.compileNode = function(o) {
2100
var ref3;
2101
if ((ref3 = this.identifier, indexOf.call(o.importedSymbols, ref3) >= 0) || o.scope.check(this.identifier)) {
2102
this.error("'" + this.identifier + "' has already been declared");
2103
} else {
2104
o.importedSymbols.push(this.identifier);
2105
}
2106
return ImportSpecifier.__super__.compileNode.call(this, o);
2107
};
2108
2109
return ImportSpecifier;
2110
2111
})(ModuleSpecifier);
2112
2113
exports.ImportDefaultSpecifier = ImportDefaultSpecifier = (function(superClass1) {
2114
extend1(ImportDefaultSpecifier, superClass1);
2115
2116
function ImportDefaultSpecifier() {
2117
return ImportDefaultSpecifier.__super__.constructor.apply(this, arguments);
2118
}
2119
2120
return ImportDefaultSpecifier;
2121
2122
})(ImportSpecifier);
2123
2124
exports.ImportNamespaceSpecifier = ImportNamespaceSpecifier = (function(superClass1) {
2125
extend1(ImportNamespaceSpecifier, superClass1);
2126
2127
function ImportNamespaceSpecifier() {
2128
return ImportNamespaceSpecifier.__super__.constructor.apply(this, arguments);
2129
}
2130
2131
return ImportNamespaceSpecifier;
2132
2133
})(ImportSpecifier);
2134
2135
exports.ExportSpecifier = ExportSpecifier = (function(superClass1) {
2136
extend1(ExportSpecifier, superClass1);
2137
2138
function ExportSpecifier(local, exported) {
2139
ExportSpecifier.__super__.constructor.call(this, local, exported, 'export');
2140
}
2141
2142
return ExportSpecifier;
2143
2144
})(ModuleSpecifier);
2145
2146
exports.Assign = Assign = (function(superClass1) {
2147
extend1(Assign, superClass1);
2148
2149
function Assign(variable1, value1, context, options) {
2150
this.variable = variable1;
2151
this.value = value1;
2152
this.context = context;
2153
if (options == null) {
2154
options = {};
2155
}
2156
this.param = options.param, this.subpattern = options.subpattern, this.operatorToken = options.operatorToken, this.moduleDeclaration = options.moduleDeclaration;
2157
}
2158
2159
Assign.prototype.children = ['variable', 'value'];
2160
2161
Assign.prototype.isStatement = function(o) {
2162
return (o != null ? o.level : void 0) === LEVEL_TOP && (this.context != null) && (this.moduleDeclaration || indexOf.call(this.context, "?") >= 0);
2163
};
2164
2165
Assign.prototype.checkAssignability = function(o, varBase) {
2166
if (Object.prototype.hasOwnProperty.call(o.scope.positions, varBase.value) && o.scope.variables[o.scope.positions[varBase.value]].type === 'import') {
2167
return varBase.error("'" + varBase.value + "' is read-only");
2168
}
2169
};
2170
2171
Assign.prototype.assigns = function(name) {
2172
return this[this.context === 'object' ? 'value' : 'variable'].assigns(name);
2173
};
2174
2175
Assign.prototype.unfoldSoak = function(o) {
2176
return unfoldSoak(o, this, 'variable');
2177
};
2178
2179
Assign.prototype.compileNode = function(o) {
2180
var answer, compiledName, isValue, j, name, properties, prototype, ref3, ref4, ref5, ref6, ref7, ref8, val, varBase;
2181
if (isValue = this.variable instanceof Value) {
2182
if (this.variable.isArray() || this.variable.isObject()) {
2183
return this.compilePatternMatch(o);
2184
}
2185
if (this.variable.isSplice()) {
2186
return this.compileSplice(o);
2187
}
2188
if ((ref3 = this.context) === '||=' || ref3 === '&&=' || ref3 === '?=') {
2189
return this.compileConditional(o);
2190
}
2191
if ((ref4 = this.context) === '**=' || ref4 === '//=' || ref4 === '%%=') {
2192
return this.compileSpecialMath(o);
2193
}
2194
}
2195
if (this.value instanceof Code) {
2196
if (this.value["static"]) {
2197
this.value.klass = this.variable.base;
2198
this.value.name = this.variable.properties[0];
2199
this.value.variable = this.variable;
2200
} else if (((ref5 = this.variable.properties) != null ? ref5.length : void 0) >= 2) {
2201
ref6 = this.variable.properties, properties = 3 <= ref6.length ? slice.call(ref6, 0, j = ref6.length - 2) : (j = 0, []), prototype = ref6[j++], name = ref6[j++];
2202
if (((ref7 = prototype.name) != null ? ref7.value : void 0) === 'prototype') {
2203
this.value.klass = new Value(this.variable.base, properties);
2204
this.value.name = name;
2205
this.value.variable = this.variable;
2206
}
2207
}
2208
}
2209
if (!this.context) {
2210
varBase = this.variable.unwrapAll();
2211
if (!varBase.isAssignable()) {
2212
this.variable.error("'" + (this.variable.compile(o)) + "' can't be assigned");
2213
}
2214
if (!(typeof varBase.hasProperties === "function" ? varBase.hasProperties() : void 0)) {
2215
if (this.moduleDeclaration) {
2216
this.checkAssignability(o, varBase);
2217
o.scope.add(varBase.value, this.moduleDeclaration);
2218
} else if (this.param) {
2219
o.scope.add(varBase.value, 'var');
2220
} else {
2221
this.checkAssignability(o, varBase);
2222
o.scope.find(varBase.value);
2223
}
2224
}
2225
}
2226
val = this.value.compileToFragments(o, LEVEL_LIST);
2227
if (isValue && this.variable.base instanceof Obj) {
2228
this.variable.front = true;
2229
}
2230
compiledName = this.variable.compileToFragments(o, LEVEL_LIST);
2231
if (this.context === 'object') {
2232
if (ref8 = fragmentsToText(compiledName), indexOf.call(JS_FORBIDDEN, ref8) >= 0) {
2233
compiledName.unshift(this.makeCode('"'));
2234
compiledName.push(this.makeCode('"'));
2235
}
2236
return compiledName.concat(this.makeCode(": "), val);
2237
}
2238
answer = compiledName.concat(this.makeCode(" " + (this.context || '=') + " "), val);
2239
if (o.level <= LEVEL_LIST) {
2240
return answer;
2241
} else {
2242
return this.wrapInBraces(answer);
2243
}
2244
};
2245
2246
Assign.prototype.compilePatternMatch = function(o) {
2247
var acc, assigns, code, defaultValue, expandedIdx, fragments, i, idx, isObject, ivar, j, len1, message, name, obj, objects, olen, ref, ref3, ref4, ref5, ref6, rest, top, val, value, vvar, vvarText;
2248
top = o.level === LEVEL_TOP;
2249
value = this.value;
2250
objects = this.variable.base.objects;
2251
if (!(olen = objects.length)) {
2252
code = value.compileToFragments(o);
2253
if (o.level >= LEVEL_OP) {
2254
return this.wrapInBraces(code);
2255
} else {
2256
return code;
2257
}
2258
}
2259
obj = objects[0];
2260
if (olen === 1 && obj instanceof Expansion) {
2261
obj.error('Destructuring assignment has no target');
2262
}
2263
isObject = this.variable.isObject();
2264
if (top && olen === 1 && !(obj instanceof Splat)) {
2265
defaultValue = null;
2266
if (obj instanceof Assign && obj.context === 'object') {
2267
ref3 = obj, (ref4 = ref3.variable, idx = ref4.base), obj = ref3.value;
2268
if (obj instanceof Assign) {
2269
defaultValue = obj.value;
2270
obj = obj.variable;
2271
}
2272
} else {
2273
if (obj instanceof Assign) {
2274
defaultValue = obj.value;
2275
obj = obj.variable;
2276
}
2277
idx = isObject ? obj["this"] ? obj.properties[0].name : new PropertyName(obj.unwrap().value) : new NumberLiteral(0);
2278
}
2279
acc = idx.unwrap() instanceof PropertyName;
2280
value = new Value(value);
2281
value.properties.push(new (acc ? Access : Index)(idx));
2282
message = isUnassignable(obj.unwrap().value);
2283
if (message) {
2284
obj.error(message);
2285
}
2286
if (defaultValue) {
2287
value = new Op('?', value, defaultValue);
2288
}
2289
return new Assign(obj, value, null, {
2290
param: this.param
2291
}).compileToFragments(o, LEVEL_TOP);
2292
}
2293
vvar = value.compileToFragments(o, LEVEL_LIST);
2294
vvarText = fragmentsToText(vvar);
2295
assigns = [];
2296
expandedIdx = false;
2297
if (!(value.unwrap() instanceof IdentifierLiteral) || this.variable.assigns(vvarText)) {
2298
assigns.push([this.makeCode((ref = o.scope.freeVariable('ref')) + " = ")].concat(slice.call(vvar)));
2299
vvar = [this.makeCode(ref)];
2300
vvarText = ref;
2301
}
2302
for (i = j = 0, len1 = objects.length; j < len1; i = ++j) {
2303
obj = objects[i];
2304
idx = i;
2305
if (!expandedIdx && obj instanceof Splat) {
2306
name = obj.name.unwrap().value;
2307
obj = obj.unwrap();
2308
val = olen + " <= " + vvarText + ".length ? " + (utility('slice', o)) + ".call(" + vvarText + ", " + i;
2309
if (rest = olen - i - 1) {
2310
ivar = o.scope.freeVariable('i', {
2311
single: true
2312
});
2313
val += ", " + ivar + " = " + vvarText + ".length - " + rest + ") : (" + ivar + " = " + i + ", [])";
2314
} else {
2315
val += ") : []";
2316
}
2317
val = new Literal(val);
2318
expandedIdx = ivar + "++";
2319
} else if (!expandedIdx && obj instanceof Expansion) {
2320
if (rest = olen - i - 1) {
2321
if (rest === 1) {
2322
expandedIdx = vvarText + ".length - 1";
2323
} else {
2324
ivar = o.scope.freeVariable('i', {
2325
single: true
2326
});
2327
val = new Literal(ivar + " = " + vvarText + ".length - " + rest);
2328
expandedIdx = ivar + "++";
2329
assigns.push(val.compileToFragments(o, LEVEL_LIST));
2330
}
2331
}
2332
continue;
2333
} else {
2334
if (obj instanceof Splat || obj instanceof Expansion) {
2335
obj.error("multiple splats/expansions are disallowed in an assignment");
2336
}
2337
defaultValue = null;
2338
if (obj instanceof Assign && obj.context === 'object') {
2339
ref5 = obj, (ref6 = ref5.variable, idx = ref6.base), obj = ref5.value;
2340
if (obj instanceof Assign) {
2341
defaultValue = obj.value;
2342
obj = obj.variable;
2343
}
2344
} else {
2345
if (obj instanceof Assign) {
2346
defaultValue = obj.value;
2347
obj = obj.variable;
2348
}
2349
idx = isObject ? obj["this"] ? obj.properties[0].name : new PropertyName(obj.unwrap().value) : new Literal(expandedIdx || idx);
2350
}
2351
name = obj.unwrap().value;
2352
acc = idx.unwrap() instanceof PropertyName;
2353
val = new Value(new Literal(vvarText), [new (acc ? Access : Index)(idx)]);
2354
if (defaultValue) {
2355
val = new Op('?', val, defaultValue);
2356
}
2357
}
2358
if (name != null) {
2359
message = isUnassignable(name);
2360
if (message) {
2361
obj.error(message);
2362
}
2363
}
2364
assigns.push(new Assign(obj, val, null, {
2365
param: this.param,
2366
subpattern: true
2367
}).compileToFragments(o, LEVEL_LIST));
2368
}
2369
if (!(top || this.subpattern)) {
2370
assigns.push(vvar);
2371
}
2372
fragments = this.joinFragmentArrays(assigns, ', ');
2373
if (o.level < LEVEL_LIST) {
2374
return fragments;
2375
} else {
2376
return this.wrapInBraces(fragments);
2377
}
2378
};
2379
2380
Assign.prototype.compileConditional = function(o) {
2381
var fragments, left, ref3, right;
2382
ref3 = this.variable.cacheReference(o), left = ref3[0], right = ref3[1];
2383
if (!left.properties.length && left.base instanceof Literal && !(left.base instanceof ThisLiteral) && !o.scope.check(left.base.value)) {
2384
this.variable.error("the variable \"" + left.base.value + "\" can't be assigned with " + this.context + " because it has not been declared before");
2385
}
2386
if (indexOf.call(this.context, "?") >= 0) {
2387
o.isExistentialEquals = true;
2388
return new If(new Existence(left), right, {
2389
type: 'if'
2390
}).addElse(new Assign(right, this.value, '=')).compileToFragments(o);
2391
} else {
2392
fragments = new Op(this.context.slice(0, -1), left, new Assign(right, this.value, '=')).compileToFragments(o);
2393
if (o.level <= LEVEL_LIST) {
2394
return fragments;
2395
} else {
2396
return this.wrapInBraces(fragments);
2397
}
2398
}
2399
};
2400
2401
Assign.prototype.compileSpecialMath = function(o) {
2402
var left, ref3, right;
2403
ref3 = this.variable.cacheReference(o), left = ref3[0], right = ref3[1];
2404
return new Assign(left, new Op(this.context.slice(0, -1), right, this.value)).compileToFragments(o);
2405
};
2406
2407
Assign.prototype.compileSplice = function(o) {
2408
var answer, exclusive, from, fromDecl, fromRef, name, ref3, ref4, ref5, to, valDef, valRef;
2409
ref3 = this.variable.properties.pop().range, from = ref3.from, to = ref3.to, exclusive = ref3.exclusive;
2410
name = this.variable.compile(o);
2411
if (from) {
2412
ref4 = this.cacheToCodeFragments(from.cache(o, LEVEL_OP)), fromDecl = ref4[0], fromRef = ref4[1];
2413
} else {
2414
fromDecl = fromRef = '0';
2415
}
2416
if (to) {
2417
if ((from != null ? from.isNumber() : void 0) && to.isNumber()) {
2418
to = to.compile(o) - fromRef;
2419
if (!exclusive) {
2420
to += 1;
2421
}
2422
} else {
2423
to = to.compile(o, LEVEL_ACCESS) + ' - ' + fromRef;
2424
if (!exclusive) {
2425
to += ' + 1';
2426
}
2427
}
2428
} else {
2429
to = "9e9";
2430
}
2431
ref5 = this.value.cache(o, LEVEL_LIST), valDef = ref5[0], valRef = ref5[1];
2432
answer = [].concat(this.makeCode("[].splice.apply(" + name + ", [" + fromDecl + ", " + to + "].concat("), valDef, this.makeCode(")), "), valRef);
2433
if (o.level > LEVEL_TOP) {
2434
return this.wrapInBraces(answer);
2435
} else {
2436
return answer;
2437
}
2438
};
2439
2440
return Assign;
2441
2442
})(Base);
2443
2444
exports.Code = Code = (function(superClass1) {
2445
extend1(Code, superClass1);
2446
2447
function Code(params, body, tag) {
2448
this.params = params || [];
2449
this.body = body || new Block;
2450
this.bound = tag === 'boundfunc';
2451
this.isGenerator = !!this.body.contains(function(node) {
2452
return (node instanceof Op && node.isYield()) || node instanceof YieldReturn;
2453
});
2454
}
2455
2456
Code.prototype.children = ['params', 'body'];
2457
2458
Code.prototype.isStatement = function() {
2459
return !!this.ctor;
2460
};
2461
2462
Code.prototype.jumps = NO;
2463
2464
Code.prototype.makeScope = function(parentScope) {
2465
return new Scope(parentScope, this.body, this);
2466
};
2467
2468
Code.prototype.compileNode = function(o) {
2469
var answer, boundfunc, code, exprs, i, j, k, l, len1, len2, len3, len4, len5, len6, lit, m, p, param, params, q, r, ref, ref3, ref4, ref5, ref6, ref7, ref8, splats, uniqs, val, wasEmpty, wrapper;
2470
if (this.bound && ((ref3 = o.scope.method) != null ? ref3.bound : void 0)) {
2471
this.context = o.scope.method.context;
2472
}
2473
if (this.bound && !this.context) {
2474
this.context = '_this';
2475
wrapper = new Code([new Param(new IdentifierLiteral(this.context))], new Block([this]));
2476
boundfunc = new Call(wrapper, [new ThisLiteral]);
2477
boundfunc.updateLocationDataIfMissing(this.locationData);
2478
return boundfunc.compileNode(o);
2479
}
2480
o.scope = del(o, 'classScope') || this.makeScope(o.scope);
2481
o.scope.shared = del(o, 'sharedScope');
2482
o.indent += TAB;
2483
delete o.bare;
2484
delete o.isExistentialEquals;
2485
params = [];
2486
exprs = [];
2487
ref4 = this.params;
2488
for (j = 0, len1 = ref4.length; j < len1; j++) {
2489
param = ref4[j];
2490
if (!(param instanceof Expansion)) {
2491
o.scope.parameter(param.asReference(o));
2492
}
2493
}
2494
ref5 = this.params;
2495
for (k = 0, len2 = ref5.length; k < len2; k++) {
2496
param = ref5[k];
2497
if (!(param.splat || param instanceof Expansion)) {
2498
continue;
2499
}
2500
ref6 = this.params;
2501
for (l = 0, len3 = ref6.length; l < len3; l++) {
2502
p = ref6[l];
2503
if (!(p instanceof Expansion) && p.name.value) {
2504
o.scope.add(p.name.value, 'var', true);
2505
}
2506
}
2507
splats = new Assign(new Value(new Arr((function() {
2508
var len4, m, ref7, results;
2509
ref7 = this.params;
2510
results = [];
2511
for (m = 0, len4 = ref7.length; m < len4; m++) {
2512
p = ref7[m];
2513
results.push(p.asReference(o));
2514
}
2515
return results;
2516
}).call(this))), new Value(new IdentifierLiteral('arguments')));
2517
break;
2518
}
2519
ref7 = this.params;
2520
for (m = 0, len4 = ref7.length; m < len4; m++) {
2521
param = ref7[m];
2522
if (param.isComplex()) {
2523
val = ref = param.asReference(o);
2524
if (param.value) {
2525
val = new Op('?', ref, param.value);
2526
}
2527
exprs.push(new Assign(new Value(param.name), val, '=', {
2528
param: true
2529
}));
2530
} else {
2531
ref = param;
2532
if (param.value) {
2533
lit = new Literal(ref.name.value + ' == null');
2534
val = new Assign(new Value(param.name), param.value, '=');
2535
exprs.push(new If(lit, val));
2536
}
2537
}
2538
if (!splats) {
2539
params.push(ref);
2540
}
2541
}
2542
wasEmpty = this.body.isEmpty();
2543
if (splats) {
2544
exprs.unshift(splats);
2545
}
2546
if (exprs.length) {
2547
(ref8 = this.body.expressions).unshift.apply(ref8, exprs);
2548
}
2549
for (i = q = 0, len5 = params.length; q < len5; i = ++q) {
2550
p = params[i];
2551
params[i] = p.compileToFragments(o);
2552
o.scope.parameter(fragmentsToText(params[i]));
2553
}
2554
uniqs = [];
2555
this.eachParamName(function(name, node) {
2556
if (indexOf.call(uniqs, name) >= 0) {
2557
node.error("multiple parameters named " + name);
2558
}
2559
return uniqs.push(name);
2560
});
2561
if (!(wasEmpty || this.noReturn)) {
2562
this.body.makeReturn();
2563
}
2564
code = 'function';
2565
if (this.isGenerator) {
2566
code += '*';
2567
}
2568
if (this.ctor) {
2569
code += ' ' + this.name;
2570
}
2571
code += '(';
2572
answer = [this.makeCode(code)];
2573
for (i = r = 0, len6 = params.length; r < len6; i = ++r) {
2574
p = params[i];
2575
if (i) {
2576
answer.push(this.makeCode(", "));
2577
}
2578
answer.push.apply(answer, p);
2579
}
2580
answer.push(this.makeCode(') {'));
2581
if (!this.body.isEmpty()) {
2582
answer = answer.concat(this.makeCode("\n"), this.body.compileWithDeclarations(o), this.makeCode("\n" + this.tab));
2583
}
2584
answer.push(this.makeCode('}'));
2585
if (this.ctor) {
2586
return [this.makeCode(this.tab)].concat(slice.call(answer));
2587
}
2588
if (this.front || (o.level >= LEVEL_ACCESS)) {
2589
return this.wrapInBraces(answer);
2590
} else {
2591
return answer;
2592
}
2593
};
2594
2595
Code.prototype.eachParamName = function(iterator) {
2596
var j, len1, param, ref3, results;
2597
ref3 = this.params;
2598
results = [];
2599
for (j = 0, len1 = ref3.length; j < len1; j++) {
2600
param = ref3[j];
2601
results.push(param.eachName(iterator));
2602
}
2603
return results;
2604
};
2605
2606
Code.prototype.traverseChildren = function(crossScope, func) {
2607
if (crossScope) {
2608
return Code.__super__.traverseChildren.call(this, crossScope, func);
2609
}
2610
};
2611
2612
return Code;
2613
2614
})(Base);
2615
2616
exports.Param = Param = (function(superClass1) {
2617
extend1(Param, superClass1);
2618
2619
function Param(name1, value1, splat) {
2620
var message, token;
2621
this.name = name1;
2622
this.value = value1;
2623
this.splat = splat;
2624
message = isUnassignable(this.name.unwrapAll().value);
2625
if (message) {
2626
this.name.error(message);
2627
}
2628
if (this.name instanceof Obj && this.name.generated) {
2629
token = this.name.objects[0].operatorToken;
2630
token.error("unexpected " + token.value);
2631
}
2632
}
2633
2634
Param.prototype.children = ['name', 'value'];
2635
2636
Param.prototype.compileToFragments = function(o) {
2637
return this.name.compileToFragments(o, LEVEL_LIST);
2638
};
2639
2640
Param.prototype.asReference = function(o) {
2641
var name, node;
2642
if (this.reference) {
2643
return this.reference;
2644
}
2645
node = this.name;
2646
if (node["this"]) {
2647
name = node.properties[0].name.value;
2648
if (indexOf.call(JS_FORBIDDEN, name) >= 0) {
2649
name = "_" + name;
2650
}
2651
node = new IdentifierLiteral(o.scope.freeVariable(name));
2652
} else if (node.isComplex()) {
2653
node = new IdentifierLiteral(o.scope.freeVariable('arg'));
2654
}
2655
node = new Value(node);
2656
if (this.splat) {
2657
node = new Splat(node);
2658
}
2659
node.updateLocationDataIfMissing(this.locationData);
2660
return this.reference = node;
2661
};
2662
2663
Param.prototype.isComplex = function() {
2664
return this.name.isComplex();
2665
};
2666
2667
Param.prototype.eachName = function(iterator, name) {
2668
var atParam, j, len1, node, obj, ref3, ref4;
2669
if (name == null) {
2670
name = this.name;
2671
}
2672
atParam = function(obj) {
2673
return iterator("@" + obj.properties[0].name.value, obj);
2674
};
2675
if (name instanceof Literal) {
2676
return iterator(name.value, name);
2677
}
2678
if (name instanceof Value) {
2679
return atParam(name);
2680
}
2681
ref4 = (ref3 = name.objects) != null ? ref3 : [];
2682
for (j = 0, len1 = ref4.length; j < len1; j++) {
2683
obj = ref4[j];
2684
if (obj instanceof Assign && (obj.context == null)) {
2685
obj = obj.variable;
2686
}
2687
if (obj instanceof Assign) {
2688
if (obj.value instanceof Assign) {
2689
obj = obj.value;
2690
}
2691
this.eachName(iterator, obj.value.unwrap());
2692
} else if (obj instanceof Splat) {
2693
node = obj.name.unwrap();
2694
iterator(node.value, node);
2695
} else if (obj instanceof Value) {
2696
if (obj.isArray() || obj.isObject()) {
2697
this.eachName(iterator, obj.base);
2698
} else if (obj["this"]) {
2699
atParam(obj);
2700
} else {
2701
iterator(obj.base.value, obj.base);
2702
}
2703
} else if (!(obj instanceof Expansion)) {
2704
obj.error("illegal parameter " + (obj.compile()));
2705
}
2706
}
2707
};
2708
2709
return Param;
2710
2711
})(Base);
2712
2713
exports.Splat = Splat = (function(superClass1) {
2714
extend1(Splat, superClass1);
2715
2716
Splat.prototype.children = ['name'];
2717
2718
Splat.prototype.isAssignable = YES;
2719
2720
function Splat(name) {
2721
this.name = name.compile ? name : new Literal(name);
2722
}
2723
2724
Splat.prototype.assigns = function(name) {
2725
return this.name.assigns(name);
2726
};
2727
2728
Splat.prototype.compileToFragments = function(o) {
2729
return this.name.compileToFragments(o);
2730
};
2731
2732
Splat.prototype.unwrap = function() {
2733
return this.name;
2734
};
2735
2736
Splat.compileSplattedArray = function(o, list, apply) {
2737
var args, base, compiledNode, concatPart, fragments, i, index, j, last, len1, node;
2738
index = -1;
2739
while ((node = list[++index]) && !(node instanceof Splat)) {
2740
continue;
2741
}
2742
if (index >= list.length) {
2743
return [];
2744
}
2745
if (list.length === 1) {
2746
node = list[0];
2747
fragments = node.compileToFragments(o, LEVEL_LIST);
2748
if (apply) {
2749
return fragments;
2750
}
2751
return [].concat(node.makeCode((utility('slice', o)) + ".call("), fragments, node.makeCode(")"));
2752
}
2753
args = list.slice(index);
2754
for (i = j = 0, len1 = args.length; j < len1; i = ++j) {
2755
node = args[i];
2756
compiledNode = node.compileToFragments(o, LEVEL_LIST);
2757
args[i] = node instanceof Splat ? [].concat(node.makeCode((utility('slice', o)) + ".call("), compiledNode, node.makeCode(")")) : [].concat(node.makeCode("["), compiledNode, node.makeCode("]"));
2758
}
2759
if (index === 0) {
2760
node = list[0];
2761
concatPart = node.joinFragmentArrays(args.slice(1), ', ');
2762
return args[0].concat(node.makeCode(".concat("), concatPart, node.makeCode(")"));
2763
}
2764
base = (function() {
2765
var k, len2, ref3, results;
2766
ref3 = list.slice(0, index);
2767
results = [];
2768
for (k = 0, len2 = ref3.length; k < len2; k++) {
2769
node = ref3[k];
2770
results.push(node.compileToFragments(o, LEVEL_LIST));
2771
}
2772
return results;
2773
})();
2774
base = list[0].joinFragmentArrays(base, ', ');
2775
concatPart = list[index].joinFragmentArrays(args, ', ');
2776
last = list[list.length - 1];
2777
return [].concat(list[0].makeCode("["), base, list[index].makeCode("].concat("), concatPart, last.makeCode(")"));
2778
};
2779
2780
return Splat;
2781
2782
})(Base);
2783
2784
exports.Expansion = Expansion = (function(superClass1) {
2785
extend1(Expansion, superClass1);
2786
2787
function Expansion() {
2788
return Expansion.__super__.constructor.apply(this, arguments);
2789
}
2790
2791
Expansion.prototype.isComplex = NO;
2792
2793
Expansion.prototype.compileNode = function(o) {
2794
return this.error('Expansion must be used inside a destructuring assignment or parameter list');
2795
};
2796
2797
Expansion.prototype.asReference = function(o) {
2798
return this;
2799
};
2800
2801
Expansion.prototype.eachName = function(iterator) {};
2802
2803
return Expansion;
2804
2805
})(Base);
2806
2807
exports.While = While = (function(superClass1) {
2808
extend1(While, superClass1);
2809
2810
function While(condition, options) {
2811
this.condition = (options != null ? options.invert : void 0) ? condition.invert() : condition;
2812
this.guard = options != null ? options.guard : void 0;
2813
}
2814
2815
While.prototype.children = ['condition', 'guard', 'body'];
2816
2817
While.prototype.isStatement = YES;
2818
2819
While.prototype.makeReturn = function(res) {
2820
if (res) {
2821
return While.__super__.makeReturn.apply(this, arguments);
2822
} else {
2823
this.returns = !this.jumps({
2824
loop: true
2825
});
2826
return this;
2827
}
2828
};
2829
2830
While.prototype.addBody = function(body1) {
2831
this.body = body1;
2832
return this;
2833
};
2834
2835
While.prototype.jumps = function() {
2836
var expressions, j, jumpNode, len1, node;
2837
expressions = this.body.expressions;
2838
if (!expressions.length) {
2839
return false;
2840
}
2841
for (j = 0, len1 = expressions.length; j < len1; j++) {
2842
node = expressions[j];
2843
if (jumpNode = node.jumps({
2844
loop: true
2845
})) {
2846
return jumpNode;
2847
}
2848
}
2849
return false;
2850
};
2851
2852
While.prototype.compileNode = function(o) {
2853
var answer, body, rvar, set;
2854
o.indent += TAB;
2855
set = '';
2856
body = this.body;
2857
if (body.isEmpty()) {
2858
body = this.makeCode('');
2859
} else {
2860
if (this.returns) {
2861
body.makeReturn(rvar = o.scope.freeVariable('results'));
2862
set = "" + this.tab + rvar + " = [];\n";
2863
}
2864
if (this.guard) {
2865
if (body.expressions.length > 1) {
2866
body.expressions.unshift(new If((new Parens(this.guard)).invert(), new StatementLiteral("continue")));
2867
} else {
2868
if (this.guard) {
2869
body = Block.wrap([new If(this.guard, body)]);
2870
}
2871
}
2872
}
2873
body = [].concat(this.makeCode("\n"), body.compileToFragments(o, LEVEL_TOP), this.makeCode("\n" + this.tab));
2874
}
2875
answer = [].concat(this.makeCode(set + this.tab + "while ("), this.condition.compileToFragments(o, LEVEL_PAREN), this.makeCode(") {"), body, this.makeCode("}"));
2876
if (this.returns) {
2877
answer.push(this.makeCode("\n" + this.tab + "return " + rvar + ";"));
2878
}
2879
return answer;
2880
};
2881
2882
return While;
2883
2884
})(Base);
2885
2886
exports.Op = Op = (function(superClass1) {
2887
var CONVERSIONS, INVERSIONS;
2888
2889
extend1(Op, superClass1);
2890
2891
function Op(op, first, second, flip) {
2892
if (op === 'in') {
2893
return new In(first, second);
2894
}
2895
if (op === 'do') {
2896
return this.generateDo(first);
2897
}
2898
if (op === 'new') {
2899
if (first instanceof Call && !first["do"] && !first.isNew) {
2900
return first.newInstance();
2901
}
2902
if (first instanceof Code && first.bound || first["do"]) {
2903
first = new Parens(first);
2904
}
2905
}
2906
this.operator = CONVERSIONS[op] || op;
2907
this.first = first;
2908
this.second = second;
2909
this.flip = !!flip;
2910
return this;
2911
}
2912
2913
CONVERSIONS = {
2914
'==': '===',
2915
'!=': '!==',
2916
'of': 'in',
2917
'yieldfrom': 'yield*'
2918
};
2919
2920
INVERSIONS = {
2921
'!==': '===',
2922
'===': '!=='
2923
};
2924
2925
Op.prototype.children = ['first', 'second'];
2926
2927
Op.prototype.isNumber = function() {
2928
var ref3;
2929
return this.isUnary() && ((ref3 = this.operator) === '+' || ref3 === '-') && this.first instanceof Value && this.first.isNumber();
2930
};
2931
2932
Op.prototype.isYield = function() {
2933
var ref3;
2934
return (ref3 = this.operator) === 'yield' || ref3 === 'yield*';
2935
};
2936
2937
Op.prototype.isUnary = function() {
2938
return !this.second;
2939
};
2940
2941
Op.prototype.isComplex = function() {
2942
return !this.isNumber();
2943
};
2944
2945
Op.prototype.isChainable = function() {
2946
var ref3;
2947
return (ref3 = this.operator) === '<' || ref3 === '>' || ref3 === '>=' || ref3 === '<=' || ref3 === '===' || ref3 === '!==';
2948
};
2949
2950
Op.prototype.invert = function() {
2951
var allInvertable, curr, fst, op, ref3;
2952
if (this.isChainable() && this.first.isChainable()) {
2953
allInvertable = true;
2954
curr = this;
2955
while (curr && curr.operator) {
2956
allInvertable && (allInvertable = curr.operator in INVERSIONS);
2957
curr = curr.first;
2958
}
2959
if (!allInvertable) {
2960
return new Parens(this).invert();
2961
}
2962
curr = this;
2963
while (curr && curr.operator) {
2964
curr.invert = !curr.invert;
2965
curr.operator = INVERSIONS[curr.operator];
2966
curr = curr.first;
2967
}
2968
return this;
2969
} else if (op = INVERSIONS[this.operator]) {
2970
this.operator = op;
2971
if (this.first.unwrap() instanceof Op) {
2972
this.first.invert();
2973
}
2974
return this;
2975
} else if (this.second) {
2976
return new Parens(this).invert();
2977
} else if (this.operator === '!' && (fst = this.first.unwrap()) instanceof Op && ((ref3 = fst.operator) === '!' || ref3 === 'in' || ref3 === 'instanceof')) {
2978
return fst;
2979
} else {
2980
return new Op('!', this);
2981
}
2982
};
2983
2984
Op.prototype.unfoldSoak = function(o) {
2985
var ref3;
2986
return ((ref3 = this.operator) === '++' || ref3 === '--' || ref3 === 'delete') && unfoldSoak(o, this, 'first');
2987
};
2988
2989
Op.prototype.generateDo = function(exp) {
2990
var call, func, j, len1, param, passedParams, ref, ref3;
2991
passedParams = [];
2992
func = exp instanceof Assign && (ref = exp.value.unwrap()) instanceof Code ? ref : exp;
2993
ref3 = func.params || [];
2994
for (j = 0, len1 = ref3.length; j < len1; j++) {
2995
param = ref3[j];
2996
if (param.value) {
2997
passedParams.push(param.value);
2998
delete param.value;
2999
} else {
3000
passedParams.push(param);
3001
}
3002
}
3003
call = new Call(exp, passedParams);
3004
call["do"] = true;
3005
return call;
3006
};
3007
3008
Op.prototype.compileNode = function(o) {
3009
var answer, isChain, lhs, message, ref3, rhs;
3010
isChain = this.isChainable() && this.first.isChainable();
3011
if (!isChain) {
3012
this.first.front = this.front;
3013
}
3014
if (this.operator === 'delete' && o.scope.check(this.first.unwrapAll().value)) {
3015
this.error('delete operand may not be argument or var');
3016
}
3017
if ((ref3 = this.operator) === '--' || ref3 === '++') {
3018
message = isUnassignable(this.first.unwrapAll().value);
3019
if (message) {
3020
this.first.error(message);
3021
}
3022
}
3023
if (this.isYield()) {
3024
return this.compileYield(o);
3025
}
3026
if (this.isUnary()) {
3027
return this.compileUnary(o);
3028
}
3029
if (isChain) {
3030
return this.compileChain(o);
3031
}
3032
switch (this.operator) {
3033
case '?':
3034
return this.compileExistence(o);
3035
case '**':
3036
return this.compilePower(o);
3037
case '//':
3038
return this.compileFloorDivision(o);
3039
case '%%':
3040
return this.compileModulo(o);
3041
default:
3042
lhs = this.first.compileToFragments(o, LEVEL_OP);
3043
rhs = this.second.compileToFragments(o, LEVEL_OP);
3044
answer = [].concat(lhs, this.makeCode(" " + this.operator + " "), rhs);
3045
if (o.level <= LEVEL_OP) {
3046
return answer;
3047
} else {
3048
return this.wrapInBraces(answer);
3049
}
3050
}
3051
};
3052
3053
Op.prototype.compileChain = function(o) {
3054
var fragments, fst, ref3, shared;
3055
ref3 = this.first.second.cache(o), this.first.second = ref3[0], shared = ref3[1];
3056
fst = this.first.compileToFragments(o, LEVEL_OP);
3057
fragments = fst.concat(this.makeCode(" " + (this.invert ? '&&' : '||') + " "), shared.compileToFragments(o), this.makeCode(" " + this.operator + " "), this.second.compileToFragments(o, LEVEL_OP));
3058
return this.wrapInBraces(fragments);
3059
};
3060
3061
Op.prototype.compileExistence = function(o) {
3062
var fst, ref;
3063
if (this.first.isComplex()) {
3064
ref = new IdentifierLiteral(o.scope.freeVariable('ref'));
3065
fst = new Parens(new Assign(ref, this.first));
3066
} else {
3067
fst = this.first;
3068
ref = fst;
3069
}
3070
return new If(new Existence(fst), ref, {
3071
type: 'if'
3072
}).addElse(this.second).compileToFragments(o);
3073
};
3074
3075
Op.prototype.compileUnary = function(o) {
3076
var op, parts, plusMinus;
3077
parts = [];
3078
op = this.operator;
3079
parts.push([this.makeCode(op)]);
3080
if (op === '!' && this.first instanceof Existence) {
3081
this.first.negated = !this.first.negated;
3082
return this.first.compileToFragments(o);
3083
}
3084
if (o.level >= LEVEL_ACCESS) {
3085
return (new Parens(this)).compileToFragments(o);
3086
}
3087
plusMinus = op === '+' || op === '-';
3088
if ((op === 'new' || op === 'typeof' || op === 'delete') || plusMinus && this.first instanceof Op && this.first.operator === op) {
3089
parts.push([this.makeCode(' ')]);
3090
}
3091
if ((plusMinus && this.first instanceof Op) || (op === 'new' && this.first.isStatement(o))) {
3092
this.first = new Parens(this.first);
3093
}
3094
parts.push(this.first.compileToFragments(o, LEVEL_OP));
3095
if (this.flip) {
3096
parts.reverse();
3097
}
3098
return this.joinFragmentArrays(parts, '');
3099
};
3100
3101
Op.prototype.compileYield = function(o) {
3102
var op, parts, ref3;
3103
parts = [];
3104
op = this.operator;
3105
if (o.scope.parent == null) {
3106
this.error('yield can only occur inside functions');
3107
}
3108
if (indexOf.call(Object.keys(this.first), 'expression') >= 0 && !(this.first instanceof Throw)) {
3109
if (this.first.expression != null) {
3110
parts.push(this.first.expression.compileToFragments(o, LEVEL_OP));
3111
}
3112
} else {
3113
if (o.level >= LEVEL_PAREN) {
3114
parts.push([this.makeCode("(")]);
3115
}
3116
parts.push([this.makeCode(op)]);
3117
if (((ref3 = this.first.base) != null ? ref3.value : void 0) !== '') {
3118
parts.push([this.makeCode(" ")]);
3119
}
3120
parts.push(this.first.compileToFragments(o, LEVEL_OP));
3121
if (o.level >= LEVEL_PAREN) {
3122
parts.push([this.makeCode(")")]);
3123
}
3124
}
3125
return this.joinFragmentArrays(parts, '');
3126
};
3127
3128
Op.prototype.compilePower = function(o) {
3129
var pow;
3130
pow = new Value(new IdentifierLiteral('Math'), [new Access(new PropertyName('pow'))]);
3131
return new Call(pow, [this.first, this.second]).compileToFragments(o);
3132
};
3133
3134
Op.prototype.compileFloorDivision = function(o) {
3135
var div, floor, second;
3136
floor = new Value(new IdentifierLiteral('Math'), [new Access(new PropertyName('floor'))]);
3137
second = this.second.isComplex() ? new Parens(this.second) : this.second;
3138
div = new Op('/', this.first, second);
3139
return new Call(floor, [div]).compileToFragments(o);
3140
};
3141
3142
Op.prototype.compileModulo = function(o) {
3143
var mod;
3144
mod = new Value(new Literal(utility('modulo', o)));
3145
return new Call(mod, [this.first, this.second]).compileToFragments(o);
3146
};
3147
3148
Op.prototype.toString = function(idt) {
3149
return Op.__super__.toString.call(this, idt, this.constructor.name + ' ' + this.operator);
3150
};
3151
3152
return Op;
3153
3154
})(Base);
3155
3156
exports.In = In = (function(superClass1) {
3157
extend1(In, superClass1);
3158
3159
function In(object, array) {
3160
this.object = object;
3161
this.array = array;
3162
}
3163
3164
In.prototype.children = ['object', 'array'];
3165
3166
In.prototype.invert = NEGATE;
3167
3168
In.prototype.compileNode = function(o) {
3169
var hasSplat, j, len1, obj, ref3;
3170
if (this.array instanceof Value && this.array.isArray() && this.array.base.objects.length) {
3171
ref3 = this.array.base.objects;
3172
for (j = 0, len1 = ref3.length; j < len1; j++) {
3173
obj = ref3[j];
3174
if (!(obj instanceof Splat)) {
3175
continue;
3176
}
3177
hasSplat = true;
3178
break;
3179
}
3180
if (!hasSplat) {
3181
return this.compileOrTest(o);
3182
}
3183
}
3184
return this.compileLoopTest(o);
3185
};
3186
3187
In.prototype.compileOrTest = function(o) {
3188
var cmp, cnj, i, item, j, len1, ref, ref3, ref4, ref5, sub, tests;
3189
ref3 = this.object.cache(o, LEVEL_OP), sub = ref3[0], ref = ref3[1];
3190
ref4 = this.negated ? [' !== ', ' && '] : [' === ', ' || '], cmp = ref4[0], cnj = ref4[1];
3191
tests = [];
3192
ref5 = this.array.base.objects;
3193
for (i = j = 0, len1 = ref5.length; j < len1; i = ++j) {
3194
item = ref5[i];
3195
if (i) {
3196
tests.push(this.makeCode(cnj));
3197
}
3198
tests = tests.concat((i ? ref : sub), this.makeCode(cmp), item.compileToFragments(o, LEVEL_ACCESS));
3199
}
3200
if (o.level < LEVEL_OP) {
3201
return tests;
3202
} else {
3203
return this.wrapInBraces(tests);
3204
}
3205
};
3206
3207
In.prototype.compileLoopTest = function(o) {
3208
var fragments, ref, ref3, sub;
3209
ref3 = this.object.cache(o, LEVEL_LIST), sub = ref3[0], ref = ref3[1];
3210
fragments = [].concat(this.makeCode(utility('indexOf', o) + ".call("), this.array.compileToFragments(o, LEVEL_LIST), this.makeCode(", "), ref, this.makeCode(") " + (this.negated ? '< 0' : '>= 0')));
3211
if (fragmentsToText(sub) === fragmentsToText(ref)) {
3212
return fragments;
3213
}
3214
fragments = sub.concat(this.makeCode(', '), fragments);
3215
if (o.level < LEVEL_LIST) {
3216
return fragments;
3217
} else {
3218
return this.wrapInBraces(fragments);
3219
}
3220
};
3221
3222
In.prototype.toString = function(idt) {
3223
return In.__super__.toString.call(this, idt, this.constructor.name + (this.negated ? '!' : ''));
3224
};
3225
3226
return In;
3227
3228
})(Base);
3229
3230
exports.Try = Try = (function(superClass1) {
3231
extend1(Try, superClass1);
3232
3233
function Try(attempt, errorVariable, recovery, ensure) {
3234
this.attempt = attempt;
3235
this.errorVariable = errorVariable;
3236
this.recovery = recovery;
3237
this.ensure = ensure;
3238
}
3239
3240
Try.prototype.children = ['attempt', 'recovery', 'ensure'];
3241
3242
Try.prototype.isStatement = YES;
3243
3244
Try.prototype.jumps = function(o) {
3245
var ref3;
3246
return this.attempt.jumps(o) || ((ref3 = this.recovery) != null ? ref3.jumps(o) : void 0);
3247
};
3248
3249
Try.prototype.makeReturn = function(res) {
3250
if (this.attempt) {
3251
this.attempt = this.attempt.makeReturn(res);
3252
}
3253
if (this.recovery) {
3254
this.recovery = this.recovery.makeReturn(res);
3255
}
3256
return this;
3257
};
3258
3259
Try.prototype.compileNode = function(o) {
3260
var catchPart, ensurePart, generatedErrorVariableName, message, placeholder, tryPart;
3261
o.indent += TAB;
3262
tryPart = this.attempt.compileToFragments(o, LEVEL_TOP);
3263
catchPart = this.recovery ? (generatedErrorVariableName = o.scope.freeVariable('error', {
3264
reserve: false
3265
}), placeholder = new IdentifierLiteral(generatedErrorVariableName), this.errorVariable ? (message = isUnassignable(this.errorVariable.unwrapAll().value), message ? this.errorVariable.error(message) : void 0, this.recovery.unshift(new Assign(this.errorVariable, placeholder))) : void 0, [].concat(this.makeCode(" catch ("), placeholder.compileToFragments(o), this.makeCode(") {\n"), this.recovery.compileToFragments(o, LEVEL_TOP), this.makeCode("\n" + this.tab + "}"))) : !(this.ensure || this.recovery) ? (generatedErrorVariableName = o.scope.freeVariable('error', {
3266
reserve: false
3267
}), [this.makeCode(" catch (" + generatedErrorVariableName + ") {}")]) : [];
3268
ensurePart = this.ensure ? [].concat(this.makeCode(" finally {\n"), this.ensure.compileToFragments(o, LEVEL_TOP), this.makeCode("\n" + this.tab + "}")) : [];
3269
return [].concat(this.makeCode(this.tab + "try {\n"), tryPart, this.makeCode("\n" + this.tab + "}"), catchPart, ensurePart);
3270
};
3271
3272
return Try;
3273
3274
})(Base);
3275
3276
exports.Throw = Throw = (function(superClass1) {
3277
extend1(Throw, superClass1);
3278
3279
function Throw(expression) {
3280
this.expression = expression;
3281
}
3282
3283
Throw.prototype.children = ['expression'];
3284
3285
Throw.prototype.isStatement = YES;
3286
3287
Throw.prototype.jumps = NO;
3288
3289
Throw.prototype.makeReturn = THIS;
3290
3291
Throw.prototype.compileNode = function(o) {
3292
return [].concat(this.makeCode(this.tab + "throw "), this.expression.compileToFragments(o), this.makeCode(";"));
3293
};
3294
3295
return Throw;
3296
3297
})(Base);
3298
3299
exports.Existence = Existence = (function(superClass1) {
3300
extend1(Existence, superClass1);
3301
3302
function Existence(expression) {
3303
this.expression = expression;
3304
}
3305
3306
Existence.prototype.children = ['expression'];
3307
3308
Existence.prototype.invert = NEGATE;
3309
3310
Existence.prototype.compileNode = function(o) {
3311
var cmp, cnj, code, ref3;
3312
this.expression.front = this.front;
3313
code = this.expression.compile(o, LEVEL_OP);
3314
if (this.expression.unwrap() instanceof IdentifierLiteral && !o.scope.check(code)) {
3315
ref3 = this.negated ? ['===', '||'] : ['!==', '&&'], cmp = ref3[0], cnj = ref3[1];
3316
code = "typeof " + code + " " + cmp + " \"undefined\" " + cnj + " " + code + " " + cmp + " null";
3317
} else {
3318
code = code + " " + (this.negated ? '==' : '!=') + " null";
3319
}
3320
return [this.makeCode(o.level <= LEVEL_COND ? code : "(" + code + ")")];
3321
};
3322
3323
return Existence;
3324
3325
})(Base);
3326
3327
exports.Parens = Parens = (function(superClass1) {
3328
extend1(Parens, superClass1);
3329
3330
function Parens(body1) {
3331
this.body = body1;
3332
}
3333
3334
Parens.prototype.children = ['body'];
3335
3336
Parens.prototype.unwrap = function() {
3337
return this.body;
3338
};
3339
3340
Parens.prototype.isComplex = function() {
3341
return this.body.isComplex();
3342
};
3343
3344
Parens.prototype.compileNode = function(o) {
3345
var bare, expr, fragments;
3346
expr = this.body.unwrap();
3347
if (expr instanceof Value && expr.isAtomic()) {
3348
expr.front = this.front;
3349
return expr.compileToFragments(o);
3350
}
3351
fragments = expr.compileToFragments(o, LEVEL_PAREN);
3352
bare = o.level < LEVEL_OP && (expr instanceof Op || expr instanceof Call || (expr instanceof For && expr.returns)) && (o.level < LEVEL_COND || fragments.length <= 3);
3353
if (bare) {
3354
return fragments;
3355
} else {
3356
return this.wrapInBraces(fragments);
3357
}
3358
};
3359
3360
return Parens;
3361
3362
})(Base);
3363
3364
exports.StringWithInterpolations = StringWithInterpolations = (function(superClass1) {
3365
extend1(StringWithInterpolations, superClass1);
3366
3367
function StringWithInterpolations() {
3368
return StringWithInterpolations.__super__.constructor.apply(this, arguments);
3369
}
3370
3371
StringWithInterpolations.prototype.compileNode = function(o) {
3372
var element, elements, expr, fragments, j, len1, value;
3373
if (!o.inTaggedTemplateCall) {
3374
return StringWithInterpolations.__super__.compileNode.apply(this, arguments);
3375
}
3376
expr = this.body.unwrap();
3377
elements = [];
3378
expr.traverseChildren(false, function(node) {
3379
if (node instanceof StringLiteral) {
3380
elements.push(node);
3381
return true;
3382
} else if (node instanceof Parens) {
3383
elements.push(node);
3384
return false;
3385
}
3386
return true;
3387
});
3388
fragments = [];
3389
fragments.push(this.makeCode('`'));
3390
for (j = 0, len1 = elements.length; j < len1; j++) {
3391
element = elements[j];
3392
if (element instanceof StringLiteral) {
3393
value = element.value.slice(1, -1);
3394
value = value.replace(/(\\*)(`|\$\{)/g, function(match, backslashes, toBeEscaped) {
3395
if (backslashes.length % 2 === 0) {
3396
return backslashes + "\\" + toBeEscaped;
3397
} else {
3398
return match;
3399
}
3400
});
3401
fragments.push(this.makeCode(value));
3402
} else {
3403
fragments.push(this.makeCode('${'));
3404
fragments.push.apply(fragments, element.compileToFragments(o, LEVEL_PAREN));
3405
fragments.push(this.makeCode('}'));
3406
}
3407
}
3408
fragments.push(this.makeCode('`'));
3409
return fragments;
3410
};
3411
3412
return StringWithInterpolations;
3413
3414
})(Parens);
3415
3416
exports.For = For = (function(superClass1) {
3417
extend1(For, superClass1);
3418
3419
function For(body, source) {
3420
var ref3;
3421
this.source = source.source, this.guard = source.guard, this.step = source.step, this.name = source.name, this.index = source.index;
3422
this.body = Block.wrap([body]);
3423
this.own = !!source.own;
3424
this.object = !!source.object;
3425
this.from = !!source.from;
3426
if (this.from && this.index) {
3427
this.index.error('cannot use index with for-from');
3428
}
3429
if (this.own && !this.object) {
3430
source.ownTag.error("cannot use own with for-" + (this.from ? 'from' : 'in'));
3431
}
3432
if (this.object) {
3433
ref3 = [this.index, this.name], this.name = ref3[0], this.index = ref3[1];
3434
}
3435
if (this.index instanceof Value && !this.index.isAssignable()) {
3436
this.index.error('index cannot be a pattern matching expression');
3437
}
3438
this.range = this.source instanceof Value && this.source.base instanceof Range && !this.source.properties.length && !this.from;
3439
this.pattern = this.name instanceof Value;
3440
if (this.range && this.index) {
3441
this.index.error('indexes do not apply to range loops');
3442
}
3443
if (this.range && this.pattern) {
3444
this.name.error('cannot pattern match over range loops');
3445
}
3446
this.returns = false;
3447
}
3448
3449
For.prototype.children = ['body', 'source', 'guard', 'step'];
3450
3451
For.prototype.compileNode = function(o) {
3452
var body, bodyFragments, compare, compareDown, declare, declareDown, defPart, defPartFragments, down, forPartFragments, guardPart, idt1, increment, index, ivar, kvar, kvarAssign, last, lvar, name, namePart, ref, ref3, ref4, resultPart, returnResult, rvar, scope, source, step, stepNum, stepVar, svar, varPart;
3453
body = Block.wrap([this.body]);
3454
ref3 = body.expressions, last = ref3[ref3.length - 1];
3455
if ((last != null ? last.jumps() : void 0) instanceof Return) {
3456
this.returns = false;
3457
}
3458
source = this.range ? this.source.base : this.source;
3459
scope = o.scope;
3460
if (!this.pattern) {
3461
name = this.name && (this.name.compile(o, LEVEL_LIST));
3462
}
3463
index = this.index && (this.index.compile(o, LEVEL_LIST));
3464
if (name && !this.pattern) {
3465
scope.find(name);
3466
}
3467
if (index && !(this.index instanceof Value)) {
3468
scope.find(index);
3469
}
3470
if (this.returns) {
3471
rvar = scope.freeVariable('results');
3472
}
3473
if (this.from) {
3474
if (this.pattern) {
3475
ivar = scope.freeVariable('x', {
3476
single: true
3477
});
3478
}
3479
} else {
3480
ivar = (this.object && index) || scope.freeVariable('i', {
3481
single: true
3482
});
3483
}
3484
kvar = ((this.range || this.from) && name) || index || ivar;
3485
kvarAssign = kvar !== ivar ? kvar + " = " : "";
3486
if (this.step && !this.range) {
3487
ref4 = this.cacheToCodeFragments(this.step.cache(o, LEVEL_LIST, isComplexOrAssignable)), step = ref4[0], stepVar = ref4[1];
3488
if (this.step.isNumber()) {
3489
stepNum = Number(stepVar);
3490
}
3491
}
3492
if (this.pattern) {
3493
name = ivar;
3494
}
3495
varPart = '';
3496
guardPart = '';
3497
defPart = '';
3498
idt1 = this.tab + TAB;
3499
if (this.range) {
3500
forPartFragments = source.compileToFragments(merge(o, {
3501
index: ivar,
3502
name: name,
3503
step: this.step,
3504
isComplex: isComplexOrAssignable
3505
}));
3506
} else {
3507
svar = this.source.compile(o, LEVEL_LIST);
3508
if ((name || this.own) && !(this.source.unwrap() instanceof IdentifierLiteral)) {
3509
defPart += "" + this.tab + (ref = scope.freeVariable('ref')) + " = " + svar + ";\n";
3510
svar = ref;
3511
}
3512
if (name && !this.pattern && !this.from) {
3513
namePart = name + " = " + svar + "[" + kvar + "]";
3514
}
3515
if (!this.object && !this.from) {
3516
if (step !== stepVar) {
3517
defPart += "" + this.tab + step + ";\n";
3518
}
3519
down = stepNum < 0;
3520
if (!(this.step && (stepNum != null) && down)) {
3521
lvar = scope.freeVariable('len');
3522
}
3523
declare = "" + kvarAssign + ivar + " = 0, " + lvar + " = " + svar + ".length";
3524
declareDown = "" + kvarAssign + ivar + " = " + svar + ".length - 1";
3525
compare = ivar + " < " + lvar;
3526
compareDown = ivar + " >= 0";
3527
if (this.step) {
3528
if (stepNum != null) {
3529
if (down) {
3530
compare = compareDown;
3531
declare = declareDown;
3532
}
3533
} else {
3534
compare = stepVar + " > 0 ? " + compare + " : " + compareDown;
3535
declare = "(" + stepVar + " > 0 ? (" + declare + ") : " + declareDown + ")";
3536
}
3537
increment = ivar + " += " + stepVar;
3538
} else {
3539
increment = "" + (kvar !== ivar ? "++" + ivar : ivar + "++");
3540
}
3541
forPartFragments = [this.makeCode(declare + "; " + compare + "; " + kvarAssign + increment)];
3542
}
3543
}
3544
if (this.returns) {
3545
resultPart = "" + this.tab + rvar + " = [];\n";
3546
returnResult = "\n" + this.tab + "return " + rvar + ";";
3547
body.makeReturn(rvar);
3548
}
3549
if (this.guard) {
3550
if (body.expressions.length > 1) {
3551
body.expressions.unshift(new If((new Parens(this.guard)).invert(), new StatementLiteral("continue")));
3552
} else {
3553
if (this.guard) {
3554
body = Block.wrap([new If(this.guard, body)]);
3555
}
3556
}
3557
}
3558
if (this.pattern) {
3559
body.expressions.unshift(new Assign(this.name, this.from ? new IdentifierLiteral(kvar) : new Literal(svar + "[" + kvar + "]")));
3560
}
3561
defPartFragments = [].concat(this.makeCode(defPart), this.pluckDirectCall(o, body));
3562
if (namePart) {
3563
varPart = "\n" + idt1 + namePart + ";";
3564
}
3565
if (this.object) {
3566
forPartFragments = [this.makeCode(kvar + " in " + svar)];
3567
if (this.own) {
3568
guardPart = "\n" + idt1 + "if (!" + (utility('hasProp', o)) + ".call(" + svar + ", " + kvar + ")) continue;";
3569
}
3570
} else if (this.from) {
3571
forPartFragments = [this.makeCode(kvar + " of " + svar)];
3572
}
3573
bodyFragments = body.compileToFragments(merge(o, {
3574
indent: idt1
3575
}), LEVEL_TOP);
3576
if (bodyFragments && bodyFragments.length > 0) {
3577
bodyFragments = [].concat(this.makeCode("\n"), bodyFragments, this.makeCode("\n"));
3578
}
3579
return [].concat(defPartFragments, this.makeCode("" + (resultPart || '') + this.tab + "for ("), forPartFragments, this.makeCode(") {" + guardPart + varPart), bodyFragments, this.makeCode(this.tab + "}" + (returnResult || '')));
3580
};
3581
3582
For.prototype.pluckDirectCall = function(o, body) {
3583
var base, defs, expr, fn, idx, j, len1, ref, ref3, ref4, ref5, ref6, ref7, ref8, ref9, val;
3584
defs = [];
3585
ref3 = body.expressions;
3586
for (idx = j = 0, len1 = ref3.length; j < len1; idx = ++j) {
3587
expr = ref3[idx];
3588
expr = expr.unwrapAll();
3589
if (!(expr instanceof Call)) {
3590
continue;
3591
}
3592
val = (ref4 = expr.variable) != null ? ref4.unwrapAll() : void 0;
3593
if (!((val instanceof Code) || (val instanceof Value && ((ref5 = val.base) != null ? ref5.unwrapAll() : void 0) instanceof Code && val.properties.length === 1 && ((ref6 = (ref7 = val.properties[0].name) != null ? ref7.value : void 0) === 'call' || ref6 === 'apply')))) {
3594
continue;
3595
}
3596
fn = ((ref8 = val.base) != null ? ref8.unwrapAll() : void 0) || val;
3597
ref = new IdentifierLiteral(o.scope.freeVariable('fn'));
3598
base = new Value(ref);
3599
if (val.base) {
3600
ref9 = [base, val], val.base = ref9[0], base = ref9[1];
3601
}
3602
body.expressions[idx] = new Call(base, expr.args);
3603
defs = defs.concat(this.makeCode(this.tab), new Assign(ref, fn).compileToFragments(o, LEVEL_TOP), this.makeCode(';\n'));
3604
}
3605
return defs;
3606
};
3607
3608
return For;
3609
3610
})(While);
3611
3612
exports.Switch = Switch = (function(superClass1) {
3613
extend1(Switch, superClass1);
3614
3615
function Switch(subject, cases, otherwise) {
3616
this.subject = subject;
3617
this.cases = cases;
3618
this.otherwise = otherwise;
3619
}
3620
3621
Switch.prototype.children = ['subject', 'cases', 'otherwise'];
3622
3623
Switch.prototype.isStatement = YES;
3624
3625
Switch.prototype.jumps = function(o) {
3626
var block, conds, j, jumpNode, len1, ref3, ref4, ref5;
3627
if (o == null) {
3628
o = {
3629
block: true
3630
};
3631
}
3632
ref3 = this.cases;
3633
for (j = 0, len1 = ref3.length; j < len1; j++) {
3634
ref4 = ref3[j], conds = ref4[0], block = ref4[1];
3635
if (jumpNode = block.jumps(o)) {
3636
return jumpNode;
3637
}
3638
}
3639
return (ref5 = this.otherwise) != null ? ref5.jumps(o) : void 0;
3640
};
3641
3642
Switch.prototype.makeReturn = function(res) {
3643
var j, len1, pair, ref3, ref4;
3644
ref3 = this.cases;
3645
for (j = 0, len1 = ref3.length; j < len1; j++) {
3646
pair = ref3[j];
3647
pair[1].makeReturn(res);
3648
}
3649
if (res) {
3650
this.otherwise || (this.otherwise = new Block([new Literal('void 0')]));
3651
}
3652
if ((ref4 = this.otherwise) != null) {
3653
ref4.makeReturn(res);
3654
}
3655
return this;
3656
};
3657
3658
Switch.prototype.compileNode = function(o) {
3659
var block, body, cond, conditions, expr, fragments, i, idt1, idt2, j, k, len1, len2, ref3, ref4, ref5;
3660
idt1 = o.indent + TAB;
3661
idt2 = o.indent = idt1 + TAB;
3662
fragments = [].concat(this.makeCode(this.tab + "switch ("), (this.subject ? this.subject.compileToFragments(o, LEVEL_PAREN) : this.makeCode("false")), this.makeCode(") {\n"));
3663
ref3 = this.cases;
3664
for (i = j = 0, len1 = ref3.length; j < len1; i = ++j) {
3665
ref4 = ref3[i], conditions = ref4[0], block = ref4[1];
3666
ref5 = flatten([conditions]);
3667
for (k = 0, len2 = ref5.length; k < len2; k++) {
3668
cond = ref5[k];
3669
if (!this.subject) {
3670
cond = cond.invert();
3671
}
3672
fragments = fragments.concat(this.makeCode(idt1 + "case "), cond.compileToFragments(o, LEVEL_PAREN), this.makeCode(":\n"));
3673
}
3674
if ((body = block.compileToFragments(o, LEVEL_TOP)).length > 0) {
3675
fragments = fragments.concat(body, this.makeCode('\n'));
3676
}
3677
if (i === this.cases.length - 1 && !this.otherwise) {
3678
break;
3679
}
3680
expr = this.lastNonComment(block.expressions);
3681
if (expr instanceof Return || (expr instanceof Literal && expr.jumps() && expr.value !== 'debugger')) {
3682
continue;
3683
}
3684
fragments.push(cond.makeCode(idt2 + 'break;\n'));
3685
}
3686
if (this.otherwise && this.otherwise.expressions.length) {
3687
fragments.push.apply(fragments, [this.makeCode(idt1 + "default:\n")].concat(slice.call(this.otherwise.compileToFragments(o, LEVEL_TOP)), [this.makeCode("\n")]));
3688
}
3689
fragments.push(this.makeCode(this.tab + '}'));
3690
return fragments;
3691
};
3692
3693
return Switch;
3694
3695
})(Base);
3696
3697
exports.If = If = (function(superClass1) {
3698
extend1(If, superClass1);
3699
3700
function If(condition, body1, options) {
3701
this.body = body1;
3702
if (options == null) {
3703
options = {};
3704
}
3705
this.condition = options.type === 'unless' ? condition.invert() : condition;
3706
this.elseBody = null;
3707
this.isChain = false;
3708
this.soak = options.soak;
3709
}
3710
3711
If.prototype.children = ['condition', 'body', 'elseBody'];
3712
3713
If.prototype.bodyNode = function() {
3714
var ref3;
3715
return (ref3 = this.body) != null ? ref3.unwrap() : void 0;
3716
};
3717
3718
If.prototype.elseBodyNode = function() {
3719
var ref3;
3720
return (ref3 = this.elseBody) != null ? ref3.unwrap() : void 0;
3721
};
3722
3723
If.prototype.addElse = function(elseBody) {
3724
if (this.isChain) {
3725
this.elseBodyNode().addElse(elseBody);
3726
} else {
3727
this.isChain = elseBody instanceof If;
3728
this.elseBody = this.ensureBlock(elseBody);
3729
this.elseBody.updateLocationDataIfMissing(elseBody.locationData);
3730
}
3731
return this;
3732
};
3733
3734
If.prototype.isStatement = function(o) {
3735
var ref3;
3736
return (o != null ? o.level : void 0) === LEVEL_TOP || this.bodyNode().isStatement(o) || ((ref3 = this.elseBodyNode()) != null ? ref3.isStatement(o) : void 0);
3737
};
3738
3739
If.prototype.jumps = function(o) {
3740
var ref3;
3741
return this.body.jumps(o) || ((ref3 = this.elseBody) != null ? ref3.jumps(o) : void 0);
3742
};
3743
3744
If.prototype.compileNode = function(o) {
3745
if (this.isStatement(o)) {
3746
return this.compileStatement(o);
3747
} else {
3748
return this.compileExpression(o);
3749
}
3750
};
3751
3752
If.prototype.makeReturn = function(res) {
3753
if (res) {
3754
this.elseBody || (this.elseBody = new Block([new Literal('void 0')]));
3755
}
3756
this.body && (this.body = new Block([this.body.makeReturn(res)]));
3757
this.elseBody && (this.elseBody = new Block([this.elseBody.makeReturn(res)]));
3758
return this;
3759
};
3760
3761
If.prototype.ensureBlock = function(node) {
3762
if (node instanceof Block) {
3763
return node;
3764
} else {
3765
return new Block([node]);
3766
}
3767
};
3768
3769
If.prototype.compileStatement = function(o) {
3770
var answer, body, child, cond, exeq, ifPart, indent;
3771
child = del(o, 'chainChild');
3772
exeq = del(o, 'isExistentialEquals');
3773
if (exeq) {
3774
return new If(this.condition.invert(), this.elseBodyNode(), {
3775
type: 'if'
3776
}).compileToFragments(o);
3777
}
3778
indent = o.indent + TAB;
3779
cond = this.condition.compileToFragments(o, LEVEL_PAREN);
3780
body = this.ensureBlock(this.body).compileToFragments(merge(o, {
3781
indent: indent
3782
}));
3783
ifPart = [].concat(this.makeCode("if ("), cond, this.makeCode(") {\n"), body, this.makeCode("\n" + this.tab + "}"));
3784
if (!child) {
3785
ifPart.unshift(this.makeCode(this.tab));
3786
}
3787
if (!this.elseBody) {
3788
return ifPart;
3789
}
3790
answer = ifPart.concat(this.makeCode(' else '));
3791
if (this.isChain) {
3792
o.chainChild = true;
3793
answer = answer.concat(this.elseBody.unwrap().compileToFragments(o, LEVEL_TOP));
3794
} else {
3795
answer = answer.concat(this.makeCode("{\n"), this.elseBody.compileToFragments(merge(o, {
3796
indent: indent
3797
}), LEVEL_TOP), this.makeCode("\n" + this.tab + "}"));
3798
}
3799
return answer;
3800
};
3801
3802
If.prototype.compileExpression = function(o) {
3803
var alt, body, cond, fragments;
3804
cond = this.condition.compileToFragments(o, LEVEL_COND);
3805
body = this.bodyNode().compileToFragments(o, LEVEL_LIST);
3806
alt = this.elseBodyNode() ? this.elseBodyNode().compileToFragments(o, LEVEL_LIST) : [this.makeCode('void 0')];
3807
fragments = cond.concat(this.makeCode(" ? "), body, this.makeCode(" : "), alt);
3808
if (o.level >= LEVEL_COND) {
3809
return this.wrapInBraces(fragments);
3810
} else {
3811
return fragments;
3812
}
3813
};
3814
3815
If.prototype.unfoldSoak = function() {
3816
return this.soak && this;
3817
};
3818
3819
return If;
3820
3821
})(Base);
3822
3823
UTILITIES = {
3824
extend: function(o) {
3825
return "function(child, parent) { for (var key in parent) { if (" + (utility('hasProp', o)) + ".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; }";
3826
},
3827
bind: function() {
3828
return 'function(fn, me){ return function(){ return fn.apply(me, arguments); }; }';
3829
},
3830
indexOf: function() {
3831
return "[].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }";
3832
},
3833
modulo: function() {
3834
return "function(a, b) { return (+a % (b = +b) + b) % b; }";
3835
},
3836
hasProp: function() {
3837
return '{}.hasOwnProperty';
3838
},
3839
slice: function() {
3840
return '[].slice';
3841
}
3842
};
3843
3844
LEVEL_TOP = 1;
3845
3846
LEVEL_PAREN = 2;
3847
3848
LEVEL_LIST = 3;
3849
3850
LEVEL_COND = 4;
3851
3852
LEVEL_OP = 5;
3853
3854
LEVEL_ACCESS = 6;
3855
3856
TAB = ' ';
3857
3858
SIMPLENUM = /^[+-]?\d+$/;
3859
3860
utility = function(name, o) {
3861
var ref, root;
3862
root = o.scope.root;
3863
if (name in root.utilities) {
3864
return root.utilities[name];
3865
} else {
3866
ref = root.freeVariable(name);
3867
root.assign(ref, UTILITIES[name](o));
3868
return root.utilities[name] = ref;
3869
}
3870
};
3871
3872
multident = function(code, tab) {
3873
code = code.replace(/\n/g, '$&' + tab);
3874
return code.replace(/\s+$/, '');
3875
};
3876
3877
isLiteralArguments = function(node) {
3878
return node instanceof IdentifierLiteral && node.value === 'arguments';
3879
};
3880
3881
isLiteralThis = function(node) {
3882
return node instanceof ThisLiteral || (node instanceof Code && node.bound) || node instanceof SuperCall;
3883
};
3884
3885
isComplexOrAssignable = function(node) {
3886
return node.isComplex() || (typeof node.isAssignable === "function" ? node.isAssignable() : void 0);
3887
};
3888
3889
unfoldSoak = function(o, parent, name) {
3890
var ifn;
3891
if (!(ifn = parent[name].unfoldSoak(o))) {
3892
return;
3893
}
3894
parent[name] = ifn.body;
3895
ifn.body = new Value(parent);
3896
return ifn;
3897
};
3898
3899
}).call(this);
3900
3901