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

1862 lines
66 KiB
JavaScript
Raw Normal View History

2010-07-25 03:15:12 -04:00
(function() {
var Accessor, 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, NO, NUMBER, Obj, Op, Param, Parens, Push, Return, SIMPLENUM, Scope, Splat, Switch, TAB, THIS, TRAILING_WHITESPACE, Throw, Try, UTILITIES, Value, While, YES, _ref, compact, del, ends, extend, flatten, last, merge, multident, starts, utility;
var __extends = function(child, parent) {
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;
if (typeof parent.extended === "function") parent.extended(child);
child.__super__ = parent.prototype;
}, __indexOf = Array.prototype.indexOf || function(item) {
for (var i = 0, l = this.length; i < l; i++) if (this[i] === item) return i;
return -1;
};
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;
};
exports.Base = (function() {
Base = (function() {
function Base() {
2010-10-04 23:21:16 -04:00
this.tags = {};
return this;
};
return Base;
2010-10-04 23:21:16 -04:00
})();
Base.prototype.compile = function(o, lvl) {
var node;
o = o ? extend({}, o) : {};
if (lvl != null) {
o.level = lvl;
}
node = this.unfoldSoak(o) || this;
node.tab = o.indent;
2010-10-23 11:10:28 -04:00
return o.level === LEVEL_TOP || node.isPureStatement() || !node.isStatement(o) ? node.compileNode(o) : 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 = o.scope;
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);
2010-10-24 01:05:31 -04:00
return level ? [sub.compile(o, level), ref.value] : [sub, ref];
}
};
2010-10-13 07:29:22 -04:00
Base.prototype.compileLoopReference = function(o, name) {
var src, tmp;
2010-10-23 11:10:28 -04:00
src = tmp = this.compile(o, LEVEL_LIST);
if (!(NUMBER.test(src) || IDENTIFIER.test(src) && o.scope.check(src, {
2010-10-13 07:29:22 -04:00
immediate: true
}))) {
2010-10-13 07:29:22 -04:00
src = "" + (tmp = o.scope.freeVariable(name)) + " = " + src;
}
return [src, tmp];
};
Base.prototype.idt = function(tabs) {
return (this.tab || '') + Array((tabs || 0) + 1).join(TAB);
};
Base.prototype.makeReturn = function() {
return new Return(this);
};
2010-10-23 13:53:10 -04:00
Base.prototype.contains = function(block, arg) {
var contains;
contains = false;
2010-10-23 13:53:10 -04:00
this.traverseChildren(false, function(node, arg) {
var rearg;
return (rearg = block(node, arg)) === true ? !(contains = true) : arg != null ? rearg : undefined;
}, arg);
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() {
2010-10-23 13:53:10 -04:00
return this.isPureStatement() || this.contains(function(node, func) {
return func(node) || (node instanceof While || node instanceof For ? function(node) {
return node instanceof Return;
} : func);
}, function(node) {
2010-10-06 20:25:00 -04:00
return node.isPureStatement();
});
};
Base.prototype.toString = function(idt, override) {
var _i, _len, _ref2, _result, child, children, klass;
idt || (idt = '');
children = ((function() {
_ref2 = this.collectChildren();
_result = [];
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
child = _ref2[_i];
_result.push(child.toString(idt + TAB));
}
return _result;
}).call(this)).join('');
klass = override || this.constructor.name + (this.soakNode ? '?' : '');
2010-09-29 16:29:20 -04:00
return '\n' + idt + klass + children;
};
Base.prototype.eachChild = function(func) {
2010-10-20 06:53:41 -04:00
var _i, _j, _len, _len2, _ref2, _ref3, attr, child;
2010-10-07 07:05:22 -04:00
if (!this.children) {
return this;
2010-05-19 15:37:42 -04:00
}
_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, _len2 = _ref3.length; _j < _len2; _j++) {
child = _ref3[_j];
if (func(child) === false) {
return this;
}
}
}
}
2010-10-20 06:53:41 -04:00
return this;
};
Base.prototype.collectChildren = function() {
var nodes;
nodes = [];
this.eachChild(function(node) {
return nodes.push(node);
});
return nodes;
};
2010-10-23 13:53:10 -04:00
Base.prototype.traverseChildren = function(crossScope, func, arg) {
return this.eachChild(function(child) {
2010-10-23 13:53:10 -04:00
if ((arg = func(child, arg)) === false) {
return false;
}
2010-10-23 13:53:10 -04:00
return child.traverseChildren(crossScope, func, arg);
});
};
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 = (function() {
2010-10-04 23:21:16 -04:00
Expressions = (function() {
function Expressions(nodes) {
2010-10-04 23:21:16 -04:00
Expressions.__super__.constructor.call(this);
this.expressions = compact(flatten(nodes || []));
return this;
};
return Expressions;
2010-10-04 23:21:16 -04:00
})();
__extends(Expressions, Base);
Expressions.prototype.children = ['expressions'];
Expressions.prototype.isStatement = YES;
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() {
return this.expressions.length === 1 ? this.expressions[0] : this;
};
Expressions.prototype.isEmpty = function() {
return !this.expressions.length;
};
Expressions.prototype.makeReturn = function() {
var _ref2, end, idx;
_ref2 = this.expressions;
for (idx = _ref2.length - 1; idx >= 0; idx--) {
end = _ref2[idx];
if (!(end instanceof Comment)) {
this.expressions[idx] = end.makeReturn();
break;
}
}
return this;
};
2010-10-24 01:05:31 -04:00
Expressions.prototype.compile = function(o, level) {
o || (o = {});
2010-10-24 01:05:31 -04:00
return o.scope ? Expressions.__super__.compile.call(this, o, level) : this.compileRoot(o);
};
Expressions.prototype.compileNode = function(o) {
var _i, _len, _ref2, _result, node;
this.tab = o.indent;
return ((function() {
_ref2 = this.expressions;
_result = [];
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
node = _ref2[_i];
_result.push(this.compileExpression(node, o));
}
return _result;
}).call(this)).join('\n');
};
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, '');
return o.bare ? code : "(function() {\n" + code + "\n}).call(this);\n";
};
Expressions.prototype.compileWithDeclarations = function(o) {
var code, scope;
code = this.compileNode(o);
scope = o.scope;
if (scope.hasAssignments(this)) {
code = "" + this.tab + "var " + (multident(scope.compiledAssignments(), this.tab)) + ";\n" + code;
}
if (!o.globals && o.scope.hasDeclarations(this)) {
code = "" + this.tab + "var " + (scope.compiledDeclarations()) + ";\n" + code;
}
return code;
};
Expressions.prototype.compileExpression = function(node, o) {
var code;
2010-10-24 19:00:12 -04:00
node = node.unwrapAll();
node = node.unfoldSoak(o) || node;
node.tags.front = true;
2010-10-23 11:10:28 -04:00
o.level = LEVEL_TOP;
code = node.compile(o);
return node.isStatement(o) ? code : this.tab + code + ';';
};
Expressions.wrap = function(nodes) {
if (nodes.length === 1 && nodes[0] instanceof Expressions) {
return nodes[0];
}
return new Expressions(nodes);
};
return Expressions;
}).call(this);
exports.Literal = (function() {
Literal = (function() {
function Literal(_arg) {
2010-10-04 23:21:16 -04:00
this.value = _arg;
Literal.__super__.constructor.call(this);
2010-10-04 23:21:16 -04:00
return this;
};
return Literal;
2010-10-04 23:21:16 -04:00
})();
__extends(Literal, Base);
Literal.prototype.makeReturn = function() {
return this.isStatement() ? this : Literal.__super__.makeReturn.call(this);
};
Literal.prototype.isPureStatement = function() {
var _ref2;
return (_ref2 = this.value) === 'break' || _ref2 === 'continue' || _ref2 === '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() {
return this.value.reserved ? "\"" + this.value + "\"" : this.value;
};
Literal.prototype.toString = function() {
2010-09-26 15:47:52 -04:00
return ' "' + this.value + '"';
};
return Literal;
})();
exports.Return = (function() {
Return = (function() {
function Return(_arg) {
2010-10-04 23:21:16 -04:00
this.expression = _arg;
Return.__super__.constructor.call(this);
2010-10-04 23:21:16 -04:00
return this;
};
return Return;
2010-10-04 23:21:16 -04:00
})();
__extends(Return, Base);
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) {
2010-10-06 22:24:52 -04:00
var _ref2, expr;
expr = (_ref2 = this.expression) != null ? _ref2.makeReturn() : undefined;
2010-10-24 01:05:31 -04:00
return expr && !(expr instanceof Return) ? expr.compile(o, level) : 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 = (function() {
Value = (function() {
2010-10-24 19:13:58 -04:00
function Value(base, props, tag) {
var _obj;
if (!props && base instanceof Value) {
return base;
2010-10-04 23:21:16 -04:00
}
2010-10-24 19:13:58 -04:00
this.base = base;
this.properties = props || [];
this.tags = tag ? (_obj = {}, _obj[tag] = true, _obj) : {};
2010-10-04 23:21:16 -04:00
return this;
};
return Value;
2010-10-04 23:21:16 -04:00
})();
__extends(Value, Base);
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() {
return this.base instanceof Arr && !this.properties.length;
};
Value.prototype.isObject = function() {
return this.base instanceof Obj && !this.properties.length;
};
Value.prototype.isComplex = function() {
2010-09-28 08:52:51 -04:00
return this.base.isComplex() || this.hasProperties();
};
Value.prototype.isAtomic = function() {
var _i, _len, _ref2, node;
_ref2 = this.properties.concat(this.base);
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
node = _ref2[_i];
if (node.soakNode || node instanceof Call) {
return false;
}
}
return true;
};
Value.prototype.isAssignable = function() {
return this.hasProperties() || this.base.isAssignable();
};
Value.prototype.assigns = function(name) {
return !this.properties.length && this.base.assigns(name);
};
Value.prototype.makeReturn = function() {
return this.properties.length ? Value.__super__.makeReturn.call(this) : this.base.makeReturn();
};
Value.prototype.unwrap = function() {
return this.properties.length ? this : this.base;
};
Value.prototype.isStatement = function(o) {
return !this.properties.length && this.base.isStatement(o);
};
Value.prototype.isSimpleNumber = function() {
return this.base instanceof Literal && SIMPLENUM.test(this.base.value);
2010-05-31 22:56:51 -04:00
};
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() : undefined)) {
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 _i, _len, code, prop, props;
this.base.tags.front = this.tags.front;
props = this.properties;
2010-10-23 11:10:28 -04:00
code = this.base.compile(o, props.length ? LEVEL_ACCESS : null);
if (props[0] instanceof Accessor && 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 _len, _ref2, fst, i, ifn, prop, ref, snd;
if (ifn = this.base.unfoldSoak(o)) {
Array.prototype.push.apply(ifn.body.properties, this.properties);
return ifn;
}
_ref2 = this.properties;
for (i = 0, _len = _ref2.length; i < _len; i++) {
prop = _ref2[i];
2010-09-12 14:44:03 -04:00
if (prop.soakNode) {
prop.soakNode = 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;
2010-10-24 19:13:58 -04:00
})();
exports.Comment = (function() {
Comment = (function() {
function Comment(_arg) {
2010-10-04 23:21:16 -04:00
this.comment = _arg;
Comment.__super__.constructor.call(this);
2010-10-04 23:21:16 -04:00
return this;
};
return Comment;
2010-10-04 23:21:16 -04:00
})();
__extends(Comment, Base);
Comment.prototype.isPureStatement = YES;
Comment.prototype.makeReturn = THIS;
Comment.prototype.compileNode = function(o) {
return this.tab + '/*' + multident(this.comment, this.tab) + '*/';
};
return Comment;
})();
exports.Call = (function() {
Call = (function() {
function Call(variable, _arg, _arg2) {
this.soakNode = _arg2;
2010-10-04 23:21:16 -04:00
this.args = _arg;
Call.__super__.constructor.call(this);
2010-10-04 23:21:16 -04:00
this.isNew = false;
this.isSuper = variable === 'super';
this.variable = this.isSuper ? null : variable;
this.args || (this.args = []);
return this;
};
return Call;
2010-10-04 23:21:16 -04:00
})();
__extends(Call, Base);
Call.prototype.children = ['variable', 'args'];
Call.prototype.compileSplatArguments = function(o) {
return Splat.compileSplattedArray(this.args, o);
};
Call.prototype.newInstance = function() {
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.');
}
return method.klass ? "" + method.klass + ".__super__." + name : "" + name + ".__super__.constructor";
};
Call.prototype.unfoldSoak = function(o) {
var _i, _len, _ref2, _ref3, call, ifn, left, list, rite;
if (this.soakNode) {
if (this.variable) {
if (ifn = If.unfoldSoak(o, this, 'variable')) {
return ifn;
}
2010-10-24 19:13:58 -04:00
_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;
}
2010-05-02 23:20:51 -04:00
}
ifn = If.unfoldSoak(o, call, 'variable');
}
return ifn;
};
Call.prototype.compileNode = function(o) {
var _i, _j, _len, _len2, _ref2, _ref3, _ref4, _result, arg, args;
if ((_ref2 = this.variable) != null) {
_ref2.tags.front = this.tags.front;
}
_ref3 = this.args;
for (_i = 0, _len = _ref3.length; _i < _len; _i++) {
arg = _ref3[_i];
if (arg instanceof Splat) {
return this.compileSplat(o);
}
}
args = ((function() {
_ref4 = this.args;
_result = [];
for (_j = 0, _len2 = _ref4.length; _j < _len2; _j++) {
arg = _ref4[_j];
2010-10-23 11:10:28 -04:00
_result.push(arg.compile(o, LEVEL_LIST));
}
return _result;
}).call(this)).join(', ');
2010-10-23 11:10:28 -04:00
return this.isSuper ? this.compileSuper(args, o) : (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) {
2010-10-12 08:48:25 -04:00
var base, fun, idt, name, ref, splatargs;
splatargs = this.compileSplatArguments(o);
if (this.isSuper) {
return "" + (this.superReference(o)) + ".apply(this, " + splatargs + ")";
}
2010-10-07 07:05:22 -04:00
if (!this.isNew) {
2010-10-24 19:13:58 -04:00
base = new Value(this.variable);
if ((name = base.properties.pop()) && base.isComplex()) {
ref = o.scope.freeVariable('this');
2010-10-23 11:10:28 -04:00
fun = "(" + ref + " = " + (base.compile(o, LEVEL_LIST)) + ")" + (name.compile(o));
} else {
2010-10-23 11:10:28 -04:00
fun = ref = base.compile(o, LEVEL_ACCESS);
if (name) {
fun += name.compile(o);
}
}
return "" + fun + ".apply(" + ref + ", " + splatargs + ")";
}
2010-10-12 08:48:25 -04:00
idt = this.idt(1);
2010-10-23 11:10:28 -04:00
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() {})";
};
return Call;
})();
exports.Extends = (function() {
Extends = (function() {
function Extends(_arg, _arg2) {
2010-10-04 23:21:16 -04:00
this.parent = _arg2;
this.child = _arg;
Extends.__super__.constructor.call(this);
2010-10-04 23:21:16 -04:00
return this;
};
return Extends;
2010-10-04 23:21:16 -04:00
})();
__extends(Extends, Base);
Extends.prototype.children = ['child', 'parent'];
Extends.prototype.compile = function(o) {
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;
})();
exports.Accessor = (function() {
Accessor = (function() {
function Accessor(_arg, tag) {
2010-10-04 23:21:16 -04:00
this.name = _arg;
Accessor.__super__.constructor.call(this);
this.proto = tag === 'prototype' ? '.prototype' : '';
2010-10-04 23:21:16 -04:00
this.soakNode = tag === 'soak';
return this;
};
return Accessor;
2010-10-04 23:21:16 -04:00
})();
__extends(Accessor, Base);
Accessor.prototype.children = ['name'];
Accessor.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);
};
Accessor.prototype.isComplex = NO;
return Accessor;
})();
exports.Index = (function() {
Index = (function() {
function Index(_arg) {
2010-10-04 23:21:16 -04:00
this.index = _arg;
Index.__super__.constructor.call(this);
2010-10-04 23:21:16 -04:00
return this;
};
return Index;
2010-10-04 23:21:16 -04:00
})();
__extends(Index, Base);
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;
})();
exports.Obj = (function() {
Obj = (function() {
function Obj(props) {
Obj.__super__.constructor.call(this);
this.objects = this.properties = props || [];
2010-10-04 23:21:16 -04:00
return this;
};
return Obj;
2010-10-04 23:21:16 -04:00
})();
__extends(Obj, Base);
Obj.prototype.children = ['properties'];
Obj.prototype.compileNode = function(o) {
2010-10-24 01:05:31 -04:00
var _i, _len, _len2, _ref2, _ref3, _result, i, indent, join, lastNoncom, nonComments, obj, prop, props;
_ref2 = this.properties;
for (i = 0, _len = _ref2.length; i < _len; i++) {
prop = _ref2[i];
if ((prop.variable || prop).base instanceof Parens) {
return this.compileDynamic(o, i);
}
}
2010-02-09 20:53:25 -05:00
o.indent = this.idt(1);
nonComments = ((function() {
2010-10-24 01:05:31 -04:00
_ref3 = this.properties;
_result = [];
2010-10-24 01:05:31 -04:00
for (_i = 0, _len2 = _ref3.length; _i < _len2; _i++) {
prop = _ref3[_i];
if (!(prop instanceof Comment)) {
_result.push(prop);
}
}
return _result;
}).call(this));
2010-09-28 08:52:51 -04:00
lastNoncom = last(nonComments);
2010-02-09 20:53:25 -05:00
props = (function() {
2010-10-24 01:05:31 -04:00
_ref3 = this.properties;
2010-10-19 19:58:59 -04:00
_result = [];
2010-10-24 01:05:31 -04:00
for (i = 0, _len2 = _ref3.length; i < _len2; i++) {
prop = _ref3[i];
join = i === this.properties.length - 1 ? '' : prop === lastNoncom || prop instanceof Comment ? '\n' : ',\n';
indent = prop instanceof Comment ? '' : this.idt(1);
if (prop instanceof Value && prop.tags["this"]) {
prop = new Assign(prop.properties[0].name, prop, 'object');
} else if (!(prop instanceof Assign) && !(prop instanceof Comment)) {
prop = new Assign(prop, prop, 'object');
}
_result.push(indent + prop.compile(o) + join);
2010-02-09 20:53:25 -05:00
}
2010-10-19 19:58:59 -04:00
return _result;
2010-02-09 20:53:25 -05:00
}).call(this);
props = props.join('');
obj = "{" + (props ? '\n' + props + '\n' + this.idt() : '') + "}";
return this.tags.front ? "(" + obj + ")" : obj;
};
Obj.prototype.compileDynamic = function(o, idx) {
2010-10-24 01:05:31 -04:00
var _len, _ref2, _ref3, code, i, key, obj, prop, ref, sub;
obj = o.scope.freeVariable('obj');
code = "" + obj + " = " + (new Obj(this.properties.slice(0, idx)).compile(o)) + ", ";
2010-10-24 01:05:31 -04:00
_ref2 = this.properties.slice(idx);
for (i = 0, _len = _ref2.length; i < _len; i++) {
prop = _ref2[i];
if (prop instanceof Assign) {
key = prop.variable.compile(o, LEVEL_PAREN);
code += "" + obj + "[" + key + "] = " + (prop.value.compile(o, LEVEL_LIST)) + ", ";
continue;
}
if (prop instanceof Comment) {
code += prop.compile(o) + ' ';
continue;
}
_ref3 = prop.base.cache(o, LEVEL_LIST, ref), sub = _ref3[0], ref = _ref3[1];
code += "" + obj + "[" + sub + "] = " + ref + ", ";
}
code += obj;
return o.level <= LEVEL_PAREN ? code : "(" + code + ")";
};
Obj.prototype.assigns = function(name) {
var _i, _len, _ref2, prop;
_ref2 = this.properties;
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
prop = _ref2[_i];
if (prop.assigns(name)) {
return true;
}
}
return false;
};
return Obj;
})();
exports.Arr = (function() {
Arr = (function() {
function Arr(objs) {
Arr.__super__.constructor.call(this);
2010-10-20 06:53:41 -04:00
this.objects = objs || [];
2010-10-04 23:21:16 -04:00
return this;
};
return Arr;
2010-10-04 23:21:16 -04:00
})();
__extends(Arr, Base);
Arr.prototype.children = ['objects'];
Arr.prototype.compileSplatLiteral = function(o) {
return Splat.compileSplattedArray(this.objects, o);
};
Arr.prototype.compileNode = function(o) {
var _i, _len, _len2, _ref2, _ref3, code, i, obj, objects;
2010-03-07 19:11:03 -05:00
o.indent = this.idt(1);
_ref2 = this.objects;
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
2010-10-10 20:40:41 -04:00
obj = _ref2[_i];
if (obj instanceof Splat) {
return this.compileSplatLiteral(o);
2010-03-07 19:11:03 -05:00
}
}
2010-10-10 20:40:41 -04:00
objects = [];
_ref3 = this.objects;
for (i = 0, _len2 = _ref3.length; i < _len2; i++) {
2010-10-10 20:40:41 -04:00
obj = _ref3[i];
2010-10-23 11:10:28 -04:00
code = obj.compile(o, LEVEL_LIST);
objects.push((obj instanceof Comment ? "\n" + code + "\n" + o.indent : i === this.objects.length - 1 ? code : code + ', '));
2010-10-10 20:40:41 -04:00
}
2010-03-07 19:11:03 -05:00
objects = objects.join('');
return 0 < objects.indexOf('\n') ? "[\n" + o.indent + objects + "\n" + this.tab + "]" : "[" + objects + "]";
2010-03-07 19:11:03 -05:00
};
Arr.prototype.assigns = function(name) {
var _i, _len, _ref2, obj;
_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;
})();
exports.Class = (function() {
Class = (function() {
function Class(_arg, _arg2, props) {
this.parent = _arg2;
this.variable = _arg;
Class.__super__.constructor.call(this);
2010-10-20 06:53:41 -04:00
this.properties = props || [];
2010-10-04 23:21:16 -04:00
this.returns = false;
return this;
};
return Class;
2010-10-04 23:21:16 -04:00
})();
__extends(Class, Base);
Class.prototype.children = ['variable', 'parent', 'properties'];
Class.prototype.isStatement = YES;
Class.prototype.makeReturn = function() {
this.returns = true;
return this;
};
Class.prototype.compileNode = function(o) {
var _i, _len, _ref2, _ref3, access, applied, apply, className, constScope, construct, constructor, extension, func, me, pname, prop, props, pvar, ref, val, variable;
variable = this.variable || new Literal(o.scope.freeVariable('ctor'));
extension = this.parent && new Extends(variable, this.parent);
props = new Expressions;
me = null;
2010-10-06 20:25:00 -04:00
className = variable.compile(o);
constScope = null;
if (this.parent) {
2010-10-06 23:41:09 -04:00
applied = new Value(this.parent, [new Accessor(new Literal('apply'))]);
constructor = new Code([], new Expressions([new Call(applied, [new Literal('this'), new Literal('arguments')])]));
} else {
constructor = new Code([], new Expressions([new Return(new Literal('this'))]));
}
_ref2 = this.properties;
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
prop = _ref2[_i];
pvar = prop.variable, func = prop.value;
if (pvar && pvar.base.value === 'constructor') {
if (!(func instanceof Code)) {
_ref3 = func.cache(o), func = _ref3[0], ref = _ref3[1];
if (func !== ref) {
props.push(func);
}
2010-10-06 23:41:09 -04:00
apply = new Call(new Value(ref, [new Accessor(new Literal('apply'))]), [new Literal('this'), new Literal('arguments')]);
func = new Code([], new Expressions([apply]));
}
if (func.bound) {
2010-10-20 06:53:41 -04:00
throw SyntaxError('cannot define a constructor as a bound function.');
}
func.name = className;
2010-10-06 23:41:09 -04:00
func.body.push(new Return(new Literal('this')));
variable = new Value(variable);
2010-10-10 20:40:41 -04:00
variable.namespaced = 0 < className.indexOf('.');
constructor = func;
if (last(props.expressions) instanceof Comment) {
constructor.comment = props.expressions.pop();
}
continue;
}
if (func instanceof Code && func.bound) {
if (prop.context === 'this') {
func.context = className;
} else {
func.bound = false;
constScope || (constScope = new Scope(o.scope, constructor.body, constructor));
me || (me = constScope.freeVariable('this'));
pname = pvar.compile(o);
if (constructor.body.isEmpty()) {
2010-10-06 23:41:09 -04:00
constructor.body.push(new Return(new Literal('this')));
}
2010-10-06 23:41:09 -04:00
constructor.body.unshift(new Literal("this." + pname + " = function(){ return " + className + ".prototype." + pname + ".apply(" + me + ", arguments); }"));
}
}
if (pvar) {
2010-10-24 19:13:58 -04:00
access = prop.context === 'this' ? pvar.properties[0] : new Accessor(pvar, 'prototype');
val = new Value(variable, [access]);
prop = new Assign(val, func);
}
props.push(prop);
}
2010-10-20 06:53:41 -04:00
constructor.className = className.match(/[$\w]+$/);
if (me) {
2010-10-06 23:41:09 -04:00
constructor.body.unshift(new Literal("" + me + " = this"));
}
o.sharedScope = constScope;
construct = this.tab + new Assign(variable, constructor).compile(o) + ';';
if (extension) {
construct += '\n' + this.tab + extension.compile(o) + ';';
}
if (!props.isEmpty()) {
construct += '\n' + props.compile(o);
}
if (this.returns) {
construct += '\n' + new Return(variable).compile(o);
}
return construct;
};
return Class;
})();
exports.Assign = (function() {
Assign = (function() {
function Assign(_arg, _arg2, _arg3) {
2010-10-04 23:21:16 -04:00
this.context = _arg3;
this.value = _arg2;
this.variable = _arg;
Assign.__super__.constructor.call(this);
2010-10-04 23:21:16 -04:00
return this;
};
return Assign;
2010-10-04 23:21:16 -04:00
})();
__extends(Assign, Base);
Assign.prototype.METHOD_DEF = /^(?:(\S+)\.prototype\.)?([$A-Za-z_][$\w]*)$/;
Assign.prototype.CONDITIONAL = ['||=', '&&=', '?='];
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 If.unfoldSoak(o, this, 'variable');
};
Assign.prototype.compileNode = function(o) {
var _ref2, isValue, match, name, val;
if (isValue = this.variable instanceof Value) {
if (this.variable.isArray() || this.variable.isObject()) {
return this.compilePatternMatch(o);
}
if (_ref2 = this.context, __indexOf.call(this.CONDITIONAL, _ref2) >= 0) {
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];
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 (!(isValue && (this.variable.hasProperties() || this.variable.namespaced))) {
2010-02-09 20:53:25 -05:00
o.scope.find(name);
}
val = name + (" " + (this.context || '=') + " ") + val;
2010-10-23 11:10:28 -04:00
return o.level <= LEVEL_LIST ? val : "(" + val + ")";
};
Assign.prototype.compilePatternMatch = function(o) {
var _len, _ref2, _ref3, _ref4, _ref5, _ref6, _ref7, acc, assigns, code, i, idx, isObject, obj, objects, olength, ref, splat, top, val, valVar, value;
2010-10-23 11:10:28 -04:00
top = o.level === LEVEL_TOP;
value = this.value;
objects = this.variable.base.objects;
2010-10-02 07:53:29 -04:00
if (!(olength = objects.length)) {
return value.compile(o);
}
2010-10-02 07:53:29 -04:00
isObject = this.variable.isObject();
2010-10-20 20:16:17 -04:00
if (top && olength === 1 && !((obj = objects[0]) instanceof Splat)) {
if (obj instanceof Assign) {
_ref2 = obj, (_ref3 = _ref2.variable, idx = _ref3.base, _ref3), obj = _ref2.value;
} else {
if (obj.base instanceof Parens) {
2010-10-24 19:13:58 -04:00
_ref4 = new Value(obj.unwrapAll()).cacheReference(o), obj = _ref4[0], idx = _ref4[1];
} else {
idx = isObject ? obj.tags["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);
value.properties.push(new (acc ? Accessor : Index)(idx));
return new Assign(obj, value).compile(o);
}
2010-10-23 11:10:28 -04:00
valVar = value.compile(o, LEVEL_LIST);
assigns = [];
2010-03-22 02:02:04 -04:00
splat = false;
if (!IDENTIFIER.test(valVar) || this.variable.assigns(valVar)) {
assigns.push("" + (ref = o.scope.freeVariable('ref')) + " = " + valVar);
valVar = 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) {
_ref5 = obj, (_ref6 = _ref5.variable, idx = _ref6.base, _ref6), obj = _ref5.value;
} else {
if (obj.base instanceof Parens) {
2010-10-24 19:13:58 -04:00
_ref7 = new Value(obj.unwrapAll()).cacheReference(o), obj = _ref7[0], idx = _ref7[1];
} else {
idx = obj.tags["this"] ? obj.properties[0].name : obj;
}
}
2010-02-09 20:53:25 -05:00
}
if (!splat && obj instanceof Splat) {
2010-10-06 23:41:09 -04:00
val = new Literal(obj.compileValue(o, valVar, i, olength - i - 1));
2010-03-22 02:02:04 -04:00
splat = true;
} else {
2010-03-22 02:02:04 -04:00
if (typeof idx !== 'object') {
idx = new Literal(splat ? "" + valVar + ".length - " + (olength - idx) : idx);
acc = false;
} else {
acc = isObject && IDENTIFIER.test(idx.unwrap().value || 0);
2010-03-22 02:02:04 -04:00
}
val = new Value(new Literal(valVar), [new (acc ? Accessor : Index)(idx)]);
}
2010-10-23 11:10:28 -04:00
assigns.push(new Assign(obj, val).compile(o, LEVEL_LIST));
2010-02-09 20:53:25 -05:00
}
2010-10-07 07:05:22 -04:00
if (!top) {
assigns.push(valVar);
}
code = assigns.join(', ');
2010-10-23 11:10:28 -04:00
return o.level < LEVEL_LIST ? code : "(" + code + ")";
};
Assign.prototype.compileConditional = function(o) {
var _ref2, left, rite;
_ref2 = this.variable.cacheReference(o), left = _ref2[0], rite = _ref2[1];
return new Op(this.context.slice(0, -1), left, new Assign(rite, this.value)).compile(o);
};
return Assign;
})();
exports.Code = (function() {
Code = (function() {
function Code(_arg, _arg2, tag) {
2010-10-04 23:21:16 -04:00
this.body = _arg2;
this.params = _arg;
Code.__super__.constructor.call(this);
2010-10-04 23:21:16 -04:00
this.params || (this.params = []);
this.body || (this.body = new Expressions);
this.bound = tag === 'boundfunc';
if (this.bound) {
this.context = 'this';
}
return this;
};
return Code;
2010-10-04 23:21:16 -04:00
})();
__extends(Code, Base);
Code.prototype.children = ['params', 'body'];
Code.prototype.compileNode = function(o) {
var _i, _len, _len2, _ref2, _ref3, _result, close, code, comm, empty, func, i, idt, open, param, params, scope, sharedScope, splat, value;
sharedScope = del(o, 'sharedScope');
o.scope = scope = sharedScope || new Scope(o.scope, this.body, this);
2010-07-25 03:15:12 -04:00
o.indent = this.idt(1);
empty = this.body.expressions.length === 0;
2010-10-13 15:09:56 -04:00
delete o.bare;
delete o.globals;
splat = undefined;
params = [];
_ref2 = this.params;
for (i = 0, _len = _ref2.length; i < _len; i++) {
param = _ref2[i];
2010-07-28 07:34:28 -04:00
if (splat) {
2010-07-28 01:54:36 -04:00
if (param.attach) {
2010-10-06 23:41:09 -04:00
param.assign = new Assign(new Value(new Literal('this'), [new Accessor(param.value)]));
2010-07-28 01:54:36 -04:00
this.body.expressions.splice(splat.index + 1, 0, param.assign);
}
splat.trailings.push(param);
} else {
2010-07-28 01:54:36 -04:00
if (param.attach) {
value = param.value;
_ref3 = [new Literal(scope.freeVariable('arg')), param.splat], param = _ref3[0], param.splat = _ref3[1];
2010-10-06 23:41:09 -04:00
this.body.unshift(new Assign(new Value(new Literal('this'), [new Accessor(value)]), param));
2010-07-28 01:54:36 -04:00
}
if (param.splat) {
splat = new Splat(param.value);
2010-07-28 01:54:36 -04:00
splat.index = i;
splat.trailings = [];
splat.arglength = this.params.length;
this.body.unshift(splat);
} else {
params.push(param);
}
}
}
scope.startLevel();
if (!(empty || this.noReturn)) {
this.body.makeReturn();
}
params = (function() {
2010-10-01 18:26:37 -04:00
_result = [];
for (_i = 0, _len2 = params.length; _i < _len2; _i++) {
param = params[_i];
scope.parameter(param = param.compile(o));
_result.push(param);
}
return _result;
})();
comm = this.comment ? this.comment.compile(o) + '\n' : '';
2010-10-04 23:21:16 -04:00
if (this.className) {
o.indent = this.idt(2);
}
idt = this.idt(1);
code = this.body.expressions.length ? "\n" + (this.body.compileWithDeclarations(o)) + "\n" : '';
if (this.className) {
open = "(function() {\n" + comm + idt + "function " + this.className + "(";
close = "" + (code && idt) + "};\n" + idt + "return " + this.className + ";\n" + this.tab + "})()";
} else {
open = "function(";
close = "" + (code && this.tab) + "}";
}
func = "" + open + (params.join(', ')) + ") {" + code + close;
scope.endLevel();
2010-07-25 03:15:12 -04:00
if (this.bound) {
return "" + (utility('bind')) + "(" + func + ", " + this.context + ")";
}
return this.tags.front ? "(" + func + ")" : func;
};
Code.prototype.traverseChildren = function(crossScope, func) {
return crossScope ? Code.__super__.traverseChildren.call(this, crossScope, func) : undefined;
};
Code.prototype["do"] = function() {
if (this.bound) {
this.bound = false;
return new Call(new Value(this, [new Accessor(new Literal('call'))]), [new Literal('this')].concat(this.params));
} else {
return new Call(this);
}
};
return Code;
})();
exports.Param = (function() {
Param = (function() {
function Param(_arg, _arg2, _arg3) {
2010-10-04 23:21:16 -04:00
this.splat = _arg3;
this.attach = _arg2;
this.name = _arg;
Param.__super__.constructor.call(this);
2010-10-06 23:41:09 -04:00
this.value = new Literal(this.name);
2010-10-04 23:21:16 -04:00
return this;
};
return Param;
2010-10-04 23:21:16 -04:00
})();
__extends(Param, Base);
Param.prototype.children = ['name'];
Param.prototype.compile = function(o) {
2010-10-23 11:10:28 -04:00
return this.value.compile(o, LEVEL_LIST);
2010-07-28 01:54:36 -04:00
};
Param.prototype.toString = function() {
var name;
name = this.name;
2010-09-26 15:47:52 -04:00
if (this.attach) {
name = '@' + name;
}
if (this.splat) {
name += '...';
}
2010-10-06 23:41:09 -04:00
return new Literal(name).toString();
2010-07-28 01:54:36 -04:00
};
return Param;
2010-07-28 01:54:36 -04:00
})();
exports.Splat = (function() {
Splat = (function() {
function Splat(name) {
Splat.__super__.constructor.call(this);
this.name = name.compile ? name : new Literal(name);
2010-10-04 23:21:16 -04:00
return this;
};
return Splat;
2010-10-04 23:21:16 -04:00
})();
__extends(Splat, Base);
Splat.prototype.children = ['name'];
Splat.prototype.isAssignable = YES;
Splat.prototype.assigns = function(name) {
return this.name.assigns(name);
};
Splat.prototype.compile = function(o) {
return this.index != null ? this.compileParam(o) : this.name.compile(o);
};
Splat.prototype.compileParam = function(o) {
var _len, _ref2, assign, end, idx, len, name, pos, trailing, variadic;
name = this.name.compile(o);
o.scope.find(name);
2010-07-25 03:15:12 -04:00
end = '';
if (this.trailings.length) {
len = o.scope.freeVariable('len');
o.scope.assign(len, 'arguments.length');
variadic = o.scope.freeVariable('result');
o.scope.assign(variadic, len + ' >= ' + this.arglength);
end = this.trailings.length ? ", " + len + " - " + this.trailings.length : undefined;
_ref2 = this.trailings;
for (idx = 0, _len = _ref2.length; idx < _len; idx++) {
trailing = _ref2[idx];
2010-07-28 01:54:36 -04:00
if (trailing.attach) {
assign = trailing.assign;
2010-10-06 23:41:09 -04:00
trailing = new Literal(o.scope.freeVariable('arg'));
2010-07-28 01:54:36 -04:00
assign.value = trailing;
}
2010-07-25 03:15:12 -04:00
pos = this.trailings.length - idx;
o.scope.assign(trailing.compile(o), "arguments[" + variadic + " ? " + len + " - " + pos + " : " + (this.index + idx) + "]");
2010-07-25 03:15:12 -04:00
}
}
return "" + name + " = " + (utility('slice')) + ".call(arguments, " + this.index + end + ")";
};
Splat.prototype.compileValue = function(o, name, index, trailings) {
var trail;
trail = trailings ? ", " + name + ".length - " + trailings : '';
return "" + (utility('slice')) + ".call(" + name + ", " + index + trail + ")";
};
Splat.compileSplattedArray = function(list, o) {
var _len, arg, args, code, end, i, prev;
args = [];
2010-09-28 08:52:51 -04:00
end = -1;
for (i = 0, _len = list.length; i < _len; i++) {
2010-10-01 18:26:37 -04:00
arg = list[i];
2010-10-23 11:10:28 -04:00
code = arg.compile(o, LEVEL_LIST);
2010-09-28 08:52:51 -04:00
prev = args[end];
if (!(arg instanceof Splat)) {
if (prev && starts(prev, '[') && ends(prev, ']')) {
args[end] = "" + (prev.slice(0, -1)) + ", " + code + "]";
continue;
2010-09-28 08:52:51 -04:00
}
if (prev && starts(prev, '.concat([') && ends(prev, '])')) {
args[end] = "" + (prev.slice(0, -2)) + ", " + code + "])";
continue;
}
code = "[" + code + "]";
}
args[++end] = i === 0 ? code : ".concat(" + code + ")";
}
return args.join('');
};
return Splat;
}).call(this);
exports.While = (function() {
While = (function() {
function While(condition, opts) {
While.__super__.constructor.call(this);
this.condition = (opts != null ? opts.invert : undefined) ? condition.invert() : condition;
this.guard = opts != null ? opts.guard : undefined;
2010-10-04 23:21:16 -04:00
return this;
};
return While;
2010-10-04 23:21:16 -04:00
})();
__extends(While, Base);
While.prototype.children = ['condition', 'guard', 'body'];
While.prototype.isStatement = YES;
While.prototype.addBody = function(body) {
this.body = body;
2010-02-09 22:05:17 -05:00
return this;
};
While.prototype.makeReturn = function() {
this.returns = true;
return this;
};
While.prototype.compileNode = function(o) {
var body, code, rvar, set;
2010-02-09 22:05:17 -05:00
o.indent = this.idt(1);
set = '';
body = this.body;
if (body.isEmpty()) {
body = '';
} else {
if (o.level > LEVEL_TOP || this.returns) {
rvar = o.scope.freeVariable('result');
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 = (function() {
Op = (function() {
function Op(op, first, second, flip) {
2010-10-20 20:16:17 -04:00
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
}
Op.__super__.constructor.call(this);
this.operator = this.CONVERSIONS[op] || op;
this.first = first;
this.second = second;
this.flip = !!flip;
2010-10-04 23:21:16 -04:00
return this;
};
return Op;
2010-10-04 23:21:16 -04:00
})();
__extends(Op, Base);
Op.prototype.CONVERSIONS = {
'==': '===',
'!=': '!==',
'of': 'in'
};
Op.prototype.INVERSIONS = {
'!==': '===',
'===': '!=='
};
Op.prototype.children = ['first', 'second'];
Op.prototype.isUnary = function() {
return !this.second;
};
Op.prototype.isChainable = function() {
var _ref2;
return (_ref2 = this.operator) === '<' || _ref2 === '>' || _ref2 === '>=' || _ref2 === '<=' || _ref2 === '===' || _ref2 === '!==';
};
Op.prototype.invert = function() {
2010-10-20 06:53:41 -04:00
var op;
if (op = this.INVERSIONS[this.operator]) {
this.operator = op;
2010-10-10 18:29:38 -04:00
return this;
} else return this.second ? new Parens(this).invert() : Op.__super__.invert.call(this);
};
Op.prototype.unfoldSoak = function(o) {
var _ref2;
return ((_ref2 = this.operator) === '++' || _ref2 === '--' || _ref2 === 'delete') && If.unfoldSoak(o, this, 'first');
};
Op.prototype.compileNode = function(o) {
if (this.isUnary()) {
return this.compileUnary(o);
}
if (this.isChainable() && this.first.isChainable()) {
return this.compileChain(o);
}
if (this.operator === '?') {
return this.compileExistence(o);
}
this.first.tags.front = this.tags.front;
2010-10-23 11:10:28 -04:00
return "" + (this.first.compile(o, LEVEL_OP)) + " " + this.operator + " " + (this.second.compile(o, LEVEL_OP));
};
Op.prototype.compileChain = function(o) {
var _ref2, code, fst, shared;
_ref2 = this.first.second.cache(o), this.first.second = _ref2[0], shared = _ref2[1];
2010-10-23 11:11:49 -04:00
fst = this.first.compile(o, LEVEL_OP);
if (fst.charAt(0) === '(') {
fst = fst.slice(1, -1);
}
2010-10-23 11:11:49 -04:00
code = "" + fst + " && " + (shared.compile(o)) + " " + this.operator + " " + (this.second.compile(o, LEVEL_OP));
return o.level < LEVEL_OP ? code : "(" + 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);
}
2010-10-23 11:10:28 -04:00
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 = (function() {
In = (function() {
function In(_arg, _arg2) {
2010-10-04 23:21:16 -04:00
this.array = _arg2;
this.object = _arg;
In.__super__.constructor.call(this);
2010-10-04 23:21:16 -04:00
return this;
};
return In;
2010-10-04 23:21:16 -04:00
})();
__extends(In, Base);
In.prototype.children = ['object', 'array'];
2010-10-20 20:16:17 -04:00
In.prototype.invert = function() {
this.negated = !this.negated;
return this;
};
In.prototype.compileNode = function(o) {
2010-10-20 20:16:17 -04:00
return this.array instanceof Value && this.array.isArray() ? this.compileOrTest(o) : this.compileLoopTest(o);
};
In.prototype.compileOrTest = function(o) {
2010-10-20 20:16:17 -04:00
var _len, _ref2, _ref3, _ref4, _result, cmp, cnj, i, item, ref, sub, tests;
2010-10-23 11:10:28 -04:00
_ref2 = this.object.cache(o, LEVEL_OP), sub = _ref2[0], ref = _ref2[1];
2010-10-20 20:16:17 -04:00
_ref3 = this.negated ? [' !== ', ' && '] : [' === ', ' || '], cmp = _ref3[0], cnj = _ref3[1];
tests = (function() {
_ref4 = this.array.base.objects;
_result = [];
for (i = 0, _len = _ref4.length; i < _len; i++) {
2010-10-20 20:16:17 -04:00
item = _ref4[i];
_result.push((i ? ref : sub) + cmp + item.compile(o));
}
return _result;
}).call(this);
2010-10-20 20:16:17 -04:00
tests = tests.join(cnj);
2010-10-23 11:10:28 -04:00
return o.level < LEVEL_OP ? tests : "(" + tests + ")";
};
In.prototype.compileLoopTest = function(o) {
var _ref2, code, ref, sub;
2010-10-23 11:10:28 -04:00
_ref2 = this.object.cache(o, LEVEL_LIST), sub = _ref2[0], ref = _ref2[1];
code = utility('indexOf') + (".call(" + (this.array.compile(o)) + ", " + ref + ") ") + (this.negated ? '< 0' : '>= 0');
if (sub === ref) {
return code;
}
code = sub + ', ' + code;
2010-10-23 11:10:28 -04:00
return o.level < LEVEL_LIST ? code : "(" + 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 = (function() {
Try = (function() {
function Try(_arg, _arg2, _arg3, _arg4) {
2010-10-04 23:21:16 -04:00
this.ensure = _arg4;
this.recovery = _arg3;
this.error = _arg2;
this.attempt = _arg;
Try.__super__.constructor.call(this);
2010-10-04 23:21:16 -04:00
return this;
};
return Try;
2010-10-04 23:21:16 -04:00
})();
__extends(Try, Base);
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;
o.indent = this.idt(1);
errorPart = this.error ? " (" + (this.error.compile(o)) + ") " : ' ';
2010-10-23 11:10:28 -04:00
catchPart = this.recovery ? " catch" + errorPart + "{\n" + (this.recovery.compile(o, LEVEL_TOP)) + "\n" + this.tab + "}" : !(this.ensure || this.recovery) ? ' catch (_e) {}' : undefined;
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 = (function() {
Throw = (function() {
function Throw(_arg) {
2010-10-04 23:21:16 -04:00
this.expression = _arg;
Throw.__super__.constructor.call(this);
2010-10-04 23:21:16 -04:00
return this;
};
return Throw;
2010-10-04 23:21:16 -04:00
})();
__extends(Throw, Base);
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 = (function() {
Existence = (function() {
function Existence(_arg) {
2010-10-04 23:21:16 -04:00
this.expression = _arg;
Existence.__super__.constructor.call(this);
2010-10-04 23:21:16 -04:00
return this;
};
return Existence;
2010-10-04 23:21:16 -04:00
})();
__extends(Existence, Base);
Existence.prototype.children = ['expression'];
Existence.prototype.compileNode = function(o) {
var code;
code = this.expression.compile(o);
code = IDENTIFIER.test(code) && !o.scope.check(code) ? "typeof " + code + " !== \"undefined\" && " + code + " !== null" : "" + code + " != null";
2010-10-23 11:10:28 -04:00
return o.level <= LEVEL_COND ? code : "(" + code + ")";
};
return Existence;
})();
2010-10-06 23:53:26 -04:00
exports.Parens = (function() {
Parens = (function() {
function Parens(_arg) {
2010-10-04 23:21:16 -04:00
this.expression = _arg;
2010-10-06 23:53:26 -04:00
Parens.__super__.constructor.call(this);
2010-10-04 23:21:16 -04:00
return this;
};
return Parens;
2010-10-04 23:21:16 -04:00
})();
2010-10-06 23:53:26 -04:00
__extends(Parens, Base);
Parens.prototype.children = ['expression'];
Parens.prototype.unwrap = function() {
return this.expression;
};
2010-10-06 23:53:26 -04:00
Parens.prototype.isComplex = function() {
return this.expression.isComplex();
};
2010-10-06 23:53:26 -04:00
Parens.prototype.makeReturn = function() {
return this.expression.makeReturn();
};
2010-10-06 23:53:26 -04:00
Parens.prototype.compileNode = function(o) {
var bare, code, expr;
expr = this.expression;
if (expr instanceof Value && expr.isAtomic()) {
expr.tags.front = this.tags.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);
return bare ? code : "(" + code + ")";
};
2010-10-06 23:53:26 -04:00
return Parens;
})();
exports.For = (function() {
For = (function() {
function For(_arg, head) {
2010-10-04 23:21:16 -04:00
this.body = _arg;
if (head.index instanceof Value) {
2010-10-20 06:53:41 -04:00
throw SyntaxError('index cannot be a pattern matching expression');
2010-10-04 23:21:16 -04:00
}
For.__super__.constructor.call(this);
extend(this, head);
if (!this.object) {
this.step || (this.step = new Literal(1));
}
2010-10-20 06:53:41 -04:00
this.pattern = this.name instanceof Value;
2010-10-04 23:21:16 -04:00
this.returns = false;
return this;
};
return For;
2010-10-04 23:21:16 -04:00
})();
__extends(For, Base);
For.prototype.children = ['body', 'source', 'guard', 'step', 'from', 'to'];
2010-10-20 06:53:41 -04:00
For.prototype.isStatement = YES;
For.prototype.makeReturn = function() {
this.returns = true;
return this;
};
For.prototype.compileReturnValue = function(val, o) {
if (this.returns) {
2010-10-06 23:41:09 -04:00
return '\n' + new Return(new Literal(val)).compile(o);
}
if (val) {
return '\n' + val;
}
return '';
};
For.prototype.compileNode = function(o) {
var _ref2, _ref3, _ref4, _ref5, _ref6, body, code, cond, defPart, forPart, guardPart, idt, index, ivar, lvar, name, namePart, pvar, retPart, rvar, scope, sourcePart, step, svar, tail, tvar, varPart, vars;
scope = o.scope;
name = !this.pattern && ((_ref2 = this.name) != null ? _ref2.compile(o) : undefined);
index = (_ref3 = this.index) != null ? _ref3.compile(o) : undefined;
2010-10-13 07:29:22 -04:00
ivar = !index ? scope.freeVariable('i') : index;
varPart = guardPart = defPart = retPart = '';
body = Expressions.wrap([this.body]);
idt = this.idt(1);
2010-10-13 07:29:22 -04:00
if (name) {
scope.find(name, {
immediate: true
});
2010-03-10 22:32:00 -05:00
}
if (index) {
scope.find(index, {
immediate: true
});
2010-03-10 22:32:00 -05:00
}
2010-10-13 07:29:22 -04:00
if (this.step) {
_ref4 = this.step.compileLoopReference(o, 'step'), step = _ref4[0], pvar = _ref4[1];
}
if (this.from) {
2010-10-13 07:29:22 -04:00
_ref5 = this.to.compileLoopReference(o, 'to'), tail = _ref5[0], tvar = _ref5[1];
vars = ivar + ' = ' + this.from.compile(o);
if (tail !== tvar) {
vars += ', ' + tail;
}
2010-10-13 07:29:22 -04:00
cond = +pvar ? "" + ivar + " " + (pvar < 0 ? '>' : '<') + "= " + tvar : "" + pvar + " < 0 ? " + ivar + " >= " + tvar + " : " + ivar + " <= " + tvar;
} else {
if (name || this.object && !this.raw) {
2010-10-13 07:29:22 -04:00
_ref6 = this.source.compileLoopReference(o, 'ref'), sourcePart = _ref6[0], svar = _ref6[1];
} else {
2010-10-23 11:10:28 -04:00
sourcePart = svar = this.source.compile(o, LEVEL_PAREN);
}
2010-10-23 11:10:28 -04:00
namePart = this.pattern ? new Assign(this.name, new Literal("" + svar + "[" + ivar + "]")).compile(o, LEVEL_TOP) : name ? "" + name + " = " + svar + "[" + ivar + "]" : undefined;
2010-10-07 07:05:22 -04:00
if (!this.object) {
if (0 > pvar && (pvar | 0) === +pvar) {
vars = "" + ivar + " = " + svar + ".length - 1";
cond = "" + ivar + " >= 0";
} else {
lvar = scope.freeVariable('len');
vars = "" + ivar + " = 0, " + lvar + " = " + svar + ".length";
cond = "" + ivar + " < " + lvar;
}
}
}
2010-10-13 07:29:22 -04:00
if (this.object) {
forPart = ivar + ' in ' + sourcePart;
guardPart = this.raw ? '' : idt + ("if (!" + (utility('hasProp')) + ".call(" + svar + ", " + ivar + ")) continue;\n");
2010-10-13 07:29:22 -04:00
} else {
if (step !== pvar) {
vars += ', ' + step;
2010-10-13 07:29:22 -04:00
}
if (svar !== sourcePart) {
defPart = this.tab + sourcePart + ';\n';
}
forPart = vars + ("; " + cond + "; ") + ivar + (function() {
2010-10-13 07:29:22 -04:00
switch (+pvar) {
case 1:
return '++';
2010-10-13 07:29:22 -04:00
case -1:
return '--';
2010-10-13 07:29:22 -04:00
default:
return pvar < 0 ? ' -= ' + pvar.slice(1) : ' += ' + pvar;
2010-10-13 07:29:22 -04:00
}
})();
}
if (namePart) {
varPart = idt + namePart + ';\n';
}
code = guardPart + varPart;
if (!body.isEmpty()) {
if (o.level > LEVEL_TOP || this.returns) {
rvar = scope.freeVariable('result');
defPart += this.tab + rvar + ' = [];\n';
retPart = this.compileReturnValue(rvar, o);
body = Push.wrap(rvar, body);
}
if (this.guard) {
body = Expressions.wrap([new If(this.guard, body)]);
}
o.indent = idt;
code += body.compile(o, LEVEL_TOP);
}
if (code) {
code = '\n' + code + '\n' + this.tab;
}
return defPart + this.tab + ("for (" + forPart + ") {" + code + "}") + retPart;
};
return For;
})();
exports.Switch = (function() {
Switch = (function() {
function Switch(_arg, _arg2, _arg3) {
2010-10-04 23:21:16 -04:00
this.otherwise = _arg3;
this.cases = _arg2;
this.subject = _arg;
Switch.__super__.constructor.call(this);
2010-10-04 23:21:16 -04:00
return this;
};
return Switch;
2010-10-04 23:21:16 -04:00
})();
__extends(Switch, Base);
Switch.prototype.children = ['subject', 'cases', 'otherwise'];
Switch.prototype.isStatement = YES;
Switch.prototype.makeReturn = function() {
var _i, _len, _ref2, _ref3, pair;
_ref2 = this.cases;
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
pair = _ref2[_i];
pair[1].makeReturn();
}
if ((_ref3 = this.otherwise) != null) {
_ref3.makeReturn();
}
return this;
};
Switch.prototype.compileNode = function(o) {
var _i, _j, _len, _len2, _ref2, _ref3, _ref4, _ref5, block, code, cond, conditions, expr, i, idt1, idt2;
2010-10-20 06:53:41 -04:00
idt1 = this.idt(1);
idt2 = o.indent = this.idt(2);
2010-10-24 17:20:45 -04:00
code = this.tab + ("switch (" + (((_ref2 = this.subject) != null ? _ref2.compile(o, LEVEL_PAREN) : undefined) || false) + ") {\n");
for (i = 0, _len = this.cases.length; i < _len; i++) {
_ref3 = this.cases[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");
}
2010-10-23 11:10:28 -04:00
code += block.compile(o, LEVEL_TOP) + '\n';
if (i === this.cases.length - 1 && !this.otherwise) {
break;
}
_ref5 = block.expressions;
for (_j = _ref5.length - 1; _j >= 0; _j--) {
expr = _ref5[_j];
if (!(expr instanceof Comment)) {
if (!(expr instanceof Return)) {
code += idt2 + 'break;\n';
}
break;
}
}
}
if (this.otherwise) {
2010-10-23 11:10:28 -04:00
code += idt1 + ("default:\n" + (this.otherwise.compile(o, LEVEL_TOP)) + "\n");
}
return code + this.tab + '}';
};
return Switch;
})();
exports.If = (function() {
If = (function() {
function If(condition, _arg, tags) {
2010-10-07 07:05:22 -04:00
this.body = _arg;
this.tags = tags || (tags = {});
this.condition = tags.invert ? condition.invert() : condition;
this.soakNode = tags.soak;
2010-10-04 23:21:16 -04:00
this.elseBody = null;
this.isChain = false;
return this;
};
return If;
2010-10-04 23:21:16 -04:00
})();
__extends(If, Base);
If.prototype.children = ['condition', 'body', 'elseBody'];
If.prototype.bodyNode = function() {
var _ref2;
return (_ref2 = this.body) != null ? _ref2.unwrap() : undefined;
};
If.prototype.elseBodyNode = function() {
var _ref2;
return (_ref2 = this.elseBody) != null ? _ref2.unwrap() : undefined;
};
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) {
2010-10-10 23:31:54 -04:00
var _ref2;
2010-10-23 11:10:28 -04:00
return (o != null ? o.level : undefined) === LEVEL_TOP || this.bodyNode().isStatement(o) || ((_ref2 = this.elseBodyNode()) != null ? _ref2.isStatement(o) : undefined);
};
If.prototype.compileNode = function(o) {
2010-10-06 20:25:00 -04:00
return this.isStatement(o) ? this.compileStatement(o) : this.compileExpression(o);
};
If.prototype.makeReturn = function() {
if (this.isStatement()) {
this.body && (this.body = this.ensureExpressions(this.body.makeReturn()));
this.elseBody && (this.elseBody = this.ensureExpressions(this.elseBody.makeReturn()));
return this;
} else {
return new Return(this);
}
};
If.prototype.ensureExpressions = function(node) {
return node instanceof Expressions ? node : 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);
o.indent = this.idt(1);
body = this.ensureExpressions(this.body).compile(o);
ifPart = "if (" + cond + ") {\n" + body + "\n" + this.tab + "}";
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 ? this.elseBodyNode().compile(merge(o, {
2010-10-10 23:31:54 -04:00
indent: this.tab,
chainChild: true
2010-10-23 11:10:28 -04:00
})) : "{\n" + (this.elseBody.compile(o, LEVEL_TOP)) + "\n" + this.tab + "}");
};
If.prototype.compileExpression = function(o) {
var _ref2, code;
2010-10-23 11:10:28 -04:00
code = this.condition.compile(o, LEVEL_COND) + ' ? ' + this.bodyNode().compile(o, LEVEL_LIST) + ' : ' + ((_ref2 = this.elseBodyNode()) != null ? _ref2.compile(o, LEVEL_LIST) : undefined);
return o.level >= LEVEL_COND ? "(" + code + ")" : code;
};
If.prototype.unfoldSoak = function() {
return this.soakNode && this;
};
If.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;
};
return If;
}).call(this);
Push = {
2010-10-06 20:25:00 -04:00
wrap: function(name, expressions) {
if (expressions.isEmpty() || last(expressions.expressions).containsPureStatement()) {
return expressions;
}
return expressions.push(new Call(new Value(new Literal(name), [new Accessor(new Literal('push'))]), [expressions.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;
}
func = new Parens(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'));
}
func = new Value(func, [new Accessor(meth)]);
2010-10-19 10:53:38 -04:00
func.noReturn = noReturn;
}
call = new Call(func, args);
return statement ? Expressions.wrap([call]) : 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
};
UTILITIES = {
2010-10-12 15:57:11 -04:00
"extends": 'function(child, parent) {\n function ctor() { this.constructor = child; }\n ctor.prototype = parent.prototype;\n child.prototype = new ctor;\n if (typeof parent.extended === "function") parent.extended(child);\n child.__super__ = parent.prototype;\n}',
2010-10-06 20:25:00 -04:00
bind: 'function(func, context) {\n return function() { return func.apply(context, arguments); };\n}',
2010-10-19 23:06:51 -04:00
indexOf: 'Array.prototype.indexOf || function(item) {\n for (var i = 0, l = this.length; i < l; i++) if (this[i] === item) return i;\n return -1;\n}',
hasProp: 'Object.prototype.hasOwnProperty',
slice: 'Array.prototype.slice'
};
2010-10-23 11:10:28 -04:00
LEVEL_TOP = 0;
LEVEL_PAREN = 1;
LEVEL_LIST = 2;
LEVEL_COND = 3;
LEVEL_OP = 4;
LEVEL_ACCESS = 5;
TAB = ' ';
TRAILING_WHITESPACE = /[ \t]+$/gm;
IDENTIFIER = /^[$A-Za-z_][$\w]*$/;
2010-10-13 07:29:22 -04:00
NUMBER = /^-?(?:0x[\da-f]+|(?:\d+(\.\d+)?|\.\d+)(?:e[+-]?\d+)?)$/i;
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);