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

1837 lines
65 KiB
JavaScript
Raw Normal View History

2010-07-25 03:15:12 -04:00
(function() {
var Accessor, ArrayLiteral, Assign, Base, Call, Class, Closure, Code, Comment, Existence, Expressions, Extends, For, IDENTIFIER, IS_STRING, If, In, Index, Literal, NO, NUMBER, ObjectLiteral, 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, 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) {
var top;
o = o ? merge(o) : {};
top = this.topSensitive() ? o.top : del(o, 'top');
this.tab = o.indent;
return top || o.asStatement || this instanceof Comment || this.isPureStatement() || !this.isStatement(o) ? this.compileNode(o) : this.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).compile(o);
};
Base.prototype.compileReference = function(o, options) {
var _len, compiled, i, node, pair, reference;
pair = (function() {
2010-10-07 07:05:22 -04:00
if (!this.isComplex()) {
return [this, this];
} else {
2010-10-06 23:41:09 -04:00
reference = new Literal(o.scope.freeVariable('ref'));
compiled = new Assign(reference, this);
return [compiled, reference];
}
}).call(this);
if ((options != null) ? options.precompile : undefined) {
for (i = 0, _len = pair.length; i < _len; i++) {
2010-10-01 18:26:37 -04:00
node = pair[i];
(pair[i] = node.compile(o));
}
}
return pair;
};
Base.prototype.compileBare = function(o) {
this.parenthetical = true;
return this.compile(o);
};
2010-10-13 07:29:22 -04:00
Base.prototype.compileLoopReference = function(o, name) {
var src, tmp;
src = tmp = this.compile(o);
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);
};
Base.prototype.contains = function(block) {
var contains;
contains = false;
this.traverseChildren(false, function(node) {
if (block(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() {
2010-10-06 20:25:00 -04:00
return this.isPureStatement() || this.contains(function(node) {
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;
};
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);
};
Base.prototype.children = [];
Base.prototype.unwrap = THIS;
Base.prototype.isStatement = NO;
Base.prototype.isPureStatement = NO;
Base.prototype.isComplex = YES;
Base.prototype.isChainable = NO;
Base.prototype.topSensitive = NO;
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.unshift = function(node) {
this.expressions.unshift(node);
return this;
};
Expressions.prototype.unwrap = function() {
return this.expressions.length === 1 ? this.expressions[0] : this;
};
Expressions.prototype.empty = function() {
return this.expressions.length === 0;
};
Expressions.prototype.makeReturn = function() {
2010-09-28 08:52:51 -04:00
var end, idx;
idx = this.expressions.length;
while ((end = this.expressions[--idx]) instanceof Comment) {
}
if (end) {
2010-09-28 08:52:51 -04:00
this.expressions[idx] = end.makeReturn();
}
return this;
};
Expressions.prototype.compile = function(o) {
o || (o = {});
return o.scope ? Expressions.__super__.compile.call(this, o) : this.compileRoot(o);
};
Expressions.prototype.compileNode = function(o) {
var _i, _len, _ref2, _result, node;
return (function() {
_ref2 = this.expressions;
_result = [];
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
node = _ref2[_i];
_result.push(this.compileExpression(node, merge(o)));
}
return _result;
2010-10-13 15:09:56 -04:00
}).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);
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;
code = this.compileNode(o);
if (o.scope.hasAssignments(this)) {
code = "" + this.tab + "var " + (o.scope.compiledAssignments().replace(/\n/g, '$&' + this.tab)) + ";\n" + code;
}
if (!o.globals && o.scope.hasDeclarations(this)) {
code = "" + this.tab + "var " + (o.scope.compiledDeclarations()) + ";\n" + code;
}
return code;
};
Expressions.prototype.compileExpression = function(node, o) {
var code;
o.top = node.tags.front = true;
this.tab = o.indent;
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.isStatement = function() {
var _ref2;
return (_ref2 = this.value) === 'break' || _ref2 === 'continue' || _ref2 === 'debugger';
};
Literal.prototype.isPureStatement = Literal.prototype.isStatement;
Literal.prototype.isComplex = NO;
Literal.prototype.assigns = function(name) {
return name === this.value;
};
Literal.prototype.compileNode = function() {
var val;
val = this.value.reserved ? "\"" + this.value + "\"" : this.value;
return this.isStatement() ? this.tab + val + ';' : val;
};
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;
Return.prototype.compile = function(o) {
2010-10-06 22:24:52 -04:00
var _ref2, expr;
expr = ((_ref2 = this.expression) != null) ? _ref2.makeReturn() : undefined;
return expr && !(expr instanceof Return) ? expr.compile(o) : Return.__super__.compile.call(this, o);
};
Return.prototype.compileNode = function(o) {
2010-10-06 22:24:52 -04:00
var expr;
expr = '';
if (this.expression) {
if (this.expression.isStatement(o)) {
o.asStatement = true;
}
expr = ' ' + this.expression.compileBare(o);
}
return this.tab + 'return' + expr + ';';
};
return Return;
})();
exports.Value = (function() {
Value = (function() {
2010-10-20 06:53:41 -04:00
function Value(_arg, props, tag) {
2010-10-04 23:21:16 -04:00
this.base = _arg;
Value.__super__.constructor.call(this);
2010-10-20 06:53:41 -04:00
this.properties = props || [];
2010-10-04 23:21:16 -04:00
if (tag) {
this.tags[tag] = true;
}
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 ArrayLiteral && !this.properties.length;
};
Value.prototype.isObject = function() {
return this.base instanceof ObjectLiteral && !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.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.base.isStatement(o) && !this.properties.length;
};
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.compile = function(o) {
this.base.tags.front = this.tags.front;
return !o.top || this.properties.length ? Value.__super__.compile.call(this, o) : this.base.compile(o);
};
Value.prototype.compileNode = function(o) {
var _i, _len, code, ifn, prop, props;
if (ifn = this.unfoldSoak(o)) {
return ifn.compile(o);
}
props = this.properties;
if (this.parenthetical && !props.length) {
this.base.parenthetical = true;
}
code = this.base.compile(o);
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,
operation: this.tags.operation
});
2010-09-12 14:44:03 -04:00
}
}
return null;
};
Value.wrap = function(node) {
return node instanceof Value ? node : new Value(node);
};
return Value;
}).call(this);
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.isStatement = YES;
Comment.prototype.makeReturn = THIS;
Comment.prototype.compileNode = function(o) {
return this.tab + '/*' + this.comment.replace(/\n/g, '\n' + 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.prefix = function() {
return this.isNew ? 'new ' : '';
};
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;
}
_ref2 = Value.wrap(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,
operation: this.tags.operation
});
}
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, ifn;
if (ifn = this.unfoldSoak(o)) {
return ifn.compile(o);
}
((_ref2 = this.variable) != null) ? _ref2.tags.front = this.tags.front : undefined;
_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];
_result.push(arg.compileBare(o));
}
return _result;
}).call(this).join(', ');
return this.isSuper ? this.compileSuper(args, o) : "" + (this.prefix()) + (this.variable.compile(o)) + "(" + 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) {
base = Value.wrap(this.variable);
if ((name = base.properties.pop()) && base.isComplex()) {
ref = o.scope.freeVariable('this');
fun = "(" + ref + " = " + (base.compile(o)) + ")" + (name.compile(o));
} else {
fun = ref = base.compile(o);
if (name) {
fun += name.compile(o);
}
}
return "" + fun + ".apply(" + ref + ", " + splatargs + ")";
}
2010-10-12 08:48:25 -04:00
idt = this.idt(1);
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)) + ", " + 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.compileNode = 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);
2010-10-04 23:21:16 -04:00
this.prototype = tag === 'prototype' ? '.prototype' : '';
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.compileNode = function(o) {
2010-10-20 06:53:41 -04:00
var name;
name = this.name.compile(o);
return this.prototype + (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.compileNode = function(o) {
return (this.proto ? '.prototype' : '') + ("[" + (this.index.compileBare(o)) + "]");
};
Index.prototype.isComplex = function() {
return this.index.isComplex();
};
return Index;
})();
exports.ObjectLiteral = (function() {
ObjectLiteral = (function() {
function ObjectLiteral(props) {
ObjectLiteral.__super__.constructor.call(this);
this.objects = this.properties = props || [];
2010-10-04 23:21:16 -04:00
return this;
};
return ObjectLiteral;
2010-10-04 23:21:16 -04:00
})();
__extends(ObjectLiteral, Base);
ObjectLiteral.prototype.children = ['properties'];
ObjectLiteral.prototype.compileNode = function(o) {
2010-10-19 19:58:59 -04:00
var _i, _len, _ref2, _result, i, indent, join, lastNoncom, nonComments, obj, prop, props, top;
top = del(o, 'top');
2010-02-09 20:53:25 -05:00
o.indent = this.idt(1);
nonComments = (function() {
_ref2 = this.properties;
_result = [];
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
prop = _ref2[_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() {
_ref2 = this.properties;
2010-10-19 19:58:59 -04:00
_result = [];
for (i = 0, _len = _ref2.length; i < _len; i++) {
2010-10-19 19:58:59 -04:00
prop = _ref2[i];
_result.push((function() {
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');
}
return indent + prop.compile(o) + join;
2010-02-09 20:53:25 -05:00
}).call(this));
}
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;
};
ObjectLiteral.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 ObjectLiteral;
})();
exports.ArrayLiteral = (function() {
ArrayLiteral = (function() {
2010-10-20 06:53:41 -04:00
function ArrayLiteral(objs) {
ArrayLiteral.__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 ArrayLiteral;
2010-10-04 23:21:16 -04:00
})();
__extends(ArrayLiteral, Base);
ArrayLiteral.prototype.children = ['objects'];
ArrayLiteral.prototype.compileSplatLiteral = function(o) {
return Splat.compileSplattedArray(this.objects, o);
};
ArrayLiteral.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];
code = obj.compileBare(o);
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
};
ArrayLiteral.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 ArrayLiteral;
})();
exports.Class = (function() {
Class = (function() {
2010-10-20 06:53:41 -04:00
function Class(variable, _arg, props) {
2010-10-06 20:25:00 -04:00
this.parent = _arg;
Class.__super__.constructor.call(this);
2010-10-06 23:41:09 -04:00
this.variable = variable === '__temp__' ? new Literal(variable) : variable;
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, returns, val, variable;
2010-10-06 20:25:00 -04:00
variable = this.variable;
if (variable.value === '__temp__') {
2010-10-06 23:41:09 -04:00
variable = new Literal(o.scope.freeVariable('ctor'));
}
extension = this.parent && new Extends(variable, this.parent);
props = new Expressions;
o.top = true;
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.compileReference(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.empty()) {
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) {
access = prop.context === 'this' ? pvar.base.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"));
}
construct = this.idt() + new Assign(variable, constructor).compile(merge(o, {
sharedScope: constScope
})) + ';';
props = !props.empty() ? '\n' + props.compile(o) : '';
extension = extension ? '\n' + this.idt() + extension.compile(o) + ';' : '';
returns = this.returns ? '\n' + new Return(variable).compile(o) : '';
return construct + extension + props + returns;
};
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.topSensitive = YES;
Assign.prototype.compileNode = function(o) {
var _ref2, ifn, isValue, match, name, stmt, top, val;
if (isValue = this.variable instanceof Value) {
if (this.variable.isArray() || this.variable.isObject()) {
return this.compilePatternMatch(o);
}
if (ifn = If.unfoldSoak(o, this, 'variable')) {
2010-10-10 23:31:54 -04:00
delete o.top;
return ifn.compile(o);
}
if ((_ref2 = this.context, __indexOf.call(this.CONDITIONAL, _ref2) >= 0)) {
return this.compileConditional(o);
}
2010-02-09 20:53:25 -05:00
}
top = del(o, 'top');
stmt = del(o, 'asStatement');
2010-02-09 20:53:25 -05:00
name = this.variable.compile(o);
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
}
val = this.value.compileBare(o);
2010-02-09 20:53:25 -05:00
if (this.context === 'object') {
return "" + name + ": " + val;
2010-02-09 20:53:25 -05:00
}
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-02-09 20:53:25 -05:00
if (stmt) {
return "" + this.tab + val + ";";
2010-02-09 20:53:25 -05:00
}
return top || this.parenthetical ? val : "(" + val + ")";
};
Assign.prototype.compilePatternMatch = function(o) {
var _len, _ref2, _ref3, accessClass, assigns, code, i, idx, isObject, obj, objects, olength, otop, ref, splat, top, val, valVar, value;
2010-10-20 20:16:17 -04:00
top = del(o, 'top');
otop = merge(o, {
top: true
});
if ((value = this.value).isStatement(o)) {
value = Closure.wrap(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, idx = _ref2.variable.base, obj = _ref2.value;
} else {
idx = isObject ? obj.tags["this"] ? obj.properties[0].name : obj : new Literal(0);
}
accessClass = IDENTIFIER.test(idx.value) ? Accessor : Index;
(value = Value.wrap(value)).properties.push(new accessClass(idx));
2010-10-20 20:16:17 -04:00
return new Assign(obj, value).compile(otop);
}
valVar = value.compile(o);
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) {
_ref3 = obj, idx = _ref3.variable.base, obj = _ref3.value;
} else {
idx = obj.tags["this"] ? obj.properties[0].name : obj;
}
2010-02-09 20:53:25 -05:00
}
if (!(obj instanceof Value || obj instanceof Splat)) {
2010-10-20 06:53:41 -04:00
throw SyntaxError('pattern matching must use only identifiers on the left-hand side.');
}
accessClass = isObject && IDENTIFIER.test(idx.value) ? Accessor : Index;
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);
2010-03-22 02:02:04 -04:00
}
2010-10-06 23:41:09 -04:00
val = new Value(new Literal(valVar), [new accessClass(idx)]);
}
assigns.push(new Assign(obj, val).compile(otop));
2010-02-09 20:53:25 -05:00
}
2010-10-07 07:05:22 -04:00
if (!top) {
assigns.push(valVar);
}
code = assigns.join(', ');
return top || this.parenthetical ? 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);
};
Assign.prototype.assigns = function(name) {
return this[this.context === 'object' ? 'value' : 'variable'].assigns(name);
};
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) {
2010-10-19 19:58:59 -04:00
var _i, _len, _len2, _ref2, _ref3, _result, close, code, comm, empty, func, i, open, param, params, sharedScope, splat, top, value;
sharedScope = del(o, 'sharedScope');
top = del(o, 'top');
o.scope = sharedScope || new Scope(o.scope, this.body, this);
o.top = true;
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;
2010-10-06 23:41:09 -04:00
_ref3 = [new Literal(o.scope.freeVariable('arg')), param.splat], param = _ref3[0], param.splat = _ref3[1];
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);
}
}
}
o.scope.startLevel();
params = (function() {
2010-10-01 18:26:37 -04:00
_result = [];
for (_i = 0, _len2 = params.length; _i < _len2; _i++) {
param = params[_i];
_result.push(param.compile(o));
}
return _result;
})();
2010-10-19 10:53:38 -04:00
if (!(empty || this.noReturn)) {
this.body.makeReturn();
}
for (_i = 0, _len2 = params.length; _i < _len2; _i++) {
2010-10-19 19:58:59 -04:00
param = params[_i];
2010-10-20 06:53:41 -04:00
o.scope.parameter(param);
}
comm = this.comment ? this.comment.compile(o) + '\n' : '';
2010-10-04 23:21:16 -04:00
if (this.className) {
o.indent = this.idt(2);
}
code = this.body.expressions.length ? "\n" + (this.body.compileWithDeclarations(o)) + "\n" : '';
open = this.className ? "(function() {\n" + comm + (this.idt(1)) + "function " + this.className + "(" : "function(";
close = this.className ? "" + (code && this.idt(1)) + "};\n" + (this.idt(1)) + "return " + this.className + ";\n" + this.tab + "})()" : "" + (code && this.tab) + "}";
func = "" + open + (params.join(', ')) + ") {" + code + close;
o.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;
};
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.compileNode = function(o) {
2010-07-28 02:17:50 -04:00
return this.value.compile(o);
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.assigns = function(name) {
return this.name.assigns(name);
};
Splat.prototype.compileNode = 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');
2010-07-25 03:15:12 -04:00
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];
code = arg.compile(o);
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);
2010-10-10 18:29:38 -04:00
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'];
2010-10-20 06:53:41 -04:00
While.prototype.topSensitive = YES;
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 cond, post, pre, rvar, set, top;
top = del(o, 'top') && !this.returns;
2010-02-09 22:05:17 -05:00
o.indent = this.idt(1);
cond = this.condition.compileBare(o);
o.top = true;
2010-02-09 22:05:17 -05:00
set = '';
2010-10-07 07:05:22 -04:00
if (!top) {
rvar = o.scope.freeVariable('result');
set = "" + this.tab + rvar + " = [];\n";
if (this.body) {
this.body = Push.wrap(rvar, this.body);
}
2010-02-09 22:05:17 -05:00
}
pre = "" + set + this.tab + "while (" + cond + ")";
if (this.guard) {
this.body = Expressions.wrap([new If(this.guard, this.body)]);
}
2010-08-11 00:40:15 -04:00
if (this.returns) {
2010-10-06 23:41:09 -04:00
post = '\n' + new Return(new Literal(rvar)).compile(merge(o, {
2010-08-11 00:40:15 -04:00
indent: this.idt()
}));
} else {
post = '';
}
return "" + pre + " {\n" + (this.body.compile(o)) + "\n" + this.tab + "}" + post;
};
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).tags.operation = true;
if (second) {
(this.second = second).tags.operation = true;
2010-10-04 23:21:16 -04:00
}
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.CHAINABLE = ['<', '>', '>=', '<=', '===', '!=='];
Op.prototype.PREFIX_OPERATORS = ['new', 'typeof', 'delete'];
Op.prototype.MUTATORS = ['++', '--', 'delete'];
Op.prototype.children = ['first', 'second'];
Op.prototype.isUnary = function() {
return !this.second;
};
Op.prototype.isComplex = function() {
return this.operator !== '!' || this.first.isComplex();
};
Op.prototype.isChainable = function() {
var _ref2;
return (_ref2 = this.operator, __indexOf.call(this.CHAINABLE, _ref2) >= 0);
};
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.toString = function(idt) {
return Op.__super__.toString.call(this, idt, this.constructor.name + ' ' + this.operator);
};
Op.prototype.compileNode = function(o) {
var _ref2, ifn;
if (this.isUnary()) {
if ((_ref2 = this.operator, __indexOf.call(this.MUTATORS, _ref2) >= 0) && (ifn = If.unfoldSoak(o, this, 'first'))) {
return ifn.compile(o);
}
return this.compileUnary(o);
}
if (this.isChainable() && this.first.unwrap().isChainable()) {
return this.compileChain(o);
}
if (this.operator === '?') {
return this.compileExistence(o);
}
this.first.tags.front = this.tags.front;
return "" + (this.first.compile(o)) + " " + this.operator + " " + (this.second.compile(o));
};
Op.prototype.compileChain = function(o) {
2010-10-20 06:53:41 -04:00
var _ref2, shared;
_ref2 = this.first.unwrap().second.compileReference(o), this.first.second = _ref2[0], shared = _ref2[1];
return "" + (this.first.compile(o)) + " && " + (shared.compile(o)) + " " + this.operator + " " + (this.second.compile(o));
};
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);
}
this.second.tags.operation = false;
return new Existence(fst).compile(o) + (" ? " + ref + " : " + (this.second.compileBare(o)));
};
Op.prototype.compileUnary = function(o) {
var _ref2, parts, space;
space = (_ref2 = this.operator, __indexOf.call(this.PREFIX_OPERATORS, _ref2) >= 0) ? ' ' : '';
parts = [this.operator, space, this.first.compile(o)];
2010-10-10 20:40:41 -04:00
return (this.flip ? parts.reverse() : parts).join('');
};
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;
_ref2 = this.object.compileReference(o, {
precompile: true
}), 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);
return this.parenthetical ? tests : "(" + tests + ")";
};
In.prototype.compileLoopTest = function(o) {
var _ref2, code, ref, sub;
2010-10-19 23:06:51 -04:00
_ref2 = this.object.compileReference(merge(o, {
top: true
}), {
precompile: true
}), sub = _ref2[0], ref = _ref2[1];
code = utility('indexOf') + (".call(" + (this.array.compile(o)) + ", " + ref + ") ") + (this.negated ? '< 0' : '>= 0');
return sub === ref ? code : "(" + sub + ", " + 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 attemptPart, catchPart, errorPart, finallyPart;
o.indent = this.idt(1);
o.top = true;
attemptPart = this.attempt.compile(o);
errorPart = this.error ? " (" + (this.error.compile(o)) + ") " : ' ';
catchPart = this.recovery ? " catch" + errorPart + "{\n" + (this.recovery.compile(o)) + "\n" + this.tab + "}" : !(this.ensure || this.recovery) ? ' catch (_e) {}' : '';
finallyPart = (this.ensure || '') && ' finally {\n' + this.ensure.compile(merge(o)) + ("\n" + this.tab + "}");
return "" + this.tab + "try {\n" + attemptPart + "\n" + this.tab + "}" + catchPart + finallyPart;
};
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";
return this.parenthetical ? 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'];
2010-10-20 06:53:41 -04:00
Parens.prototype.topSensitive = YES;
2010-10-06 23:53:26 -04:00
Parens.prototype.isStatement = function(o) {
return this.expression.isStatement(o);
};
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 code, expr, top;
top = del(o, 'top');
expr = this.expression;
if (expr instanceof Value && expr.isAtomic()) {
expr.tags.front = this.tags.front;
return expr.compile(o);
}
code = expr.compileBare(o);
if (this.parenthetical || expr.isStatement(o)) {
return top ? this.tab + code + ';' : code;
}
return "(" + 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;
if (this.range && this.pattern) {
throw SyntaxError('cannot pattern match a range loop');
}
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'];
For.prototype.topSensitive = YES;
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, cond, defPart, forPart, guardPart, idt, index, ivar, lvar, name, namePart, pvar, resultRet, rvar, scope, sourcePart, step, svar, tail, top, tvar, varPart, vars;
scope = o.scope;
2010-10-13 07:29:22 -04:00
top = del(o, 'top') && !this.returns;
name = !this.pattern && (((_ref2 = this.name) != null) ? _ref2.compile(o) : undefined);
index = ((_ref3 = this.index) != null) ? _ref3.compile(o) : undefined;
ivar = !index ? scope.freeVariable('i') : index;
varPart = '';
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) {
2010-10-13 07:29:22 -04:00
_ref6 = this.source.compileLoopReference(o, 'ref'), sourcePart = _ref6[0], svar = _ref6[1];
} else {
sourcePart = svar = this.source.compile(o);
}
2010-10-06 23:41:09 -04:00
namePart = this.pattern ? new Assign(this.name, new Literal("" + svar + "[" + ivar + "]")).compile(merge(o, {
top: true
})) : 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;
}
}
}
defPart = '';
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");
} else {
if (step !== pvar) {
vars += ", " + step;
}
if (svar !== sourcePart) {
defPart = "" + this.tab + sourcePart + ";\n";
}
2010-10-13 07:29:22 -04:00
forPart = ("" + vars + "; " + cond + "; ") + (function() {
switch (+pvar) {
case 1:
return ivar + '++';
2010-10-13 07:29:22 -04:00
case -1:
return ivar + '--';
2010-10-13 07:29:22 -04:00
default:
return ivar + (pvar < 0 ? ' -= ' + pvar.slice(1) : ' += ' + pvar);
}
})();
}
if (!top) {
rvar = scope.freeVariable('result');
defPart += "" + this.tab + rvar + " = [];\n";
resultRet = this.compileReturnValue(rvar, o);
body = Push.wrap(rvar, body);
}
2010-08-11 00:40:15 -04:00
if (this.guard) {
body = Expressions.wrap([new If(this.guard, body)]);
}
2010-10-13 07:29:22 -04:00
if (namePart) {
varPart = "" + idt + namePart + ";\n";
}
return "" + (defPart || '') + this.tab + "for (" + forPart + ") {\n" + (guardPart || '') + varPart + (body.compile(merge(o, {
indent: idt,
top: true
2010-10-21 21:27:45 -04:00
}))) + "\n" + this.tab + "}" + (resultRet || '');
};
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, pair;
_ref2 = this.cases;
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
pair = _ref2[_i];
pair[1].makeReturn();
}
if (this.otherwise) {
this.otherwise.makeReturn();
}
return this;
};
Switch.prototype.compileNode = function(o) {
var _i, _j, _len, _len2, _ref2, _ref3, _ref4, block, code, condition, conditions, idt1, idt2;
2010-10-20 06:53:41 -04:00
idt1 = this.idt(1);
idt2 = o.indent = this.idt(2);
o.top = true;
code = "" + this.tab + "switch (" + ((((_ref2 = this.subject) != null) ? _ref2.compile(o) : undefined) || true) + ") {";
for (_i = 0, _len = this.cases.length; _i < _len; _i++) {
_ref3 = this.cases[_i], conditions = _ref3[0], block = _ref3[1];
_ref4 = flatten([conditions]);
for (_j = 0, _len2 = _ref4.length; _j < _len2; _j++) {
condition = _ref4[_j];
2010-10-20 06:53:41 -04:00
if (!this.subject) {
condition = condition.invert().invert();
}
code += "\n" + idt1 + "case " + (condition.compile(o)) + ":";
}
code += "\n" + (block.compile(o));
2010-10-20 06:53:41 -04:00
if (!(last(block.expressions) instanceof Return)) {
code += "\n" + idt2 + "break;";
}
}
if (this.otherwise) {
code += "\n" + idt1 + "default:\n" + (this.otherwise.compile(o));
}
code += "\n" + this.tab + "}";
return code;
};
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.topSensitive = YES;
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;
return this.statement || (this.statement = ((o != null) ? o.top : undefined) || 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) {
2010-10-10 23:31:54 -04:00
var child, condO, ifPart, top;
2010-08-11 00:40:15 -04:00
top = del(o, 'top');
child = del(o, 'chainChild');
condO = merge(o);
o.indent = this.idt(1);
o.top = true;
ifPart = "if (" + (this.condition.compileBare(condO)) + ") {\n" + (this.body.compile(o)) + "\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
})) : "{\n" + (this.elseBody.compile(o)) + "\n" + this.tab + "}");
};
If.prototype.compileExpression = function(o) {
var _ref2, code;
this.condition.tags.operation = true;
code = this.condition.compile(o) + ' ? ' + this.bodyNode().compileBare(o) + ' : ' + (((_ref2 = this.elseBodyNode()) != null) ? _ref2.compileBare(o) : undefined);
return this.tags.operation ? "(" + 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);
if (parent.tags.operation) {
ifn.tags.operation = true;
}
return ifn;
};
return If;
}).call(this);
Push = {
2010-10-06 20:25:00 -04:00
wrap: function(name, expressions) {
if (expressions.empty() || expressions.containsPureStatement()) {
return expressions;
}
2010-10-06 23:41:09 -04:00
return Expressions.wrap([new Call(new Value(new Literal(name), [new Accessor(new Literal('push'))]), [expressions.unwrap()])]);
}
2010-10-06 20:25:00 -04:00
};
Closure = {
2010-10-19 10:53:38 -04:00
wrap: function(expressions, statement, noReturn) {
2010-10-06 20:25:00 -04:00
var args, call, func, mentionsArgs, meth;
if (expressions.containsPureStatement()) {
return expressions;
}
2010-10-06 23:53:26 -04:00
func = new Parens(new Code([], Expressions.wrap([expressions])));
args = [];
2010-10-06 20:25:00 -04:00
if ((mentionsArgs = expressions.contains(this.literalArgs)) || (expressions.contains(this.literalThis))) {
2010-10-06 23:41:09 -04:00
meth = new Literal(mentionsArgs ? 'apply' : 'call');
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'
};
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;
};
}).call(this);