1
0
Fork 0
arangodb/js/node/node_modules/iced-coffee-script/lib/coffee-script/nodes.js

4441 lines
145 KiB
JavaScript

// Generated by IcedCoffeeScript 1.7.1-f
(function() {
var Access, Arr, Assign, Await, Base, Block, Call, Class, Closure, Code, CodeFragment, Comment, CpsCascade, Defer, Existence, Expansion, Extends, For, HEXNUM, IDENTIFIER, IDENTIFIER_STR, IS_REGEX, IS_STRING, IcedReturnValue, IcedRuntime, IcedTailCall, If, In, Index, InlineRuntime, LEVEL_ACCESS, LEVEL_COND, LEVEL_LIST, LEVEL_OP, LEVEL_PAREN, LEVEL_TOP, Literal, METHOD_DEF, NEGATE, NO, NULL, NUMBER, Obj, Op, Param, Parens, RESERVED, Range, Return, SIMPLENUM, STRICT_PROSCRIBED, Scope, Slice, Slot, Splat, Switch, TAB, THIS, Throw, Try, UTILITIES, Value, While, YES, addLocationDataFn, compact, del, ends, extend, flatten, fragmentsToText, iced, isLiteralArguments, isLiteralThis, last, locationDataToString, merge, multident, parseNum, some, starts, throwSyntaxError, unfoldSoak, utility, _ref, _ref1,
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
__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; },
__slice = [].slice;
Error.stackTraceLimit = Infinity;
Scope = require('./scope').Scope;
_ref = require('./lexer'), RESERVED = _ref.RESERVED, STRICT_PROSCRIBED = _ref.STRICT_PROSCRIBED;
iced = require('iced-runtime');
_ref1 = require('./helpers'), compact = _ref1.compact, flatten = _ref1.flatten, extend = _ref1.extend, merge = _ref1.merge, del = _ref1.del, starts = _ref1.starts, ends = _ref1.ends, last = _ref1.last, some = _ref1.some, addLocationDataFn = _ref1.addLocationDataFn, locationDataToString = _ref1.locationDataToString, throwSyntaxError = _ref1.throwSyntaxError;
exports.extend = extend;
exports.addLocationDataFn = addLocationDataFn;
YES = function() {
return true;
};
NO = function() {
return false;
};
THIS = function() {
return this;
};
NEGATE = function() {
this.negated = !this.negated;
return this;
};
NULL = function() {
return new Value(new Literal('null'));
};
exports.CodeFragment = CodeFragment = (function() {
function CodeFragment(parent, code) {
var _ref2;
this.code = "" + code;
this.locationData = parent != null ? parent.locationData : void 0;
this.type = (parent != null ? (_ref2 = parent.constructor) != null ? _ref2.name : void 0 : void 0) || 'unknown';
}
CodeFragment.prototype.toString = function() {
return "" + this.code + (this.locationData ? ": " + locationDataToString(this.locationData) : '');
};
return CodeFragment;
})();
fragmentsToText = function(fragments) {
var fragment;
return ((function() {
var _i, _len, _results;
_results = [];
for (_i = 0, _len = fragments.length; _i < _len; _i++) {
fragment = fragments[_i];
_results.push(fragment.code);
}
return _results;
})()).join('');
};
exports.Base = Base = (function() {
function Base() {
this.icedContinuationBlock = null;
this.icedLoopFlag = false;
this.icedNodeFlag = false;
this.icedGotCpsSplitFlag = false;
this.icedCpsPivotFlag = false;
this.icedHasAutocbFlag = false;
this.icedFoundArguments = false;
this.icedParentAwait = null;
this.icedCallContinuationFlag = false;
}
Base.prototype.compile = function(o, lvl) {
return fragmentsToText(this.compileToFragments(o, lvl));
};
Base.prototype.compileToFragments = function(o, lvl) {
var node;
o = extend({}, o);
if (lvl) {
o.level = lvl;
}
node = this.unfoldSoak(o) || this;
node.tab = o.indent;
if (node.icedHasContinuation() && !node.icedGotCpsSplitFlag) {
return node.icedCompileCps(o);
} else if (o.level === LEVEL_TOP || !node.isStatement(o)) {
return node.compileNode(o);
} else {
return node.compileClosure(o);
}
};
Base.prototype.compileClosure = function(o) {
var args, argumentsNode, func, jumpNode, meth;
if (jumpNode = this.jumps()) {
jumpNode.error('cannot use a pure statement in an expression');
}
o.sharedScope = true;
this.icedClearAutocbFlags();
func = new Code([], Block.wrap([this]));
args = [];
if ((argumentsNode = this.contains(isLiteralArguments)) || this.contains(isLiteralThis)) {
args = [new Literal('this')];
if (argumentsNode) {
meth = 'apply';
args.push(new Literal('arguments'));
} else {
meth = 'call';
}
func = new Value(func, [new Access(new Literal(meth))]);
}
return (new Call(func, args)).compileNode(o);
};
Base.prototype.cache = function(o, level, reused) {
var ref, sub;
if (!this.isComplex()) {
ref = level ? this.compileToFragments(o, level) : this;
return [ref, ref];
} else {
ref = new Literal(reused || o.scope.freeVariable('ref'));
sub = new Assign(ref, this);
if (level) {
return [sub.compileToFragments(o, level), [this.makeCode(ref.value)]];
} else {
return [sub, ref];
}
}
};
Base.prototype.cacheToCodeFragments = function(cacheValues) {
return [fragmentsToText(cacheValues[0]), fragmentsToText(cacheValues[1])];
};
Base.prototype.makeReturn = function(res) {
var me;
me = this.unwrapAll();
if (res) {
return new Call(new Literal("" + res + ".push"), [me]);
} else {
return new Return(me, this.icedHasAutocbFlag);
}
};
Base.prototype.contains = function(pred) {
var node;
node = void 0;
this.traverseChildren(false, function(n) {
if (pred(n)) {
node = n;
return false;
}
});
return node;
};
Base.prototype.lastNonComment = function(list) {
var i;
i = list.length;
while (i--) {
if (!(list[i] instanceof Comment)) {
return list[i];
}
}
return null;
};
Base.prototype.toString = function(idt, name) {
var extras, tree;
if (idt == null) {
idt = '';
}
if (name == null) {
name = this.constructor.name;
}
extras = [];
if (this.icedNodeFlag) {
extras.push("A");
}
if (this.icedLoopFlag) {
extras.push("L");
}
if (this.icedCpsPivotFlag) {
extras.push("P");
}
if (this.icedHasAutocbFlag) {
extras.push("C");
}
if (this.icedParentAwait) {
extras.push("D");
}
if (this.icedFoundArguments) {
extras.push("G");
}
if (extras.length) {
extras = " (" + extras.join('') + ")";
}
tree = '\n' + idt + name;
tree = '\n' + idt + name;
if (this.soak) {
tree += '?';
}
tree += extras;
this.eachChild(function(node) {
return tree += node.toString(idt + TAB);
});
if (this.icedContinuationBlock) {
idt += TAB;
tree += '\n' + idt + "Continuation";
tree += this.icedContinuationBlock.toString(idt + TAB);
}
return tree;
};
Base.prototype.eachChild = function(func) {
var attr, child, _i, _j, _len, _len1, _ref2, _ref3;
if (!this.children) {
return this;
}
_ref2 = this.children;
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
attr = _ref2[_i];
if (this[attr]) {
_ref3 = flatten([this[attr]]);
for (_j = 0, _len1 = _ref3.length; _j < _len1; _j++) {
child = _ref3[_j];
if (func(child) === false) {
return this;
}
}
}
}
return this;
};
Base.prototype.traverseChildren = function(crossScope, func) {
return this.eachChild(function(child) {
var recur;
recur = func(child);
if (recur !== false) {
return child.traverseChildren(crossScope, func);
}
});
};
Base.prototype.invert = function() {
return new Op('!', this);
};
Base.prototype.unwrapAll = function() {
var node;
node = this;
while (node !== (node = node.unwrap())) {
continue;
}
return node;
};
Base.prototype.flattenChildren = function() {
var attr, child, out, _i, _j, _len, _len1, _ref2, _ref3;
out = [];
_ref2 = this.children;
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
attr = _ref2[_i];
if (this[attr]) {
_ref3 = flatten([this[attr]]);
for (_j = 0, _len1 = _ref3.length; _j < _len1; _j++) {
child = _ref3[_j];
out.push(child);
}
}
}
return out;
};
Base.prototype.icedCompileCps = function(o) {
var code;
this.icedGotCpsSplitFlag = true;
code = CpsCascade.wrap(this, this.icedContinuationBlock, null, o);
o.sharedScope = true;
return code.compileNode(o);
};
Base.prototype.icedWalkAst = function(p, o) {
var child, _i, _len, _ref2;
this.icedParentAwait = p;
this.icedHasAutocbFlag = o.foundAutocb;
_ref2 = this.flattenChildren();
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
child = _ref2[_i];
if (child.icedWalkAst(p, o)) {
this.icedNodeFlag = true;
}
}
return this.icedNodeFlag;
};
Base.prototype.icedWalkAstLoops = function(flood) {
var child, _i, _len, _ref2;
if (this.isLoop() && this.icedNodeFlag) {
flood = true;
}
if (this.isLoop() && !this.icedNodeFlag) {
flood = false;
}
this.icedLoopFlag = flood;
_ref2 = this.flattenChildren();
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
child = _ref2[_i];
if (child.icedWalkAstLoops(flood)) {
this.icedLoopFlag = true;
}
}
return this.icedLoopFlag;
};
Base.prototype.icedWalkCpsPivots = function() {
var child, _i, _len, _ref2;
if (this.icedNodeFlag || (this.icedLoopFlag && this.icedIsJump())) {
this.icedCpsPivotFlag = true;
}
_ref2 = this.flattenChildren();
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
child = _ref2[_i];
if (child.icedWalkCpsPivots()) {
this.icedCpsPivotFlag = true;
}
}
return this.icedCpsPivotFlag;
};
Base.prototype.icedClearAutocbFlags = function() {
this.icedHasAutocbFlag = false;
return this.traverseChildren(false, function(node) {
node.icedHasAutocbFlag = false;
return true;
});
};
Base.prototype.icedCpsRotate = function() {
var child, _i, _len, _ref2;
_ref2 = this.flattenChildren();
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
child = _ref2[_i];
child.icedCpsRotate();
}
return this;
};
Base.prototype.icedIsCpsPivot = function() {
return this.icedCpsPivotFlag;
};
Base.prototype.icedNestContinuationBlock = function(b) {
return this.icedContinuationBlock = b;
};
Base.prototype.icedHasContinuation = function() {
return !!this.icedContinuationBlock;
};
Base.prototype.icedCallContinuation = function() {
return this.icedCallContinuationFlag = true;
};
Base.prototype.icedWrapContinuation = NO;
Base.prototype.icedIsJump = NO;
Base.prototype.icedUnwrap = function(e) {
if (e.icedHasContinuation() && this.icedHasContinuation()) {
return this;
} else {
if (this.icedHasContinuation()) {
e.icedContinuationBlock = this.icedContinuationBlock;
}
return e;
}
};
Base.prototype.icedStatementAssertion = function() {
if (this.icedIsCpsPivot()) {
return this.error("await'ed statements can't act as expressions");
}
};
Base.prototype.children = [];
Base.prototype.isStatement = NO;
Base.prototype.jumps = NO;
Base.prototype.isComplex = YES;
Base.prototype.isChainable = NO;
Base.prototype.isAssignable = NO;
Base.prototype.isLoop = NO;
Base.prototype.unwrap = THIS;
Base.prototype.unfoldSoak = NO;
Base.prototype.assigns = NO;
Base.prototype.updateLocationDataIfMissing = function(locationData) {
if (this.locationData) {
return this;
}
this.locationData = locationData;
return this.eachChild(function(child) {
return child.updateLocationDataIfMissing(locationData);
});
};
Base.prototype.error = function(message) {
return throwSyntaxError(message, this.locationData);
};
Base.prototype.makeCode = function(code) {
return new CodeFragment(this, code);
};
Base.prototype.wrapInBraces = function(fragments) {
return [].concat(this.makeCode('('), fragments, this.makeCode(')'));
};
Base.prototype.joinFragmentArrays = function(fragmentsList, joinStr) {
var answer, fragments, i, _i, _len;
answer = [];
for (i = _i = 0, _len = fragmentsList.length; _i < _len; i = ++_i) {
fragments = fragmentsList[i];
if (i) {
answer.push(this.makeCode(joinStr));
}
answer = answer.concat(fragments);
}
return answer;
};
return Base;
})();
exports.Block = Block = (function(_super) {
__extends(Block, _super);
function Block(nodes) {
Block.__super__.constructor.call(this);
this.expressions = compact(flatten(nodes || []));
}
Block.prototype.children = ['expressions'];
Block.prototype.push = function(node) {
this.expressions.push(node);
return this;
};
Block.prototype.pop = function() {
return this.expressions.pop();
};
Block.prototype.unshift = function(node) {
this.expressions.unshift(node);
return this;
};
Block.prototype.unwrap = function() {
if (this.expressions.length === 1) {
return this.icedUnwrap(this.expressions[0]);
} else {
return this;
}
};
Block.prototype.isEmpty = function() {
return !this.expressions.length;
};
Block.prototype.isStatement = function(o) {
var exp, _i, _len, _ref2;
_ref2 = this.expressions;
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
exp = _ref2[_i];
if (exp.isStatement(o)) {
return true;
}
}
return false;
};
Block.prototype.jumps = function(o) {
var exp, jumpNode, _i, _len, _ref2;
_ref2 = this.expressions;
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
exp = _ref2[_i];
if (jumpNode = exp.jumps(o)) {
return jumpNode;
}
}
};
Block.prototype.makeReturn = function(res) {
var expr, foundReturn, len;
len = this.expressions.length;
foundReturn = false;
while (len--) {
expr = this.expressions[len];
if (!(expr instanceof Comment)) {
this.expressions[len] = expr.makeReturn(res);
if (expr instanceof Return && !expr.expression && !expr.icedHasAutocbFlag) {
this.expressions.splice(len, 1);
foundReturn = true;
} else if (!(expr instanceof If) || expr.elseBody) {
foundReturn = true;
}
break;
}
}
if (this.icedHasAutocbFlag && !this.icedNodeFlag && !foundReturn) {
this.expressions.push(new Return(null, true));
}
return this;
};
Block.prototype.compileToFragments = function(o, level) {
if (o == null) {
o = {};
}
if (o.scope) {
return Block.__super__.compileToFragments.call(this, o, level);
} else {
return this.compileRoot(o);
}
};
Block.prototype.compileNode = function(o) {
var answer, compiledNodes, fragments, index, node, top, _i, _len, _ref2;
this.tab = o.indent;
top = o.level === LEVEL_TOP;
compiledNodes = [];
_ref2 = this.expressions;
for (index = _i = 0, _len = _ref2.length; _i < _len; index = ++_i) {
node = _ref2[index];
node = node.unwrapAll();
node = node.unfoldSoak(o) || node;
if (node instanceof Block) {
compiledNodes.push(node.compileNode(o));
} else if (top) {
node.front = true;
fragments = node.compileToFragments(o);
if (!node.isStatement(o)) {
fragments.unshift(this.makeCode("" + this.tab));
fragments.push(this.makeCode(";"));
}
compiledNodes.push(fragments);
} else {
compiledNodes.push(node.compileToFragments(o, LEVEL_LIST));
}
}
if (top) {
if (this.spaced) {
return [].concat(this.joinFragmentArrays(compiledNodes, '\n\n'), this.makeCode("\n"));
} else {
return this.joinFragmentArrays(compiledNodes, '\n');
}
}
if (compiledNodes.length) {
answer = this.joinFragmentArrays(compiledNodes, ', ');
} else {
answer = [this.makeCode("void 0")];
}
if (compiledNodes.length > 1 && o.level >= LEVEL_LIST) {
return this.wrapInBraces(answer);
} else {
return answer;
}
};
Block.prototype.compileRoot = function(o) {
var exp, fragments, i, name, prelude, preludeExps, rest, _i, _len, _ref2;
o.indent = o.bare ? '' : TAB;
o.level = LEVEL_TOP;
this.spaced = true;
o.scope = new Scope(null, this, null);
_ref2 = o.locals || [];
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
name = _ref2[_i];
o.scope.parameter(name);
}
prelude = [];
if (!o.bare) {
preludeExps = (function() {
var _j, _len1, _ref3, _results;
_ref3 = this.expressions;
_results = [];
for (i = _j = 0, _len1 = _ref3.length; _j < _len1; i = ++_j) {
exp = _ref3[i];
if (!(exp.unwrap() instanceof Comment)) {
break;
}
_results.push(exp);
}
return _results;
}).call(this);
rest = this.expressions.slice(preludeExps.length);
this.expressions = preludeExps;
if (preludeExps.length) {
prelude = this.compileNode(merge(o, {
indent: ''
}));
prelude.push(this.makeCode("\n"));
}
this.expressions = rest;
}
fragments = this.compileWithDeclarations(o);
if (o.bare) {
return fragments;
}
return [].concat(prelude, this.makeCode("(function() {\n"), fragments, this.makeCode("\n}).call(this);\n"));
};
Block.prototype.compileWithDeclarations = function(o) {
var assigns, declars, exp, fragments, i, post, rest, scope, spaced, _i, _len, _ref2, _ref3, _ref4;
fragments = [];
post = [];
_ref2 = this.expressions;
for (i = _i = 0, _len = _ref2.length; _i < _len; i = ++_i) {
exp = _ref2[i];
exp = exp.unwrap();
if (!(exp instanceof Comment || exp instanceof Literal)) {
break;
}
}
o = merge(o, {
level: LEVEL_TOP
});
if (i) {
rest = this.expressions.splice(i, 9e9);
_ref3 = [this.spaced, false], spaced = _ref3[0], this.spaced = _ref3[1];
_ref4 = [this.compileNode(o), spaced], fragments = _ref4[0], this.spaced = _ref4[1];
this.expressions = rest;
}
post = this.compileNode(o);
scope = o.scope;
if (scope.expressions === this) {
declars = o.scope.hasDeclarations();
assigns = scope.hasAssignments;
if (declars || assigns) {
if (i) {
fragments.push(this.makeCode('\n'));
}
fragments.push(this.makeCode("" + this.tab + "var "));
if (declars) {
fragments.push(this.makeCode(scope.declaredVariables().join(', ')));
}
if (assigns) {
if (declars) {
fragments.push(this.makeCode(",\n" + (this.tab + TAB)));
}
fragments.push(this.makeCode(scope.assignedVariables().join(",\n" + (this.tab + TAB))));
}
fragments.push(this.makeCode(";\n" + (this.spaced ? '\n' : '')));
} else if (fragments.length && post.length) {
fragments.push(this.makeCode("\n"));
}
}
return fragments.concat(post);
};
Block.wrap = function(nodes) {
if (nodes.length === 1 && nodes[0] instanceof Block) {
return nodes[0];
}
return new Block(nodes);
};
Block.prototype.icedThreadReturn = function(call) {
var expr, len;
call = call || new IcedTailCall;
len = this.expressions.length;
while (len--) {
expr = this.expressions[len];
if (expr.isStatement()) {
break;
}
if (!(expr instanceof Comment) && !(expr instanceof Return)) {
call.assignValue(expr);
this.expressions[len] = call;
return;
}
}
return this.expressions.push(call);
};
Block.prototype.icedCompileCps = function(o) {
this.icedGotCpsSplitFlag = true;
if (this.expressions.length > 1) {
return Block.__super__.icedCompileCps.call(this, o);
} else {
return this.compileNode(o);
}
};
Block.prototype.icedCpsRotate = function() {
var child, e, i, pivot, rest, _i, _j, _len, _len1, _ref2;
pivot = null;
_ref2 = this.expressions;
for (i = _i = 0, _len = _ref2.length; _i < _len; i = ++_i) {
e = _ref2[i];
if (e.icedIsCpsPivot()) {
pivot = e;
pivot.icedCallContinuation();
}
e.icedCpsRotate();
if (pivot) {
break;
}
}
if (!pivot) {
return this;
}
if (pivot.icedContinuationBlock) {
throw SyntaxError("unexpected continuation block in node");
}
rest = this.expressions.slice(i + 1);
this.expressions = this.expressions.slice(0, i + 1);
if (rest.length) {
child = new Block(rest);
pivot.icedNestContinuationBlock(child);
for (_j = 0, _len1 = rest.length; _j < _len1; _j++) {
e = rest[_j];
if (e.icedNodeFlag) {
child.icedNodeFlag = true;
}
if (e.icedLoopFlag) {
child.icedLoopFlag = true;
}
if (e.icedCpsPivotFlag) {
child.icedCpsPivotFlag = true;
}
if (e.icedHasAutocbFlag) {
child.icedHasAutocbFlag = true;
}
}
child.icedCpsRotate();
}
return this;
};
Block.prototype.icedAddRuntime = function(foundDefer, foundAwait) {
var index, node;
index = 0;
while ((node = this.expressions[index]) && node instanceof Comment || node instanceof Value && node.isString()) {
index++;
}
return this.expressions.splice(index, 0, new IcedRuntime(foundDefer, foundAwait));
};
Block.prototype.icedTransform = function(opts) {
var obj;
obj = {};
this.icedWalkAst(null, obj);
if (!(opts != null ? opts.repl : void 0) && (obj.foundDefer || obj.foundAwait || opts.runforce)) {
this.icedAddRuntime(obj.foundDefer, obj.foundAwait);
}
if (obj.foundAwait) {
this.icedWalkAstLoops(false);
this.icedWalkCpsPivots();
this.icedCpsRotate();
}
return this;
};
Block.prototype.icedGetSingle = function() {
if (this.expressions.length === 1) {
return this.expressions[0];
} else {
return null;
}
};
return Block;
})(Base);
exports.Literal = Literal = (function(_super) {
__extends(Literal, _super);
function Literal(value) {
this.value = value;
if (this.value == null) {
throw new Error("whoops!");
}
Literal.__super__.constructor.call(this);
}
Literal.prototype.makeReturn = function() {
if (this.isStatement()) {
return this;
} else {
return Literal.__super__.makeReturn.apply(this, arguments);
}
};
Literal.prototype.isAssignable = function() {
return IDENTIFIER.test(this.value);
};
Literal.prototype.isStatement = function() {
var _ref2;
return (_ref2 = this.value) === 'break' || _ref2 === 'continue' || _ref2 === 'debugger';
};
Literal.prototype.isComplex = NO;
Literal.prototype.assigns = function(name) {
return name === this.value;
};
Literal.prototype.jumps = function(o) {
if (this.value === 'break' && !((o != null ? o.loop : void 0) || (o != null ? o.block : void 0))) {
return this;
}
if (this.value === 'continue' && !(o != null ? o.loop : void 0)) {
return this;
}
};
Literal.prototype.compileNode = function(o) {
var answer, code, _ref2;
if (this.icedLoopFlag && this.icedIsJump()) {
return this.icedCompileIced(o);
}
code = this.value === 'this' ? ((_ref2 = o.scope.method) != null ? _ref2.bound : void 0) ? o.scope.method.context : this.value : this.value.reserved ? "\"" + this.value + "\"" : this.value;
answer = this.isStatement() ? "" + this.tab + code + ";" : code;
return [this.makeCode(answer)];
};
Literal.prototype.toString = function() {
return ' "' + this.value + '"';
};
Literal.prototype.icedWalkAst = function(parent, o) {
if (this.value === 'arguments' && o.foundAwaitFunc) {
o.foundArguments = true;
this.value = "_arguments";
}
return false;
};
Literal.prototype.icedIsJump = function() {
var _ref2;
return (_ref2 = this.value) === 'break' || _ref2 === 'continue';
};
Literal.prototype.icedCompileIced = function(o) {
var call, d, func, l;
d = {
'continue': iced["const"].c_while,
'break': iced["const"].b_while
};
l = d[this.value];
func = new Value(new Literal(l));
call = new Call(func, []);
return call.compileNode(o);
};
return Literal;
})(Base);
exports.Undefined = (function(_super) {
__extends(Undefined, _super);
function Undefined() {
return Undefined.__super__.constructor.apply(this, arguments);
}
Undefined.prototype.isAssignable = NO;
Undefined.prototype.isComplex = NO;
Undefined.prototype.compileNode = function(o) {
return [this.makeCode(o.level >= LEVEL_ACCESS ? '(void 0)' : 'void 0')];
};
return Undefined;
})(Base);
exports.Null = (function(_super) {
__extends(Null, _super);
function Null() {
return Null.__super__.constructor.apply(this, arguments);
}
Null.prototype.isAssignable = NO;
Null.prototype.isComplex = NO;
Null.prototype.compileNode = function() {
return [this.makeCode("null")];
};
return Null;
})(Base);
exports.Bool = (function(_super) {
__extends(Bool, _super);
Bool.prototype.isAssignable = NO;
Bool.prototype.isComplex = NO;
Bool.prototype.compileNode = function() {
return [this.makeCode(this.val)];
};
function Bool(val) {
this.val = val;
}
return Bool;
})(Base);
exports.Return = Return = (function(_super) {
__extends(Return, _super);
function Return(expr, auto) {
Return.__super__.constructor.call(this);
this.icedHasAutocbFlag = auto;
if (expr && !expr.unwrap().isUndefined) {
this.expression = expr;
}
}
Return.prototype.children = ['expression'];
Return.prototype.isStatement = YES;
Return.prototype.makeReturn = THIS;
Return.prototype.jumps = THIS;
Return.prototype.compileToFragments = function(o, level) {
var expr, _ref2;
expr = (_ref2 = this.expression) != null ? _ref2.makeReturn() : void 0;
if (expr && !(expr instanceof Return)) {
return expr.compileToFragments(o, level);
} else {
return Return.__super__.compileToFragments.call(this, o, level);
}
};
Return.prototype.compileNode = function(o) {
var answer;
if (this.icedHasAutocbFlag) {
return this.icedCompileIced(o);
}
answer = [];
answer.push(this.makeCode(this.tab + ("return" + (this.expression ? " " : ""))));
if (this.expression) {
answer = answer.concat(this.expression.compileToFragments(o, LEVEL_PAREN));
}
answer.push(this.makeCode(";"));
return answer;
};
Return.prototype.icedCompileIced = function(o) {
var args, block, call, cb, ret;
cb = new Value(new Literal(iced["const"].autocb));
args = this.expression ? [this.expression] : [];
call = new Call(cb, args);
ret = new Literal("return");
block = new Block([call, ret]);
return block.compileNode(o);
};
return Return;
})(Base);
exports.Value = Value = (function(_super) {
__extends(Value, _super);
function Value(base, props, tag) {
Value.__super__.constructor.call(this);
if (!props && base instanceof Value) {
return base;
}
this.base = base;
this.properties = props || [];
if (tag) {
this[tag] = true;
}
return this;
}
Value.prototype.children = ['base', 'properties'];
Value.prototype.copy = function() {
return new Value(this.base, this.properties);
};
Value.prototype.add = function(props) {
this.properties = this.properties.concat(props);
return this;
};
Value.prototype.hasProperties = function() {
return !!this.properties.length;
};
Value.prototype.bareLiteral = function(type) {
return !this.properties.length && this.base instanceof type;
};
Value.prototype.isArray = function() {
return this.bareLiteral(Arr);
};
Value.prototype.isRange = function() {
return this.bareLiteral(Range);
};
Value.prototype.isComplex = function() {
return this.hasProperties() || this.base.isComplex();
};
Value.prototype.isAssignable = function() {
return this.hasProperties() || this.base.isAssignable();
};
Value.prototype.isSimpleNumber = function() {
return this.bareLiteral(Literal) && SIMPLENUM.test(this.base.value);
};
Value.prototype.isString = function() {
return this.bareLiteral(Literal) && IS_STRING.test(this.base.value);
};
Value.prototype.isRegex = function() {
return this.bareLiteral(Literal) && IS_REGEX.test(this.base.value);
};
Value.prototype.isAtomic = function() {
var node, _i, _len, _ref2;
_ref2 = this.properties.concat(this.base);
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
node = _ref2[_i];
if (node.soak || node instanceof Call) {
return false;
}
}
return true;
};
Value.prototype.isNotCallable = function() {
return this.isSimpleNumber() || this.isString() || this.isRegex() || this.isArray() || this.isRange() || this.isSplice() || this.isObject();
};
Value.prototype.isStatement = function(o) {
return !this.properties.length && this.base.isStatement(o);
};
Value.prototype.assigns = function(name) {
return !this.properties.length && this.base.assigns(name);
};
Value.prototype.jumps = function(o) {
return !this.properties.length && this.base.jumps(o);
};
Value.prototype.isObject = function(onlyGenerated) {
if (this.properties.length) {
return false;
}
return (this.base instanceof Obj) && (!onlyGenerated || this.base.generated);
};
Value.prototype.isSplice = function() {
return last(this.properties) instanceof Slice;
};
Value.prototype.looksStatic = function(className) {
var _ref2;
return this.base.value === className && this.properties.length && ((_ref2 = this.properties[0].name) != null ? _ref2.value : void 0) !== 'prototype';
};
Value.prototype.unwrap = function() {
if (this.properties.length) {
return this;
} else {
return this.base;
}
};
Value.prototype.cacheReference = function(o) {
var base, bref, name, nref;
name = last(this.properties);
if (this.properties.length < 2 && !this.base.isComplex() && !(name != null ? name.isComplex() : void 0)) {
return [this, this];
}
base = new Value(this.base, this.properties.slice(0, -1));
if (base.isComplex()) {
bref = new Literal(o.scope.freeVariable('base'));
base = new Value(new Parens(new Assign(bref, base)));
}
if (!name) {
return [base, bref];
}
if (name.isComplex()) {
nref = new Literal(o.scope.freeVariable('name'));
name = new Index(new Assign(nref, name.index));
nref = new Index(nref);
}
return [base.add(name), new Value(bref || base.base, [nref || name])];
};
Value.prototype.compileNode = function(o) {
var fragments, prop, props, _i, _len;
this.base.front = this.front;
props = this.properties;
fragments = this.base.compileToFragments(o, (props.length ? LEVEL_ACCESS : null));
if ((this.base instanceof Parens || props.length) && SIMPLENUM.test(fragmentsToText(fragments))) {
fragments.push(this.makeCode('.'));
}
for (_i = 0, _len = props.length; _i < _len; _i++) {
prop = props[_i];
fragments.push.apply(fragments, prop.compileToFragments(o));
}
return fragments;
};
Value.prototype.unfoldSoak = function(o) {
return this.unfoldedSoak != null ? this.unfoldedSoak : this.unfoldedSoak = (function(_this) {
return function() {
var fst, i, ifn, prop, ref, snd, _i, _len, _ref2, _ref3;
if (ifn = _this.base.unfoldSoak(o)) {
(_ref2 = ifn.body.properties).push.apply(_ref2, _this.properties);
return ifn;
}
_ref3 = _this.properties;
for (i = _i = 0, _len = _ref3.length; _i < _len; i = ++_i) {
prop = _ref3[i];
if (!prop.soak) {
continue;
}
prop.soak = false;
fst = new Value(_this.base, _this.properties.slice(0, i));
snd = new Value(_this.base, _this.properties.slice(i));
if (fst.isComplex()) {
ref = new Literal(o.scope.freeVariable('ref'));
fst = new Parens(new Assign(ref, fst));
snd.base = ref;
}
return new If(new Existence(fst), snd, {
soak: true
});
}
return false;
};
})(this)();
};
Value.prototype.icedToSlot = function(i) {
var sufffix, suffix;
if (this.base instanceof Obj) {
return this.base.icedToSlot(i);
}
sufffix = null;
if (this.properties && this.properties.length) {
suffix = this.properties.pop();
}
return new Slot(i, this, suffix);
};
Value.prototype.icedToSlotAccess = function() {
if (this["this"]) {
return this.properties[0];
} else {
return new Access(this);
}
};
return Value;
})(Base);
exports.Comment = Comment = (function(_super) {
__extends(Comment, _super);
function Comment(comment) {
this.comment = comment;
Comment.__super__.constructor.call(this);
}
Comment.prototype.isStatement = YES;
Comment.prototype.makeReturn = THIS;
Comment.prototype.compileNode = function(o, level) {
var code, comment;
comment = this.comment.replace(/^(\s*)#/gm, "$1 *");
code = "/*" + (multident(comment, this.tab)) + (__indexOf.call(comment, '\n') >= 0 ? "\n" + this.tab : '') + " */";
if ((level || o.level) === LEVEL_TOP) {
code = o.indent + code;
}
return [this.makeCode("\n"), this.makeCode(code)];
};
return Comment;
})(Base);
exports.Call = Call = (function(_super) {
__extends(Call, _super);
function Call(variable, args, soak) {
this.args = args != null ? args : [];
this.soak = soak;
Call.__super__.constructor.call(this);
this.isNew = false;
this.isSuper = variable === 'super';
this.variable = this.isSuper ? null : variable;
if (variable instanceof Value && variable.isNotCallable()) {
variable.error("literal is not a function");
}
}
Call.prototype.children = ['variable', 'args'];
Call.prototype.newInstance = function() {
var base, _ref2;
base = ((_ref2 = this.variable) != null ? _ref2.base : void 0) || this.variable;
if (base instanceof Call && !base.isNew) {
base.newInstance();
} else {
this.isNew = true;
}
return this;
};
Call.prototype.superReference = function(o) {
var accesses, method;
method = o.scope.namedMethod();
if (method != null ? method.klass : void 0) {
accesses = [new Access(new Literal('__super__'))];
if (method["static"]) {
accesses.push(new Access(new Literal('constructor')));
}
accesses.push(new Access(new Literal(method.name)));
return (new Value(new Literal(method.klass), accesses)).compile(o);
} else if (method != null ? method.ctor : void 0) {
return "" + method.name + ".__super__.constructor";
} else {
return this.error('cannot call super outside of an instance method.');
}
};
Call.prototype.superThis = function(o) {
var method;
if (o.scope.icedgen) {
return "_this";
} else {
method = o.scope.method;
return (method && !method.klass && method.context) || "this";
}
};
Call.prototype.unfoldSoak = function(o) {
var call, ifn, left, list, rite, _i, _len, _ref2, _ref3;
if (this.soak) {
if (this.variable) {
if (ifn = unfoldSoak(o, this, 'variable')) {
return ifn;
}
_ref2 = new Value(this.variable).cacheReference(o), left = _ref2[0], rite = _ref2[1];
} else {
left = new Literal(this.superReference(o));
rite = new Value(left);
}
rite = new Call(rite, this.args);
rite.isNew = this.isNew;
left = new Literal("typeof " + (left.compile(o)) + " === \"function\"");
return new If(left, new Value(rite), {
soak: true
});
}
call = this;
list = [];
while (true) {
if (call.variable instanceof Call) {
list.push(call);
call = call.variable;
continue;
}
if (!(call.variable instanceof Value)) {
break;
}
list.push(call);
if (!((call = call.variable.base) instanceof Call)) {
break;
}
}
_ref3 = list.reverse();
for (_i = 0, _len = _ref3.length; _i < _len; _i++) {
call = _ref3[_i];
if (ifn) {
if (call.variable instanceof Call) {
call.variable = ifn;
} else {
call.variable.base = ifn;
}
}
ifn = unfoldSoak(o, call, 'variable');
}
return ifn;
};
Call.prototype.compileNode = function(o) {
var arg, argIndex, compiledArgs, compiledArray, fragments, preface, _i, _len, _ref2, _ref3;
if ((_ref2 = this.variable) != null) {
_ref2.front = this.front;
}
compiledArray = Splat.compileSplattedArray(o, this.args, true);
if (compiledArray.length) {
return this.compileSplat(o, compiledArray);
}
compiledArgs = [];
_ref3 = this.args;
for (argIndex = _i = 0, _len = _ref3.length; _i < _len; argIndex = ++_i) {
arg = _ref3[argIndex];
arg.icedStatementAssertion();
if (argIndex) {
compiledArgs.push(this.makeCode(", "));
}
compiledArgs.push.apply(compiledArgs, arg.compileToFragments(o, LEVEL_LIST));
}
fragments = [];
if (this.isSuper) {
preface = this.superReference(o) + (".call(" + (this.superThis(o)));
if (compiledArgs.length) {
preface += ", ";
}
fragments.push(this.makeCode(preface));
} else {
if (this.isNew) {
fragments.push(this.makeCode('new '));
}
fragments.push.apply(fragments, this.variable.compileToFragments(o, LEVEL_ACCESS));
fragments.push(this.makeCode("("));
}
fragments.push.apply(fragments, compiledArgs);
fragments.push(this.makeCode(")"));
return fragments;
};
Call.prototype.compileSplat = function(o, splatArgs) {
var answer, base, fun, idt, name, ref;
if (this.isSuper) {
return [].concat(this.makeCode("" + (this.superReference(o)) + ".apply(" + (this.superThis(o)) + ", "), splatArgs, this.makeCode(")"));
}
if (this.isNew) {
idt = this.tab + TAB;
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(){})"));
}
answer = [];
base = new Value(this.variable);
if ((name = base.properties.pop()) && base.isComplex()) {
ref = o.scope.freeVariable('ref');
answer = answer.concat(this.makeCode("(" + ref + " = "), base.compileToFragments(o, LEVEL_LIST), this.makeCode(")"), name.compileToFragments(o));
} else {
fun = base.compileToFragments(o, LEVEL_ACCESS);
if (SIMPLENUM.test(fragmentsToText(fun))) {
fun = this.wrapInBraces(fun);
}
if (name) {
ref = fragmentsToText(fun);
fun.push.apply(fun, name.compileToFragments(o));
} else {
ref = 'null';
}
answer = answer.concat(fun);
}
return answer = answer.concat(this.makeCode(".apply(" + ref + ", "), splatArgs, this.makeCode(")"));
};
return Call;
})(Base);
exports.Extends = Extends = (function(_super) {
__extends(Extends, _super);
function Extends(child, parent) {
this.child = child;
this.parent = parent;
Extends.__super__.constructor.call(this);
}
Extends.prototype.children = ['child', 'parent'];
Extends.prototype.compileToFragments = function(o) {
return new Call(new Value(new Literal(utility('extends'))), [this.child, this.parent]).compileToFragments(o);
};
return Extends;
})(Base);
exports.Access = Access = (function(_super) {
__extends(Access, _super);
function Access(name, tag) {
this.name = name;
Access.__super__.constructor.call(this);
this.name.asKey = true;
this.soak = tag === 'soak';
}
Access.prototype.children = ['name'];
Access.prototype.compileToFragments = function(o) {
var name;
name = this.name.compileToFragments(o);
if ((IDENTIFIER.test(fragmentsToText(name))) || this.name instanceof Defer) {
name.unshift(this.makeCode("."));
} else {
name.unshift(this.makeCode("["));
name.push(this.makeCode("]"));
}
return name;
};
Access.prototype.isComplex = NO;
return Access;
})(Base);
exports.Index = Index = (function(_super) {
__extends(Index, _super);
function Index(index) {
this.index = index;
Index.__super__.constructor.call(this);
}
Index.prototype.children = ['index'];
Index.prototype.compileToFragments = function(o) {
return [].concat(this.makeCode("["), this.index.compileToFragments(o, LEVEL_PAREN), this.makeCode("]"));
};
Index.prototype.isComplex = function() {
return this.index.isComplex();
};
return Index;
})(Base);
exports.Range = Range = (function(_super) {
__extends(Range, _super);
Range.prototype.children = ['from', 'to'];
function Range(from, to, tag) {
this.from = from;
this.to = to;
Range.__super__.constructor.call(this);
this.exclusive = tag === 'exclusive';
this.equals = this.exclusive ? '' : '=';
}
Range.prototype.compileVariables = function(o) {
var step, _ref2, _ref3, _ref4, _ref5;
o = merge(o, {
top: true
});
_ref2 = this.cacheToCodeFragments(this.from.cache(o, LEVEL_LIST)), this.fromC = _ref2[0], this.fromVar = _ref2[1];
_ref3 = this.cacheToCodeFragments(this.to.cache(o, LEVEL_LIST)), this.toC = _ref3[0], this.toVar = _ref3[1];
if (step = del(o, 'step')) {
_ref4 = this.cacheToCodeFragments(step.cache(o, LEVEL_LIST)), this.step = _ref4[0], this.stepVar = _ref4[1];
}
_ref5 = [this.fromVar.match(NUMBER), this.toVar.match(NUMBER)], this.fromNum = _ref5[0], this.toNum = _ref5[1];
if (this.stepVar) {
return this.stepNum = this.stepVar.match(NUMBER);
}
};
Range.prototype.compileNode = function(o) {
var cond, condPart, from, gt, idx, idxName, known, lt, namedIndex, stepPart, to, varPart, _ref2, _ref3;
if (!this.fromVar) {
this.compileVariables(o);
}
if (!o.index) {
return this.compileArray(o);
}
known = this.fromNum && this.toNum;
idx = del(o, 'index');
idxName = del(o, 'name');
namedIndex = idxName && idxName !== idx;
varPart = "" + idx + " = " + this.fromC;
if (this.toC !== this.toVar) {
varPart += ", " + this.toC;
}
if (this.step !== this.stepVar) {
varPart += ", " + this.step;
}
_ref2 = ["" + idx + " <" + this.equals, "" + idx + " >" + this.equals], lt = _ref2[0], gt = _ref2[1];
condPart = this.stepNum ? parseNum(this.stepNum[0]) > 0 ? "" + lt + " " + this.toVar : "" + gt + " " + this.toVar : known ? ((_ref3 = [parseNum(this.fromNum[0]), parseNum(this.toNum[0])], from = _ref3[0], to = _ref3[1], _ref3), from <= to ? "" + lt + " " + to : "" + gt + " " + to) : (cond = this.stepVar ? "" + this.stepVar + " > 0" : "" + this.fromVar + " <= " + this.toVar, "" + cond + " ? " + lt + " " + this.toVar + " : " + gt + " " + this.toVar);
stepPart = this.stepVar ? "" + idx + " += " + this.stepVar : known ? namedIndex ? from <= to ? "++" + idx : "--" + idx : from <= to ? "" + idx + "++" : "" + idx + "--" : namedIndex ? "" + cond + " ? ++" + idx + " : --" + idx : "" + cond + " ? " + idx + "++ : " + idx + "--";
if (namedIndex) {
varPart = "" + idxName + " = " + varPart;
}
if (namedIndex) {
stepPart = "" + idxName + " = " + stepPart;
}
return [this.makeCode("" + varPart + "; " + condPart + "; " + stepPart)];
};
Range.prototype.compileArray = function(o) {
var args, body, cond, hasArgs, i, idt, post, pre, range, result, vars, _i, _ref2, _ref3, _results;
if (this.fromNum && this.toNum && Math.abs(this.fromNum - this.toNum) <= 20) {
range = (function() {
_results = [];
for (var _i = _ref2 = +this.fromNum, _ref3 = +this.toNum; _ref2 <= _ref3 ? _i <= _ref3 : _i >= _ref3; _ref2 <= _ref3 ? _i++ : _i--){ _results.push(_i); }
return _results;
}).apply(this);
if (this.exclusive) {
range.pop();
}
return [this.makeCode("[" + (range.join(', ')) + "]")];
}
idt = this.tab + TAB;
i = o.scope.freeVariable('i');
result = o.scope.freeVariable('results');
pre = "\n" + idt + result + " = [];";
if (this.fromNum && this.toNum) {
o.index = i;
body = fragmentsToText(this.compileNode(o));
} else {
vars = ("" + i + " = " + this.fromC) + (this.toC !== this.toVar ? ", " + this.toC : '');
cond = "" + this.fromVar + " <= " + this.toVar;
body = "var " + vars + "; " + cond + " ? " + i + " <" + this.equals + " " + this.toVar + " : " + i + " >" + this.equals + " " + this.toVar + "; " + cond + " ? " + i + "++ : " + i + "--";
}
post = "{ " + result + ".push(" + i + "); }\n" + idt + "return " + result + ";\n" + o.indent;
hasArgs = function(node) {
return node != null ? node.contains(isLiteralArguments) : void 0;
};
if (hasArgs(this.from) || hasArgs(this.to)) {
args = ', arguments';
}
return [this.makeCode("(function() {" + pre + "\n" + idt + "for (" + body + ")" + post + "}).apply(this" + (args != null ? args : '') + ")")];
};
return Range;
})(Base);
exports.Slice = Slice = (function(_super) {
__extends(Slice, _super);
Slice.prototype.children = ['range'];
function Slice(range) {
this.range = range;
Slice.__super__.constructor.call(this);
}
Slice.prototype.compileNode = function(o) {
var compiled, compiledText, from, fromCompiled, to, toStr, _ref2;
_ref2 = this.range, to = _ref2.to, from = _ref2.from;
fromCompiled = from && from.compileToFragments(o, LEVEL_PAREN) || [this.makeCode('0')];
if (to) {
compiled = to.compileToFragments(o, LEVEL_PAREN);
compiledText = fragmentsToText(compiled);
if (!(!this.range.exclusive && +compiledText === -1)) {
toStr = ', ' + (this.range.exclusive ? compiledText : SIMPLENUM.test(compiledText) ? "" + (+compiledText + 1) : (compiled = to.compileToFragments(o, LEVEL_ACCESS), "+" + (fragmentsToText(compiled)) + " + 1 || 9e9"));
}
}
return [this.makeCode(".slice(" + (fragmentsToText(fromCompiled)) + (toStr || '') + ")")];
};
return Slice;
})(Base);
exports.Obj = Obj = (function(_super) {
__extends(Obj, _super);
function Obj(props, generated) {
this.generated = generated != null ? generated : false;
this.objects = this.properties = props || [];
Obj.__super__.constructor.call(this);
}
Obj.prototype.children = ['properties'];
Obj.prototype.compileNode = function(o) {
var answer, i, idt, indent, join, lastNoncom, node, prop, props, _i, _j, _len, _len1;
props = this.properties;
if (!props.length) {
return [this.makeCode(this.front ? '({})' : '{}')];
}
if (this.generated) {
for (_i = 0, _len = props.length; _i < _len; _i++) {
node = props[_i];
if (node instanceof Value) {
node.error('cannot have an implicit value in an implicit object');
}
}
}
idt = o.indent += TAB;
lastNoncom = this.lastNonComment(this.properties);
answer = [];
for (i = _j = 0, _len1 = props.length; _j < _len1; i = ++_j) {
prop = props[i];
join = i === props.length - 1 ? '' : prop === lastNoncom || prop instanceof Comment ? '\n' : ',\n';
indent = prop instanceof Comment ? '' : idt;
if (prop instanceof Assign && prop.variable instanceof Value && prop.variable.hasProperties()) {
prop.variable.error('Invalid object key');
}
if (prop instanceof Value && prop["this"]) {
prop = new Assign(prop.properties[0].name, prop, 'object');
}
if (!(prop instanceof Comment)) {
if (!(prop instanceof Assign)) {
prop = new Assign(prop, prop, 'object');
}
(prop.variable.base || prop.variable).asKey = true;
}
if (indent) {
answer.push(this.makeCode(indent));
}
answer.push.apply(answer, prop.compileToFragments(o, LEVEL_TOP));
if (join) {
answer.push(this.makeCode(join));
}
}
answer.unshift(this.makeCode("{" + (props.length && '\n')));
answer.push(this.makeCode("" + (props.length && '\n' + this.tab) + "}"));
if (this.front) {
return this.wrapInBraces(answer);
} else {
return answer;
}
};
Obj.prototype.assigns = function(name) {
var prop, _i, _len, _ref2;
_ref2 = this.properties;
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
prop = _ref2[_i];
if (prop.assigns(name)) {
return true;
}
}
return false;
};
Obj.prototype.icedToSlot = function(i) {
var access, prop, _i, _len, _ref2, _results;
_ref2 = this.properties;
_results = [];
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
prop = _ref2[_i];
if (prop instanceof Assign) {
_results.push((prop.value.icedToSlot(i)).addAccess(prop.variable.icedToSlotAccess()));
} else if (prop instanceof Value) {
access = prop.icedToSlotAccess();
_results.push((prop.icedToSlot(i)).addAccess(access));
} else {
_results.push(void 0);
}
}
return _results;
};
return Obj;
})(Base);
exports.Arr = Arr = (function(_super) {
__extends(Arr, _super);
function Arr(objs) {
this.objects = objs || [];
Arr.__super__.constructor.call(this);
}
Arr.prototype.children = ['objects'];
Arr.prototype.compileNode = function(o) {
var answer, compiledObjs, fragments, index, obj, _i, _len;
if (!this.objects.length) {
return [this.makeCode('[]')];
}
o.indent += TAB;
answer = Splat.compileSplattedArray(o, this.objects);
if (answer.length) {
return answer;
}
answer = [];
compiledObjs = (function() {
var _i, _len, _ref2, _results;
_ref2 = this.objects;
_results = [];
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
obj = _ref2[_i];
_results.push(obj.compileToFragments(o, LEVEL_LIST));
}
return _results;
}).call(this);
for (index = _i = 0, _len = compiledObjs.length; _i < _len; index = ++_i) {
fragments = compiledObjs[index];
if (index) {
answer.push(this.makeCode(", "));
}
answer.push.apply(answer, fragments);
}
if (fragmentsToText(answer).indexOf('\n') >= 0) {
answer.unshift(this.makeCode("[\n" + o.indent));
answer.push(this.makeCode("\n" + this.tab + "]"));
} else {
answer.unshift(this.makeCode("["));
answer.push(this.makeCode("]"));
}
return answer;
};
Arr.prototype.assigns = function(name) {
var obj, _i, _len, _ref2;
_ref2 = this.objects;
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
obj = _ref2[_i];
if (obj.assigns(name)) {
return true;
}
}
return false;
};
return Arr;
})(Base);
exports.Class = Class = (function(_super) {
__extends(Class, _super);
function Class(variable, parent, body) {
this.variable = variable;
this.parent = parent;
this.body = body != null ? body : new Block;
Class.__super__.constructor.call(this);
this.boundFuncs = [];
this.body.classBody = true;
}
Class.prototype.children = ['variable', 'parent', 'body'];
Class.prototype.determineName = function() {
var decl, tail;
if (!this.variable) {
return null;
}
decl = (tail = last(this.variable.properties)) ? tail instanceof Access && tail.name.value : this.variable.base.value;
if (__indexOf.call(STRICT_PROSCRIBED, decl) >= 0) {
this.variable.error("class variable name may not be " + decl);
}
return decl && (decl = IDENTIFIER.test(decl) && decl);
};
Class.prototype.setContext = function(name) {
return this.body.traverseChildren(false, function(node) {
if (node.classBody) {
return false;
}
if (node instanceof Literal && node.value === 'this') {
return node.value = name;
} else if (node instanceof Code) {
node.klass = name;
if (node.bound) {
return node.context = name;
}
}
});
};
Class.prototype.addBoundFunctions = function(o) {
var bvar, lhs, _i, _len, _ref2;
_ref2 = this.boundFuncs;
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
bvar = _ref2[_i];
lhs = (new Value(new Literal("this"), [new Access(bvar)])).compile(o);
this.ctor.body.unshift(new Literal("" + lhs + " = " + (utility('bind')) + "(" + lhs + ", this)"));
}
};
Class.prototype.addProperties = function(node, name, o) {
var assign, base, exprs, func, props;
props = node.base.properties.slice(0);
exprs = (function() {
var _results;
_results = [];
while (assign = props.shift()) {
if (assign instanceof Assign) {
base = assign.variable.base;
delete assign.context;
func = assign.value;
if (base.value === 'constructor') {
if (this.ctor) {
assign.error('cannot define more than one constructor in a class');
}
if (func.bound) {
assign.error('cannot define a constructor as a bound function');
}
if (func instanceof Code) {
assign = this.ctor = func;
} else {
this.externalCtor = o.classScope.freeVariable('class');
assign = new Assign(new Literal(this.externalCtor), func);
}
} else {
if (assign.variable["this"]) {
func["static"] = true;
} else {
assign.variable = new Value(new Literal(name), [new Access(new Literal('prototype')), new Access(base)]);
if (func instanceof Code && func.bound) {
this.boundFuncs.push(base);
func.bound = false;
}
}
}
}
_results.push(assign);
}
return _results;
}).call(this);
return compact(exprs);
};
Class.prototype.walkBody = function(name, o) {
return this.traverseChildren(false, (function(_this) {
return function(child) {
var cont, exps, i, node, _i, _len, _ref2;
cont = true;
if (child instanceof Class) {
return false;
}
if (child instanceof Block) {
_ref2 = exps = child.expressions;
for (i = _i = 0, _len = _ref2.length; _i < _len; i = ++_i) {
node = _ref2[i];
if (node instanceof Assign && node.variable.looksStatic(name)) {
node.value["static"] = true;
} else if (node instanceof Value && node.isObject(true)) {
cont = false;
exps[i] = _this.addProperties(node, name, o);
}
}
child.expressions = exps = flatten(exps);
}
return cont && !(child instanceof Class);
};
})(this));
};
Class.prototype.hoistDirectivePrologue = function() {
var expressions, index, node;
index = 0;
expressions = this.body.expressions;
while ((node = expressions[index]) && node instanceof Comment || node instanceof Value && node.isString()) {
++index;
}
return this.directives = expressions.splice(0, index);
};
Class.prototype.ensureConstructor = function(name) {
if (!this.ctor) {
this.ctor = new Code;
if (this.externalCtor) {
this.ctor.body.push(new Literal("" + this.externalCtor + ".apply(this, arguments)"));
} else if (this.parent) {
this.ctor.body.push(new Literal("" + name + ".__super__.constructor.apply(this, arguments)"));
}
this.ctor.body.makeReturn();
this.body.expressions.unshift(this.ctor);
}
this.ctor.ctor = this.ctor.name = name;
this.ctor.klass = null;
return this.ctor.noReturn = true;
};
Class.prototype.compileNode = function(o) {
var args, argumentsNode, func, jumpNode, klass, lname, name, superClass, _ref2;
if (jumpNode = this.body.jumps()) {
jumpNode.error('Class bodies cannot contain pure statements');
}
if (argumentsNode = this.body.contains(isLiteralArguments)) {
argumentsNode.error("Class bodies shouldn't reference arguments");
}
name = this.determineName() || '_Class';
if (name.reserved) {
name = "_" + name;
}
lname = new Literal(name);
func = new Code([], Block.wrap([this.body]));
args = [];
o.classScope = func.makeScope(o.scope);
this.hoistDirectivePrologue();
this.setContext(name);
this.walkBody(name, o);
this.ensureConstructor(name);
this.addBoundFunctions(o);
this.body.spaced = true;
this.body.expressions.push(lname);
if (this.parent) {
superClass = new Literal(o.classScope.freeVariable('super', false));
this.body.expressions.unshift(new Extends(lname, superClass));
func.params.push(new Param(superClass));
args.push(this.parent);
}
(_ref2 = this.body.expressions).unshift.apply(_ref2, this.directives);
klass = new Parens(new Call(func, args));
if (this.variable) {
klass = new Assign(this.variable, klass);
}
return klass.compileToFragments(o);
};
return Class;
})(Base);
exports.Assign = Assign = (function(_super) {
__extends(Assign, _super);
function Assign(variable, value, context, options) {
var forbidden, name, _ref2;
this.variable = variable;
this.value = value;
this.context = context;
Assign.__super__.constructor.call(this);
this.param = options && options.param;
this.subpattern = options && options.subpattern;
forbidden = (_ref2 = (name = this.variable.unwrapAll().value), __indexOf.call(STRICT_PROSCRIBED, _ref2) >= 0);
if (forbidden && this.context !== 'object') {
this.variable.error("variable name may not be \"" + name + "\"");
}
this.icedlocal = options && options.icedlocal;
}
Assign.prototype.children = ['variable', 'value'];
Assign.prototype.isStatement = function(o) {
return (o != null ? o.level : void 0) === LEVEL_TOP && (this.context != null) && __indexOf.call(this.context, "?") >= 0;
};
Assign.prototype.assigns = function(name) {
return this[this.context === 'object' ? 'value' : 'variable'].assigns(name);
};
Assign.prototype.unfoldSoak = function(o) {
return unfoldSoak(o, this, 'variable');
};
Assign.prototype.compileNode = function(o) {
var answer, compiledName, isValue, match, name, val, varBase, _ref2, _ref3, _ref4, _ref5;
this.value.icedStatementAssertion();
if (isValue = this.variable instanceof Value) {
if (this.variable.isArray() || this.variable.isObject()) {
return this.compilePatternMatch(o);
}
if (this.variable.isSplice()) {
return this.compileSplice(o);
}
if ((_ref2 = this.context) === '||=' || _ref2 === '&&=' || _ref2 === '?=') {
return this.compileConditional(o);
}
if ((_ref3 = this.context) === '**=' || _ref3 === '//=' || _ref3 === '%%=') {
return this.compileSpecialMath(o);
}
}
compiledName = this.variable.compileToFragments(o, LEVEL_LIST);
name = fragmentsToText(compiledName);
if (!this.context) {
varBase = this.variable.unwrapAll();
if (!varBase.isAssignable()) {
this.variable.error("\"" + (this.variable.compile(o)) + "\" cannot be assigned");
}
if (!(typeof varBase.hasProperties === "function" ? varBase.hasProperties() : void 0)) {
if (this.param || this.icedlocal) {
o.scope.add(name, 'var', this.icedlocal);
} else {
o.scope.find(name);
}
}
}
if (this.value instanceof Code && (match = METHOD_DEF.exec(name))) {
if (match[2]) {
this.value.klass = match[1];
}
this.value.name = (_ref4 = (_ref5 = match[3]) != null ? _ref5 : match[4]) != null ? _ref4 : match[5];
}
val = this.value.compileToFragments(o, LEVEL_LIST);
if (this.context === 'object') {
return compiledName.concat(this.makeCode(": "), val);
}
answer = compiledName.concat(this.makeCode(" " + (this.context || '=') + " "), val);
if (o.level <= LEVEL_LIST) {
return answer;
} else {
return this.wrapInBraces(answer);
}
};
Assign.prototype.compilePatternMatch = function(o) {
var acc, assigns, code, expandedIdx, fragments, i, idx, isObject, ivar, name, obj, objects, olen, ref, rest, top, val, value, vvar, vvarText, _i, _len, _ref2, _ref3, _ref4, _ref5, _ref6, _ref7;
top = o.level === LEVEL_TOP;
value = this.value;
objects = this.variable.base.objects;
if (!(olen = objects.length)) {
code = value.compileToFragments(o);
if (o.level >= LEVEL_OP) {
return this.wrapInBraces(code);
} else {
return code;
}
}
isObject = this.variable.isObject();
if (top && olen === 1 && !((obj = objects[0]) instanceof Splat)) {
if (obj instanceof Assign) {
_ref2 = obj, (_ref3 = _ref2.variable, idx = _ref3.base), obj = _ref2.value;
} else {
idx = isObject ? obj["this"] ? obj.properties[0].name : obj : new Literal(0);
}
acc = IDENTIFIER.test(idx.unwrap().value || 0);
value = new Value(value);
value.properties.push(new (acc ? Access : Index)(idx));
if (_ref4 = obj.unwrap().value, __indexOf.call(RESERVED, _ref4) >= 0) {
obj.error("assignment to a reserved word: " + (obj.compile(o)));
}
return new Assign(obj, value, null, {
param: this.param
}).compileToFragments(o, LEVEL_TOP);
}
vvar = value.compileToFragments(o, LEVEL_LIST);
vvarText = fragmentsToText(vvar);
assigns = [];
expandedIdx = false;
if (!IDENTIFIER.test(vvarText) || this.variable.assigns(vvarText)) {
assigns.push([this.makeCode("" + (ref = o.scope.freeVariable('ref')) + " = ")].concat(__slice.call(vvar)));
vvar = [this.makeCode(ref)];
vvarText = ref;
}
for (i = _i = 0, _len = objects.length; _i < _len; i = ++_i) {
obj = objects[i];
idx = i;
if (isObject) {
if (obj instanceof Assign) {
_ref5 = obj, (_ref6 = _ref5.variable, idx = _ref6.base), obj = _ref5.value;
} else {
if (obj.base instanceof Parens) {
_ref7 = new Value(obj.unwrapAll()).cacheReference(o), obj = _ref7[0], idx = _ref7[1];
} else {
idx = obj["this"] ? obj.properties[0].name : obj;
}
}
}
if (!expandedIdx && obj instanceof Splat) {
name = obj.name.unwrap().value;
obj = obj.unwrap();
val = "" + olen + " <= " + vvarText + ".length ? " + (utility('slice')) + ".call(" + vvarText + ", " + i;
if (rest = olen - i - 1) {
ivar = o.scope.freeVariable('i');
val += ", " + ivar + " = " + vvarText + ".length - " + rest + ") : (" + ivar + " = " + i + ", [])";
} else {
val += ") : []";
}
val = new Literal(val);
expandedIdx = "" + ivar + "++";
} else if (!expandedIdx && obj instanceof Expansion) {
if (rest = olen - i - 1) {
if (rest === 1) {
expandedIdx = "" + vvarText + ".length - 1";
} else {
ivar = o.scope.freeVariable('i');
val = new Literal("" + ivar + " = " + vvarText + ".length - " + rest);
expandedIdx = "" + ivar + "++";
assigns.push(val.compileToFragments(o, LEVEL_LIST));
}
}
continue;
} else {
name = obj.unwrap().value;
if (obj instanceof Splat || obj instanceof Expansion) {
obj.error("multiple splats/expansions are disallowed in an assignment");
}
if (typeof idx === 'number') {
idx = new Literal(expandedIdx || idx);
acc = false;
} else {
acc = isObject && IDENTIFIER.test(idx.unwrap().value || 0);
}
val = new Value(new Literal(vvarText), [new (acc ? Access : Index)(idx)]);
}
if ((name != null) && __indexOf.call(RESERVED, name) >= 0) {
obj.error("assignment to a reserved word: " + (obj.compile(o)));
}
assigns.push(new Assign(obj, val, null, {
param: this.param,
subpattern: true
}).compileToFragments(o, LEVEL_LIST));
}
if (!(top || this.subpattern)) {
assigns.push(vvar);
}
fragments = this.joinFragmentArrays(assigns, ', ');
if (o.level < LEVEL_LIST) {
return fragments;
} else {
return this.wrapInBraces(fragments);
}
};
Assign.prototype.compileConditional = function(o) {
var fragments, left, right, _ref2;
_ref2 = this.variable.cacheReference(o), left = _ref2[0], right = _ref2[1];
if (!left.properties.length && left.base instanceof Literal && left.base.value !== "this" && !o.scope.check(left.base.value)) {
this.variable.error("the variable \"" + left.base.value + "\" can't be assigned with " + this.context + " because it has not been declared before");
}
if (__indexOf.call(this.context, "?") >= 0) {
o.isExistentialEquals = true;
return new If(new Existence(left), right, {
type: 'if'
}).addElse(new Assign(right, this.value, '=')).compileToFragments(o);
} else {
fragments = new Op(this.context.slice(0, -1), left, new Assign(right, this.value, '=')).compileToFragments(o);
if (o.level <= LEVEL_LIST) {
return fragments;
} else {
return this.wrapInBraces(fragments);
}
}
};
Assign.prototype.compileSpecialMath = function(o) {
var left, right, _ref2;
_ref2 = this.variable.cacheReference(o), left = _ref2[0], right = _ref2[1];
return new Assign(left, new Op(this.context.slice(0, -1), right, this.value)).compileToFragments(o);
};
Assign.prototype.compileSplice = function(o) {
var answer, exclusive, from, fromDecl, fromRef, name, to, valDef, valRef, _ref2, _ref3, _ref4;
_ref2 = this.variable.properties.pop().range, from = _ref2.from, to = _ref2.to, exclusive = _ref2.exclusive;
name = this.variable.compile(o);
if (from) {
_ref3 = this.cacheToCodeFragments(from.cache(o, LEVEL_OP)), fromDecl = _ref3[0], fromRef = _ref3[1];
} else {
fromDecl = fromRef = '0';
}
if (to) {
if (from instanceof Value && from.isSimpleNumber() && to instanceof Value && to.isSimpleNumber()) {
to = to.compile(o) - fromRef;
if (!exclusive) {
to += 1;
}
} else {
to = to.compile(o, LEVEL_ACCESS) + ' - ' + fromRef;
if (!exclusive) {
to += ' + 1';
}
}
} else {
to = "9e9";
}
_ref4 = this.value.cache(o, LEVEL_LIST), valDef = _ref4[0], valRef = _ref4[1];
answer = [].concat(this.makeCode("[].splice.apply(" + name + ", [" + fromDecl + ", " + to + "].concat("), valDef, this.makeCode(")), "), valRef);
if (o.level > LEVEL_TOP) {
return this.wrapInBraces(answer);
} else {
return answer;
}
};
return Assign;
})(Base);
exports.Code = Code = (function(_super) {
__extends(Code, _super);
function Code(params, body, tag) {
Code.__super__.constructor.call(this);
this.params = params || [];
this.body = body || new Block;
this.icedgen = tag === 'icedgen';
this.icedPassedDeferral = null;
this.bound = tag === 'boundfunc' || this.icedgen;
}
Code.prototype.children = ['params', 'body'];
Code.prototype.isStatement = function() {
return !!this.ctor;
};
Code.prototype.jumps = NO;
Code.prototype.makeScope = function(parentScope) {
return new Scope(parentScope, this.body, this);
};
Code.prototype.compileNode = function(o) {
var answer, boundfunc, code, exprs, i, lit, p, param, params, ref, splats, uniqs, val, wasEmpty, wrapper, _i, _j, _k, _l, _len, _len1, _len2, _len3, _len4, _len5, _m, _n, _ref2, _ref3, _ref4, _ref5, _ref6, _ref7;
if (this.bound && ((_ref2 = o.scope.method) != null ? _ref2.bound : void 0)) {
this.context = o.scope.method.context;
}
if (this.bound && !this.context) {
this.context = '_this';
wrapper = new Code([new Param(new Literal(this.context))], new Block([this]));
boundfunc = new Call(wrapper, [new Literal('this')]);
boundfunc.updateLocationDataIfMissing(this.locationData);
return boundfunc.compileNode(o);
}
o.scope = del(o, 'classScope') || this.makeScope(o.scope);
o.scope.shared = del(o, 'sharedScope') || this.icedgen;
o.scope.icedgen = this.icedgen;
o.indent += TAB;
delete o.bare;
delete o.isExistentialEquals;
params = [];
exprs = [];
_ref3 = this.params;
for (_i = 0, _len = _ref3.length; _i < _len; _i++) {
param = _ref3[_i];
if (!(param instanceof Expansion)) {
o.scope.parameter(param.asReference(o));
}
}
_ref4 = this.params;
for (_j = 0, _len1 = _ref4.length; _j < _len1; _j++) {
param = _ref4[_j];
if (!(param.splat || param instanceof Expansion)) {
continue;
}
_ref5 = this.params;
for (_k = 0, _len2 = _ref5.length; _k < _len2; _k++) {
p = _ref5[_k].name;
if (!(!(param instanceof Expansion))) {
continue;
}
if (p["this"]) {
p = p.properties[0].name;
}
if (p.value) {
o.scope.add(p.value, 'var', true);
}
}
splats = new Assign(new Value(new Arr((function() {
var _l, _len3, _ref6, _results;
_ref6 = this.params;
_results = [];
for (_l = 0, _len3 = _ref6.length; _l < _len3; _l++) {
p = _ref6[_l];
_results.push(p.asReference(o));
}
return _results;
}).call(this))), new Value(new Literal('arguments')));
break;
}
_ref6 = this.params;
for (_l = 0, _len3 = _ref6.length; _l < _len3; _l++) {
param = _ref6[_l];
if (param.isComplex()) {
val = ref = param.asReference(o);
if (param.value) {
val = new Op('?', ref, param.value);
}
exprs.push(new Assign(new Value(param.name), val, '=', {
param: true
}));
} else {
ref = param;
if (param.value) {
lit = new Literal(ref.name.value + ' == null');
val = new Assign(new Value(param.name), param.value, '=');
exprs.push(new If(lit, val));
}
}
if (!splats) {
params.push(ref);
}
}
wasEmpty = this.body.isEmpty();
if (splats) {
exprs.unshift(splats);
}
if (exprs.length) {
(_ref7 = this.body.expressions).unshift.apply(_ref7, exprs);
}
for (i = _m = 0, _len4 = params.length; _m < _len4; i = ++_m) {
p = params[i];
params[i] = p.compileToFragments(o);
o.scope.parameter(fragmentsToText(params[i]));
}
uniqs = [];
this.eachParamName(function(name, node) {
if (__indexOf.call(uniqs, name) >= 0) {
node.error("multiple parameters named '" + name + "'");
}
return uniqs.push(name);
});
if (this.icedHasAutocbFlag) {
wasEmpty = false;
}
if (!(wasEmpty || this.noReturn)) {
this.body.makeReturn();
}
code = 'function';
if (this.ctor) {
code += ' ' + this.name;
}
code += '(';
answer = [this.makeCode(code)];
for (i = _n = 0, _len5 = params.length; _n < _len5; i = ++_n) {
p = params[i];
if (i) {
answer.push(this.makeCode(", "));
}
answer.push.apply(answer, p);
}
answer.push(this.makeCode(') {'));
this.icedPatchBody(o);
if (!this.body.isEmpty()) {
answer = answer.concat(this.makeCode("\n"), this.body.compileWithDeclarations(o), this.makeCode("\n" + this.tab));
}
answer.push(this.makeCode('}'));
if (this.ctor) {
return [this.makeCode(this.tab)].concat(__slice.call(answer));
}
if (this.front || (o.level >= LEVEL_ACCESS)) {
return this.wrapInBraces(answer);
} else {
return answer;
}
};
Code.prototype.eachParamName = function(iterator) {
var param, _i, _len, _ref2, _results;
_ref2 = this.params;
_results = [];
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
param = _ref2[_i];
_results.push(param.eachName(iterator));
}
return _results;
};
Code.prototype.traverseChildren = function(crossScope, func) {
if (crossScope) {
return Code.__super__.traverseChildren.call(this, crossScope, func);
}
};
Code.prototype.icedPatchBody = function(o) {
var f, lhs, r, rhs;
if (this.icedFoundArguments && this.icedNodeFlag) {
o.scope.assign('_arguments', 'arguments');
}
if (this.icedNodeFlag && !this.icedgen) {
this.icedPassedDeferral = o.scope.freeVariable(iced["const"].passed_deferral);
lhs = new Value(new Literal(this.icedPassedDeferral));
f = new Value(new Literal(iced["const"].ns));
f.add(new Access(new Value(new Literal(iced["const"].findDeferral))));
rhs = new Call(f, [new Value(new Literal('arguments'))]);
this.body.unshift(new Assign(lhs, rhs));
}
if (this.icedNodeFlag && !this.icedgen) {
r = this.icedHasAutocbFlag ? iced["const"].autocb : iced["const"].k_noop;
rhs = new Value(new Literal(r));
lhs = new Value(new Literal(iced["const"].k));
return this.body.unshift(new Assign(lhs, rhs, null, {
icedlocal: true
}));
}
};
Code.prototype.icedWalkAst = function(parent, o) {
var cf_prev, fa_prev, faf_prev, fg_prev, param, _i, _len, _ref2;
this.icedParentAwait = parent;
fa_prev = o.foundAutocb;
cf_prev = o.currFunc;
fg_prev = o.foundArguments;
faf_prev = o.foundAwaitFunc;
o.foundAutocb = false;
o.foundArguments = false;
o.foundAwaitFunc = false;
o.currFunc = this;
_ref2 = this.params;
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
param = _ref2[_i];
if (param.name instanceof Literal && param.name.value === iced["const"].autocb) {
o.foundAutocb = true;
break;
}
}
this.icedHasAutocbFlag = o.foundAutocb;
Code.__super__.icedWalkAst.call(this, parent, o);
this.icedFoundArguments = o.foundArguments;
o.foundAwaitFunc = faf_prev;
o.foundArguments = fg_prev;
o.foundAutocb = fa_prev;
o.currFunc = cf_prev;
return false;
};
Code.prototype.icedWalkAstLoops = function(flood) {
if (Code.__super__.icedWalkAstLoops.call(this, false)) {
this.icedLoopFlag = true;
}
return false;
};
Code.prototype.icedWalkCpsPivots = function() {
Code.__super__.icedWalkCpsPivots.call(this);
return this.icedCpsPivotFlag = false;
};
Code.prototype.icedTraceName = function() {
var parts;
parts = [];
if (this.klass) {
parts.push(this.klass);
}
if (this.name) {
parts.push(this.name);
}
return parts.join('.');
};
return Code;
})(Base);
exports.Param = Param = (function(_super) {
__extends(Param, _super);
function Param(name, value, splat) {
var _ref2;
this.name = name;
this.value = value;
this.splat = splat;
Param.__super__.constructor.call(this);
if (_ref2 = (name = this.name.unwrapAll().value), __indexOf.call(STRICT_PROSCRIBED, _ref2) >= 0) {
this.name.error("parameter name \"" + name + "\" is not allowed");
}
}
Param.prototype.children = ['name', 'value'];
Param.prototype.compileToFragments = function(o) {
return this.name.compileToFragments(o, LEVEL_LIST);
};
Param.prototype.asReference = function(o) {
var node;
if (this.reference) {
return this.reference;
}
node = this.name;
if (node["this"]) {
node = node.properties[0].name;
if (node.value.reserved) {
node = new Literal(o.scope.freeVariable(node.value));
}
} else if (node.isComplex()) {
node = new Literal(o.scope.freeVariable('arg'));
}
node = new Value(node);
if (this.splat) {
node = new Splat(node);
}
node.updateLocationDataIfMissing(this.locationData);
return this.reference = node;
};
Param.prototype.isComplex = function() {
return this.name.isComplex();
};
Param.prototype.eachName = function(iterator, name) {
var atParam, node, obj, _i, _len, _ref2;
if (name == null) {
name = this.name;
}
atParam = function(obj) {
var node;
node = obj.properties[0].name;
if (!node.value.reserved) {
return iterator(node.value, node);
}
};
if (name instanceof Literal) {
return iterator(name.value, name);
}
if (name instanceof Value) {
return atParam(name);
}
_ref2 = name.objects;
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
obj = _ref2[_i];
if (obj instanceof Assign) {
this.eachName(iterator, obj.value.unwrap());
} else if (obj instanceof Splat) {
node = obj.name.unwrap();
iterator(node.value, node);
} else if (obj instanceof Value) {
if (obj.isArray() || obj.isObject()) {
this.eachName(iterator, obj.base);
} else if (obj["this"]) {
atParam(obj);
} else {
iterator(obj.base.value, obj.base);
}
} else if (!(obj instanceof Expansion)) {
obj.error("illegal parameter " + (obj.compile()));
}
}
};
return Param;
})(Base);
exports.Splat = Splat = (function(_super) {
__extends(Splat, _super);
Splat.prototype.children = ['name'];
Splat.prototype.isAssignable = YES;
function Splat(name) {
Splat.__super__.constructor.call(this);
this.name = name.compile ? name : new Literal(name);
}
Splat.prototype.assigns = function(name) {
return this.name.assigns(name);
};
Splat.prototype.compileToFragments = function(o) {
return this.name.compileToFragments(o);
};
Splat.prototype.unwrap = function() {
return this.name;
};
Splat.compileSplattedArray = function(o, list, apply) {
var args, base, compiledNode, concatPart, fragments, i, index, node, _i, _len;
index = -1;
while ((node = list[++index]) && !(node instanceof Splat)) {
continue;
}
if (index >= list.length) {
return [];
}
if (list.length === 1) {
node = list[0];
fragments = node.compileToFragments(o, LEVEL_LIST);
if (apply) {
return fragments;
}
return [].concat(node.makeCode("" + (utility('slice')) + ".call("), fragments, node.makeCode(")"));
}
args = list.slice(index);
for (i = _i = 0, _len = args.length; _i < _len; i = ++_i) {
node = args[i];
compiledNode = node.compileToFragments(o, LEVEL_LIST);
args[i] = node instanceof Splat ? [].concat(node.makeCode("" + (utility('slice')) + ".call("), compiledNode, node.makeCode(")")) : [].concat(node.makeCode("["), compiledNode, node.makeCode("]"));
}
if (index === 0) {
node = list[0];
concatPart = node.joinFragmentArrays(args.slice(1), ', ');
return args[0].concat(node.makeCode(".concat("), concatPart, node.makeCode(")"));
}
base = (function() {
var _j, _len1, _ref2, _results;
_ref2 = list.slice(0, index);
_results = [];
for (_j = 0, _len1 = _ref2.length; _j < _len1; _j++) {
node = _ref2[_j];
_results.push(node.compileToFragments(o, LEVEL_LIST));
}
return _results;
})();
base = list[0].joinFragmentArrays(base, ', ');
concatPart = list[index].joinFragmentArrays(args, ', ');
return [].concat(list[0].makeCode("["), base, list[index].makeCode("].concat("), concatPart, (last(list)).makeCode(")"));
};
Splat.prototype.icedToSlot = function(i) {
return new Slot(i, new Value(this.name), null, true);
};
return Splat;
})(Base);
exports.Expansion = Expansion = (function(_super) {
__extends(Expansion, _super);
function Expansion() {
return Expansion.__super__.constructor.apply(this, arguments);
}
Expansion.prototype.isComplex = NO;
Expansion.prototype.compileNode = function(o) {
return this.error('Expansion must be used inside a destructuring assignment or parameter list');
};
Expansion.prototype.asReference = function(o) {
return this;
};
Expansion.prototype.eachName = function(iterator) {};
return Expansion;
})(Base);
exports.While = While = (function(_super) {
__extends(While, _super);
function While(condition, options) {
this.condition = (options != null ? options.invert : void 0) ? condition.invert() : condition;
this.guard = options != null ? options.guard : void 0;
}
While.prototype.children = ['condition', 'guard', 'body'];
While.prototype.isStatement = YES;
While.prototype.isLoop = YES;
While.prototype.makeReturn = function(res) {
if (res) {
return While.__super__.makeReturn.apply(this, arguments);
} else {
this.returns = !this.jumps({
loop: true
});
return this;
}
};
While.prototype.addBody = function(body) {
this.body = body;
return this;
};
While.prototype.jumps = function() {
var expressions, jumpNode, node, _i, _len;
expressions = this.body.expressions;
if (!expressions.length) {
return false;
}
for (_i = 0, _len = expressions.length; _i < _len; _i++) {
node = expressions[_i];
if (jumpNode = node.jumps({
loop: true
})) {
return jumpNode;
}
}
return false;
};
While.prototype.compileNode = function(o) {
var answer, body, rvar, set;
this.condition.icedStatementAssertion();
if (this.icedNodeFlag) {
return this.icedCompileIced(o);
}
o.indent += TAB;
set = '';
body = this.body;
if (body.isEmpty()) {
body = this.makeCode('');
} else {
if (this.returns) {
body.makeReturn(rvar = o.scope.freeVariable('results'));
set = "" + this.tab + rvar + " = [];\n";
}
if (this.guard) {
if (body.expressions.length > 1) {
body.expressions.unshift(new If((new Parens(this.guard)).invert(), new Literal("continue")));
} else {
if (this.guard) {
body = Block.wrap([new If(this.guard, body)]);
}
}
}
body = [].concat(this.makeCode("\n"), body.compileToFragments(o, LEVEL_TOP), this.makeCode("\n" + this.tab));
}
answer = [].concat(this.makeCode(set + this.tab + "while ("), this.condition.compileToFragments(o, LEVEL_PAREN), this.makeCode(") {"), body, this.makeCode("}"));
if (this.returns) {
if (this.icedHasAutocbFlag) {
answer.push(this.makeCode("\n" + this.tab + iced["const"].autocb + "(" + rvar + ");"));
answer.push(this.makeCode("\n" + this.tab + "return;"));
} else {
answer.push(this.makeCode("\n" + this.tab + "return " + rvar + ";"));
}
}
return answer;
};
While.prototype.icedWrap = function(d) {
var body, break_assign, break_block, break_body, break_expr, break_id, call1, call2, cond, condition, continue_assign, continue_block, continue_block_inner, continue_body, continue_fn, continue_id, f, guard_if, k_id, k_param, next_arg, next_assign, next_block, next_body, next_id, outStatements, rvar, rvar_init, rvar_value, top_assign, top_block, top_body, top_call, top_func, top_id, top_statements, tramp;
condition = d.condition;
body = d.body;
rvar = d.rvar;
outStatements = [];
if (rvar) {
rvar_value = new Value(new Literal(rvar));
}
top_id = new Value(new Literal(iced["const"].t_while));
k_id = new Value(new Literal(iced["const"].k));
k_param = new Param(new Literal(iced["const"].k));
break_id = new Value(new Literal(iced["const"].b_while));
if (rvar) {
break_expr = new Call(k_id, [rvar_value]);
break_block = new Block([break_expr]);
break_body = new Code([], break_block, 'icedgen');
break_assign = new Assign(break_id, break_body, null, {
icedlocal: true
});
} else {
break_assign = new Assign(break_id, k_id, null, {
icedlocal: true
});
}
continue_id = new Value(new Literal(iced["const"].c_while));
continue_block_inner = new Block([new Call(top_id, [k_id])]);
if (d.step) {
continue_block_inner.unshift(d.step);
}
continue_fn = new Code([], continue_block_inner);
tramp = new Value(new Literal(iced["const"].ns));
tramp.add(new Access(new Value(new Literal(iced["const"].trampoline))));
continue_block = new Block([new Call(tramp, [continue_fn])]);
continue_body = new Code([], continue_block, 'icedgen');
continue_assign = new Assign(continue_id, continue_body, null, {
icedlocal: true
});
next_id = new Value(new Literal(iced["const"].n_while));
if (rvar) {
next_arg = new Param(new Literal(iced["const"].n_arg));
f = rvar_value.copy();
f.add(new Access(new Value(new Literal('push'))));
call1 = new Call(f, [next_arg]);
call2 = new Call(continue_id, []);
next_block = new Block([call1, call2]);
next_body = new Code([next_arg], next_block, 'icedgen');
next_assign = new Assign(next_id, next_body, null, {
icedlocal: true
});
} else {
next_assign = new Assign(next_id, continue_id);
}
cond = new If(condition.invert(), new Block([new Call(break_id, [])]));
if (d.guard) {
continue_block = new Block([new Call(continue_id, [])]);
guard_if = new If(d.guard, body);
guard_if.addElse(continue_block);
cond.addElse(new Block([d.pre_body, guard_if]));
} else {
cond.addElse(new Block([d.pre_body, body]));
}
top_body = new Block([break_assign, continue_assign, next_assign, cond]);
top_func = new Code([k_param], top_body, 'icedgen');
top_assign = new Assign(top_id, top_func, null, {
icedlocal: true
});
top_call = new Call(top_id, [k_id]);
top_statements = [];
if (d.init) {
top_statements = top_statements.concat(d.init);
}
if (rvar) {
rvar_init = new Assign(rvar_value, new Arr);
top_statements.push(rvar_init);
}
top_statements = top_statements.concat([top_assign, top_call]);
return top_block = new Block(top_statements);
};
While.prototype.icedCallContinuation = function() {
return this.body.icedThreadReturn(new IcedTailCall(iced["const"].n_while));
};
While.prototype.icedCompileIced = function(o) {
var b, opts;
opts = {
condition: this.condition,
body: this.body,
guard: this.guard
};
if (this.returns) {
opts.rvar = o.scope.freeVariable('results');
}
b = this.icedWrap(opts);
return b.compileNode(o);
};
return While;
})(Base);
exports.Op = Op = (function(_super) {
var CONVERSIONS, INVERSIONS;
__extends(Op, _super);
function Op(op, first, second, flip) {
Op.__super__.constructor.call(this);
if (op === 'in') {
return new In(first, second);
}
if (op === 'do') {
return this.generateDo(first);
}
if (op === 'new') {
if (first instanceof Call && !first["do"] && !first.isNew) {
return first.newInstance();
}
if (first instanceof Code && first.bound || first["do"]) {
first = new Parens(first);
}
}
this.operator = CONVERSIONS[op] || op;
this.first = first;
this.second = second;
this.flip = !!flip;
return this;
}
CONVERSIONS = {
'==': '===',
'!=': '!==',
'of': 'in'
};
INVERSIONS = {
'!==': '===',
'===': '!=='
};
Op.prototype.children = ['first', 'second'];
Op.prototype.isSimpleNumber = NO;
Op.prototype.isUnary = function() {
return !this.second;
};
Op.prototype.isComplex = function() {
var _ref2;
return !(this.isUnary() && ((_ref2 = this.operator) === '+' || _ref2 === '-')) || this.first.isComplex();
};
Op.prototype.isChainable = function() {
var _ref2;
return (_ref2 = this.operator) === '<' || _ref2 === '>' || _ref2 === '>=' || _ref2 === '<=' || _ref2 === '===' || _ref2 === '!==';
};
Op.prototype.invert = function() {
var allInvertable, curr, fst, op, _ref2;
if (this.isChainable() && this.first.isChainable()) {
allInvertable = true;
curr = this;
while (curr && curr.operator) {
allInvertable && (allInvertable = curr.operator in INVERSIONS);
curr = curr.first;
}
if (!allInvertable) {
return new Parens(this).invert();
}
curr = this;
while (curr && curr.operator) {
curr.invert = !curr.invert;
curr.operator = INVERSIONS[curr.operator];
curr = curr.first;
}
return this;
} else if (op = INVERSIONS[this.operator]) {
this.operator = op;
if (this.first.unwrap() instanceof Op) {
this.first.invert();
}
return this;
} else if (this.second) {
return new Parens(this).invert();
} else if (this.operator === '!' && (fst = this.first.unwrap()) instanceof Op && ((_ref2 = fst.operator) === '!' || _ref2 === 'in' || _ref2 === 'instanceof')) {
return fst;
} else {
return new Op('!', this);
}
};
Op.prototype.unfoldSoak = function(o) {
var _ref2;
return ((_ref2 = this.operator) === '++' || _ref2 === '--' || _ref2 === 'delete') && unfoldSoak(o, this, 'first');
};
Op.prototype.generateDo = function(exp) {
var call, func, param, passedParams, ref, _i, _len, _ref2;
passedParams = [];
func = exp instanceof Assign && (ref = exp.value.unwrap()) instanceof Code ? ref : exp;
_ref2 = func.params || [];
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
param = _ref2[_i];
if (param.value) {
passedParams.push(param.value);
delete param.value;
} else {
passedParams.push(param);
}
}
call = new Call(exp, passedParams);
call["do"] = true;
return call;
};
Op.prototype.compileNode = function(o) {
var answer, isChain, lhs, rhs, _ref2, _ref3;
isChain = this.isChainable() && this.first.isChainable();
if (!isChain) {
this.first.front = this.front;
}
if (this.operator === 'delete' && o.scope.check(this.first.unwrapAll().value)) {
this.error('delete operand may not be argument or var');
}
if (((_ref2 = this.operator) === '--' || _ref2 === '++') && (_ref3 = this.first.unwrapAll().value, __indexOf.call(STRICT_PROSCRIBED, _ref3) >= 0)) {
this.error("cannot increment/decrement \"" + (this.first.unwrapAll().value) + "\"");
}
if (this.isUnary()) {
return this.compileUnary(o);
}
if (isChain) {
return this.compileChain(o);
}
switch (this.operator) {
case '?':
return this.compileExistence(o);
case '**':
return this.compilePower(o);
case '//':
return this.compileFloorDivision(o);
case '%%':
return this.compileModulo(o);
default:
lhs = this.first.compileToFragments(o, LEVEL_OP);
rhs = this.second.compileToFragments(o, LEVEL_OP);
answer = [].concat(lhs, this.makeCode(" " + this.operator + " "), rhs);
if (o.level <= LEVEL_OP) {
return answer;
} else {
return this.wrapInBraces(answer);
}
}
};
Op.prototype.compileChain = function(o) {
var fragments, fst, shared, _ref2;
_ref2 = this.first.second.cache(o), this.first.second = _ref2[0], shared = _ref2[1];
fst = this.first.compileToFragments(o, LEVEL_OP);
fragments = fst.concat(this.makeCode(" " + (this.invert ? '&&' : '||') + " "), shared.compileToFragments(o), this.makeCode(" " + this.operator + " "), this.second.compileToFragments(o, LEVEL_OP));
return this.wrapInBraces(fragments);
};
Op.prototype.compileExistence = function(o) {
var fst, ref;
if (this.first.isComplex()) {
ref = new Literal(o.scope.freeVariable('ref'));
fst = new Parens(new Assign(ref, this.first));
} else {
fst = this.first;
ref = fst;
}
return new If(new Existence(fst), ref, {
type: 'if'
}).addElse(this.second).compileToFragments(o);
};
Op.prototype.compileUnary = function(o) {
var op, parts, plusMinus;
parts = [];
op = this.operator;
parts.push([this.makeCode(op)]);
if (op === '!' && this.first instanceof Existence) {
this.first.negated = !this.first.negated;
return this.first.compileToFragments(o);
}
if (o.level >= LEVEL_ACCESS) {
return (new Parens(this)).compileToFragments(o);
}
plusMinus = op === '+' || op === '-';
if ((op === 'new' || op === 'typeof' || op === 'delete') || plusMinus && this.first instanceof Op && this.first.operator === op) {
parts.push([this.makeCode(' ')]);
}
if ((plusMinus && this.first instanceof Op) || (op === 'new' && this.first.isStatement(o))) {
this.first = new Parens(this.first);
}
parts.push(this.first.compileToFragments(o, LEVEL_OP));
if (this.flip) {
parts.reverse();
}
return this.joinFragmentArrays(parts, '');
};
Op.prototype.compilePower = function(o) {
var pow;
pow = new Value(new Literal('Math'), [new Access(new Literal('pow'))]);
return new Call(pow, [this.first, this.second]).compileToFragments(o);
};
Op.prototype.compileFloorDivision = function(o) {
var div, floor;
floor = new Value(new Literal('Math'), [new Access(new Literal('floor'))]);
div = new Op('/', this.first, this.second);
return new Call(floor, [div]).compileToFragments(o);
};
Op.prototype.compileModulo = function(o) {
var mod;
mod = new Value(new Literal(utility('modulo')));
return new Call(mod, [this.first, this.second]).compileToFragments(o);
};
Op.prototype.toString = function(idt) {
return Op.__super__.toString.call(this, idt, this.constructor.name + ' ' + this.operator);
};
Op.prototype.icedWrapContinuation = function() {
return this.icedCallContinuationFlag;
};
return Op;
})(Base);
exports.In = In = (function(_super) {
__extends(In, _super);
function In(object, array) {
this.object = object;
this.array = array;
In.__super__.constructor.call(this);
}
In.prototype.children = ['object', 'array'];
In.prototype.invert = NEGATE;
In.prototype.compileNode = function(o) {
var hasSplat, obj, _i, _len, _ref2;
if (this.array instanceof Value && this.array.isArray() && this.array.base.objects.length) {
_ref2 = this.array.base.objects;
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
obj = _ref2[_i];
if (!(obj instanceof Splat)) {
continue;
}
hasSplat = true;
break;
}
if (!hasSplat) {
return this.compileOrTest(o);
}
}
return this.compileLoopTest(o);
};
In.prototype.compileOrTest = function(o) {
var cmp, cnj, i, item, ref, sub, tests, _i, _len, _ref2, _ref3, _ref4;
_ref2 = this.object.cache(o, LEVEL_OP), sub = _ref2[0], ref = _ref2[1];
_ref3 = this.negated ? [' !== ', ' && '] : [' === ', ' || '], cmp = _ref3[0], cnj = _ref3[1];
tests = [];
_ref4 = this.array.base.objects;
for (i = _i = 0, _len = _ref4.length; _i < _len; i = ++_i) {
item = _ref4[i];
if (i) {
tests.push(this.makeCode(cnj));
}
tests = tests.concat((i ? ref : sub), this.makeCode(cmp), item.compileToFragments(o, LEVEL_ACCESS));
}
if (o.level < LEVEL_OP) {
return tests;
} else {
return this.wrapInBraces(tests);
}
};
In.prototype.compileLoopTest = function(o) {
var fragments, ref, sub, _ref2;
_ref2 = this.object.cache(o, LEVEL_LIST), sub = _ref2[0], ref = _ref2[1];
fragments = [].concat(this.makeCode(utility('indexOf') + ".call("), this.array.compileToFragments(o, LEVEL_LIST), this.makeCode(", "), ref, this.makeCode(") " + (this.negated ? '< 0' : '>= 0')));
if (fragmentsToText(sub) === fragmentsToText(ref)) {
return fragments;
}
fragments = sub.concat(this.makeCode(', '), fragments);
if (o.level < LEVEL_LIST) {
return fragments;
} else {
return this.wrapInBraces(fragments);
}
};
In.prototype.toString = function(idt) {
return In.__super__.toString.call(this, idt, this.constructor.name + (this.negated ? '!' : ''));
};
return In;
})(Base);
exports.Slot = Slot = (function(_super) {
__extends(Slot, _super);
function Slot(index, value, suffix, splat) {
Slot.__super__.constructor.call(this);
this.index = index;
this.value = value;
this.suffix = suffix;
this.splat = splat;
this.access = null;
}
Slot.prototype.addAccess = function(a) {
this.access = a;
return this;
};
Slot.prototype.children = ['value', 'suffix'];
return Slot;
})(Base);
exports.Defer = Defer = (function(_super) {
__extends(Defer, _super);
function Defer(args, lineno) {
var a, i;
this.lineno = lineno;
Defer.__super__.constructor.call(this);
this.slots = flatten((function() {
var _i, _len, _results;
_results = [];
for (i = _i = 0, _len = args.length; _i < _len; i = ++_i) {
a = args[i];
_results.push(a.icedToSlot(i));
}
return _results;
})());
this.params = [];
this.vars = [];
this.custom = false;
}
Defer.prototype.children = ['slots'];
Defer.prototype.setCustom = function() {
this.custom = true;
return this;
};
Defer.prototype.newParam = function() {
var l;
l = "" + iced["const"].slot + "_" + (this.params.length + 1);
this.params.push(new Param(new Literal(l)));
return new Value(new Literal(l));
};
Defer.prototype.makeAssignFn = function(o) {
var a, args, assign, assignments, block, call, func, i, i_lit, inner_fn, lit, outer_block, outer_fn, prop, s, slot, _i, _len, _ref2;
if (this.slots.length === 0) {
return null;
}
assignments = [];
args = [];
i = 0;
_ref2 = this.slots;
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
s = _ref2[_i];
i = s.index;
a = new Value(new Literal("arguments"));
i_lit = new Value(new Literal(i));
if (s.splat) {
func = new Value(new Literal(utility('slice')));
func.add(new Access(new Value(new Literal('call'))));
call = new Call(func, [a, i_lit]);
slot = s.value;
this.vars.push(slot);
assign = new Assign(slot, call);
} else {
a.add(new Index(i_lit));
if (s.access) {
a.add(s.access);
}
if (!s.suffix) {
lit = s.value.compile(o, LEVEL_TOP);
if (lit === "_") {
slot = new Value(new Literal(iced["const"].deferrals));
slot.add(new Access(new Value(new Literal(iced["const"].retslot))));
} else {
slot = s.value;
this.vars.push(slot);
}
} else {
args.push(s.value);
slot = this.newParam();
if (s.suffix instanceof Index) {
prop = new Index(this.newParam());
args.push(s.suffix.index);
} else {
prop = s.suffix;
}
slot.add(prop);
}
assign = new Assign(slot, a);
}
assignments.push(assign);
}
block = new Block(assignments);
inner_fn = new Code([], block, 'icedgen');
outer_block = new Block([new Return(inner_fn)]);
outer_fn = new Code(this.params, outer_block, 'icedgen');
return call = new Call(outer_fn, args);
};
Defer.prototype.transform = function(o) {
var assign_fn, assignments, context_assign, context_lhs, context_rhs, fn, ln_assign, ln_lhs, ln_rhs, meth;
meth = new Value(new Literal(iced["const"].defer_method));
if (this.custom) {
fn = meth;
} else {
fn = new Value(new Literal(iced["const"].deferrals));
fn.add(new Access(meth));
}
assignments = [];
if ((assign_fn = this.makeAssignFn(o))) {
assignments.push(new Assign(new Value(new Literal(iced["const"].assign_fn)), assign_fn, "object"));
}
ln_lhs = new Value(new Literal(iced["const"].lineno));
ln_rhs = new Value(new Literal(this.lineno));
ln_assign = new Assign(ln_lhs, ln_rhs, "object");
assignments.push(ln_assign);
if (this.custom) {
context_lhs = new Value(new Literal(iced["const"].context));
context_rhs = new Value(new Literal(iced["const"].deferrals));
context_assign = new Assign(context_lhs, context_rhs, "object");
assignments.push(context_assign);
}
o = new Obj(assignments);
return new Call(fn, [new Value(o)]);
};
Defer.prototype.compileNode = function(o) {
var call, name, scope, v, _i, _len, _ref2;
call = this.transform(o);
_ref2 = this.vars;
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
v = _ref2[_i];
name = v.compile(o, LEVEL_LIST);
scope = o.scope;
scope.add(name, 'var');
}
return call.compileNode(o);
};
Defer.prototype.icedWalkAst = function(p, o) {
this.icedHasAutocbFlag = o.foundAutocb;
o.foundDefer = true;
this.parentFunc = o.currFunc;
return Defer.__super__.icedWalkAst.call(this, p, o);
};
return Defer;
})(Base);
exports.Await = Await = (function(_super) {
__extends(Await, _super);
function Await(body) {
this.body = body;
Await.__super__.constructor.call(this);
}
Await.prototype.transform = function(o) {
var assign, assignments, body, call, cb_assignment, cb_lhs, cb_rhs, cls, fn_assignment, fn_lhs, fn_rhs, func_assignment, func_lhs, func_rhs, lhs, meth, n, name, rhs, trace, _ref2, _ref3;
body = this.body;
name = iced["const"].deferrals;
o.scope.add(name, 'var');
lhs = new Value(new Literal(name));
cls = new Value(new Literal(iced["const"].ns));
cls.add(new Access(new Value(new Literal(iced["const"].Deferrals))));
assignments = [];
if (n = (_ref2 = this.parentFunc) != null ? _ref2.icedPassedDeferral : void 0) {
cb_lhs = new Value(new Literal(iced["const"].parent));
cb_rhs = new Value(new Literal(n));
cb_assignment = new Assign(cb_lhs, cb_rhs, "object");
assignments.push(cb_assignment);
}
if (o.filename != null) {
fn_lhs = new Value(new Literal(iced["const"].filename));
fn_rhs = new Value(new Literal('"' + o.filename.replace(/\\/g, '\\\\') + '"'));
fn_assignment = new Assign(fn_lhs, fn_rhs, "object");
assignments.push(fn_assignment);
}
if (n = (_ref3 = this.parentFunc) != null ? _ref3.icedTraceName() : void 0) {
func_lhs = new Value(new Literal(iced["const"].funcname));
func_rhs = new Value(new Literal('"' + n + '"'));
func_assignment = new Assign(func_lhs, func_rhs, "object");
assignments.push(func_assignment);
}
trace = new Obj(assignments, true);
call = new Call(cls, [new Value(new Literal(iced["const"].k)), trace]);
rhs = new Op("new", call);
assign = new Assign(lhs, rhs);
body.unshift(assign);
meth = lhs.copy().add(new Access(new Value(new Literal(iced["const"].fulfill))));
call = new Call(meth, []);
body.push(call);
return this.body = body;
};
Await.prototype.children = ['body'];
Await.prototype.isStatement = function() {
return YES;
};
Await.prototype.makeReturn = THIS;
Await.prototype.compileNode = function(o) {
this.transform(o);
return this.body.compileNode(o);
};
Await.prototype.icedWalkAst = function(p, o) {
this.icedHasAutocbFlag = o.foundAutocb;
this.parentFunc = o.currFunc;
p = p || this;
this.icedParentAwait = p;
Await.__super__.icedWalkAst.call(this, p, o);
return this.icedNodeFlag = o.foundAwaitFunc = o.foundAwait = true;
};
return Await;
})(Base);
IcedRuntime = (function(_super) {
__extends(IcedRuntime, _super);
function IcedRuntime(foundDefer, foundAwait) {
this.foundDefer = foundDefer;
this.foundAwait = foundAwait;
IcedRuntime.__super__.constructor.call(this);
}
IcedRuntime.prototype.compileNode = function(o, level) {
var access, accessname, assign, call, callv, file, inc, interp, k, klass, lhs_vec, modname, ns, req, rhs, v, val, window_mode, window_val, _i, _j, _len, _len1, _ref2;
this.expressions = [];
v = o.runtime ? o.runtime : o.bare ? "none" : this.foundDefer ? "node" : "none";
if (o.runtime && !this.foundDefer && !o.runforce) {
v = "none";
}
window_mode = false;
window_val = null;
inc = null;
inc = (function() {
switch (v) {
case "inline":
case "window":
if (v === "window") {
window_mode = true;
}
if (window_mode) {
window_val = new Value(new Literal(v));
}
return InlineRuntime.generate(window_val ? window_val.copy() : null);
case "node":
case "browserify":
case "interp":
interp = v === "interp";
modname = interp ? "iced-coffee-script" : "iced-runtime";
accessname = iced["const"].ns;
file = new Literal("'" + modname + "'");
access = new Access(new Literal(accessname));
req = new Value(new Literal("require"));
call = new Call(req, [file]);
callv = new Value(call);
if (interp) {
callv.add(access);
}
ns = new Value(new Literal(iced["const"].ns));
return new Assign(ns, callv);
case "none":
return null;
default:
throw SyntaxError("unexpected flag IcedRuntime " + v);
}
})();
if (inc) {
this.push(inc);
}
if (this.foundAwait) {
rhs = new Code([], new Block([]));
lhs_vec = [];
_ref2 = [iced["const"].k_noop, iced["const"].k];
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
k = _ref2[_i];
val = new Value(new Literal(k));
if (window_val) {
klass = window_val.copy();
klass.add(new Access(val));
val = klass;
}
lhs_vec.push(val);
}
assign = rhs;
for (_j = 0, _len1 = lhs_vec.length; _j < _len1; _j++) {
v = lhs_vec[_j];
assign = new Assign(v, assign);
}
this.push(assign);
}
if (this.isEmpty()) {
return [];
} else {
return IcedRuntime.__super__.compileNode.call(this, o);
}
};
IcedRuntime.prototype.icedWalkAst = function(p, o) {
this.icedHasAutocbFlag = o.foundAutocb;
return IcedRuntime.__super__.icedWalkAst.call(this, p, o);
};
return IcedRuntime;
})(Block);
exports.Try = Try = (function(_super) {
__extends(Try, _super);
function Try(attempt, errorVariable, recovery, ensure) {
this.attempt = attempt;
this.errorVariable = errorVariable;
this.recovery = recovery;
this.ensure = ensure;
}
Try.prototype.children = ['attempt', 'recovery', 'ensure'];
Try.prototype.isStatement = YES;
Try.prototype.jumps = function(o) {
var _ref2;
return this.attempt.jumps(o) || ((_ref2 = this.recovery) != null ? _ref2.jumps(o) : void 0);
};
Try.prototype.makeReturn = function(res) {
if (this.attempt) {
this.attempt = this.attempt.makeReturn(res);
}
if (this.recovery) {
this.recovery = this.recovery.makeReturn(res);
}
return this;
};
Try.prototype.compileNode = function(o) {
var catchPart, ensurePart, placeholder, tryPart;
o.indent += TAB;
tryPart = this.attempt.compileToFragments(o, LEVEL_TOP);
catchPart = this.recovery ? (placeholder = new Literal('_error'), this.errorVariable ? 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) ? [this.makeCode(' catch (_error) {}')] : [];
ensurePart = this.ensure ? [].concat(this.makeCode(" finally {\n"), this.ensure.compileToFragments(o, LEVEL_TOP), this.makeCode("\n" + this.tab + "}")) : [];
return [].concat(this.makeCode("" + this.tab + "try {\n"), tryPart, this.makeCode("\n" + this.tab + "}"), catchPart, ensurePart);
};
return Try;
})(Base);
exports.Throw = Throw = (function(_super) {
__extends(Throw, _super);
function Throw(expression) {
this.expression = expression;
Throw.__super__.constructor.call(this);
}
Throw.prototype.children = ['expression'];
Throw.prototype.isStatement = YES;
Throw.prototype.jumps = NO;
Throw.prototype.makeReturn = THIS;
Throw.prototype.compileNode = function(o) {
return [].concat(this.makeCode(this.tab + "throw "), this.expression.compileToFragments(o), this.makeCode(";"));
};
return Throw;
})(Base);
exports.Existence = Existence = (function(_super) {
__extends(Existence, _super);
function Existence(expression) {
this.expression = expression;
Existence.__super__.constructor.call(this);
}
Existence.prototype.children = ['expression'];
Existence.prototype.invert = NEGATE;
Existence.prototype.compileNode = function(o) {
var cmp, cnj, code, _ref2;
this.expression.front = this.front;
code = this.expression.compile(o, LEVEL_OP);
if (IDENTIFIER.test(code) && !o.scope.check(code)) {
_ref2 = this.negated ? ['===', '||'] : ['!==', '&&'], cmp = _ref2[0], cnj = _ref2[1];
code = "typeof " + code + " " + cmp + " \"undefined\" " + cnj + " " + code + " " + cmp + " null";
} else {
code = "" + code + " " + (this.negated ? '==' : '!=') + " null";
}
return [this.makeCode(o.level <= LEVEL_COND ? code : "(" + code + ")")];
};
return Existence;
})(Base);
exports.Parens = Parens = (function(_super) {
__extends(Parens, _super);
function Parens(body) {
this.body = body;
Parens.__super__.constructor.call(this);
}
Parens.prototype.children = ['body'];
Parens.prototype.unwrap = function() {
return this.body;
};
Parens.prototype.isComplex = function() {
return this.body.isComplex();
};
Parens.prototype.compileNode = function(o) {
var bare, expr, fragments;
expr = this.body.unwrap();
if (expr instanceof Value && expr.isAtomic()) {
expr.front = this.front;
return expr.compileToFragments(o);
}
fragments = expr.compileToFragments(o, LEVEL_PAREN);
bare = o.level < LEVEL_OP && (expr instanceof Op || expr instanceof Call || (expr instanceof For && expr.returns));
if (bare) {
return fragments;
} else {
return this.wrapInBraces(fragments);
}
};
return Parens;
})(Base);
exports.For = For = (function(_super) {
__extends(For, _super);
function For(body, source) {
var _ref2;
For.__super__.constructor.call(this);
this.source = source.source, this.guard = source.guard, this.step = source.step, this.name = source.name, this.index = source.index;
this.body = Block.wrap([body]);
this.own = !!source.own;
this.object = !!source.object;
if (this.object) {
_ref2 = [this.index, this.name], this.name = _ref2[0], this.index = _ref2[1];
}
if (this.index instanceof Value) {
this.index.error('index cannot be a pattern matching expression');
}
this.range = this.source instanceof Value && this.source.base instanceof Range && !this.source.properties.length;
this.pattern = this.name instanceof Value;
if (this.range && this.index) {
this.index.error('indexes do not apply to range loops');
}
if (this.range && this.pattern) {
this.name.error('cannot pattern match over range loops');
}
if (this.own && !this.object) {
this.name.error('cannot use own with for-in');
}
this.returns = false;
}
For.prototype.children = ['body', 'source', 'guard', 'step'];
For.prototype.compileNode = function(o) {
var body, bodyFragments, compare, compareDown, declare, declareDown, defPart, defPartFragments, down, forPartFragments, guardPart, idt1, increment, index, ivar, kvar, kvarAssign, lastJumps, lvar, name, namePart, ref, resultPart, returnResult, rvar, scope, source, step, stepNum, stepVar, svar, varPart, _ref2, _ref3;
body = Block.wrap([this.body]);
lastJumps = (_ref2 = last(body.expressions)) != null ? _ref2.jumps() : void 0;
if (lastJumps && lastJumps instanceof Return) {
this.returns = false;
}
source = this.range ? this.source.base : this.source;
scope = o.scope;
if (!this.pattern) {
name = this.name && (this.name.compile(o, LEVEL_LIST));
}
index = this.index && (this.index.compile(o, LEVEL_LIST));
if (name && !this.pattern) {
scope.find(name);
}
if (index) {
scope.find(index);
}
if (this.returns) {
rvar = scope.freeVariable('results');
}
ivar = (this.object && index) || scope.freeVariable('i');
kvar = (this.range && name) || index || ivar;
kvarAssign = kvar !== ivar ? "" + kvar + " = " : "";
if (this.step && !this.range) {
_ref3 = this.cacheToCodeFragments(this.step.cache(o, LEVEL_LIST)), step = _ref3[0], stepVar = _ref3[1];
stepNum = stepVar.match(NUMBER);
}
if (this.pattern) {
name = ivar;
}
varPart = '';
guardPart = '';
defPart = '';
idt1 = this.tab + TAB;
source.icedStatementAssertion();
if (this.icedNodeFlag) {
return this.icedCompileIced(o, {
stepVar: stepVar,
body: body,
rvar: rvar,
kvar: kvar,
guard: this.guard
});
}
if (this.range) {
forPartFragments = source.compileToFragments(merge(o, {
index: ivar,
name: name,
step: this.step
}));
} else {
svar = this.source.compile(o, LEVEL_LIST);
if ((name || this.own) && !IDENTIFIER.test(svar)) {
defPart += "" + this.tab + (ref = scope.freeVariable('ref')) + " = " + svar + ";\n";
svar = ref;
}
if (name && !this.pattern) {
namePart = "" + name + " = " + svar + "[" + kvar + "]";
}
if (!this.object) {
if (step !== stepVar) {
defPart += "" + this.tab + step + ";\n";
}
if (!(this.step && stepNum && (down = parseNum(stepNum[0]) < 0))) {
lvar = scope.freeVariable('len');
}
declare = "" + kvarAssign + ivar + " = 0, " + lvar + " = " + svar + ".length";
declareDown = "" + kvarAssign + ivar + " = " + svar + ".length - 1";
compare = "" + ivar + " < " + lvar;
compareDown = "" + ivar + " >= 0";
if (this.step) {
if (stepNum) {
if (down) {
compare = compareDown;
declare = declareDown;
}
} else {
compare = "" + stepVar + " > 0 ? " + compare + " : " + compareDown;
declare = "(" + stepVar + " > 0 ? (" + declare + ") : " + declareDown + ")";
}
increment = "" + ivar + " += " + stepVar;
} else {
increment = "" + (kvar !== ivar ? "++" + ivar : "" + ivar + "++");
}
forPartFragments = [this.makeCode("" + declare + "; " + compare + "; " + kvarAssign + increment)];
}
}
if (this.returns) {
resultPart = "" + this.tab + rvar + " = [];\n";
returnResult = this.icedHasAutocbFlag ? "\n" + this.tab + iced["const"].autocb + "(" + rvar + "); return;" : "\n" + this.tab + "return " + rvar + ";";
body.makeReturn(rvar);
}
if (this.guard) {
if (body.expressions.length > 1) {
body.expressions.unshift(new If((new Parens(this.guard)).invert(), new Literal("continue")));
} else {
if (this.guard) {
body = Block.wrap([new If(this.guard, body)]);
}
}
}
if (this.pattern) {
body.expressions.unshift(new Assign(this.name, new Literal("" + svar + "[" + kvar + "]")));
}
defPartFragments = [].concat(this.makeCode(defPart), this.pluckDirectCall(o, body));
if (namePart) {
varPart = "\n" + idt1 + namePart + ";";
}
if (this.object) {
forPartFragments = [this.makeCode("" + kvar + " in " + svar)];
if (this.own) {
guardPart = "\n" + idt1 + "if (!" + (utility('hasProp')) + ".call(" + svar + ", " + kvar + ")) continue;";
}
}
bodyFragments = body.compileToFragments(merge(o, {
indent: idt1
}), LEVEL_TOP);
if (bodyFragments && (bodyFragments.length > 0)) {
bodyFragments = [].concat(this.makeCode("\n"), bodyFragments, this.makeCode("\n"));
}
return [].concat(defPartFragments, this.makeCode("" + (resultPart || '') + this.tab + "for ("), forPartFragments, this.makeCode(") {" + guardPart + varPart), bodyFragments, this.makeCode("" + this.tab + "}" + (returnResult || '')));
};
For.prototype.pluckDirectCall = function(o, body) {
var base, defs, expr, fn, idx, ref, val, _i, _len, _ref2, _ref3, _ref4, _ref5, _ref6, _ref7, _ref8;
defs = [];
_ref2 = body.expressions;
for (idx = _i = 0, _len = _ref2.length; _i < _len; idx = ++_i) {
expr = _ref2[idx];
expr = expr.unwrapAll();
if (!(expr instanceof Call)) {
continue;
}
val = (_ref3 = expr.variable) != null ? _ref3.unwrapAll() : void 0;
if (!((val instanceof Code) || (val instanceof Value && ((_ref4 = val.base) != null ? _ref4.unwrapAll() : void 0) instanceof Code && val.properties.length === 1 && ((_ref5 = (_ref6 = val.properties[0].name) != null ? _ref6.value : void 0) === 'call' || _ref5 === 'apply')))) {
continue;
}
fn = ((_ref7 = val.base) != null ? _ref7.unwrapAll() : void 0) || val;
ref = new Literal(o.scope.freeVariable('fn'));
base = new Value(ref);
if (val.base) {
_ref8 = [base, val], val.base = _ref8[0], base = _ref8[1];
}
body.expressions[idx] = new Call(base, expr.args);
defs = defs.concat(this.makeCode(this.tab), new Assign(ref, fn).compileToFragments(o, LEVEL_TOP), this.makeCode(';\n'));
}
return defs;
};
For.prototype.icedCompileIced = function(o, d) {
var a1, a2, a3, a4, a5, b, begin, body, condition, empty_arr, end, excl, guard, iname, init, ival, key, key_lit, key_val, keys, keys_access, keys_len, keys_val, kval, len, len_rhs, len_val, loop_body, loop_keys, loop_source, neg, pos, positive, pre_body, ref, ref_val, ref_val_copy, rvar, scope, source_access, step, stepVal;
body = d.body;
condition = null;
init = [];
step = null;
scope = o.scope;
pre_body = new Block([]);
if (this.object) {
ref = scope.freeVariable('ref');
ref_val = new Value(new Literal(ref));
a1 = new Assign(ref_val, this.source);
keys = scope.freeVariable('keys');
keys_val = new Value(new Literal(keys));
key = scope.freeVariable('k');
key_lit = new Literal(key);
key_val = new Value(key_lit);
empty_arr = new Value(new Arr);
loop_body = new Block([key_val]);
loop_source = {
object: true,
name: key_lit,
source: ref_val
};
loop_keys = new For(loop_body, loop_source);
a2 = new Assign(keys_val, loop_keys);
iname = scope.freeVariable('i');
ival = new Value(new Literal(iname));
a3 = new Assign(ival, new Value(new Literal(0)));
init = [a1, a2, a3];
keys_len = keys_val.copy();
keys_len.add(new Access(new Value(new Literal("length"))));
condition = new Op('<', ival, keys_len);
step = new Op('++', ival);
if (this.name) {
source_access = ref_val.copy();
source_access.add(new Index(this.index));
a5 = new Assign(this.name, source_access);
pre_body.unshift(a5);
}
keys_access = keys_val.copy();
keys_access.add(new Index(ival));
a4 = new Assign(this.index, keys_access);
pre_body.unshift(a4);
} else if (this.range) {
if (!this.name) {
this.name = new Literal(d.kvar);
}
begin = new Value(new Literal("_begin"));
end = new Value(new Literal("_end"));
positive = new Value(new Literal("_positive"));
stepVal = this.step || new Literal(1);
step = new If(positive, new Op("+=", this.name, stepVal));
step.addElse(new Op("-=", this.name, stepVal));
excl = this.source.base.exclusive ? "=" : '';
pos = new Op("&&", new Op("===", positive, new Literal(true)), new Op(">" + excl, this.name, this.source.base.to));
neg = new Op("&&", new Op("===", positive, new Literal(false)), new Op("<" + excl, this.name, this.source.base.to));
condition = new Op("||", new Parens(pos), new Parens(neg));
condition = condition.invert();
init = [new Assign(this.name, this.source.base.from), new Assign(begin, this.source.base.from), new Assign(end, this.source.base.to), new Assign(positive, new Op(">", end, begin))];
} else if (!this.range && this.name) {
kval = new Value(new Literal(d.kvar));
len = scope.freeVariable('len');
ref = scope.freeVariable('ref');
ref_val = new Value(new Literal(ref));
len_val = new Value(new Literal(len));
a1 = new Assign(ref_val, this.source);
len_rhs = ref_val.copy().add(new Access(new Value(new Literal("length"))));
a2 = new Assign(len_val, len_rhs);
a3 = new Assign(kval, new Value(new Literal(0)));
init = [a1, a2, a3];
condition = new Op('<', kval, len_val);
step = new Op('++', kval);
ref_val_copy = ref_val.copy();
ref_val_copy.add(new Index(kval));
a4 = new Assign(this.name, ref_val_copy);
pre_body.unshift(a4);
}
rvar = d.rvar;
guard = d.guard;
b = this.icedWrap({
condition: condition,
body: body,
init: init,
step: step,
rvar: rvar,
guard: guard,
pre_body: pre_body
});
return b.compileNode(o);
};
return For;
})(While);
exports.Switch = Switch = (function(_super) {
__extends(Switch, _super);
function Switch(subject, cases, otherwise) {
this.subject = subject;
this.cases = cases;
this.otherwise = otherwise;
Switch.__super__.constructor.call(this);
}
Switch.prototype.children = ['subject', 'cases', 'otherwise'];
Switch.prototype.isStatement = YES;
Switch.prototype.jumps = function(o) {
var block, conds, jumpNode, _i, _len, _ref2, _ref3, _ref4;
if (o == null) {
o = {
block: true
};
}
_ref2 = this.cases;
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
_ref3 = _ref2[_i], conds = _ref3[0], block = _ref3[1];
if (jumpNode = block.jumps(o)) {
return jumpNode;
}
}
return (_ref4 = this.otherwise) != null ? _ref4.jumps(o) : void 0;
};
Switch.prototype.makeReturn = function(res) {
var pair, _i, _len, _ref2, _ref3;
_ref2 = this.cases;
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
pair = _ref2[_i];
pair[1].makeReturn(res);
}
if (res) {
this.otherwise || (this.otherwise = new Block([new Literal('void 0')]));
}
if ((_ref3 = this.otherwise) != null) {
_ref3.makeReturn(res);
}
return this;
};
Switch.prototype.compileNode = function(o) {
var block, body, cond, conditions, expr, fragments, i, idt1, idt2, _i, _j, _len, _len1, _ref2, _ref3, _ref4;
if (this.subject) {
this.subject.icedStatementAssertion();
}
idt1 = o.indent + TAB;
idt2 = o.indent = idt1 + TAB;
fragments = [].concat(this.makeCode(this.tab + "switch ("), (this.subject ? this.subject.compileToFragments(o, LEVEL_PAREN) : this.makeCode("false")), this.makeCode(") {\n"));
_ref2 = this.cases;
for (i = _i = 0, _len = _ref2.length; _i < _len; i = ++_i) {
_ref3 = _ref2[i], conditions = _ref3[0], block = _ref3[1];
_ref4 = flatten([conditions]);
for (_j = 0, _len1 = _ref4.length; _j < _len1; _j++) {
cond = _ref4[_j];
if (!this.subject) {
cond = cond.invert();
}
fragments = fragments.concat(this.makeCode(idt1 + "case "), cond.compileToFragments(o, LEVEL_PAREN), this.makeCode(":\n"));
}
if ((body = block.compileToFragments(o, LEVEL_TOP)).length > 0) {
fragments = fragments.concat(body, this.makeCode('\n'));
}
if (i === this.cases.length - 1 && !this.otherwise) {
break;
}
expr = this.lastNonComment(block.expressions);
if (expr instanceof Return || (expr instanceof Literal && expr.jumps() && expr.value !== 'debugger')) {
continue;
}
fragments.push(cond.makeCode(idt2 + 'break;\n'));
}
if (this.otherwise && this.otherwise.expressions.length) {
fragments.push.apply(fragments, [this.makeCode(idt1 + "default:\n")].concat(__slice.call(this.otherwise.compileToFragments(o, LEVEL_TOP)), [this.makeCode("\n")]));
}
fragments.push(this.makeCode(this.tab + '}'));
return fragments;
};
Switch.prototype.icedCallContinuation = function() {
var block, condition, _i, _len, _ref2, _ref3;
_ref2 = this.cases;
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
_ref3 = _ref2[_i], condition = _ref3[0], block = _ref3[1];
block.icedThreadReturn();
}
if (this.otherwise != null) {
return this.otherwise.icedThreadReturn();
} else {
return this.otherwise = new Block([new IcedTailCall]);
}
};
return Switch;
})(Base);
exports.If = If = (function(_super) {
__extends(If, _super);
function If(condition, body, options) {
this.body = body;
if (options == null) {
options = {};
}
If.__super__.constructor.call(this);
this.condition = options.type === 'unless' ? condition.invert() : condition;
this.elseBody = null;
this.isChain = false;
this.soak = options.soak;
}
If.prototype.children = ['condition', 'body', 'elseBody'];
If.prototype.bodyNode = function() {
var _ref2;
return (_ref2 = this.body) != null ? _ref2.unwrap() : void 0;
};
If.prototype.elseBodyNode = function() {
var _ref2;
return (_ref2 = this.elseBody) != null ? _ref2.unwrap() : void 0;
};
If.prototype.addElse = function(elseBody) {
if (this.isChain) {
this.elseBodyNode().addElse(elseBody);
} else {
this.isChain = elseBody instanceof If;
this.elseBody = this.ensureBlock(elseBody);
this.elseBody.updateLocationDataIfMissing(elseBody.locationData);
}
return this;
};
If.prototype.isStatement = function(o) {
var _ref2;
return (o != null ? o.level : void 0) === LEVEL_TOP || this.bodyNode().isStatement(o) || ((_ref2 = this.elseBodyNode()) != null ? _ref2.isStatement(o) : void 0);
};
If.prototype.jumps = function(o) {
var _ref2;
return this.body.jumps(o) || ((_ref2 = this.elseBody) != null ? _ref2.jumps(o) : void 0);
};
If.prototype.compileNode = function(o) {
this.condition.icedStatementAssertion();
if (this.isStatement(o || this.icedIsCpsPivot())) {
return this.compileStatement(o);
} else {
return this.compileExpression(o);
}
};
If.prototype.makeReturn = function(res) {
if (res) {
this.elseBody || (this.elseBody = new Block([new Literal('void 0')]));
}
this.body && (this.body = new Block([this.body.makeReturn(res)]));
this.elseBody && (this.elseBody = new Block([this.elseBody.makeReturn(res)]));
return this;
};
If.prototype.ensureBlock = function(node) {
if (node instanceof Block) {
return node;
} else {
return new Block([node]);
}
};
If.prototype.compileStatement = function(o) {
var answer, body, child, cond, exeq, ifPart, indent;
child = del(o, 'chainChild');
exeq = del(o, 'isExistentialEquals');
if (exeq) {
return new If(this.condition.invert(), this.elseBodyNode(), {
type: 'if'
}).compileToFragments(o);
}
indent = o.indent + TAB;
cond = this.condition.compileToFragments(o, LEVEL_PAREN);
body = this.ensureBlock(this.body).compileToFragments(merge(o, {
indent: indent
}));
ifPart = [].concat(this.makeCode("if ("), cond, this.makeCode(") {\n"), body, this.makeCode("\n" + this.tab + "}"));
if (!child) {
ifPart.unshift(this.makeCode(this.tab));
}
if (!this.elseBody) {
return ifPart;
}
answer = ifPart.concat(this.makeCode(' else '));
if (this.isChain) {
o.chainChild = true;
answer = answer.concat(this.elseBody.unwrap().compileToFragments(o, LEVEL_TOP));
} else {
answer = answer.concat(this.makeCode("{\n"), this.elseBody.compileToFragments(merge(o, {
indent: indent
}), LEVEL_TOP), this.makeCode("\n" + this.tab + "}"));
}
return answer;
};
If.prototype.compileExpression = function(o) {
var alt, body, cond, fragments;
cond = this.condition.compileToFragments(o, LEVEL_COND);
body = this.bodyNode().compileToFragments(o, LEVEL_LIST);
alt = this.elseBodyNode() ? this.elseBodyNode().compileToFragments(o, LEVEL_LIST) : [this.makeCode('void 0')];
fragments = cond.concat(this.makeCode(" ? "), body, this.makeCode(" : "), alt);
if (o.level >= LEVEL_COND) {
return this.wrapInBraces(fragments);
} else {
return fragments;
}
};
If.prototype.unfoldSoak = function() {
return this.soak && this;
};
If.prototype.icedCallContinuation = function() {
if (this.elseBody) {
this.elseBody.icedThreadReturn();
this.isChain = false;
} else {
this.addElse(new IcedTailCall);
}
return this.body.icedThreadReturn();
};
return If;
})(Base);
Closure = {
wrap: function(expressions, statement, noReturn) {
var args, argumentsNode, call, func, meth;
if (expressions.jumps()) {
return expressions;
}
func = new Code([], Block.wrap([expressions]));
args = [];
argumentsNode = expressions.contains(this.isLiteralArguments);
if (argumentsNode && expressions.classBody) {
argumentsNode.error("Class bodies shouldn't reference arguments");
}
if (argumentsNode || expressions.contains(this.isLiteralThis)) {
meth = new Literal(argumentsNode ? 'apply' : 'call');
args = [new Literal('this')];
if (argumentsNode) {
args.push(new Literal('arguments'));
}
func = new Value(func, [new Access(meth)]);
}
func.noReturn = noReturn;
call = new Call(func, args);
if (statement) {
return Block.wrap([call]);
} else {
return call;
}
},
isLiteralArguments: function(node) {
return node instanceof Literal && node.value === 'arguments' && !node.asKey;
},
isLiteralThis: function(node) {
return (node instanceof Literal && node.value === 'this' && !node.asKey) || (node instanceof Code && node.bound) || (node instanceof Call && node.isSuper);
}
};
unfoldSoak = function(o, parent, name) {
var ifn;
if (!(ifn = parent[name].unfoldSoak(o))) {
return;
}
parent[name] = ifn.body;
ifn.body = new Value(parent);
return ifn;
};
CpsCascade = {
wrap: function(statement, rest, returnValue, o) {
var args, block, call, cont, e, func;
func = new Code([new Param(new Literal(iced["const"].k))], Block.wrap([statement]), 'icedgen');
args = [];
if (returnValue) {
returnValue.bindName(o);
args.push(returnValue);
}
block = Block.wrap([rest]);
if ((e = block.icedGetSingle()) && e instanceof IcedTailCall && e.canInline()) {
cont = e.extractFunc();
} else {
cont = new Code(args, block, 'icedgen');
}
call = new Call(func, [cont]);
return new Block([call]);
}
};
IcedTailCall = (function(_super) {
__extends(IcedTailCall, _super);
function IcedTailCall(func, val) {
this.func = func;
if (val == null) {
val = null;
}
IcedTailCall.__super__.constructor.call(this);
if (!this.func) {
this.func = iced["const"].k;
}
this.value = val;
}
IcedTailCall.prototype.children = ['value'];
IcedTailCall.prototype.assignValue = function(v) {
return this.value = v;
};
IcedTailCall.prototype.canInline = function() {
return !this.value || this.value instanceof IcedReturnValue;
};
IcedTailCall.prototype.literalFunc = function() {
return new Literal(this.func);
};
IcedTailCall.prototype.extractFunc = function() {
return new Value(this.literalFunc());
};
IcedTailCall.prototype.compileNode = function(o) {
var args, f, out;
f = this.literalFunc();
out = o.level === LEVEL_TOP ? this.value ? new Block([this.value, new Call(f)]) : new Call(f) : (args = this.value ? [this.value] : [], new Call(f, args));
return out.compileNode(o);
};
return IcedTailCall;
})(Base);
IcedReturnValue = (function(_super) {
__extends(IcedReturnValue, _super);
IcedReturnValue.counter = 0;
function IcedReturnValue() {
IcedReturnValue.__super__.constructor.call(this, null, null, false);
}
IcedReturnValue.prototype.bindName = function(o) {
var l;
l = "" + (o.scope.freeVariable(iced["const"].param, false)) + "_" + (IcedReturnValue.counter++);
return this.name = new Literal(l);
};
IcedReturnValue.prototype.compile = function(o) {
if (!this.name) {
this.bindName(o);
}
return IcedReturnValue.__super__.compile.call(this, o);
};
return IcedReturnValue;
})(Param);
InlineRuntime = {
generate: function(ns_window) {
var a1, a2, af, apply_call, assignments, body, call_meth, cn, cnt, cnt_member, constructor_assign, constructor_body, constructor_code, constructor_name, constructor_params, decr, defer_assign, defer_body, defer_code, defer_name, defer_params, dp, dp_value, fn, fn_assign, fn_code, fn_name, if_body, if_cond, if_expr, inc, inner_body, inner_code, inner_params, ip, k, k_member, klass, klass_assign, my_apply, my_if, my_null, ns, ns_obj, ns_val, obj, outer_block, p1, ret_member, tr_assign, tr_block, tr_code, tr_name, tr_params, _fulfill_assign, _fulfill_body, _fulfill_call, _fulfill_code, _fulfill_method, _fulfill_name;
k = new Literal("continuation");
cnt = new Literal("count");
cn = new Value(new Literal(iced["const"].Deferrals));
ns = new Value(new Literal(iced["const"].ns));
if (ns_window) {
ns_window.add(new Access(ns));
ns = ns_window;
}
k_member = new Value(new Literal("this"));
k_member.add(new Access(k));
p1 = new Param(k_member);
cnt_member = new Value(new Literal("this"));
cnt_member.add(new Access(cnt));
ret_member = new Value(new Literal("this"));
ret_member.add(new Access(new Value(new Literal(iced["const"].retslot))));
a1 = new Assign(cnt_member, new Value(new Literal(1)));
a2 = new Assign(ret_member, NULL());
constructor_params = [p1];
constructor_body = new Block([a1, a2]);
constructor_code = new Code(constructor_params, constructor_body);
constructor_name = new Value(new Literal("constructor"));
constructor_assign = new Assign(constructor_name, constructor_code);
if_expr = new Call(k_member, [ret_member]);
if_body = new Block([if_expr]);
decr = new Op('--', cnt_member);
if_cond = new Op('!', decr);
my_if = new If(if_cond, if_body);
_fulfill_body = new Block([my_if]);
_fulfill_code = new Code([], _fulfill_body);
_fulfill_name = new Value(new Literal(iced["const"].fulfill));
_fulfill_assign = new Assign(_fulfill_name, _fulfill_code);
inc = new Op("++", cnt_member);
ip = new Literal("inner_params");
dp = new Literal("defer_params");
dp_value = new Value(dp);
call_meth = new Value(dp);
af = new Literal(iced["const"].assign_fn);
call_meth.add(new Access(af, "soak"));
my_apply = new Literal("apply");
call_meth.add(new Access(my_apply, "soak"));
my_null = NULL();
apply_call = new Call(call_meth, [my_null, new Value(ip)]);
_fulfill_method = new Value(new Literal("this"));
_fulfill_method.add(new Access(new Literal(iced["const"].fulfill)));
_fulfill_call = new Call(_fulfill_method, []);
inner_body = new Block([apply_call, _fulfill_call]);
inner_params = [new Param(ip, null, true)];
inner_code = new Code(inner_params, inner_body, "boundfunc");
defer_body = new Block([inc, inner_code]);
defer_params = [new Param(dp)];
defer_code = new Code(defer_params, defer_body);
defer_name = new Value(new Literal(iced["const"].defer_method));
defer_assign = new Assign(defer_name, defer_code);
assignments = [constructor_assign, _fulfill_assign, defer_assign];
obj = new Obj(assignments, true);
body = new Block([new Value(obj)]);
klass = new Class(null, null, body);
klass_assign = new Assign(cn, klass, "object");
outer_block = new Block([NULL()]);
fn_code = new Code([], outer_block);
fn_name = new Value(new Literal(iced["const"].findDeferral));
fn_assign = new Assign(fn_name, fn_code, "object");
fn = new Literal("_fn");
tr_block = new Block([new Call(new Value(fn), [])]);
tr_params = [new Param(fn)];
tr_code = new Code(tr_params, tr_block);
tr_name = new Value(new Literal(iced["const"].trampoline));
tr_assign = new Assign(tr_name, tr_code, "object");
ns_obj = new Obj([klass_assign, fn_assign, tr_assign], true);
ns_val = new Value(ns_obj);
return new Assign(ns, ns_val);
}
};
UTILITIES = {
"extends": function() {
return "function(child, parent) { for (var key in parent) { if (" + (utility('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; }";
},
bind: function() {
return 'function(fn, me){ return function(){ return fn.apply(me, arguments); }; }';
},
indexOf: function() {
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; }";
},
modulo: function() {
return "function(a, b) { return (a % b + +b) % b; }";
},
hasProp: function() {
return '{}.hasOwnProperty';
},
slice: function() {
return '[].slice';
}
};
LEVEL_TOP = 1;
LEVEL_PAREN = 2;
LEVEL_LIST = 3;
LEVEL_COND = 4;
LEVEL_OP = 5;
LEVEL_ACCESS = 6;
TAB = ' ';
IDENTIFIER_STR = "[$A-Za-z_\\x7f-\\uffff][$\\w\\x7f-\\uffff]*";
IDENTIFIER = RegExp("^" + IDENTIFIER_STR + "$");
SIMPLENUM = /^[+-]?\d+$/;
HEXNUM = /^[+-]?0x[\da-f]+/i;
NUMBER = /^[+-]?(?:0x[\da-f]+|\d*\.?\d+(?:e[+-]?\d+)?)$/i;
METHOD_DEF = RegExp("^(" + IDENTIFIER_STR + ")(\\.prototype)?(?:\\.(" + IDENTIFIER_STR + ")|\\[(\"(?:[^\\\\\"\\r\\n]|\\\\.)*\"|'(?:[^\\\\'\\r\\n]|\\\\.)*')\\]|\\[(0x[\\da-fA-F]+|\\d*\\.?\\d+(?:[eE][+-]?\\d+)?)\\])$");
IS_STRING = /^['"]/;
IS_REGEX = /^\//;
utility = function(name) {
var ref;
ref = "__" + name;
Scope.root.assign(ref, UTILITIES[name]());
return ref;
};
multident = function(code, tab) {
code = code.replace(/\n/g, '$&' + tab);
return code.replace(/\s+$/, '');
};
parseNum = function(x) {
if (x == null) {
return 0;
} else if (x.match(HEXNUM)) {
return parseInt(x, 16);
} else {
return parseFloat(x);
}
};
isLiteralArguments = function(node) {
return node instanceof Literal && node.value === 'arguments' && !node.asKey;
};
isLiteralThis = function(node) {
return (node instanceof Literal && node.value === 'this' && !node.asKey) || (node instanceof Code && node.bound) || (node instanceof Call && node.isSuper);
};
unfoldSoak = function(o, parent, name) {
var ifn;
if (!(ifn = parent[name].unfoldSoak(o))) {
return;
}
parent[name] = ifn.body;
ifn.body = new Value(parent);
return ifn;
};
}).call(this);