1
0
Fork 0
mirror of https://github.com/jashkenas/coffeescript.git synced 2022-11-09 12:23:24 -05:00
jashkenas--coffeescript/lib/nodes.js

2112 lines
71 KiB
JavaScript
Raw Normal View History

2010-07-25 03:15:12 -04:00
(function() {
2010-11-20 20:51:43 -05:00
var Access, Arr, Assign, Base, Call, Class, Closure, Code, Comment, Existence, Expressions, Extends, For, IDENTIFIER, IS_STRING, If, In, Index, LEVEL_ACCESS, LEVEL_COND, LEVEL_LIST, LEVEL_OP, LEVEL_PAREN, LEVEL_TOP, Literal, NEGATE, NO, Obj, Op, Param, Parens, Push, Range, Return, SIMPLENUM, Scope, Slice, Splat, Switch, TAB, THIS, TRAILING_WHITESPACE, Throw, Try, UTILITIES, Value, While, YES, compact, del, ends, extend, flatten, last, merge, multident, starts, unfoldSoak, utility, _ref;
var __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) {
2010-11-20 19:17:09 -05:00
for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; }
2010-10-12 15:57:11 -04:00
function ctor() { this.constructor = child; }
ctor.prototype = parent.prototype;
2010-10-12 15:57:11 -04:00
child.prototype = new ctor;
child.__super__ = parent.prototype;
2010-11-09 00:26:31 -05:00
return child;
2010-11-13 18:08:02 -05:00
}, __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
Scope = require('./scope').Scope;
_ref = require('./helpers'), compact = _ref.compact, flatten = _ref.flatten, extend = _ref.extend, merge = _ref.merge, del = _ref.del, starts = _ref.starts, ends = _ref.ends, last = _ref.last;
exports.extend = extend;
YES = function() {
return true;
};
NO = function() {
return false;
};
THIS = function() {
return this;
};
2010-11-08 23:48:38 -05:00
NEGATE = function() {
this.negated = !this.negated;
return this;
};
exports.Base = Base = function() {
function Base() {}
Base.prototype.compile = function(o, lvl) {
var node;
o = extend({}, o);
2010-11-13 21:08:42 -05:00
if (lvl) {
o.level = lvl;
}
node = this.unfoldSoak(o) || this;
node.tab = o.indent;
if (o.level === LEVEL_TOP || node.isPureStatement() || !node.isStatement(o)) {
return node.compileNode(o);
} else {
return node.compileClosure(o);
}
};
Base.prototype.compileClosure = function(o) {
if (this.containsPureStatement()) {
2010-10-20 06:53:41 -04:00
throw SyntaxError('cannot include a pure statement in an expression.');
}
o.sharedScope = true;
return Closure.wrap(this).compileNode(o);
};
2010-10-24 01:05:31 -04:00
Base.prototype.cache = function(o, level, reused) {
var ref, sub;
if (!this.isComplex()) {
2010-10-24 01:05:31 -04:00
ref = level ? this.compile(o, level) : this;
return [ref, ref];
} else {
2010-10-24 01:05:31 -04:00
ref = new Literal(reused || o.scope.freeVariable('ref'));
sub = new Assign(ref, this);
if (level) {
return [sub.compile(o, level), ref.value];
} else {
return [sub, ref];
}
}
};
2010-10-13 07:29:22 -04:00
Base.prototype.compileLoopReference = function(o, name) {
2010-11-20 20:51:43 -05:00
var src, tmp, _ref;
2010-10-23 11:10:28 -04:00
src = tmp = this.compile(o, LEVEL_LIST);
2010-11-20 20:51:43 -05:00
if (!((-Infinity < (_ref = +src) && _ref < Infinity) || IDENTIFIER.test(src) && o.scope.check(src, true))) {
2010-10-13 07:29:22 -04:00
src = "" + (tmp = o.scope.freeVariable(name)) + " = " + src;
}
return [src, tmp];
};
Base.prototype.makeReturn = function() {
return new Return(this);
};
Base.prototype.contains = function(pred) {
var contains;
contains = false;
this.traverseChildren(false, function(node) {
if (pred(node)) {
contains = true;
return false;
}
});
return contains;
};
Base.prototype.containsType = function(type) {
2010-10-06 20:25:00 -04:00
return this instanceof type || this.contains(function(node) {
return node instanceof type;
});
};
Base.prototype.containsPureStatement = function() {
return this.isPureStatement() || this.contains(function(node) {
2010-12-15 22:59:28 -05:00
return node.isPureStatement() && !(node instanceof Comment);
});
};
2010-12-16 11:35:51 -05:00
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 tree;
if (idt == null) {
idt = '';
}
if (name == null) {
name = this.constructor.name;
}
tree = '\n' + idt + name;
if (this.soak) {
tree += '?';
}
this.eachChild(function(node) {
return tree += node.toString(idt + TAB);
});
return tree;
};
Base.prototype.eachChild = function(func) {
var attr, child, _i, _j, _len, _len2, _ref, _ref2;
2010-10-07 07:05:22 -04:00
if (!this.children) {
return this;
2010-05-19 15:37:42 -04:00
}
_ref = this.children;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
attr = _ref[_i];
if (this[attr]) {
_ref2 = flatten([this[attr]]);
for (_j = 0, _len2 = _ref2.length; _j < _len2; _j++) {
child = _ref2[_j];
if (func(child) === false) {
return this;
}
}
}
}
2010-10-20 06:53:41 -04:00
return this;
};
Base.prototype.traverseChildren = function(crossScope, func) {
return this.eachChild(function(child) {
if (func(child) === false) {
return false;
}
return child.traverseChildren(crossScope, func);
});
};
2010-10-10 18:29:38 -04:00
Base.prototype.invert = function() {
return new Op('!', this);
};
2010-10-24 19:00:12 -04:00
Base.prototype.unwrapAll = function() {
var node;
node = this;
while (node !== (node = node.unwrap())) {
continue;
}
return node;
};
Base.prototype.children = [];
Base.prototype.isStatement = NO;
Base.prototype.isPureStatement = NO;
Base.prototype.isComplex = YES;
Base.prototype.isChainable = NO;
Base.prototype.isAssignable = NO;
Base.prototype.unwrap = THIS;
Base.prototype.unfoldSoak = NO;
Base.prototype.assigns = NO;
return Base;
}();
exports.Expressions = Expressions = function() {
__extends(Expressions, Base);
function Expressions(nodes) {
this.expressions = compact(flatten(nodes || []));
}
Expressions.prototype.children = ['expressions'];
Expressions.prototype.push = function(node) {
this.expressions.push(node);
return this;
};
Expressions.prototype.pop = function() {
return this.expressions.pop();
};
Expressions.prototype.unshift = function(node) {
this.expressions.unshift(node);
return this;
};
Expressions.prototype.unwrap = function() {
if (this.expressions.length === 1) {
return this.expressions[0];
} else {
return this;
}
};
Expressions.prototype.isEmpty = function() {
return !this.expressions.length;
};
Expressions.prototype.isStatement = function(o) {
var exp, _i, _len, _ref;
_ref = this.expressions;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
exp = _ref[_i];
if (exp.isPureStatement() || exp.isStatement(o)) {
return true;
}
}
return false;
};
Expressions.prototype.makeReturn = function() {
2010-11-20 16:25:22 -05:00
var expr, len;
len = this.expressions.length;
while (len--) {
expr = this.expressions[len];
if (!(expr instanceof Comment)) {
this.expressions[len] = expr.makeReturn();
break;
}
}
return this;
};
2010-10-24 01:05:31 -04:00
Expressions.prototype.compile = function(o, level) {
if (o == null) {
o = {};
}
if (o.scope) {
return Expressions.__super__.compile.call(this, o, level);
} else {
return this.compileRoot(o);
}
};
Expressions.prototype.compileNode = function(o) {
var code, codes, node, top, _i, _len, _ref;
this.tab = o.indent;
top = o.level === LEVEL_TOP;
codes = [];
_ref = this.expressions;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
node = _ref[_i];
node = node.unwrapAll();
node = node.unfoldSoak(o) || node;
if (top) {
node.front = true;
code = node.compile(o);
codes.push(node.isStatement(o) ? code : this.tab + code + ';');
} else {
codes.push(node.compile(o, LEVEL_LIST));
}
}
if (top) {
return codes.join('\n');
}
code = codes.join(', ') || 'void 0';
if (codes.length > 1 && o.level >= LEVEL_LIST) {
return "(" + code + ")";
} else {
return code;
}
};
Expressions.prototype.compileRoot = function(o) {
2010-10-13 15:09:56 -04:00
var code;
o.indent = this.tab = o.bare ? '' : TAB;
o.scope = new Scope(null, this, null);
2010-10-23 11:10:28 -04:00
o.level = LEVEL_TOP;
code = this.compileWithDeclarations(o);
code = code.replace(TRAILING_WHITESPACE, '');
if (o.bare) {
return code;
} else {
return "(function() {\n" + code + "\n}).call(this);\n";
}
};
Expressions.prototype.compileWithDeclarations = function(o) {
var code, exp, i, post, rest, scope, _len, _ref;
code = post = '';
_ref = this.expressions;
for (i = 0, _len = _ref.length; i < _len; i++) {
exp = _ref[i];
exp = exp.unwrap();
if (!(exp instanceof Comment || exp instanceof Literal)) {
break;
}
}
o.level = LEVEL_TOP;
if (i) {
rest = this.expressions.splice(i, this.expressions.length);
code = this.compileNode(o);
this.expressions = rest;
}
post = this.compileNode(o);
scope = o.scope;
2010-12-06 23:32:32 -05:00
if (scope.expressions === this) {
if (!o.globals && o.scope.hasDeclarations()) {
2010-12-06 23:32:32 -05:00
code += "" + this.tab + "var " + (scope.declaredVariables().join(', ')) + ";\n";
}
if (scope.hasAssignments) {
code += "" + this.tab + "var " + (multident(scope.assignedVariables().join(', '), this.tab)) + ";\n";
}
}
return code + post;
};
Expressions.wrap = function(nodes) {
if (nodes.length === 1 && nodes[0] instanceof Expressions) {
return nodes[0];
}
return new Expressions(nodes);
};
return Expressions;
}();
exports.Literal = Literal = function() {
__extends(Literal, Base);
function Literal(value) {
this.value = value;
}
Literal.prototype.makeReturn = function() {
if (this.isPureStatement()) {
return this;
} else {
return new Return(this);
}
};
Literal.prototype.isPureStatement = function() {
var _ref;
return (_ref = this.value) === 'break' || _ref === 'continue' || _ref === 'debugger';
};
Literal.prototype.isAssignable = function() {
return IDENTIFIER.test(this.value);
};
Literal.prototype.isComplex = NO;
Literal.prototype.assigns = function(name) {
return name === this.value;
};
Literal.prototype.compile = function() {
if (this.value.reserved) {
return "\"" + this.value + "\"";
} else {
return this.value;
}
};
Literal.prototype.toString = function() {
2010-09-26 15:47:52 -04:00
return ' "' + this.value + '"';
};
return Literal;
}();
exports.Return = Return = function() {
__extends(Return, Base);
function Return(expression) {
this.expression = expression;
}
2010-10-20 06:53:41 -04:00
Return.prototype.children = ['expression'];
Return.prototype.isStatement = YES;
Return.prototype.isPureStatement = YES;
Return.prototype.makeReturn = THIS;
2010-10-24 01:05:31 -04:00
Return.prototype.compile = function(o, level) {
var expr, _ref;
expr = (_ref = this.expression) != null ? _ref.makeReturn() : void 0;
if (expr && !(expr instanceof Return)) {
return expr.compile(o, level);
} else {
return Return.__super__.compile.call(this, o, level);
}
};
Return.prototype.compileNode = function(o) {
2010-10-23 11:10:28 -04:00
o.level = LEVEL_PAREN;
return this.tab + ("return" + (this.expression ? ' ' + this.expression.compile(o) : '') + ";");
};
return Return;
}();
exports.Value = Value = function() {
__extends(Value, Base);
function Value(base, props, tag) {
if (!props && base instanceof Value) {
return base;
}
this.base = base;
this.properties = props || [];
if (tag) {
this[tag] = true;
}
2010-11-20 17:46:44 -05:00
return this;
}
Value.prototype.children = ['base', 'properties'];
Value.prototype.push = function(prop) {
this.properties.push(prop);
return this;
};
Value.prototype.hasProperties = function() {
return !!this.properties.length;
};
Value.prototype.isArray = function() {
2010-10-24 19:55:42 -04:00
return !this.properties.length && this.base instanceof Arr;
};
Value.prototype.isComplex = function() {
2010-10-24 19:55:42 -04:00
return this.hasProperties() || this.base.isComplex();
};
Value.prototype.isAssignable = function() {
return this.hasProperties() || this.base.isAssignable();
};
Value.prototype.isSimpleNumber = function() {
return this.base instanceof Literal && SIMPLENUM.test(this.base.value);
};
Value.prototype.isAtomic = function() {
var node, _i, _len, _ref;
_ref = this.properties.concat(this.base);
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
node = _ref[_i];
2010-10-25 09:31:52 -04:00
if (node.soak || node instanceof Call) {
return false;
}
}
return true;
};
2010-10-24 19:55:42 -04:00
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.isObject = function(onlyGenerated) {
if (this.properties.length) {
return false;
}
return (this.base instanceof Obj) && (!onlyGenerated || this.base.generated);
};
2010-11-20 16:25:22 -05:00
Value.prototype.isSplice = function() {
return last(this.properties) instanceof Slice;
};
Value.prototype.makeReturn = function() {
if (this.properties.length) {
return Value.__super__.makeReturn.call(this);
} else {
return this.base.makeReturn();
}
};
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];
2010-09-12 11:08:05 -04:00
}
base = new Value(this.base, this.properties.slice(0, -1));
if (base.isComplex()) {
2010-10-06 23:41:09 -04:00
bref = new Literal(o.scope.freeVariable('base'));
2010-10-06 23:53:26 -04:00
base = new Value(new Parens(new Assign(bref, base)));
}
2010-10-07 07:05:22 -04:00
if (!name) {
return [base, bref];
}
if (name.isComplex()) {
2010-10-06 23:41:09 -04:00
nref = new Literal(o.scope.freeVariable('name'));
name = new Index(new Assign(nref, name.index));
nref = new Index(nref);
}
return [base.push(name), new Value(bref || base.base, [nref || name])];
};
Value.prototype.compileNode = function(o) {
var code, prop, props, _i, _len;
2010-11-01 23:25:28 -04:00
this.base.front = this.front;
props = this.properties;
2010-10-23 11:10:28 -04:00
code = this.base.compile(o, props.length ? LEVEL_ACCESS : null);
2010-11-20 20:09:36 -05:00
if (props[0] instanceof Access && this.isSimpleNumber()) {
code = "(" + code + ")";
}
for (_i = 0, _len = props.length; _i < _len; _i++) {
2010-10-01 18:26:37 -04:00
prop = props[_i];
code += prop.compile(o);
}
return code;
};
Value.prototype.unfoldSoak = function(o) {
var fst, i, ifn, prop, ref, snd, _len, _ref;
if (ifn = this.base.unfoldSoak(o)) {
Array.prototype.push.apply(ifn.body.properties, this.properties);
return ifn;
}
_ref = this.properties;
for (i = 0, _len = _ref.length; i < _len; i++) {
prop = _ref[i];
2010-10-25 09:31:52 -04:00
if (prop.soak) {
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()) {
2010-10-06 23:41:09 -04:00
ref = new Literal(o.scope.freeVariable('ref'));
2010-10-06 23:53:26 -04:00
fst = new Parens(new Assign(ref, fst));
snd.base = ref;
}
return new If(new Existence(fst), snd, {
soak: true
});
2010-09-12 14:44:03 -04:00
}
}
return null;
};
return Value;
}();
exports.Comment = Comment = function() {
__extends(Comment, Base);
function Comment(comment) {
this.comment = comment;
}
Comment.prototype.isPureStatement = YES;
Comment.prototype.isStatement = YES;
Comment.prototype.makeReturn = THIS;
Comment.prototype.compileNode = function(o, level) {
var code;
code = '/*' + multident(this.comment, this.tab) + '*/';
2010-11-13 21:08:42 -05:00
if ((level || o.level) === LEVEL_TOP) {
code = o.indent + code;
}
return code;
};
return Comment;
}();
exports.Call = Call = function() {
__extends(Call, Base);
function Call(variable, args, soak) {
this.args = args != null ? args : [];
this.soak = soak;
this.isNew = false;
this.isSuper = variable === 'super';
this.variable = this.isSuper ? null : variable;
}
Call.prototype.children = ['variable', 'args'];
Call.prototype.newInstance = function() {
2010-12-05 20:44:32 -05:00
var base;
base = this.variable.base || this.variable;
if (base instanceof Call) {
base.newInstance();
} else {
this.isNew = true;
}
return this;
};
Call.prototype.superReference = function(o) {
var method, name;
method = o.scope.method;
2010-10-07 07:05:22 -04:00
if (!method) {
2010-10-20 06:53:41 -04:00
throw SyntaxError('cannot call super outside of a function.');
}
name = method.name;
2010-10-07 07:05:22 -04:00
if (!name) {
2010-10-20 06:53:41 -04:00
throw SyntaxError('cannot call super on an anonymous function.');
}
if (method.klass) {
return "" + method.klass + ".__super__." + name;
} else {
return "" + name + ".__super__.constructor";
}
};
Call.prototype.unfoldSoak = function(o) {
var call, ifn, left, list, rite, _i, _len, _ref, _ref2;
2010-10-25 09:31:52 -04:00
if (this.soak) {
if (this.variable) {
if (ifn = unfoldSoak(o, this, 'variable')) {
return ifn;
}
_ref = new Value(this.variable).cacheReference(o), left = _ref[0], rite = _ref[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;
}
}
_ref2 = list.reverse();
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
call = _ref2[_i];
if (ifn) {
if (call.variable instanceof Call) {
call.variable = ifn;
} else {
call.variable.base = ifn;
}
2010-05-02 23:20:51 -04:00
}
ifn = unfoldSoak(o, call, 'variable');
}
return ifn;
};
Call.prototype.compileNode = function(o) {
var arg, args, code, _ref;
if ((_ref = this.variable) != null) {
_ref.front = this.front;
}
if (code = Splat.compileSplattedArray(o, this.args, true)) {
return this.compileSplat(o, code);
}
2010-11-21 19:59:22 -05:00
args = (function() {
var _i, _len, _ref, _results;
_ref = this.args;
2010-11-09 00:17:08 -05:00
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
arg = _ref[_i];
2010-11-09 00:17:08 -05:00
_results.push(arg.compile(o, LEVEL_LIST));
}
2010-11-09 00:17:08 -05:00
return _results;
2010-11-21 19:59:22 -05:00
}.call(this)).join(', ');
if (this.isSuper) {
return this.superReference(o) + (".call(this" + (args && ', ' + args) + ")");
} else {
return (this.isNew ? 'new ' : '') + this.variable.compile(o, LEVEL_ACCESS) + ("(" + args + ")");
}
};
Call.prototype.compileSuper = function(args, o) {
return "" + (this.superReference(o)) + ".call(this" + (args.length ? ', ' : '') + args + ")";
};
Call.prototype.compileSplat = function(o, splatArgs) {
var base, fun, idt, name, ref;
if (this.isSuper) {
return "" + (this.superReference(o)) + ".apply(this, " + splatArgs + ")";
}
if (this.isNew) {
idt = this.tab + TAB;
return "(function(func, args, ctor) {\n" + idt + "ctor.prototype = func.prototype;\n" + idt + "var child = new ctor, result = func.apply(child, args);\n" + idt + "return typeof result === \"object\" ? result : child;\n" + this.tab + "})(" + (this.variable.compile(o, LEVEL_LIST)) + ", " + splatArgs + ", function() {})";
}
base = new Value(this.variable);
if ((name = base.properties.pop()) && base.isComplex()) {
2010-12-05 13:10:14 -05:00
ref = o.scope.freeVariable('ref');
fun = "(" + ref + " = " + (base.compile(o, LEVEL_LIST)) + ")" + (name.compile(o));
} else {
fun = base.compile(o, LEVEL_ACCESS);
if (name) {
ref = fun;
fun += name.compile(o);
} else {
ref = 'null';
}
}
return "" + fun + ".apply(" + ref + ", " + splatArgs + ")";
};
return Call;
}();
exports.Extends = Extends = function() {
__extends(Extends, Base);
function Extends(child, parent) {
this.child = child;
this.parent = parent;
}
Extends.prototype.children = ['child', 'parent'];
Extends.prototype.compile = function(o) {
utility('hasProp');
2010-10-20 06:53:41 -04:00
return new Call(new Value(new Literal(utility('extends'))), [this.child, this.parent]).compile(o);
};
return Extends;
}();
2010-11-20 20:09:36 -05:00
exports.Access = Access = function() {
__extends(Access, Base);
function Access(name, tag) {
this.name = name;
this.proto = tag === 'proto' ? '.prototype' : '';
this.soak = tag === 'soak';
}
2010-11-20 20:09:36 -05:00
Access.prototype.children = ['name'];
Access.prototype.compile = function(o) {
2010-10-20 06:53:41 -04:00
var name;
name = this.name.compile(o);
return this.proto + (IS_STRING.test(name) ? "[" + name + "]" : "." + name);
};
2010-11-20 20:09:36 -05:00
Access.prototype.isComplex = NO;
return Access;
}();
exports.Index = Index = function() {
__extends(Index, Base);
function Index(index) {
this.index = index;
}
Index.prototype.children = ['index'];
Index.prototype.compile = function(o) {
2010-10-23 11:10:28 -04:00
return (this.proto ? '.prototype' : '') + ("[" + (this.index.compile(o, LEVEL_PAREN)) + "]");
};
Index.prototype.isComplex = function() {
return this.index.isComplex();
};
return Index;
}();
2010-11-20 16:25:22 -05:00
exports.Range = Range = function() {
__extends(Range, Base);
Range.prototype.children = ['from', 'to'];
function Range(from, to, tag) {
this.from = from;
this.to = to;
2010-11-20 16:25:22 -05:00
this.exclusive = tag === 'exclusive';
this.equals = this.exclusive ? '' : '=';
}
Range.prototype.compileVariables = function(o) {
var parts, _ref, _ref2, _ref3;
o = merge(o, {
top: true
});
_ref = this.from.cache(o, LEVEL_LIST), this.from = _ref[0], this.fromVar = _ref[1];
_ref2 = this.to.cache(o, LEVEL_LIST), this.to = _ref2[0], this.toVar = _ref2[1];
_ref3 = [this.fromVar.match(SIMPLENUM), this.toVar.match(SIMPLENUM)], this.fromNum = _ref3[0], this.toNum = _ref3[1];
parts = [];
if (this.from !== this.fromVar) {
parts.push(this.from);
}
if (this.to !== this.toVar) {
return parts.push(this.to);
}
};
Range.prototype.compileNode = function(o) {
var compare, idx, incr, intro, step, stepPart, vars;
this.compileVariables(o);
if (!o.index) {
return this.compileArray(o);
}
if (this.fromNum && this.toNum) {
return this.compileSimple(o);
}
idx = del(o, 'index');
step = del(o, 'step');
vars = ("" + idx + " = " + this.from) + (this.to !== this.toVar ? ", " + this.to : '');
intro = "(" + this.fromVar + " <= " + this.toVar + " ? " + idx;
compare = "" + intro + " <" + this.equals + " " + this.toVar + " : " + idx + " >" + this.equals + " " + this.toVar + ")";
stepPart = step ? step.compile(o) : '1';
incr = step ? "" + idx + " += " + stepPart : "" + intro + " += " + stepPart + " : " + idx + " -= " + stepPart + ")";
return "" + vars + "; " + compare + "; " + incr;
};
Range.prototype.compileSimple = function(o) {
var from, idx, step, to, _ref;
_ref = [+this.fromNum, +this.toNum], from = _ref[0], to = _ref[1];
idx = del(o, 'index');
step = del(o, 'step');
step && (step = "" + idx + " += " + (step.compile(o)));
if (from <= to) {
return "" + idx + " = " + from + "; " + idx + " <" + this.equals + " " + to + "; " + (step || ("" + idx + "++"));
} else {
return "" + idx + " = " + from + "; " + idx + " >" + this.equals + " " + to + "; " + (step || ("" + idx + "--"));
}
};
Range.prototype.compileArray = function(o) {
var body, clause, i, idt, post, pre, range, result, vars, _i, _ref, _ref2, _results;
if (this.fromNum && this.toNum && Math.abs(this.fromNum - this.toNum) <= 20) {
range = (function() {
_results = [];
for (var _i = _ref = +this.fromNum, _ref2 = +this.toNum; _ref <= _ref2 ? _i <= _ref2 : _i >= _ref2; _ref <= _ref2 ? _i += 1 : _i -= 1){ _results.push(_i); }
return _results;
}).call(this);
if (this.exclusive) {
range.pop();
}
return "[" + (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 = this.compileSimple(o);
} else {
vars = ("" + i + " = " + this.from) + (this.to !== this.toVar ? ", " + this.to : '');
clause = "" + this.fromVar + " <= " + this.toVar + " ?";
body = "var " + vars + "; " + clause + " " + i + " <" + this.equals + " " + this.toVar + " : " + i + " >" + this.equals + " " + this.toVar + "; " + clause + " " + i + " += 1 : " + i + " -= 1";
}
post = "{ " + result + ".push(" + i + "); }\n" + idt + "return " + result + ";\n" + o.indent;
return "(function() {" + pre + "\n" + idt + "for (" + body + ")" + post + "}).call(this)";
};
return Range;
}();
exports.Slice = Slice = function() {
__extends(Slice, Base);
Slice.prototype.children = ['range'];
function Slice(range) {
this.range = range;
2010-11-20 16:25:22 -05:00
Slice.__super__.constructor.call(this);
}
Slice.prototype.compileNode = function(o) {
2010-12-20 08:13:07 -05:00
var compiled, from, fromStr, to, toStr, _ref;
_ref = this.range, to = _ref.to, from = _ref.from;
2010-12-19 20:19:50 -05:00
fromStr = from && from.compile(o, LEVEL_PAREN) || '0';
compiled = to && to.compile(o, LEVEL_PAREN);
if (to && !(!this.range.exclusive && +compiled === -1)) {
2010-12-20 08:13:07 -05:00
toStr = ', ' + (this.range.exclusive ? compiled : SIMPLENUM.test(compiled) ? (+compiled + 1).toString() : "(" + compiled + " + 1) || 9e9");
}
return ".slice(" + fromStr + (toStr || '') + ")";
2010-11-20 16:25:22 -05:00
};
return Slice;
}();
exports.Obj = Obj = function() {
__extends(Obj, Base);
function Obj(props, generated) {
this.generated = generated != null ? generated : false;
this.objects = this.properties = props || [];
}
Obj.prototype.children = ['properties'];
Obj.prototype.compileNode = function(o) {
2010-12-16 11:35:51 -05:00
var i, idt, indent, join, lastNoncom, obj, prop, props;
2010-11-08 23:48:38 -05:00
props = this.properties;
if (!props.length) {
if (this.front) {
return '({})';
} else {
return '{}';
}
}
2010-11-08 23:20:11 -05:00
idt = o.indent += TAB;
2010-12-16 11:35:51 -05:00
lastNoncom = this.lastNonComment(this.properties);
2010-11-21 19:59:22 -05:00
props = function() {
var _len, _results;
_results = [];
for (i = 0, _len = props.length; i < _len; i++) {
2010-11-11 02:17:56 -05:00
prop = props[i];
join = i === props.length - 1 ? '' : prop === lastNoncom || prop instanceof Comment ? '\n' : ',\n';
indent = prop instanceof Comment ? '' : idt;
2010-11-01 23:25:28 -04:00
if (prop instanceof Value && prop["this"]) {
prop = new Assign(prop.properties[0].name, prop, 'object');
} else if (!(prop instanceof Assign) && !(prop instanceof Comment)) {
prop = new Assign(prop, prop, 'object');
}
_results.push(indent + prop.compile(o, LEVEL_TOP) + join);
2010-02-09 20:53:25 -05:00
}
return _results;
2010-11-21 19:59:22 -05:00
}();
props = props.join('');
obj = "{" + (props && '\n' + props + '\n' + this.tab) + "}";
if (this.front) {
return "(" + obj + ")";
} else {
return obj;
}
};
Obj.prototype.assigns = function(name) {
var prop, _i, _len, _ref;
_ref = this.properties;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
prop = _ref[_i];
if (prop.assigns(name)) {
return true;
}
}
return false;
};
return Obj;
}();
exports.Arr = Arr = function() {
__extends(Arr, Base);
function Arr(objs) {
this.objects = objs || [];
}
Arr.prototype.children = ['objects'];
Arr.prototype.compileNode = function(o) {
var code, obj;
2010-11-08 23:48:38 -05:00
if (!this.objects.length) {
return '[]';
}
2010-11-08 23:20:11 -05:00
o.indent += TAB;
if (code = Splat.compileSplattedArray(o, this.objects)) {
return code;
}
2010-11-21 19:59:22 -05:00
code = (function() {
var _i, _len, _ref, _results;
_ref = this.objects;
2010-11-09 00:17:08 -05:00
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
obj = _ref[_i];
2010-11-09 00:17:08 -05:00
_results.push(obj.compile(o, LEVEL_LIST));
}
2010-11-09 00:17:08 -05:00
return _results;
2010-11-21 19:59:22 -05:00
}.call(this)).join(', ');
if (code.indexOf('\n') >= 0) {
return "[\n" + o.indent + code + "\n" + this.tab + "]";
} else {
return "[" + code + "]";
}
2010-03-07 19:11:03 -05:00
};
Arr.prototype.assigns = function(name) {
var obj, _i, _len, _ref;
_ref = this.objects;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
obj = _ref[_i];
if (obj.assigns(name)) {
return true;
}
}
return false;
};
return Arr;
}();
exports.Class = Class = function() {
__extends(Class, Base);
function Class(variable, parent, body) {
this.variable = variable;
this.parent = parent;
this.body = body != null ? body : new Expressions;
this.boundFuncs = [];
}
Class.prototype.children = ['variable', 'parent', 'body'];
Class.prototype.determineName = function() {
var decl, tail;
if (!this.variable) {
return null;
}
2010-11-20 20:09:36 -05:00
decl = (tail = last(this.variable.properties)) ? tail instanceof Access && tail.name.value : this.variable.base.value;
return decl && (decl = IDENTIFIER.test(decl) && decl);
};
Class.prototype.setContext = function(name) {
return this.body.traverseChildren(false, function(node) {
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;
}
}
});
};
2010-11-13 17:05:54 -05:00
Class.prototype.addBoundFunctions = function(o) {
var bname, bvar, _i, _len, _ref, _results;
if (this.boundFuncs.length) {
_ref = this.boundFuncs;
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
bvar = _ref[_i];
bname = bvar.compile(o);
_results.push(this.ctor.body.unshift(new Literal("this." + bname + " = " + (utility('bind')) + "(this." + bname + ", this);")));
}
return _results;
}
};
Class.prototype.addProperties = function(node, name) {
var assign, base, func, props, _results;
props = node.base.properties.slice(0);
_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) {
throw new Error('cannot define more than one constructor in a class');
}
if (func.bound) {
throw new Error('cannot define a constructor as a bound function');
}
if (func instanceof Code) {
assign = this.ctor = func;
} else {
assign = this.ctor = new Assign(new Value(new Literal(name)), func);
}
} else {
if (!assign.variable["this"]) {
2010-11-20 20:09:36 -05:00
assign.variable = new Value(new Literal(name), [new Access(base, 'proto')]);
}
if (func instanceof Code && func.bound) {
this.boundFuncs.push(base);
func.bound = false;
}
}
}
_results.push(assign);
}
return _results;
};
2010-11-13 17:05:54 -05:00
Class.prototype.walkBody = function(name) {
2010-11-13 18:02:50 -05:00
return this.traverseChildren(false, __bind(function(child) {
var exps, i, node, _len, _ref;
2010-11-28 17:56:07 -05:00
if (child instanceof Class) {
return false;
}
2010-11-13 18:02:50 -05:00
if (child instanceof Expressions) {
_ref = exps = child.expressions;
for (i = 0, _len = _ref.length; i < _len; i++) {
node = _ref[i];
if (node instanceof Value && node.isObject(true)) {
exps[i] = this.addProperties(node, name);
}
2010-11-13 18:02:50 -05:00
}
return child.expressions = exps = flatten(exps);
}
2010-11-13 18:02:50 -05:00
}, this));
2010-11-13 17:05:54 -05:00
};
Class.prototype.ensureConstructor = function(name) {
if (!this.ctor) {
this.ctor = new Code;
if (this.parent) {
2010-11-13 17:05:54 -05:00
this.ctor.body.push(new Call('super', [new Splat(new Literal('arguments'))]));
}
this.body.expressions.unshift(this.ctor);
}
2010-11-13 17:05:54 -05:00
this.ctor.ctor = this.ctor.name = name;
this.ctor.klass = null;
return this.ctor.noReturn = true;
};
Class.prototype.compileNode = function(o) {
var decl, klass, lname, name;
2010-11-13 17:05:54 -05:00
decl = this.determineName();
name = decl || this.name || '_Class';
lname = new Literal(name);
this.setContext(name);
this.walkBody(name);
if (this.parent) {
2010-11-13 17:05:54 -05:00
this.body.expressions.unshift(new Extends(lname, this.parent));
}
this.ensureConstructor(name);
2010-11-13 17:05:54 -05:00
this.body.expressions.push(lname);
this.addBoundFunctions(o);
klass = new Parens(new Call(new Code([], this.body)), true);
if (this.variable) {
klass = new Assign(this.variable, klass);
}
return klass.compile(o);
};
return Class;
}();
exports.Assign = Assign = function() {
__extends(Assign, Base);
function Assign(variable, value, context, options) {
this.variable = variable;
this.value = value;
this.context = context;
this.param = options && options.param;
}
Assign.prototype.METHOD_DEF = /^(?:(\S+)\.prototype\.|\S+?)?\b([$A-Za-z_][$\w]*)$/;
Assign.prototype.children = ['variable', 'value'];
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 isValue, match, name, val, _ref;
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 ((_ref = this.context) === '||=' || _ref === '&&=' || _ref === '?=') {
return this.compileConditional(o);
}
2010-02-09 20:53:25 -05:00
}
2010-10-23 11:10:28 -04:00
name = this.variable.compile(o, LEVEL_LIST);
if (this.value instanceof Code && (match = this.METHOD_DEF.exec(name))) {
this.value.name = match[2];
if (match[1]) {
this.value.klass = match[1];
}
2010-02-09 20:53:25 -05:00
}
2010-10-23 11:10:28 -04:00
val = this.value.compile(o, LEVEL_LIST);
2010-02-09 20:53:25 -05:00
if (this.context === 'object') {
return "" + name + ": " + val;
2010-02-09 20:53:25 -05:00
}
if (!this.variable.isAssignable()) {
throw SyntaxError("\"" + (this.variable.compile(o)) + "\" cannot be assigned.");
}
if (!(this.context || isValue && (this.variable.namespaced || this.variable.hasProperties()))) {
if (this.param) {
o.scope.add(name, 'var');
} else {
o.scope.find(name);
}
2010-02-09 20:53:25 -05:00
}
val = name + (" " + (this.context || '=') + " ") + val;
if (o.level <= LEVEL_LIST) {
return val;
} else {
return "(" + val + ")";
}
};
Assign.prototype.compilePatternMatch = function(o) {
var acc, assigns, code, i, idx, isObject, ivar, obj, objects, olen, ref, rest, splat, top, val, value, vvar, _len, _ref, _ref2, _ref3, _ref4;
2010-10-23 11:10:28 -04:00
top = o.level === LEVEL_TOP;
value = this.value;
objects = this.variable.base.objects;
2010-10-26 07:51:02 -04:00
if (!(olen = objects.length)) {
return value.compile(o);
}
2010-10-02 07:53:29 -04:00
isObject = this.variable.isObject();
2010-10-26 07:51:02 -04:00
if (top && olen === 1 && !((obj = objects[0]) instanceof Splat)) {
if (obj instanceof Assign) {
_ref = obj, idx = _ref.variable.base, obj = _ref.value;
} else {
if (obj.base instanceof Parens) {
_ref2 = new Value(obj.unwrapAll()).cacheReference(o), obj = _ref2[0], idx = _ref2[1];
} else {
2010-11-01 23:25:28 -04:00
idx = isObject ? obj["this"] ? obj.properties[0].name : obj : new Literal(0);
}
}
acc = IDENTIFIER.test(idx.unwrap().value || 0);
2010-10-24 19:13:58 -04:00
value = new Value(value);
2010-11-20 20:09:36 -05:00
value.properties.push(new (acc ? Access : Index)(idx));
return new Assign(obj, value).compile(o);
}
2010-10-26 07:51:02 -04:00
vvar = value.compile(o, LEVEL_LIST);
assigns = [];
2010-03-22 02:02:04 -04:00
splat = false;
2010-10-26 07:51:02 -04:00
if (!IDENTIFIER.test(vvar) || this.variable.assigns(vvar)) {
assigns.push("" + (ref = o.scope.freeVariable('ref')) + " = " + vvar);
vvar = ref;
}
for (i = 0, _len = objects.length; i < _len; i++) {
2010-10-01 18:26:37 -04:00
obj = objects[i];
2010-02-11 18:44:00 -05:00
idx = i;
if (isObject) {
if (obj instanceof Assign) {
_ref3 = obj, idx = _ref3.variable.base, obj = _ref3.value;
} else {
if (obj.base instanceof Parens) {
_ref4 = new Value(obj.unwrapAll()).cacheReference(o), obj = _ref4[0], idx = _ref4[1];
} else {
2010-11-01 23:25:28 -04:00
idx = obj["this"] ? obj.properties[0].name : obj;
}
}
2010-02-09 20:53:25 -05:00
}
if (!splat && obj instanceof Splat) {
2010-10-26 07:51:02 -04:00
val = "" + olen + " <= " + vvar + ".length ? " + (utility('slice')) + ".call(" + vvar + ", " + i;
if (rest = olen - i - 1) {
ivar = o.scope.freeVariable('i');
2010-10-26 07:51:02 -04:00
val += ", " + ivar + " = " + vvar + ".length - " + rest + ") : (" + ivar + " = " + i + ", [])";
} else {
val += ") : []";
}
2010-10-26 07:51:02 -04:00
val = new Literal(val);
splat = "" + ivar + "++";
} else {
if (obj instanceof Splat) {
obj = obj.name.compile(o);
throw SyntaxError("multiple splats are disallowed in an assignment: " + obj + " ...");
}
if (typeof idx === 'number') {
idx = new Literal(splat || idx);
acc = false;
} else {
acc = isObject && IDENTIFIER.test(idx.unwrap().value || 0);
2010-03-22 02:02:04 -04:00
}
2010-11-20 20:09:36 -05:00
val = new Value(new Literal(vvar), [new (acc ? Access : Index)(idx)]);
}
assigns.push(new Assign(obj, val, null, {
param: this.param
}).compile(o, LEVEL_TOP));
2010-02-09 20:53:25 -05:00
}
2010-10-07 07:05:22 -04:00
if (!top) {
2010-10-26 07:51:02 -04:00
assigns.push(vvar);
}
code = assigns.join(', ');
if (o.level < LEVEL_LIST) {
return code;
} else {
return "(" + code + ")";
}
};
Assign.prototype.compileConditional = function(o) {
var left, rite, _ref;
_ref = this.variable.cacheReference(o), left = _ref[0], rite = _ref[1];
return new Op(this.context.slice(0, -1), left, new Assign(rite, this.value, '=')).compile(o);
};
2010-11-20 16:25:22 -05:00
Assign.prototype.compileSplice = function(o) {
var excl, from, name, range, to, val;
2010-11-20 16:25:22 -05:00
range = this.variable.properties.pop().range;
name = this.variable.compile(o);
excl = range.exclusive;
2010-11-20 16:25:22 -05:00
from = range.from ? range.from.compile(o) : '0';
if (!range.to) {
to = "" + name + ".length";
}
if (!to) {
if (range.from && range.from.isSimpleNumber() && range.to.isSimpleNumber()) {
to = +range.to.compile(o) - +from;
if (!excl) {
to += 1;
}
} else {
to = range.to.compile(o) + ' - ' + from;
if (!excl) {
to += ' + 1';
}
}
}
2010-11-20 16:25:22 -05:00
val = this.value.compile(o);
return "[].splice.apply(" + name + ", [" + from + ", " + to + "].concat(" + val + "))";
2010-11-20 16:25:22 -05:00
};
return Assign;
}();
exports.Code = Code = function() {
__extends(Code, Base);
function Code(params, body, tag) {
this.params = params || [];
this.body = body || new Expressions;
this.bound = tag === 'boundfunc';
if (this.bound) {
this.context = 'this';
}
}
Code.prototype.children = ['params', 'body'];
Code.prototype.isStatement = function() {
return !!this.ctor;
};
Code.prototype.compileNode = function(o) {
var code, exprs, i, idt, lit, p, param, ref, splats, v, val, vars, wasEmpty, _i, _j, _len, _len2, _len3, _ref, _ref2, _ref3;
o.scope = new Scope(o.scope, this.body, this);
o.scope.shared = del(o, 'sharedScope');
2010-11-08 23:20:11 -05:00
o.indent += TAB;
2010-10-13 15:09:56 -04:00
delete o.bare;
delete o.globals;
vars = [];
exprs = [];
_ref = this.params;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
param = _ref[_i];
if (param.splat) {
2010-11-21 19:59:22 -05:00
splats = new Assign(new Value(new Arr(function() {
var _i, _len, _ref, _results;
_ref = this.params;
2010-11-09 00:17:08 -05:00
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
p = _ref[_i];
2010-11-09 00:17:08 -05:00
_results.push(p.asReference(o));
}
2010-11-09 00:17:08 -05:00
return _results;
2010-11-21 19:59:22 -05:00
}.call(this))), new Value(new Literal('arguments')));
break;
}
}
_ref2 = this.params;
for (_j = 0, _len2 = _ref2.length; _j < _len2; _j++) {
param = _ref2[_j];
if (param.isComplex()) {
2010-11-01 23:11:25 -04:00
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) {
2010-11-01 23:11:25 -04:00
lit = new Literal(ref.name.value + ' == null');
val = new Assign(new Value(param.name), param.value, '=');
exprs.push(new If(lit, val));
2010-07-28 01:54:36 -04:00
}
}
if (!splats) {
vars.push(ref);
}
}
wasEmpty = this.body.isEmpty();
if (splats) {
exprs.unshift(splats);
}
if (exprs.length) {
(_ref3 = this.body.expressions).unshift.apply(_ref3, exprs);
}
if (!splats) {
for (i = 0, _len3 = vars.length; i < _len3; i++) {
v = vars[i];
o.scope.parameter(vars[i] = v.compile(o));
}
2010-10-26 00:09:46 -04:00
}
if (!(wasEmpty || this.noReturn)) {
this.body.makeReturn();
}
2010-11-08 23:20:11 -05:00
idt = o.indent;
code = 'function';
if (this.ctor) {
code += ' ' + this.name;
2010-10-04 23:21:16 -04:00
}
code += '(' + vars.join(', ') + ') {';
if (!this.body.isEmpty()) {
code += "\n" + (this.body.compileWithDeclarations(o)) + "\n" + this.tab;
}
code += '}';
if (this.ctor) {
return this.tab + code;
}
2010-07-25 03:15:12 -04:00
if (this.bound) {
return utility('bind') + ("(" + code + ", " + this.context + ")");
}
if (this.front) {
return "(" + code + ")";
} else {
return code;
}
};
Code.prototype.traverseChildren = function(crossScope, func) {
if (crossScope) {
return Code.__super__.traverseChildren.call(this, crossScope, func);
}
};
return Code;
}();
exports.Param = Param = function() {
__extends(Param, Base);
function Param(name, value, splat) {
this.name = name;
this.value = value;
this.splat = splat;
}
2010-10-26 00:09:46 -04:00
Param.prototype.children = ['name', 'value'];
Param.prototype.compile = function(o) {
2010-10-26 00:09:46 -04:00
return this.name.compile(o, LEVEL_LIST);
2010-07-28 01:54:36 -04:00
};
Param.prototype.asReference = function(o) {
var node;
if (this.reference) {
return this.reference;
2010-09-26 15:47:52 -04:00
}
node = this.name;
if (node["this"]) {
node = node.properties[0].name;
if (node.value.reserved) {
node = new Literal('_' + node.value);
}
} else if (node.isComplex()) {
node = new Literal(o.scope.freeVariable('arg'));
}
node = new Value(node);
2010-09-26 15:47:52 -04:00
if (this.splat) {
node = new Splat(node);
2010-09-26 15:47:52 -04:00
}
return this.reference = node;
};
Param.prototype.isComplex = function() {
return this.name.isComplex();
2010-07-28 01:54:36 -04:00
};
return Param;
}();
exports.Splat = Splat = function() {
__extends(Splat, Base);
Splat.prototype.children = ['name'];
Splat.prototype.isAssignable = YES;
function Splat(name) {
this.name = name.compile ? name : new Literal(name);
}
Splat.prototype.assigns = function(name) {
return this.name.assigns(name);
};
Splat.prototype.compile = function(o) {
if (this.index != null) {
return this.compileParam(o);
} else {
return this.name.compile(o);
}
};
Splat.compileSplattedArray = function(o, list, apply) {
var args, base, code, i, index, node, _len;
index = -1;
while ((node = list[++index]) && !(node instanceof Splat)) {
continue;
}
if (index >= list.length) {
return '';
}
if (list.length === 1) {
code = list[0].compile(o, LEVEL_LIST);
if (apply) {
return code;
}
return "" + (utility('slice')) + ".call(" + code + ")";
}
args = list.slice(index);
for (i = 0, _len = args.length; i < _len; i++) {
node = args[i];
code = node.compile(o, LEVEL_LIST);
args[i] = node instanceof Splat ? "" + (utility('slice')) + ".call(" + code + ")" : "[" + code + "]";
}
if (index === 0) {
return args[0] + (".concat(" + (args.slice(1).join(', ')) + ")");
}
2010-11-21 19:59:22 -05:00
base = (function() {
var _i, _len, _ref, _results;
_ref = list.slice(0, index);
2010-11-09 00:17:08 -05:00
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
node = _ref[_i];
2010-11-09 00:17:08 -05:00
_results.push(node.compile(o, LEVEL_LIST));
}
2010-11-09 00:17:08 -05:00
return _results;
2010-11-21 19:59:22 -05:00
}());
return "[" + (base.join(', ')) + "].concat(" + (args.join(', ')) + ")";
};
return Splat;
}();
exports.While = While = function() {
__extends(While, Base);
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.makeReturn = function() {
this.returns = true;
return this;
};
While.prototype.addBody = function(body) {
this.body = body;
2010-02-09 22:05:17 -05:00
return this;
};
While.prototype.containsPureStatement = function() {
var expressions, i, ret, _ref;
expressions = this.body.expressions;
i = expressions.length;
if ((_ref = expressions[--i]) != null ? _ref.containsPureStatement() : void 0) {
return true;
}
ret = function(node) {
return node instanceof Return;
};
while (i--) {
if (expressions[i].contains(ret)) {
return true;
}
}
return false;
};
While.prototype.compileNode = function(o) {
var body, code, rvar, set;
2010-11-08 23:20:11 -05:00
o.indent += TAB;
2010-02-09 22:05:17 -05:00
set = '';
body = this.body;
if (body.isEmpty()) {
body = '';
} else {
if (o.level > LEVEL_TOP || this.returns) {
2010-11-09 00:17:08 -05:00
rvar = o.scope.freeVariable('results');
set = "" + this.tab + rvar + " = [];\n";
if (body) {
body = Push.wrap(rvar, body);
}
}
if (this.guard) {
body = Expressions.wrap([new If(this.guard, body)]);
}
body = "\n" + (body.compile(o, LEVEL_TOP)) + "\n" + this.tab;
2010-02-09 22:05:17 -05:00
}
code = set + this.tab + ("while (" + (this.condition.compile(o, LEVEL_PAREN)) + ") {" + body + "}");
2010-08-11 00:40:15 -04:00
if (this.returns) {
o.indent = this.tab;
code += '\n' + new Return(new Literal(rvar)).compile(o);
}
return code;
};
return While;
}();
exports.Op = Op = function() {
var CONVERSIONS, INVERSIONS;
__extends(Op, Base);
function Op(op, first, second, flip) {
if (op === 'in') {
return new In(first, second);
}
if (op === 'new') {
if (first instanceof Call) {
return first.newInstance();
}
if (first instanceof Code && first.bound) {
first = new Parens(first);
2010-10-04 23:21:16 -04:00
}
}
this.operator = CONVERSIONS[op] || op;
this.first = first;
this.second = second;
this.flip = !!flip;
2010-11-20 17:46:44 -05:00
return this;
}
CONVERSIONS = {
'==': '===',
'!=': '!==',
'of': 'in'
};
INVERSIONS = {
'!==': '===',
'===': '!=='
};
Op.prototype.children = ['first', 'second'];
Op.prototype.isUnary = function() {
return !this.second;
};
Op.prototype.isChainable = function() {
var _ref;
return (_ref = this.operator) === '<' || _ref === '>' || _ref === '>=' || _ref === '<=' || _ref === '===' || _ref === '!==';
};
Op.prototype.invert = function() {
var allInvertable, curr, fst, op, _ref;
if (this.isChainable() && this.first.isChainable()) {
allInvertable = true;
curr = this;
2010-12-03 01:23:54 -05:00
while (curr && curr.operator) {
allInvertable && (allInvertable = curr.operator in INVERSIONS);
curr = curr.first;
}
if (!allInvertable) {
return new Parens(this).invert();
}
curr = this;
2010-12-03 01:23:54 -05:00
while (curr && curr.operator) {
curr.invert = !curr.invert;
curr.operator = INVERSIONS[curr.operator];
curr = curr.first;
}
return this;
} else if (op = INVERSIONS[this.operator]) {
2010-10-20 06:53:41 -04:00
this.operator = op;
if (this.first.unwrap() instanceof Op) {
this.first.invert();
}
2010-10-10 18:29:38 -04:00
return this;
} else if (this.second) {
return new Parens(this).invert();
} else if (this.operator === '!' && (fst = this.first.unwrap()) instanceof Op && ((_ref = fst.operator) === '!' || _ref === 'in' || _ref === 'instanceof')) {
return fst;
} else {
return new Op('!', this);
}
};
Op.prototype.unfoldSoak = function(o) {
var _ref;
return ((_ref = this.operator) === '++' || _ref === '--' || _ref === 'delete') && unfoldSoak(o, this, 'first');
};
Op.prototype.compileNode = function(o) {
var code;
if (this.isUnary()) {
return this.compileUnary(o);
}
if (this.isChainable() && this.first.isChainable()) {
return this.compileChain(o);
}
if (this.operator === '?') {
return this.compileExistence(o);
}
2010-11-01 23:25:28 -04:00
this.first.front = this.front;
code = this.first.compile(o, LEVEL_OP) + ' ' + this.operator + ' ' + this.second.compile(o, LEVEL_OP);
if (o.level <= LEVEL_OP) {
return code;
} else {
return "(" + code + ")";
}
};
Op.prototype.compileChain = function(o) {
var code, fst, shared, _ref;
_ref = this.first.second.cache(o), this.first.second = _ref[0], shared = _ref[1];
2010-10-23 11:11:49 -04:00
fst = this.first.compile(o, LEVEL_OP);
code = "" + fst + " " + (this.invert ? '&&' : '||') + " " + (shared.compile(o)) + " " + this.operator + " " + (this.second.compile(o, LEVEL_OP));
return "(" + code + ")";
};
Op.prototype.compileExistence = function(o) {
var fst, ref;
if (this.first.isComplex()) {
ref = o.scope.freeVariable('ref');
2010-10-06 23:53:26 -04:00
fst = new Parens(new Assign(new Literal(ref), this.first));
} else {
fst = this.first;
ref = fst.compile(o);
}
return new Existence(fst).compile(o) + (" ? " + ref + " : " + (this.second.compile(o, LEVEL_LIST)));
};
Op.prototype.compileUnary = function(o) {
var op, parts;
parts = [op = this.operator];
if ((op === 'new' || op === 'typeof' || op === 'delete') || (op === '+' || op === '-') && this.first instanceof Op && this.first.operator === op) {
parts.push(' ');
}
2010-10-23 11:11:49 -04:00
parts.push(this.first.compile(o, LEVEL_OP));
if (this.flip) {
parts.reverse();
}
return parts.join('');
};
Op.prototype.toString = function(idt) {
return Op.__super__.toString.call(this, idt, this.constructor.name + ' ' + this.operator);
};
return Op;
}();
exports.In = In = function() {
__extends(In, Base);
function In(object, array) {
this.object = object;
this.array = array;
}
In.prototype.children = ['object', 'array'];
2010-11-08 23:48:38 -05:00
In.prototype.invert = NEGATE;
In.prototype.compileNode = function(o) {
if (this.array instanceof Value && this.array.isArray()) {
return this.compileOrTest(o);
} else {
return this.compileLoopTest(o);
}
};
In.prototype.compileOrTest = function(o) {
var cmp, cnj, i, item, ref, sub, tests, _ref, _ref2;
_ref = this.object.cache(o, LEVEL_OP), sub = _ref[0], ref = _ref[1];
_ref2 = this.negated ? [' !== ', ' && '] : [' === ', ' || '], cmp = _ref2[0], cnj = _ref2[1];
2010-11-21 19:59:22 -05:00
tests = function() {
var _len, _ref, _results;
_ref = this.array.base.objects;
2010-11-09 00:17:08 -05:00
_results = [];
for (i = 0, _len = _ref.length; i < _len; i++) {
item = _ref[i];
2010-11-09 00:17:08 -05:00
_results.push((i ? ref : sub) + cmp + item.compile(o, LEVEL_OP));
}
2010-11-09 00:17:08 -05:00
return _results;
2010-11-21 19:59:22 -05:00
}.call(this);
2010-10-20 20:16:17 -04:00
tests = tests.join(cnj);
if (o.level < LEVEL_OP) {
return tests;
} else {
return "(" + tests + ")";
}
};
In.prototype.compileLoopTest = function(o) {
var code, ref, sub, _ref;
_ref = this.object.cache(o, LEVEL_LIST), sub = _ref[0], ref = _ref[1];
code = utility('indexOf') + (".call(" + (this.array.compile(o, LEVEL_LIST)) + ", " + ref + ") ") + (this.negated ? '< 0' : '>= 0');
if (sub === ref) {
return code;
}
code = sub + ', ' + code;
if (o.level < LEVEL_LIST) {
return code;
} else {
return "(" + code + ")";
}
2010-10-20 20:16:17 -04:00
};
In.prototype.toString = function(idt) {
return In.__super__.toString.call(this, idt, this.constructor.name + (this.negated ? '!' : ''));
};
return In;
}();
exports.Try = Try = function() {
__extends(Try, Base);
function Try(attempt, error, recovery, ensure) {
this.attempt = attempt;
this.error = error;
this.recovery = recovery;
this.ensure = ensure;
}
Try.prototype.children = ['attempt', 'recovery', 'ensure'];
Try.prototype.isStatement = YES;
Try.prototype.makeReturn = function() {
if (this.attempt) {
this.attempt = this.attempt.makeReturn();
}
if (this.recovery) {
this.recovery = this.recovery.makeReturn();
}
return this;
};
Try.prototype.compileNode = function(o) {
var catchPart, errorPart;
2010-11-08 23:20:11 -05:00
o.indent += TAB;
errorPart = this.error ? " (" + (this.error.compile(o)) + ") " : ' ';
catchPart = this.recovery ? " catch" + errorPart + "{\n" + (this.recovery.compile(o, LEVEL_TOP)) + "\n" + this.tab + "}" : !(this.ensure || this.recovery) ? ' catch (_e) {}' : void 0;
2010-10-23 11:10:28 -04:00
return ("" + this.tab + "try {\n" + (this.attempt.compile(o, LEVEL_TOP)) + "\n" + this.tab + "}" + (catchPart || '')) + (this.ensure ? " finally {\n" + (this.ensure.compile(o, LEVEL_TOP)) + "\n" + this.tab + "}" : '');
};
return Try;
}();
exports.Throw = Throw = function() {
__extends(Throw, Base);
function Throw(expression) {
this.expression = expression;
}
Throw.prototype.children = ['expression'];
Throw.prototype.isStatement = YES;
Throw.prototype.makeReturn = THIS;
Throw.prototype.compileNode = function(o) {
return this.tab + ("throw " + (this.expression.compile(o)) + ";");
};
return Throw;
}();
exports.Existence = Existence = function() {
__extends(Existence, Base);
function Existence(expression) {
this.expression = expression;
}
Existence.prototype.children = ['expression'];
2010-11-08 23:48:38 -05:00
Existence.prototype.invert = NEGATE;
Existence.prototype.compileNode = function(o) {
var code, sym;
code = this.expression.compile(o, LEVEL_OP);
code = IDENTIFIER.test(code) && !o.scope.check(code) ? this.negated ? "typeof " + code + " == \"undefined\" || " + code + " === null" : "typeof " + code + " != \"undefined\" && " + code + " !== null" : (sym = this.negated ? '==' : '!=', "" + code + " " + sym + " null");
if (o.level <= LEVEL_COND) {
return code;
} else {
return "(" + code + ")";
}
};
return Existence;
}();
exports.Parens = Parens = function() {
__extends(Parens, Base);
function Parens(body) {
this.body = body;
}
Parens.prototype.children = ['body'];
Parens.prototype.unwrap = function() {
return this.body;
};
2010-10-06 23:53:26 -04:00
Parens.prototype.isComplex = function() {
return this.body.isComplex();
};
2010-10-06 23:53:26 -04:00
Parens.prototype.makeReturn = function() {
return this.body.makeReturn();
};
2010-10-06 23:53:26 -04:00
Parens.prototype.compileNode = function(o) {
var bare, code, expr;
expr = this.body.unwrap();
if (expr instanceof Value && expr.isAtomic()) {
2010-11-01 23:25:28 -04:00
expr.front = this.front;
return expr.compile(o);
}
2010-10-23 11:10:28 -04:00
bare = o.level < LEVEL_OP && (expr instanceof Op || expr instanceof Call);
code = expr.compile(o, LEVEL_PAREN);
if (bare) {
return code;
} else {
return "(" + code + ")";
}
};
2010-10-06 23:53:26 -04:00
return Parens;
}();
exports.For = For = function() {
__extends(For, Base);
function For(body, source, name, index) {
2010-11-20 16:25:22 -05:00
var _ref;
this.name = name;
this.index = index;
2010-11-20 16:25:22 -05:00
this.source = source.source, this.guard = source.guard, this.step = source.step;
this.body = Expressions.wrap([body]);
2010-11-28 18:33:43 -05:00
this.own = !!source.own;
2010-11-20 16:25:22 -05:00
this.object = !!source.object;
if (this.object) {
_ref = [this.index, this.name], this.name = _ref[0], this.index = _ref[1];
}
if (this.index instanceof Value) {
throw SyntaxError('index cannot be a pattern matching expression');
}
2010-11-20 16:25:22 -05:00
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) {
throw SyntaxError('indexes do not apply to range loops');
}
2010-11-20 16:25:22 -05:00
if (this.range && this.pattern) {
throw SyntaxError('cannot pattern match over range loops');
2010-11-20 16:25:22 -05:00
}
this.returns = false;
}
2010-11-20 16:25:22 -05:00
For.prototype.children = ['body', 'source', 'guard', 'step'];
2010-10-20 06:53:41 -04:00
For.prototype.isStatement = YES;
For.prototype.makeReturn = function() {
this.returns = true;
return this;
};
For.prototype.containsPureStatement = While.prototype.containsPureStatement;
For.prototype.compileNode = function(o) {
var body, defPart, forPart, guardPart, hasCode, hasPure, idt1, index, ivar, lvar, name, namePart, ref, resultPart, returnResult, rvar, scope, source, stepPart, svar, varPart, _ref;
body = Expressions.wrap([this.body]);
hasCode = body.contains(function(node) {
2010-11-20 18:14:05 -05:00
return node instanceof Code;
});
hasPure = (_ref = last(body.expressions)) != null ? _ref.containsPureStatement() : void 0;
2010-11-20 16:25:22 -05:00
source = this.range ? this.source.base : this.source;
scope = o.scope;
2010-11-20 16:25:22 -05:00
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, {
immediate: true
});
}
if (index) {
scope.find(index, {
immediate: true
});
}
if (this.returns && !hasPure) {
2010-11-20 16:25:22 -05:00
rvar = scope.freeVariable('results');
}
ivar = (this.range ? name : index) || scope.freeVariable('i');
varPart = '';
guardPart = '';
2010-11-20 18:14:05 -05:00
defPart = '';
2010-11-20 16:25:22 -05:00
idt1 = this.tab + TAB;
if (this.range) {
forPart = source.compile(merge(o, {
index: ivar,
step: this.step
}));
} else {
2010-12-09 21:12:24 -05:00
svar = this.source.compile(o, LEVEL_LIST);
2010-11-28 18:33:43 -05:00
if ((name || this.own) && !IDENTIFIER.test(svar)) {
2010-11-20 18:14:05 -05:00
defPart = "" + this.tab + (ref = scope.freeVariable('ref')) + " = " + svar + ";\n";
2010-11-20 16:25:22 -05:00
svar = ref;
}
namePart = this.pattern ? new Assign(this.name, new Literal("" + svar + "[" + ivar + "]")).compile(o, LEVEL_TOP) : name ? "" + name + " = " + svar + "[" + ivar + "]" : void 0;
2010-10-07 07:05:22 -04:00
if (!this.object) {
2010-11-20 16:25:22 -05:00
lvar = scope.freeVariable('len');
stepPart = this.step ? "" + ivar + " += " + (this.step.compile(o, LEVEL_OP)) : "" + ivar + "++";
forPart = "" + ivar + " = 0, " + lvar + " = " + svar + ".length; " + ivar + " < " + lvar + "; " + stepPart;
}
}
if (this.returns && !hasPure) {
2010-11-20 16:25:22 -05:00
resultPart = "" + this.tab + rvar + " = [];\n";
returnResult = '\n' + (new Return(new Literal(rvar)).compile(o, LEVEL_PAREN));
body = Push.wrap(rvar, body);
2010-10-13 07:29:22 -04:00
}
2010-11-20 16:25:22 -05:00
if (this.guard) {
body = Expressions.wrap([new If(this.guard, body)]);
}
2010-11-20 18:14:05 -05:00
if (hasCode) {
body = Closure.wrap(body, true, !this.returns);
2010-11-20 18:14:05 -05:00
}
2010-10-13 07:29:22 -04:00
if (namePart) {
2010-11-20 18:37:19 -05:00
varPart = "\n" + idt1 + namePart + ";";
}
2010-11-20 16:25:22 -05:00
if (this.object) {
forPart = "" + ivar + " in " + svar;
2010-11-28 18:33:43 -05:00
if (this.own) {
2010-11-20 16:25:22 -05:00
guardPart = "\n" + idt1 + "if (!" + (utility('hasProp')) + ".call(" + svar + ", " + ivar + ")) continue;";
}
}
2010-11-20 18:14:05 -05:00
if (!this.pattern) {
defPart += this.pluckDirectCall(o, body, name, index);
}
2010-11-20 16:25:22 -05:00
body = body.compile(merge(o, {
2010-11-20 18:14:05 -05:00
indent: idt1
}), LEVEL_TOP);
2010-11-20 18:37:19 -05:00
if (body) {
body = '\n' + body + '\n';
}
return "" + defPart + (resultPart || '') + this.tab + "for (" + forPart + ") {" + guardPart + varPart + body + this.tab + "}" + (returnResult || '');
};
For.prototype.pluckDirectCall = function(o, body, name, index) {
var arg, args, base, defs, expr, fn, i, idx, ref, val, _len, _len2, _ref, _ref2, _ref3, _ref4, _ref5, _ref6;
defs = '';
_ref = body.expressions;
for (idx = 0, _len = _ref.length; idx < _len; idx++) {
expr = _ref[idx];
expr = expr.unwrapAll();
if (!(expr instanceof Call)) {
continue;
}
val = expr.variable.unwrapAll();
if (!((val instanceof Code) || (val instanceof Value && ((_ref2 = val.base) != null ? _ref2.unwrapAll() : void 0) instanceof Code && val.properties.length === 1 && ((_ref3 = (_ref4 = val.properties[0].name) != null ? _ref4.value : void 0) === 'call' || _ref3 === 'apply')))) {
continue;
}
fn = ((_ref5 = val.base) != null ? _ref5.unwrapAll() : void 0) || val;
ref = new Literal(o.scope.freeVariable('fn'));
base = new Value(ref);
args = compact([name, index]);
if (this.object) {
args.reverse();
}
for (i = 0, _len2 = args.length; i < _len2; i++) {
arg = args[i];
fn.params.push(new Param(args[i] = new Literal(arg)));
}
if (val.base) {
_ref6 = [base, val], val.base = _ref6[0], base = _ref6[1];
args.unshift(new Literal('this'));
}
body.expressions[idx] = new Call(base, args);
defs += this.tab + new Assign(ref, fn).compile(o, LEVEL_TOP) + ';\n';
}
return defs;
};
return For;
}();
exports.Switch = Switch = function() {
__extends(Switch, Base);
function Switch(subject, cases, otherwise) {
this.subject = subject;
this.cases = cases;
this.otherwise = otherwise;
}
Switch.prototype.children = ['subject', 'cases', 'otherwise'];
Switch.prototype.isStatement = YES;
Switch.prototype.makeReturn = function() {
var pair, _i, _len, _ref, _ref2;
_ref = this.cases;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
pair = _ref[_i];
pair[1].makeReturn();
}
if ((_ref2 = this.otherwise) != null) {
_ref2.makeReturn();
}
return this;
};
Switch.prototype.compileNode = function(o) {
var block, body, code, cond, conditions, expr, i, idt1, idt2, _i, _len, _len2, _ref, _ref2, _ref3, _ref4;
2010-11-08 23:20:11 -05:00
idt1 = o.indent + TAB;
idt2 = o.indent = idt1 + TAB;
code = this.tab + ("switch (" + (((_ref = this.subject) != null ? _ref.compile(o, LEVEL_PAREN) : void 0) || false) + ") {\n");
2010-11-20 16:25:22 -05:00
_ref2 = this.cases;
for (i = 0, _len = _ref2.length; i < _len; i++) {
_ref3 = _ref2[i], conditions = _ref3[0], block = _ref3[1];
_ref4 = flatten([conditions]);
for (_i = 0, _len2 = _ref4.length; _i < _len2; _i++) {
cond = _ref4[_i];
2010-10-20 06:53:41 -04:00
if (!this.subject) {
2010-10-24 17:20:45 -04:00
cond = cond.invert();
}
2010-10-23 11:10:28 -04:00
code += idt1 + ("case " + (cond.compile(o, LEVEL_PAREN)) + ":\n");
}
if (body = block.compile(o, LEVEL_TOP)) {
code += body + '\n';
}
if (i === this.cases.length - 1 && !this.otherwise) {
break;
}
2010-12-16 11:35:51 -05:00
expr = this.lastNonComment(block.expressions);
if (!expr || !expr.isPureStatement()) {
2010-12-15 23:38:27 -05:00
code += idt2 + 'break;\n';
}
}
if (this.otherwise) {
code += idt1 + ("default:\n" + (this.otherwise.compile(o, LEVEL_TOP)) + "\n");
}
return code + this.tab + '}';
};
return Switch;
}();
exports.If = If = function() {
__extends(If, Base);
function If(condition, body, options) {
this.body = body;
if (options == null) {
options = {};
}
this.condition = options.invert ? condition.invert() : condition;
this.elseBody = null;
this.isChain = false;
this.soak = options.soak;
}
If.prototype.children = ['condition', 'body', 'elseBody'];
If.prototype.bodyNode = function() {
var _ref;
return (_ref = this.body) != null ? _ref.unwrap() : void 0;
};
If.prototype.elseBodyNode = function() {
var _ref;
return (_ref = this.elseBody) != null ? _ref.unwrap() : void 0;
};
2010-10-10 23:31:54 -04:00
If.prototype.addElse = function(elseBody) {
if (this.isChain) {
2010-10-10 23:31:54 -04:00
this.elseBodyNode().addElse(elseBody);
} else {
this.isChain = elseBody instanceof If;
this.elseBody = this.ensureExpressions(elseBody);
}
return this;
};
If.prototype.isStatement = function(o) {
var _ref;
return (o != null ? o.level : void 0) === LEVEL_TOP || this.bodyNode().isStatement(o) || ((_ref = this.elseBodyNode()) != null ? _ref.isStatement(o) : void 0);
};
If.prototype.compileNode = function(o) {
if (this.isStatement(o)) {
return this.compileStatement(o);
} else {
return this.compileExpression(o);
}
};
If.prototype.makeReturn = function() {
this.body && (this.body = new Expressions([this.body.makeReturn()]));
this.elseBody && (this.elseBody = new Expressions([this.elseBody.makeReturn()]));
return this;
};
If.prototype.ensureExpressions = function(node) {
if (node instanceof Expressions) {
return node;
} else {
return new Expressions([node]);
}
};
If.prototype.compileStatement = function(o) {
var body, child, cond, ifPart;
child = del(o, 'chainChild');
2010-10-23 11:10:28 -04:00
cond = this.condition.compile(o, LEVEL_PAREN);
2010-11-08 23:20:11 -05:00
o.indent += TAB;
body = this.ensureExpressions(this.body).compile(o);
if (body) {
body = "\n" + body + "\n" + this.tab;
}
ifPart = "if (" + cond + ") {" + body + "}";
2010-10-10 23:31:54 -04:00
if (!child) {
ifPart = this.tab + ifPart;
}
2010-10-07 07:05:22 -04:00
if (!this.elseBody) {
return ifPart;
}
return ifPart + ' else ' + (this.isChain ? (o.indent = this.tab, o.chainChild = true, this.elseBody.unwrap().compile(o, LEVEL_TOP)) : "{\n" + (this.elseBody.compile(o, LEVEL_TOP)) + "\n" + this.tab + "}");
};
If.prototype.compileExpression = function(o) {
var alt, body, code, cond;
cond = this.condition.compile(o, LEVEL_COND);
body = this.bodyNode().compile(o, LEVEL_LIST);
alt = this.elseBodyNode() ? this.elseBodyNode().compile(o, LEVEL_LIST) : 'void 0';
code = "" + cond + " ? " + body + " : " + alt;
if (o.level >= LEVEL_COND) {
return "(" + code + ")";
} else {
return code;
}
};
If.prototype.unfoldSoak = function() {
2010-10-25 09:31:52 -04:00
return this.soak && this;
};
return If;
}();
Push = {
2010-10-24 19:55:42 -04:00
wrap: function(name, exps) {
if (exps.isEmpty() || last(exps.expressions).containsPureStatement()) {
return exps;
}
2010-11-20 20:09:36 -05:00
return exps.push(new Call(new Value(new Literal(name), [new Access(new Literal('push'))]), [exps.pop()]));
}
2010-10-06 20:25:00 -04:00
};
Closure = {
2010-10-19 10:53:38 -04:00
wrap: function(expressions, statement, noReturn) {
var args, call, func, mentionsArgs, meth;
if (expressions.containsPureStatement()) {
return expressions;
}
2010-11-21 19:59:22 -05:00
func = new Code([], Expressions.wrap([expressions]));
args = [];
if ((mentionsArgs = expressions.contains(this.literalArgs)) || (expressions.contains(this.literalThis))) {
meth = new Literal(mentionsArgs ? 'apply' : 'call');
2010-10-06 23:41:09 -04:00
args = [new Literal('this')];
if (mentionsArgs) {
2010-10-06 23:41:09 -04:00
args.push(new Literal('arguments'));
}
2010-11-20 20:09:36 -05:00
func = new Value(func, [new Access(meth)]);
}
func.noReturn = noReturn;
call = new Call(func, args);
if (statement) {
return Expressions.wrap([call]);
} else {
return call;
}
2010-10-06 20:25:00 -04:00
},
literalArgs: function(node) {
return node instanceof Literal && node.value === 'arguments';
2010-10-06 20:25:00 -04:00
},
literalThis: function(node) {
return node instanceof Literal && node.value === 'this' || node instanceof Code && node.bound;
}
2010-10-06 20:25:00 -04:00
};
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;
};
UTILITIES = {
2010-11-20 19:17:09 -05:00
"extends": 'function(child, parent) {\n for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; }\n function ctor() { this.constructor = child; }\n ctor.prototype = parent.prototype;\n child.prototype = new ctor;\n child.__super__ = parent.prototype;\n return child;\n}',
2010-11-13 18:08:02 -05:00
bind: 'function(fn, me){ return function(){ return fn.apply(me, arguments); }; }',
indexOf: 'Array.prototype.indexOf || function(item) {\n for (var i = 0, l = this.length; i < l; i++) {\n if (this[i] === item) return i;\n }\n return -1;\n}',
hasProp: 'Object.prototype.hasOwnProperty',
slice: 'Array.prototype.slice'
};
2010-11-13 21:08:42 -05:00
LEVEL_TOP = 1;
LEVEL_PAREN = 2;
LEVEL_LIST = 3;
LEVEL_COND = 4;
LEVEL_OP = 5;
LEVEL_ACCESS = 6;
TAB = ' ';
TRAILING_WHITESPACE = /[ \t]+$/gm;
IDENTIFIER = /^[$A-Za-z_][$\w]*$/;
SIMPLENUM = /^[+-]?\d+$/;
IS_STRING = /^['"]/;
utility = function(name) {
var ref;
ref = "__" + name;
Scope.root.assign(ref, UTILITIES[name]);
return ref;
};
multident = function(code, tab) {
return code.replace(/\n/g, '$&' + tab);
};
}).call(this);