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