2010-01-30 23:17:36 -05:00
|
|
|
(function(){
|
2010-06-27 12:59:54 -04:00
|
|
|
var AccessorNode, ArrayNode, AssignNode, BaseNode, CallNode, ClassNode, ClosureNode, CodeNode, ExistenceNode, Expressions, ExtendsNode, ForNode, IDENTIFIER, IS_STRING, IfNode, InNode, IndexNode, LiteralNode, NUMBER, ObjectNode, OpNode, ParentheticalNode, PushNode, RangeNode, ReturnNode, Scope, SliceNode, SplatNode, TAB, TRAILING_WHITESPACE, ThrowNode, TryNode, UTILITIES, ValueNode, WhileNode, _a, compact, del, flatten, helpers, include, indexOf, literal, merge, starts, utility;
|
2010-05-19 15:50:29 -04:00
|
|
|
var __extends = function(child, parent) {
|
2010-05-14 23:40:04 -04:00
|
|
|
var ctor = function(){ };
|
|
|
|
ctor.prototype = parent.prototype;
|
|
|
|
child.__superClass__ = parent.prototype;
|
|
|
|
child.prototype = new ctor();
|
|
|
|
child.prototype.constructor = child;
|
2010-03-30 19:27:38 -04:00
|
|
|
};
|
2010-03-09 21:24:30 -05:00
|
|
|
if ((typeof process !== "undefined" && process !== null)) {
|
2010-03-15 23:39:46 -04:00
|
|
|
Scope = require('./scope').Scope;
|
|
|
|
helpers = require('./helpers').helpers;
|
2010-03-09 21:24:30 -05:00
|
|
|
} else {
|
|
|
|
this.exports = this;
|
2010-03-16 23:18:54 -04:00
|
|
|
helpers = this.helpers;
|
|
|
|
Scope = this.Scope;
|
2010-03-09 21:24:30 -05:00
|
|
|
}
|
2010-04-25 22:29:43 -04:00
|
|
|
_a = helpers;
|
|
|
|
compact = _a.compact;
|
|
|
|
flatten = _a.flatten;
|
|
|
|
merge = _a.merge;
|
|
|
|
del = _a.del;
|
2010-05-25 02:15:46 -04:00
|
|
|
include = _a.include;
|
2010-06-12 19:05:13 -04:00
|
|
|
indexOf = _a.indexOf;
|
2010-05-31 19:38:45 -04:00
|
|
|
starts = _a.starts;
|
2010-02-27 19:19:53 -05:00
|
|
|
exports.BaseNode = (function() {
|
2010-05-31 10:36:50 -04:00
|
|
|
BaseNode = function() { };
|
2010-05-14 23:40:04 -04:00
|
|
|
BaseNode.prototype.compile = function(o) {
|
2010-02-27 19:19:53 -05:00
|
|
|
var closure, top;
|
2010-03-14 17:58:32 -04:00
|
|
|
this.options = merge(o || {});
|
2010-03-07 16:41:06 -05:00
|
|
|
this.tab = o.indent;
|
2010-05-03 09:04:26 -04:00
|
|
|
if (!(this instanceof ValueNode || this instanceof CallNode)) {
|
2010-03-14 17:58:32 -04:00
|
|
|
del(this.options, 'operation');
|
2010-05-03 09:04:26 -04:00
|
|
|
if (!(this instanceof AccessorNode || this instanceof IndexNode)) {
|
2010-06-12 19:05:13 -04:00
|
|
|
del(this.options, 'chainRoot');
|
2010-05-03 09:04:26 -04:00
|
|
|
}
|
2010-02-07 15:45:05 -05:00
|
|
|
}
|
2010-06-12 19:05:13 -04:00
|
|
|
top = this.topSensitive() ? this.options.top : del(this.options, 'top');
|
2010-06-27 12:59:54 -04:00
|
|
|
closure = this.isStatement() && !this.isPureStatement() && !top && !this.options.asStatement && !this.containsPureStatement();
|
2010-03-21 11:28:05 -04:00
|
|
|
if (closure) {
|
2010-06-12 19:05:13 -04:00
|
|
|
return this.compileClosure(this.options);
|
2010-03-21 11:28:05 -04:00
|
|
|
} else {
|
2010-06-12 19:05:13 -04:00
|
|
|
return this.compileNode(this.options);
|
2010-03-21 11:28:05 -04:00
|
|
|
}
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
BaseNode.prototype.compileClosure = function(o) {
|
2010-03-07 16:41:06 -05:00
|
|
|
this.tab = o.indent;
|
2010-06-12 19:05:13 -04:00
|
|
|
o.sharedScope = o.scope;
|
2010-02-27 19:19:53 -05:00
|
|
|
return ClosureNode.wrap(this).compile(o);
|
|
|
|
};
|
2010-06-21 23:51:12 -04:00
|
|
|
BaseNode.prototype.compileReference = function(o, options) {
|
|
|
|
var compiled, pair, reference;
|
|
|
|
pair = (function() {
|
|
|
|
if (!(this instanceof CallNode || this instanceof ValueNode && (!(this.base instanceof LiteralNode) || this.hasProperties()))) {
|
|
|
|
return [this, this];
|
|
|
|
} else {
|
|
|
|
reference = literal(o.scope.freeVariable());
|
|
|
|
compiled = new AssignNode(reference, this);
|
|
|
|
return [compiled, reference];
|
|
|
|
}
|
|
|
|
}).call(this);
|
|
|
|
if (!(options && options.precompile)) {
|
|
|
|
return pair;
|
2010-05-31 19:38:45 -04:00
|
|
|
}
|
2010-06-21 23:51:12 -04:00
|
|
|
return [pair[0].compile(o), pair[1].compile(o)];
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-05-14 23:40:04 -04:00
|
|
|
BaseNode.prototype.idt = function(tabs) {
|
2010-03-07 17:31:39 -05:00
|
|
|
var idt, num;
|
2010-03-07 16:41:06 -05:00
|
|
|
idt = this.tab || '';
|
2010-03-07 17:31:39 -05:00
|
|
|
num = (tabs || 0) + 1;
|
|
|
|
while (num -= 1) {
|
2010-03-10 20:15:12 -05:00
|
|
|
idt += TAB;
|
2010-02-27 19:19:53 -05:00
|
|
|
}
|
|
|
|
return idt;
|
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
BaseNode.prototype.makeReturn = function() {
|
2010-03-21 07:17:58 -04:00
|
|
|
return new ReturnNode(this);
|
|
|
|
};
|
2010-05-14 23:40:04 -04:00
|
|
|
BaseNode.prototype.contains = function(block) {
|
2010-05-10 06:58:01 -04:00
|
|
|
var contains;
|
|
|
|
contains = false;
|
2010-06-12 19:05:13 -04:00
|
|
|
this.traverseChildren(false, function(node) {
|
2010-05-10 22:41:18 -04:00
|
|
|
if (block(node)) {
|
|
|
|
contains = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
2010-05-10 06:58:01 -04:00
|
|
|
return contains;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
BaseNode.prototype.containsType = function(type) {
|
2010-03-31 22:48:47 -04:00
|
|
|
return this instanceof type || this.contains(function(n) {
|
|
|
|
return n instanceof type;
|
|
|
|
});
|
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
BaseNode.prototype.containsPureStatement = function() {
|
|
|
|
return this.isPureStatement() || this.contains(function(n) {
|
|
|
|
return n.isPureStatement();
|
2010-03-21 11:28:05 -04:00
|
|
|
});
|
|
|
|
};
|
2010-05-14 23:40:04 -04:00
|
|
|
BaseNode.prototype.traverse = function(block) {
|
2010-06-12 19:05:13 -04:00
|
|
|
return this.traverseChildren(true, block);
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-05-14 23:40:04 -04:00
|
|
|
BaseNode.prototype.toString = function(idt) {
|
2010-04-25 22:29:43 -04:00
|
|
|
var _b, _c, _d, _e, child;
|
2010-02-27 19:19:53 -05:00
|
|
|
idt = idt || '';
|
2010-06-15 01:03:14 -04:00
|
|
|
return '\n' + idt + this['class'] + (function() {
|
2010-06-12 19:05:13 -04:00
|
|
|
_b = []; _d = this.collectChildren();
|
2010-04-25 22:29:43 -04:00
|
|
|
for (_c = 0, _e = _d.length; _c < _e; _c++) {
|
|
|
|
child = _d[_c];
|
|
|
|
_b.push(child.toString(idt + TAB));
|
2010-02-27 19:19:53 -05:00
|
|
|
}
|
2010-04-25 22:29:43 -04:00
|
|
|
return _b;
|
2010-02-27 19:19:53 -05:00
|
|
|
}).call(this).join('');
|
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
BaseNode.prototype.eachChild = function(func) {
|
2010-06-13 01:13:52 -04:00
|
|
|
var _b, _c, _d, _e, _f, _g, _h, attr, child;
|
2010-05-31 10:36:50 -04:00
|
|
|
if (!(this.children)) {
|
2010-05-19 15:37:42 -04:00
|
|
|
return null;
|
|
|
|
}
|
2010-06-13 01:13:52 -04:00
|
|
|
_b = []; _d = this.children;
|
|
|
|
for (_c = 0, _e = _d.length; _c < _e; _c++) {
|
|
|
|
attr = _d[_c];
|
2010-05-10 22:41:18 -04:00
|
|
|
if (this[attr]) {
|
2010-06-13 01:13:52 -04:00
|
|
|
_g = flatten([this[attr]]);
|
|
|
|
for (_f = 0, _h = _g.length; _f < _h; _f++) {
|
|
|
|
child = _g[_f];
|
2010-05-10 22:41:18 -04:00
|
|
|
if (func(child) === false) {
|
2010-05-10 06:58:01 -04:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-06-13 01:13:52 -04:00
|
|
|
return _b;
|
2010-05-10 06:58:01 -04:00
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
BaseNode.prototype.collectChildren = function() {
|
2010-05-31 10:36:50 -04:00
|
|
|
var nodes;
|
|
|
|
nodes = [];
|
2010-06-12 19:05:13 -04:00
|
|
|
this.eachChild(function(node) {
|
2010-05-31 10:36:50 -04:00
|
|
|
return nodes.push(node);
|
|
|
|
});
|
|
|
|
return nodes;
|
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
BaseNode.prototype.traverseChildren = function(crossScope, func) {
|
|
|
|
return this.eachChild(function(child) {
|
2010-05-10 06:58:01 -04:00
|
|
|
func.apply(this, arguments);
|
|
|
|
if (child instanceof BaseNode) {
|
2010-06-12 19:05:13 -04:00
|
|
|
return child.traverseChildren(crossScope, func);
|
2010-05-10 06:58:01 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
2010-06-15 01:03:14 -04:00
|
|
|
BaseNode.prototype['class'] = 'BaseNode';
|
2010-05-31 10:36:50 -04:00
|
|
|
BaseNode.prototype.children = [];
|
2010-05-14 23:40:04 -04:00
|
|
|
BaseNode.prototype.unwrap = function() {
|
2010-02-27 19:19:53 -05:00
|
|
|
return this;
|
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
BaseNode.prototype.isStatement = function() {
|
2010-02-27 19:19:53 -05:00
|
|
|
return false;
|
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
BaseNode.prototype.isPureStatement = function() {
|
2010-02-27 19:19:53 -05:00
|
|
|
return false;
|
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
BaseNode.prototype.topSensitive = function() {
|
2010-02-27 19:19:53 -05:00
|
|
|
return false;
|
|
|
|
};
|
|
|
|
return BaseNode;
|
2010-04-10 14:40:05 -04:00
|
|
|
})();
|
2010-02-27 19:19:53 -05:00
|
|
|
exports.Expressions = (function() {
|
2010-05-14 23:40:04 -04:00
|
|
|
Expressions = function(nodes) {
|
2010-05-10 06:58:01 -04:00
|
|
|
this.expressions = compact(flatten(nodes || []));
|
2010-02-08 21:10:48 -05:00
|
|
|
return this;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-03-30 20:15:51 -04:00
|
|
|
__extends(Expressions, BaseNode);
|
2010-06-15 01:03:14 -04:00
|
|
|
Expressions.prototype['class'] = 'Expressions';
|
2010-05-31 10:36:50 -04:00
|
|
|
Expressions.prototype.children = ['expressions'];
|
2010-06-12 19:05:13 -04:00
|
|
|
Expressions.prototype.isStatement = function() {
|
2010-05-31 10:36:50 -04:00
|
|
|
return true;
|
|
|
|
};
|
2010-05-14 23:40:04 -04:00
|
|
|
Expressions.prototype.push = function(node) {
|
2010-02-08 20:20:11 -05:00
|
|
|
this.expressions.push(node);
|
|
|
|
return this;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-05-14 23:40:04 -04:00
|
|
|
Expressions.prototype.unshift = function(node) {
|
2010-02-08 20:20:11 -05:00
|
|
|
this.expressions.unshift(node);
|
|
|
|
return this;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-05-14 23:40:04 -04:00
|
|
|
Expressions.prototype.unwrap = function() {
|
2010-03-21 07:17:58 -04:00
|
|
|
if (this.expressions.length === 1) {
|
|
|
|
return this.expressions[0];
|
|
|
|
} else {
|
|
|
|
return this;
|
|
|
|
}
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-05-14 23:40:04 -04:00
|
|
|
Expressions.prototype.empty = function() {
|
2010-02-08 20:20:11 -05:00
|
|
|
return this.expressions.length === 0;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
Expressions.prototype.makeReturn = function() {
|
2010-03-21 11:28:05 -04:00
|
|
|
var idx, last;
|
|
|
|
idx = this.expressions.length - 1;
|
|
|
|
last = this.expressions[idx];
|
|
|
|
if (!last || last instanceof ReturnNode) {
|
|
|
|
return this;
|
|
|
|
}
|
2010-06-13 01:13:52 -04:00
|
|
|
this.expressions[idx] = last.makeReturn();
|
2010-03-21 07:17:58 -04:00
|
|
|
return this;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-05-14 23:40:04 -04:00
|
|
|
Expressions.prototype.compile = function(o) {
|
2010-02-13 01:13:08 -05:00
|
|
|
o = o || {};
|
2010-03-21 07:17:58 -04:00
|
|
|
if (o.scope) {
|
|
|
|
return Expressions.__superClass__.compile.call(this, o);
|
|
|
|
} else {
|
2010-06-12 19:05:13 -04:00
|
|
|
return this.compileRoot(o);
|
2010-03-21 07:17:58 -04:00
|
|
|
}
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
Expressions.prototype.compileNode = function(o) {
|
2010-04-25 22:29:43 -04:00
|
|
|
var _b, _c, _d, _e, node;
|
2010-02-17 19:19:51 -05:00
|
|
|
return (function() {
|
2010-04-25 22:29:43 -04:00
|
|
|
_b = []; _d = this.expressions;
|
|
|
|
for (_c = 0, _e = _d.length; _c < _e; _c++) {
|
|
|
|
node = _d[_c];
|
2010-06-12 19:05:13 -04:00
|
|
|
_b.push(this.compileExpression(node, merge(o)));
|
2010-02-08 20:20:11 -05:00
|
|
|
}
|
2010-04-25 22:29:43 -04:00
|
|
|
return _b;
|
2010-02-17 19:19:51 -05:00
|
|
|
}).call(this).join("\n");
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
Expressions.prototype.compileRoot = function(o) {
|
2010-03-07 16:41:06 -05:00
|
|
|
var code;
|
2010-06-12 19:05:13 -04:00
|
|
|
o.indent = (this.tab = o.noWrap ? '' : TAB);
|
2010-02-08 20:20:11 -05:00
|
|
|
o.scope = new Scope(null, this, null);
|
2010-06-12 19:05:13 -04:00
|
|
|
code = o.globals ? this.compileNode(o) : this.compileWithDeclarations(o);
|
2010-02-08 20:20:11 -05:00
|
|
|
code = code.replace(TRAILING_WHITESPACE, '');
|
2010-06-12 19:05:13 -04:00
|
|
|
if (o.noWrap) {
|
2010-03-21 07:17:58 -04:00
|
|
|
return code;
|
|
|
|
} else {
|
|
|
|
return "(function(){\n" + code + "\n})();\n";
|
|
|
|
}
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
Expressions.prototype.compileWithDeclarations = function(o) {
|
2010-03-15 23:46:14 -04:00
|
|
|
var code;
|
2010-06-12 19:05:13 -04:00
|
|
|
code = this.compileNode(o);
|
|
|
|
if (o.scope.hasAssignments(this)) {
|
|
|
|
code = ("" + (this.tab) + "var " + (o.scope.compiledAssignments()) + ";\n" + code);
|
2010-02-08 20:20:11 -05:00
|
|
|
}
|
2010-06-12 19:05:13 -04:00
|
|
|
if (o.scope.hasDeclarations(this)) {
|
|
|
|
code = ("" + (this.tab) + "var " + (o.scope.compiledDeclarations()) + ";\n" + code);
|
2010-02-08 20:20:11 -05:00
|
|
|
}
|
|
|
|
return code;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
Expressions.prototype.compileExpression = function(node, o) {
|
|
|
|
var compiledNode;
|
2010-03-07 16:41:06 -05:00
|
|
|
this.tab = o.indent;
|
2010-06-12 19:05:13 -04:00
|
|
|
compiledNode = node.compile(merge(o, {
|
2010-03-21 07:17:58 -04:00
|
|
|
top: true
|
|
|
|
}));
|
2010-06-12 19:05:13 -04:00
|
|
|
if (node.isStatement()) {
|
|
|
|
return compiledNode;
|
2010-03-21 11:28:05 -04:00
|
|
|
} else {
|
2010-06-12 19:05:13 -04:00
|
|
|
return "" + (this.idt()) + compiledNode + ";";
|
2010-02-08 20:20:11 -05:00
|
|
|
}
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
|
|
|
return Expressions;
|
2010-04-10 14:40:05 -04:00
|
|
|
})();
|
2010-05-14 23:40:04 -04:00
|
|
|
Expressions.wrap = function(nodes) {
|
2010-02-08 21:10:48 -05:00
|
|
|
if (nodes.length === 1 && nodes[0] instanceof Expressions) {
|
|
|
|
return nodes[0];
|
|
|
|
}
|
|
|
|
return new Expressions(nodes);
|
|
|
|
};
|
2010-02-27 19:19:53 -05:00
|
|
|
exports.LiteralNode = (function() {
|
2010-05-14 23:40:04 -04:00
|
|
|
LiteralNode = function(value) {
|
2010-02-11 20:20:59 -05:00
|
|
|
this.value = value;
|
2010-02-08 22:22:59 -05:00
|
|
|
return this;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-03-30 20:15:51 -04:00
|
|
|
__extends(LiteralNode, BaseNode);
|
2010-06-15 01:03:14 -04:00
|
|
|
LiteralNode.prototype['class'] = 'LiteralNode';
|
2010-06-12 19:05:13 -04:00
|
|
|
LiteralNode.prototype.isStatement = function() {
|
2010-02-08 21:10:48 -05:00
|
|
|
return this.value === 'break' || this.value === 'continue';
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
LiteralNode.prototype.isPureStatement = LiteralNode.prototype.isStatement;
|
|
|
|
LiteralNode.prototype.compileNode = function(o) {
|
2010-02-08 21:10:48 -05:00
|
|
|
var end, idt;
|
2010-06-12 19:05:13 -04:00
|
|
|
idt = this.isStatement() ? this.idt() : '';
|
|
|
|
end = this.isStatement() ? ';' : '';
|
2010-03-23 00:18:50 -04:00
|
|
|
return "" + idt + this.value + end;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-05-14 23:40:04 -04:00
|
|
|
LiteralNode.prototype.toString = function(idt) {
|
2010-03-05 22:52:28 -05:00
|
|
|
return " \"" + this.value + "\"";
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
|
|
|
return LiteralNode;
|
2010-04-10 14:40:05 -04:00
|
|
|
})();
|
2010-02-27 19:19:53 -05:00
|
|
|
exports.ReturnNode = (function() {
|
2010-05-14 23:40:04 -04:00
|
|
|
ReturnNode = function(expression) {
|
2010-05-10 06:58:01 -04:00
|
|
|
this.expression = expression;
|
2010-02-08 22:22:59 -05:00
|
|
|
return this;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-03-30 20:15:51 -04:00
|
|
|
__extends(ReturnNode, BaseNode);
|
2010-06-15 01:03:14 -04:00
|
|
|
ReturnNode.prototype['class'] = 'ReturnNode';
|
2010-06-12 19:05:13 -04:00
|
|
|
ReturnNode.prototype.isStatement = function() {
|
2010-05-31 10:36:50 -04:00
|
|
|
return true;
|
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
ReturnNode.prototype.isPureStatement = function() {
|
2010-05-31 10:36:50 -04:00
|
|
|
return true;
|
|
|
|
};
|
|
|
|
ReturnNode.prototype.children = ['expression'];
|
2010-06-12 19:05:13 -04:00
|
|
|
ReturnNode.prototype.topSensitive = function() {
|
2010-03-25 18:51:24 -04:00
|
|
|
return true;
|
|
|
|
};
|
2010-06-13 01:13:52 -04:00
|
|
|
ReturnNode.prototype.makeReturn = function() {
|
|
|
|
return this;
|
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
ReturnNode.prototype.compileNode = function(o) {
|
2010-03-25 18:51:24 -04:00
|
|
|
var expr;
|
2010-06-12 19:05:13 -04:00
|
|
|
expr = this.expression.makeReturn();
|
2010-03-25 18:51:24 -04:00
|
|
|
if (!(expr instanceof ReturnNode)) {
|
|
|
|
return expr.compile(o);
|
|
|
|
}
|
|
|
|
del(o, 'top');
|
2010-06-12 19:05:13 -04:00
|
|
|
if (this.expression.isStatement()) {
|
|
|
|
o.asStatement = true;
|
2010-03-21 11:28:05 -04:00
|
|
|
}
|
2010-03-23 00:18:50 -04:00
|
|
|
return "" + (this.tab) + "return " + (this.expression.compile(o)) + ";";
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
|
|
|
return ReturnNode;
|
2010-04-10 14:40:05 -04:00
|
|
|
})();
|
2010-02-27 19:19:53 -05:00
|
|
|
exports.ValueNode = (function() {
|
2010-05-14 23:40:04 -04:00
|
|
|
ValueNode = function(base, properties) {
|
2010-05-10 06:58:01 -04:00
|
|
|
this.base = base;
|
|
|
|
this.properties = (properties || []);
|
2010-02-08 22:22:59 -05:00
|
|
|
return this;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-03-30 20:15:51 -04:00
|
|
|
__extends(ValueNode, BaseNode);
|
2010-02-27 19:19:53 -05:00
|
|
|
ValueNode.prototype.SOAK = " == undefined ? undefined : ";
|
2010-06-15 01:03:14 -04:00
|
|
|
ValueNode.prototype['class'] = 'ValueNode';
|
2010-05-31 10:36:50 -04:00
|
|
|
ValueNode.prototype.children = ['base', 'properties'];
|
2010-05-14 23:40:04 -04:00
|
|
|
ValueNode.prototype.push = function(prop) {
|
2010-02-08 22:22:59 -05:00
|
|
|
this.properties.push(prop);
|
2010-02-08 23:42:03 -05:00
|
|
|
return this;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
ValueNode.prototype.hasProperties = function() {
|
2010-02-19 07:51:52 -05:00
|
|
|
return !!this.properties.length;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
ValueNode.prototype.isArray = function() {
|
|
|
|
return this.base instanceof ArrayNode && !this.hasProperties();
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
ValueNode.prototype.isObject = function() {
|
|
|
|
return this.base instanceof ObjectNode && !this.hasProperties();
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
ValueNode.prototype.isSplice = function() {
|
|
|
|
return this.hasProperties() && this.properties[this.properties.length - 1] instanceof SliceNode;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
ValueNode.prototype.makeReturn = function() {
|
|
|
|
if (this.hasProperties()) {
|
|
|
|
return ValueNode.__superClass__.makeReturn.call(this);
|
2010-03-21 11:28:05 -04:00
|
|
|
} else {
|
2010-06-12 19:05:13 -04:00
|
|
|
return this.base.makeReturn();
|
2010-03-21 07:17:58 -04:00
|
|
|
}
|
|
|
|
};
|
2010-05-14 23:40:04 -04:00
|
|
|
ValueNode.prototype.unwrap = function() {
|
2010-03-21 07:17:58 -04:00
|
|
|
if (this.properties.length) {
|
|
|
|
return this;
|
|
|
|
} else {
|
|
|
|
return this.base;
|
|
|
|
}
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
ValueNode.prototype.isStatement = function() {
|
|
|
|
return this.base.isStatement && this.base.isStatement() && !this.hasProperties();
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
ValueNode.prototype.isNumber = function() {
|
2010-05-31 22:56:51 -04:00
|
|
|
return this.base instanceof LiteralNode && this.base.value.match(NUMBER);
|
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
ValueNode.prototype.isStart = function(o) {
|
2010-05-05 05:58:48 -04:00
|
|
|
var node;
|
2010-06-12 19:05:13 -04:00
|
|
|
if (this === o.chainRoot && this.properties[0] instanceof AccessorNode) {
|
2010-05-05 05:58:48 -04:00
|
|
|
return true;
|
|
|
|
}
|
2010-06-12 19:05:13 -04:00
|
|
|
node = o.chainRoot.base || o.chainRoot.variable;
|
2010-05-05 05:58:48 -04:00
|
|
|
while (node instanceof CallNode) {
|
|
|
|
node = node.variable;
|
|
|
|
}
|
|
|
|
return node === this;
|
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
ValueNode.prototype.compileNode = function(o) {
|
2010-05-05 05:58:48 -04:00
|
|
|
var _b, _c, baseline, complete, i, only, op, part, prop, props, temp;
|
2010-06-12 19:05:13 -04:00
|
|
|
only = del(o, 'onlyFirst');
|
2010-03-14 17:58:32 -04:00
|
|
|
op = del(o, 'operation');
|
2010-03-30 20:06:44 -04:00
|
|
|
props = only ? this.properties.slice(0, this.properties.length - 1) : this.properties;
|
2010-06-12 19:05:13 -04:00
|
|
|
o.chainRoot = o.chainRoot || this;
|
2010-02-08 22:22:59 -05:00
|
|
|
baseline = this.base.compile(o);
|
2010-06-12 19:05:13 -04:00
|
|
|
if (this.hasProperties() && (this.base instanceof ObjectNode || this.isNumber())) {
|
2010-04-04 02:59:44 -04:00
|
|
|
baseline = ("(" + baseline + ")");
|
2010-02-15 19:13:08 -05:00
|
|
|
}
|
2010-02-22 22:11:47 -05:00
|
|
|
complete = (this.last = baseline);
|
2010-05-05 05:58:48 -04:00
|
|
|
_b = props;
|
|
|
|
for (i = 0, _c = _b.length; i < _c; i++) {
|
|
|
|
prop = _b[i];
|
2010-02-22 22:11:47 -05:00
|
|
|
this.source = baseline;
|
2010-06-12 19:05:13 -04:00
|
|
|
if (prop.soakNode) {
|
2010-05-05 05:58:48 -04:00
|
|
|
if (this.base instanceof CallNode && i === 0) {
|
2010-06-12 19:05:13 -04:00
|
|
|
temp = o.scope.freeVariable();
|
2010-05-05 05:58:48 -04:00
|
|
|
complete = ("(" + (baseline = temp) + " = (" + complete + "))");
|
2010-02-08 22:22:59 -05:00
|
|
|
}
|
2010-06-12 19:05:13 -04:00
|
|
|
if (i === 0 && this.isStart(o)) {
|
2010-05-05 05:58:48 -04:00
|
|
|
complete = ("typeof " + complete + " === \"undefined\" || " + baseline);
|
2010-02-08 22:22:59 -05:00
|
|
|
}
|
2010-05-05 05:58:48 -04:00
|
|
|
complete += this.SOAK + (baseline += prop.compile(o));
|
2010-02-08 22:22:59 -05:00
|
|
|
} else {
|
|
|
|
part = prop.compile(o);
|
|
|
|
baseline += part;
|
2010-02-22 22:11:47 -05:00
|
|
|
complete += part;
|
|
|
|
this.last = part;
|
2010-02-08 22:22:59 -05:00
|
|
|
}
|
|
|
|
}
|
2010-05-03 09:04:26 -04:00
|
|
|
if (op && this.wrapped) {
|
2010-03-21 07:17:58 -04:00
|
|
|
return "(" + complete + ")";
|
|
|
|
} else {
|
|
|
|
return complete;
|
|
|
|
}
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
|
|
|
return ValueNode;
|
2010-04-10 14:40:05 -04:00
|
|
|
})();
|
2010-02-27 19:19:53 -05:00
|
|
|
exports.CallNode = (function() {
|
2010-05-14 23:40:04 -04:00
|
|
|
CallNode = function(variable, args) {
|
2010-06-12 19:05:13 -04:00
|
|
|
this.isNew = false;
|
|
|
|
this.isSuper = variable === 'super';
|
|
|
|
this.variable = this.isSuper ? null : variable;
|
2010-05-10 06:58:01 -04:00
|
|
|
this.args = (args || []);
|
2010-06-12 19:05:13 -04:00
|
|
|
this.compileSplatArguments = function(o) {
|
|
|
|
return SplatNode.compileMixedArray.call(this, this.args, o);
|
2010-06-12 11:00:56 -04:00
|
|
|
};
|
2010-02-08 22:55:56 -05:00
|
|
|
return this;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-03-30 20:15:51 -04:00
|
|
|
__extends(CallNode, BaseNode);
|
2010-06-15 01:03:14 -04:00
|
|
|
CallNode.prototype['class'] = 'CallNode';
|
2010-05-31 10:36:50 -04:00
|
|
|
CallNode.prototype.children = ['variable', 'args'];
|
2010-06-12 19:05:13 -04:00
|
|
|
CallNode.prototype.newInstance = function() {
|
|
|
|
this.isNew = true;
|
2010-02-08 22:55:56 -05:00
|
|
|
return this;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-05-14 23:40:04 -04:00
|
|
|
CallNode.prototype.prefix = function() {
|
2010-06-12 19:05:13 -04:00
|
|
|
if (this.isNew) {
|
2010-03-21 07:17:58 -04:00
|
|
|
return 'new ';
|
|
|
|
} else {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
CallNode.prototype.superReference = function(o) {
|
2010-04-24 15:57:15 -04:00
|
|
|
var meth, methname;
|
|
|
|
methname = o.scope.method.name;
|
2010-05-02 23:20:51 -04:00
|
|
|
meth = (function() {
|
|
|
|
if (o.scope.method.proto) {
|
|
|
|
return "" + (o.scope.method.proto) + ".__superClass__." + methname;
|
|
|
|
} else if (methname) {
|
|
|
|
return "" + (methname) + ".__superClass__.constructor";
|
|
|
|
} else {
|
|
|
|
throw new Error("cannot call super on an anonymous function.");
|
|
|
|
}
|
|
|
|
})();
|
2010-04-24 15:57:15 -04:00
|
|
|
return meth;
|
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
CallNode.prototype.compileNode = function(o) {
|
2010-05-03 09:04:26 -04:00
|
|
|
var _b, _c, _d, _e, _f, _g, _h, arg, args, compilation;
|
2010-06-12 19:05:13 -04:00
|
|
|
if (!(o.chainRoot)) {
|
|
|
|
o.chainRoot = this;
|
2010-05-03 09:04:26 -04:00
|
|
|
}
|
2010-04-25 22:29:43 -04:00
|
|
|
_c = this.args;
|
|
|
|
for (_b = 0, _d = _c.length; _b < _d; _b++) {
|
|
|
|
arg = _c[_b];
|
2010-06-12 19:05:13 -04:00
|
|
|
arg instanceof SplatNode ? (compilation = this.compileSplat(o)) : null;
|
2010-02-08 22:55:56 -05:00
|
|
|
}
|
2010-05-03 09:04:26 -04:00
|
|
|
if (!(compilation)) {
|
|
|
|
args = (function() {
|
|
|
|
_e = []; _g = this.args;
|
|
|
|
for (_f = 0, _h = _g.length; _f < _h; _f++) {
|
|
|
|
arg = _g[_f];
|
|
|
|
_e.push(arg.compile(o));
|
|
|
|
}
|
|
|
|
return _e;
|
|
|
|
}).call(this).join(', ');
|
2010-06-12 19:05:13 -04:00
|
|
|
compilation = this.isSuper ? this.compileSuper(args, o) : ("" + (this.prefix()) + (this.variable.compile(o)) + "(" + args + ")");
|
2010-05-03 09:04:26 -04:00
|
|
|
}
|
|
|
|
if (o.operation && this.wrapped) {
|
|
|
|
return "(" + compilation + ")";
|
|
|
|
} else {
|
|
|
|
return compilation;
|
2010-02-08 22:55:56 -05:00
|
|
|
}
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
CallNode.prototype.compileSuper = function(args, o) {
|
|
|
|
return "" + (this.superReference(o)) + ".call(this" + (args.length ? ', ' : '') + args + ")";
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
CallNode.prototype.compileSplat = function(o) {
|
2010-03-17 12:25:04 -04:00
|
|
|
var meth, obj, temp;
|
2010-06-12 19:05:13 -04:00
|
|
|
meth = this.variable ? this.variable.compile(o) : this.superReference(o);
|
2010-04-24 15:57:15 -04:00
|
|
|
obj = this.variable && this.variable.source || 'this';
|
2010-02-27 15:15:26 -05:00
|
|
|
if (obj.match(/\(/)) {
|
2010-06-12 19:05:13 -04:00
|
|
|
temp = o.scope.freeVariable();
|
2010-02-27 15:15:26 -05:00
|
|
|
obj = temp;
|
2010-04-04 02:59:44 -04:00
|
|
|
meth = ("(" + temp + " = " + (this.variable.source) + ")" + (this.variable.last));
|
2010-02-27 15:15:26 -05:00
|
|
|
}
|
2010-06-12 19:05:13 -04:00
|
|
|
return "" + (this.prefix()) + (meth) + ".apply(" + obj + ", " + (this.compileSplatArguments(o)) + ")";
|
2010-03-17 12:25:04 -04:00
|
|
|
};
|
2010-02-27 19:19:53 -05:00
|
|
|
return CallNode;
|
2010-04-10 14:40:05 -04:00
|
|
|
})();
|
2010-02-27 19:19:53 -05:00
|
|
|
exports.ExtendsNode = (function() {
|
2010-05-14 23:40:04 -04:00
|
|
|
ExtendsNode = function(child, parent) {
|
2010-05-10 06:58:01 -04:00
|
|
|
this.child = child;
|
|
|
|
this.parent = parent;
|
2010-02-08 23:16:29 -05:00
|
|
|
return this;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-03-30 20:15:51 -04:00
|
|
|
__extends(ExtendsNode, BaseNode);
|
2010-06-15 01:03:14 -04:00
|
|
|
ExtendsNode.prototype['class'] = 'ExtendsNode';
|
2010-05-31 10:36:50 -04:00
|
|
|
ExtendsNode.prototype.children = ['child', 'parent'];
|
2010-06-12 19:05:13 -04:00
|
|
|
ExtendsNode.prototype.compileNode = function(o) {
|
2010-03-30 09:02:51 -04:00
|
|
|
var ref;
|
2010-03-30 20:15:51 -04:00
|
|
|
ref = new ValueNode(literal(utility('extends')));
|
2010-03-30 09:02:51 -04:00
|
|
|
return (new CallNode(ref, [this.child, this.parent])).compile(o);
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
|
|
|
return ExtendsNode;
|
2010-04-10 14:40:05 -04:00
|
|
|
})();
|
2010-02-27 19:19:53 -05:00
|
|
|
exports.AccessorNode = (function() {
|
2010-05-14 23:40:04 -04:00
|
|
|
AccessorNode = function(name, tag) {
|
2010-05-10 06:58:01 -04:00
|
|
|
this.name = name;
|
2010-06-15 00:54:02 -04:00
|
|
|
this.prototype = tag === 'prototype' ? '.prototype' : '';
|
2010-06-12 19:05:13 -04:00
|
|
|
this.soakNode = tag === 'soak';
|
2010-02-08 23:42:03 -05:00
|
|
|
return this;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-03-30 20:15:51 -04:00
|
|
|
__extends(AccessorNode, BaseNode);
|
2010-06-15 01:03:14 -04:00
|
|
|
AccessorNode.prototype['class'] = 'AccessorNode';
|
2010-05-31 10:36:50 -04:00
|
|
|
AccessorNode.prototype.children = ['name'];
|
2010-06-12 19:05:13 -04:00
|
|
|
AccessorNode.prototype.compileNode = function(o) {
|
2010-06-15 00:54:02 -04:00
|
|
|
var name, namePart;
|
|
|
|
name = this.name.compile(o);
|
2010-06-12 19:05:13 -04:00
|
|
|
o.chainRoot.wrapped = o.chainRoot.wrapped || this.soakNode;
|
2010-06-15 00:54:02 -04:00
|
|
|
namePart = name.match(IS_STRING) ? ("[" + name + "]") : ("." + name);
|
|
|
|
return this.prototype + namePart;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
|
|
|
return AccessorNode;
|
2010-04-10 14:40:05 -04:00
|
|
|
})();
|
2010-02-27 19:19:53 -05:00
|
|
|
exports.IndexNode = (function() {
|
2010-05-31 22:32:43 -04:00
|
|
|
IndexNode = function(index) {
|
2010-05-10 06:58:01 -04:00
|
|
|
this.index = index;
|
2010-02-08 23:45:46 -05:00
|
|
|
return this;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-03-30 20:15:51 -04:00
|
|
|
__extends(IndexNode, BaseNode);
|
2010-06-15 01:03:14 -04:00
|
|
|
IndexNode.prototype['class'] = 'IndexNode';
|
2010-05-31 10:36:50 -04:00
|
|
|
IndexNode.prototype.children = ['index'];
|
2010-06-12 19:05:13 -04:00
|
|
|
IndexNode.prototype.compileNode = function(o) {
|
2010-05-31 22:32:43 -04:00
|
|
|
var idx, prefix;
|
2010-06-12 19:05:13 -04:00
|
|
|
o.chainRoot.wrapped = o.chainRoot.wrapped || this.soakNode;
|
2010-03-06 00:23:54 -05:00
|
|
|
idx = this.index.compile(o);
|
2010-05-31 22:32:43 -04:00
|
|
|
prefix = this.proto ? '.prototype' : '';
|
|
|
|
return "" + prefix + "[" + idx + "]";
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
|
|
|
return IndexNode;
|
2010-04-10 14:40:05 -04:00
|
|
|
})();
|
2010-02-27 19:19:53 -05:00
|
|
|
exports.RangeNode = (function() {
|
2010-05-14 23:40:04 -04:00
|
|
|
RangeNode = function(from, to, exclusive) {
|
2010-05-10 06:58:01 -04:00
|
|
|
this.from = from;
|
|
|
|
this.to = to;
|
2010-02-09 19:30:28 -05:00
|
|
|
this.exclusive = !!exclusive;
|
|
|
|
return this;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-03-30 20:15:51 -04:00
|
|
|
__extends(RangeNode, BaseNode);
|
2010-06-15 01:03:14 -04:00
|
|
|
RangeNode.prototype['class'] = 'RangeNode';
|
2010-05-31 10:36:50 -04:00
|
|
|
RangeNode.prototype.children = ['from', 'to'];
|
2010-06-12 19:05:13 -04:00
|
|
|
RangeNode.prototype.compileVariables = function(o) {
|
2010-05-31 19:54:28 -04:00
|
|
|
var _b, _c, parts;
|
2010-06-21 23:51:12 -04:00
|
|
|
_b = this.from.compileReference(o);
|
2010-05-31 19:38:45 -04:00
|
|
|
this.from = _b[0];
|
2010-06-12 19:05:13 -04:00
|
|
|
this.fromVar = _b[1];
|
2010-06-21 23:51:12 -04:00
|
|
|
_c = this.to.compileReference(o);
|
2010-05-31 19:38:45 -04:00
|
|
|
this.to = _c[0];
|
2010-06-12 19:05:13 -04:00
|
|
|
this.toVar = _c[1];
|
2010-05-31 19:38:45 -04:00
|
|
|
parts = [];
|
2010-06-12 19:05:13 -04:00
|
|
|
if (this.from !== this.fromVar) {
|
2010-05-31 19:38:45 -04:00
|
|
|
parts.push(this.from.compile(o));
|
|
|
|
}
|
2010-06-12 19:05:13 -04:00
|
|
|
if (this.to !== this.toVar) {
|
2010-05-31 19:38:45 -04:00
|
|
|
parts.push(this.to.compile(o));
|
|
|
|
}
|
|
|
|
if (parts.length) {
|
2010-05-31 19:54:28 -04:00
|
|
|
return "" + (parts.join('; ')) + ";\n" + o.indent;
|
2010-05-31 19:38:45 -04:00
|
|
|
} else {
|
2010-05-31 19:54:28 -04:00
|
|
|
return '';
|
2010-05-31 19:38:45 -04:00
|
|
|
}
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
RangeNode.prototype.compileNode = function(o) {
|
2010-05-31 19:38:45 -04:00
|
|
|
var equals, idx, op, step, vars;
|
2010-02-09 19:30:28 -05:00
|
|
|
if (!(o.index)) {
|
2010-06-12 19:05:13 -04:00
|
|
|
return this.compileArray(o);
|
2010-02-09 19:30:28 -05:00
|
|
|
}
|
2010-03-14 17:58:32 -04:00
|
|
|
idx = del(o, 'index');
|
|
|
|
step = del(o, 'step');
|
2010-06-12 19:05:13 -04:00
|
|
|
vars = ("" + idx + " = " + (this.fromVar.compile(o)));
|
2010-02-11 20:20:59 -05:00
|
|
|
step = step ? step.compile(o) : '1';
|
2010-02-09 19:30:28 -05:00
|
|
|
equals = this.exclusive ? '' : '=';
|
2010-05-31 19:38:45 -04:00
|
|
|
op = starts(step, '-') ? (">" + equals) : ("<" + equals);
|
2010-06-12 19:05:13 -04:00
|
|
|
return "" + vars + "; " + (idx) + " " + op + " " + (this.toVar.compile(o)) + "; " + idx + " += " + step;
|
2010-05-31 19:38:45 -04:00
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
RangeNode.prototype.compileArray = function(o) {
|
2010-05-31 19:38:45 -04:00
|
|
|
var body, clause, equals, from, idt, post, pre, to, vars;
|
|
|
|
idt = this.idt(1);
|
2010-06-12 19:05:13 -04:00
|
|
|
vars = this.compileVariables(merge(o, {
|
2010-05-31 19:38:45 -04:00
|
|
|
indent: idt
|
|
|
|
}));
|
|
|
|
equals = this.exclusive ? '' : '=';
|
2010-06-12 19:05:13 -04:00
|
|
|
from = this.fromVar.compile(o);
|
|
|
|
to = this.toVar.compile(o);
|
2010-05-31 19:38:45 -04:00
|
|
|
clause = ("" + from + " <= " + to + " ?");
|
|
|
|
pre = ("\n" + (idt) + "a = [];" + (vars));
|
|
|
|
body = ("var i = " + from + "; (" + clause + " i <" + equals + " " + to + " : i >" + equals + " " + to + "); (" + clause + " i += 1 : i -= 1)");
|
|
|
|
post = ("a.push(i);\n" + (idt) + "return a;\n" + o.indent);
|
|
|
|
return "(function(){" + (pre) + "for (" + body + ") " + post + "}).call(this)";
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
|
|
|
return RangeNode;
|
2010-04-10 14:40:05 -04:00
|
|
|
})();
|
2010-02-27 19:19:53 -05:00
|
|
|
exports.SliceNode = (function() {
|
2010-05-14 23:40:04 -04:00
|
|
|
SliceNode = function(range) {
|
2010-05-10 06:58:01 -04:00
|
|
|
this.range = range;
|
2010-02-09 19:30:28 -05:00
|
|
|
return this;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-03-30 20:15:51 -04:00
|
|
|
__extends(SliceNode, BaseNode);
|
2010-06-15 01:03:14 -04:00
|
|
|
SliceNode.prototype['class'] = 'SliceNode';
|
2010-05-31 10:36:50 -04:00
|
|
|
SliceNode.prototype.children = ['range'];
|
2010-06-12 19:05:13 -04:00
|
|
|
SliceNode.prototype.compileNode = function(o) {
|
|
|
|
var from, plusPart, to;
|
2010-03-21 12:11:44 -04:00
|
|
|
from = this.range.from.compile(o);
|
|
|
|
to = this.range.to.compile(o);
|
2010-06-12 19:05:13 -04:00
|
|
|
plusPart = this.range.exclusive ? '' : ' + 1';
|
|
|
|
return ".slice(" + from + ", " + to + plusPart + ")";
|
2010-03-21 12:11:44 -04:00
|
|
|
};
|
2010-02-27 19:19:53 -05:00
|
|
|
return SliceNode;
|
2010-04-10 14:40:05 -04:00
|
|
|
})();
|
2010-02-27 19:19:53 -05:00
|
|
|
exports.ObjectNode = (function() {
|
2010-05-14 23:40:04 -04:00
|
|
|
ObjectNode = function(props) {
|
2010-05-10 06:58:01 -04:00
|
|
|
this.objects = (this.properties = props || []);
|
2010-02-09 20:53:25 -05:00
|
|
|
return this;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-03-30 20:15:51 -04:00
|
|
|
__extends(ObjectNode, BaseNode);
|
2010-06-15 01:03:14 -04:00
|
|
|
ObjectNode.prototype['class'] = 'ObjectNode';
|
2010-05-31 10:36:50 -04:00
|
|
|
ObjectNode.prototype.children = ['properties'];
|
2010-06-12 19:05:13 -04:00
|
|
|
ObjectNode.prototype.compileNode = function(o) {
|
2010-06-27 12:59:54 -04:00
|
|
|
var _b, _c, _d, i, inner, join, last, prop, props;
|
2010-02-09 20:53:25 -05:00
|
|
|
o.indent = this.idt(1);
|
2010-06-27 12:59:54 -04:00
|
|
|
last = this.properties.length - 1;
|
2010-02-09 20:53:25 -05:00
|
|
|
props = (function() {
|
2010-06-27 12:59:54 -04:00
|
|
|
_b = []; _c = this.properties;
|
|
|
|
for (i = 0, _d = _c.length; i < _d; i++) {
|
|
|
|
prop = _c[i];
|
|
|
|
_b.push((function() {
|
|
|
|
join = i === last ? '' : ',\n';
|
|
|
|
if (!(prop instanceof AssignNode)) {
|
2010-04-25 23:54:47 -04:00
|
|
|
prop = new AssignNode(prop, prop, 'object');
|
|
|
|
}
|
2010-06-27 12:59:54 -04:00
|
|
|
return this.idt(1) + prop.compile(o) + join;
|
2010-02-09 20:53:25 -05:00
|
|
|
}).call(this));
|
|
|
|
}
|
2010-06-27 12:59:54 -04:00
|
|
|
return _b;
|
2010-02-09 20:53:25 -05:00
|
|
|
}).call(this);
|
2010-02-11 23:57:31 -05:00
|
|
|
props = props.join('');
|
|
|
|
inner = props ? '\n' + props + '\n' + this.idt() : '';
|
2010-03-06 00:23:54 -05:00
|
|
|
return "{" + inner + "}";
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
|
|
|
return ObjectNode;
|
2010-04-10 14:40:05 -04:00
|
|
|
})();
|
2010-03-07 19:11:03 -05:00
|
|
|
exports.ArrayNode = (function() {
|
2010-05-14 23:40:04 -04:00
|
|
|
ArrayNode = function(objects) {
|
2010-05-10 06:58:01 -04:00
|
|
|
this.objects = objects || [];
|
2010-06-12 19:05:13 -04:00
|
|
|
this.compileSplatLiteral = function(o) {
|
|
|
|
return SplatNode.compileMixedArray.call(this, this.objects, o);
|
2010-06-12 11:00:56 -04:00
|
|
|
};
|
2010-03-07 19:11:03 -05:00
|
|
|
return this;
|
|
|
|
};
|
2010-03-30 20:15:51 -04:00
|
|
|
__extends(ArrayNode, BaseNode);
|
2010-06-15 01:03:14 -04:00
|
|
|
ArrayNode.prototype['class'] = 'ArrayNode';
|
2010-05-31 10:36:50 -04:00
|
|
|
ArrayNode.prototype.children = ['objects'];
|
2010-06-12 19:05:13 -04:00
|
|
|
ArrayNode.prototype.compileNode = function(o) {
|
2010-04-26 23:46:35 -04:00
|
|
|
var _b, _c, code, i, obj, objects;
|
2010-03-07 19:11:03 -05:00
|
|
|
o.indent = this.idt(1);
|
2010-03-21 11:28:05 -04:00
|
|
|
objects = [];
|
2010-04-25 22:29:43 -04:00
|
|
|
_b = this.objects;
|
|
|
|
for (i = 0, _c = _b.length; i < _c; i++) {
|
|
|
|
obj = _b[i];
|
2010-03-21 11:28:05 -04:00
|
|
|
code = obj.compile(o);
|
|
|
|
if (obj instanceof SplatNode) {
|
2010-06-12 19:05:13 -04:00
|
|
|
return this.compileSplatLiteral(this.objects, o);
|
2010-03-21 11:28:05 -04:00
|
|
|
} else if (i === this.objects.length - 1) {
|
|
|
|
objects.push(code);
|
|
|
|
} else {
|
2010-04-04 02:59:44 -04:00
|
|
|
objects.push(("" + code + ", "));
|
2010-03-07 19:11:03 -05:00
|
|
|
}
|
2010-03-21 11:28:05 -04:00
|
|
|
}
|
2010-03-07 19:11:03 -05:00
|
|
|
objects = objects.join('');
|
2010-06-12 19:05:13 -04:00
|
|
|
if (indexOf(objects, '\n') >= 0) {
|
2010-04-26 23:46:35 -04:00
|
|
|
return "[\n" + (this.idt(1)) + objects + "\n" + this.tab + "]";
|
|
|
|
} else {
|
|
|
|
return "[" + objects + "]";
|
|
|
|
}
|
2010-03-07 19:11:03 -05:00
|
|
|
};
|
|
|
|
return ArrayNode;
|
2010-04-10 14:40:05 -04:00
|
|
|
})();
|
2010-02-27 19:19:53 -05:00
|
|
|
exports.ClassNode = (function() {
|
2010-05-14 23:40:04 -04:00
|
|
|
ClassNode = function(variable, parent, props) {
|
2010-05-10 06:58:01 -04:00
|
|
|
this.variable = variable;
|
|
|
|
this.parent = parent;
|
|
|
|
this.properties = props || [];
|
2010-03-21 11:28:05 -04:00
|
|
|
this.returns = false;
|
2010-02-27 18:57:45 -05:00
|
|
|
return this;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-03-30 20:15:51 -04:00
|
|
|
__extends(ClassNode, BaseNode);
|
2010-06-15 01:03:14 -04:00
|
|
|
ClassNode.prototype['class'] = 'ClassNode';
|
2010-05-31 10:36:50 -04:00
|
|
|
ClassNode.prototype.children = ['variable', 'parent', 'properties'];
|
2010-06-12 19:05:13 -04:00
|
|
|
ClassNode.prototype.isStatement = function() {
|
2010-05-31 10:36:50 -04:00
|
|
|
return true;
|
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
ClassNode.prototype.makeReturn = function() {
|
2010-03-21 11:28:05 -04:00
|
|
|
this.returns = true;
|
2010-03-21 07:17:58 -04:00
|
|
|
return this;
|
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
ClassNode.prototype.compileNode = function(o) {
|
2010-06-16 06:21:42 -04:00
|
|
|
var _b, _c, _d, _e, access, applied, className, constScope, construct, constructor, extension, func, me, pname, prop, props, pvar, returns, val;
|
2010-02-27 18:57:45 -05:00
|
|
|
extension = this.parent && new ExtendsNode(this.variable, this.parent);
|
|
|
|
props = new Expressions();
|
|
|
|
o.top = true;
|
2010-06-15 02:21:01 -04:00
|
|
|
me = null;
|
|
|
|
className = this.variable.compile(o);
|
|
|
|
constScope = null;
|
2010-06-15 01:28:30 -04:00
|
|
|
if (this.parent) {
|
|
|
|
applied = new ValueNode(this.parent, [new AccessorNode(literal('apply'))]);
|
|
|
|
constructor = new CodeNode([], new Expressions([new CallNode(applied, [literal('this'), literal('arguments')])]));
|
|
|
|
} else {
|
|
|
|
constructor = new CodeNode();
|
|
|
|
}
|
2010-04-25 22:29:43 -04:00
|
|
|
_c = this.properties;
|
|
|
|
for (_b = 0, _d = _c.length; _b < _d; _b++) {
|
|
|
|
prop = _c[_b];
|
|
|
|
_e = [prop.variable, prop.value];
|
|
|
|
pvar = _e[0];
|
|
|
|
func = _e[1];
|
2010-04-03 14:53:26 -04:00
|
|
|
if (pvar && pvar.base.value === 'constructor' && func instanceof CodeNode) {
|
2010-06-15 02:21:01 -04:00
|
|
|
func.name = className;
|
2010-02-28 00:22:06 -05:00
|
|
|
func.body.push(new ReturnNode(literal('this')));
|
2010-05-25 02:15:46 -04:00
|
|
|
this.variable = new ValueNode(this.variable);
|
|
|
|
this.variable.namespaced = include(func.name, '.');
|
2010-06-15 01:28:30 -04:00
|
|
|
constructor = func;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (func instanceof CodeNode && func.bound) {
|
2010-06-15 02:21:01 -04:00
|
|
|
func.bound = false;
|
|
|
|
constScope = constScope || new Scope(o.scope, constructor.body, constructor);
|
|
|
|
me = me || constScope.freeVariable();
|
|
|
|
pname = pvar.compile(o);
|
2010-06-15 01:28:30 -04:00
|
|
|
if (constructor.body.empty()) {
|
|
|
|
constructor.body.push(new ReturnNode(literal('this')));
|
|
|
|
}
|
2010-06-15 02:21:01 -04:00
|
|
|
constructor.body.unshift(literal(("this." + (pname) + " = function(){ return " + (className) + ".prototype." + (pname) + ".apply(" + me + ", arguments); }")));
|
2010-02-27 18:57:45 -05:00
|
|
|
}
|
2010-06-15 02:21:01 -04:00
|
|
|
if (pvar) {
|
|
|
|
access = prop.context === 'this' ? pvar.base.properties[0] : new AccessorNode(pvar, 'prototype');
|
|
|
|
val = new ValueNode(this.variable, [access]);
|
|
|
|
prop = new AssignNode(val, func);
|
|
|
|
}
|
|
|
|
props.push(prop);
|
|
|
|
}
|
|
|
|
if (me) {
|
|
|
|
constructor.body.unshift(literal(("" + me + " = this")));
|
2010-02-27 18:57:45 -05:00
|
|
|
}
|
2010-06-15 02:21:01 -04:00
|
|
|
construct = this.idt() + (new AssignNode(this.variable, constructor)).compile(merge(o, {
|
|
|
|
sharedScope: constScope
|
|
|
|
})) + ';\n';
|
2010-02-27 18:57:45 -05:00
|
|
|
props = props.empty() ? '' : props.compile(o) + '\n';
|
2010-02-27 19:29:34 -05:00
|
|
|
extension = extension ? this.idt() + extension.compile(o) + ';\n' : '';
|
2010-03-21 11:28:05 -04:00
|
|
|
returns = this.returns ? new ReturnNode(this.variable).compile(o) : '';
|
2010-03-23 00:18:50 -04:00
|
|
|
return "" + construct + extension + props + returns;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
|
|
|
return ClassNode;
|
2010-04-10 14:40:05 -04:00
|
|
|
})();
|
2010-02-27 19:19:53 -05:00
|
|
|
exports.AssignNode = (function() {
|
2010-05-14 23:40:04 -04:00
|
|
|
AssignNode = function(variable, value, context) {
|
2010-05-10 06:58:01 -04:00
|
|
|
this.variable = variable;
|
|
|
|
this.value = value;
|
2010-02-09 20:53:25 -05:00
|
|
|
this.context = context;
|
|
|
|
return this;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-03-30 20:15:51 -04:00
|
|
|
__extends(AssignNode, BaseNode);
|
2010-02-27 19:19:53 -05:00
|
|
|
AssignNode.prototype.PROTO_ASSIGN = /^(\S+)\.prototype/;
|
|
|
|
AssignNode.prototype.LEADING_DOT = /^\.(prototype\.)?/;
|
2010-06-15 01:03:14 -04:00
|
|
|
AssignNode.prototype['class'] = 'AssignNode';
|
2010-05-31 10:36:50 -04:00
|
|
|
AssignNode.prototype.children = ['variable', 'value'];
|
2010-06-12 19:05:13 -04:00
|
|
|
AssignNode.prototype.topSensitive = function() {
|
2010-02-09 20:53:25 -05:00
|
|
|
return true;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
AssignNode.prototype.isValue = function() {
|
2010-02-09 20:53:25 -05:00
|
|
|
return this.variable instanceof ValueNode;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
AssignNode.prototype.makeReturn = function() {
|
2010-03-21 07:17:58 -04:00
|
|
|
return new Expressions([this, new ReturnNode(this.variable)]);
|
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
AssignNode.prototype.isStatement = function() {
|
|
|
|
return this.isValue() && (this.variable.isArray() || this.variable.isObject());
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
AssignNode.prototype.compileNode = function(o) {
|
2010-02-09 20:53:25 -05:00
|
|
|
var last, match, name, proto, stmt, top, val;
|
2010-03-14 17:58:32 -04:00
|
|
|
top = del(o, 'top');
|
2010-06-12 19:05:13 -04:00
|
|
|
if (this.isStatement()) {
|
|
|
|
return this.compilePatternMatch(o);
|
2010-02-09 20:53:25 -05:00
|
|
|
}
|
2010-06-12 19:05:13 -04:00
|
|
|
if (this.isValue() && this.variable.isSplice()) {
|
|
|
|
return this.compileSplice(o);
|
2010-02-09 20:53:25 -05:00
|
|
|
}
|
2010-06-12 19:05:13 -04:00
|
|
|
stmt = del(o, 'asStatement');
|
2010-02-09 20:53:25 -05:00
|
|
|
name = this.variable.compile(o);
|
2010-06-12 19:05:13 -04:00
|
|
|
last = this.isValue() ? this.variable.last.replace(this.LEADING_DOT, '') : name;
|
2010-02-09 20:53:25 -05:00
|
|
|
match = name.match(this.PROTO_ASSIGN);
|
|
|
|
proto = match && match[1];
|
|
|
|
if (this.value instanceof CodeNode) {
|
2010-02-10 00:05:56 -05:00
|
|
|
if (last.match(IDENTIFIER)) {
|
2010-02-09 20:53:25 -05:00
|
|
|
this.value.name = last;
|
|
|
|
}
|
|
|
|
if (proto) {
|
|
|
|
this.value.proto = proto;
|
|
|
|
}
|
|
|
|
}
|
2010-03-06 00:23:54 -05:00
|
|
|
val = this.value.compile(o);
|
2010-02-09 20:53:25 -05:00
|
|
|
if (this.context === 'object') {
|
2010-04-04 02:59:44 -04:00
|
|
|
return ("" + name + ": " + val);
|
2010-02-09 20:53:25 -05:00
|
|
|
}
|
2010-06-12 19:05:13 -04:00
|
|
|
if (!(this.isValue() && (this.variable.hasProperties() || this.variable.namespaced))) {
|
2010-02-09 20:53:25 -05:00
|
|
|
o.scope.find(name);
|
|
|
|
}
|
2010-04-04 02:59:44 -04:00
|
|
|
val = ("" + name + " = " + val);
|
2010-02-09 20:53:25 -05:00
|
|
|
if (stmt) {
|
2010-04-04 02:59:44 -04:00
|
|
|
return ("" + this.tab + val + ";");
|
2010-02-09 20:53:25 -05:00
|
|
|
}
|
2010-03-21 11:28:05 -04:00
|
|
|
if (top) {
|
|
|
|
return val;
|
|
|
|
} else {
|
|
|
|
return "(" + val + ")";
|
2010-02-09 20:53:25 -05:00
|
|
|
}
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
AssignNode.prototype.compilePatternMatch = function(o) {
|
|
|
|
var _b, _c, _d, accessClass, assigns, code, i, idx, isString, obj, oindex, olength, splat, val, valVar, value;
|
|
|
|
valVar = o.scope.freeVariable();
|
|
|
|
value = this.value.isStatement() ? ClosureNode.wrap(this.value) : this.value;
|
|
|
|
assigns = [("" + this.tab + valVar + " = " + (value.compile(o)) + ";")];
|
2010-02-09 20:53:25 -05:00
|
|
|
o.top = true;
|
2010-06-12 19:05:13 -04:00
|
|
|
o.asStatement = true;
|
2010-03-22 02:02:04 -04:00
|
|
|
splat = false;
|
2010-04-25 22:29:43 -04:00
|
|
|
_b = this.variable.base.objects;
|
|
|
|
for (i = 0, _c = _b.length; i < _c; i++) {
|
|
|
|
obj = _b[i];
|
2010-02-11 18:44:00 -05:00
|
|
|
idx = i;
|
2010-06-12 19:05:13 -04:00
|
|
|
if (this.variable.isObject()) {
|
2010-04-25 22:21:53 -04:00
|
|
|
if (obj instanceof AssignNode) {
|
2010-04-25 22:29:43 -04:00
|
|
|
_d = [obj.value, obj.variable.base];
|
|
|
|
obj = _d[0];
|
|
|
|
idx = _d[1];
|
2010-04-25 22:21:53 -04:00
|
|
|
} else {
|
|
|
|
idx = obj;
|
|
|
|
}
|
2010-02-09 20:53:25 -05:00
|
|
|
}
|
2010-04-25 11:22:15 -04:00
|
|
|
if (!(obj instanceof ValueNode || obj instanceof SplatNode)) {
|
|
|
|
throw new Error('pattern matching must use only identifiers on the left-hand side.');
|
|
|
|
}
|
2010-06-12 19:05:13 -04:00
|
|
|
isString = idx.value && idx.value.match(IS_STRING);
|
|
|
|
accessClass = isString || this.variable.isArray() ? IndexNode : AccessorNode;
|
2010-03-22 02:02:04 -04:00
|
|
|
if (obj instanceof SplatNode && !splat) {
|
2010-06-12 19:05:13 -04:00
|
|
|
val = literal(obj.compileValue(o, valVar, (oindex = indexOf(this.variable.base.objects, obj)), (olength = this.variable.base.objects.length) - oindex - 1));
|
2010-03-22 02:02:04 -04:00
|
|
|
splat = true;
|
2010-02-11 20:11:11 -05:00
|
|
|
} else {
|
2010-03-22 02:02:04 -04:00
|
|
|
if (typeof idx !== 'object') {
|
2010-06-12 19:05:13 -04:00
|
|
|
idx = literal(splat ? ("" + (valVar) + ".length - " + (olength - idx)) : idx);
|
2010-03-22 02:02:04 -04:00
|
|
|
}
|
2010-06-12 19:05:13 -04:00
|
|
|
val = new ValueNode(literal(valVar), [new accessClass(idx)]);
|
2010-02-11 20:11:11 -05:00
|
|
|
}
|
2010-02-09 20:53:25 -05:00
|
|
|
assigns.push(new AssignNode(obj, val).compile(o));
|
|
|
|
}
|
2010-02-25 00:43:02 -05:00
|
|
|
code = assigns.join("\n");
|
|
|
|
return code;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
AssignNode.prototype.compileSplice = function(o) {
|
2010-03-30 20:06:44 -04:00
|
|
|
var from, l, name, plus, range, to, val;
|
|
|
|
name = this.variable.compile(merge(o, {
|
2010-06-12 19:05:13 -04:00
|
|
|
onlyFirst: true
|
2010-02-09 20:53:25 -05:00
|
|
|
}));
|
2010-03-30 20:06:44 -04:00
|
|
|
l = this.variable.properties.length;
|
|
|
|
range = this.variable.properties[l - 1].range;
|
|
|
|
plus = range.exclusive ? '' : ' + 1';
|
|
|
|
from = range.from.compile(o);
|
|
|
|
to = range.to.compile(o) + ' - ' + from + plus;
|
|
|
|
val = this.value.compile(o);
|
|
|
|
return "" + (name) + ".splice.apply(" + name + ", [" + from + ", " + to + "].concat(" + val + "))";
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
|
|
|
return AssignNode;
|
2010-04-10 14:40:05 -04:00
|
|
|
})();
|
2010-02-27 19:19:53 -05:00
|
|
|
exports.CodeNode = (function() {
|
2010-05-14 23:40:04 -04:00
|
|
|
CodeNode = function(params, body, tag) {
|
2010-02-27 18:57:45 -05:00
|
|
|
this.params = params || [];
|
|
|
|
this.body = body || new Expressions();
|
2010-02-09 21:31:07 -05:00
|
|
|
this.bound = tag === 'boundfunc';
|
|
|
|
return this;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-03-30 20:15:51 -04:00
|
|
|
__extends(CodeNode, BaseNode);
|
2010-06-15 01:03:14 -04:00
|
|
|
CodeNode.prototype['class'] = 'CodeNode';
|
2010-05-31 10:36:50 -04:00
|
|
|
CodeNode.prototype.children = ['params', 'body'];
|
2010-06-12 19:05:13 -04:00
|
|
|
CodeNode.prototype.compileNode = function(o) {
|
|
|
|
var _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, code, func, i, inner, param, params, sharedScope, splat, top;
|
|
|
|
sharedScope = del(o, 'sharedScope');
|
2010-03-14 17:58:32 -04:00
|
|
|
top = del(o, 'top');
|
2010-06-12 19:05:13 -04:00
|
|
|
o.scope = sharedScope || new Scope(o.scope, this.body, this);
|
2010-02-09 21:31:07 -05:00
|
|
|
o.top = true;
|
|
|
|
o.indent = this.idt(this.bound ? 2 : 1);
|
2010-06-12 19:05:13 -04:00
|
|
|
del(o, 'noWrap');
|
2010-03-14 17:58:32 -04:00
|
|
|
del(o, 'globals');
|
2010-03-17 14:41:09 -04:00
|
|
|
i = 0;
|
|
|
|
splat = undefined;
|
|
|
|
params = [];
|
2010-04-25 22:29:43 -04:00
|
|
|
_c = this.params;
|
|
|
|
for (_b = 0, _d = _c.length; _b < _d; _b++) {
|
|
|
|
param = _c[_b];
|
2010-03-17 14:41:09 -04:00
|
|
|
if (param instanceof SplatNode && !(typeof splat !== "undefined" && splat !== null)) {
|
|
|
|
splat = param;
|
|
|
|
splat.index = i;
|
|
|
|
splat.trailings = [];
|
2010-05-08 12:44:54 -04:00
|
|
|
splat.arglength = this.params.length;
|
|
|
|
this.body.unshift(splat);
|
2010-03-17 14:41:09 -04:00
|
|
|
} else if ((typeof splat !== "undefined" && splat !== null)) {
|
|
|
|
splat.trailings.push(param);
|
|
|
|
} else {
|
|
|
|
params.push(param);
|
|
|
|
}
|
|
|
|
i += 1;
|
2010-02-09 21:31:07 -05:00
|
|
|
}
|
2010-02-17 19:19:51 -05:00
|
|
|
params = (function() {
|
2010-04-25 22:29:43 -04:00
|
|
|
_e = []; _g = params;
|
|
|
|
for (_f = 0, _h = _g.length; _f < _h; _f++) {
|
|
|
|
param = _g[_f];
|
|
|
|
_e.push(param.compile(o));
|
2010-02-12 13:35:34 -05:00
|
|
|
}
|
2010-04-25 22:29:43 -04:00
|
|
|
return _e;
|
2010-04-10 14:40:05 -04:00
|
|
|
})();
|
2010-06-12 19:05:13 -04:00
|
|
|
this.body.makeReturn();
|
2010-04-25 22:29:43 -04:00
|
|
|
_j = params;
|
|
|
|
for (_i = 0, _k = _j.length; _i < _k; _i++) {
|
|
|
|
param = _j[_i];
|
2010-02-09 21:31:07 -05:00
|
|
|
(o.scope.parameter(param));
|
|
|
|
}
|
2010-06-12 19:05:13 -04:00
|
|
|
code = this.body.expressions.length ? ("\n" + (this.body.compileWithDeclarations(o)) + "\n") : '';
|
2010-05-14 23:40:04 -04:00
|
|
|
func = ("function(" + (params.join(', ')) + ") {" + code + (this.idt(this.bound ? 1 : 0)) + "}");
|
2010-02-09 21:31:07 -05:00
|
|
|
if (top && !this.bound) {
|
2010-04-04 02:59:44 -04:00
|
|
|
func = ("(" + func + ")");
|
2010-02-09 21:31:07 -05:00
|
|
|
}
|
|
|
|
if (!(this.bound)) {
|
|
|
|
return func;
|
|
|
|
}
|
2010-06-12 11:00:56 -04:00
|
|
|
inner = ("(function() {\n" + (this.idt(2)) + "return __func.apply(__this, arguments);\n" + (this.idt(1)) + "});");
|
|
|
|
return "(function(__this) {\n" + (this.idt(1)) + "var __func = " + func + ";\n" + (this.idt(1)) + "return " + inner + "\n" + this.tab + "})(this)";
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
CodeNode.prototype.topSensitive = function() {
|
2010-02-09 21:31:07 -05:00
|
|
|
return true;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
CodeNode.prototype.traverseChildren = function(crossScope, func) {
|
|
|
|
if (crossScope) {
|
|
|
|
return CodeNode.__superClass__.traverseChildren.call(this, crossScope, func);
|
2010-02-27 01:22:21 -05:00
|
|
|
}
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-05-14 23:40:04 -04:00
|
|
|
CodeNode.prototype.toString = function(idt) {
|
2010-05-19 15:50:29 -04:00
|
|
|
var _b, _c, _d, _e, child, children;
|
2010-02-13 09:51:52 -05:00
|
|
|
idt = idt || '';
|
2010-03-06 00:23:54 -05:00
|
|
|
children = (function() {
|
2010-06-12 19:05:13 -04:00
|
|
|
_b = []; _d = this.collectChildren();
|
2010-04-25 22:29:43 -04:00
|
|
|
for (_c = 0, _e = _d.length; _c < _e; _c++) {
|
|
|
|
child = _d[_c];
|
|
|
|
_b.push(child.toString(idt + TAB));
|
2010-02-16 18:38:03 -05:00
|
|
|
}
|
2010-04-25 22:29:43 -04:00
|
|
|
return _b;
|
2010-02-17 19:19:51 -05:00
|
|
|
}).call(this).join('');
|
2010-03-06 00:23:54 -05:00
|
|
|
return "\n" + idt + children;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
|
|
|
return CodeNode;
|
2010-04-10 14:40:05 -04:00
|
|
|
})();
|
2010-02-27 19:19:53 -05:00
|
|
|
exports.SplatNode = (function() {
|
2010-05-14 23:40:04 -04:00
|
|
|
SplatNode = function(name) {
|
2010-02-11 23:57:31 -05:00
|
|
|
if (!(name.compile)) {
|
2010-02-28 00:22:06 -05:00
|
|
|
name = literal(name);
|
2010-02-11 23:57:31 -05:00
|
|
|
}
|
2010-05-10 06:58:01 -04:00
|
|
|
this.name = name;
|
2010-02-09 21:44:34 -05:00
|
|
|
return this;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-03-30 20:15:51 -04:00
|
|
|
__extends(SplatNode, BaseNode);
|
2010-06-15 01:03:14 -04:00
|
|
|
SplatNode.prototype['class'] = 'SplatNode';
|
2010-05-31 10:36:50 -04:00
|
|
|
SplatNode.prototype.children = ['name'];
|
2010-06-12 19:05:13 -04:00
|
|
|
SplatNode.prototype.compileNode = function(o) {
|
2010-04-25 22:29:43 -04:00
|
|
|
var _b;
|
|
|
|
if ((typeof (_b = this.index) !== "undefined" && _b !== null)) {
|
2010-06-12 19:05:13 -04:00
|
|
|
return this.compileParam(o);
|
2010-03-21 07:17:58 -04:00
|
|
|
} else {
|
|
|
|
return this.name.compile(o);
|
|
|
|
}
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
SplatNode.prototype.compileParam = function(o) {
|
2010-05-08 12:44:54 -04:00
|
|
|
var _b, _c, idx, len, name, pos, trailing, variadic;
|
2010-02-11 20:11:11 -05:00
|
|
|
name = this.name.compile(o);
|
|
|
|
o.scope.find(name);
|
2010-06-12 19:05:13 -04:00
|
|
|
len = o.scope.freeVariable();
|
2010-05-08 12:20:14 -04:00
|
|
|
o.scope.assign(len, "arguments.length");
|
2010-06-12 19:05:13 -04:00
|
|
|
variadic = o.scope.freeVariable();
|
2010-05-08 12:44:54 -04:00
|
|
|
o.scope.assign(variadic, ("" + len + " >= " + this.arglength));
|
2010-05-08 12:15:47 -04:00
|
|
|
_b = this.trailings;
|
2010-05-08 12:44:54 -04:00
|
|
|
for (idx = 0, _c = _b.length; idx < _c; idx++) {
|
|
|
|
trailing = _b[idx];
|
|
|
|
pos = this.trailings.length - idx;
|
|
|
|
o.scope.assign(trailing.compile(o), ("arguments[" + variadic + " ? " + len + " - " + pos + " : " + (this.index + idx) + "]"));
|
2010-03-17 14:41:09 -04:00
|
|
|
}
|
2010-05-08 12:20:14 -04:00
|
|
|
return "" + name + " = " + (utility('slice')) + ".call(arguments, " + this.index + ", " + len + " - " + (this.trailings.length) + ")";
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
SplatNode.prototype.compileValue = function(o, name, index, trailings) {
|
2010-03-30 19:42:09 -04:00
|
|
|
var trail;
|
2010-04-04 02:59:44 -04:00
|
|
|
trail = trailings ? (", " + (name) + ".length - " + trailings) : '';
|
2010-03-30 19:48:37 -04:00
|
|
|
return "" + (utility('slice')) + ".call(" + name + ", " + index + trail + ")";
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
SplatNode.compileMixedArray = function(list, o) {
|
2010-04-25 22:29:43 -04:00
|
|
|
var _b, _c, _d, arg, args, code, i, prev;
|
2010-03-29 21:49:20 -04:00
|
|
|
args = [];
|
|
|
|
i = 0;
|
2010-04-25 22:29:43 -04:00
|
|
|
_c = list;
|
|
|
|
for (_b = 0, _d = _c.length; _b < _d; _b++) {
|
|
|
|
arg = _c[_b];
|
2010-03-29 21:49:20 -04:00
|
|
|
code = arg.compile(o);
|
|
|
|
if (!(arg instanceof SplatNode)) {
|
|
|
|
prev = args[i - 1];
|
|
|
|
if (i === 1 && prev.substr(0, 1) === '[' && prev.substr(prev.length - 1, 1) === ']') {
|
2010-04-04 02:59:44 -04:00
|
|
|
args[i - 1] = ("" + (prev.substr(0, prev.length - 1)) + ", " + code + "]");
|
2010-03-29 21:49:20 -04:00
|
|
|
continue;
|
|
|
|
} else if (i > 1 && prev.substr(0, 9) === '.concat([' && prev.substr(prev.length - 2, 2) === '])') {
|
2010-04-04 02:59:44 -04:00
|
|
|
args[i - 1] = ("" + (prev.substr(0, prev.length - 2)) + ", " + code + "])");
|
2010-03-29 21:49:20 -04:00
|
|
|
continue;
|
|
|
|
} else {
|
2010-04-04 02:59:44 -04:00
|
|
|
code = ("[" + code + "]");
|
2010-03-29 21:49:20 -04:00
|
|
|
}
|
2010-03-18 09:27:13 -04:00
|
|
|
}
|
2010-04-04 02:59:44 -04:00
|
|
|
args.push(i === 0 ? code : (".concat(" + code + ")"));
|
2010-03-29 21:49:20 -04:00
|
|
|
i += 1;
|
2010-03-18 09:27:13 -04:00
|
|
|
}
|
2010-03-29 21:49:20 -04:00
|
|
|
return args.join('');
|
|
|
|
};
|
|
|
|
return SplatNode;
|
|
|
|
}).call(this);
|
2010-02-27 19:19:53 -05:00
|
|
|
exports.WhileNode = (function() {
|
2010-05-14 23:40:04 -04:00
|
|
|
WhileNode = function(condition, opts) {
|
2010-04-28 22:08:00 -04:00
|
|
|
if (opts && opts.invert) {
|
2010-05-15 01:18:05 -04:00
|
|
|
if (condition instanceof OpNode) {
|
|
|
|
condition = new ParentheticalNode(condition);
|
|
|
|
}
|
2010-04-28 22:08:00 -04:00
|
|
|
condition = new OpNode('!', condition);
|
|
|
|
}
|
2010-05-10 06:58:01 -04:00
|
|
|
this.condition = condition;
|
2010-04-28 21:46:47 -04:00
|
|
|
this.guard = opts && opts.guard;
|
2010-02-23 22:53:43 -05:00
|
|
|
return this;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-03-30 20:15:51 -04:00
|
|
|
__extends(WhileNode, BaseNode);
|
2010-06-15 01:03:14 -04:00
|
|
|
WhileNode.prototype['class'] = 'WhileNode';
|
2010-05-31 10:36:50 -04:00
|
|
|
WhileNode.prototype.children = ['condition', 'guard', 'body'];
|
2010-06-12 19:05:13 -04:00
|
|
|
WhileNode.prototype.isStatement = function() {
|
2010-05-31 10:36:50 -04:00
|
|
|
return true;
|
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
WhileNode.prototype.addBody = function(body) {
|
2010-05-10 06:58:01 -04:00
|
|
|
this.body = body;
|
2010-02-09 22:05:17 -05:00
|
|
|
return this;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
WhileNode.prototype.makeReturn = function() {
|
2010-03-21 11:28:05 -04:00
|
|
|
this.returns = true;
|
2010-03-21 07:17:58 -04:00
|
|
|
return this;
|
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
WhileNode.prototype.topSensitive = function() {
|
2010-02-09 22:05:17 -05:00
|
|
|
return true;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
WhileNode.prototype.compileNode = function(o) {
|
2010-03-21 07:17:58 -04:00
|
|
|
var cond, post, pre, rvar, set, top;
|
2010-03-21 11:28:05 -04:00
|
|
|
top = del(o, 'top') && !this.returns;
|
2010-02-09 22:05:17 -05:00
|
|
|
o.indent = this.idt(1);
|
|
|
|
o.top = true;
|
|
|
|
cond = this.condition.compile(o);
|
|
|
|
set = '';
|
2010-04-27 19:38:24 -04:00
|
|
|
if (!(top)) {
|
2010-06-12 19:05:13 -04:00
|
|
|
rvar = o.scope.freeVariable();
|
2010-04-04 02:59:44 -04:00
|
|
|
set = ("" + this.tab + rvar + " = [];\n");
|
2010-02-23 22:53:43 -05:00
|
|
|
if (this.body) {
|
|
|
|
this.body = PushNode.wrap(rvar, this.body);
|
|
|
|
}
|
2010-02-09 22:05:17 -05:00
|
|
|
}
|
2010-04-04 02:59:44 -04:00
|
|
|
pre = ("" + set + (this.tab) + "while (" + cond + ")");
|
2010-04-28 21:46:47 -04:00
|
|
|
if (this.guard) {
|
|
|
|
this.body = Expressions.wrap([new IfNode(this.guard, this.body)]);
|
2010-02-23 22:53:43 -05:00
|
|
|
}
|
2010-05-12 21:47:31 -04:00
|
|
|
this.returns ? (post = '\n' + new ReturnNode(literal(rvar)).compile(merge(o, {
|
2010-03-21 07:17:58 -04:00
|
|
|
indent: this.idt()
|
|
|
|
}))) : (post = '');
|
2010-05-12 21:47:31 -04:00
|
|
|
return "" + pre + " {\n" + (this.body.compile(o)) + "\n" + this.tab + "}" + post;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
|
|
|
return WhileNode;
|
2010-04-10 14:40:05 -04:00
|
|
|
})();
|
2010-02-27 19:19:53 -05:00
|
|
|
exports.OpNode = (function() {
|
2010-05-14 23:40:04 -04:00
|
|
|
OpNode = function(operator, first, second, flip) {
|
2010-05-10 06:58:01 -04:00
|
|
|
this.first = first;
|
|
|
|
this.second = second;
|
2010-02-27 19:19:53 -05:00
|
|
|
this.operator = this.CONVERSIONS[operator] || operator;
|
|
|
|
this.flip = !!flip;
|
|
|
|
return this;
|
|
|
|
};
|
2010-03-30 20:15:51 -04:00
|
|
|
__extends(OpNode, BaseNode);
|
2010-02-27 19:19:53 -05:00
|
|
|
OpNode.prototype.CONVERSIONS = {
|
2010-02-09 22:20:04 -05:00
|
|
|
'==': '===',
|
2010-03-21 21:46:53 -04:00
|
|
|
'!=': '!=='
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
|
|
|
OpNode.prototype.CHAINABLE = ['<', '>', '>=', '<=', '===', '!=='];
|
|
|
|
OpNode.prototype.ASSIGNMENT = ['||=', '&&=', '?='];
|
|
|
|
OpNode.prototype.PREFIX_OPERATORS = ['typeof', 'delete'];
|
2010-06-15 01:03:14 -04:00
|
|
|
OpNode.prototype['class'] = 'OpNode';
|
2010-05-31 10:36:50 -04:00
|
|
|
OpNode.prototype.children = ['first', 'second'];
|
2010-06-12 19:05:13 -04:00
|
|
|
OpNode.prototype.isUnary = function() {
|
2010-02-09 22:20:04 -05:00
|
|
|
return !this.second;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
OpNode.prototype.isChainable = function() {
|
|
|
|
return indexOf(this.CHAINABLE, this.operator) >= 0;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
OpNode.prototype.compileNode = function(o) {
|
2010-02-20 20:00:07 -05:00
|
|
|
o.operation = true;
|
2010-06-12 19:05:13 -04:00
|
|
|
if (this.isChainable() && this.first.unwrap() instanceof OpNode && this.first.unwrap().isChainable()) {
|
|
|
|
return this.compileChain(o);
|
2010-02-09 22:20:04 -05:00
|
|
|
}
|
2010-06-12 19:05:13 -04:00
|
|
|
if (indexOf(this.ASSIGNMENT, this.operator) >= 0) {
|
|
|
|
return this.compileAssignment(o);
|
2010-02-10 00:05:56 -05:00
|
|
|
}
|
2010-06-12 19:05:13 -04:00
|
|
|
if (this.isUnary()) {
|
|
|
|
return this.compileUnary(o);
|
2010-02-10 00:05:56 -05:00
|
|
|
}
|
|
|
|
if (this.operator === '?') {
|
2010-06-12 19:05:13 -04:00
|
|
|
return this.compileExistence(o);
|
2010-02-10 00:05:56 -05:00
|
|
|
}
|
2010-03-06 00:23:54 -05:00
|
|
|
return [this.first.compile(o), this.operator, this.second.compile(o)].join(' ');
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
OpNode.prototype.compileChain = function(o) {
|
2010-04-25 22:29:43 -04:00
|
|
|
var _b, _c, first, second, shared;
|
2010-02-10 00:05:56 -05:00
|
|
|
shared = this.first.unwrap().second;
|
2010-06-12 19:05:13 -04:00
|
|
|
if (shared.containsType(CallNode)) {
|
|
|
|
_b = shared.compileReference(o);
|
2010-04-25 22:29:43 -04:00
|
|
|
this.first.second = _b[0];
|
|
|
|
shared = _b[1];
|
2010-02-10 00:05:56 -05:00
|
|
|
}
|
2010-04-25 22:29:43 -04:00
|
|
|
_c = [this.first.compile(o), this.second.compile(o), shared.compile(o)];
|
|
|
|
first = _c[0];
|
|
|
|
second = _c[1];
|
|
|
|
shared = _c[2];
|
2010-03-06 00:23:54 -05:00
|
|
|
return "(" + first + ") && (" + shared + " " + this.operator + " " + second + ")";
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
OpNode.prototype.compileAssignment = function(o) {
|
2010-04-25 22:29:43 -04:00
|
|
|
var _b, first, second;
|
|
|
|
_b = [this.first.compile(o), this.second.compile(o)];
|
|
|
|
first = _b[0];
|
|
|
|
second = _b[1];
|
2010-02-12 14:15:14 -05:00
|
|
|
if (first.match(IDENTIFIER)) {
|
2010-02-10 00:05:56 -05:00
|
|
|
o.scope.find(first);
|
|
|
|
}
|
|
|
|
if (this.operator === '?=') {
|
2010-06-12 19:05:13 -04:00
|
|
|
return ("" + first + " = " + (ExistenceNode.compileTest(o, this.first)) + " ? " + first + " : " + second);
|
2010-02-10 00:05:56 -05:00
|
|
|
}
|
2010-03-23 00:18:50 -04:00
|
|
|
return "" + first + " = " + first + " " + (this.operator.substr(0, 2)) + " " + second;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
OpNode.prototype.compileExistence = function(o) {
|
2010-04-25 22:29:43 -04:00
|
|
|
var _b, first, second, test;
|
|
|
|
_b = [this.first.compile(o), this.second.compile(o)];
|
|
|
|
first = _b[0];
|
|
|
|
second = _b[1];
|
2010-06-12 19:05:13 -04:00
|
|
|
test = ExistenceNode.compileTest(o, this.first);
|
2010-03-23 00:18:50 -04:00
|
|
|
return "" + test + " ? " + first + " : " + second;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
OpNode.prototype.compileUnary = function(o) {
|
2010-02-10 00:05:56 -05:00
|
|
|
var parts, space;
|
2010-06-12 19:05:13 -04:00
|
|
|
space = indexOf(this.PREFIX_OPERATORS, this.operator) >= 0 ? ' ' : '';
|
2010-02-10 00:05:56 -05:00
|
|
|
parts = [this.operator, space, this.first.compile(o)];
|
|
|
|
if (this.flip) {
|
|
|
|
parts = parts.reverse();
|
|
|
|
}
|
|
|
|
return parts.join('');
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
|
|
|
return OpNode;
|
2010-04-10 14:40:05 -04:00
|
|
|
})();
|
2010-06-21 23:51:12 -04:00
|
|
|
exports.InNode = (function() {
|
|
|
|
InNode = function(object, array) {
|
|
|
|
this.object = object;
|
|
|
|
this.array = array;
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
__extends(InNode, BaseNode);
|
|
|
|
InNode.prototype['class'] = 'InNode';
|
|
|
|
InNode.prototype.children = ['object', 'array'];
|
|
|
|
InNode.prototype.isArray = function() {
|
|
|
|
return this.array instanceof ValueNode && this.array.isArray();
|
|
|
|
};
|
|
|
|
InNode.prototype.compileNode = function(o) {
|
|
|
|
var _b;
|
|
|
|
_b = this.object.compileReference(o, {
|
|
|
|
precompile: true
|
|
|
|
});
|
|
|
|
this.obj1 = _b[0];
|
|
|
|
this.obj2 = _b[1];
|
|
|
|
if (this.isArray()) {
|
|
|
|
return this.compileOrTest(o);
|
|
|
|
} else {
|
|
|
|
return this.compileLoopTest(o);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
InNode.prototype.compileOrTest = function(o) {
|
|
|
|
var _b, _c, _d, i, item, tests;
|
|
|
|
tests = (function() {
|
|
|
|
_b = []; _c = this.array.base.objects;
|
|
|
|
for (i = 0, _d = _c.length; i < _d; i++) {
|
|
|
|
item = _c[i];
|
|
|
|
_b.push(("" + (item.compile(o)) + " === " + (i ? this.obj2 : this.obj1)));
|
|
|
|
}
|
|
|
|
return _b;
|
|
|
|
}).call(this);
|
|
|
|
return "(" + (tests.join(' || ')) + ")";
|
|
|
|
};
|
|
|
|
InNode.prototype.compileLoopTest = function(o) {
|
2010-06-27 13:27:51 -04:00
|
|
|
var _b, _c, i, l, prefix;
|
2010-06-21 23:51:12 -04:00
|
|
|
_b = this.array.compileReference(o, {
|
|
|
|
precompile: true
|
|
|
|
});
|
|
|
|
this.arr1 = _b[0];
|
|
|
|
this.arr2 = _b[1];
|
|
|
|
_c = [o.scope.freeVariable(), o.scope.freeVariable()];
|
|
|
|
i = _c[0];
|
|
|
|
l = _c[1];
|
2010-06-27 13:27:51 -04:00
|
|
|
prefix = this.obj1 !== this.obj2 ? this.obj1 + '; ' : '';
|
|
|
|
return "!!(function(){ " + (prefix) + "for (var " + i + "=0, " + l + "=" + (this.arr1) + ".length; " + i + "<" + l + "; " + i + "++) if (" + (this.arr2) + "[" + i + "] === " + this.obj2 + ") return true; })()";
|
2010-06-21 23:51:12 -04:00
|
|
|
};
|
|
|
|
return InNode;
|
|
|
|
})();
|
2010-02-27 19:19:53 -05:00
|
|
|
exports.TryNode = (function() {
|
2010-05-14 23:40:04 -04:00
|
|
|
TryNode = function(attempt, error, recovery, ensure) {
|
2010-05-10 06:58:01 -04:00
|
|
|
this.attempt = attempt;
|
|
|
|
this.recovery = recovery;
|
|
|
|
this.ensure = ensure;
|
2010-02-10 18:33:03 -05:00
|
|
|
this.error = error;
|
|
|
|
return this;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-03-30 20:15:51 -04:00
|
|
|
__extends(TryNode, BaseNode);
|
2010-06-15 01:03:14 -04:00
|
|
|
TryNode.prototype['class'] = 'TryNode';
|
2010-05-31 10:36:50 -04:00
|
|
|
TryNode.prototype.children = ['attempt', 'recovery', 'ensure'];
|
2010-06-12 19:05:13 -04:00
|
|
|
TryNode.prototype.isStatement = function() {
|
2010-05-31 10:36:50 -04:00
|
|
|
return true;
|
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
TryNode.prototype.makeReturn = function() {
|
2010-03-21 07:17:58 -04:00
|
|
|
if (this.attempt) {
|
2010-06-12 19:05:13 -04:00
|
|
|
this.attempt = this.attempt.makeReturn();
|
2010-03-21 07:17:58 -04:00
|
|
|
}
|
|
|
|
if (this.recovery) {
|
2010-06-12 19:05:13 -04:00
|
|
|
this.recovery = this.recovery.makeReturn();
|
2010-03-21 07:17:58 -04:00
|
|
|
}
|
|
|
|
return this;
|
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
TryNode.prototype.compileNode = function(o) {
|
|
|
|
var attemptPart, catchPart, errorPart, finallyPart;
|
2010-02-10 18:33:03 -05:00
|
|
|
o.indent = this.idt(1);
|
|
|
|
o.top = true;
|
2010-06-12 19:05:13 -04:00
|
|
|
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 + "}") : '';
|
|
|
|
finallyPart = (this.ensure || '') && ' finally {\n' + this.ensure.compile(merge(o)) + ("\n" + this.tab + "}");
|
|
|
|
return "" + (this.tab) + "try {\n" + attemptPart + "\n" + this.tab + "}" + catchPart + finallyPart;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
|
|
|
return TryNode;
|
2010-04-10 14:40:05 -04:00
|
|
|
})();
|
2010-02-27 19:19:53 -05:00
|
|
|
exports.ThrowNode = (function() {
|
2010-05-14 23:40:04 -04:00
|
|
|
ThrowNode = function(expression) {
|
2010-05-10 06:58:01 -04:00
|
|
|
this.expression = expression;
|
2010-02-10 19:13:11 -05:00
|
|
|
return this;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-03-30 20:15:51 -04:00
|
|
|
__extends(ThrowNode, BaseNode);
|
2010-06-15 01:03:14 -04:00
|
|
|
ThrowNode.prototype['class'] = 'ThrowNode';
|
2010-05-31 10:36:50 -04:00
|
|
|
ThrowNode.prototype.children = ['expression'];
|
2010-06-12 19:05:13 -04:00
|
|
|
ThrowNode.prototype.isStatement = function() {
|
2010-05-31 10:36:50 -04:00
|
|
|
return true;
|
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
ThrowNode.prototype.makeReturn = function() {
|
2010-03-21 07:17:58 -04:00
|
|
|
return this;
|
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
ThrowNode.prototype.compileNode = function(o) {
|
2010-03-23 00:18:50 -04:00
|
|
|
return "" + (this.tab) + "throw " + (this.expression.compile(o)) + ";";
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
|
|
|
return ThrowNode;
|
2010-04-10 14:40:05 -04:00
|
|
|
})();
|
2010-02-27 19:19:53 -05:00
|
|
|
exports.ExistenceNode = (function() {
|
2010-05-14 23:40:04 -04:00
|
|
|
ExistenceNode = function(expression) {
|
2010-05-10 06:58:01 -04:00
|
|
|
this.expression = expression;
|
2010-02-10 19:13:11 -05:00
|
|
|
return this;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-03-30 20:15:51 -04:00
|
|
|
__extends(ExistenceNode, BaseNode);
|
2010-06-15 01:03:14 -04:00
|
|
|
ExistenceNode.prototype['class'] = 'ExistenceNode';
|
2010-05-31 10:36:50 -04:00
|
|
|
ExistenceNode.prototype.children = ['expression'];
|
2010-06-12 19:05:13 -04:00
|
|
|
ExistenceNode.prototype.compileNode = function(o) {
|
|
|
|
return ExistenceNode.compileTest(o, this.expression);
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
ExistenceNode.compileTest = function(o, variable) {
|
2010-05-31 19:38:45 -04:00
|
|
|
var _b, first, second;
|
2010-06-21 23:51:12 -04:00
|
|
|
_b = variable.compileReference(o);
|
2010-04-25 22:29:43 -04:00
|
|
|
first = _b[0];
|
|
|
|
second = _b[1];
|
2010-05-31 19:38:45 -04:00
|
|
|
return "(typeof " + (first.compile(o)) + " !== \"undefined\" && " + (second.compile(o)) + " !== null)";
|
2010-03-29 21:49:20 -04:00
|
|
|
};
|
2010-02-27 19:19:53 -05:00
|
|
|
return ExistenceNode;
|
|
|
|
}).call(this);
|
|
|
|
exports.ParentheticalNode = (function() {
|
2010-05-14 23:40:04 -04:00
|
|
|
ParentheticalNode = function(expression) {
|
2010-05-10 06:58:01 -04:00
|
|
|
this.expression = expression;
|
2010-02-10 19:13:11 -05:00
|
|
|
return this;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-03-30 20:15:51 -04:00
|
|
|
__extends(ParentheticalNode, BaseNode);
|
2010-06-15 01:03:14 -04:00
|
|
|
ParentheticalNode.prototype['class'] = 'ParentheticalNode';
|
2010-05-31 10:36:50 -04:00
|
|
|
ParentheticalNode.prototype.children = ['expression'];
|
2010-06-12 19:05:13 -04:00
|
|
|
ParentheticalNode.prototype.isStatement = function() {
|
|
|
|
return this.expression.isStatement();
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
ParentheticalNode.prototype.makeReturn = function() {
|
|
|
|
return this.expression.makeReturn();
|
2010-03-21 07:17:58 -04:00
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
ParentheticalNode.prototype.compileNode = function(o) {
|
2010-02-10 19:13:11 -05:00
|
|
|
var code, l;
|
2010-02-17 19:19:51 -05:00
|
|
|
code = this.expression.compile(o);
|
2010-06-12 19:05:13 -04:00
|
|
|
if (this.isStatement()) {
|
2010-02-17 19:19:51 -05:00
|
|
|
return code;
|
|
|
|
}
|
2010-02-10 19:13:11 -05:00
|
|
|
l = code.length;
|
|
|
|
if (code.substr(l - 1, 1) === ';') {
|
|
|
|
code = code.substr(o, l - 1);
|
|
|
|
}
|
2010-03-23 00:18:50 -04:00
|
|
|
if (this.expression instanceof AssignNode) {
|
|
|
|
return code;
|
|
|
|
} else {
|
|
|
|
return "(" + code + ")";
|
|
|
|
}
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
|
|
|
return ParentheticalNode;
|
2010-04-10 14:40:05 -04:00
|
|
|
})();
|
2010-02-27 19:19:53 -05:00
|
|
|
exports.ForNode = (function() {
|
2010-05-14 23:40:04 -04:00
|
|
|
ForNode = function(body, source, name, index) {
|
2010-04-25 22:29:43 -04:00
|
|
|
var _b;
|
2010-02-10 20:19:59 -05:00
|
|
|
this.body = body;
|
|
|
|
this.name = name;
|
|
|
|
this.index = index || null;
|
|
|
|
this.source = source.source;
|
2010-04-28 21:46:47 -04:00
|
|
|
this.guard = source.guard;
|
2010-02-10 20:19:59 -05:00
|
|
|
this.step = source.step;
|
|
|
|
this.object = !!source.object;
|
|
|
|
if (this.object) {
|
2010-04-25 22:29:43 -04:00
|
|
|
_b = [this.index, this.name];
|
|
|
|
this.name = _b[0];
|
|
|
|
this.index = _b[1];
|
2010-02-10 20:19:59 -05:00
|
|
|
}
|
2010-04-26 14:35:35 -04:00
|
|
|
this.pattern = this.name instanceof ValueNode;
|
2010-04-26 14:54:31 -04:00
|
|
|
if (this.index instanceof ValueNode) {
|
2010-04-26 14:35:35 -04:00
|
|
|
throw new Error('index cannot be a pattern matching expression');
|
|
|
|
}
|
2010-03-21 11:28:05 -04:00
|
|
|
this.returns = false;
|
2010-02-10 20:19:59 -05:00
|
|
|
return this;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-03-30 20:15:51 -04:00
|
|
|
__extends(ForNode, BaseNode);
|
2010-06-15 01:03:14 -04:00
|
|
|
ForNode.prototype['class'] = 'ForNode';
|
2010-05-31 10:36:50 -04:00
|
|
|
ForNode.prototype.children = ['body', 'source', 'guard'];
|
2010-06-12 19:05:13 -04:00
|
|
|
ForNode.prototype.isStatement = function() {
|
2010-05-31 10:36:50 -04:00
|
|
|
return true;
|
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
ForNode.prototype.topSensitive = function() {
|
2010-02-10 20:19:59 -05:00
|
|
|
return true;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
ForNode.prototype.makeReturn = function() {
|
2010-03-21 11:28:05 -04:00
|
|
|
this.returns = true;
|
2010-03-21 07:17:58 -04:00
|
|
|
return this;
|
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
ForNode.prototype.compileReturnValue = function(val, o) {
|
2010-03-21 11:28:05 -04:00
|
|
|
if (this.returns) {
|
2010-05-12 21:47:31 -04:00
|
|
|
return '\n' + new ReturnNode(literal(val)).compile(o);
|
|
|
|
}
|
|
|
|
if (val) {
|
|
|
|
return '\n' + val;
|
2010-03-21 07:17:58 -04:00
|
|
|
}
|
2010-05-12 21:47:31 -04:00
|
|
|
return '';
|
2010-03-21 07:17:58 -04:00
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
ForNode.prototype.compileNode = function(o) {
|
2010-06-13 21:21:30 -04:00
|
|
|
var body, close, codeInBody, forPart, index, ivar, lvar, name, namePart, range, returnResult, rvar, scope, source, sourcePart, stepPart, svar, topLevel, varPart, vars;
|
2010-06-12 19:05:13 -04:00
|
|
|
topLevel = del(o, 'top') && !this.returns;
|
2010-02-10 20:19:59 -05:00
|
|
|
range = this.source instanceof ValueNode && this.source.base instanceof RangeNode && !this.source.properties.length;
|
|
|
|
source = range ? this.source.base : this.source;
|
2010-06-13 21:21:30 -04:00
|
|
|
codeInBody = this.body.contains(function(n) {
|
|
|
|
return n instanceof CodeNode;
|
|
|
|
});
|
2010-02-10 20:19:59 -05:00
|
|
|
scope = o.scope;
|
|
|
|
name = this.name && this.name.compile(o);
|
|
|
|
index = this.index && this.index.compile(o);
|
2010-06-13 21:21:30 -04:00
|
|
|
if (name && !this.pattern && !codeInBody) {
|
2010-03-10 22:32:00 -05:00
|
|
|
scope.find(name);
|
|
|
|
}
|
2010-06-13 21:43:04 -04:00
|
|
|
if (index) {
|
2010-03-10 22:32:00 -05:00
|
|
|
scope.find(index);
|
|
|
|
}
|
2010-06-12 19:05:13 -04:00
|
|
|
if (!(topLevel)) {
|
|
|
|
rvar = scope.freeVariable();
|
2010-02-10 20:19:59 -05:00
|
|
|
}
|
2010-06-13 21:43:04 -04:00
|
|
|
ivar = (function() {
|
|
|
|
if (range) {
|
|
|
|
return name;
|
|
|
|
} else if (codeInBody) {
|
|
|
|
return scope.freeVariable();
|
|
|
|
} else {
|
|
|
|
return index || scope.freeVariable();
|
|
|
|
}
|
|
|
|
})();
|
2010-06-12 19:05:13 -04:00
|
|
|
varPart = '';
|
2010-02-10 20:19:59 -05:00
|
|
|
body = Expressions.wrap([this.body]);
|
|
|
|
if (range) {
|
2010-06-12 19:05:13 -04:00
|
|
|
sourcePart = source.compileVariables(o);
|
|
|
|
forPart = source.compile(merge(o, {
|
2010-02-10 20:19:59 -05:00
|
|
|
index: ivar,
|
|
|
|
step: this.step
|
2010-03-06 00:23:54 -05:00
|
|
|
}));
|
2010-02-10 20:19:59 -05:00
|
|
|
} else {
|
2010-06-12 19:05:13 -04:00
|
|
|
svar = scope.freeVariable();
|
|
|
|
sourcePart = ("" + svar + " = " + (this.source.compile(o)) + ";");
|
2010-04-26 14:35:35 -04:00
|
|
|
if (this.pattern) {
|
2010-06-13 21:21:30 -04:00
|
|
|
namePart = new AssignNode(this.name, literal(("" + svar + "[" + ivar + "]"))).compile(merge(o, {
|
2010-04-26 23:46:35 -04:00
|
|
|
indent: this.idt(1),
|
|
|
|
top: true
|
|
|
|
})) + "\n";
|
2010-04-27 09:32:45 -04:00
|
|
|
} else {
|
|
|
|
if (name) {
|
2010-06-13 21:21:30 -04:00
|
|
|
namePart = ("" + name + " = " + svar + "[" + ivar + "]");
|
2010-04-27 09:32:45 -04:00
|
|
|
}
|
2010-02-10 20:19:59 -05:00
|
|
|
}
|
2010-04-27 19:38:24 -04:00
|
|
|
if (!(this.object)) {
|
2010-06-12 19:05:13 -04:00
|
|
|
lvar = scope.freeVariable();
|
|
|
|
stepPart = this.step ? ("" + ivar + " += " + (this.step.compile(o))) : ("" + ivar + "++");
|
|
|
|
forPart = ("" + ivar + " = 0, " + lvar + " = " + (svar) + ".length; " + ivar + " < " + lvar + "; " + stepPart);
|
2010-02-25 23:39:14 -05:00
|
|
|
}
|
2010-02-10 20:19:59 -05:00
|
|
|
}
|
2010-06-12 19:05:13 -04:00
|
|
|
sourcePart = (rvar ? ("" + rvar + " = []; ") : '') + sourcePart;
|
|
|
|
sourcePart = sourcePart ? ("" + this.tab + sourcePart + "\n" + this.tab) : this.tab;
|
|
|
|
returnResult = this.compileReturnValue(rvar, o);
|
|
|
|
if (!(topLevel)) {
|
2010-02-10 20:19:59 -05:00
|
|
|
body = PushNode.wrap(rvar, body);
|
|
|
|
}
|
2010-04-28 21:46:47 -04:00
|
|
|
this.guard ? (body = Expressions.wrap([new IfNode(this.guard, body)])) : null;
|
2010-06-13 21:21:30 -04:00
|
|
|
if (codeInBody) {
|
|
|
|
if (namePart) {
|
|
|
|
body.unshift(literal(("var " + namePart)));
|
|
|
|
}
|
2010-06-13 21:43:04 -04:00
|
|
|
if (index) {
|
|
|
|
body.unshift(literal(("var " + index + " = " + ivar)));
|
|
|
|
}
|
2010-06-13 21:21:30 -04:00
|
|
|
body = ClosureNode.wrap(body, true);
|
|
|
|
} else {
|
|
|
|
if (namePart) {
|
|
|
|
varPart = ("" + (this.idt(1)) + namePart + ";\n");
|
|
|
|
}
|
|
|
|
}
|
2010-06-12 19:05:13 -04:00
|
|
|
this.object ? (forPart = ("" + ivar + " in " + svar + ") { if (" + (utility('hasProp')) + ".call(" + svar + ", " + ivar + ")")) : null;
|
2010-03-14 17:58:32 -04:00
|
|
|
body = body.compile(merge(o, {
|
2010-06-13 21:21:30 -04:00
|
|
|
indent: this.idt(1),
|
2010-02-10 20:19:59 -05:00
|
|
|
top: true
|
|
|
|
}));
|
2010-04-04 02:59:44 -04:00
|
|
|
vars = range ? name : ("" + name + ", " + ivar);
|
2010-05-12 21:47:31 -04:00
|
|
|
close = this.object ? '}}' : '}';
|
2010-06-12 19:05:13 -04:00
|
|
|
return "" + (sourcePart) + "for (" + forPart + ") {\n" + varPart + body + "\n" + this.tab + close + returnResult;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
|
|
|
return ForNode;
|
2010-04-10 14:40:05 -04:00
|
|
|
})();
|
2010-02-27 19:19:53 -05:00
|
|
|
exports.IfNode = (function() {
|
2010-05-14 23:40:04 -04:00
|
|
|
IfNode = function(condition, body, tags) {
|
2010-02-10 21:40:10 -05:00
|
|
|
this.condition = condition;
|
2010-04-27 09:32:45 -04:00
|
|
|
this.body = body;
|
2010-06-12 19:05:13 -04:00
|
|
|
this.elseBody = null;
|
2010-02-13 01:13:08 -05:00
|
|
|
this.tags = tags || {};
|
2010-02-10 21:40:10 -05:00
|
|
|
if (this.tags.invert) {
|
|
|
|
this.condition = new OpNode('!', new ParentheticalNode(this.condition));
|
|
|
|
}
|
2010-06-12 19:05:13 -04:00
|
|
|
this.isChain = false;
|
2010-02-10 21:40:10 -05:00
|
|
|
return this;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-03-30 20:15:51 -04:00
|
|
|
__extends(IfNode, BaseNode);
|
2010-06-15 01:03:14 -04:00
|
|
|
IfNode.prototype['class'] = 'IfNode';
|
2010-06-12 19:05:13 -04:00
|
|
|
IfNode.prototype.children = ['condition', 'switchSubject', 'body', 'elseBody', 'assigner'];
|
|
|
|
IfNode.prototype.bodyNode = function() {
|
2010-04-27 09:32:45 -04:00
|
|
|
return this.body == undefined ? undefined : this.body.unwrap();
|
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
IfNode.prototype.elseBodyNode = function() {
|
|
|
|
return this.elseBody == undefined ? undefined : this.elseBody.unwrap();
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
IfNode.prototype.forceStatement = function() {
|
2010-02-10 21:40:10 -05:00
|
|
|
this.tags.statement = true;
|
|
|
|
return this;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
IfNode.prototype.switchesOver = function(expression) {
|
|
|
|
this.switchSubject = expression;
|
2010-02-17 21:23:59 -05:00
|
|
|
return this;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
IfNode.prototype.rewriteSwitch = function(o) {
|
2010-05-10 06:58:01 -04:00
|
|
|
var _b, _c, _d, cond, i, variable;
|
2010-06-12 19:05:13 -04:00
|
|
|
this.assigner = this.switchSubject;
|
|
|
|
if (!((this.switchSubject.unwrap() instanceof LiteralNode))) {
|
|
|
|
variable = literal(o.scope.freeVariable());
|
|
|
|
this.assigner = new AssignNode(variable, this.switchSubject);
|
|
|
|
this.switchSubject = variable;
|
2010-02-17 21:23:59 -05:00
|
|
|
}
|
2010-02-10 21:40:10 -05:00
|
|
|
this.condition = (function() {
|
2010-05-10 22:50:11 -04:00
|
|
|
_b = []; _c = flatten([this.condition]);
|
|
|
|
for (i = 0, _d = _c.length; i < _d; i++) {
|
|
|
|
cond = _c[i];
|
|
|
|
_b.push((function() {
|
|
|
|
if (cond instanceof OpNode) {
|
|
|
|
cond = new ParentheticalNode(cond);
|
|
|
|
}
|
2010-06-12 19:05:13 -04:00
|
|
|
return new OpNode('==', (i === 0 ? this.assigner : this.switchSubject), cond);
|
2010-05-10 22:50:11 -04:00
|
|
|
}).call(this));
|
2010-02-10 21:40:10 -05:00
|
|
|
}
|
2010-05-10 22:50:11 -04:00
|
|
|
return _b;
|
2010-02-10 21:40:10 -05:00
|
|
|
}).call(this);
|
2010-06-12 19:05:13 -04:00
|
|
|
if (this.isChain) {
|
|
|
|
this.elseBodyNode().switchesOver(this.switchSubject);
|
2010-02-10 21:40:10 -05:00
|
|
|
}
|
2010-06-12 19:05:13 -04:00
|
|
|
this.switchSubject = undefined;
|
2010-02-10 21:40:10 -05:00
|
|
|
return this;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
IfNode.prototype.addElse = function(elseBody, statement) {
|
|
|
|
if (this.isChain) {
|
|
|
|
this.elseBodyNode().addElse(elseBody, statement);
|
2010-02-22 19:22:09 -05:00
|
|
|
} else {
|
2010-06-12 19:05:13 -04:00
|
|
|
this.isChain = elseBody instanceof IfNode;
|
|
|
|
this.elseBody = this.ensureExpressions(elseBody);
|
2010-02-22 19:22:09 -05:00
|
|
|
}
|
2010-02-10 21:40:10 -05:00
|
|
|
return this;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
IfNode.prototype.isStatement = function() {
|
2010-06-27 12:59:54 -04:00
|
|
|
return this.statement = this.statement || !!(this.tags.statement || this.bodyNode().isStatement() || (this.elseBody && this.elseBodyNode().isStatement()));
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
IfNode.prototype.compileCondition = function(o) {
|
2010-04-25 22:29:43 -04:00
|
|
|
var _b, _c, _d, _e, cond;
|
2010-02-17 19:19:51 -05:00
|
|
|
return (function() {
|
2010-04-25 22:29:43 -04:00
|
|
|
_b = []; _d = flatten([this.condition]);
|
|
|
|
for (_c = 0, _e = _d.length; _c < _e; _c++) {
|
|
|
|
cond = _d[_c];
|
|
|
|
_b.push(cond.compile(o));
|
2010-02-10 21:40:10 -05:00
|
|
|
}
|
2010-04-25 22:29:43 -04:00
|
|
|
return _b;
|
2010-02-17 19:19:51 -05:00
|
|
|
}).call(this).join(' || ');
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
IfNode.prototype.compileNode = function(o) {
|
|
|
|
if (this.isStatement()) {
|
|
|
|
return this.compileStatement(o);
|
2010-03-21 07:17:58 -04:00
|
|
|
} else {
|
2010-06-12 19:05:13 -04:00
|
|
|
return this.compileTernary(o);
|
2010-03-21 07:17:58 -04:00
|
|
|
}
|
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
IfNode.prototype.makeReturn = function() {
|
|
|
|
this.body = this.body && this.ensureExpressions(this.body.makeReturn());
|
|
|
|
this.elseBody = this.elseBody && this.ensureExpressions(this.elseBody.makeReturn());
|
2010-03-21 07:17:58 -04:00
|
|
|
return this;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
IfNode.prototype.ensureExpressions = function(node) {
|
2010-06-13 18:04:09 -04:00
|
|
|
if (node instanceof Expressions) {
|
|
|
|
return node;
|
|
|
|
} else {
|
|
|
|
return new Expressions([node]);
|
2010-04-27 09:32:45 -04:00
|
|
|
}
|
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
IfNode.prototype.compileStatement = function(o) {
|
2010-06-27 12:59:54 -04:00
|
|
|
var body, child, comDent, condO, elsePart, ifDent, ifPart;
|
2010-06-12 19:05:13 -04:00
|
|
|
if (this.switchSubject) {
|
|
|
|
this.rewriteSwitch(o);
|
2010-02-17 21:23:59 -05:00
|
|
|
}
|
2010-06-12 19:05:13 -04:00
|
|
|
child = del(o, 'chainChild');
|
|
|
|
condO = merge(o);
|
2010-02-10 21:40:10 -05:00
|
|
|
o.indent = this.idt(1);
|
|
|
|
o.top = true;
|
2010-06-12 19:05:13 -04:00
|
|
|
ifDent = child ? '' : this.idt();
|
|
|
|
comDent = child ? this.idt() : '';
|
2010-04-27 09:32:45 -04:00
|
|
|
body = this.body.compile(o);
|
2010-06-27 12:59:54 -04:00
|
|
|
ifPart = ("" + (ifDent) + "if (" + (this.compileCondition(condO)) + ") {\n" + body + "\n" + this.tab + "}");
|
2010-06-12 19:05:13 -04:00
|
|
|
if (!(this.elseBody)) {
|
|
|
|
return ifPart;
|
2010-02-10 21:40:10 -05:00
|
|
|
}
|
2010-06-12 19:05:13 -04:00
|
|
|
elsePart = this.isChain ? ' else ' + this.elseBodyNode().compile(merge(o, {
|
2010-02-10 21:40:10 -05:00
|
|
|
indent: this.idt(),
|
2010-06-12 19:05:13 -04:00
|
|
|
chainChild: true
|
|
|
|
})) : (" else {\n" + (this.elseBody.compile(o)) + "\n" + this.tab + "}");
|
|
|
|
return "" + ifPart + elsePart;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
IfNode.prototype.compileTernary = function(o) {
|
|
|
|
var elsePart, ifPart;
|
|
|
|
ifPart = this.condition.compile(o) + ' ? ' + this.bodyNode().compile(o);
|
|
|
|
elsePart = this.elseBody ? this.elseBodyNode().compile(o) : 'null';
|
|
|
|
return "" + ifPart + " : " + elsePart;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
|
|
|
return IfNode;
|
2010-04-10 14:40:05 -04:00
|
|
|
})();
|
2010-03-09 23:04:16 -05:00
|
|
|
PushNode = (exports.PushNode = {
|
2010-05-14 23:40:04 -04:00
|
|
|
wrap: function(array, expressions) {
|
2010-03-09 23:04:16 -05:00
|
|
|
var expr;
|
|
|
|
expr = expressions.unwrap();
|
2010-06-12 19:05:13 -04:00
|
|
|
if (expr.isPureStatement() || expr.containsPureStatement()) {
|
2010-03-09 23:04:16 -05:00
|
|
|
return expressions;
|
|
|
|
}
|
|
|
|
return Expressions.wrap([new CallNode(new ValueNode(literal(array), [new AccessorNode(literal('push'))]), [expr])]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
ClosureNode = (exports.ClosureNode = {
|
2010-05-14 23:40:04 -04:00
|
|
|
wrap: function(expressions, statement) {
|
2010-06-12 19:05:13 -04:00
|
|
|
var args, call, func, mentionsArgs, mentionsThis, meth;
|
|
|
|
if (expressions.containsPureStatement()) {
|
2010-03-16 01:27:31 -04:00
|
|
|
return expressions;
|
|
|
|
}
|
2010-04-10 14:40:05 -04:00
|
|
|
func = new ParentheticalNode(new CodeNode([], Expressions.wrap([expressions])));
|
|
|
|
args = [];
|
2010-06-12 19:05:13 -04:00
|
|
|
mentionsArgs = expressions.contains(function(n) {
|
2010-04-10 14:20:32 -04:00
|
|
|
return (n instanceof LiteralNode) && (n.value === 'arguments');
|
|
|
|
});
|
2010-06-12 19:05:13 -04:00
|
|
|
mentionsThis = expressions.contains(function(n) {
|
2010-04-10 14:40:05 -04:00
|
|
|
return (n instanceof LiteralNode) && (n.value === 'this');
|
|
|
|
});
|
2010-06-12 19:05:13 -04:00
|
|
|
if (mentionsArgs || mentionsThis) {
|
|
|
|
meth = literal(mentionsArgs ? 'apply' : 'call');
|
2010-04-10 14:40:05 -04:00
|
|
|
args = [literal('this')];
|
2010-06-12 19:05:13 -04:00
|
|
|
if (mentionsArgs) {
|
2010-04-10 14:40:05 -04:00
|
|
|
args.push(literal('arguments'));
|
|
|
|
}
|
|
|
|
func = new ValueNode(func, [new AccessorNode(meth)]);
|
2010-04-10 14:20:32 -04:00
|
|
|
}
|
2010-04-10 14:40:05 -04:00
|
|
|
call = new CallNode(func, args);
|
2010-03-21 07:17:58 -04:00
|
|
|
if (statement) {
|
|
|
|
return Expressions.wrap([call]);
|
|
|
|
} else {
|
|
|
|
return call;
|
|
|
|
}
|
2010-03-09 23:04:16 -05:00
|
|
|
}
|
|
|
|
});
|
2010-03-30 19:42:09 -04:00
|
|
|
UTILITIES = {
|
2010-05-14 21:50:17 -04:00
|
|
|
__extends: "function(child, parent) {\n var ctor = function(){ };\n ctor.prototype = parent.prototype;\n child.__superClass__ = parent.prototype;\n child.prototype = new ctor();\n child.prototype.constructor = child;\n }",
|
2010-03-30 20:15:51 -04:00
|
|
|
__hasProp: 'Object.prototype.hasOwnProperty',
|
|
|
|
__slice: 'Array.prototype.slice'
|
2010-03-30 19:42:09 -04:00
|
|
|
};
|
2010-03-07 16:41:06 -05:00
|
|
|
TAB = ' ';
|
2010-05-12 21:47:31 -04:00
|
|
|
TRAILING_WHITESPACE = /[ \t]+$/gm;
|
2010-03-08 13:05:02 -05:00
|
|
|
IDENTIFIER = /^[a-zA-Z\$_](\w|\$)*$/;
|
2010-05-31 22:56:51 -04:00
|
|
|
NUMBER = /^(((\b0(x|X)[0-9a-fA-F]+)|((\b[0-9]+(\.[0-9]+)?|\.[0-9]+)(e[+\-]?[0-9]+)?)))\b$/i;
|
2010-04-25 11:07:09 -04:00
|
|
|
IS_STRING = /^['"]/;
|
2010-05-14 23:40:04 -04:00
|
|
|
literal = function(name) {
|
2010-03-07 16:41:06 -05:00
|
|
|
return new LiteralNode(name);
|
|
|
|
};
|
2010-05-14 23:40:04 -04:00
|
|
|
utility = function(name) {
|
2010-03-30 19:48:37 -04:00
|
|
|
var ref;
|
2010-04-04 02:59:44 -04:00
|
|
|
ref = ("__" + name);
|
2010-03-30 20:15:51 -04:00
|
|
|
Scope.root.assign(ref, UTILITIES[ref]);
|
2010-03-30 19:48:37 -04:00
|
|
|
return ref;
|
|
|
|
};
|
2010-02-24 18:56:32 -05:00
|
|
|
})();
|