livescript
Version:
LiveScript is a language which compiles to JavaScript. It has a straightforward mapping to JavaScript and allows you to write expressive code devoid of repetitive boilerplate. While LiveScript adds many features to assist in functional style programming,
1,610 lines (1,609 loc) • 171 kB
JavaScript
// Generated by LiveScript 1.6.0
var fold, ref$, nameFromPath, stripString, SourceNode, SourceMapGenerator, sn, snEmpty, snSafe, snRemoveLeft, Node, Negatable, Block, Atom, Literal, Var, Key, Index, Slice, Chain, Call, List, Obj, Prop, Arr, Yield, Unary, Binary, Assign, Import, In, Existence, Fun, Class, Super, Parens, Splat, Jump, Throw, Return, While, For, StepSlice, Try, Switch, Case, If, Label, Cascade, JS, Require, Util, Vars, CopyL, DECLS, UTILS, LEVEL_TOP, LEVEL_PAREN, LEVEL_LIST, LEVEL_COND, LEVEL_OP, LEVEL_CALL, PREC, TAB, ID, SIMPLENUM, slice$ = [].slice, arrayFrom$ = Array.from || function(x){return slice$.call(x);}, toString$ = {}.toString;
fold = require('prelude-ls').fold;
ref$ = require('./util'), nameFromPath = ref$.nameFromPath, stripString = ref$.stripString;
ref$ = require('source-map'), SourceNode = ref$.SourceNode, SourceMapGenerator = ref$.SourceMapGenerator;
sn = function(node){
var parts, res$, i$, to$, result, e;
node == null && (node = {});
res$ = [];
for (i$ = 1, to$ = arguments.length; i$ < to$; ++i$) {
res$.push(arguments[i$]);
}
parts = res$;
try {
result = new SourceNode(node.line, node.column, null, parts);
result.displayName = node.constructor.displayName;
return result;
} catch (e$) {
e = e$;
console.dir(parts);
throw e;
}
};
snEmpty = function(node){
var i$, ref$, len$, child;
if (node instanceof SourceNode) {
for (i$ = 0, len$ = (ref$ = node.children).length; i$ < len$; ++i$) {
child = ref$[i$];
if (!snEmpty(child)) {
return false;
}
}
return true;
} else {
return !node;
}
};
snSafe = function(code){
if (code instanceof SourceNode) {
return code;
} else {
return code.toString();
}
};
snRemoveLeft = function(node, count){
var i$, to$, i, child;
for (i$ = 0, to$ = node.children.length; i$ < to$; ++i$) {
i = i$;
child = node.children[i];
if (child instanceof SourceNode) {
count = snRemoveLeft(child, count);
} else {
child = child.toString();
node.children[i] = child.slice(count);
count -= child.length;
}
if (count <= 0) {
return 0;
}
}
return count;
};
SourceNode.prototype.replace = function(){
var args, res$, i$, to$;
res$ = [];
for (i$ = 0, to$ = arguments.length; i$ < to$; ++i$) {
res$.push(arguments[i$]);
}
args = res$;
return new SourceNode(this.line, this.column, this.source, (function(){
var i$, x$, ref$, len$, results$ = [];
for (i$ = 0, len$ = (ref$ = this.children).length; i$ < len$; ++i$) {
x$ = ref$[i$];
results$.push(x$.replace.apply(x$, args));
}
return results$;
}.call(this)), this.name);
};
SourceNode.prototype.setFile = function(filename){
var i$, ref$, len$, child, results$ = [];
this.source = filename;
for (i$ = 0, len$ = (ref$ = this.children).length; i$ < len$; ++i$) {
child = ref$[i$];
if (child instanceof SourceNode) {
results$.push(child.setFile(filename));
}
}
return results$;
};
SourceNode.prototype.toStringWithSourceMap = function(){
var args, res$, i$, to$, gen, genLine, genColumn, stack, code, debugOutput, debugIndent, debugIndentStr, genForNode;
res$ = [];
for (i$ = 0, to$ = arguments.length; i$ < to$; ++i$) {
res$.push(arguments[i$]);
}
args = res$;
gen = (function(func, args, ctor) {
ctor.prototype = func.prototype;
var child = new ctor, result = func.apply(child, args), t;
return (t = typeof result) == "object" || t == "function" ? result || child : child;
})(SourceMapGenerator, args, function(){});
genLine = 1;
genColumn = 0;
stack = [];
code = '';
debugOutput = '';
debugIndent = '';
debugIndentStr = ' ';
genForNode = function(node){
var valid, i$, ref$, len$, child, cur, to$, i, c, results$ = [];
if (node instanceof SourceNode) {
debugOutput += debugIndent + node.displayName;
valid = node.line && 'column' in node;
if (valid) {
stack.push(node);
debugOutput += '!';
}
debugOutput += " " + node.line + ":" + node.column + " " + genLine + ":" + genColumn + "\n";
debugIndent += debugIndentStr;
for (i$ = 0, len$ = (ref$ = node.children).length; i$ < len$; ++i$) {
child = ref$[i$];
genForNode(child);
}
debugIndent = debugIndent.slice(0, debugIndent.length - debugIndentStr.length);
if (valid) {
return stack.pop();
}
} else {
debugOutput += debugIndent + "" + JSON.stringify(node) + "\n";
code += node;
cur = stack[stack.length - 1];
if (cur) {
gen.addMapping({
source: cur.source,
original: {
line: cur.line,
column: cur.column
},
generated: {
line: genLine,
column: genColumn
},
name: cur.name
});
}
for (i$ = 0, to$ = node.length; i$ < to$; ++i$) {
i = i$;
c = node.charAt(i);
if (c === "\n") {
genColumn = 0;
++genLine;
if (cur) {
results$.push(gen.addMapping({
source: cur.source,
original: {
line: cur.line,
column: cur.column
},
generated: {
line: genLine,
column: genColumn
},
name: cur.name
}));
}
} else {
results$.push(++genColumn);
}
}
return results$;
}
};
genForNode(this);
return {
code: code,
map: gen,
debug: debugOutput
};
};
/* # Use this to track down places where a SourceNode is being converted into a string and causing the location to be lost
tmp-to-string = SourceNode::to-string
SourceNode::to-string = (...args) ->
console.log("toString(): ", new Error().stack)
tmp-to-string.apply this, args
*/
(Node = function(){
throw Error('unimplemented');
}).prototype = {
compile: function(options, level){
var o, node, code, that, i$, len$, tmp;
o = import$({}, options);
if (level != null) {
o.level = level;
}
node = this.unfoldSoak(o) || this;
if (o.level && node.isStatement()) {
return node.compileClosure(o);
}
code = (node.tab = o.indent, node).compileNode(o);
if (that = node.temps) {
for (i$ = 0, len$ = that.length; i$ < len$; ++i$) {
tmp = that[i$];
o.scope.free(tmp);
}
}
return code;
},
compileClosure: function(o){
var that, fun, call, hasArgs, hasThis, out;
if (that = this.getJump()) {
that.carp('inconvertible statement');
}
fun = Fun([], Block(this));
call = Call();
if (o.inAsync) {
fun.async = true;
}
if (o.inGenerator) {
fun.generator = true;
}
this.traverseChildren(function(it){
switch (it.value) {
case 'this':
hasThis = true;
break;
case 'arguments':
hasArgs = it.value = 'args$';
}
});
if (hasThis) {
call.args.push(Literal('this'));
call.method = '.call';
}
if (hasArgs) {
call.args.push(Literal('arguments'));
fun.params.push(Var('args$'));
}
out = Parens(Chain((fun.wrapper = true, fun['void'] = this['void'], fun), [call]), true);
if (o.inGenerator) {
out = new Yield('yieldfrom', out);
} else if (o.inAsync) {
out = new Yield('await', out);
}
return out.compile(o);
},
compileBlock: function(o, node){
var code;
if (!snEmpty(code = node != null ? node.compile(o, LEVEL_TOP) : void 8)) {
return sn(null, "{\n", code, "\n" + this.tab + "}");
} else {
return sn(node, '{}');
}
},
compileSpreadOver: function(o, list, transform){
var ob, them, i$, len$, i, node, sp, lat, ref$;
ob = list instanceof Obj;
them = list.items;
for (i$ = 0, len$ = them.length; i$ < len$; ++i$) {
i = i$;
node = them[i$];
if (sp = node instanceof Splat) {
node = node.it;
}
if (ob && !sp) {
node = node.val;
}
node = transform(node);
if (sp) {
node = lat = Splat(node);
}
if (ob && !sp) {
them[i].val = node;
} else {
them[i] = node;
}
}
if (!lat && (this['void'] || !o.level)) {
list = (ref$ = Block(ob ? (function(){
var i$, x$, ref$, len$, results$ = [];
for (i$ = 0, len$ = (ref$ = them).length; i$ < len$; ++i$) {
x$ = ref$[i$];
results$.push(x$.val);
}
return results$;
}()) : them), ref$.front = this.front, ref$['void'] = true, ref$);
}
return list.compile(o, LEVEL_PAREN);
},
cache: function(o, once, level, tempName){
var ref$, ref, sub, tempvars;
if (!this.isComplex()) {
return [ref$ = level != null ? this.compile(o, level) : this, ref$];
}
if (ref = this.getRef()) {
sub = this;
} else {
sub = Assign(ref = Var(o.scope.temporary(tempName)), this);
if (once) {
ref.temp = true;
} else {
tempvars = [ref.value];
}
}
if (level != null) {
sub = sub.compile(o, level);
if (once && tempvars) {
o.scope.free(ref.value);
}
return [sub, ref.value];
}
return [sub, ref, tempvars];
},
compileLoopReference: function(o, name, ret, safeAccess){
var ref$, code, asn, tmp;
if (this instanceof Var && o.scope.check(this.value) || this instanceof Unary && ((ref$ = this.op) === '+' || ref$ === '-') && (-1 / 0 < (ref$ = +this.it.value) && ref$ < 1 / 0) || this instanceof Literal && !this.isComplex()) {
code = this.compile(o, LEVEL_PAREN);
if (safeAccess && !(this instanceof Var)) {
code = "(" + code + ")";
}
return [code, code];
}
asn = Assign(Var(tmp = o.scope.temporary(name)), this);
ret || (asn['void'] = true);
return [tmp, asn.compile(o, ret ? LEVEL_CALL : LEVEL_PAREN)];
},
eachChild: function(fn){
var i$, ref$, len$, name, child, j$, len1$, i, node, that;
for (i$ = 0, len$ = (ref$ = this.children).length; i$ < len$; ++i$) {
name = ref$[i$];
if (child = this[name]) {
if ('length' in child) {
for (j$ = 0, len1$ = child.length; j$ < len1$; ++j$) {
i = j$;
node = child[j$];
if (that = fn(node, name, i)) {
return that;
}
}
} else {
if ((that = fn(child, name)) != null) {
return that;
}
}
}
}
},
traverseChildren: function(fn, xscope){
var this$ = this;
return this.eachChild(function(node, name, index){
var ref$;
return (ref$ = fn(node, this$, name, index)) != null
? ref$
: node.traverseChildren(fn, xscope);
});
},
rewriteShorthand: function(o, assign){
var i$, ref$, len$, name, child, j$, len1$, i, node, that;
for (i$ = 0, len$ = (ref$ = this.children).length; i$ < len$; ++i$) {
name = ref$[i$];
if (child = this[name]) {
if ('length' in child) {
for (j$ = 0, len1$ = child.length; j$ < len1$; ++j$) {
i = j$;
node = child[j$];
if (that = node.rewriteShorthand(o, assign)) {
child[i] = that;
}
}
} else if (that = child.rewriteShorthand(o, assign)) {
this[name] = that;
}
}
}
},
anaphorize: function(){
var base, name, ref$;
this.children = this.aTargets;
if (this.eachChild(hasThat)) {
if ((base = this)[name = this.aSource] instanceof Existence) {
base[name].doAnaphorize = true;
} else if (base[name].value !== 'that') {
base[name] = Assign(Var('that'), base[name]);
}
}
function hasThat(it){
var that;
return it.value === 'that' || ((that = it.aSource)
? (that = it[that]) ? hasThat(that) : void 8
: it.eachChild(hasThat));
}
delete this.children;
return ref$ = this[this.aSource], ref$.cond = true, ref$;
},
carp: function(msg, type){
type == null && (type = SyntaxError);
throw type(msg + " " + this.lineMsg());
},
warn: function(msg){
if (typeof console != 'undefined' && console !== null) {
console.warn("WARNING: " + msg + " " + this.lineMsg());
}
},
lineMsg: function(){
return "on line " + (this.line || this.traverseChildren(function(it){
return it.line;
}));
},
delegate: function(names, fn){
var i$, len$;
for (i$ = 0, len$ = names.length; i$ < len$; ++i$) {
(fn$.call(this, names[i$]));
}
function fn$(name){
this[name] = function(it){
return fn.call(this, name, it);
};
}
},
children: [],
terminator: ';',
isComplex: YES,
isStatement: NO,
isAssignable: NO,
isCallable: NO,
isEmpty: NO,
isArray: NO,
isString: NO,
isRegex: NO,
isMatcher: function(){
return this.isString() || this.isRegex();
},
assigns: NO,
ripName: VOID,
getRef: VOID,
unfoldSoak: VOID,
unfoldAssign: VOID,
unparen: THIS,
unwrap: THIS,
maybeKey: VOID,
varName: String,
getAccessors: VOID,
getCall: VOID,
getDefault: VOID,
getJump: VOID,
isNextUnreachable: NO,
extractKeyRef: function(o, assign){
return this.maybeKey() || this.carp(assign ? "invalid assign" : "invalid property shorthand");
},
invert: function(){
return Unary('!', this, true);
},
invertCheck: function(it){
if (it.inverted) {
return this.invert();
} else {
return this;
}
},
addElse: function($else){
this['else'] = $else;
return this;
},
makeReturn: function(ref, obj){
var items, kv, i, v;
if (obj) {
items = this instanceof Arr
? (this.items[0] == null || this.items[1] == null && this.carp('must specify both key and value for object comprehension'), this.items)
: (kv = 'keyValue$', (function(){
var i$, ref$, len$, results$ = [];
for (i$ = 0, len$ = (ref$ = [Assign(Var(kv), this), Var(kv)]).length; i$ < len$; ++i$) {
i = i$;
v = ref$[i$];
results$.push(Chain(v).add(Index(Literal(i))));
}
return results$;
}.call(this)));
return Assign(Chain(Var(ref)).add(Index(items[0], '.', true)), items[1]);
} else if (ref) {
return Call.make(JS(ref + '.push'), [this]);
} else {
return Return(this);
}
},
show: String,
toString: function(idt){
var tree, that;
idt || (idt = '');
tree = '\n' + idt + this.constructor.displayName;
if (that = this.show()) {
tree += ' ' + that;
}
this.eachChild(function(it){
tree += it.toString(idt + TAB);
});
return tree;
},
stringify: function(space){
return JSON.stringify(this, null, space);
},
toJSON: function(){
return import$({
type: this.constructor.displayName
}, this);
}
};
exports.parse = function(json){
return exports.fromJSON(JSON.parse(json));
};
exports.fromJSON = (function(){
function fromJSON(it){
var that, node, key, val, i$, len$, v, results$ = [];
if (!(it && typeof it === 'object')) {
return it;
}
if (that = it.type) {
node = clone$(exports[that].prototype);
for (key in it) {
val = it[key];
node[key] = fromJSON(val);
}
return node;
}
if (it.length != null) {
for (i$ = 0, len$ = it.length; i$ < len$; ++i$) {
v = it[i$];
results$.push(fromJSON(v));
}
return results$;
} else {
return it;
}
}
return fromJSON;
}());
Negatable = {
show: function(){
return this.negated && '!';
},
invert: function(){
this.negated = !this.negated;
return this;
}
};
exports.Block = Block = (function(superclass){
var prototype = extend$((import$(Block, superclass).displayName = 'Block', Block), superclass).prototype, constructor = Block;
function Block(body){
var this$ = this instanceof ctor$ ? this : new ctor$;
body || (body = []);
if ('length' in body) {
this$.lines = body;
} else {
this$.lines = [];
this$.add(body);
}
return this$;
} function ctor$(){} ctor$.prototype = prototype;
Block.prototype.children = ['lines'];
Block.prototype.toJSON = function(){
delete this.back;
return superclass.prototype.toJSON.call(this);
};
Block.prototype.add = function(it){
var that, ref$;
it = it.unparen();
switch (false) {
case !(that = this.back):
that.add(it);
break;
case !(that = it.lines):
(ref$ = this.lines).push.apply(ref$, that);
break;
default:
this.lines.push(it);
if (that = (ref$ = it.back, delete it.back, ref$)) {
this.back = that;
}
}
return this;
};
Block.prototype.prepend = function(){
var ref$;
(ref$ = this.lines).splice.apply(ref$, [this.neck(), 0].concat(arrayFrom$(arguments)));
return this;
};
Block.prototype.pipe = function(target, type){
var args;
args = type === '|>' ? this.lines.pop() : target;
if (toString$.call(args).slice(8, -1) !== 'Array') {
args = [args];
}
switch (type) {
case '|>':
this.lines.push(Call.make(target, args, {
pipe: true
}));
break;
case '<|':
this.lines.push(Call.make(this.lines.pop(), args));
}
return this;
};
Block.prototype.unwrap = function(){
if (this.lines.length === 1) {
return this.lines[0];
} else {
return this;
}
};
Block.prototype.chomp = function(){
var lines, i, that;
lines = this.lines;
i = lines.length;
while (that = lines[--i]) {
if (!that.comment) {
break;
}
}
lines.length = i + 1;
return this;
};
Block.prototype.neck = function(){
var pos, i$, ref$, len$, x;
pos = 0;
for (i$ = 0, len$ = (ref$ = this.lines).length; i$ < len$; ++i$) {
x = ref$[i$];
if (!(x.comment || x instanceof Literal)) {
break;
}
++pos;
}
return pos;
};
Block.prototype.isComplex = function(){
var ref$;
return this.lines.length > 1 || ((ref$ = this.lines[0]) != null ? ref$.isComplex() : void 8);
};
prototype.delegate(['isCallable', 'isArray', 'isString', 'isRegex'], function(it){
var ref$, ref1$;
return (ref$ = (ref1$ = this.lines)[ref1$.length - 1]) != null ? ref$[it]() : void 8;
});
Block.prototype.getJump = function(it){
var i$, ref$, len$, node, that;
for (i$ = 0, len$ = (ref$ = this.lines).length; i$ < len$; ++i$) {
node = ref$[i$];
if (that = node.getJump(it)) {
return that;
}
}
};
Block.prototype.isNextUnreachable = function(){
var i$, ref$, len$, node;
for (i$ = 0, len$ = (ref$ = this.lines).length; i$ < len$; ++i$) {
node = ref$[i$];
if (node.isNextUnreachable()) {
return true;
}
}
return false;
};
Block.prototype.makeReturn = function(){
var that, ref$, key$;
this.chomp();
if (that = (ref$ = this.lines)[key$ = ref$.length - 1] != null ? ref$[key$] = (ref$ = ref$[key$]).makeReturn.apply(ref$, arguments) : void 8) {
if (that instanceof Return && !that.it) {
--this.lines.length;
}
}
return this;
};
Block.prototype.compile = function(o, level){
var tab, codes, i$, ref$, len$, node, that, code;
level == null && (level = o.level);
if (level) {
return this.compileExpressions(o, level);
}
o.block = this;
tab = o.indent;
codes = [];
for (i$ = 0, len$ = (ref$ = this.lines).length; i$ < len$; ++i$) {
node = ref$[i$];
if (that = node.rewriteShorthand(o)) {
node = that;
}
node = node.unfoldSoak(o) || node;
if (snEmpty(code = (node.front = true, node).compile(o, level))) {
continue;
}
codes.push(tab);
codes.push(code);
node.isStatement() || codes.push(node.terminator);
codes.push('\n');
}
codes.pop();
return sn.apply(null, [null].concat(arrayFrom$(codes)));
};
Block.prototype.compileRoot = function(options){
var o, that, ref$, bare, prefix, ref1$, ref2$, comment, ref3$, code;
o = (import$({
level: LEVEL_TOP,
scope: this.scope = Scope.root = new Scope
}, options));
if (that = (ref$ = o.saveScope, delete o.saveScope, ref$)) {
this.scope = Scope.root = o.scope = that.savedScope || (that.savedScope = o.scope);
}
delete o.filename;
o.indent = (bare = (ref$ = o.bare, delete o.bare, ref$)) ? '' : TAB;
if (/^\s*(?:#!|javascript:)/.test((ref$ = this.lines[0]) != null ? ref$.code : void 8)) {
prefix = this.lines.shift().code + '\n';
}
if (((ref1$ = this.lines[0]) != null ? (ref2$ = ref1$.code) != null ? ref2$[0] : void 8 : void 8) === '/') {
comment = this.lines.shift().code + '\n';
}
if ((ref3$ = o.eval, delete o.eval, ref3$) && this.chomp().lines.length) {
if (bare) {
this.lines.push(Parens(this.lines.pop()));
} else {
this.makeReturn();
}
}
code = [this.compileWithDeclarations(o)];
bare || (code = ["(function(){\n"].concat(arrayFrom$(code), ["\n}).call(this);\n"]));
return sn(null, prefix || [], options.header || [], comment || [], code);
};
Block.prototype.compileWithDeclarations = function(o){
var pre, i, rest, post, that;
o.level = LEVEL_TOP;
pre = [];
if (i = this.neck()) {
rest = this.lines.splice(i, 9e9);
pre = [this.compile(o), "\n"];
this.lines = rest;
}
if (snEmpty(post = this.compile(o))) {
return sn(this, pre[0] || []);
}
return sn.apply(null, [null].concat(arrayFrom$(pre), [(that = this.scope) ? that.emit(post, o.indent) : post]));
};
Block.prototype.compileExpressions = function(o, level){
var lines, i, that, line, code, last, i$, len$, node;
lines = this.chomp().lines;
i = -1;
while (that = lines[++i]) {
if (that.comment) {
lines.splice(i--, 1);
}
}
if (!lines.length) {
lines.push(Literal('void'));
}
lines[0].front = this.front;
lines[lines.length - 1]['void'] = this['void'];
if (!lines[1]) {
line = lines[0];
if (that = line.rewriteShorthand(o)) {
line = that;
}
return line.compile(o, level);
}
code = [];
last = lines.pop();
for (i$ = 0, len$ = lines.length; i$ < len$; ++i$) {
node = lines[i$];
if (that = node.rewriteShorthand(o)) {
node = that;
}
code.push((node['void'] = true, node).compile(o, LEVEL_PAREN), ', ');
}
if (that = last.rewriteShorthand(o)) {
last = that;
}
code.push(last.compile(o, LEVEL_PAREN));
if (level < LEVEL_LIST) {
return sn.apply(null, [null].concat(arrayFrom$(code)));
} else {
return sn.apply(null, [null, "("].concat(arrayFrom$(code), [")"]));
}
};
Block.prototype.rewriteShorthand = VOID;
return Block;
}(Node));
Atom = (function(superclass){
var prototype = extend$((import$(Atom, superclass).displayName = 'Atom', Atom), superclass).prototype, constructor = Atom;
Atom.prototype.show = function(){
return this.value;
};
Atom.prototype.isComplex = NO;
function Atom(){
Atom.superclass.apply(this, arguments);
}
return Atom;
}(Node));
exports.Literal = Literal = (function(superclass){
var prototype = extend$((import$(Literal, superclass).displayName = 'Literal', Literal), superclass).prototype, constructor = Literal;
function Literal(value){
var this$ = this instanceof ctor$ ? this : new ctor$;
this$.value = value;
if (value.js) {
return JS(value + "", true);
}
if (value === 'super') {
return new Super;
}
return this$;
} function ctor$(){} ctor$.prototype = prototype;
Literal.prototype.isEmpty = function(){
var ref$;
return (ref$ = this.value) === 'void' || ref$ === 'null';
};
Literal.prototype.isCallable = function(){
var ref$;
return (ref$ = this.value) === 'this' || ref$ === 'eval' || ref$ === '..';
};
Literal.prototype.isString = function(){
return 0 <= '\'"'.indexOf((this.value + "").charAt());
};
Literal.prototype.isRegex = function(){
return (this.value + "").charAt() === '/';
};
Literal.prototype.isComplex = function(){
return this.isRegex() || this.value === 'debugger';
};
Literal.prototype.isWhat = function(){
switch (false) {
case !this.isEmpty():
return 'empty';
case !this.isCallable():
return 'callable';
case !this.isString():
return 'string';
case !this.isRegex():
return 'regex';
case !this.isComplex():
return 'complex';
default:
}
};
Literal.prototype.varName = function(){
if (/^\w+$/.test(this.value)) {
return '$' + this.value;
} else {
return '';
}
};
Literal.prototype.makeReturn = function(it){
if (!it && this.value === 'debugger') {
return this;
} else {
return superclass.prototype.makeReturn.apply(this, arguments);
}
};
Literal.prototype.maybeKey = function(){
var ref$;
if (ID.test(this.value)) {
return Key(this.value, (ref$ = this.value) !== 'arguments' && ref$ !== 'eval');
} else {
return this;
}
};
Literal.prototype.compile = function(o, level){
var val, ref$;
level == null && (level = o.level);
switch (val = this.value + "") {
case 'this':
return sn(this, ((ref$ = o.scope.fun) != null ? ref$.bound : void 8) || val);
case 'void':
if (!level) {
return sn(this, '');
}
val += ' 8';
// fallthrough
case 'null':
if (level === LEVEL_CALL) {
this.carp('invalid use of ' + this.value);
}
break;
case 'on':
case 'yes':
val = 'true';
break;
case 'off':
case 'no':
val = 'false';
break;
case '*':
this.carp('stray star');
break;
case '..':
if (!(val = o.ref)) {
this.carp('stray reference');
}
this.cascadee || (val.erred = true);
break;
case 'debugger':
if (level) {
return sn(this, "(function(){ debugger; }())");
}
}
return sn(this, snSafe(val));
};
return Literal;
}(Atom));
exports.Var = Var = (function(superclass){
var prototype = extend$((import$(Var, superclass).displayName = 'Var', Var), superclass).prototype, constructor = Var;
function Var(value){
var this$ = this instanceof ctor$ ? this : new ctor$;
this$.value = value;
return this$;
} function ctor$(){} ctor$.prototype = prototype;
prototype.isAssignable = prototype.isCallable = YES;
Var.prototype.assigns = function(){
return [this.value];
};
Var.prototype.maybeKey = function(){
var ref$;
return ref$ = Key(this.value), ref$.line = this.line, ref$;
};
Var.prototype.varName = prototype.show;
Var.prototype.compile = function(o){
return sn(this, this.temp
? o.scope.free(this.value)
: this.value);
};
return Var;
}(Atom));
exports.Key = Key = (function(superclass){
var prototype = extend$((import$(Key, superclass).displayName = 'Key', Key), superclass).prototype, constructor = Key;
function Key(name, reserved){
var this$ = this instanceof ctor$ ? this : new ctor$;
this$.reserved = reserved || name.reserved;
this$.name = '' + name;
return this$;
} function ctor$(){} ctor$.prototype = prototype;
Key.prototype.isComplex = NO;
Key.prototype.assigns = function(){
return [this.name];
};
Key.prototype.maybeKey = THIS;
Key.prototype.varName = function(){
var name;
name = this.name;
if (this.reserved || (name === 'arguments' || name === 'eval')) {
return "$" + name;
} else {
return name;
}
};
Key.prototype.show = function(){
if (this.reserved) {
return "'" + this.name + "'";
} else {
return this.name;
}
};
Key.prototype.compile = function(){
return sn(this, this.show());
};
return Key;
}(Node));
exports.Index = Index = (function(superclass){
var prototype = extend$((import$(Index, superclass).displayName = 'Index', Index), superclass).prototype, constructor = Index;
function Index(key, symbol, init){
var k, this$ = this instanceof ctor$ ? this : new ctor$;
symbol || (symbol = '.');
if (init && key instanceof Arr) {
switch (key.items.length) {
case 1:
if (!((k = key.items[0]) instanceof Splat)) {
key = Parens(k);
}
}
}
switch (symbol) {
case '[]':
this$.vivify = Arr;
break;
case '{}':
this$.vivify = Obj;
break;
default:
if ('=' === symbol.slice(-1)) {
this$.assign = symbol.slice(1);
}
}
this$.key = key;
this$.symbol = symbol;
return this$;
} function ctor$(){} ctor$.prototype = prototype;
Index.prototype.children = ['key'];
Index.prototype.show = function(){
return [this.soak ? '?' : void 8] + this.symbol;
};
Index.prototype.isComplex = function(){
return this.key.isComplex() || this.vivify != null;
};
Index.prototype.varName = function(){
var ref$;
return ((ref$ = this.key) instanceof Key || ref$ instanceof Literal) && this.key.varName();
};
Index.prototype.compile = function(o){
var code;
code = this.key.compile(o, LEVEL_PAREN);
if (this.key instanceof Key && '\'' !== code.toString().charAt(0)) {
return sn(this, ".", code);
} else {
return sn(this, "[", code, "]");
}
};
return Index;
}(Node));
exports.Slice = Slice = (function(superclass){
var prototype = extend$((import$(Slice, superclass).displayName = 'Slice', Slice), superclass).prototype, constructor = Slice;
function Slice(arg$){
var this$ = this instanceof ctor$ ? this : new ctor$;
this$.type = arg$.type, this$.target = arg$.target, this$.from = arg$.from, this$.to = arg$.to;
this$.from == null && (this$.from = Literal(0));
if (this$.to && this$.type === 'to') {
this$.to = Binary('+', this$.to, Literal('1'));
}
return this$;
} function ctor$(){} ctor$.prototype = prototype;
Slice.prototype.children = ['target', 'from', 'to'];
Slice.prototype.show = function(){
return this.type;
};
Slice.prototype.compileNode = function(o){
var args;
if (this.to && this.type === 'to') {
this.to = Binary('||', this.to, Literal('9e9'));
}
args = [this.target, this.from];
if (this.to) {
args.push(this.to);
}
return Chain(Var(util('slice'))).add(Index(Key('call'), '.', true)).add(Call(args)).compile(o);
};
return Slice;
}(Node));
exports.Chain = Chain = (function(superclass){
var prototype = extend$((import$(Chain, superclass).displayName = 'Chain', Chain), superclass).prototype, constructor = Chain;
function Chain(head, tails){
var this$ = this instanceof ctor$ ? this : new ctor$;
if (!tails && head instanceof Chain) {
return head;
}
this$.head = head;
this$.tails = tails || [];
return this$;
} function ctor$(){} ctor$.prototype = prototype;
Chain.prototype.children = ['head', 'tails'];
Chain.prototype.add = function(it){
var last, ref$, index, ref1$, bi, logics, call, f;
if (this.tails.length) {
last = (ref$ = this.tails)[ref$.length - 1];
if (last instanceof Call && ((ref$ = last.partialized) != null ? ref$.length : void 8) === 1 && it.args.length === 1) {
index = last.partialized[0].head.value;
delete last.partialized;
last.args[index] = it.args[0];
return this;
}
}
if (this.head instanceof Existence) {
ref1$ = Chain(this.head.it), this.head = ref1$.head, this.tails = ref1$.tails;
it.soak = true;
}
this.tails.push(it);
bi = this.head instanceof Parens && this.head.it instanceof Binary && !this.head.it.partial
? this.head.it
: this.head instanceof Binary && !this.head.partial ? this.head : void 8;
if (this.head instanceof Super) {
if (!this.head.called && it instanceof Call && !it.method) {
it.method = '.call';
it.args.unshift(Literal('this'));
this.head.called = true;
} else if (!this.tails[1] && ((ref1$ = it.key) != null ? ref1$.name : void 8) === 'prototype') {
this.head.sproto = true;
}
} else if (it instanceof Call && this.tails.length === 1 && bi && in$(bi.op, logics = ['&&', '||', 'xor'])) {
call = it;
f = function(x, key){
var y;
y = x[key];
if (y instanceof Binary && in$(y.op, logics)) {
f(y, 'first');
return f(y, 'second');
} else {
return x[key] = Chain(y).autoCompare(call.args);
}
};
f(bi, 'first');
f(bi, 'second');
return bi;
}
return this;
};
Chain.prototype.autoCompare = function(target){
var test;
if (!this.tails.length) {
test = this.head;
}
switch (false) {
case !(test instanceof Literal):
return Binary('===', test, target[0]);
case !(test instanceof Unary && test.it instanceof Literal):
return Binary('===', test, target[0]);
case !(test instanceof Arr || test instanceof Obj):
return Binary('====', test, target[0]);
case !(test instanceof Var && test.value === '_'):
return Literal('true');
default:
return this.add(Call(target)) || [];
}
};
Chain.prototype.flipIt = function(){
this.flip = true;
return this;
};
Chain.prototype.unwrap = function(){
if (this.tails.length) {
return this;
} else {
return this.head;
}
};
prototype.delegate(['getJump', 'assigns', 'isStatement', 'isString'], function(it, arg){
return !this.tails.length && this.head[it](arg);
});
Chain.prototype.isComplex = function(){
return this.tails.length || this.head.isComplex();
};
Chain.prototype.isCallable = function(){
var that, ref$;
if (that = (ref$ = this.tails)[ref$.length - 1]) {
return !((ref$ = that.key) != null && ref$.items);
} else {
return this.head.isCallable();
}
};
Chain.prototype.isArray = function(){
var that, ref$;
if (that = (ref$ = this.tails)[ref$.length - 1]) {
return that.key instanceof Arr;
} else {
return this.head.isArray();
}
};
Chain.prototype.isRegex = function(){
return this.head.value === 'RegExp' && !this.tails[1] && this.tails[0] instanceof Call;
};
Chain.prototype.isAssignable = function(){
var tail, ref$, i$, len$;
if (!(tail = (ref$ = this.tails)[ref$.length - 1])) {
return this.head.isAssignable();
}
if (!(tail instanceof Index) || tail.key instanceof List || tail.symbol === '.~') {
return false;
}
for (i$ = 0, len$ = (ref$ = this.tails).length; i$ < len$; ++i$) {
tail = ref$[i$];
if (tail.assign) {
return false;
}
}
return true;
};
Chain.prototype.isSimpleAccess = function(){
return this.tails.length === 1 && !this.head.isComplex() && !this.tails[0].isComplex();
};
Chain.prototype.makeReturn = function(){
var ref$;
if (this.tails.length) {
return superclass.prototype.makeReturn.apply(this, arguments);
} else {
return (ref$ = this.head).makeReturn.apply(ref$, arguments);
}
};
Chain.prototype.getCall = function(){
var tail, ref$;
return (tail = (ref$ = this.tails)[ref$.length - 1]) instanceof Call && tail;
};
Chain.prototype.varName = function(){
var ref$, ref1$;
return (ref$ = (ref1$ = this.tails)[ref1$.length - 1]) != null ? ref$.varName() : void 8;
};
Chain.prototype.cacheReference = function(o){
var name, ref$, base, bref, nref, key;
name = (ref$ = this.tails)[ref$.length - 1];
if (!this.isAssignable()) {
return this.unwrap().cache(o, true);
}
if (this.tails.length < 2 && !this.head.isComplex() && !(name != null && name.isComplex())) {
return [this, this];
}
base = Chain(this.head, this.tails.slice(0, -1));
if (base.isComplex()) {
ref$ = base.unwrap().cache(o, true), base = ref$[0], bref = ref$[1];
base = Chain(base);
}
if (!name) {
return [base, bref];
}
nref = name;
if (name.symbol !== '.') {
nref = name;
name = Index(name.key, '.');
}
if (name.isComplex()) {
ref$ = name.key.unwrap().cache(o, true, void 8, 'key'), key = ref$[0], nref.key = ref$[1];
name = Index(key);
}
return [base.add(name), Chain(bref || base.head, [nref])];
};
Chain.prototype.compileNode = function(o){
var head, tails, that, i$, len$, t, hasPartial, pre, rest, broken, partial, post, context, idt, func, base, news, ref$;
if (this.flip) {
util('flip');
util('curry');
}
head = this.head, tails = this.tails;
head.front = this.front;
head.newed = this.newed;
if (!tails.length) {
return head.compile(o);
}
if (that = this.unfoldAssign(o)) {
return that.compile(o);
}
for (i$ = 0, len$ = tails.length; i$ < len$; ++i$) {
t = tails[i$];
if (t.partialized) {
hasPartial = true;
break;
}
}
if (hasPartial) {
util('slice');
pre = [];
rest = [];
for (i$ = 0, len$ = tails.length; i$ < len$; ++i$) {
t = tails[i$];
broken = broken || t.partialized != null;
if (broken) {
rest.push(t);
} else {
pre.push(t);
}
}
if (rest != null) {
partial = rest[0], post = slice$.call(rest, 1);
}
this.tails = pre;
context = pre.length
? Chain(head, slice$.call(pre, 0, -1))
: Literal('this');
return Chain(Chain(Var(util('partialize'))).add(Index(Key('apply'))).add(Call([context, Arr([this, Arr(partial.args), Arr(partial.partialized)])])), post).compile(o);
}
if (tails[0] instanceof Call && !head.isCallable()) {
this.carp('invalid callee');
}
this.expandVivify();
this.expandBind(o);
this.expandSplat(o);
this.expandStar(o);
if (this.splattedNewArgs) {
idt = o.indent + TAB;
func = Chain(this.head, tails.slice(0, -1));
return sn(null, "(function(func, args, ctor) {\n" + idt + "ctor.prototype = func.prototype;\n" + idt + "var child = new ctor, result = func.apply(child, args), t;\n" + idt + "return (t = typeof result) == \"object\" || t == \"function\" ? result || child : child;\n" + TAB + "})(", func.compile(o), ", ", this.splattedNewArgs, ", function(){})");
}
if (!this.tails.length) {
return this.head.compile(o);
}
base = [this.head.compile(o, LEVEL_CALL)];
news = [];
rest = [];
for (i$ = 0, len$ = (ref$ = this.tails).length; i$ < len$; ++i$) {
t = ref$[i$];
if (t['new']) {
news.push('new ');
}
rest.push(t.compile(o));
}
if ('.' === rest.join("").charAt(0) && SIMPLENUM.test(base[0].toString())) {
base.push(' ');
}
return sn.apply(null, [null].concat(arrayFrom$(news), arrayFrom$(base), arrayFrom$(rest)));
};
Chain.prototype.unfoldSoak = function(o){
var that, ref$, i$, len$, i, node, ref1$, bust, test;
if (that = this.head.unfoldSoak(o)) {
(ref$ = that.then.tails).push.apply(ref$, this.tails);
return that;
}
for (i$ = 0, len$ = (ref$ = this.tails).length; i$ < len$; ++i$) {
i = i$;
node = ref$[i$];
if (ref1$ = node.soak, delete node.soak, ref1$) {
bust = Chain(this.head, this.tails.splice(0, i));
if (node.assign && !bust.isAssignable()) {
node.carp('invalid accessign');
}
if (i && (node.assign || node instanceof Call)) {
ref1$ = bust.cacheReference(o), test = ref1$[0], bust = ref1$[1];
if (bust instanceof Chain) {
(ref1$ = this.tails).unshift.apply(ref1$, bust.tails);
bust = bust.head;
}
this.head = bust;
} else {
ref1$ = bust.unwrap().cache(o), test = ref1$[0], this.head = ref1$[1];
}
test = node instanceof Call
? JS("typeof " + test.compile(o, LEVEL_OP) + " == 'function'")
: Existence(test);
return ref1$ = If(test, this), ref1$.soak = true, ref1$.cond = this.cond, ref1$['void'] = this['void'], ref1$;
}
}
};
Chain.prototype.unfoldAssign = function(o){
var that, ref$, i$, len$, i, index, op, left, lefts, rites, j$, len1$, node, ref1$;
if (that = this.head.unfoldAssign(o)) {
(ref$ = that.right.tails).push.apply(ref$, this.tails);
return that;
}
for (i$ = 0, len$ = (ref$ = this.tails).length; i$ < len$; ++i$) {
i = i$;
index = ref$[i$];
if (op = index.assign) {
index.assign = '';
left = Chain(this.head, this.tails.splice(0, i)).unwrap();
if (left instanceof Arr) {
lefts = left.items;
rites = (this.head = Arr()).items;
for (j$ = 0, len1$ = lefts.length; j$ < len1$; ++j$) {
i = j$;
node = lefts[j$];
ref1$ = Chain(node).cacheReference(o), rites[i] = ref1$[0], lefts[i] = ref1$[1];
}
} else {
ref1$ = Chain(left).cacheReference(o), left = ref1$[0], this.head = ref1$[1];
}
if (op === '=') {
op = ':=';
}
return ref1$ = Assign(left, this, op), ref1$.access = true, ref1$;
}
}
};
Chain.prototype.expandSplat = function(o){
var tails, i, call, args, ctx, ref$;
tails = this.tails;
i = -1;
while (call = tails[++i]) {
if (!(args = call.args)) {
continue;
}
ctx = call.method === '.call' && (args = args.concat()).shift();
if (!!snEmpty(args = Splat.compileArray(o, args, true))) {
continue;
}
if (call['new']) {
this.splattedNewArgs = args;
} else {
if (!ctx && tails[i - 1] instanceof Index) {
ref$ = Chain(this.head, tails.splice(0, i - 1)).cache(o, true), this.head = ref$[0], ctx = ref$[1];
i = 0;
}
call.method = '.apply';
call.args = [ctx || Literal('null'), JS(args)];
}
}
};
Chain.prototype.expandVivify = function(){
var tails, i, that, ref$, ref1$;
tails = this.tails;
i = 0;
while (i < tails.length) {
if (that = (ref1$ = (ref$ = tails[i++]).vivify, delete ref$.vivify, ref1$)) {
this.head = Assign(Chain(this.head, tails.splice(0, i)), that(), '=', '||');
i = 0;
}
}
};
Chain.prototype.expandBind = function(o){
var tails, i, that, obj, key, call;
tails = this.tails;
i = -1;
while (that = tails[++i]) {
if (that.symbol !== '.~') {
continue;
}
that.symbol = '';
obj = Chain(this.head, tails.splice(0, i)).unwrap();
key = tails.shift().key;
call = Call.make(Util('bind'), [obj, (key.reserved = true, key)]);
this.head = this.newed ? Parens(call, true) : call;
i = -1;
}
};
Chain.prototype.expandStar = function(o){
var tails, i, that, stars, ref$, sub, ref, temps, value, i$, len$, star;
tails = this.tails;
i = -1;
while (that = tails[++i]) {
if (that.args || that.stars || that.key instanceof Key) {
continue;
}
stars = that.stars = [];
that.eachChild(seek);
if (!stars.length) {
continue;
}
ref$ = Chain(this.head, tails.splice(0, i)).unwrap().cache(o), sub = ref$[0], ref = ref$[1], temps = ref$[2];
value = Chain(ref, [Index(Key('length'))]).compile(o);
for (i$ = 0, len$ = stars.length; i$ < len$; ++i$) {
star = stars[i$];
star.value = value;
star.isAssignable = YES;
}
this.head = JS(sub.compile(o, LEVEL_CALL) + tails.shift().compile(o));
if (temps) {
o.scope.free(temps[0]);
}
i = -1;
}
function seek(it){
if (it.value === '*') {
stars.push(it);
} else if (!(it instanceof Index)) {
it.eachChild(seek);
}
}
};
Chain.prototype.rewriteShorthand = function(o, assign){
var that, i$, ref$, len$, i, item;
if (that = this.head.rewriteShorthand(o)) {
this.head = that;
}
for (i$ = 0, len$ = (ref$ = this.tails).length; i$ < len$; ++i$) {
i = i$;
item = ref$[i$];
if (that = item.rewriteShorthand(o, assign)) {
this.tails[i] = that;
}
}
this.expandSlice(o, assign);
return this.unwrap();
};
Chain.prototype.expandSlice = function(o, assign){
var tails, i, tail, ref$, x;
tails = this.tails;
i = -1;
while (tail = tails[++i]) {
if ((ref$ = tail.key) != null && ref$.items) {
if (tails[i + 1] instanceof Call) {
tail.carp('calling a slice');
}
x = tails.splice(0, i + 1);
x = x.pop().key.toSlice(o, Chain(this.head, x).unwrap(), tail.symbol, assign);
this.head = (x.front = this.front, x);
i = -1;
}
}
return this;
};
Chain.prototype.extractKeyRef = function(o, assign){
var ref$, ref1$;
return ((ref$ = (ref1$ = this.tails)[ref1$.length - 1]) != null ? (ref1$ = ref$.key) != null ? ref1$.extractKeyRef(o, assign) : void 8 : void 8) || superclass.prototype.extractKeyRef.apply(this, arguments);
};
return Chain;
}(Node));
exports.Call = Call = (function(superclass){
var prototype = extend$((import$(Call, superclass).displayName = 'Call', Call), superclass).prototype, constructor = Call;
function Call(args){
var splat, i$, len$, i, a, ref$, this$ = this instanceof ctor$ ? this : new ctor$;
args || (args = []);
if (args.length === 1 && (splat = args[0]) instanceof Splat) {
if (splat.filler) {
this$.method = '.call';
args[0] = Literal('this');
args[1] = Splat(Literal('arguments'));
} else if (splat.it instanceof Arr) {
args = splat.it.items;
}
} else {
for (i$ = 0, len$ = args.length; i$ < len$; ++i$) {
i = i$;
a = args[i$];
if (a.value === '_') {
args[i] = Chain(Literal('void'));
args[i].placeholder = true;
((ref$ = this$.partialized) != null
? ref$
: this$.partialized = []).push(Chain(Literal(i)));
}
}
}
this$.args = args;
return this$;
} function ctor$(){} ctor$.prototype = prototype;
Call.prototype.children = ['args'];
Call.prototype.show = function(){
return [this['new']] + [this.method] + [this.soak ? '?' : void 8];
};
Call.prototype.compile = function(o){
var code, i$, ref$, len$, i, a;
code = [sn(this, this.method || '', '(') + (this.pipe ? "\n" + o.indent : '')];
for (i$ = 0, len$ = (ref$ = this.args).length; i$ < len$; ++i$) {
i = i$;
a = ref$[i$];
code.push(i ? ', ' : '', a.compile(o, LEVEL_LIST));
}
code.push(sn(this, ')'));
return sn.apply(null, [null].concat(arrayFrom$(code)));
};
Call.make = function(callee, args, opts){
var call;
call = Call(args);
if (opts) {
import$(call, opts);
}
return Chain(callee).add(call);
};
Call.block = function(fun, args, method){
var ref$, ref1$;
return ref$ = Parens(Chain(fun, [(ref1$ = Call(args), ref1$.method = method, ref1$)]), true), ref$.calling = true, ref$;
};
Call.back = function(params, node, bound, curried, hushed, generator){
var fun, ref$, args, index, i$, len$, a;
fun = Fun(params, void 8, bound, curried, hushed, generator);
if (node instanceof Label) {
fun.name = node.label;
fun.labeled = true;
node = node.it;
}
if (!fun.hushed && (fun.hushed = node.op === '!')) {
node = node.it;
}
if ((ref$ = node.getCall()) != null) {
ref$.partialized = null;
}
args = (node.getCall() || (node = Chain(node).add(Call())).getCall()).args;
index = 0;
for (i$ = 0, len$ = args.length; i$ < len$; ++i$) {
a = args[i$];
if (a.placeholder) {
break;
}
++index;
}
return node.back = (args[index] = fun).body, node;
};
Call['let'] = function(args, body){
var hasYield, hasAwait, params, res$, i$, len$, i, a, that, gotThis;
hasYield = false;
hasAwait = false;
body.traverseChildren(function(child){
if (child instanceof Yield) {
switch (child.op) {
case 'yield':
case 'yieldfrom':
hasYield = true;
break;
case 'await':
hasAwait = true;
}
}
if (hasYield && hasAwait) {
return true;
}
});
res$ = [];
for (i$ = 0, len$ = args.length; i$ < len$; ++i$) {
i = i$;
a = args[i$];
if (that = a.op === '=' && !a.logic && a.right) {
args[i] = that;
if (i === 0 && (gotThis = a.left.value === 'this')) {
continue;
}
res$.push(a.left);
} else {
res$.push(Var(a.varName() || a.carp('invalid "let" argument')));
}
}
params = res$;
gotThis || args.unshift(Literal('this'));
body = this.block(Fun(params, body, null, null, null, hasYield, hasAwait), args, '.call');
if (hasYield || hasAwait) {
return Block(Yield(hasYield ? 'yieldfrom' : 'await', body));
} else {
return body;
}
};
return Call;
}(Node));
List = (function(superclass){
var prototype = extend$((import$(List, superclass).displayName = 'List', List), superclass).prototype, constructor = List;
List.prototype.children = ['items'];
List.prototype.show = function(){
return this.name;
};
List.prototype.named = function(name){
this.name = name;
return this;
};
List.prototype.isEmpty = function(){
return !this.items.length;
};
List.prototype.assigns = function(){
var x