2010-07-25 07:15:12 +00:00
|
|
|
(function() {
|
2010-09-29 02:30:05 +00:00
|
|
|
var AccessorNode, ArrayNode, AssignNode, BaseNode, CallNode, ClassNode, ClosureNode, CodeNode, CommentNode, ExistenceNode, Expressions, ExtendsNode, ForNode, IDENTIFIER, IS_STRING, IfNode, InNode, IndexNode, LiteralNode, NO, NUMBER, ObjectNode, OpNode, ParamNode, ParentheticalNode, PushNode, RangeNode, ReturnNode, SIMPLENUM, Scope, SliceNode, SplatNode, SwitchNode, TAB, THIS, TRAILING_WHITESPACE, ThrowNode, TryNode, UTILITIES, ValueNode, WhileNode, YES, _ref, compact, del, ends, flatten, include, indexOf, last, literal, merge, starts, utility;
|
2010-05-19 19:50:29 +00:00
|
|
|
var __extends = function(child, parent) {
|
2010-07-25 07:15:12 +00:00
|
|
|
var ctor = function(){};
|
2010-05-15 03:40:04 +00:00
|
|
|
ctor.prototype = parent.prototype;
|
|
|
|
child.prototype = new ctor();
|
|
|
|
child.prototype.constructor = child;
|
2010-07-21 17:32:36 +00:00
|
|
|
if (typeof parent.extended === "function") parent.extended(child);
|
2010-08-24 01:19:43 +00:00
|
|
|
child.__super__ = parent.prototype;
|
2010-03-30 23:27:38 +00:00
|
|
|
};
|
2010-09-28 20:47:12 +00:00
|
|
|
Scope = require('./scope').Scope;
|
|
|
|
_ref = require('./helpers'), compact = _ref.compact, flatten = _ref.flatten, merge = _ref.merge, del = _ref.del, include = _ref.include, indexOf = _ref.indexOf, starts = _ref.starts, ends = _ref.ends, last = _ref.last;
|
2010-09-27 08:56:56 +00:00
|
|
|
YES = function() {
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
NO = function() {
|
|
|
|
return false;
|
|
|
|
};
|
2010-09-29 02:30:05 +00:00
|
|
|
THIS = function() {
|
|
|
|
return this;
|
|
|
|
};
|
2010-02-28 00:19:53 +00:00
|
|
|
exports.BaseNode = (function() {
|
2010-08-17 00:18:42 +00:00
|
|
|
BaseNode = function() {
|
|
|
|
this.tags = {};
|
|
|
|
return this;
|
|
|
|
};
|
2010-05-15 03:40:04 +00:00
|
|
|
BaseNode.prototype.compile = function(o) {
|
2010-09-19 16:34:27 +00:00
|
|
|
var closure, code, top;
|
2010-10-01 22:17:35 +00:00
|
|
|
this.options = o ? merge(o) : {};
|
2010-03-07 21:41:06 +00:00
|
|
|
this.tab = o.indent;
|
2010-06-12 23:05:13 +00:00
|
|
|
top = this.topSensitive() ? this.options.top : del(this.options, 'top');
|
2010-08-14 15:42:19 +00:00
|
|
|
closure = this.isStatement(o) && !this.isPureStatement() && !top && !this.options.asStatement && !(this instanceof CommentNode) && !this.containsPureStatement();
|
2010-09-19 16:34:27 +00:00
|
|
|
if (!o.keepLevel) {
|
|
|
|
o.scope.startLevel();
|
|
|
|
}
|
|
|
|
code = closure ? this.compileClosure(this.options) : this.compileNode(this.options);
|
|
|
|
if (!o.keepLevel) {
|
|
|
|
o.scope.endLevel();
|
|
|
|
}
|
|
|
|
return code;
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
2010-06-12 23:05:13 +00:00
|
|
|
BaseNode.prototype.compileClosure = function(o) {
|
2010-03-07 21:41:06 +00:00
|
|
|
this.tab = o.indent;
|
2010-06-12 23:05:13 +00:00
|
|
|
o.sharedScope = o.scope;
|
2010-02-28 00:19:53 +00:00
|
|
|
return ClosureNode.wrap(this).compile(o);
|
|
|
|
};
|
2010-06-22 03:51:12 +00:00
|
|
|
BaseNode.prototype.compileReference = function(o, options) {
|
2010-10-01 22:26:37 +00:00
|
|
|
var _len, compiled, i, node, pair, reference;
|
2010-06-22 03:51:12 +00:00
|
|
|
pair = (function() {
|
2010-10-01 22:17:35 +00:00
|
|
|
if (!(this.isComplex())) {
|
2010-06-22 03:51:12 +00:00
|
|
|
return [this, this];
|
|
|
|
} else {
|
2010-09-21 01:56:18 +00:00
|
|
|
reference = literal(o.scope.freeVariable('ref'));
|
2010-06-22 03:51:12 +00:00
|
|
|
compiled = new AssignNode(reference, this);
|
|
|
|
return [compiled, reference];
|
|
|
|
}
|
|
|
|
}).call(this);
|
2010-10-02 00:52:23 +00:00
|
|
|
if (((options != null) ? options.precompile : undefined)) {
|
2010-10-01 22:26:37 +00:00
|
|
|
for (i = 0, _len = pair.length; i < _len; i++) {
|
|
|
|
node = pair[i];
|
2010-10-01 22:17:35 +00:00
|
|
|
(pair[i] = node.compile(o));
|
|
|
|
}
|
2010-05-31 23:38:45 +00:00
|
|
|
}
|
2010-07-31 03:37:13 +00:00
|
|
|
return pair;
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
2010-05-15 03:40:04 +00:00
|
|
|
BaseNode.prototype.idt = function(tabs) {
|
2010-03-07 22:31:39 +00:00
|
|
|
var idt, num;
|
2010-03-07 21:41:06 +00:00
|
|
|
idt = this.tab || '';
|
2010-03-07 22:31:39 +00:00
|
|
|
num = (tabs || 0) + 1;
|
|
|
|
while (num -= 1) {
|
2010-03-11 01:15:12 +00:00
|
|
|
idt += TAB;
|
2010-02-28 00:19:53 +00:00
|
|
|
}
|
|
|
|
return idt;
|
|
|
|
};
|
2010-06-12 23:05:13 +00:00
|
|
|
BaseNode.prototype.makeReturn = function() {
|
2010-03-21 11:17:58 +00:00
|
|
|
return new ReturnNode(this);
|
|
|
|
};
|
2010-05-15 03:40:04 +00:00
|
|
|
BaseNode.prototype.contains = function(block) {
|
2010-05-10 10:58:01 +00:00
|
|
|
var contains;
|
|
|
|
contains = false;
|
2010-06-12 23:05:13 +00:00
|
|
|
this.traverseChildren(false, function(node) {
|
2010-05-11 02:41:18 +00:00
|
|
|
if (block(node)) {
|
|
|
|
contains = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
2010-05-10 10:58:01 +00:00
|
|
|
return contains;
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
2010-06-12 23:05:13 +00:00
|
|
|
BaseNode.prototype.containsType = function(type) {
|
2010-04-01 02:48:47 +00:00
|
|
|
return this instanceof type || this.contains(function(n) {
|
|
|
|
return n instanceof type;
|
|
|
|
});
|
|
|
|
};
|
2010-06-12 23:05:13 +00:00
|
|
|
BaseNode.prototype.containsPureStatement = function() {
|
|
|
|
return this.isPureStatement() || this.contains(function(n) {
|
2010-10-01 22:17:35 +00:00
|
|
|
return (typeof n.isPureStatement !== "function" ? undefined : n.isPureStatement());
|
2010-03-21 15:28:05 +00:00
|
|
|
});
|
|
|
|
};
|
2010-05-15 03:40:04 +00:00
|
|
|
BaseNode.prototype.traverse = function(block) {
|
2010-06-12 23:05:13 +00:00
|
|
|
return this.traverseChildren(true, block);
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
2010-07-10 15:27:43 +00:00
|
|
|
BaseNode.prototype.toString = function(idt, override) {
|
2010-09-29 20:29:20 +00:00
|
|
|
var _i, _len, _ref2, _result, child, children, klass;
|
2010-08-14 22:02:07 +00:00
|
|
|
idt || (idt = '');
|
2010-07-10 15:27:43 +00:00
|
|
|
children = (function() {
|
2010-09-21 01:56:18 +00:00
|
|
|
_result = []; _ref2 = this.collectChildren();
|
2010-09-21 02:13:08 +00:00
|
|
|
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
|
2010-09-21 01:56:18 +00:00
|
|
|
child = _ref2[_i];
|
2010-09-19 12:29:15 +00:00
|
|
|
_result.push(child.toString(idt + TAB));
|
2010-02-28 00:19:53 +00:00
|
|
|
}
|
2010-09-19 12:29:15 +00:00
|
|
|
return _result;
|
2010-02-28 00:19:53 +00:00
|
|
|
}).call(this).join('');
|
2010-09-29 20:29:20 +00:00
|
|
|
klass = override || this["class"] + (this.soakNode || this.exist ? '?' : '');
|
|
|
|
return '\n' + idt + klass + children;
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
2010-06-12 23:05:13 +00:00
|
|
|
BaseNode.prototype.eachChild = function(func) {
|
2010-09-21 04:55:08 +00:00
|
|
|
var _i, _j, _len, _len2, _ref2, _ref3, _result, attr, child;
|
2010-05-31 14:36:50 +00:00
|
|
|
if (!(this.children)) {
|
2010-05-19 19:37:42 +00:00
|
|
|
return null;
|
|
|
|
}
|
2010-09-21 01:56:18 +00:00
|
|
|
_result = []; _ref2 = this.children;
|
2010-09-21 02:13:08 +00:00
|
|
|
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
|
2010-09-21 01:56:18 +00:00
|
|
|
attr = _ref2[_i];
|
2010-05-11 02:41:18 +00:00
|
|
|
if (this[attr]) {
|
2010-09-21 01:56:18 +00:00
|
|
|
_ref3 = flatten([this[attr]]);
|
2010-09-21 04:55:08 +00:00
|
|
|
for (_j = 0, _len2 = _ref3.length; _j < _len2; _j++) {
|
|
|
|
child = _ref3[_j];
|
2010-05-11 02:41:18 +00:00
|
|
|
if (func(child) === false) {
|
2010-05-10 10:58:01 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-09-19 12:29:15 +00:00
|
|
|
return _result;
|
2010-05-10 10:58:01 +00:00
|
|
|
};
|
2010-06-12 23:05:13 +00:00
|
|
|
BaseNode.prototype.collectChildren = function() {
|
2010-05-31 14:36:50 +00:00
|
|
|
var nodes;
|
|
|
|
nodes = [];
|
2010-06-12 23:05:13 +00:00
|
|
|
this.eachChild(function(node) {
|
2010-05-31 14:36:50 +00:00
|
|
|
return nodes.push(node);
|
|
|
|
});
|
|
|
|
return nodes;
|
|
|
|
};
|
2010-06-12 23:05:13 +00:00
|
|
|
BaseNode.prototype.traverseChildren = function(crossScope, func) {
|
|
|
|
return this.eachChild(function(child) {
|
2010-10-01 22:17:35 +00:00
|
|
|
if (func(child) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
2010-10-02 00:52:23 +00:00
|
|
|
return child instanceof BaseNode && (crossScope || !(child instanceof CodeNode)) ? child.traverseChildren(crossScope, func) : undefined;
|
2010-05-10 10:58:01 +00:00
|
|
|
});
|
|
|
|
};
|
2010-07-31 00:29:05 +00:00
|
|
|
BaseNode.prototype["class"] = 'BaseNode';
|
2010-05-31 14:36:50 +00:00
|
|
|
BaseNode.prototype.children = [];
|
2010-09-29 02:30:05 +00:00
|
|
|
BaseNode.prototype.unwrap = THIS;
|
2010-09-27 08:56:56 +00:00
|
|
|
BaseNode.prototype.isStatement = NO;
|
|
|
|
BaseNode.prototype.isPureStatement = NO;
|
|
|
|
BaseNode.prototype.isComplex = YES;
|
|
|
|
BaseNode.prototype.topSensitive = NO;
|
2010-02-28 00:19:53 +00:00
|
|
|
return BaseNode;
|
2010-04-10 18:40:05 +00:00
|
|
|
})();
|
2010-02-28 00:19:53 +00:00
|
|
|
exports.Expressions = (function() {
|
2010-05-15 03:40:04 +00:00
|
|
|
Expressions = function(nodes) {
|
2010-08-24 01:19:43 +00:00
|
|
|
Expressions.__super__.constructor.call(this);
|
2010-05-10 10:58:01 +00:00
|
|
|
this.expressions = compact(flatten(nodes || []));
|
2010-02-09 02:10:48 +00:00
|
|
|
return this;
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
2010-03-31 00:15:51 +00:00
|
|
|
__extends(Expressions, BaseNode);
|
2010-07-31 00:29:05 +00:00
|
|
|
Expressions.prototype["class"] = 'Expressions';
|
2010-05-31 14:36:50 +00:00
|
|
|
Expressions.prototype.children = ['expressions'];
|
2010-09-27 08:56:56 +00:00
|
|
|
Expressions.prototype.isStatement = YES;
|
2010-05-15 03:40:04 +00:00
|
|
|
Expressions.prototype.push = function(node) {
|
2010-02-09 01:20:11 +00:00
|
|
|
this.expressions.push(node);
|
|
|
|
return this;
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
2010-05-15 03:40:04 +00:00
|
|
|
Expressions.prototype.unshift = function(node) {
|
2010-02-09 01:20:11 +00:00
|
|
|
this.expressions.unshift(node);
|
|
|
|
return this;
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
2010-05-15 03:40:04 +00:00
|
|
|
Expressions.prototype.unwrap = function() {
|
2010-07-07 03:04:35 +00:00
|
|
|
return this.expressions.length === 1 ? this.expressions[0] : this;
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
2010-05-15 03:40:04 +00:00
|
|
|
Expressions.prototype.empty = function() {
|
2010-02-09 01:20:11 +00:00
|
|
|
return this.expressions.length === 0;
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
2010-06-12 23:05:13 +00:00
|
|
|
Expressions.prototype.makeReturn = function() {
|
2010-09-28 12:52:51 +00:00
|
|
|
var end, idx;
|
|
|
|
end = this.expressions[(idx = this.expressions.length - 1)];
|
|
|
|
if (end instanceof CommentNode) {
|
|
|
|
end = this.expressions[idx -= 1];
|
2010-07-02 01:26:33 +00:00
|
|
|
}
|
2010-09-28 12:52:51 +00:00
|
|
|
if (end && !(end instanceof ReturnNode)) {
|
|
|
|
this.expressions[idx] = end.makeReturn();
|
2010-03-21 15:28:05 +00:00
|
|
|
}
|
2010-03-21 11:17:58 +00:00
|
|
|
return this;
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
2010-05-15 03:40:04 +00:00
|
|
|
Expressions.prototype.compile = function(o) {
|
2010-08-14 22:02:07 +00:00
|
|
|
o || (o = {});
|
2010-08-24 01:19:43 +00:00
|
|
|
return o.scope ? Expressions.__super__.compile.call(this, o) : this.compileRoot(o);
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
2010-06-12 23:05:13 +00:00
|
|
|
Expressions.prototype.compileNode = function(o) {
|
2010-09-21 02:13:08 +00:00
|
|
|
var _i, _len, _ref2, _result, node;
|
2010-02-18 00:19:51 +00:00
|
|
|
return (function() {
|
2010-09-21 01:56:18 +00:00
|
|
|
_result = []; _ref2 = this.expressions;
|
2010-09-21 02:13:08 +00:00
|
|
|
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
|
2010-09-21 01:56:18 +00:00
|
|
|
node = _ref2[_i];
|
2010-09-19 12:29:15 +00:00
|
|
|
_result.push(this.compileExpression(node, merge(o)));
|
2010-02-09 01:20:11 +00:00
|
|
|
}
|
2010-09-19 12:29:15 +00:00
|
|
|
return _result;
|
2010-02-18 00:19:51 +00:00
|
|
|
}).call(this).join("\n");
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
2010-06-12 23:05:13 +00:00
|
|
|
Expressions.prototype.compileRoot = function(o) {
|
2010-03-07 21:41:06 +00:00
|
|
|
var code;
|
2010-06-12 23:05:13 +00:00
|
|
|
o.indent = (this.tab = o.noWrap ? '' : TAB);
|
2010-02-09 01:20:11 +00:00
|
|
|
o.scope = new Scope(null, this, null);
|
2010-07-09 05:01:31 +00:00
|
|
|
code = this.compileWithDeclarations(o);
|
2010-02-09 01:20:11 +00:00
|
|
|
code = code.replace(TRAILING_WHITESPACE, '');
|
2010-10-04 12:50:50 +00:00
|
|
|
return o.noWrap ? code : ("(function() {\n" + code + "\n}).call(this);\n");
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
2010-06-12 23:05:13 +00:00
|
|
|
Expressions.prototype.compileWithDeclarations = function(o) {
|
2010-03-16 03:46:14 +00:00
|
|
|
var code;
|
2010-06-12 23:05:13 +00:00
|
|
|
code = this.compileNode(o);
|
|
|
|
if (o.scope.hasAssignments(this)) {
|
2010-10-04 12:50:50 +00:00
|
|
|
code = ("" + this.tab + "var " + (o.scope.compiledAssignments()) + ";\n" + code);
|
2010-02-09 01:20:11 +00:00
|
|
|
}
|
2010-07-09 05:01:31 +00:00
|
|
|
if (!o.globals && o.scope.hasDeclarations(this)) {
|
2010-10-04 12:50:50 +00:00
|
|
|
code = ("" + this.tab + "var " + (o.scope.compiledDeclarations()) + ";\n" + code);
|
2010-02-09 01:20:11 +00:00
|
|
|
}
|
|
|
|
return code;
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
2010-06-12 23:05:13 +00:00
|
|
|
Expressions.prototype.compileExpression = function(node, o) {
|
|
|
|
var compiledNode;
|
2010-03-07 21:41:06 +00:00
|
|
|
this.tab = o.indent;
|
2010-06-12 23:05:13 +00:00
|
|
|
compiledNode = node.compile(merge(o, {
|
2010-03-21 11:17:58 +00:00
|
|
|
top: true
|
|
|
|
}));
|
2010-10-04 12:50:50 +00:00
|
|
|
return node.isStatement(o) ? compiledNode : ("" + (this.idt()) + compiledNode + ";");
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
|
|
|
return Expressions;
|
2010-04-10 18:40:05 +00:00
|
|
|
})();
|
2010-05-15 03:40:04 +00:00
|
|
|
Expressions.wrap = function(nodes) {
|
2010-02-09 02:10:48 +00:00
|
|
|
if (nodes.length === 1 && nodes[0] instanceof Expressions) {
|
|
|
|
return nodes[0];
|
|
|
|
}
|
|
|
|
return new Expressions(nodes);
|
|
|
|
};
|
2010-02-28 00:19:53 +00:00
|
|
|
exports.LiteralNode = (function() {
|
2010-09-19 12:29:15 +00:00
|
|
|
LiteralNode = function(_arg) {
|
|
|
|
this.value = _arg;
|
2010-08-24 01:19:43 +00:00
|
|
|
LiteralNode.__super__.constructor.call(this);
|
2010-02-09 03:22:59 +00:00
|
|
|
return this;
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
2010-03-31 00:15:51 +00:00
|
|
|
__extends(LiteralNode, BaseNode);
|
2010-07-31 00:29:05 +00:00
|
|
|
LiteralNode.prototype["class"] = 'LiteralNode';
|
2010-07-30 01:33:35 +00:00
|
|
|
LiteralNode.prototype.makeReturn = function() {
|
2010-08-24 01:19:43 +00:00
|
|
|
return this.isStatement() ? this : LiteralNode.__super__.makeReturn.call(this);
|
2010-07-30 01:33:35 +00:00
|
|
|
};
|
2010-06-12 23:05:13 +00:00
|
|
|
LiteralNode.prototype.isStatement = function() {
|
2010-09-28 20:47:12 +00:00
|
|
|
var _ref2;
|
|
|
|
return ('break' === (_ref2 = this.value) || 'continue' === _ref2 || 'debugger' === _ref2);
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
2010-06-12 23:05:13 +00:00
|
|
|
LiteralNode.prototype.isPureStatement = LiteralNode.prototype.isStatement;
|
2010-09-27 08:56:56 +00:00
|
|
|
LiteralNode.prototype.isComplex = NO;
|
2010-06-12 23:05:13 +00:00
|
|
|
LiteralNode.prototype.compileNode = function(o) {
|
2010-02-09 02:10:48 +00:00
|
|
|
var end, idt;
|
2010-08-14 15:42:19 +00:00
|
|
|
idt = this.isStatement(o) ? this.idt() : '';
|
|
|
|
end = this.isStatement(o) ? ';' : '';
|
2010-08-07 12:02:16 +00:00
|
|
|
return idt + this.value + end;
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
2010-09-26 19:47:52 +00:00
|
|
|
LiteralNode.prototype.toString = function() {
|
|
|
|
return ' "' + this.value + '"';
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
|
|
|
return LiteralNode;
|
2010-04-10 18:40:05 +00:00
|
|
|
})();
|
2010-02-28 00:19:53 +00:00
|
|
|
exports.ReturnNode = (function() {
|
2010-09-19 12:29:15 +00:00
|
|
|
ReturnNode = function(_arg) {
|
|
|
|
this.expression = _arg;
|
2010-08-24 01:19:43 +00:00
|
|
|
ReturnNode.__super__.constructor.call(this);
|
2010-02-09 03:22:59 +00:00
|
|
|
return this;
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
2010-03-31 00:15:51 +00:00
|
|
|
__extends(ReturnNode, BaseNode);
|
2010-07-31 00:29:05 +00:00
|
|
|
ReturnNode.prototype["class"] = 'ReturnNode';
|
2010-09-27 08:56:56 +00:00
|
|
|
ReturnNode.prototype.isStatement = YES;
|
|
|
|
ReturnNode.prototype.isPureStatement = YES;
|
2010-05-31 14:36:50 +00:00
|
|
|
ReturnNode.prototype.children = ['expression'];
|
2010-09-29 02:30:05 +00:00
|
|
|
ReturnNode.prototype.makeReturn = THIS;
|
2010-07-10 13:49:01 +00:00
|
|
|
ReturnNode.prototype.compile = function(o) {
|
2010-03-25 22:51:24 +00:00
|
|
|
var expr;
|
2010-06-12 23:05:13 +00:00
|
|
|
expr = this.expression.makeReturn();
|
2010-03-25 22:51:24 +00:00
|
|
|
if (!(expr instanceof ReturnNode)) {
|
|
|
|
return expr.compile(o);
|
|
|
|
}
|
2010-08-24 01:19:43 +00:00
|
|
|
return ReturnNode.__super__.compile.call(this, o);
|
2010-07-10 13:49:01 +00:00
|
|
|
};
|
|
|
|
ReturnNode.prototype.compileNode = function(o) {
|
2010-08-14 15:42:19 +00:00
|
|
|
if (this.expression.isStatement(o)) {
|
2010-06-12 23:05:13 +00:00
|
|
|
o.asStatement = true;
|
2010-03-21 15:28:05 +00:00
|
|
|
}
|
2010-10-04 12:50:50 +00:00
|
|
|
return "" + this.tab + "return " + (this.expression.compile(o)) + ";";
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
|
|
|
return ReturnNode;
|
2010-04-10 18:40:05 +00:00
|
|
|
})();
|
2010-02-28 00:19:53 +00:00
|
|
|
exports.ValueNode = (function() {
|
2010-09-19 12:29:15 +00:00
|
|
|
ValueNode = function(_arg, _arg2) {
|
|
|
|
this.properties = _arg2;
|
|
|
|
this.base = _arg;
|
2010-08-24 01:19:43 +00:00
|
|
|
ValueNode.__super__.constructor.call(this);
|
2010-08-14 22:02:07 +00:00
|
|
|
this.properties || (this.properties = []);
|
2010-02-09 03:22:59 +00:00
|
|
|
return this;
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
2010-03-31 00:15:51 +00:00
|
|
|
__extends(ValueNode, BaseNode);
|
2010-07-31 00:29:05 +00:00
|
|
|
ValueNode.prototype["class"] = 'ValueNode';
|
2010-05-31 14:36:50 +00:00
|
|
|
ValueNode.prototype.children = ['base', 'properties'];
|
2010-05-15 03:40:04 +00:00
|
|
|
ValueNode.prototype.push = function(prop) {
|
2010-02-09 03:22:59 +00:00
|
|
|
this.properties.push(prop);
|
2010-02-09 04:42:03 +00:00
|
|
|
return this;
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
2010-06-12 23:05:13 +00:00
|
|
|
ValueNode.prototype.hasProperties = function() {
|
2010-02-19 12:51:52 +00:00
|
|
|
return !!this.properties.length;
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
2010-06-12 23:05:13 +00:00
|
|
|
ValueNode.prototype.isArray = function() {
|
2010-10-01 22:17:35 +00:00
|
|
|
return this.base instanceof ArrayNode && !this.properties.length;
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
2010-06-12 23:05:13 +00:00
|
|
|
ValueNode.prototype.isObject = function() {
|
2010-10-01 22:17:35 +00:00
|
|
|
return this.base instanceof ObjectNode && !this.properties.length;
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
2010-06-12 23:05:13 +00:00
|
|
|
ValueNode.prototype.isSplice = function() {
|
2010-09-28 12:52:51 +00:00
|
|
|
return last(this.properties) instanceof SliceNode;
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
2010-09-27 08:56:56 +00:00
|
|
|
ValueNode.prototype.isComplex = function() {
|
2010-09-28 12:52:51 +00:00
|
|
|
return this.base.isComplex() || this.hasProperties();
|
2010-09-27 08:56:56 +00:00
|
|
|
};
|
2010-06-12 23:05:13 +00:00
|
|
|
ValueNode.prototype.makeReturn = function() {
|
2010-10-01 22:17:35 +00:00
|
|
|
return this.properties.length ? ValueNode.__super__.makeReturn.call(this) : this.base.makeReturn();
|
2010-03-21 11:17:58 +00:00
|
|
|
};
|
2010-05-15 03:40:04 +00:00
|
|
|
ValueNode.prototype.unwrap = function() {
|
2010-07-07 03:04:35 +00:00
|
|
|
return this.properties.length ? this : this.base;
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
2010-08-14 15:42:19 +00:00
|
|
|
ValueNode.prototype.isStatement = function(o) {
|
2010-09-29 02:30:05 +00:00
|
|
|
return this.base.isStatement(o) && !this.properties.length;
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
2010-06-12 23:05:13 +00:00
|
|
|
ValueNode.prototype.isNumber = function() {
|
2010-09-29 02:30:05 +00:00
|
|
|
return this.base instanceof LiteralNode && NUMBER.test(this.base.value);
|
2010-06-01 02:56:51 +00:00
|
|
|
};
|
2010-10-01 22:17:35 +00:00
|
|
|
ValueNode.prototype.cacheReference = function(o) {
|
|
|
|
var base, bref, name, nref;
|
|
|
|
name = last(this.properties);
|
2010-10-02 00:52:23 +00:00
|
|
|
if (!this.base.isComplex() && this.properties.length < 2 && !((name != null) ? name.isComplex() : undefined)) {
|
2010-10-01 22:17:35 +00:00
|
|
|
return [this, this];
|
2010-09-12 15:08:05 +00:00
|
|
|
}
|
2010-10-01 22:17:35 +00:00
|
|
|
base = new ValueNode(this.base, this.properties.slice(0, -1));
|
|
|
|
if (base.isComplex()) {
|
|
|
|
bref = literal(o.scope.freeVariable('base'));
|
|
|
|
base = new ValueNode(new ParentheticalNode(new AssignNode(bref, base)));
|
|
|
|
}
|
|
|
|
if (!(name)) {
|
|
|
|
return [base, bref];
|
2010-07-31 03:37:13 +00:00
|
|
|
}
|
2010-10-01 22:17:35 +00:00
|
|
|
if (name.isComplex()) {
|
|
|
|
nref = literal(o.scope.freeVariable('name'));
|
|
|
|
name = new IndexNode(new AssignNode(nref, name.index));
|
|
|
|
nref = new IndexNode(nref);
|
|
|
|
}
|
|
|
|
return [base.push(name), new ValueNode(bref || base.base, [nref || name])];
|
2010-07-31 03:37:13 +00:00
|
|
|
};
|
2010-07-10 13:49:01 +00:00
|
|
|
ValueNode.prototype.compile = function(o) {
|
2010-08-24 01:19:43 +00:00
|
|
|
return !o.top || this.properties.length ? ValueNode.__super__.compile.call(this, o) : this.base.compile(o);
|
2010-07-10 13:49:01 +00:00
|
|
|
};
|
2010-06-12 23:05:13 +00:00
|
|
|
ValueNode.prototype.compileNode = function(o) {
|
2010-10-01 22:26:37 +00:00
|
|
|
var _i, _len, code, ex, prop, props;
|
2010-10-01 22:17:35 +00:00
|
|
|
if (ex = this.unfoldSoak(o)) {
|
|
|
|
return ex.compile(o);
|
2010-09-12 20:18:05 +00:00
|
|
|
}
|
2010-10-01 22:17:35 +00:00
|
|
|
props = this.properties;
|
2010-08-14 21:25:29 +00:00
|
|
|
if (this.parenthetical && !props.length) {
|
|
|
|
this.base.parenthetical = true;
|
|
|
|
}
|
2010-10-01 22:17:35 +00:00
|
|
|
code = this.base.compile(o);
|
|
|
|
if (props[0] instanceof AccessorNode && this.isNumber() || o.top && this.base instanceof ObjectNode) {
|
2010-10-04 12:50:50 +00:00
|
|
|
code = ("(" + code + ")");
|
2010-02-16 00:13:08 +00:00
|
|
|
}
|
2010-10-01 22:26:37 +00:00
|
|
|
for (_i = 0, _len = props.length; _i < _len; _i++) {
|
|
|
|
prop = props[_i];
|
2010-10-01 22:17:35 +00:00
|
|
|
(code += prop.compile(o));
|
|
|
|
}
|
|
|
|
return code;
|
|
|
|
};
|
|
|
|
ValueNode.prototype.unfoldSoak = function(o) {
|
|
|
|
var _len, _ref2, fst, i, ifn, prop, ref, snd;
|
|
|
|
if (this.base.soakNode) {
|
|
|
|
Array.prototype.push.apply(this.base.body.properties, this.properties);
|
|
|
|
return this.base;
|
|
|
|
}
|
|
|
|
_ref2 = this.properties;
|
2010-09-21 02:13:08 +00:00
|
|
|
for (i = 0, _len = _ref2.length; i < _len; i++) {
|
|
|
|
prop = _ref2[i];
|
2010-09-12 18:44:03 +00:00
|
|
|
if (prop.soakNode) {
|
2010-10-01 22:17:35 +00:00
|
|
|
prop.soakNode = false;
|
|
|
|
fst = new ValueNode(this.base, this.properties.slice(0, i));
|
|
|
|
snd = new ValueNode(this.base, this.properties.slice(i));
|
|
|
|
if (fst.isComplex()) {
|
|
|
|
ref = literal(o.scope.freeVariable('ref'));
|
|
|
|
fst = new ParentheticalNode(new AssignNode(ref, fst));
|
|
|
|
snd.base = ref;
|
2010-02-09 03:22:59 +00:00
|
|
|
}
|
2010-10-01 22:17:35 +00:00
|
|
|
ifn = new IfNode(new ExistenceNode(fst), snd, {
|
|
|
|
operation: true
|
|
|
|
});
|
|
|
|
ifn.soakNode = true;
|
|
|
|
return ifn;
|
2010-09-12 18:44:03 +00:00
|
|
|
}
|
2010-02-09 03:22:59 +00:00
|
|
|
}
|
2010-10-01 22:17:35 +00:00
|
|
|
return null;
|
|
|
|
};
|
|
|
|
ValueNode.unfoldSoak = function(o, parent, name) {
|
|
|
|
var ifnode, node;
|
|
|
|
node = parent[name];
|
|
|
|
if (node instanceof IfNode && node.soakNode) {
|
|
|
|
ifnode = node;
|
|
|
|
} else if (node instanceof ValueNode) {
|
|
|
|
ifnode = node.unfoldSoak(o);
|
|
|
|
}
|
|
|
|
if (!(ifnode)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
parent[name] = ifnode.body;
|
|
|
|
ifnode.body = new ValueNode(parent);
|
|
|
|
return ifnode;
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
|
|
|
return ValueNode;
|
2010-10-01 22:17:35 +00:00
|
|
|
}).call(this);
|
2010-07-02 01:26:33 +00:00
|
|
|
exports.CommentNode = (function() {
|
2010-09-19 12:29:15 +00:00
|
|
|
CommentNode = function(_arg) {
|
|
|
|
this.comment = _arg;
|
2010-08-24 01:19:43 +00:00
|
|
|
CommentNode.__super__.constructor.call(this);
|
2010-07-02 01:26:33 +00:00
|
|
|
return this;
|
|
|
|
};
|
|
|
|
__extends(CommentNode, BaseNode);
|
2010-07-31 00:29:05 +00:00
|
|
|
CommentNode.prototype["class"] = 'CommentNode';
|
2010-09-27 08:56:56 +00:00
|
|
|
CommentNode.prototype.isStatement = YES;
|
2010-09-29 02:30:05 +00:00
|
|
|
CommentNode.prototype.makeReturn = THIS;
|
2010-07-02 01:26:33 +00:00
|
|
|
CommentNode.prototype.compileNode = function(o) {
|
2010-09-27 08:56:56 +00:00
|
|
|
return this.tab + '/*' + this.comment.replace(/\n/g, '\n' + this.tab) + '*/';
|
2010-07-02 01:26:33 +00:00
|
|
|
};
|
|
|
|
return CommentNode;
|
|
|
|
})();
|
2010-02-28 00:19:53 +00:00
|
|
|
exports.CallNode = (function() {
|
2010-09-19 12:29:15 +00:00
|
|
|
CallNode = function(variable, _arg, _arg2) {
|
|
|
|
this.exist = _arg2;
|
|
|
|
this.args = _arg;
|
2010-08-24 01:19:43 +00:00
|
|
|
CallNode.__super__.constructor.call(this);
|
2010-06-12 23:05:13 +00:00
|
|
|
this.isNew = false;
|
|
|
|
this.isSuper = variable === 'super';
|
|
|
|
this.variable = this.isSuper ? null : variable;
|
2010-08-14 22:02:07 +00:00
|
|
|
this.args || (this.args = []);
|
2010-02-09 03:55:56 +00:00
|
|
|
return this;
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
2010-03-31 00:15:51 +00:00
|
|
|
__extends(CallNode, BaseNode);
|
2010-07-31 00:29:05 +00:00
|
|
|
CallNode.prototype["class"] = 'CallNode';
|
2010-05-31 14:36:50 +00:00
|
|
|
CallNode.prototype.children = ['variable', 'args'];
|
2010-10-01 22:17:35 +00:00
|
|
|
CallNode.prototype.compileSplatArguments = function(o) {
|
|
|
|
return SplatNode.compileSplattedArray(this.args, o);
|
|
|
|
};
|
2010-06-12 23:05:13 +00:00
|
|
|
CallNode.prototype.newInstance = function() {
|
|
|
|
this.isNew = true;
|
2010-02-09 03:55:56 +00:00
|
|
|
return this;
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
2010-05-15 03:40:04 +00:00
|
|
|
CallNode.prototype.prefix = function() {
|
2010-07-07 03:04:35 +00:00
|
|
|
return this.isNew ? 'new ' : '';
|
2010-03-21 11:17:58 +00:00
|
|
|
};
|
2010-06-12 23:05:13 +00:00
|
|
|
CallNode.prototype.superReference = function(o) {
|
2010-10-01 22:17:35 +00:00
|
|
|
var method, name;
|
|
|
|
method = o.scope.method;
|
|
|
|
if (!(method)) {
|
|
|
|
throw Error("cannot call super outside of a function");
|
|
|
|
}
|
|
|
|
name = method.name;
|
|
|
|
if (!(name)) {
|
|
|
|
throw Error("cannot call super on an anonymous function.");
|
|
|
|
}
|
2010-10-04 12:50:50 +00:00
|
|
|
return method.klass ? ("" + (method.klass) + ".__super__." + name) : ("" + name + ".__super__.constructor");
|
2010-10-01 22:17:35 +00:00
|
|
|
};
|
|
|
|
CallNode.prototype.unfoldSoak = function(o) {
|
|
|
|
var _i, _len, _ref2, call, list, node;
|
|
|
|
call = this;
|
|
|
|
list = [];
|
|
|
|
while (true) {
|
|
|
|
if (call.variable instanceof CallNode) {
|
|
|
|
list.push(call);
|
|
|
|
call = call.variable;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!(call.variable instanceof ValueNode)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
list.push(call);
|
|
|
|
if (!((call = call.variable.base) instanceof CallNode)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ref2 = list.reverse();
|
|
|
|
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
|
|
|
|
call = _ref2[_i];
|
|
|
|
if (node) {
|
|
|
|
if (call.variable instanceof CallNode) {
|
|
|
|
call.variable = node;
|
|
|
|
} else {
|
|
|
|
call.variable.base = node;
|
|
|
|
}
|
2010-05-03 03:20:51 +00:00
|
|
|
}
|
2010-10-01 22:17:35 +00:00
|
|
|
node = ValueNode.unfoldSoak(o, call, 'variable');
|
|
|
|
}
|
|
|
|
return node;
|
2010-04-24 19:57:15 +00:00
|
|
|
};
|
2010-06-12 23:05:13 +00:00
|
|
|
CallNode.prototype.compileNode = function(o) {
|
2010-10-01 22:17:35 +00:00
|
|
|
var _i, _len, _ref2, _result, arg, args, left, node, rite, val;
|
|
|
|
if (node = this.unfoldSoak(o)) {
|
|
|
|
return node.compile(o);
|
2010-05-03 13:04:26 +00:00
|
|
|
}
|
2010-08-25 19:39:30 +00:00
|
|
|
if (this.exist) {
|
2010-10-01 22:17:35 +00:00
|
|
|
if (val = this.variable) {
|
|
|
|
if (!(val instanceof ValueNode)) {
|
|
|
|
val = new ValueNode(val);
|
|
|
|
}
|
|
|
|
_ref2 = val.cacheReference(o), left = _ref2[0], rite = _ref2[1];
|
|
|
|
rite = new CallNode(rite, this.args);
|
2010-09-18 10:27:14 +00:00
|
|
|
} else {
|
2010-10-01 22:17:35 +00:00
|
|
|
left = literal(this.superReference(o));
|
|
|
|
rite = new CallNode(new ValueNode(left), this.args);
|
|
|
|
rite.isNew = this.isNew;
|
2010-09-18 10:27:14 +00:00
|
|
|
}
|
2010-10-01 22:17:35 +00:00
|
|
|
left = ("typeof " + (left.compile(o)) + " !== \"function\"");
|
|
|
|
rite = rite.compile(o);
|
2010-10-04 12:50:50 +00:00
|
|
|
return ("(" + left + " ? undefined : " + rite + ")");
|
2010-08-25 19:39:30 +00:00
|
|
|
}
|
2010-09-21 02:13:08 +00:00
|
|
|
_ref2 = this.args;
|
|
|
|
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
|
|
|
|
arg = _ref2[_i];
|
2010-08-11 04:40:15 +00:00
|
|
|
if (arg instanceof SplatNode) {
|
2010-10-01 22:17:35 +00:00
|
|
|
return this.compileSplat(o);
|
2010-08-14 15:42:19 +00:00
|
|
|
}
|
2010-02-09 03:55:56 +00:00
|
|
|
}
|
2010-10-01 22:17:35 +00:00
|
|
|
args = (function() {
|
|
|
|
_result = []; _ref2 = this.args;
|
|
|
|
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
|
|
|
|
arg = _ref2[_i];
|
|
|
|
_result.push((arg.parenthetical = true) && arg.compile(o));
|
|
|
|
}
|
|
|
|
return _result;
|
|
|
|
}).call(this).join(', ');
|
2010-10-04 12:50:50 +00:00
|
|
|
return this.isSuper ? this.compileSuper(args, o) : ("" + (this.prefix()) + (this.variable.compile(o)) + "(" + args + ")");
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
2010-06-12 23:05:13 +00:00
|
|
|
CallNode.prototype.compileSuper = function(args, o) {
|
2010-10-04 12:50:50 +00:00
|
|
|
return "" + (this.superReference(o)) + ".call(this" + (args.length ? ', ' : '') + args + ")";
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
2010-06-12 23:05:13 +00:00
|
|
|
CallNode.prototype.compileSplat = function(o) {
|
2010-10-01 22:17:35 +00:00
|
|
|
var _i, _len, _ref2, a, arg, argvar, b, base, c, call, fun, idt, name, ref, splatargs;
|
|
|
|
splatargs = this.compileSplatArguments(o);
|
|
|
|
if (this.isSuper) {
|
2010-10-04 12:50:50 +00:00
|
|
|
return ("" + (this.superReference(o)) + ".apply(this, " + splatargs + ")");
|
2010-10-01 22:17:35 +00:00
|
|
|
}
|
|
|
|
if (!(this.isNew)) {
|
|
|
|
if (!((base = this.variable) instanceof ValueNode)) {
|
|
|
|
base = new ValueNode(base);
|
|
|
|
}
|
|
|
|
if ((name = base.properties.pop()) && base.isComplex()) {
|
|
|
|
ref = o.scope.freeVariable('this');
|
2010-10-04 12:50:50 +00:00
|
|
|
fun = ("(" + ref + " = " + (base.compile(o)) + ")" + (name.compile(o)));
|
2010-10-01 22:17:35 +00:00
|
|
|
} else {
|
|
|
|
fun = (ref = base.compile(o));
|
|
|
|
if (name) {
|
|
|
|
fun += name.compile(o);
|
|
|
|
}
|
|
|
|
}
|
2010-10-04 12:50:50 +00:00
|
|
|
return ("" + fun + ".apply(" + ref + ", " + splatargs + ")");
|
2010-10-01 22:17:35 +00:00
|
|
|
}
|
|
|
|
call = 'call(this)';
|
|
|
|
argvar = function(n) {
|
|
|
|
return n instanceof LiteralNode && n.value === 'arguments';
|
|
|
|
};
|
|
|
|
_ref2 = this.args;
|
|
|
|
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
|
|
|
|
arg = _ref2[_i];
|
|
|
|
if (arg.contains(argvar)) {
|
|
|
|
call = 'apply(this, arguments)';
|
|
|
|
break;
|
2010-09-08 12:18:08 +00:00
|
|
|
}
|
2010-07-13 02:42:21 +00:00
|
|
|
}
|
2010-10-01 22:17:35 +00:00
|
|
|
a = o.scope.freeVariable('ctor');
|
|
|
|
b = o.scope.freeVariable('ref');
|
|
|
|
c = o.scope.freeVariable('result');
|
2010-10-04 12:50:50 +00:00
|
|
|
return "(function() {\n" + (idt = this.idt(1)) + "var ctor = function() {};\n" + idt + (utility('extends')) + "(ctor, " + a + " = " + (this.variable.compile(o)) + ");\n" + idt + "return typeof (" + c + " = " + a + ".apply(" + b + " = new ctor, " + splatargs + ")) === \"object\" ? " + c + " : " + b + ";\n" + this.tab + "})." + call;
|
2010-03-17 16:25:04 +00:00
|
|
|
};
|
2010-02-28 00:19:53 +00:00
|
|
|
return CallNode;
|
2010-04-10 18:40:05 +00:00
|
|
|
})();
|
2010-02-28 00:19:53 +00:00
|
|
|
exports.ExtendsNode = (function() {
|
2010-09-19 12:29:15 +00:00
|
|
|
ExtendsNode = function(_arg, _arg2) {
|
|
|
|
this.parent = _arg2;
|
|
|
|
this.child = _arg;
|
2010-08-24 01:19:43 +00:00
|
|
|
ExtendsNode.__super__.constructor.call(this);
|
2010-02-09 04:16:29 +00:00
|
|
|
return this;
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
2010-03-31 00:15:51 +00:00
|
|
|
__extends(ExtendsNode, BaseNode);
|
2010-07-31 00:29:05 +00:00
|
|
|
ExtendsNode.prototype["class"] = 'ExtendsNode';
|
2010-05-31 14:36:50 +00:00
|
|
|
ExtendsNode.prototype.children = ['child', 'parent'];
|
2010-06-12 23:05:13 +00:00
|
|
|
ExtendsNode.prototype.compileNode = function(o) {
|
2010-03-30 13:02:51 +00:00
|
|
|
var ref;
|
2010-03-31 00:15:51 +00:00
|
|
|
ref = new ValueNode(literal(utility('extends')));
|
2010-03-30 13:02:51 +00:00
|
|
|
return (new CallNode(ref, [this.child, this.parent])).compile(o);
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
|
|
|
return ExtendsNode;
|
2010-04-10 18:40:05 +00:00
|
|
|
})();
|
2010-02-28 00:19:53 +00:00
|
|
|
exports.AccessorNode = (function() {
|
2010-09-19 12:29:15 +00:00
|
|
|
AccessorNode = function(_arg, tag) {
|
|
|
|
this.name = _arg;
|
2010-08-24 01:19:43 +00:00
|
|
|
AccessorNode.__super__.constructor.call(this);
|
2010-06-15 04:54:02 +00:00
|
|
|
this.prototype = tag === 'prototype' ? '.prototype' : '';
|
2010-06-12 23:05:13 +00:00
|
|
|
this.soakNode = tag === 'soak';
|
2010-02-09 04:42:03 +00:00
|
|
|
return this;
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
2010-03-31 00:15:51 +00:00
|
|
|
__extends(AccessorNode, BaseNode);
|
2010-07-31 00:29:05 +00:00
|
|
|
AccessorNode.prototype["class"] = 'AccessorNode';
|
2010-05-31 14:36:50 +00:00
|
|
|
AccessorNode.prototype.children = ['name'];
|
2010-06-12 23:05:13 +00:00
|
|
|
AccessorNode.prototype.compileNode = function(o) {
|
2010-10-01 23:33:57 +00:00
|
|
|
var name, namePart;
|
2010-06-15 04:54:02 +00:00
|
|
|
name = this.name.compile(o);
|
2010-10-04 12:50:50 +00:00
|
|
|
namePart = name.match(IS_STRING) ? ("[" + name + "]") : ("." + name);
|
2010-06-15 04:54:02 +00:00
|
|
|
return this.prototype + namePart;
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
2010-09-27 08:56:56 +00:00
|
|
|
AccessorNode.prototype.isComplex = NO;
|
2010-02-28 00:19:53 +00:00
|
|
|
return AccessorNode;
|
2010-04-10 18:40:05 +00:00
|
|
|
})();
|
2010-02-28 00:19:53 +00:00
|
|
|
exports.IndexNode = (function() {
|
2010-09-19 12:29:15 +00:00
|
|
|
IndexNode = function(_arg) {
|
|
|
|
this.index = _arg;
|
2010-08-24 01:19:43 +00:00
|
|
|
IndexNode.__super__.constructor.call(this);
|
2010-02-09 04:45:46 +00:00
|
|
|
return this;
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
2010-03-31 00:15:51 +00:00
|
|
|
__extends(IndexNode, BaseNode);
|
2010-07-31 00:29:05 +00:00
|
|
|
IndexNode.prototype["class"] = 'IndexNode';
|
2010-05-31 14:36:50 +00:00
|
|
|
IndexNode.prototype.children = ['index'];
|
2010-06-12 23:05:13 +00:00
|
|
|
IndexNode.prototype.compileNode = function(o) {
|
2010-10-01 23:33:57 +00:00
|
|
|
var idx, prefix;
|
2010-03-06 05:23:54 +00:00
|
|
|
idx = this.index.compile(o);
|
2010-06-01 02:32:43 +00:00
|
|
|
prefix = this.proto ? '.prototype' : '';
|
2010-10-04 12:50:50 +00:00
|
|
|
return "" + prefix + "[" + idx + "]";
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
2010-09-27 08:56:56 +00:00
|
|
|
IndexNode.prototype.isComplex = function() {
|
|
|
|
return this.index.isComplex();
|
|
|
|
};
|
2010-02-28 00:19:53 +00:00
|
|
|
return IndexNode;
|
2010-04-10 18:40:05 +00:00
|
|
|
})();
|
2010-02-28 00:19:53 +00:00
|
|
|
exports.RangeNode = (function() {
|
2010-09-19 12:29:15 +00:00
|
|
|
RangeNode = function(_arg, _arg2, tag) {
|
|
|
|
this.to = _arg2;
|
|
|
|
this.from = _arg;
|
2010-08-24 01:19:43 +00:00
|
|
|
RangeNode.__super__.constructor.call(this);
|
2010-09-04 10:48:20 +00:00
|
|
|
this.exclusive = tag === 'exclusive';
|
2010-07-17 22:45:29 +00:00
|
|
|
this.equals = this.exclusive ? '' : '=';
|
2010-02-10 00:30:28 +00:00
|
|
|
return this;
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
2010-03-31 00:15:51 +00:00
|
|
|
__extends(RangeNode, BaseNode);
|
2010-07-31 00:29:05 +00:00
|
|
|
RangeNode.prototype["class"] = 'RangeNode';
|
2010-05-31 14:36:50 +00:00
|
|
|
RangeNode.prototype.children = ['from', 'to'];
|
2010-06-12 23:05:13 +00:00
|
|
|
RangeNode.prototype.compileVariables = function(o) {
|
2010-09-21 02:13:08 +00:00
|
|
|
var _ref2, parts;
|
2010-07-22 17:37:13 +00:00
|
|
|
o = merge(o, {
|
|
|
|
top: true
|
|
|
|
});
|
2010-09-21 01:56:18 +00:00
|
|
|
_ref2 = this.from.compileReference(o, {
|
2010-07-17 22:45:29 +00:00
|
|
|
precompile: true
|
2010-09-28 20:47:12 +00:00
|
|
|
}), this.from = _ref2[0], this.fromVar = _ref2[1];
|
2010-09-21 02:13:08 +00:00
|
|
|
_ref2 = this.to.compileReference(o, {
|
2010-07-17 22:45:29 +00:00
|
|
|
precompile: true
|
2010-09-28 20:47:12 +00:00
|
|
|
}), this.to = _ref2[0], this.toVar = _ref2[1];
|
|
|
|
_ref2 = [this.fromVar.match(SIMPLENUM), this.toVar.match(SIMPLENUM)], this.fromNum = _ref2[0], this.toNum = _ref2[1];
|
2010-05-31 23:38:45 +00:00
|
|
|
parts = [];
|
2010-06-12 23:05:13 +00:00
|
|
|
if (this.from !== this.fromVar) {
|
2010-07-17 22:45:29 +00:00
|
|
|
parts.push(this.from);
|
2010-05-31 23:38:45 +00:00
|
|
|
}
|
2010-06-12 23:05:13 +00:00
|
|
|
if (this.to !== this.toVar) {
|
2010-07-17 22:45:29 +00:00
|
|
|
parts.push(this.to);
|
2010-05-31 23:38:45 +00:00
|
|
|
}
|
2010-07-17 22:45:29 +00:00
|
|
|
return parts.length ? ("" + (parts.join('; ')) + "; ") : '';
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
2010-06-12 23:05:13 +00:00
|
|
|
RangeNode.prototype.compileNode = function(o) {
|
2010-07-17 22:45:29 +00:00
|
|
|
var compare, idx, incr, intro, step, stepPart, vars;
|
2010-02-10 00:30:28 +00:00
|
|
|
if (!(o.index)) {
|
2010-06-12 23:05:13 +00:00
|
|
|
return this.compileArray(o);
|
2010-02-10 00:30:28 +00:00
|
|
|
}
|
2010-07-17 22:45:29 +00:00
|
|
|
if (this.fromNum && this.toNum) {
|
|
|
|
return this.compileSimple(o);
|
|
|
|
}
|
2010-03-14 21:58:32 +00:00
|
|
|
idx = del(o, 'index');
|
|
|
|
step = del(o, 'step');
|
2010-10-04 12:50:50 +00:00
|
|
|
vars = ("" + idx + " = " + this.fromVar);
|
|
|
|
intro = ("(" + this.fromVar + " <= " + this.toVar + " ? " + idx);
|
|
|
|
compare = ("" + intro + " <" + this.equals + " " + this.toVar + " : " + idx + " >" + this.equals + " " + this.toVar + ")");
|
2010-07-17 22:45:29 +00:00
|
|
|
stepPart = step ? step.compile(o) : '1';
|
2010-10-04 12:50:50 +00:00
|
|
|
incr = step ? ("" + idx + " += " + stepPart) : ("" + intro + " += " + stepPart + " : " + idx + " -= " + stepPart + ")");
|
|
|
|
return "" + vars + "; " + compare + "; " + incr;
|
2010-07-17 22:45:29 +00:00
|
|
|
};
|
|
|
|
RangeNode.prototype.compileSimple = function(o) {
|
2010-09-21 01:56:18 +00:00
|
|
|
var _ref2, from, idx, step, to;
|
2010-09-28 20:47:12 +00:00
|
|
|
_ref2 = [+this.fromNum, +this.toNum], from = _ref2[0], to = _ref2[1];
|
2010-07-17 22:45:29 +00:00
|
|
|
idx = del(o, 'index');
|
|
|
|
step = del(o, 'step');
|
2010-10-04 12:50:50 +00:00
|
|
|
step && (step = ("" + idx + " += " + (step.compile(o))));
|
|
|
|
return from <= to ? ("" + idx + " = " + from + "; " + idx + " <" + this.equals + " " + to + "; " + (step || ("" + idx + "++"))) : ("" + idx + " = " + from + "; " + idx + " >" + this.equals + " " + to + "; " + (step || ("" + idx + "--")));
|
2010-05-31 23:38:45 +00:00
|
|
|
};
|
2010-06-12 23:05:13 +00:00
|
|
|
RangeNode.prototype.compileArray = function(o) {
|
2010-09-27 08:56:56 +00:00
|
|
|
var _i, _ref2, _ref3, _result, body, clause, i, idt, post, pre, range, result, vars;
|
2010-05-31 23:38:45 +00:00
|
|
|
idt = this.idt(1);
|
2010-06-12 23:05:13 +00:00
|
|
|
vars = this.compileVariables(merge(o, {
|
2010-05-31 23:38:45 +00:00
|
|
|
indent: idt
|
|
|
|
}));
|
2010-09-28 02:17:46 +00:00
|
|
|
if (this.fromNum && this.toNum && (Math.abs(this.fromNum - this.toNum) <= 20)) {
|
2010-07-28 03:39:28 +00:00
|
|
|
range = (function() {
|
2010-09-27 08:56:56 +00:00
|
|
|
_result = []; _ref2 = +this.fromNum; _ref3 = +this.toNum;
|
|
|
|
for (var _i = _ref2; _ref2 <= _ref3 ? _i <= _ref3 : _i >= _ref3; _ref2 <= _ref3 ? _i += 1 : _i -= 1){ _result.push(_i); }
|
2010-09-19 12:29:15 +00:00
|
|
|
return _result;
|
2010-07-28 03:39:28 +00:00
|
|
|
}).call(this);
|
|
|
|
if (this.exclusive) {
|
|
|
|
range.pop();
|
|
|
|
}
|
|
|
|
return ("[" + (range.join(', ')) + "]");
|
|
|
|
}
|
2010-09-21 01:56:18 +00:00
|
|
|
i = o.scope.freeVariable('i');
|
2010-09-19 12:29:15 +00:00
|
|
|
result = o.scope.freeVariable('result');
|
2010-10-04 12:50:50 +00:00
|
|
|
pre = ("\n" + idt + result + " = []; " + vars);
|
2010-07-17 22:45:29 +00:00
|
|
|
if (this.fromNum && this.toNum) {
|
2010-07-28 03:39:28 +00:00
|
|
|
o.index = i;
|
|
|
|
body = this.compileSimple(o);
|
2010-07-17 22:45:29 +00:00
|
|
|
} else {
|
2010-10-04 12:50:50 +00:00
|
|
|
clause = ("" + this.fromVar + " <= " + this.toVar + " ?");
|
|
|
|
body = ("var " + i + " = " + this.fromVar + "; " + clause + " " + i + " <" + this.equals + " " + this.toVar + " : " + i + " >" + this.equals + " " + this.toVar + "; " + clause + " " + i + " += 1 : " + i + " -= 1");
|
2010-07-17 22:45:29 +00:00
|
|
|
}
|
2010-10-04 12:50:50 +00:00
|
|
|
post = ("{ " + result + ".push(" + i + "); }\n" + idt + "return " + result + ";\n" + (o.indent));
|
|
|
|
return "(function() {" + pre + "\n" + idt + "for (" + body + ")" + post + "}).call(this)";
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
|
|
|
return RangeNode;
|
2010-04-10 18:40:05 +00:00
|
|
|
})();
|
2010-02-28 00:19:53 +00:00
|
|
|
exports.SliceNode = (function() {
|
2010-09-19 12:29:15 +00:00
|
|
|
SliceNode = function(_arg) {
|
|
|
|
this.range = _arg;
|
2010-08-24 01:19:43 +00:00
|
|
|
SliceNode.__super__.constructor.call(this);
|
2010-02-10 00:30:28 +00:00
|
|
|
return this;
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
2010-03-31 00:15:51 +00:00
|
|
|
__extends(SliceNode, BaseNode);
|
2010-07-31 00:29:05 +00:00
|
|
|
SliceNode.prototype["class"] = 'SliceNode';
|
2010-05-31 14:36:50 +00:00
|
|
|
SliceNode.prototype.children = ['range'];
|
2010-06-12 23:05:13 +00:00
|
|
|
SliceNode.prototype.compileNode = function(o) {
|
2010-08-19 01:51:44 +00:00
|
|
|
var from, to;
|
|
|
|
from = this.range.from ? this.range.from.compile(o) : '0';
|
|
|
|
to = this.range.to ? this.range.to.compile(o) : '';
|
|
|
|
to += (!to || this.range.exclusive ? '' : ' + 1');
|
|
|
|
if (to) {
|
|
|
|
to = ', ' + to;
|
|
|
|
}
|
2010-10-04 12:50:50 +00:00
|
|
|
return ".slice(" + from + to + ")";
|
2010-03-21 16:11:44 +00:00
|
|
|
};
|
2010-02-28 00:19:53 +00:00
|
|
|
return SliceNode;
|
2010-04-10 18:40:05 +00:00
|
|
|
})();
|
2010-02-28 00:19:53 +00:00
|
|
|
exports.ObjectNode = (function() {
|
2010-05-15 03:40:04 +00:00
|
|
|
ObjectNode = function(props) {
|
2010-08-24 01:19:43 +00:00
|
|
|
ObjectNode.__super__.constructor.call(this);
|
2010-05-10 10:58:01 +00:00
|
|
|
this.objects = (this.properties = props || []);
|
2010-02-10 01:53:25 +00:00
|
|
|
return this;
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
2010-03-31 00:15:51 +00:00
|
|
|
__extends(ObjectNode, BaseNode);
|
2010-07-31 00:29:05 +00:00
|
|
|
ObjectNode.prototype["class"] = 'ObjectNode';
|
2010-05-31 14:36:50 +00:00
|
|
|
ObjectNode.prototype.children = ['properties'];
|
2010-09-27 08:56:56 +00:00
|
|
|
ObjectNode.prototype.topSensitive = YES;
|
2010-06-12 23:05:13 +00:00
|
|
|
ObjectNode.prototype.compileNode = function(o) {
|
2010-09-21 02:13:08 +00:00
|
|
|
var _i, _len, _ref2, _result, i, indent, join, lastNoncom, nonComments, obj, prop, props, top;
|
2010-08-11 02:16:51 +00:00
|
|
|
top = del(o, 'top');
|
2010-02-10 01:53:25 +00:00
|
|
|
o.indent = this.idt(1);
|
2010-07-02 01:26:33 +00:00
|
|
|
nonComments = (function() {
|
2010-09-21 01:56:18 +00:00
|
|
|
_result = []; _ref2 = this.properties;
|
2010-09-21 02:13:08 +00:00
|
|
|
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
|
2010-09-21 01:56:18 +00:00
|
|
|
prop = _ref2[_i];
|
2010-08-11 04:40:15 +00:00
|
|
|
if (!(prop instanceof CommentNode)) {
|
2010-09-19 12:29:15 +00:00
|
|
|
_result.push(prop);
|
2010-08-14 15:42:19 +00:00
|
|
|
}
|
2010-07-02 01:26:33 +00:00
|
|
|
}
|
2010-09-19 12:29:15 +00:00
|
|
|
return _result;
|
2010-07-02 01:26:33 +00:00
|
|
|
}).call(this);
|
2010-09-28 12:52:51 +00:00
|
|
|
lastNoncom = last(nonComments);
|
2010-02-10 01:53:25 +00:00
|
|
|
props = (function() {
|
2010-09-21 02:13:08 +00:00
|
|
|
_result = []; _ref2 = this.properties;
|
|
|
|
for (i = 0, _len = _ref2.length; i < _len; i++) {
|
|
|
|
prop = _ref2[i];
|
2010-09-19 16:34:27 +00:00
|
|
|
_result.push((function() {
|
2010-07-02 01:26:33 +00:00
|
|
|
join = ",\n";
|
|
|
|
if ((prop === lastNoncom) || (prop instanceof CommentNode)) {
|
|
|
|
join = "\n";
|
|
|
|
}
|
|
|
|
if (i === this.properties.length - 1) {
|
|
|
|
join = '';
|
|
|
|
}
|
2010-07-26 03:59:28 +00:00
|
|
|
indent = prop instanceof CommentNode ? '' : this.idt(1);
|
2010-07-02 01:26:33 +00:00
|
|
|
if (!(prop instanceof AssignNode || prop instanceof CommentNode)) {
|
2010-04-26 03:54:47 +00:00
|
|
|
prop = new AssignNode(prop, prop, 'object');
|
|
|
|
}
|
2010-07-02 01:26:33 +00:00
|
|
|
return indent + prop.compile(o) + join;
|
2010-02-10 01:53:25 +00:00
|
|
|
}).call(this));
|
|
|
|
}
|
2010-09-19 16:34:27 +00:00
|
|
|
return _result;
|
2010-02-10 01:53:25 +00:00
|
|
|
}).call(this);
|
2010-02-12 04:57:31 +00:00
|
|
|
props = props.join('');
|
2010-08-11 02:16:51 +00:00
|
|
|
obj = '{' + (props ? '\n' + props + '\n' + this.idt() : '') + '}';
|
2010-10-04 12:50:50 +00:00
|
|
|
return top ? ("(" + obj + ")") : obj;
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
|
|
|
return ObjectNode;
|
2010-04-10 18:40:05 +00:00
|
|
|
})();
|
2010-03-08 00:11:03 +00:00
|
|
|
exports.ArrayNode = (function() {
|
2010-09-19 12:29:15 +00:00
|
|
|
ArrayNode = function(_arg) {
|
|
|
|
this.objects = _arg;
|
2010-08-24 01:19:43 +00:00
|
|
|
ArrayNode.__super__.constructor.call(this);
|
2010-08-14 22:02:07 +00:00
|
|
|
this.objects || (this.objects = []);
|
2010-03-08 00:11:03 +00:00
|
|
|
return this;
|
|
|
|
};
|
2010-03-31 00:15:51 +00:00
|
|
|
__extends(ArrayNode, BaseNode);
|
2010-07-31 00:29:05 +00:00
|
|
|
ArrayNode.prototype["class"] = 'ArrayNode';
|
2010-05-31 14:36:50 +00:00
|
|
|
ArrayNode.prototype.children = ['objects'];
|
2010-10-01 22:17:35 +00:00
|
|
|
ArrayNode.prototype.compileSplatLiteral = function(o) {
|
|
|
|
return SplatNode.compileSplattedArray(this.objects, o);
|
|
|
|
};
|
2010-06-12 23:05:13 +00:00
|
|
|
ArrayNode.prototype.compileNode = function(o) {
|
2010-09-21 02:13:08 +00:00
|
|
|
var _len, _ref2, code, i, obj, objects;
|
2010-03-08 00:11:03 +00:00
|
|
|
o.indent = this.idt(1);
|
2010-03-21 15:28:05 +00:00
|
|
|
objects = [];
|
2010-09-21 01:56:18 +00:00
|
|
|
_ref2 = this.objects;
|
2010-09-21 02:13:08 +00:00
|
|
|
for (i = 0, _len = _ref2.length; i < _len; i++) {
|
2010-09-21 01:56:18 +00:00
|
|
|
obj = _ref2[i];
|
2010-03-21 15:28:05 +00:00
|
|
|
code = obj.compile(o);
|
|
|
|
if (obj instanceof SplatNode) {
|
2010-07-14 01:51:27 +00:00
|
|
|
return this.compileSplatLiteral(o);
|
2010-07-02 01:26:33 +00:00
|
|
|
} else if (obj instanceof CommentNode) {
|
2010-10-04 12:50:50 +00:00
|
|
|
objects.push("\n" + code + "\n" + (o.indent));
|
2010-03-21 15:28:05 +00:00
|
|
|
} else if (i === this.objects.length - 1) {
|
|
|
|
objects.push(code);
|
|
|
|
} else {
|
2010-10-04 12:50:50 +00:00
|
|
|
objects.push("" + code + ", ");
|
2010-03-08 00:11:03 +00:00
|
|
|
}
|
2010-03-21 15:28:05 +00:00
|
|
|
}
|
2010-03-08 00:11:03 +00:00
|
|
|
objects = objects.join('');
|
2010-10-04 12:50:50 +00:00
|
|
|
return indexOf(objects, '\n') >= 0 ? ("[\n" + (this.idt(1)) + objects + "\n" + this.tab + "]") : ("[" + objects + "]");
|
2010-03-08 00:11:03 +00:00
|
|
|
};
|
|
|
|
return ArrayNode;
|
2010-04-10 18:40:05 +00:00
|
|
|
})();
|
2010-02-28 00:19:53 +00:00
|
|
|
exports.ClassNode = (function() {
|
2010-09-19 12:29:15 +00:00
|
|
|
ClassNode = function(_arg, _arg2, _arg3) {
|
|
|
|
this.properties = _arg3;
|
|
|
|
this.parent = _arg2;
|
|
|
|
this.variable = _arg;
|
2010-08-24 01:19:43 +00:00
|
|
|
ClassNode.__super__.constructor.call(this);
|
2010-08-14 22:02:07 +00:00
|
|
|
this.properties || (this.properties = []);
|
2010-03-21 15:28:05 +00:00
|
|
|
this.returns = false;
|
2010-02-27 23:57:45 +00:00
|
|
|
return this;
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
2010-03-31 00:15:51 +00:00
|
|
|
__extends(ClassNode, BaseNode);
|
2010-07-31 00:29:05 +00:00
|
|
|
ClassNode.prototype["class"] = 'ClassNode';
|
2010-05-31 14:36:50 +00:00
|
|
|
ClassNode.prototype.children = ['variable', 'parent', 'properties'];
|
2010-09-27 08:56:56 +00:00
|
|
|
ClassNode.prototype.isStatement = YES;
|
2010-06-12 23:05:13 +00:00
|
|
|
ClassNode.prototype.makeReturn = function() {
|
2010-03-21 15:28:05 +00:00
|
|
|
this.returns = true;
|
2010-03-21 11:17:58 +00:00
|
|
|
return this;
|
|
|
|
};
|
2010-06-12 23:05:13 +00:00
|
|
|
ClassNode.prototype.compileNode = function(o) {
|
2010-09-21 02:13:08 +00:00
|
|
|
var _i, _len, _ref2, _ref3, access, applied, className, constScope, construct, constructor, extension, func, me, pname, prop, props, pvar, returns, val;
|
2010-07-28 02:05:55 +00:00
|
|
|
if (this.variable === '__temp__') {
|
2010-09-21 01:56:18 +00:00
|
|
|
this.variable = literal(o.scope.freeVariable('ctor'));
|
2010-07-28 02:05:55 +00:00
|
|
|
}
|
2010-02-27 23:57:45 +00:00
|
|
|
extension = this.parent && new ExtendsNode(this.variable, this.parent);
|
2010-09-25 08:39:19 +00:00
|
|
|
props = new Expressions;
|
2010-02-27 23:57:45 +00:00
|
|
|
o.top = true;
|
2010-06-15 06:21:01 +00:00
|
|
|
me = null;
|
|
|
|
className = this.variable.compile(o);
|
|
|
|
constScope = null;
|
2010-06-15 05:28:30 +00: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 {
|
2010-09-25 08:39:19 +00:00
|
|
|
constructor = new CodeNode;
|
2010-06-15 05:28:30 +00:00
|
|
|
}
|
2010-09-21 01:56:18 +00:00
|
|
|
_ref2 = this.properties;
|
2010-09-21 02:13:08 +00:00
|
|
|
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
|
2010-09-21 01:56:18 +00:00
|
|
|
prop = _ref2[_i];
|
2010-09-28 20:47:12 +00:00
|
|
|
_ref3 = [prop.variable, prop.value], pvar = _ref3[0], func = _ref3[1];
|
2010-04-03 18:53:26 +00:00
|
|
|
if (pvar && pvar.base.value === 'constructor' && func instanceof CodeNode) {
|
2010-07-01 02:03:20 +00:00
|
|
|
if (func.bound) {
|
|
|
|
throw new Error("cannot define a constructor as a bound function.");
|
|
|
|
}
|
2010-06-15 06:21:01 +00:00
|
|
|
func.name = className;
|
2010-02-28 05:22:06 +00:00
|
|
|
func.body.push(new ReturnNode(literal('this')));
|
2010-05-25 06:15:46 +00:00
|
|
|
this.variable = new ValueNode(this.variable);
|
|
|
|
this.variable.namespaced = include(func.name, '.');
|
2010-06-15 05:28:30 +00:00
|
|
|
constructor = func;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (func instanceof CodeNode && func.bound) {
|
2010-08-18 01:07:36 +00:00
|
|
|
if (prop.context === 'this') {
|
|
|
|
func.context = className;
|
|
|
|
} else {
|
|
|
|
func.bound = false;
|
|
|
|
constScope || (constScope = new Scope(o.scope, constructor.body, constructor));
|
2010-09-21 01:56:18 +00:00
|
|
|
me || (me = constScope.freeVariable('this'));
|
2010-08-18 01:07:36 +00:00
|
|
|
pname = pvar.compile(o);
|
|
|
|
if (constructor.body.empty()) {
|
|
|
|
constructor.body.push(new ReturnNode(literal('this')));
|
|
|
|
}
|
2010-10-04 12:50:50 +00:00
|
|
|
constructor.body.unshift(literal("this." + pname + " = function(){ return " + className + ".prototype." + pname + ".apply(" + me + ", arguments); }"));
|
2010-06-15 05:28:30 +00:00
|
|
|
}
|
2010-02-27 23:57:45 +00:00
|
|
|
}
|
2010-06-15 06:21:01 +00: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) {
|
2010-10-04 12:50:50 +00:00
|
|
|
constructor.body.unshift(literal("" + me + " = this"));
|
2010-02-27 23:57:45 +00:00
|
|
|
}
|
2010-06-15 06:21:01 +00:00
|
|
|
construct = this.idt() + (new AssignNode(this.variable, constructor)).compile(merge(o, {
|
|
|
|
sharedScope: constScope
|
2010-07-13 02:51:10 +00:00
|
|
|
})) + ';';
|
|
|
|
props = !props.empty() ? '\n' + props.compile(o) : '';
|
|
|
|
extension = extension ? '\n' + this.idt() + extension.compile(o) + ';' : '';
|
2010-08-21 23:16:02 +00:00
|
|
|
returns = this.returns ? '\n' + new ReturnNode(this.variable).compile(o) : '';
|
2010-08-07 12:02:16 +00:00
|
|
|
return construct + extension + props + returns;
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
|
|
|
return ClassNode;
|
2010-04-10 18:40:05 +00:00
|
|
|
})();
|
2010-02-28 00:19:53 +00:00
|
|
|
exports.AssignNode = (function() {
|
2010-09-19 12:29:15 +00:00
|
|
|
AssignNode = function(_arg, _arg2, _arg3) {
|
|
|
|
this.context = _arg3;
|
|
|
|
this.value = _arg2;
|
|
|
|
this.variable = _arg;
|
2010-08-24 01:19:43 +00:00
|
|
|
AssignNode.__super__.constructor.call(this);
|
2010-02-10 01:53:25 +00:00
|
|
|
return this;
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
2010-03-31 00:15:51 +00:00
|
|
|
__extends(AssignNode, BaseNode);
|
2010-10-01 22:17:35 +00:00
|
|
|
AssignNode.prototype.METHOD_DEF = /^(?:(\S+)\.prototype\.)?([$A-Za-z_][$\w]*)$/;
|
2010-07-31 00:29:05 +00:00
|
|
|
AssignNode.prototype["class"] = 'AssignNode';
|
2010-05-31 14:36:50 +00:00
|
|
|
AssignNode.prototype.children = ['variable', 'value'];
|
2010-09-27 08:56:56 +00:00
|
|
|
AssignNode.prototype.topSensitive = YES;
|
2010-06-12 23:05:13 +00:00
|
|
|
AssignNode.prototype.isValue = function() {
|
2010-02-10 01:53:25 +00:00
|
|
|
return this.variable instanceof ValueNode;
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
2010-06-12 23:05:13 +00:00
|
|
|
AssignNode.prototype.compileNode = function(o) {
|
2010-10-01 22:17:35 +00:00
|
|
|
var isValue, match, name, node, stmt, top, val;
|
2010-09-28 20:47:12 +00:00
|
|
|
if (isValue = this.isValue()) {
|
|
|
|
if (this.variable.isArray() || this.variable.isObject()) {
|
|
|
|
return this.compilePatternMatch(o);
|
|
|
|
}
|
|
|
|
if (this.variable.isSplice()) {
|
|
|
|
return this.compileSplice(o);
|
|
|
|
}
|
2010-10-01 22:17:35 +00:00
|
|
|
if (node = ValueNode.unfoldSoak(o, this, 'variable')) {
|
|
|
|
return node.compile(o);
|
|
|
|
}
|
2010-02-10 01:53:25 +00:00
|
|
|
}
|
2010-09-28 20:47:12 +00:00
|
|
|
top = del(o, 'top');
|
2010-06-12 23:05:13 +00:00
|
|
|
stmt = del(o, 'asStatement');
|
2010-02-10 01:53:25 +00:00
|
|
|
name = this.variable.compile(o);
|
2010-10-01 22:17:35 +00:00
|
|
|
if (this.value instanceof CodeNode && (match = this.METHOD_DEF.exec(name))) {
|
|
|
|
this.value.name = match[2];
|
|
|
|
this.value.klass = match[1];
|
2010-02-10 01:53:25 +00:00
|
|
|
}
|
2010-03-06 05:23:54 +00:00
|
|
|
val = this.value.compile(o);
|
2010-02-10 01:53:25 +00:00
|
|
|
if (this.context === 'object') {
|
2010-10-04 12:50:50 +00:00
|
|
|
return ("" + name + ": " + val);
|
2010-02-10 01:53:25 +00:00
|
|
|
}
|
2010-09-28 20:47:12 +00:00
|
|
|
if (!(isValue && (this.variable.hasProperties() || this.variable.namespaced))) {
|
2010-02-10 01:53:25 +00:00
|
|
|
o.scope.find(name);
|
|
|
|
}
|
2010-10-04 12:50:50 +00:00
|
|
|
val = ("" + name + " = " + val);
|
2010-02-10 01:53:25 +00:00
|
|
|
if (stmt) {
|
2010-10-04 12:50:50 +00:00
|
|
|
return ("" + this.tab + val + ";");
|
2010-02-10 01:53:25 +00:00
|
|
|
}
|
2010-10-04 12:50:50 +00:00
|
|
|
return top || this.parenthetical ? val : ("(" + val + ")");
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
2010-06-12 23:05:13 +00:00
|
|
|
AssignNode.prototype.compilePatternMatch = function(o) {
|
2010-10-02 11:53:29 +00:00
|
|
|
var _len, _ref2, accessClass, assigns, code, i, idx, isObject, obj, objects, olength, otop, splat, top, val, valVar, value;
|
2010-09-28 20:47:12 +00:00
|
|
|
if ((value = this.value).isStatement(o)) {
|
|
|
|
value = ClosureNode.wrap(value);
|
|
|
|
}
|
|
|
|
objects = this.variable.base.objects;
|
2010-10-02 11:53:29 +00:00
|
|
|
if (!(olength = objects.length)) {
|
2010-09-28 20:47:12 +00:00
|
|
|
return value.compile(o);
|
|
|
|
}
|
2010-10-02 11:53:29 +00:00
|
|
|
isObject = this.variable.isObject();
|
2010-10-02 12:49:21 +00:00
|
|
|
if (o.top && olength === 1 && !((obj = objects[0]) instanceof SplatNode)) {
|
2010-10-02 11:53:29 +00:00
|
|
|
if (obj instanceof AssignNode) {
|
2010-09-28 20:47:12 +00:00
|
|
|
_ref2 = obj, idx = _ref2.variable.base, obj = _ref2.value;
|
|
|
|
} else {
|
2010-10-02 11:53:29 +00:00
|
|
|
idx = isObject ? obj : literal(0);
|
2010-09-28 20:47:12 +00:00
|
|
|
}
|
|
|
|
if (!(value instanceof ValueNode)) {
|
|
|
|
value = new ValueNode(value);
|
|
|
|
}
|
|
|
|
accessClass = IDENTIFIER.test(idx.value) ? AccessorNode : IndexNode;
|
|
|
|
value.properties.push(new accessClass(idx));
|
|
|
|
return new AssignNode(obj, value).compile(o);
|
|
|
|
}
|
|
|
|
top = del(o, 'top');
|
|
|
|
otop = merge(o, {
|
|
|
|
top: true
|
|
|
|
});
|
2010-09-21 01:56:18 +00:00
|
|
|
valVar = o.scope.freeVariable('ref');
|
2010-10-04 12:50:50 +00:00
|
|
|
assigns = [("" + valVar + " = " + (value.compile(o)))];
|
2010-03-22 06:02:04 +00:00
|
|
|
splat = false;
|
2010-10-01 22:26:37 +00:00
|
|
|
for (i = 0, _len = objects.length; i < _len; i++) {
|
|
|
|
obj = objects[i];
|
2010-02-11 23:44:00 +00:00
|
|
|
idx = i;
|
2010-09-28 20:47:12 +00:00
|
|
|
if (isObject) {
|
2010-04-26 02:21:53 +00:00
|
|
|
if (obj instanceof AssignNode) {
|
2010-10-01 22:26:37 +00:00
|
|
|
_ref2 = [obj.value, obj.variable.base], obj = _ref2[0], idx = _ref2[1];
|
2010-04-26 02:21:53 +00:00
|
|
|
} else {
|
|
|
|
idx = obj;
|
|
|
|
}
|
2010-02-10 01:53:25 +00:00
|
|
|
}
|
2010-04-25 15:22:15 +00:00
|
|
|
if (!(obj instanceof ValueNode || obj instanceof SplatNode)) {
|
|
|
|
throw new Error('pattern matching must use only identifiers on the left-hand side.');
|
|
|
|
}
|
2010-09-28 20:47:12 +00:00
|
|
|
accessClass = isObject && IDENTIFIER.test(idx.value) ? AccessorNode : IndexNode;
|
|
|
|
if (!splat && obj instanceof SplatNode) {
|
2010-10-02 11:53:29 +00:00
|
|
|
val = literal(obj.compileValue(o, valVar, i, olength - i - 1));
|
2010-03-22 06:02:04 +00:00
|
|
|
splat = true;
|
2010-02-12 01:11:11 +00:00
|
|
|
} else {
|
2010-03-22 06:02:04 +00:00
|
|
|
if (typeof idx !== 'object') {
|
2010-10-04 12:50:50 +00:00
|
|
|
idx = literal(splat ? ("" + valVar + ".length - " + (olength - idx)) : idx);
|
2010-03-22 06:02:04 +00:00
|
|
|
}
|
2010-06-12 23:05:13 +00:00
|
|
|
val = new ValueNode(literal(valVar), [new accessClass(idx)]);
|
2010-02-12 01:11:11 +00:00
|
|
|
}
|
2010-09-28 20:47:12 +00:00
|
|
|
assigns.push(new AssignNode(obj, val).compile(otop));
|
2010-02-10 01:53:25 +00:00
|
|
|
}
|
2010-09-28 20:47:12 +00:00
|
|
|
if (!(top)) {
|
|
|
|
assigns.push(valVar);
|
|
|
|
}
|
|
|
|
code = assigns.join(', ');
|
2010-10-04 12:50:50 +00:00
|
|
|
return top || this.parenthetical ? code : ("(" + code + ")");
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
2010-06-12 23:05:13 +00:00
|
|
|
AssignNode.prototype.compileSplice = function(o) {
|
2010-10-01 22:17:35 +00:00
|
|
|
var from, name, plus, range, ref, to, val;
|
|
|
|
range = this.variable.properties.pop().range;
|
|
|
|
name = this.variable.compile(o);
|
2010-03-31 00:06:44 +00:00
|
|
|
plus = range.exclusive ? '' : ' + 1';
|
2010-08-19 01:51:44 +00:00
|
|
|
from = range.from ? range.from.compile(o) : '0';
|
2010-10-04 12:50:50 +00:00
|
|
|
to = range.to ? range.to.compile(o) + ' - ' + from + plus : ("" + name + ".length");
|
2010-10-01 22:17:35 +00:00
|
|
|
ref = o.scope.freeVariable('ref');
|
2010-03-31 00:06:44 +00:00
|
|
|
val = this.value.compile(o);
|
2010-10-04 12:50:50 +00:00
|
|
|
return "([].splice.apply(" + name + ", [" + from + ", " + to + "].concat(" + ref + " = " + val + ")), " + ref + ")";
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
|
|
|
return AssignNode;
|
2010-04-10 18:40:05 +00:00
|
|
|
})();
|
2010-02-28 00:19:53 +00:00
|
|
|
exports.CodeNode = (function() {
|
2010-09-19 12:29:15 +00:00
|
|
|
CodeNode = function(_arg, _arg2, tag) {
|
|
|
|
this.body = _arg2;
|
|
|
|
this.params = _arg;
|
2010-08-24 01:19:43 +00:00
|
|
|
CodeNode.__super__.constructor.call(this);
|
2010-08-14 22:02:07 +00:00
|
|
|
this.params || (this.params = []);
|
2010-10-01 22:17:35 +00:00
|
|
|
this.body || (this.body = new Expressions);
|
2010-02-10 02:31:07 +00:00
|
|
|
this.bound = tag === 'boundfunc';
|
2010-08-18 01:07:36 +00:00
|
|
|
if (this.bound) {
|
|
|
|
this.context = 'this';
|
|
|
|
}
|
2010-02-10 02:31:07 +00:00
|
|
|
return this;
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
2010-03-31 00:15:51 +00:00
|
|
|
__extends(CodeNode, BaseNode);
|
2010-07-31 00:29:05 +00:00
|
|
|
CodeNode.prototype["class"] = 'CodeNode';
|
2010-05-31 14:36:50 +00:00
|
|
|
CodeNode.prototype.children = ['params', 'body'];
|
2010-06-12 23:05:13 +00:00
|
|
|
CodeNode.prototype.compileNode = function(o) {
|
2010-09-21 02:13:08 +00:00
|
|
|
var _i, _len, _ref2, _ref3, _result, code, empty, func, i, param, params, sharedScope, splat, top, value;
|
2010-06-12 23:05:13 +00:00
|
|
|
sharedScope = del(o, 'sharedScope');
|
2010-03-14 21:58:32 +00:00
|
|
|
top = del(o, 'top');
|
2010-06-12 23:05:13 +00:00
|
|
|
o.scope = sharedScope || new Scope(o.scope, this.body, this);
|
2010-02-10 02:31:07 +00:00
|
|
|
o.top = true;
|
2010-07-25 07:15:12 +00:00
|
|
|
o.indent = this.idt(1);
|
2010-07-29 00:35:59 +00:00
|
|
|
empty = this.body.expressions.length === 0;
|
2010-06-12 23:05:13 +00:00
|
|
|
del(o, 'noWrap');
|
2010-03-14 21:58:32 +00:00
|
|
|
del(o, 'globals');
|
2010-03-17 18:41:09 +00:00
|
|
|
splat = undefined;
|
|
|
|
params = [];
|
2010-09-21 01:56:18 +00:00
|
|
|
_ref2 = this.params;
|
2010-09-21 02:13:08 +00:00
|
|
|
for (i = 0, _len = _ref2.length; i < _len; i++) {
|
2010-09-21 01:56:18 +00:00
|
|
|
param = _ref2[i];
|
2010-07-28 11:34:28 +00:00
|
|
|
if (splat) {
|
2010-07-28 05:54:36 +00:00
|
|
|
if (param.attach) {
|
2010-07-28 06:17:50 +00:00
|
|
|
param.assign = new AssignNode(new ValueNode(literal('this'), [new AccessorNode(param.value)]));
|
2010-07-28 05:54:36 +00:00
|
|
|
this.body.expressions.splice(splat.index + 1, 0, param.assign);
|
|
|
|
}
|
2010-03-17 18:41:09 +00:00
|
|
|
splat.trailings.push(param);
|
|
|
|
} else {
|
2010-07-28 05:54:36 +00:00
|
|
|
if (param.attach) {
|
2010-09-28 20:47:12 +00:00
|
|
|
value = param.value;
|
|
|
|
_ref3 = [literal(o.scope.freeVariable('arg')), param.splat], param = _ref3[0], param.splat = _ref3[1];
|
2010-07-28 06:17:50 +00:00
|
|
|
this.body.unshift(new AssignNode(new ValueNode(literal('this'), [new AccessorNode(value)]), param));
|
2010-07-28 05:54:36 +00:00
|
|
|
}
|
|
|
|
if (param.splat) {
|
2010-07-28 06:17:50 +00:00
|
|
|
splat = new SplatNode(param.value);
|
2010-07-28 05:54:36 +00:00
|
|
|
splat.index = i;
|
|
|
|
splat.trailings = [];
|
|
|
|
splat.arglength = this.params.length;
|
|
|
|
this.body.unshift(splat);
|
|
|
|
} else {
|
|
|
|
params.push(param);
|
|
|
|
}
|
2010-03-17 18:41:09 +00:00
|
|
|
}
|
2010-02-10 02:31:07 +00:00
|
|
|
}
|
2010-02-18 00:19:51 +00:00
|
|
|
params = (function() {
|
2010-10-01 22:26:37 +00:00
|
|
|
_result = [];
|
|
|
|
for (_i = 0, _len = params.length; _i < _len; _i++) {
|
|
|
|
param = params[_i];
|
2010-09-19 12:29:15 +00:00
|
|
|
_result.push(param.compile(o));
|
2010-02-12 18:35:34 +00:00
|
|
|
}
|
2010-09-19 12:29:15 +00:00
|
|
|
return _result;
|
2010-04-10 18:40:05 +00:00
|
|
|
})();
|
2010-07-29 00:35:59 +00:00
|
|
|
if (!(empty)) {
|
|
|
|
this.body.makeReturn();
|
|
|
|
}
|
2010-10-01 22:26:37 +00:00
|
|
|
for (_i = 0, _len = params.length; _i < _len; _i++) {
|
|
|
|
param = params[_i];
|
2010-02-10 02:31:07 +00:00
|
|
|
(o.scope.parameter(param));
|
|
|
|
}
|
2010-06-12 23:05:13 +00:00
|
|
|
code = this.body.expressions.length ? ("\n" + (this.body.compileWithDeclarations(o)) + "\n") : '';
|
2010-10-04 12:50:50 +00:00
|
|
|
func = ("function(" + (params.join(', ')) + ") {" + code + (code && this.tab) + "}");
|
2010-07-25 07:15:12 +00:00
|
|
|
if (this.bound) {
|
2010-10-04 12:50:50 +00:00
|
|
|
return ("(" + (utility('bind')) + "(" + func + ", " + this.context + "))");
|
2010-02-10 02:31:07 +00:00
|
|
|
}
|
2010-10-04 12:50:50 +00:00
|
|
|
return top ? ("(" + func + ")") : func;
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
2010-09-28 20:47:12 +00:00
|
|
|
CodeNode.prototype.topSensitive = YES;
|
2010-06-12 23:05:13 +00:00
|
|
|
CodeNode.prototype.traverseChildren = function(crossScope, func) {
|
2010-10-02 00:52:23 +00:00
|
|
|
return crossScope ? CodeNode.__super__.traverseChildren.call(this, crossScope, func) : undefined;
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
|
|
|
return CodeNode;
|
2010-04-10 18:40:05 +00:00
|
|
|
})();
|
2010-07-28 05:54:36 +00:00
|
|
|
exports.ParamNode = (function() {
|
2010-09-19 12:29:15 +00:00
|
|
|
ParamNode = function(_arg, _arg2, _arg3) {
|
|
|
|
this.splat = _arg3;
|
|
|
|
this.attach = _arg2;
|
|
|
|
this.name = _arg;
|
2010-08-24 01:19:43 +00:00
|
|
|
ParamNode.__super__.constructor.call(this);
|
2010-07-28 06:17:50 +00:00
|
|
|
this.value = literal(this.name);
|
2010-07-28 05:54:36 +00:00
|
|
|
return this;
|
|
|
|
};
|
|
|
|
__extends(ParamNode, BaseNode);
|
2010-07-31 00:29:05 +00:00
|
|
|
ParamNode.prototype["class"] = 'ParamNode';
|
2010-07-28 05:54:36 +00:00
|
|
|
ParamNode.prototype.children = ['name'];
|
|
|
|
ParamNode.prototype.compileNode = function(o) {
|
2010-07-28 06:17:50 +00:00
|
|
|
return this.value.compile(o);
|
2010-07-28 05:54:36 +00:00
|
|
|
};
|
2010-09-26 19:47:52 +00:00
|
|
|
ParamNode.prototype.toString = function() {
|
2010-09-28 20:47:12 +00:00
|
|
|
var name;
|
|
|
|
name = this.name;
|
2010-09-26 19:47:52 +00:00
|
|
|
if (this.attach) {
|
|
|
|
name = '@' + name;
|
|
|
|
}
|
|
|
|
if (this.splat) {
|
|
|
|
name += '...';
|
|
|
|
}
|
|
|
|
return literal(name).toString();
|
2010-07-28 05:54:36 +00:00
|
|
|
};
|
|
|
|
return ParamNode;
|
|
|
|
})();
|
2010-02-28 00:19:53 +00:00
|
|
|
exports.SplatNode = (function() {
|
2010-05-15 03:40:04 +00:00
|
|
|
SplatNode = function(name) {
|
2010-08-24 01:19:43 +00:00
|
|
|
SplatNode.__super__.constructor.call(this);
|
2010-02-12 04:57:31 +00:00
|
|
|
if (!(name.compile)) {
|
2010-02-28 05:22:06 +00:00
|
|
|
name = literal(name);
|
2010-02-12 04:57:31 +00:00
|
|
|
}
|
2010-05-10 10:58:01 +00:00
|
|
|
this.name = name;
|
2010-02-10 02:44:34 +00:00
|
|
|
return this;
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
2010-03-31 00:15:51 +00:00
|
|
|
__extends(SplatNode, BaseNode);
|
2010-07-31 00:29:05 +00:00
|
|
|
SplatNode.prototype["class"] = 'SplatNode';
|
2010-05-31 14:36:50 +00:00
|
|
|
SplatNode.prototype.children = ['name'];
|
2010-06-12 23:05:13 +00:00
|
|
|
SplatNode.prototype.compileNode = function(o) {
|
2010-10-01 22:17:35 +00:00
|
|
|
return (this.index != null) ? this.compileParam(o) : this.name.compile(o);
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
2010-06-12 23:05:13 +00:00
|
|
|
SplatNode.prototype.compileParam = function(o) {
|
2010-09-21 02:13:08 +00:00
|
|
|
var _len, _ref2, assign, end, idx, len, name, pos, trailing, variadic;
|
2010-02-12 01:11:11 +00:00
|
|
|
name = this.name.compile(o);
|
|
|
|
o.scope.find(name);
|
2010-07-25 07:15:12 +00:00
|
|
|
end = '';
|
|
|
|
if (this.trailings.length) {
|
2010-09-21 02:13:08 +00:00
|
|
|
len = o.scope.freeVariable('len');
|
2010-07-25 07:15:12 +00:00
|
|
|
o.scope.assign(len, "arguments.length");
|
2010-09-19 12:29:15 +00:00
|
|
|
variadic = o.scope.freeVariable('result');
|
2010-08-07 12:02:16 +00:00
|
|
|
o.scope.assign(variadic, len + ' >= ' + this.arglength);
|
2010-10-04 12:50:50 +00:00
|
|
|
end = this.trailings.length ? (", " + len + " - " + (this.trailings.length)) : undefined;
|
2010-09-21 01:56:18 +00:00
|
|
|
_ref2 = this.trailings;
|
2010-09-21 02:13:08 +00:00
|
|
|
for (idx = 0, _len = _ref2.length; idx < _len; idx++) {
|
2010-09-21 01:56:18 +00:00
|
|
|
trailing = _ref2[idx];
|
2010-07-28 05:54:36 +00:00
|
|
|
if (trailing.attach) {
|
|
|
|
assign = trailing.assign;
|
2010-09-19 12:29:15 +00:00
|
|
|
trailing = literal(o.scope.freeVariable('arg'));
|
2010-07-28 05:54:36 +00:00
|
|
|
assign.value = trailing;
|
|
|
|
}
|
2010-07-25 07:15:12 +00:00
|
|
|
pos = this.trailings.length - idx;
|
2010-10-04 12:50:50 +00:00
|
|
|
o.scope.assign(trailing.compile(o), "arguments[" + variadic + " ? " + len + " - " + pos + " : " + (this.index + idx) + "]");
|
2010-07-25 07:15:12 +00:00
|
|
|
}
|
|
|
|
}
|
2010-10-04 12:50:50 +00:00
|
|
|
return "" + name + " = " + (utility('slice')) + ".call(arguments, " + this.index + end + ")";
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
2010-06-12 23:05:13 +00:00
|
|
|
SplatNode.prototype.compileValue = function(o, name, index, trailings) {
|
2010-03-30 23:42:09 +00:00
|
|
|
var trail;
|
2010-10-04 12:50:50 +00:00
|
|
|
trail = trailings ? (", " + name + ".length - " + trailings) : '';
|
|
|
|
return "" + (utility('slice')) + ".call(" + name + ", " + index + trail + ")";
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
2010-07-14 02:16:19 +00:00
|
|
|
SplatNode.compileSplattedArray = function(list, o) {
|
2010-10-01 22:26:37 +00:00
|
|
|
var _len, arg, args, code, end, i, prev;
|
2010-03-30 01:49:20 +00:00
|
|
|
args = [];
|
2010-09-28 12:52:51 +00:00
|
|
|
end = -1;
|
2010-10-01 22:26:37 +00:00
|
|
|
for (i = 0, _len = list.length; i < _len; i++) {
|
|
|
|
arg = list[i];
|
2010-03-30 01:49:20 +00:00
|
|
|
code = arg.compile(o);
|
2010-09-28 12:52:51 +00:00
|
|
|
prev = args[end];
|
2010-03-30 01:49:20 +00:00
|
|
|
if (!(arg instanceof SplatNode)) {
|
2010-07-14 02:16:19 +00:00
|
|
|
if (prev && starts(prev, '[') && ends(prev, ']')) {
|
2010-10-04 12:50:50 +00:00
|
|
|
args[end] = ("" + (prev.slice(0, -1)) + ", " + code + "]");
|
2010-03-30 01:49:20 +00:00
|
|
|
continue;
|
2010-09-28 12:52:51 +00:00
|
|
|
}
|
|
|
|
if (prev && starts(prev, '.concat([') && ends(prev, '])')) {
|
2010-10-04 12:50:50 +00:00
|
|
|
args[end] = ("" + (prev.slice(0, -2)) + ", " + code + "])");
|
2010-03-30 01:49:20 +00:00
|
|
|
continue;
|
|
|
|
}
|
2010-10-04 12:50:50 +00:00
|
|
|
code = ("[" + code + "]");
|
2010-03-18 13:27:13 +00:00
|
|
|
}
|
2010-10-04 12:50:50 +00:00
|
|
|
args[++end] = i === 0 ? code : (".concat(" + code + ")");
|
2010-03-18 13:27:13 +00:00
|
|
|
}
|
2010-03-30 01:49:20 +00:00
|
|
|
return args.join('');
|
|
|
|
};
|
|
|
|
return SplatNode;
|
|
|
|
}).call(this);
|
2010-02-28 00:19:53 +00:00
|
|
|
exports.WhileNode = (function() {
|
2010-05-15 03:40:04 +00:00
|
|
|
WhileNode = function(condition, opts) {
|
2010-08-24 01:19:43 +00:00
|
|
|
WhileNode.__super__.constructor.call(this);
|
2010-10-02 00:52:23 +00:00
|
|
|
if (((opts != null) ? opts.invert : undefined)) {
|
2010-05-15 05:18:05 +00:00
|
|
|
if (condition instanceof OpNode) {
|
|
|
|
condition = new ParentheticalNode(condition);
|
|
|
|
}
|
2010-04-29 02:08:00 +00:00
|
|
|
condition = new OpNode('!', condition);
|
|
|
|
}
|
2010-05-10 10:58:01 +00:00
|
|
|
this.condition = condition;
|
2010-10-02 00:52:23 +00:00
|
|
|
this.guard = ((opts != null) ? opts.guard : undefined);
|
2010-02-24 03:53:43 +00:00
|
|
|
return this;
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
2010-03-31 00:15:51 +00:00
|
|
|
__extends(WhileNode, BaseNode);
|
2010-07-31 00:29:05 +00:00
|
|
|
WhileNode.prototype["class"] = 'WhileNode';
|
2010-05-31 14:36:50 +00:00
|
|
|
WhileNode.prototype.children = ['condition', 'guard', 'body'];
|
2010-09-27 08:56:56 +00:00
|
|
|
WhileNode.prototype.isStatement = YES;
|
2010-06-12 23:05:13 +00:00
|
|
|
WhileNode.prototype.addBody = function(body) {
|
2010-05-10 10:58:01 +00:00
|
|
|
this.body = body;
|
2010-02-10 03:05:17 +00:00
|
|
|
return this;
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
2010-06-12 23:05:13 +00:00
|
|
|
WhileNode.prototype.makeReturn = function() {
|
2010-03-21 15:28:05 +00:00
|
|
|
this.returns = true;
|
2010-03-21 11:17:58 +00:00
|
|
|
return this;
|
|
|
|
};
|
2010-09-28 20:47:12 +00:00
|
|
|
WhileNode.prototype.topSensitive = YES;
|
2010-06-12 23:05:13 +00:00
|
|
|
WhileNode.prototype.compileNode = function(o) {
|
2010-03-21 11:17:58 +00:00
|
|
|
var cond, post, pre, rvar, set, top;
|
2010-03-21 15:28:05 +00:00
|
|
|
top = del(o, 'top') && !this.returns;
|
2010-02-10 03:05:17 +00:00
|
|
|
o.indent = this.idt(1);
|
|
|
|
o.top = true;
|
2010-08-14 21:52:37 +00:00
|
|
|
this.condition.parenthetical = true;
|
2010-02-10 03:05:17 +00:00
|
|
|
cond = this.condition.compile(o);
|
|
|
|
set = '';
|
2010-04-27 23:38:24 +00:00
|
|
|
if (!(top)) {
|
2010-09-19 12:29:15 +00:00
|
|
|
rvar = o.scope.freeVariable('result');
|
2010-10-04 12:50:50 +00:00
|
|
|
set = ("" + this.tab + rvar + " = [];\n");
|
2010-02-24 03:53:43 +00:00
|
|
|
if (this.body) {
|
|
|
|
this.body = PushNode.wrap(rvar, this.body);
|
|
|
|
}
|
2010-02-10 03:05:17 +00:00
|
|
|
}
|
2010-10-04 12:50:50 +00:00
|
|
|
pre = ("" + set + this.tab + "while (" + cond + ")");
|
2010-04-29 01:46:47 +00:00
|
|
|
if (this.guard) {
|
|
|
|
this.body = Expressions.wrap([new IfNode(this.guard, this.body)]);
|
2010-02-24 03:53:43 +00:00
|
|
|
}
|
2010-08-11 04:40:15 +00:00
|
|
|
if (this.returns) {
|
2010-08-21 23:16:02 +00:00
|
|
|
post = '\n' + new ReturnNode(literal(rvar)).compile(merge(o, {
|
2010-08-11 04:40:15 +00:00
|
|
|
indent: this.idt()
|
|
|
|
}));
|
|
|
|
} else {
|
|
|
|
post = '';
|
2010-08-14 15:42:19 +00:00
|
|
|
}
|
2010-10-04 12:50:50 +00:00
|
|
|
return "" + pre + " {\n" + (this.body.compile(o)) + "\n" + this.tab + "}" + post;
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
|
|
|
return WhileNode;
|
2010-04-10 18:40:05 +00:00
|
|
|
})();
|
2010-02-28 00:19:53 +00:00
|
|
|
exports.OpNode = (function() {
|
2010-09-19 12:29:15 +00:00
|
|
|
OpNode = function(_arg, _arg2, _arg3, flip) {
|
|
|
|
this.second = _arg3;
|
|
|
|
this.first = _arg2;
|
|
|
|
this.operator = _arg;
|
2010-08-24 01:19:43 +00:00
|
|
|
OpNode.__super__.constructor.call(this);
|
2010-07-28 23:34:02 +00:00
|
|
|
this.operator = this.CONVERSIONS[this.operator] || this.operator;
|
2010-02-28 00:19:53 +00:00
|
|
|
this.flip = !!flip;
|
2010-08-11 04:40:15 +00:00
|
|
|
if (this.first instanceof ValueNode && this.first.base instanceof ObjectNode) {
|
|
|
|
this.first = new ParentheticalNode(this.first);
|
2010-09-25 08:39:19 +00:00
|
|
|
} else if (this.operator === 'new' && this.first instanceof CallNode) {
|
|
|
|
return this.first.newInstance();
|
2010-08-14 15:42:19 +00:00
|
|
|
}
|
2010-08-17 00:18:42 +00:00
|
|
|
this.first.tags.operation = true;
|
|
|
|
if (this.second) {
|
|
|
|
this.second.tags.operation = true;
|
|
|
|
}
|
2010-02-28 00:19:53 +00:00
|
|
|
return this;
|
|
|
|
};
|
2010-03-31 00:15:51 +00:00
|
|
|
__extends(OpNode, BaseNode);
|
2010-02-28 00:19:53 +00:00
|
|
|
OpNode.prototype.CONVERSIONS = {
|
2010-02-10 03:20:04 +00:00
|
|
|
'==': '===',
|
2010-03-22 01:46:53 +00:00
|
|
|
'!=': '!=='
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
2010-08-15 19:13:33 +00:00
|
|
|
OpNode.prototype.INVERSIONS = {
|
|
|
|
'!==': '===',
|
|
|
|
'===': '!=='
|
|
|
|
};
|
2010-02-28 00:19:53 +00:00
|
|
|
OpNode.prototype.CHAINABLE = ['<', '>', '>=', '<=', '===', '!=='];
|
|
|
|
OpNode.prototype.ASSIGNMENT = ['||=', '&&=', '?='];
|
2010-09-25 08:39:19 +00:00
|
|
|
OpNode.prototype.PREFIX_OPERATORS = ['new', 'typeof', 'delete'];
|
2010-07-31 00:29:05 +00:00
|
|
|
OpNode.prototype["class"] = 'OpNode';
|
2010-05-31 14:36:50 +00:00
|
|
|
OpNode.prototype.children = ['first', 'second'];
|
2010-06-12 23:05:13 +00:00
|
|
|
OpNode.prototype.isUnary = function() {
|
2010-02-10 03:20:04 +00:00
|
|
|
return !this.second;
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
2010-08-18 01:46:00 +00:00
|
|
|
OpNode.prototype.isInvertible = function() {
|
2010-09-21 01:56:18 +00:00
|
|
|
var _ref2;
|
|
|
|
return (('===' === (_ref2 = this.operator) || '!==' === _ref2)) && !(this.first instanceof OpNode) && !(this.second instanceof OpNode);
|
2010-08-15 19:13:33 +00:00
|
|
|
};
|
2010-09-27 08:56:56 +00:00
|
|
|
OpNode.prototype.isComplex = function() {
|
|
|
|
return this.operator !== '!' || this.first.isComplex();
|
|
|
|
};
|
2010-08-15 19:13:33 +00:00
|
|
|
OpNode.prototype.isMutator = function() {
|
2010-09-21 01:56:18 +00:00
|
|
|
var _ref2;
|
2010-09-29 20:29:20 +00:00
|
|
|
return ends(this.operator, '=') && !('===' === (_ref2 = this.operator) || '!==' === _ref2);
|
2010-08-14 18:43:25 +00:00
|
|
|
};
|
2010-06-12 23:05:13 +00:00
|
|
|
OpNode.prototype.isChainable = function() {
|
2010-08-14 18:43:25 +00:00
|
|
|
return include(this.CHAINABLE, this.operator);
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
2010-08-15 19:13:33 +00:00
|
|
|
OpNode.prototype.invert = function() {
|
|
|
|
return (this.operator = this.INVERSIONS[this.operator]);
|
|
|
|
};
|
2010-07-10 15:27:43 +00:00
|
|
|
OpNode.prototype.toString = function(idt) {
|
2010-08-24 01:19:43 +00:00
|
|
|
return OpNode.__super__.toString.call(this, idt, this["class"] + ' ' + this.operator);
|
2010-07-10 15:27:43 +00:00
|
|
|
};
|
2010-06-12 23:05:13 +00:00
|
|
|
OpNode.prototype.compileNode = function(o) {
|
2010-10-01 22:17:35 +00:00
|
|
|
var node;
|
|
|
|
if (node = ValueNode.unfoldSoak(o, this, 'first')) {
|
|
|
|
return node.compile(o);
|
|
|
|
}
|
2010-06-12 23:05:13 +00:00
|
|
|
if (this.isChainable() && this.first.unwrap() instanceof OpNode && this.first.unwrap().isChainable()) {
|
|
|
|
return this.compileChain(o);
|
2010-02-10 03:20:04 +00:00
|
|
|
}
|
2010-06-12 23:05:13 +00:00
|
|
|
if (indexOf(this.ASSIGNMENT, this.operator) >= 0) {
|
|
|
|
return this.compileAssignment(o);
|
2010-02-10 05:05:56 +00:00
|
|
|
}
|
2010-06-12 23:05:13 +00:00
|
|
|
if (this.isUnary()) {
|
|
|
|
return this.compileUnary(o);
|
2010-02-10 05:05:56 +00:00
|
|
|
}
|
|
|
|
if (this.operator === '?') {
|
2010-06-12 23:05:13 +00:00
|
|
|
return this.compileExistence(o);
|
2010-02-10 05:05:56 +00:00
|
|
|
}
|
2010-08-14 18:43:25 +00:00
|
|
|
if (this.first instanceof OpNode && this.first.isMutator()) {
|
|
|
|
this.first = new ParentheticalNode(this.first);
|
|
|
|
}
|
|
|
|
if (this.second instanceof OpNode && this.second.isMutator()) {
|
|
|
|
this.second = new ParentheticalNode(this.second);
|
|
|
|
}
|
2010-03-06 05:23:54 +00:00
|
|
|
return [this.first.compile(o), this.operator, this.second.compile(o)].join(' ');
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
2010-06-12 23:05:13 +00:00
|
|
|
OpNode.prototype.compileChain = function(o) {
|
2010-09-21 02:13:08 +00:00
|
|
|
var _ref2, first, second, shared;
|
2010-02-10 05:05:56 +00:00
|
|
|
shared = this.first.unwrap().second;
|
2010-09-28 20:47:12 +00:00
|
|
|
_ref2 = shared.compileReference(o), this.first.second = _ref2[0], shared = _ref2[1];
|
|
|
|
_ref2 = [this.first.compile(o), this.second.compile(o), shared.compile(o)], first = _ref2[0], second = _ref2[1], shared = _ref2[2];
|
2010-10-04 12:50:50 +00:00
|
|
|
return "(" + first + ") && (" + shared + " " + this.operator + " " + second + ")";
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
2010-06-12 23:05:13 +00:00
|
|
|
OpNode.prototype.compileAssignment = function(o) {
|
2010-10-01 22:47:28 +00:00
|
|
|
var _ref2, left, rite;
|
2010-10-01 22:17:35 +00:00
|
|
|
_ref2 = this.first.cacheReference(o), left = _ref2[0], rite = _ref2[1];
|
|
|
|
rite = new AssignNode(rite, this.second);
|
|
|
|
return new OpNode(this.operator.slice(0, -1), left, rite).compile(o);
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
2010-06-12 23:05:13 +00:00
|
|
|
OpNode.prototype.compileExistence = function(o) {
|
2010-10-01 22:17:35 +00:00
|
|
|
var fst, ref;
|
|
|
|
if (this.first.isComplex()) {
|
|
|
|
ref = o.scope.freeVariable('ref');
|
|
|
|
fst = new ParentheticalNode(new AssignNode(literal(ref), this.first));
|
|
|
|
} else {
|
|
|
|
fst = this.first;
|
|
|
|
ref = fst.compile(o);
|
|
|
|
}
|
2010-10-04 12:50:50 +00:00
|
|
|
return new ExistenceNode(fst).compile(o) + (" ? " + ref + " : " + (this.second.compile(o)));
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
2010-06-12 23:05:13 +00:00
|
|
|
OpNode.prototype.compileUnary = function(o) {
|
2010-02-10 05:05:56 +00:00
|
|
|
var parts, space;
|
2010-06-12 23:05:13 +00:00
|
|
|
space = indexOf(this.PREFIX_OPERATORS, this.operator) >= 0 ? ' ' : '';
|
2010-02-10 05:05:56 +00:00
|
|
|
parts = [this.operator, space, this.first.compile(o)];
|
|
|
|
if (this.flip) {
|
|
|
|
parts = parts.reverse();
|
|
|
|
}
|
|
|
|
return parts.join('');
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
|
|
|
return OpNode;
|
2010-04-10 18:40:05 +00:00
|
|
|
})();
|
2010-06-22 03:51:12 +00:00
|
|
|
exports.InNode = (function() {
|
2010-09-19 12:29:15 +00:00
|
|
|
InNode = function(_arg, _arg2) {
|
|
|
|
this.array = _arg2;
|
|
|
|
this.object = _arg;
|
2010-08-24 01:19:43 +00:00
|
|
|
InNode.__super__.constructor.call(this);
|
2010-06-22 03:51:12 +00:00
|
|
|
return this;
|
|
|
|
};
|
|
|
|
__extends(InNode, BaseNode);
|
2010-07-31 00:29:05 +00:00
|
|
|
InNode.prototype["class"] = 'InNode';
|
2010-06-22 03:51:12 +00:00
|
|
|
InNode.prototype.children = ['object', 'array'];
|
|
|
|
InNode.prototype.isArray = function() {
|
|
|
|
return this.array instanceof ValueNode && this.array.isArray();
|
|
|
|
};
|
|
|
|
InNode.prototype.compileNode = function(o) {
|
2010-09-21 01:56:18 +00:00
|
|
|
var _ref2;
|
|
|
|
_ref2 = this.object.compileReference(o, {
|
2010-06-22 03:51:12 +00:00
|
|
|
precompile: true
|
2010-09-28 20:47:12 +00:00
|
|
|
}), this.obj1 = _ref2[0], this.obj2 = _ref2[1];
|
2010-07-07 03:04:35 +00:00
|
|
|
return this.isArray() ? this.compileOrTest(o) : this.compileLoopTest(o);
|
2010-06-22 03:51:12 +00:00
|
|
|
};
|
|
|
|
InNode.prototype.compileOrTest = function(o) {
|
2010-09-21 02:13:08 +00:00
|
|
|
var _len, _ref2, _result, i, item, tests;
|
2010-06-22 03:51:12 +00:00
|
|
|
tests = (function() {
|
2010-09-21 01:56:18 +00:00
|
|
|
_result = []; _ref2 = this.array.base.objects;
|
2010-09-21 02:13:08 +00:00
|
|
|
for (i = 0, _len = _ref2.length; i < _len; i++) {
|
2010-09-21 01:56:18 +00:00
|
|
|
item = _ref2[i];
|
2010-09-19 12:29:15 +00:00
|
|
|
_result.push("" + (item.compile(o)) + " === " + (i ? this.obj2 : this.obj1));
|
2010-06-22 03:51:12 +00:00
|
|
|
}
|
2010-09-19 12:29:15 +00:00
|
|
|
return _result;
|
2010-06-22 03:51:12 +00:00
|
|
|
}).call(this);
|
|
|
|
return "(" + (tests.join(' || ')) + ")";
|
|
|
|
};
|
|
|
|
InNode.prototype.compileLoopTest = function(o) {
|
2010-09-21 02:13:08 +00:00
|
|
|
var _ref2, i, l, prefix;
|
2010-09-21 01:56:18 +00:00
|
|
|
_ref2 = this.array.compileReference(o, {
|
2010-06-22 03:51:12 +00:00
|
|
|
precompile: true
|
2010-09-28 20:47:12 +00:00
|
|
|
}), this.arr1 = _ref2[0], this.arr2 = _ref2[1];
|
|
|
|
_ref2 = [o.scope.freeVariable('i'), o.scope.freeVariable('len')], i = _ref2[0], l = _ref2[1];
|
2010-06-27 17:27:51 +00:00
|
|
|
prefix = this.obj1 !== this.obj2 ? this.obj1 + '; ' : '';
|
2010-10-04 12:50:50 +00:00
|
|
|
return "(function(){ " + prefix + "for (var " + i + "=0, " + l + "=" + this.arr1 + ".length; " + i + "<" + l + "; " + i + "++) { if (" + this.arr2 + "[" + i + "] === " + this.obj2 + ") return true; } return false; }).call(this)";
|
2010-06-22 03:51:12 +00:00
|
|
|
};
|
|
|
|
return InNode;
|
|
|
|
})();
|
2010-02-28 00:19:53 +00:00
|
|
|
exports.TryNode = (function() {
|
2010-09-19 12:29:15 +00:00
|
|
|
TryNode = function(_arg, _arg2, _arg3, _arg4) {
|
|
|
|
this.ensure = _arg4;
|
|
|
|
this.recovery = _arg3;
|
|
|
|
this.error = _arg2;
|
|
|
|
this.attempt = _arg;
|
2010-08-24 01:19:43 +00:00
|
|
|
TryNode.__super__.constructor.call(this);
|
2010-02-10 23:33:03 +00:00
|
|
|
return this;
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
2010-03-31 00:15:51 +00:00
|
|
|
__extends(TryNode, BaseNode);
|
2010-07-31 00:29:05 +00:00
|
|
|
TryNode.prototype["class"] = 'TryNode';
|
2010-05-31 14:36:50 +00:00
|
|
|
TryNode.prototype.children = ['attempt', 'recovery', 'ensure'];
|
2010-09-27 08:56:56 +00:00
|
|
|
TryNode.prototype.isStatement = YES;
|
2010-06-12 23:05:13 +00:00
|
|
|
TryNode.prototype.makeReturn = function() {
|
2010-03-21 11:17:58 +00:00
|
|
|
if (this.attempt) {
|
2010-06-12 23:05:13 +00:00
|
|
|
this.attempt = this.attempt.makeReturn();
|
2010-03-21 11:17:58 +00:00
|
|
|
}
|
|
|
|
if (this.recovery) {
|
2010-06-12 23:05:13 +00:00
|
|
|
this.recovery = this.recovery.makeReturn();
|
2010-03-21 11:17:58 +00:00
|
|
|
}
|
|
|
|
return this;
|
|
|
|
};
|
2010-06-12 23:05:13 +00:00
|
|
|
TryNode.prototype.compileNode = function(o) {
|
|
|
|
var attemptPart, catchPart, errorPart, finallyPart;
|
2010-02-10 23:33:03 +00:00
|
|
|
o.indent = this.idt(1);
|
|
|
|
o.top = true;
|
2010-06-12 23:05:13 +00:00
|
|
|
attemptPart = this.attempt.compile(o);
|
|
|
|
errorPart = this.error ? (" (" + (this.error.compile(o)) + ") ") : ' ';
|
2010-10-04 12:50:50 +00:00
|
|
|
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-28 00:19:53 +00:00
|
|
|
};
|
|
|
|
return TryNode;
|
2010-04-10 18:40:05 +00:00
|
|
|
})();
|
2010-02-28 00:19:53 +00:00
|
|
|
exports.ThrowNode = (function() {
|
2010-09-19 12:29:15 +00:00
|
|
|
ThrowNode = function(_arg) {
|
|
|
|
this.expression = _arg;
|
2010-08-24 01:19:43 +00:00
|
|
|
ThrowNode.__super__.constructor.call(this);
|
2010-02-11 00:13:11 +00:00
|
|
|
return this;
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
2010-03-31 00:15:51 +00:00
|
|
|
__extends(ThrowNode, BaseNode);
|
2010-07-31 00:29:05 +00:00
|
|
|
ThrowNode.prototype["class"] = 'ThrowNode';
|
2010-05-31 14:36:50 +00:00
|
|
|
ThrowNode.prototype.children = ['expression'];
|
2010-09-27 08:56:56 +00:00
|
|
|
ThrowNode.prototype.isStatement = YES;
|
2010-09-29 02:30:05 +00:00
|
|
|
ThrowNode.prototype.makeReturn = THIS;
|
2010-06-12 23:05:13 +00:00
|
|
|
ThrowNode.prototype.compileNode = function(o) {
|
2010-10-04 12:50:50 +00:00
|
|
|
return "" + this.tab + "throw " + (this.expression.compile(o)) + ";";
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
|
|
|
return ThrowNode;
|
2010-04-10 18:40:05 +00:00
|
|
|
})();
|
2010-02-28 00:19:53 +00:00
|
|
|
exports.ExistenceNode = (function() {
|
2010-09-19 12:29:15 +00:00
|
|
|
ExistenceNode = function(_arg) {
|
|
|
|
this.expression = _arg;
|
2010-08-24 01:19:43 +00:00
|
|
|
ExistenceNode.__super__.constructor.call(this);
|
2010-02-11 00:13:11 +00:00
|
|
|
return this;
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
2010-03-31 00:15:51 +00:00
|
|
|
__extends(ExistenceNode, BaseNode);
|
2010-07-31 00:29:05 +00:00
|
|
|
ExistenceNode.prototype["class"] = 'ExistenceNode';
|
2010-05-31 14:36:50 +00:00
|
|
|
ExistenceNode.prototype.children = ['expression'];
|
2010-06-12 23:05:13 +00:00
|
|
|
ExistenceNode.prototype.compileNode = function(o) {
|
2010-10-01 22:17:35 +00:00
|
|
|
var code;
|
|
|
|
code = this.expression.compile(o);
|
2010-10-04 12:50:50 +00:00
|
|
|
code = IDENTIFIER.test(code) && !o.scope.check(code) ? ("typeof " + code + " !== \"undefined\" && " + code + " !== null") : ("" + code + " != null");
|
|
|
|
return this.parenthetical ? code : ("(" + code + ")");
|
2010-03-30 01:49:20 +00:00
|
|
|
};
|
2010-02-28 00:19:53 +00:00
|
|
|
return ExistenceNode;
|
2010-10-01 22:17:35 +00:00
|
|
|
})();
|
2010-02-28 00:19:53 +00:00
|
|
|
exports.ParentheticalNode = (function() {
|
2010-09-19 12:29:15 +00:00
|
|
|
ParentheticalNode = function(_arg) {
|
|
|
|
this.expression = _arg;
|
2010-08-24 01:19:43 +00:00
|
|
|
ParentheticalNode.__super__.constructor.call(this);
|
2010-02-11 00:13:11 +00:00
|
|
|
return this;
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
2010-03-31 00:15:51 +00:00
|
|
|
__extends(ParentheticalNode, BaseNode);
|
2010-07-31 00:29:05 +00:00
|
|
|
ParentheticalNode.prototype["class"] = 'ParentheticalNode';
|
2010-05-31 14:36:50 +00:00
|
|
|
ParentheticalNode.prototype.children = ['expression'];
|
2010-08-14 15:42:19 +00:00
|
|
|
ParentheticalNode.prototype.isStatement = function(o) {
|
|
|
|
return this.expression.isStatement(o);
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
2010-09-27 08:56:56 +00:00
|
|
|
ParentheticalNode.prototype.isComplex = function() {
|
|
|
|
return this.expression.isComplex();
|
|
|
|
};
|
|
|
|
ParentheticalNode.prototype.topSensitive = YES;
|
2010-06-12 23:05:13 +00:00
|
|
|
ParentheticalNode.prototype.makeReturn = function() {
|
|
|
|
return this.expression.makeReturn();
|
2010-03-21 11:17:58 +00:00
|
|
|
};
|
2010-06-12 23:05:13 +00:00
|
|
|
ParentheticalNode.prototype.compileNode = function(o) {
|
2010-08-14 21:25:29 +00:00
|
|
|
var code, top;
|
2010-07-10 13:49:01 +00:00
|
|
|
top = del(o, 'top');
|
2010-08-14 21:25:29 +00:00
|
|
|
this.expression.parenthetical = true;
|
2010-02-18 00:19:51 +00:00
|
|
|
code = this.expression.compile(o);
|
2010-08-21 12:30:22 +00:00
|
|
|
if (top && this.expression.isPureStatement(o)) {
|
|
|
|
return code;
|
|
|
|
}
|
2010-08-14 21:25:29 +00:00
|
|
|
if (this.parenthetical || this.isStatement(o)) {
|
|
|
|
return top ? this.tab + code + ';' : code;
|
2010-02-11 00:13:11 +00:00
|
|
|
}
|
2010-10-04 12:50:50 +00:00
|
|
|
return "(" + code + ")";
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
|
|
|
return ParentheticalNode;
|
2010-04-10 18:40:05 +00:00
|
|
|
})();
|
2010-02-28 00:19:53 +00:00
|
|
|
exports.ForNode = (function() {
|
2010-09-19 12:29:15 +00:00
|
|
|
ForNode = function(_arg, source, _arg2, _arg3) {
|
2010-09-21 01:56:18 +00:00
|
|
|
var _ref2;
|
2010-09-19 12:29:15 +00:00
|
|
|
this.index = _arg3;
|
|
|
|
this.name = _arg2;
|
|
|
|
this.body = _arg;
|
2010-08-24 01:19:43 +00:00
|
|
|
ForNode.__super__.constructor.call(this);
|
2010-08-14 22:02:07 +00:00
|
|
|
this.index || (this.index = null);
|
2010-02-11 01:19:59 +00:00
|
|
|
this.source = source.source;
|
2010-04-29 01:46:47 +00:00
|
|
|
this.guard = source.guard;
|
2010-02-11 01:19:59 +00:00
|
|
|
this.step = source.step;
|
2010-07-16 01:18:35 +00:00
|
|
|
this.raw = !!source.raw;
|
2010-02-11 01:19:59 +00:00
|
|
|
this.object = !!source.object;
|
|
|
|
if (this.object) {
|
2010-09-28 20:47:12 +00:00
|
|
|
_ref2 = [this.index, this.name], this.name = _ref2[0], this.index = _ref2[1];
|
2010-02-11 01:19:59 +00:00
|
|
|
}
|
2010-04-26 18:35:35 +00:00
|
|
|
this.pattern = this.name instanceof ValueNode;
|
2010-04-26 18:54:31 +00:00
|
|
|
if (this.index instanceof ValueNode) {
|
2010-04-26 18:35:35 +00:00
|
|
|
throw new Error('index cannot be a pattern matching expression');
|
|
|
|
}
|
2010-03-21 15:28:05 +00:00
|
|
|
this.returns = false;
|
2010-02-11 01:19:59 +00:00
|
|
|
return this;
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
2010-03-31 00:15:51 +00:00
|
|
|
__extends(ForNode, BaseNode);
|
2010-07-31 00:29:05 +00:00
|
|
|
ForNode.prototype["class"] = 'ForNode';
|
2010-05-31 14:36:50 +00:00
|
|
|
ForNode.prototype.children = ['body', 'source', 'guard'];
|
2010-09-27 08:56:56 +00:00
|
|
|
ForNode.prototype.isStatement = YES;
|
|
|
|
ForNode.prototype.topSensitive = YES;
|
2010-06-12 23:05:13 +00:00
|
|
|
ForNode.prototype.makeReturn = function() {
|
2010-03-21 15:28:05 +00:00
|
|
|
this.returns = true;
|
2010-03-21 11:17:58 +00:00
|
|
|
return this;
|
|
|
|
};
|
2010-06-12 23:05:13 +00:00
|
|
|
ForNode.prototype.compileReturnValue = function(val, o) {
|
2010-03-21 15:28:05 +00:00
|
|
|
if (this.returns) {
|
2010-08-21 23:16:02 +00:00
|
|
|
return '\n' + new ReturnNode(literal(val)).compile(o);
|
2010-05-13 01:47:31 +00:00
|
|
|
}
|
|
|
|
if (val) {
|
|
|
|
return '\n' + val;
|
2010-03-21 11:17:58 +00:00
|
|
|
}
|
2010-05-13 01:47:31 +00:00
|
|
|
return '';
|
2010-03-21 11:17:58 +00:00
|
|
|
};
|
2010-06-12 23:05:13 +00:00
|
|
|
ForNode.prototype.compileNode = function(o) {
|
2010-10-02 11:19:37 +00:00
|
|
|
var body, codeInBody, forPart, guardPart, idt1, index, ivar, lvar, name, namePart, range, ref, returnResult, rvar, scope, source, sourcePart, stepPart, svar, topLevel, varPart, vars;
|
2010-06-12 23:05:13 +00:00
|
|
|
topLevel = del(o, 'top') && !this.returns;
|
2010-02-11 01:19:59 +00:00
|
|
|
range = this.source instanceof ValueNode && this.source.base instanceof RangeNode && !this.source.properties.length;
|
|
|
|
source = range ? this.source.base : this.source;
|
2010-06-14 01:21:30 +00:00
|
|
|
codeInBody = this.body.contains(function(n) {
|
|
|
|
return n instanceof CodeNode;
|
|
|
|
});
|
2010-02-11 01:19:59 +00:00
|
|
|
scope = o.scope;
|
2010-09-23 03:26:11 +00:00
|
|
|
name = this.name && this.name.compile(o);
|
2010-02-11 01:19:59 +00:00
|
|
|
index = this.index && this.index.compile(o);
|
2010-08-08 14:52:59 +00:00
|
|
|
if (name && !this.pattern && (range || !codeInBody)) {
|
2010-08-25 02:19:53 +00:00
|
|
|
scope.find(name, {
|
|
|
|
immediate: true
|
|
|
|
});
|
2010-03-11 03:32:00 +00:00
|
|
|
}
|
2010-06-14 01:43:04 +00:00
|
|
|
if (index) {
|
2010-08-25 02:19:53 +00:00
|
|
|
scope.find(index, {
|
|
|
|
immediate: true
|
|
|
|
});
|
2010-03-11 03:32:00 +00:00
|
|
|
}
|
2010-06-12 23:05:13 +00:00
|
|
|
if (!(topLevel)) {
|
2010-09-19 12:29:15 +00:00
|
|
|
rvar = scope.freeVariable('result');
|
2010-02-11 01:19:59 +00:00
|
|
|
}
|
2010-09-23 03:26:11 +00:00
|
|
|
ivar = range ? name : index;
|
|
|
|
if (!ivar || codeInBody) {
|
|
|
|
ivar = scope.freeVariable('i');
|
|
|
|
}
|
2010-06-12 23:05:13 +00:00
|
|
|
varPart = '';
|
2010-07-17 02:31:36 +00:00
|
|
|
guardPart = '';
|
2010-02-11 01:19:59 +00:00
|
|
|
body = Expressions.wrap([this.body]);
|
2010-10-02 11:19:37 +00:00
|
|
|
idt1 = this.idt(1);
|
2010-02-11 01:19:59 +00:00
|
|
|
if (range) {
|
2010-06-12 23:05:13 +00:00
|
|
|
sourcePart = source.compileVariables(o);
|
|
|
|
forPart = source.compile(merge(o, {
|
2010-02-11 01:19:59 +00:00
|
|
|
index: ivar,
|
|
|
|
step: this.step
|
2010-03-06 05:23:54 +00:00
|
|
|
}));
|
2010-02-11 01:19:59 +00:00
|
|
|
} else {
|
2010-10-01 22:26:37 +00:00
|
|
|
svar = this.source.compile(o);
|
|
|
|
if (IDENTIFIER.test(svar) && scope.check(svar, {
|
|
|
|
immediate: true
|
|
|
|
})) {
|
|
|
|
sourcePart = '';
|
|
|
|
} else {
|
|
|
|
ref = scope.freeVariable('ref');
|
2010-10-04 12:50:50 +00:00
|
|
|
sourcePart = ("" + ref + " = " + svar + ";");
|
2010-10-01 22:26:37 +00:00
|
|
|
svar = ref;
|
|
|
|
}
|
2010-10-04 12:50:50 +00:00
|
|
|
namePart = this.pattern ? new AssignNode(this.name, literal("" + svar + "[" + ivar + "]")).compile(merge(o, {
|
2010-10-02 11:19:37 +00:00
|
|
|
top: true
|
2010-10-04 12:50:50 +00:00
|
|
|
})) : (name ? ("" + name + " = " + svar + "[" + ivar + "]") : undefined);
|
2010-04-27 23:38:24 +00:00
|
|
|
if (!(this.object)) {
|
2010-09-21 02:13:08 +00:00
|
|
|
lvar = scope.freeVariable('len');
|
2010-10-04 12:50:50 +00:00
|
|
|
stepPart = this.step ? ("" + ivar + " += " + (this.step.compile(o))) : ("" + ivar + "++");
|
|
|
|
forPart = ("" + ivar + " = 0, " + lvar + " = " + svar + ".length; " + ivar + " < " + lvar + "; " + stepPart);
|
2010-02-26 04:39:14 +00:00
|
|
|
}
|
2010-02-11 01:19:59 +00:00
|
|
|
}
|
2010-10-04 12:50:50 +00:00
|
|
|
sourcePart = (rvar ? ("" + rvar + " = []; ") : '') + sourcePart;
|
|
|
|
sourcePart = sourcePart ? ("" + this.tab + sourcePart + "\n" + this.tab) : this.tab;
|
2010-06-12 23:05:13 +00:00
|
|
|
returnResult = this.compileReturnValue(rvar, o);
|
|
|
|
if (!(topLevel)) {
|
2010-02-11 01:19:59 +00:00
|
|
|
body = PushNode.wrap(rvar, body);
|
|
|
|
}
|
2010-08-11 04:40:15 +00:00
|
|
|
if (this.guard) {
|
|
|
|
body = Expressions.wrap([new IfNode(this.guard, body)]);
|
2010-08-14 15:42:19 +00:00
|
|
|
}
|
2010-06-14 01:21:30 +00:00
|
|
|
if (codeInBody) {
|
2010-08-08 14:52:59 +00:00
|
|
|
if (range) {
|
2010-10-04 12:50:50 +00:00
|
|
|
body.unshift(literal("var " + name + " = " + ivar));
|
2010-08-08 14:52:59 +00:00
|
|
|
}
|
2010-06-14 01:21:30 +00:00
|
|
|
if (namePart) {
|
2010-10-04 12:50:50 +00:00
|
|
|
body.unshift(literal("var " + namePart));
|
2010-06-14 01:21:30 +00:00
|
|
|
}
|
2010-06-14 01:43:04 +00:00
|
|
|
if (index) {
|
2010-10-04 12:50:50 +00:00
|
|
|
body.unshift(literal("var " + index + " = " + ivar));
|
2010-06-14 01:43:04 +00:00
|
|
|
}
|
2010-06-14 01:21:30 +00:00
|
|
|
body = ClosureNode.wrap(body, true);
|
|
|
|
} else {
|
2010-10-02 11:19:37 +00:00
|
|
|
if (namePart) {
|
2010-10-04 12:50:50 +00:00
|
|
|
varPart = ("" + idt1 + namePart + ";\n");
|
2010-10-02 11:19:37 +00:00
|
|
|
}
|
2010-06-14 01:21:30 +00:00
|
|
|
}
|
2010-07-17 02:31:36 +00:00
|
|
|
if (this.object) {
|
2010-10-04 12:50:50 +00:00
|
|
|
forPart = ("" + ivar + " in " + svar);
|
2010-07-17 02:31:36 +00:00
|
|
|
if (!(this.raw)) {
|
2010-10-04 12:50:50 +00:00
|
|
|
guardPart = ("\n" + idt1 + "if (!" + (utility('hasProp')) + ".call(" + svar + ", " + ivar + ")) continue;");
|
2010-07-17 02:31:36 +00:00
|
|
|
}
|
|
|
|
}
|
2010-03-14 21:58:32 +00:00
|
|
|
body = body.compile(merge(o, {
|
2010-10-02 11:19:37 +00:00
|
|
|
indent: idt1,
|
2010-02-11 01:19:59 +00:00
|
|
|
top: true
|
|
|
|
}));
|
2010-10-04 12:50:50 +00:00
|
|
|
vars = range ? name : ("" + name + ", " + ivar);
|
|
|
|
return "" + sourcePart + "for (" + forPart + ") {" + guardPart + "\n" + varPart + body + "\n" + this.tab + "}" + returnResult;
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
|
|
|
return ForNode;
|
2010-04-10 18:40:05 +00:00
|
|
|
})();
|
2010-09-16 03:46:01 +00:00
|
|
|
exports.SwitchNode = (function() {
|
2010-09-19 12:29:15 +00:00
|
|
|
SwitchNode = function(_arg, _arg2, _arg3) {
|
|
|
|
this.otherwise = _arg3;
|
|
|
|
this.cases = _arg2;
|
|
|
|
this.subject = _arg;
|
2010-09-16 03:46:01 +00:00
|
|
|
SwitchNode.__super__.constructor.call(this);
|
|
|
|
this.tags.subjectless = !this.subject;
|
|
|
|
this.subject || (this.subject = literal('true'));
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
__extends(SwitchNode, BaseNode);
|
|
|
|
SwitchNode.prototype["class"] = 'SwitchNode';
|
|
|
|
SwitchNode.prototype.children = ['subject', 'cases', 'otherwise'];
|
2010-09-27 08:56:56 +00:00
|
|
|
SwitchNode.prototype.isStatement = YES;
|
2010-09-16 03:46:01 +00:00
|
|
|
SwitchNode.prototype.makeReturn = function() {
|
2010-09-21 02:13:08 +00:00
|
|
|
var _i, _len, _ref2, pair;
|
2010-09-21 01:56:18 +00:00
|
|
|
_ref2 = this.cases;
|
2010-09-21 02:13:08 +00:00
|
|
|
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
|
2010-09-21 01:56:18 +00:00
|
|
|
pair = _ref2[_i];
|
2010-09-16 03:46:01 +00:00
|
|
|
pair[1].makeReturn();
|
|
|
|
}
|
|
|
|
if (this.otherwise) {
|
|
|
|
this.otherwise.makeReturn();
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
SwitchNode.prototype.compileNode = function(o) {
|
2010-09-21 04:55:08 +00:00
|
|
|
var _i, _j, _len, _len2, _ref2, _ref3, block, code, condition, conditions, exprs, idt, pair;
|
2010-09-22 03:58:05 +00:00
|
|
|
idt = (o.indent = this.idt(2));
|
2010-09-16 03:46:01 +00:00
|
|
|
o.top = true;
|
2010-10-04 12:50:50 +00:00
|
|
|
code = ("" + this.tab + "switch (" + (this.subject.compile(o)) + ") {");
|
2010-09-21 01:56:18 +00:00
|
|
|
_ref2 = this.cases;
|
2010-09-21 02:13:08 +00:00
|
|
|
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
|
2010-09-21 01:56:18 +00:00
|
|
|
pair = _ref2[_i];
|
2010-09-28 20:47:12 +00:00
|
|
|
_ref3 = pair, conditions = _ref3[0], block = _ref3[1];
|
2010-09-16 03:46:01 +00:00
|
|
|
exprs = block.expressions;
|
2010-09-21 02:13:08 +00:00
|
|
|
_ref3 = flatten([conditions]);
|
2010-09-21 04:55:08 +00:00
|
|
|
for (_j = 0, _len2 = _ref3.length; _j < _len2; _j++) {
|
|
|
|
condition = _ref3[_j];
|
2010-09-16 03:46:01 +00:00
|
|
|
if (this.tags.subjectless) {
|
|
|
|
condition = new OpNode('!!', new ParentheticalNode(condition));
|
|
|
|
}
|
2010-09-22 03:58:05 +00:00
|
|
|
code += ("\n" + (this.idt(1)) + "case " + (condition.compile(o)) + ":");
|
2010-09-16 03:46:01 +00:00
|
|
|
}
|
|
|
|
code += ("\n" + (block.compile(o)));
|
2010-09-28 12:52:51 +00:00
|
|
|
if (!(last(exprs) instanceof ReturnNode)) {
|
2010-10-04 12:50:50 +00:00
|
|
|
code += ("\n" + idt + "break;");
|
2010-09-16 03:46:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (this.otherwise) {
|
2010-09-22 03:58:05 +00:00
|
|
|
code += ("\n" + (this.idt(1)) + "default:\n" + (this.otherwise.compile(o)));
|
2010-09-16 03:46:01 +00:00
|
|
|
}
|
2010-10-04 12:50:50 +00:00
|
|
|
code += ("\n" + this.tab + "}");
|
2010-09-16 03:46:01 +00:00
|
|
|
return code;
|
|
|
|
};
|
|
|
|
return SwitchNode;
|
|
|
|
})();
|
2010-02-28 00:19:53 +00:00
|
|
|
exports.IfNode = (function() {
|
2010-09-19 12:29:15 +00:00
|
|
|
IfNode = function(_arg, _arg2, _arg3) {
|
|
|
|
this.tags = _arg3;
|
|
|
|
this.body = _arg2;
|
|
|
|
this.condition = _arg;
|
2010-08-14 22:02:07 +00:00
|
|
|
this.tags || (this.tags = {});
|
2010-02-11 02:40:10 +00:00
|
|
|
if (this.tags.invert) {
|
2010-08-18 01:46:00 +00:00
|
|
|
if (this.condition instanceof OpNode && this.condition.isInvertible()) {
|
2010-08-15 19:13:33 +00:00
|
|
|
this.condition.invert();
|
|
|
|
} else {
|
|
|
|
this.condition = new OpNode('!', new ParentheticalNode(this.condition));
|
|
|
|
}
|
2010-02-11 02:40:10 +00:00
|
|
|
}
|
2010-07-24 03:44:56 +00:00
|
|
|
this.elseBody = null;
|
2010-06-12 23:05:13 +00:00
|
|
|
this.isChain = false;
|
2010-02-11 02:40:10 +00:00
|
|
|
return this;
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
2010-03-31 00:15:51 +00:00
|
|
|
__extends(IfNode, BaseNode);
|
2010-07-31 00:29:05 +00:00
|
|
|
IfNode.prototype["class"] = 'IfNode';
|
2010-09-16 03:48:20 +00:00
|
|
|
IfNode.prototype.children = ['condition', 'body', 'elseBody', 'assigner'];
|
2010-09-27 08:56:56 +00:00
|
|
|
IfNode.prototype.topSensitive = YES;
|
2010-06-12 23:05:13 +00:00
|
|
|
IfNode.prototype.bodyNode = function() {
|
2010-10-01 22:17:35 +00:00
|
|
|
var _ref2;
|
2010-10-02 00:52:23 +00:00
|
|
|
return (((_ref2 = this.body) != null) ? _ref2.unwrap() : undefined);
|
2010-04-27 13:32:45 +00:00
|
|
|
};
|
2010-06-12 23:05:13 +00:00
|
|
|
IfNode.prototype.elseBodyNode = function() {
|
2010-10-01 22:17:35 +00:00
|
|
|
var _ref2;
|
2010-10-02 00:52:23 +00:00
|
|
|
return (((_ref2 = this.elseBody) != null) ? _ref2.unwrap() : undefined);
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
2010-06-12 23:05:13 +00:00
|
|
|
IfNode.prototype.addElse = function(elseBody, statement) {
|
|
|
|
if (this.isChain) {
|
|
|
|
this.elseBodyNode().addElse(elseBody, statement);
|
2010-02-23 00:22:09 +00:00
|
|
|
} else {
|
2010-06-12 23:05:13 +00:00
|
|
|
this.isChain = elseBody instanceof IfNode;
|
|
|
|
this.elseBody = this.ensureExpressions(elseBody);
|
2010-02-23 00:22:09 +00:00
|
|
|
}
|
2010-02-11 02:40:10 +00:00
|
|
|
return this;
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
2010-08-14 15:42:19 +00:00
|
|
|
IfNode.prototype.isStatement = function(o) {
|
2010-10-01 22:17:35 +00:00
|
|
|
return this.statement || (this.statement = !!((o && o.top) || this.bodyNode().isStatement(o) || (this.elseBody && this.elseBodyNode().isStatement(o))));
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
2010-06-12 23:05:13 +00:00
|
|
|
IfNode.prototype.compileCondition = function(o) {
|
2010-10-01 22:26:37 +00:00
|
|
|
var _i, _len, _result, cond, conditions;
|
2010-08-14 21:52:37 +00:00
|
|
|
conditions = flatten([this.condition]);
|
|
|
|
if (conditions.length === 1) {
|
|
|
|
conditions[0].parenthetical = true;
|
|
|
|
}
|
2010-02-18 00:19:51 +00:00
|
|
|
return (function() {
|
2010-10-01 22:26:37 +00:00
|
|
|
_result = [];
|
|
|
|
for (_i = 0, _len = conditions.length; _i < _len; _i++) {
|
|
|
|
cond = conditions[_i];
|
2010-09-19 12:29:15 +00:00
|
|
|
_result.push(cond.compile(o));
|
2010-02-11 02:40:10 +00:00
|
|
|
}
|
2010-09-19 12:29:15 +00:00
|
|
|
return _result;
|
2010-08-14 21:52:37 +00:00
|
|
|
})().join(' || ');
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
2010-06-12 23:05:13 +00:00
|
|
|
IfNode.prototype.compileNode = function(o) {
|
2010-08-14 15:42:19 +00:00
|
|
|
return this.isStatement(o) ? this.compileStatement(o) : this.compileTernary(o);
|
2010-07-07 03:04:35 +00:00
|
|
|
};
|
|
|
|
IfNode.prototype.makeReturn = function() {
|
2010-06-12 23:05:13 +00:00
|
|
|
if (this.isStatement()) {
|
2010-08-14 22:02:07 +00:00
|
|
|
this.body && (this.body = this.ensureExpressions(this.body.makeReturn()));
|
|
|
|
this.elseBody && (this.elseBody = this.ensureExpressions(this.elseBody.makeReturn()));
|
2010-07-07 03:04:35 +00:00
|
|
|
return this;
|
2010-03-21 11:17:58 +00:00
|
|
|
} else {
|
2010-07-07 03:04:35 +00:00
|
|
|
return new ReturnNode(this);
|
2010-03-21 11:17:58 +00:00
|
|
|
}
|
|
|
|
};
|
2010-06-12 23:05:13 +00:00
|
|
|
IfNode.prototype.ensureExpressions = function(node) {
|
2010-07-26 03:59:28 +00:00
|
|
|
return node instanceof Expressions ? node : new Expressions([node]);
|
2010-04-27 13:32:45 +00:00
|
|
|
};
|
2010-06-12 23:05:13 +00:00
|
|
|
IfNode.prototype.compileStatement = function(o) {
|
2010-08-11 04:40:15 +00:00
|
|
|
var body, child, comDent, condO, elsePart, ifDent, ifPart, top;
|
|
|
|
top = del(o, 'top');
|
2010-06-12 23:05:13 +00:00
|
|
|
child = del(o, 'chainChild');
|
|
|
|
condO = merge(o);
|
2010-02-11 02:40:10 +00:00
|
|
|
o.indent = this.idt(1);
|
|
|
|
o.top = true;
|
2010-08-14 15:42:19 +00:00
|
|
|
ifDent = child || (top && !this.isStatement(o)) ? '' : this.idt();
|
2010-06-12 23:05:13 +00:00
|
|
|
comDent = child ? this.idt() : '';
|
2010-04-27 13:32:45 +00:00
|
|
|
body = this.body.compile(o);
|
2010-10-04 12:50:50 +00:00
|
|
|
ifPart = ("" + ifDent + "if (" + (this.compileCondition(condO)) + ") {\n" + body + "\n" + this.tab + "}");
|
2010-06-12 23:05:13 +00:00
|
|
|
if (!(this.elseBody)) {
|
|
|
|
return ifPart;
|
2010-02-11 02:40:10 +00:00
|
|
|
}
|
2010-06-12 23:05:13 +00:00
|
|
|
elsePart = this.isChain ? ' else ' + this.elseBodyNode().compile(merge(o, {
|
2010-02-11 02:40:10 +00:00
|
|
|
indent: this.idt(),
|
2010-06-12 23:05:13 +00:00
|
|
|
chainChild: true
|
2010-10-04 12:50:50 +00:00
|
|
|
})) : (" else {\n" + (this.elseBody.compile(o)) + "\n" + this.tab + "}");
|
|
|
|
return "" + ifPart + elsePart;
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
2010-06-12 23:05:13 +00:00
|
|
|
IfNode.prototype.compileTernary = function(o) {
|
2010-08-17 00:18:42 +00:00
|
|
|
var code, elsePart, ifPart;
|
|
|
|
this.bodyNode().tags.operation = (this.condition.tags.operation = true);
|
|
|
|
if (this.elseBody) {
|
|
|
|
this.elseBodyNode().tags.operation = true;
|
|
|
|
}
|
2010-06-12 23:05:13 +00:00
|
|
|
ifPart = this.condition.compile(o) + ' ? ' + this.bodyNode().compile(o);
|
2010-10-02 00:52:23 +00:00
|
|
|
elsePart = this.elseBody ? this.elseBodyNode().compile(o) : 'undefined';
|
2010-10-04 12:50:50 +00:00
|
|
|
code = ("" + ifPart + " : " + elsePart);
|
|
|
|
return this.tags.operation ? ("(" + code + ")") : code;
|
2010-02-28 00:19:53 +00:00
|
|
|
};
|
|
|
|
return IfNode;
|
2010-04-10 18:40:05 +00:00
|
|
|
})();
|
2010-03-10 04:04:16 +00:00
|
|
|
PushNode = (exports.PushNode = {
|
2010-05-15 03:40:04 +00:00
|
|
|
wrap: function(array, expressions) {
|
2010-03-10 04:04:16 +00:00
|
|
|
var expr;
|
|
|
|
expr = expressions.unwrap();
|
2010-06-12 23:05:13 +00:00
|
|
|
if (expr.isPureStatement() || expr.containsPureStatement()) {
|
2010-03-10 04:04:16 +00:00
|
|
|
return expressions;
|
|
|
|
}
|
|
|
|
return Expressions.wrap([new CallNode(new ValueNode(literal(array), [new AccessorNode(literal('push'))]), [expr])]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
ClosureNode = (exports.ClosureNode = {
|
2010-05-15 03:40:04 +00:00
|
|
|
wrap: function(expressions, statement) {
|
2010-06-12 23:05:13 +00:00
|
|
|
var args, call, func, mentionsArgs, mentionsThis, meth;
|
|
|
|
if (expressions.containsPureStatement()) {
|
2010-03-16 05:27:31 +00:00
|
|
|
return expressions;
|
|
|
|
}
|
2010-04-10 18:40:05 +00:00
|
|
|
func = new ParentheticalNode(new CodeNode([], Expressions.wrap([expressions])));
|
|
|
|
args = [];
|
2010-06-12 23:05:13 +00:00
|
|
|
mentionsArgs = expressions.contains(function(n) {
|
2010-07-01 01:54:16 +00:00
|
|
|
return n instanceof LiteralNode && (n.value === 'arguments');
|
2010-04-10 18:20:32 +00:00
|
|
|
});
|
2010-06-12 23:05:13 +00:00
|
|
|
mentionsThis = expressions.contains(function(n) {
|
2010-07-01 01:54:16 +00:00
|
|
|
return (n instanceof LiteralNode && (n.value === 'this')) || (n instanceof CodeNode && n.bound);
|
2010-04-10 18:40:05 +00:00
|
|
|
});
|
2010-06-12 23:05:13 +00:00
|
|
|
if (mentionsArgs || mentionsThis) {
|
|
|
|
meth = literal(mentionsArgs ? 'apply' : 'call');
|
2010-04-10 18:40:05 +00:00
|
|
|
args = [literal('this')];
|
2010-06-12 23:05:13 +00:00
|
|
|
if (mentionsArgs) {
|
2010-04-10 18:40:05 +00:00
|
|
|
args.push(literal('arguments'));
|
|
|
|
}
|
|
|
|
func = new ValueNode(func, [new AccessorNode(meth)]);
|
2010-04-10 18:20:32 +00:00
|
|
|
}
|
2010-04-10 18:40:05 +00:00
|
|
|
call = new CallNode(func, args);
|
2010-07-07 03:04:35 +00:00
|
|
|
return statement ? Expressions.wrap([call]) : call;
|
2010-03-10 04:04:16 +00:00
|
|
|
}
|
|
|
|
});
|
2010-03-30 23:42:09 +00:00
|
|
|
UTILITIES = {
|
2010-08-24 01:19:43 +00:00
|
|
|
"extends": "function(child, parent) {\n var ctor = function(){};\n ctor.prototype = parent.prototype;\n child.prototype = new ctor();\n child.prototype.constructor = child;\n if (typeof parent.extended === \"function\") parent.extended(child);\n child.__super__ = parent.prototype;\n }",
|
2010-07-25 07:15:12 +00:00
|
|
|
bind: "function(func, context) {\n return function(){ return func.apply(context, arguments); };\n }",
|
2010-07-17 21:30:10 +00:00
|
|
|
hasProp: 'Object.prototype.hasOwnProperty',
|
|
|
|
slice: 'Array.prototype.slice'
|
2010-03-30 23:42:09 +00:00
|
|
|
};
|
2010-03-07 21:41:06 +00:00
|
|
|
TAB = ' ';
|
2010-05-13 01:47:31 +00:00
|
|
|
TRAILING_WHITESPACE = /[ \t]+$/gm;
|
2010-10-01 23:33:57 +00:00
|
|
|
IDENTIFIER = /^[$A-Za-z_][$\w]*$/;
|
2010-09-27 08:56:56 +00:00
|
|
|
NUMBER = /^0x[\da-f]+|^(?:\d+(\.\d+)?|\.\d+)(?:e[+-]?\d+)?$/i;
|
2010-09-02 01:20:23 +00:00
|
|
|
SIMPLENUM = /^-?\d+$/;
|
2010-04-25 15:07:09 +00:00
|
|
|
IS_STRING = /^['"]/;
|
2010-05-15 03:40:04 +00:00
|
|
|
literal = function(name) {
|
2010-03-07 21:41:06 +00:00
|
|
|
return new LiteralNode(name);
|
|
|
|
};
|
2010-05-15 03:40:04 +00:00
|
|
|
utility = function(name) {
|
2010-03-30 23:48:37 +00:00
|
|
|
var ref;
|
2010-10-04 12:50:50 +00:00
|
|
|
ref = ("__" + name);
|
2010-07-17 21:30:10 +00:00
|
|
|
Scope.root.assign(ref, UTILITIES[name]);
|
2010-03-30 23:48:37 +00:00
|
|
|
return ref;
|
|
|
|
};
|
2010-09-21 07:53:58 +00:00
|
|
|
}).call(this);
|