2010-07-25 03:15:12 -04:00
|
|
|
(function() {
|
2010-11-20 20:51:43 -05:00
|
|
|
var Access, Arr, Assign, Base, Call, Class, Closure, Code, Comment, Existence, Expressions, Extends, For, IDENTIFIER, IS_STRING, If, In, Index, LEVEL_ACCESS, LEVEL_COND, LEVEL_LIST, LEVEL_OP, LEVEL_PAREN, LEVEL_TOP, Literal, NEGATE, NO, Obj, Op, Param, Parens, Push, Range, Return, SIMPLENUM, Scope, Slice, Splat, Switch, TAB, THIS, TRAILING_WHITESPACE, Throw, Try, UTILITIES, Value, While, YES, compact, del, ends, extend, flatten, last, merge, multident, starts, unfoldSoak, utility, _ref;
|
2010-11-12 00:20:08 -05:00
|
|
|
var __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) {
|
2010-11-20 19:17:09 -05:00
|
|
|
for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; }
|
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-08-23 21:19:43 -04:00
|
|
|
child.__super__ = parent.prototype;
|
2010-11-09 00:26:31 -05:00
|
|
|
return child;
|
2010-11-13 18:08:02 -05:00
|
|
|
}, __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
|
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-11-08 23:48:38 -05:00
|
|
|
NEGATE = function() {
|
|
|
|
this.negated = !this.negated;
|
|
|
|
return this;
|
|
|
|
};
|
2010-11-11 21:48:08 -05:00
|
|
|
exports.Base = Base = function() {
|
|
|
|
function Base() {}
|
2010-10-22 21:44:03 -04:00
|
|
|
Base.prototype.compile = function(o, lvl) {
|
2010-10-23 05:06:04 -04:00
|
|
|
var node;
|
2010-11-08 23:07:51 -05:00
|
|
|
o = extend({}, o);
|
2010-11-13 21:08:42 -05:00
|
|
|
if (lvl) {
|
2010-10-22 21:44:03 -04:00
|
|
|
o.level = lvl;
|
|
|
|
}
|
2010-10-23 05:06:04 -04:00
|
|
|
node = this.unfoldSoak(o) || this;
|
|
|
|
node.tab = o.indent;
|
2010-11-08 23:07:51 -05:00
|
|
|
if (o.level === LEVEL_TOP || node.isPureStatement() || !node.isStatement(o)) {
|
|
|
|
return node.compileNode(o);
|
|
|
|
} else {
|
|
|
|
return node.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-12-13 21:24:32 -05:00
|
|
|
o.sharedScope = true;
|
2010-10-22 21:44:03 -04:00
|
|
|
return Closure.wrap(this).compileNode(o);
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-10-24 01:05:31 -04:00
|
|
|
Base.prototype.cache = function(o, level, reused) {
|
2010-10-22 21:44:03 -04:00
|
|
|
var ref, sub;
|
|
|
|
if (!this.isComplex()) {
|
2010-10-24 01:05:31 -04:00
|
|
|
ref = level ? this.compile(o, level) : this;
|
2010-10-22 21:44:03 -04:00
|
|
|
return [ref, ref];
|
|
|
|
} else {
|
2010-10-24 01:05:31 -04:00
|
|
|
ref = new Literal(reused || o.scope.freeVariable('ref'));
|
2010-10-22 21:44:03 -04:00
|
|
|
sub = new Assign(ref, this);
|
2010-11-08 23:07:51 -05:00
|
|
|
if (level) {
|
|
|
|
return [sub.compile(o, level), ref.value];
|
|
|
|
} else {
|
|
|
|
return [sub, ref];
|
|
|
|
}
|
2010-05-31 19:38:45 -04:00
|
|
|
}
|
2010-10-20 13:29:06 -04:00
|
|
|
};
|
2010-10-13 07:29:22 -04:00
|
|
|
Base.prototype.compileLoopReference = function(o, name) {
|
2010-11-20 20:51:43 -05:00
|
|
|
var src, tmp, _ref;
|
2010-10-23 11:10:28 -04:00
|
|
|
src = tmp = this.compile(o, LEVEL_LIST);
|
2010-11-20 20:51:43 -05:00
|
|
|
if (!((-Infinity < (_ref = +src) && _ref < Infinity) || IDENTIFIER.test(src) && o.scope.check(src, true))) {
|
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.makeReturn = function() {
|
|
|
|
return new Return(this);
|
2010-03-21 07:17:58 -04:00
|
|
|
};
|
2010-11-01 23:53:49 -04:00
|
|
|
Base.prototype.contains = function(pred) {
|
2010-05-10 06:58:01 -04:00
|
|
|
var contains;
|
|
|
|
contains = false;
|
2010-11-01 23:53:49 -04:00
|
|
|
this.traverseChildren(false, function(node) {
|
|
|
|
if (pred(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-11-01 23:53:49 -04:00
|
|
|
return this.isPureStatement() || this.contains(function(node) {
|
2010-12-15 22:59:28 -05:00
|
|
|
return node.isPureStatement() && !(node instanceof Comment);
|
2010-03-21 11:28:05 -04:00
|
|
|
});
|
|
|
|
};
|
2010-12-16 11:35:51 -05:00
|
|
|
Base.prototype.lastNonComment = function(list) {
|
|
|
|
var i;
|
|
|
|
i = list.length;
|
|
|
|
while (i--) {
|
|
|
|
if (!(list[i] instanceof Comment)) {
|
|
|
|
return list[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
2010-11-14 09:44:40 -05:00
|
|
|
Base.prototype.toString = function(idt, name) {
|
|
|
|
var tree;
|
2010-11-21 21:12:59 -05:00
|
|
|
if (idt == null) {
|
|
|
|
idt = '';
|
|
|
|
}
|
|
|
|
if (name == null) {
|
|
|
|
name = this.constructor.name;
|
|
|
|
}
|
2010-11-14 09:44:40 -05:00
|
|
|
tree = '\n' + idt + name;
|
|
|
|
if (this.soak) {
|
|
|
|
tree += '?';
|
|
|
|
}
|
|
|
|
this.eachChild(function(node) {
|
|
|
|
return tree += node.toString(idt + TAB);
|
|
|
|
});
|
|
|
|
return tree;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-10-06 22:44:32 -04:00
|
|
|
Base.prototype.eachChild = function(func) {
|
2010-11-28 12:28:46 -05:00
|
|
|
var attr, child, _i, _j, _len, _len2, _ref, _ref2;
|
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-11-02 00:31:42 -04:00
|
|
|
_ref = this.children;
|
|
|
|
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
|
|
|
attr = _ref[_i];
|
2010-05-10 22:41:18 -04:00
|
|
|
if (this[attr]) {
|
2010-11-02 00:31:42 -04:00
|
|
|
_ref2 = flatten([this[attr]]);
|
2010-11-28 12:28:46 -05:00
|
|
|
for (_j = 0, _len2 = _ref2.length; _j < _len2; _j++) {
|
|
|
|
child = _ref2[_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-11-01 23:53:49 -04:00
|
|
|
Base.prototype.traverseChildren = function(crossScope, func) {
|
2010-06-12 19:05:13 -04:00
|
|
|
return this.eachChild(function(child) {
|
2010-11-01 23:53:49 -04:00
|
|
|
if (func(child) === false) {
|
2010-10-01 18:17:35 -04:00
|
|
|
return false;
|
|
|
|
}
|
2010-11-01 23:53:49 -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-24 19:00:12 -04:00
|
|
|
Base.prototype.unwrapAll = function() {
|
|
|
|
var node;
|
|
|
|
node = this;
|
|
|
|
while (node !== (node = node.unwrap())) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return node;
|
|
|
|
};
|
2010-10-06 22:44:32 -04:00
|
|
|
Base.prototype.children = [];
|
|
|
|
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-24 18:33:41 -04:00
|
|
|
Base.prototype.isAssignable = NO;
|
|
|
|
Base.prototype.unwrap = THIS;
|
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-11-11 21:48:08 -05:00
|
|
|
}();
|
|
|
|
exports.Expressions = Expressions = function() {
|
2010-12-06 01:01:57 -05:00
|
|
|
__extends(Expressions, Base);
|
2010-11-11 21:48:08 -05:00
|
|
|
function Expressions(nodes) {
|
|
|
|
this.expressions = compact(flatten(nodes || []));
|
|
|
|
}
|
2010-05-31 10:36:50 -04:00
|
|
|
Expressions.prototype.children = ['expressions'];
|
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-10-24 01:36:23 -04:00
|
|
|
Expressions.prototype.pop = function() {
|
|
|
|
return this.expressions.pop();
|
|
|
|
};
|
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-11-08 23:07:51 -05:00
|
|
|
if (this.expressions.length === 1) {
|
|
|
|
return this.expressions[0];
|
|
|
|
} else {
|
|
|
|
return this;
|
|
|
|
}
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-10-24 12:54:36 -04:00
|
|
|
Expressions.prototype.isEmpty = function() {
|
|
|
|
return !this.expressions.length;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-11-08 23:07:51 -05:00
|
|
|
Expressions.prototype.isStatement = function(o) {
|
|
|
|
var exp, _i, _len, _ref;
|
|
|
|
_ref = this.expressions;
|
|
|
|
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
|
|
|
exp = _ref[_i];
|
|
|
|
if (exp.isPureStatement() || exp.isStatement(o)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
Expressions.prototype.makeReturn = function() {
|
2010-11-20 16:25:22 -05:00
|
|
|
var expr, len;
|
|
|
|
len = this.expressions.length;
|
|
|
|
while (len--) {
|
|
|
|
expr = this.expressions[len];
|
|
|
|
if (!(expr instanceof Comment)) {
|
|
|
|
this.expressions[len] = expr.makeReturn();
|
2010-10-22 13:59:25 -04:00
|
|
|
break;
|
|
|
|
}
|
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-10-24 01:05:31 -04:00
|
|
|
Expressions.prototype.compile = function(o, level) {
|
2010-11-21 21:12:59 -05:00
|
|
|
if (o == null) {
|
|
|
|
o = {};
|
|
|
|
}
|
2010-11-08 23:07:51 -05:00
|
|
|
if (o.scope) {
|
|
|
|
return Expressions.__super__.compile.call(this, o, level);
|
|
|
|
} else {
|
|
|
|
return 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-11-08 23:07:51 -05:00
|
|
|
var code, codes, node, top, _i, _len, _ref;
|
2010-10-23 05:06:04 -04:00
|
|
|
this.tab = o.indent;
|
2010-11-08 23:07:51 -05:00
|
|
|
top = o.level === LEVEL_TOP;
|
|
|
|
codes = [];
|
|
|
|
_ref = this.expressions;
|
|
|
|
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
|
|
|
node = _ref[_i];
|
|
|
|
node = node.unwrapAll();
|
|
|
|
node = node.unfoldSoak(o) || node;
|
|
|
|
if (top) {
|
|
|
|
node.front = true;
|
|
|
|
code = node.compile(o);
|
|
|
|
codes.push(node.isStatement(o) ? code : this.tab + code + ';');
|
|
|
|
} else {
|
|
|
|
codes.push(node.compile(o, LEVEL_LIST));
|
2010-02-08 20:20:11 -05:00
|
|
|
}
|
2010-11-08 23:07:51 -05:00
|
|
|
}
|
|
|
|
if (top) {
|
|
|
|
return codes.join('\n');
|
|
|
|
}
|
|
|
|
code = codes.join(', ') || 'void 0';
|
|
|
|
if (codes.length > 1 && o.level >= LEVEL_LIST) {
|
|
|
|
return "(" + code + ")";
|
|
|
|
} else {
|
|
|
|
return code;
|
|
|
|
}
|
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-10-23 11:10:28 -04:00
|
|
|
o.level = LEVEL_TOP;
|
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-11-08 23:07:51 -05:00
|
|
|
if (o.bare) {
|
|
|
|
return code;
|
|
|
|
} else {
|
|
|
|
return "(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-11-16 00:11:52 -05:00
|
|
|
var code, exp, i, post, rest, scope, _len, _ref;
|
|
|
|
code = post = '';
|
|
|
|
_ref = this.expressions;
|
|
|
|
for (i = 0, _len = _ref.length; i < _len; i++) {
|
|
|
|
exp = _ref[i];
|
|
|
|
exp = exp.unwrap();
|
|
|
|
if (!(exp instanceof Comment || exp instanceof Literal)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-11-08 23:07:51 -05:00
|
|
|
o.level = LEVEL_TOP;
|
2010-11-16 00:11:52 -05:00
|
|
|
if (i) {
|
|
|
|
rest = this.expressions.splice(i, this.expressions.length);
|
|
|
|
code = this.compileNode(o);
|
|
|
|
this.expressions = rest;
|
2010-02-08 20:20:11 -05:00
|
|
|
}
|
2010-11-16 00:11:52 -05:00
|
|
|
post = this.compileNode(o);
|
|
|
|
scope = o.scope;
|
2010-12-06 23:32:32 -05:00
|
|
|
if (scope.expressions === this) {
|
2010-12-12 12:16:27 -05:00
|
|
|
if (!o.globals && o.scope.hasDeclarations()) {
|
2010-12-06 23:32:32 -05:00
|
|
|
code += "" + this.tab + "var " + (scope.declaredVariables().join(', ')) + ";\n";
|
|
|
|
}
|
|
|
|
if (scope.hasAssignments) {
|
|
|
|
code += "" + this.tab + "var " + (multident(scope.assignedVariables().join(', '), this.tab)) + ";\n";
|
|
|
|
}
|
2010-02-08 20:20:11 -05:00
|
|
|
}
|
2010-11-16 00:11:52 -05:00
|
|
|
return code + post;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-10-22 12:44:20 -04:00
|
|
|
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-11-11 21:48:08 -05:00
|
|
|
}();
|
|
|
|
exports.Literal = Literal = function() {
|
2010-12-06 01:01:57 -05:00
|
|
|
__extends(Literal, Base);
|
2010-11-20 20:39:35 -05:00
|
|
|
function Literal(value) {
|
|
|
|
this.value = value;
|
2010-11-11 21:48:08 -05:00
|
|
|
}
|
2010-10-06 22:44:32 -04:00
|
|
|
Literal.prototype.makeReturn = function() {
|
2010-11-08 23:07:51 -05:00
|
|
|
if (this.isPureStatement()) {
|
|
|
|
return this;
|
|
|
|
} else {
|
|
|
|
return new Return(this);
|
|
|
|
}
|
2010-07-29 21:33:35 -04:00
|
|
|
};
|
2010-10-23 05:06:04 -04:00
|
|
|
Literal.prototype.isPureStatement = function() {
|
2010-11-02 00:31:42 -04:00
|
|
|
var _ref;
|
|
|
|
return (_ref = this.value) === 'break' || _ref === 'continue' || _ref === 'debugger';
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-10-24 18:33:41 -04:00
|
|
|
Literal.prototype.isAssignable = function() {
|
|
|
|
return IDENTIFIER.test(this.value);
|
|
|
|
};
|
2010-10-06 22:44:32 -04:00
|
|
|
Literal.prototype.isComplex = NO;
|
2010-10-10 20:17:31 -04:00
|
|
|
Literal.prototype.assigns = function(name) {
|
|
|
|
return name === this.value;
|
|
|
|
};
|
2010-10-23 05:06:04 -04:00
|
|
|
Literal.prototype.compile = function() {
|
2010-11-08 23:07:51 -05:00
|
|
|
if (this.value.reserved) {
|
|
|
|
return "\"" + this.value + "\"";
|
|
|
|
} else {
|
|
|
|
return this.value;
|
|
|
|
}
|
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-11-11 21:48:08 -05:00
|
|
|
}();
|
|
|
|
exports.Return = Return = function() {
|
2010-12-06 01:01:57 -05:00
|
|
|
__extends(Return, Base);
|
2010-11-20 20:39:35 -05:00
|
|
|
function Return(expression) {
|
|
|
|
this.expression = expression;
|
2010-11-11 21:48:08 -05:00
|
|
|
}
|
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;
|
2010-10-24 01:05:31 -04:00
|
|
|
Return.prototype.compile = function(o, level) {
|
2010-11-02 00:31:42 -04:00
|
|
|
var expr, _ref;
|
|
|
|
expr = (_ref = this.expression) != null ? _ref.makeReturn() : void 0;
|
2010-11-08 23:07:51 -05:00
|
|
|
if (expr && !(expr instanceof Return)) {
|
|
|
|
return expr.compile(o, level);
|
|
|
|
} else {
|
|
|
|
return Return.__super__.compile.call(this, o, level);
|
|
|
|
}
|
2010-07-10 09:49:01 -04:00
|
|
|
};
|
2010-10-06 22:44:32 -04:00
|
|
|
Return.prototype.compileNode = function(o) {
|
2010-10-23 11:10:28 -04:00
|
|
|
o.level = LEVEL_PAREN;
|
2010-10-23 05:06:04 -04:00
|
|
|
return this.tab + ("return" + (this.expression ? ' ' + this.expression.compile(o) : '') + ";");
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-10-06 22:44:32 -04:00
|
|
|
return Return;
|
2010-11-11 21:48:08 -05:00
|
|
|
}();
|
|
|
|
exports.Value = Value = function() {
|
2010-12-06 01:01:57 -05:00
|
|
|
__extends(Value, Base);
|
2010-11-11 21:48:08 -05:00
|
|
|
function Value(base, props, tag) {
|
|
|
|
if (!props && base instanceof Value) {
|
|
|
|
return base;
|
|
|
|
}
|
|
|
|
this.base = base;
|
|
|
|
this.properties = props || [];
|
|
|
|
if (tag) {
|
|
|
|
this[tag] = true;
|
2010-10-24 20:51:55 -04:00
|
|
|
}
|
2010-11-20 17:46:44 -05:00
|
|
|
return this;
|
2010-11-11 21:48:08 -05:00
|
|
|
}
|
2010-10-06 22:44:32 -04:00
|
|
|
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() {
|
2010-10-24 19:55:42 -04:00
|
|
|
return !this.properties.length && this.base instanceof Arr;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-10-06 22:44:32 -04:00
|
|
|
Value.prototype.isComplex = function() {
|
2010-10-24 19:55:42 -04:00
|
|
|
return this.hasProperties() || this.base.isComplex();
|
|
|
|
};
|
|
|
|
Value.prototype.isAssignable = function() {
|
|
|
|
return this.hasProperties() || this.base.isAssignable();
|
|
|
|
};
|
|
|
|
Value.prototype.isSimpleNumber = function() {
|
|
|
|
return this.base instanceof Literal && SIMPLENUM.test(this.base.value);
|
2010-09-27 04:56:56 -04:00
|
|
|
};
|
2010-10-21 19:50:36 -04:00
|
|
|
Value.prototype.isAtomic = function() {
|
2010-11-02 00:31:42 -04:00
|
|
|
var node, _i, _len, _ref;
|
|
|
|
_ref = this.properties.concat(this.base);
|
|
|
|
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
|
|
|
node = _ref[_i];
|
2010-10-25 09:31:52 -04:00
|
|
|
if (node.soak || node instanceof Call) {
|
2010-10-21 19:50:36 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
2010-10-24 19:55:42 -04:00
|
|
|
Value.prototype.isStatement = function(o) {
|
|
|
|
return !this.properties.length && this.base.isStatement(o);
|
2010-10-24 18:33:41 -04:00
|
|
|
};
|
2010-10-10 20:17:31 -04:00
|
|
|
Value.prototype.assigns = function(name) {
|
|
|
|
return !this.properties.length && this.base.assigns(name);
|
|
|
|
};
|
2010-11-13 12:17:09 -05:00
|
|
|
Value.prototype.isObject = function(onlyGenerated) {
|
|
|
|
if (this.properties.length) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return (this.base instanceof Obj) && (!onlyGenerated || this.base.generated);
|
|
|
|
};
|
2010-11-20 16:25:22 -05:00
|
|
|
Value.prototype.isSplice = function() {
|
|
|
|
return last(this.properties) instanceof Slice;
|
|
|
|
};
|
2010-10-06 22:44:32 -04:00
|
|
|
Value.prototype.makeReturn = function() {
|
2010-11-08 23:07:51 -05:00
|
|
|
if (this.properties.length) {
|
|
|
|
return Value.__super__.makeReturn.call(this);
|
|
|
|
} else {
|
|
|
|
return this.base.makeReturn();
|
|
|
|
}
|
2010-03-21 07:17:58 -04:00
|
|
|
};
|
2010-10-06 22:44:32 -04:00
|
|
|
Value.prototype.unwrap = function() {
|
2010-11-08 23:07:51 -05:00
|
|
|
if (this.properties.length) {
|
|
|
|
return this;
|
|
|
|
} else {
|
|
|
|
return this.base;
|
|
|
|
}
|
2010-02-27 19:19:53 -05: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-27 22:50:20 -04:00
|
|
|
if (this.properties.length < 2 && !this.base.isComplex() && !(name != null ? name.isComplex() : void 0)) {
|
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.compileNode = function(o) {
|
2010-11-01 23:58:03 -04:00
|
|
|
var code, prop, props, _i, _len;
|
2010-11-01 23:25:28 -04:00
|
|
|
this.base.front = this.front;
|
2010-10-01 18:17:35 -04:00
|
|
|
props = this.properties;
|
2010-10-23 11:10:28 -04:00
|
|
|
code = this.base.compile(o, props.length ? LEVEL_ACCESS : null);
|
2010-11-20 20:09:36 -05:00
|
|
|
if (props[0] instanceof Access && 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-23 05:06:04 -04:00
|
|
|
code += prop.compile(o);
|
2010-10-01 18:17:35 -04:00
|
|
|
}
|
|
|
|
return code;
|
|
|
|
};
|
2010-10-06 22:44:32 -04:00
|
|
|
Value.prototype.unfoldSoak = function(o) {
|
2010-11-02 00:31:42 -04:00
|
|
|
var fst, i, ifn, prop, ref, snd, _len, _ref;
|
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-11-02 00:31:42 -04:00
|
|
|
_ref = this.properties;
|
|
|
|
for (i = 0, _len = _ref.length; i < _len; i++) {
|
|
|
|
prop = _ref[i];
|
2010-10-25 09:31:52 -04:00
|
|
|
if (prop.soak) {
|
|
|
|
prop.soak = 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, {
|
2010-10-22 21:44:03 -04:00
|
|
|
soak: true
|
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-06 22:44:32 -04:00
|
|
|
return Value;
|
2010-11-11 21:48:08 -05:00
|
|
|
}();
|
|
|
|
exports.Comment = Comment = function() {
|
2010-12-06 01:01:57 -05:00
|
|
|
__extends(Comment, Base);
|
2010-11-20 20:39:35 -05:00
|
|
|
function Comment(comment) {
|
|
|
|
this.comment = comment;
|
2010-11-11 21:48:08 -05:00
|
|
|
}
|
2010-10-23 05:06:04 -04:00
|
|
|
Comment.prototype.isPureStatement = YES;
|
2010-10-24 21:53:48 -04:00
|
|
|
Comment.prototype.isStatement = YES;
|
2010-10-06 22:44:32 -04:00
|
|
|
Comment.prototype.makeReturn = THIS;
|
2010-11-08 23:39:21 -05:00
|
|
|
Comment.prototype.compileNode = function(o, level) {
|
|
|
|
var code;
|
|
|
|
code = '/*' + multident(this.comment, this.tab) + '*/';
|
2010-11-13 21:08:42 -05:00
|
|
|
if ((level || o.level) === LEVEL_TOP) {
|
2010-11-08 23:39:21 -05:00
|
|
|
code = o.indent + code;
|
|
|
|
}
|
|
|
|
return code;
|
2010-07-01 21:26:33 -04:00
|
|
|
};
|
2010-10-06 22:44:32 -04:00
|
|
|
return Comment;
|
2010-11-11 21:48:08 -05:00
|
|
|
}();
|
|
|
|
exports.Call = Call = function() {
|
2010-12-06 01:01:57 -05:00
|
|
|
__extends(Call, Base);
|
2010-11-20 20:39:35 -05:00
|
|
|
function Call(variable, args, soak) {
|
|
|
|
this.args = args != null ? args : [];
|
|
|
|
this.soak = soak;
|
2010-11-11 21:48:08 -05:00
|
|
|
this.isNew = false;
|
|
|
|
this.isSuper = variable === 'super';
|
|
|
|
this.variable = this.isSuper ? null : variable;
|
|
|
|
}
|
2010-10-06 22:44:32 -04:00
|
|
|
Call.prototype.children = ['variable', 'args'];
|
|
|
|
Call.prototype.newInstance = function() {
|
2010-12-05 20:44:32 -05:00
|
|
|
var base;
|
|
|
|
base = this.variable.base || this.variable;
|
|
|
|
if (base instanceof Call) {
|
|
|
|
base.newInstance();
|
2010-12-05 15:08:41 -05:00
|
|
|
} else {
|
|
|
|
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.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-11-08 23:07:51 -05:00
|
|
|
if (method.klass) {
|
|
|
|
return "" + method.klass + ".__super__." + name;
|
|
|
|
} else {
|
|
|
|
return "" + 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-11-02 00:31:42 -04:00
|
|
|
var call, ifn, left, list, rite, _i, _len, _ref, _ref2;
|
2010-10-25 09:31:52 -04:00
|
|
|
if (this.soak) {
|
2010-10-20 15:51:11 -04:00
|
|
|
if (this.variable) {
|
2010-11-11 21:48:08 -05:00
|
|
|
if (ifn = unfoldSoak(o, this, 'variable')) {
|
2010-10-20 15:51:11 -04:00
|
|
|
return ifn;
|
2010-10-17 00:19:51 -04:00
|
|
|
}
|
2010-11-02 00:31:42 -04:00
|
|
|
_ref = new Value(this.variable).cacheReference(o), left = _ref[0], rite = _ref[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);
|
2010-10-26 08:42:31 -04:00
|
|
|
rite.isNew = this.isNew;
|
2010-10-17 00:19:51 -04:00
|
|
|
left = new Literal("typeof " + (left.compile(o)) + " === \"function\"");
|
2010-10-20 15:51:11 -04:00
|
|
|
return new If(left, new Value(rite), {
|
2010-10-22 21:44:03 -04:00
|
|
|
soak: true
|
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-11-02 00:31:42 -04:00
|
|
|
_ref2 = list.reverse();
|
|
|
|
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
|
|
|
|
call = _ref2[_i];
|
2010-10-17 00:19:51 -04:00
|
|
|
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-11-11 21:48:08 -05:00
|
|
|
ifn = 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-12-13 21:24:32 -05:00
|
|
|
var arg, args, code, _ref;
|
2010-11-02 00:31:42 -04:00
|
|
|
if ((_ref = this.variable) != null) {
|
|
|
|
_ref.front = this.front;
|
2010-05-03 09:04:26 -04:00
|
|
|
}
|
2010-11-01 23:41:05 -04:00
|
|
|
if (code = Splat.compileSplattedArray(o, this.args, true)) {
|
|
|
|
return this.compileSplat(o, code);
|
2010-02-08 22:55:56 -05:00
|
|
|
}
|
2010-11-21 19:59:22 -05:00
|
|
|
args = (function() {
|
2010-12-13 21:24:32 -05:00
|
|
|
var _i, _len, _ref, _results;
|
|
|
|
_ref = this.args;
|
2010-11-09 00:17:08 -05:00
|
|
|
_results = [];
|
2010-12-13 21:24:32 -05:00
|
|
|
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
|
|
|
arg = _ref[_i];
|
2010-11-09 00:17:08 -05:00
|
|
|
_results.push(arg.compile(o, LEVEL_LIST));
|
2010-10-01 18:17:35 -04:00
|
|
|
}
|
2010-11-09 00:17:08 -05:00
|
|
|
return _results;
|
2010-11-21 19:59:22 -05:00
|
|
|
}.call(this)).join(', ');
|
2010-11-08 23:07:51 -05:00
|
|
|
if (this.isSuper) {
|
2010-11-26 02:08:25 -05:00
|
|
|
return this.superReference(o) + (".call(this" + (args && ', ' + args) + ")");
|
2010-11-08 23:07:51 -05:00
|
|
|
} else {
|
|
|
|
return (this.isNew ? 'new ' : '') + this.variable.compile(o, LEVEL_ACCESS) + ("(" + 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-11-01 23:41:05 -04:00
|
|
|
Call.prototype.compileSplat = function(o, splatArgs) {
|
|
|
|
var base, fun, idt, name, ref;
|
2010-10-01 18:17:35 -04:00
|
|
|
if (this.isSuper) {
|
2010-11-01 23:41:05 -04:00
|
|
|
return "" + (this.superReference(o)) + ".apply(this, " + splatArgs + ")";
|
2010-10-01 18:17:35 -04:00
|
|
|
}
|
2010-11-26 02:08:25 -05:00
|
|
|
if (this.isNew) {
|
|
|
|
idt = this.tab + TAB;
|
|
|
|
return "(function(func, args, ctor) {\n" + idt + "ctor.prototype = func.prototype;\n" + idt + "var child = new ctor, result = func.apply(child, args);\n" + idt + "return typeof result === \"object\" ? result : child;\n" + this.tab + "})(" + (this.variable.compile(o, LEVEL_LIST)) + ", " + splatArgs + ", function() {})";
|
|
|
|
}
|
|
|
|
base = new Value(this.variable);
|
|
|
|
if ((name = base.properties.pop()) && base.isComplex()) {
|
2010-12-05 13:10:14 -05:00
|
|
|
ref = o.scope.freeVariable('ref');
|
2010-11-26 02:08:25 -05:00
|
|
|
fun = "(" + ref + " = " + (base.compile(o, LEVEL_LIST)) + ")" + (name.compile(o));
|
|
|
|
} else {
|
|
|
|
fun = base.compile(o, LEVEL_ACCESS);
|
|
|
|
if (name) {
|
|
|
|
ref = fun;
|
|
|
|
fun += name.compile(o);
|
2010-10-01 18:17:35 -04:00
|
|
|
} else {
|
2010-11-26 02:08:25 -05:00
|
|
|
ref = 'null';
|
2010-10-01 18:17:35 -04:00
|
|
|
}
|
|
|
|
}
|
2010-11-26 02:08:25 -05:00
|
|
|
return "" + fun + ".apply(" + ref + ", " + splatArgs + ")";
|
2010-03-17 12:25:04 -04:00
|
|
|
};
|
2010-10-06 22:44:32 -04:00
|
|
|
return Call;
|
2010-11-11 21:48:08 -05:00
|
|
|
}();
|
|
|
|
exports.Extends = Extends = function() {
|
2010-12-06 01:01:57 -05:00
|
|
|
__extends(Extends, Base);
|
2010-11-20 20:39:35 -05:00
|
|
|
function Extends(child, parent) {
|
|
|
|
this.child = child;
|
|
|
|
this.parent = parent;
|
2010-11-11 21:48:08 -05:00
|
|
|
}
|
2010-10-06 22:44:32 -04:00
|
|
|
Extends.prototype.children = ['child', 'parent'];
|
2010-10-23 05:06:04 -04:00
|
|
|
Extends.prototype.compile = function(o) {
|
2010-11-12 00:20:08 -05:00
|
|
|
utility('hasProp');
|
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-11-11 21:48:08 -05:00
|
|
|
}();
|
2010-11-20 20:09:36 -05:00
|
|
|
exports.Access = Access = function() {
|
2010-12-06 01:01:57 -05:00
|
|
|
__extends(Access, Base);
|
2010-11-20 20:39:35 -05:00
|
|
|
function Access(name, tag) {
|
|
|
|
this.name = name;
|
2010-11-11 21:48:08 -05:00
|
|
|
this.proto = tag === 'proto' ? '.prototype' : '';
|
|
|
|
this.soak = tag === 'soak';
|
|
|
|
}
|
2010-11-20 20:09:36 -05:00
|
|
|
Access.prototype.children = ['name'];
|
|
|
|
Access.prototype.compile = 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-22 21:44:03 -04:00
|
|
|
return this.proto + (IS_STRING.test(name) ? "[" + name + "]" : "." + name);
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-11-20 20:09:36 -05:00
|
|
|
Access.prototype.isComplex = NO;
|
|
|
|
return Access;
|
2010-11-11 21:48:08 -05:00
|
|
|
}();
|
|
|
|
exports.Index = Index = function() {
|
2010-12-06 01:01:57 -05:00
|
|
|
__extends(Index, Base);
|
2010-11-20 20:39:35 -05:00
|
|
|
function Index(index) {
|
|
|
|
this.index = index;
|
2010-11-11 21:48:08 -05:00
|
|
|
}
|
2010-10-06 22:44:32 -04:00
|
|
|
Index.prototype.children = ['index'];
|
2010-10-23 05:06:04 -04:00
|
|
|
Index.prototype.compile = function(o) {
|
2010-10-23 11:10:28 -04:00
|
|
|
return (this.proto ? '.prototype' : '') + ("[" + (this.index.compile(o, LEVEL_PAREN)) + "]");
|
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-11-11 21:48:08 -05:00
|
|
|
}();
|
2010-11-20 16:25:22 -05:00
|
|
|
exports.Range = Range = function() {
|
2010-12-06 01:01:57 -05:00
|
|
|
__extends(Range, Base);
|
|
|
|
Range.prototype.children = ['from', 'to'];
|
2010-11-20 20:39:35 -05:00
|
|
|
function Range(from, to, tag) {
|
|
|
|
this.from = from;
|
|
|
|
this.to = to;
|
2010-11-20 16:25:22 -05:00
|
|
|
this.exclusive = tag === 'exclusive';
|
|
|
|
this.equals = this.exclusive ? '' : '=';
|
|
|
|
}
|
|
|
|
Range.prototype.compileVariables = function(o) {
|
|
|
|
var parts, _ref, _ref2, _ref3;
|
|
|
|
o = merge(o, {
|
|
|
|
top: true
|
|
|
|
});
|
|
|
|
_ref = this.from.cache(o, LEVEL_LIST), this.from = _ref[0], this.fromVar = _ref[1];
|
|
|
|
_ref2 = this.to.cache(o, LEVEL_LIST), this.to = _ref2[0], this.toVar = _ref2[1];
|
|
|
|
_ref3 = [this.fromVar.match(SIMPLENUM), this.toVar.match(SIMPLENUM)], this.fromNum = _ref3[0], this.toNum = _ref3[1];
|
|
|
|
parts = [];
|
|
|
|
if (this.from !== this.fromVar) {
|
|
|
|
parts.push(this.from);
|
|
|
|
}
|
|
|
|
if (this.to !== this.toVar) {
|
|
|
|
return parts.push(this.to);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Range.prototype.compileNode = function(o) {
|
|
|
|
var compare, idx, incr, intro, step, stepPart, vars;
|
|
|
|
this.compileVariables(o);
|
|
|
|
if (!o.index) {
|
|
|
|
return this.compileArray(o);
|
|
|
|
}
|
|
|
|
if (this.fromNum && this.toNum) {
|
|
|
|
return this.compileSimple(o);
|
|
|
|
}
|
|
|
|
idx = del(o, 'index');
|
|
|
|
step = del(o, 'step');
|
|
|
|
vars = ("" + idx + " = " + this.from) + (this.to !== this.toVar ? ", " + this.to : '');
|
|
|
|
intro = "(" + this.fromVar + " <= " + this.toVar + " ? " + idx;
|
|
|
|
compare = "" + intro + " <" + this.equals + " " + this.toVar + " : " + idx + " >" + this.equals + " " + this.toVar + ")";
|
|
|
|
stepPart = step ? step.compile(o) : '1';
|
|
|
|
incr = step ? "" + idx + " += " + stepPart : "" + intro + " += " + stepPart + " : " + idx + " -= " + stepPart + ")";
|
|
|
|
return "" + vars + "; " + compare + "; " + incr;
|
|
|
|
};
|
|
|
|
Range.prototype.compileSimple = function(o) {
|
|
|
|
var from, idx, step, to, _ref;
|
|
|
|
_ref = [+this.fromNum, +this.toNum], from = _ref[0], to = _ref[1];
|
|
|
|
idx = del(o, 'index');
|
|
|
|
step = del(o, 'step');
|
|
|
|
step && (step = "" + idx + " += " + (step.compile(o)));
|
|
|
|
if (from <= to) {
|
|
|
|
return "" + idx + " = " + from + "; " + idx + " <" + this.equals + " " + to + "; " + (step || ("" + idx + "++"));
|
|
|
|
} else {
|
|
|
|
return "" + idx + " = " + from + "; " + idx + " >" + this.equals + " " + to + "; " + (step || ("" + idx + "--"));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Range.prototype.compileArray = function(o) {
|
|
|
|
var body, clause, i, idt, post, pre, range, result, vars, _i, _ref, _ref2, _results;
|
|
|
|
if (this.fromNum && this.toNum && Math.abs(this.fromNum - this.toNum) <= 20) {
|
|
|
|
range = (function() {
|
|
|
|
_results = [];
|
|
|
|
for (var _i = _ref = +this.fromNum, _ref2 = +this.toNum; _ref <= _ref2 ? _i <= _ref2 : _i >= _ref2; _ref <= _ref2 ? _i += 1 : _i -= 1){ _results.push(_i); }
|
|
|
|
return _results;
|
|
|
|
}).call(this);
|
|
|
|
if (this.exclusive) {
|
|
|
|
range.pop();
|
|
|
|
}
|
|
|
|
return "[" + (range.join(', ')) + "]";
|
|
|
|
}
|
|
|
|
idt = this.tab + TAB;
|
|
|
|
i = o.scope.freeVariable('i');
|
|
|
|
result = o.scope.freeVariable('results');
|
|
|
|
pre = "\n" + idt + result + " = [];";
|
|
|
|
if (this.fromNum && this.toNum) {
|
|
|
|
o.index = i;
|
|
|
|
body = this.compileSimple(o);
|
|
|
|
} else {
|
|
|
|
vars = ("" + i + " = " + this.from) + (this.to !== this.toVar ? ", " + this.to : '');
|
|
|
|
clause = "" + this.fromVar + " <= " + this.toVar + " ?";
|
|
|
|
body = "var " + vars + "; " + clause + " " + i + " <" + this.equals + " " + this.toVar + " : " + i + " >" + this.equals + " " + this.toVar + "; " + clause + " " + i + " += 1 : " + i + " -= 1";
|
|
|
|
}
|
|
|
|
post = "{ " + result + ".push(" + i + "); }\n" + idt + "return " + result + ";\n" + o.indent;
|
|
|
|
return "(function() {" + pre + "\n" + idt + "for (" + body + ")" + post + "}).call(this)";
|
|
|
|
};
|
|
|
|
return Range;
|
|
|
|
}();
|
|
|
|
exports.Slice = Slice = function() {
|
2010-12-06 01:01:57 -05:00
|
|
|
__extends(Slice, Base);
|
|
|
|
Slice.prototype.children = ['range'];
|
2010-11-20 20:39:35 -05:00
|
|
|
function Slice(range) {
|
|
|
|
this.range = range;
|
2010-11-20 16:25:22 -05:00
|
|
|
Slice.__super__.constructor.call(this);
|
|
|
|
}
|
|
|
|
Slice.prototype.compileNode = function(o) {
|
2010-12-20 08:13:07 -05:00
|
|
|
var compiled, from, fromStr, to, toStr, _ref;
|
2010-12-16 04:39:17 -05:00
|
|
|
_ref = this.range, to = _ref.to, from = _ref.from;
|
2010-12-19 20:19:50 -05:00
|
|
|
fromStr = from && from.compile(o, LEVEL_PAREN) || '0';
|
|
|
|
compiled = to && to.compile(o, LEVEL_PAREN);
|
2010-12-16 04:39:17 -05:00
|
|
|
if (to && !(!this.range.exclusive && +compiled === -1)) {
|
2010-12-20 08:13:07 -05:00
|
|
|
toStr = ', ' + (this.range.exclusive ? compiled : SIMPLENUM.test(compiled) ? (+compiled + 1).toString() : "(" + compiled + " + 1) || 9e9");
|
2010-12-16 04:39:17 -05:00
|
|
|
}
|
|
|
|
return ".slice(" + fromStr + (toStr || '') + ")";
|
2010-11-20 16:25:22 -05:00
|
|
|
};
|
|
|
|
return Slice;
|
|
|
|
}();
|
2010-11-11 21:48:08 -05:00
|
|
|
exports.Obj = Obj = function() {
|
2010-12-06 01:01:57 -05:00
|
|
|
__extends(Obj, Base);
|
2010-11-20 20:39:35 -05:00
|
|
|
function Obj(props, generated) {
|
|
|
|
this.generated = generated != null ? generated : false;
|
2010-11-11 21:48:08 -05:00
|
|
|
this.objects = this.properties = props || [];
|
|
|
|
}
|
2010-10-24 11:35:47 -04:00
|
|
|
Obj.prototype.children = ['properties'];
|
|
|
|
Obj.prototype.compileNode = function(o) {
|
2010-12-16 11:35:51 -05:00
|
|
|
var i, idt, indent, join, lastNoncom, obj, prop, props;
|
2010-11-08 23:48:38 -05:00
|
|
|
props = this.properties;
|
|
|
|
if (!props.length) {
|
|
|
|
if (this.front) {
|
|
|
|
return '({})';
|
|
|
|
} else {
|
|
|
|
return '{}';
|
|
|
|
}
|
|
|
|
}
|
2010-11-08 23:20:11 -05:00
|
|
|
idt = o.indent += TAB;
|
2010-12-16 11:35:51 -05:00
|
|
|
lastNoncom = this.lastNonComment(this.properties);
|
2010-11-21 19:59:22 -05:00
|
|
|
props = function() {
|
2010-12-13 21:24:32 -05:00
|
|
|
var _len, _results;
|
|
|
|
_results = [];
|
|
|
|
for (i = 0, _len = props.length; i < _len; i++) {
|
2010-11-11 02:17:56 -05:00
|
|
|
prop = props[i];
|
|
|
|
join = i === props.length - 1 ? '' : prop === lastNoncom || prop instanceof Comment ? '\n' : ',\n';
|
2010-11-01 13:51:46 -04:00
|
|
|
indent = prop instanceof Comment ? '' : idt;
|
2010-11-01 23:25:28 -04:00
|
|
|
if (prop instanceof Value && prop["this"]) {
|
2010-10-24 01:36:23 -04:00
|
|
|
prop = new Assign(prop.properties[0].name, prop, 'object');
|
|
|
|
} else if (!(prop instanceof Assign) && !(prop instanceof Comment)) {
|
|
|
|
prop = new Assign(prop, prop, 'object');
|
|
|
|
}
|
2010-12-13 21:24:32 -05:00
|
|
|
_results.push(indent + prop.compile(o, LEVEL_TOP) + join);
|
2010-02-09 20:53:25 -05:00
|
|
|
}
|
2010-12-13 21:24:32 -05:00
|
|
|
return _results;
|
2010-11-21 19:59:22 -05:00
|
|
|
}();
|
2010-02-11 23:57:31 -05:00
|
|
|
props = props.join('');
|
2010-11-01 13:51:46 -04:00
|
|
|
obj = "{" + (props && '\n' + props + '\n' + this.tab) + "}";
|
2010-11-08 23:07:51 -05:00
|
|
|
if (this.front) {
|
|
|
|
return "(" + obj + ")";
|
|
|
|
} else {
|
|
|
|
return obj;
|
|
|
|
}
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-10-24 11:35:47 -04:00
|
|
|
Obj.prototype.assigns = function(name) {
|
2010-11-02 00:31:42 -04:00
|
|
|
var prop, _i, _len, _ref;
|
|
|
|
_ref = this.properties;
|
|
|
|
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
|
|
|
prop = _ref[_i];
|
2010-10-10 20:17:31 -04:00
|
|
|
if (prop.assigns(name)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
2010-10-24 11:35:47 -04:00
|
|
|
return Obj;
|
2010-11-11 21:48:08 -05:00
|
|
|
}();
|
|
|
|
exports.Arr = Arr = function() {
|
2010-12-06 01:01:57 -05:00
|
|
|
__extends(Arr, Base);
|
2010-11-11 21:48:08 -05:00
|
|
|
function Arr(objs) {
|
|
|
|
this.objects = objs || [];
|
|
|
|
}
|
2010-10-24 11:35:47 -04:00
|
|
|
Arr.prototype.children = ['objects'];
|
|
|
|
Arr.prototype.compileNode = function(o) {
|
2010-12-13 21:24:32 -05:00
|
|
|
var code, obj;
|
2010-11-08 23:48:38 -05:00
|
|
|
if (!this.objects.length) {
|
|
|
|
return '[]';
|
|
|
|
}
|
2010-11-08 23:20:11 -05:00
|
|
|
o.indent += TAB;
|
2010-11-01 23:41:05 -04:00
|
|
|
if (code = Splat.compileSplattedArray(o, this.objects)) {
|
|
|
|
return code;
|
2010-03-21 11:28:05 -04:00
|
|
|
}
|
2010-11-21 19:59:22 -05:00
|
|
|
code = (function() {
|
2010-12-13 21:24:32 -05:00
|
|
|
var _i, _len, _ref, _results;
|
2010-11-08 23:39:21 -05:00
|
|
|
_ref = this.objects;
|
2010-11-09 00:17:08 -05:00
|
|
|
_results = [];
|
2010-11-08 23:39:21 -05:00
|
|
|
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
|
|
|
obj = _ref[_i];
|
2010-11-09 00:17:08 -05:00
|
|
|
_results.push(obj.compile(o, LEVEL_LIST));
|
2010-11-08 23:39:21 -05:00
|
|
|
}
|
2010-11-09 00:17:08 -05:00
|
|
|
return _results;
|
2010-11-21 19:59:22 -05:00
|
|
|
}.call(this)).join(', ');
|
2010-11-08 23:39:21 -05:00
|
|
|
if (code.indexOf('\n') >= 0) {
|
|
|
|
return "[\n" + o.indent + code + "\n" + this.tab + "]";
|
2010-11-08 23:07:51 -05:00
|
|
|
} else {
|
2010-11-08 23:39:21 -05:00
|
|
|
return "[" + code + "]";
|
2010-11-08 23:07:51 -05:00
|
|
|
}
|
2010-03-07 19:11:03 -05:00
|
|
|
};
|
2010-10-24 11:35:47 -04:00
|
|
|
Arr.prototype.assigns = function(name) {
|
2010-11-02 00:31:42 -04:00
|
|
|
var obj, _i, _len, _ref;
|
|
|
|
_ref = this.objects;
|
|
|
|
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
|
|
|
obj = _ref[_i];
|
2010-10-10 20:17:31 -04:00
|
|
|
if (obj.assigns(name)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
2010-10-24 11:35:47 -04:00
|
|
|
return Arr;
|
2010-11-11 21:48:08 -05:00
|
|
|
}();
|
|
|
|
exports.Class = Class = function() {
|
2010-12-06 01:01:57 -05:00
|
|
|
__extends(Class, Base);
|
2010-11-20 20:39:35 -05:00
|
|
|
function Class(variable, parent, body) {
|
|
|
|
this.variable = variable;
|
|
|
|
this.parent = parent;
|
|
|
|
this.body = body != null ? body : new Expressions;
|
2010-11-13 16:53:55 -05:00
|
|
|
this.boundFuncs = [];
|
2010-11-11 21:48:08 -05:00
|
|
|
}
|
|
|
|
Class.prototype.children = ['variable', 'parent', 'body'];
|
2010-11-13 16:53:55 -05:00
|
|
|
Class.prototype.determineName = function() {
|
|
|
|
var decl, tail;
|
|
|
|
if (!this.variable) {
|
|
|
|
return null;
|
2010-11-11 21:48:08 -05:00
|
|
|
}
|
2010-11-20 20:09:36 -05:00
|
|
|
decl = (tail = last(this.variable.properties)) ? tail instanceof Access && tail.name.value : this.variable.base.value;
|
2010-11-13 16:53:55 -05:00
|
|
|
return decl && (decl = IDENTIFIER.test(decl) && decl);
|
|
|
|
};
|
|
|
|
Class.prototype.setContext = function(name) {
|
|
|
|
return this.body.traverseChildren(false, function(node) {
|
2010-11-11 21:48:08 -05:00
|
|
|
if (node instanceof Literal && node.value === 'this') {
|
|
|
|
return node.value = name;
|
|
|
|
} else if (node instanceof Code) {
|
|
|
|
node.klass = name;
|
|
|
|
if (node.bound) {
|
|
|
|
return node.context = name;
|
2010-06-30 22:03:20 -04:00
|
|
|
}
|
2010-06-15 01:28:30 -04:00
|
|
|
}
|
2010-11-11 21:48:08 -05:00
|
|
|
});
|
2010-11-13 16:53:55 -05:00
|
|
|
};
|
2010-11-13 17:05:54 -05:00
|
|
|
Class.prototype.addBoundFunctions = function(o) {
|
|
|
|
var bname, bvar, _i, _len, _ref, _results;
|
|
|
|
if (this.boundFuncs.length) {
|
|
|
|
_ref = this.boundFuncs;
|
|
|
|
_results = [];
|
|
|
|
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
|
|
|
bvar = _ref[_i];
|
|
|
|
bname = bvar.compile(o);
|
|
|
|
_results.push(this.ctor.body.unshift(new Literal("this." + bname + " = " + (utility('bind')) + "(this." + bname + ", this);")));
|
|
|
|
}
|
|
|
|
return _results;
|
|
|
|
}
|
|
|
|
};
|
2010-11-13 16:53:55 -05:00
|
|
|
Class.prototype.addProperties = function(node, name) {
|
|
|
|
var assign, base, func, props, _results;
|
|
|
|
props = node.base.properties.slice(0);
|
|
|
|
_results = [];
|
|
|
|
while (assign = props.shift()) {
|
|
|
|
if (assign instanceof Assign) {
|
|
|
|
base = assign.variable.base;
|
|
|
|
delete assign.context;
|
|
|
|
func = assign.value;
|
2010-11-14 14:21:55 -05:00
|
|
|
if (base.value === 'constructor') {
|
|
|
|
if (this.ctor) {
|
|
|
|
throw new Error('cannot define more than one constructor in a class');
|
|
|
|
}
|
|
|
|
if (func.bound) {
|
|
|
|
throw new Error('cannot define a constructor as a bound function');
|
|
|
|
}
|
|
|
|
if (func instanceof Code) {
|
2010-12-06 01:01:57 -05:00
|
|
|
assign = this.ctor = func;
|
2010-11-14 14:21:55 -05:00
|
|
|
} else {
|
2010-12-06 01:01:57 -05:00
|
|
|
assign = this.ctor = new Assign(new Value(new Literal(name)), func);
|
2010-11-14 14:21:55 -05:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!assign.variable["this"]) {
|
2010-11-20 20:09:36 -05:00
|
|
|
assign.variable = new Value(new Literal(name), [new Access(base, 'proto')]);
|
2010-11-14 14:21:55 -05:00
|
|
|
}
|
|
|
|
if (func instanceof Code && func.bound) {
|
|
|
|
this.boundFuncs.push(base);
|
|
|
|
func.bound = false;
|
|
|
|
}
|
2010-11-12 00:05:30 -05:00
|
|
|
}
|
|
|
|
}
|
2010-11-13 16:53:55 -05:00
|
|
|
_results.push(assign);
|
|
|
|
}
|
|
|
|
return _results;
|
|
|
|
};
|
2010-11-13 17:05:54 -05:00
|
|
|
Class.prototype.walkBody = function(name) {
|
2010-11-13 18:02:50 -05:00
|
|
|
return this.traverseChildren(false, __bind(function(child) {
|
|
|
|
var exps, i, node, _len, _ref;
|
2010-11-28 17:56:07 -05:00
|
|
|
if (child instanceof Class) {
|
|
|
|
return false;
|
|
|
|
}
|
2010-11-13 18:02:50 -05:00
|
|
|
if (child instanceof Expressions) {
|
|
|
|
_ref = exps = child.expressions;
|
|
|
|
for (i = 0, _len = _ref.length; i < _len; i++) {
|
|
|
|
node = _ref[i];
|
|
|
|
if (node instanceof Value && node.isObject(true)) {
|
2010-12-06 01:01:57 -05:00
|
|
|
exps[i] = this.addProperties(node, name);
|
2010-11-12 00:05:30 -05:00
|
|
|
}
|
2010-11-13 18:02:50 -05:00
|
|
|
}
|
2010-12-06 01:01:57 -05:00
|
|
|
return child.expressions = exps = flatten(exps);
|
2010-11-13 16:53:55 -05:00
|
|
|
}
|
2010-11-13 18:02:50 -05:00
|
|
|
}, this));
|
2010-11-13 17:05:54 -05:00
|
|
|
};
|
|
|
|
Class.prototype.ensureConstructor = function(name) {
|
|
|
|
if (!this.ctor) {
|
|
|
|
this.ctor = new Code;
|
2010-11-11 21:48:08 -05:00
|
|
|
if (this.parent) {
|
2010-11-13 17:05:54 -05:00
|
|
|
this.ctor.body.push(new Call('super', [new Splat(new Literal('arguments'))]));
|
2010-11-11 21:48:08 -05:00
|
|
|
}
|
2010-12-06 01:01:57 -05:00
|
|
|
this.body.expressions.unshift(this.ctor);
|
2010-02-27 18:57:45 -05:00
|
|
|
}
|
2010-11-13 17:05:54 -05:00
|
|
|
this.ctor.ctor = this.ctor.name = name;
|
|
|
|
this.ctor.klass = null;
|
|
|
|
return this.ctor.noReturn = true;
|
|
|
|
};
|
|
|
|
Class.prototype.compileNode = function(o) {
|
2010-11-28 13:08:49 -05:00
|
|
|
var decl, klass, lname, name;
|
2010-11-13 17:05:54 -05:00
|
|
|
decl = this.determineName();
|
|
|
|
name = decl || this.name || '_Class';
|
|
|
|
lname = new Literal(name);
|
|
|
|
this.setContext(name);
|
|
|
|
this.walkBody(name);
|
2010-11-11 21:48:08 -05:00
|
|
|
if (this.parent) {
|
2010-11-13 17:05:54 -05:00
|
|
|
this.body.expressions.unshift(new Extends(lname, this.parent));
|
2010-11-11 23:04:58 -05:00
|
|
|
}
|
2010-12-06 01:01:57 -05:00
|
|
|
this.ensureConstructor(name);
|
2010-11-13 17:05:54 -05:00
|
|
|
this.body.expressions.push(lname);
|
|
|
|
this.addBoundFunctions(o);
|
2010-11-11 21:48:08 -05:00
|
|
|
klass = new Parens(new Call(new Code([], this.body)), true);
|
|
|
|
if (this.variable) {
|
|
|
|
klass = new Assign(this.variable, klass);
|
2010-10-23 05:06:04 -04:00
|
|
|
}
|
2010-11-11 21:48:08 -05:00
|
|
|
return klass.compile(o);
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-10-06 22:44:32 -04:00
|
|
|
return Class;
|
2010-11-11 21:48:08 -05:00
|
|
|
}();
|
|
|
|
exports.Assign = Assign = function() {
|
2010-12-06 01:01:57 -05:00
|
|
|
__extends(Assign, Base);
|
2010-12-09 21:34:52 -05:00
|
|
|
function Assign(variable, value, context, options) {
|
2010-11-20 20:39:35 -05:00
|
|
|
this.variable = variable;
|
|
|
|
this.value = value;
|
|
|
|
this.context = context;
|
2010-12-09 21:34:52 -05:00
|
|
|
this.param = options && options.param;
|
2010-11-11 21:48:08 -05:00
|
|
|
}
|
|
|
|
Assign.prototype.METHOD_DEF = /^(?:(\S+)\.prototype\.|\S+?)?\b([$A-Za-z_][$\w]*)$/;
|
2010-10-06 22:44:32 -04:00
|
|
|
Assign.prototype.children = ['variable', 'value'];
|
2010-10-23 05:06:04 -04:00
|
|
|
Assign.prototype.assigns = function(name) {
|
|
|
|
return this[this.context === 'object' ? 'value' : 'variable'].assigns(name);
|
|
|
|
};
|
|
|
|
Assign.prototype.unfoldSoak = function(o) {
|
2010-11-11 21:48:08 -05:00
|
|
|
return unfoldSoak(o, this, 'variable');
|
2010-10-23 05:06:04 -04:00
|
|
|
};
|
2010-10-06 22:44:32 -04:00
|
|
|
Assign.prototype.compileNode = function(o) {
|
2010-11-02 00:31:42 -04:00
|
|
|
var isValue, match, name, val, _ref;
|
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-11-20 18:49:17 -05:00
|
|
|
if (this.variable.isSplice()) {
|
|
|
|
return this.compileSplice(o);
|
|
|
|
}
|
2010-11-02 00:31:42 -04:00
|
|
|
if ((_ref = this.context) === '||=' || _ref === '&&=' || _ref === '?=') {
|
2010-10-19 20:42:12 -04:00
|
|
|
return this.compileConditional(o);
|
|
|
|
}
|
2010-02-09 20:53:25 -05:00
|
|
|
}
|
2010-10-23 11:10:28 -04:00
|
|
|
name = this.variable.compile(o, LEVEL_LIST);
|
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];
|
2010-11-11 21:48:08 -05:00
|
|
|
if (match[1]) {
|
|
|
|
this.value.klass = match[1];
|
|
|
|
}
|
2010-02-09 20:53:25 -05:00
|
|
|
}
|
2010-10-23 11:10:28 -04:00
|
|
|
val = this.value.compile(o, LEVEL_LIST);
|
2010-02-09 20:53:25 -05:00
|
|
|
if (this.context === 'object') {
|
2010-10-20 18:19:08 -04:00
|
|
|
return "" + name + ": " + val;
|
2010-02-09 20:53:25 -05:00
|
|
|
}
|
2010-10-24 18:33:41 -04:00
|
|
|
if (!this.variable.isAssignable()) {
|
|
|
|
throw SyntaxError("\"" + (this.variable.compile(o)) + "\" cannot be assigned.");
|
2010-10-24 15:45:31 -04:00
|
|
|
}
|
2010-10-31 21:42:42 -04:00
|
|
|
if (!(this.context || isValue && (this.variable.namespaced || this.variable.hasProperties()))) {
|
2010-12-09 21:34:52 -05:00
|
|
|
if (this.param) {
|
|
|
|
o.scope.add(name, 'var');
|
|
|
|
} else {
|
|
|
|
o.scope.find(name);
|
|
|
|
}
|
2010-02-09 20:53:25 -05:00
|
|
|
}
|
2010-10-19 20:42:12 -04:00
|
|
|
val = name + (" " + (this.context || '=') + " ") + val;
|
2010-11-08 23:07:51 -05:00
|
|
|
if (o.level <= LEVEL_LIST) {
|
|
|
|
return val;
|
|
|
|
} else {
|
|
|
|
return "(" + val + ")";
|
|
|
|
}
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-10-06 22:44:32 -04:00
|
|
|
Assign.prototype.compilePatternMatch = function(o) {
|
2010-11-14 09:47:06 -05:00
|
|
|
var acc, assigns, code, i, idx, isObject, ivar, obj, objects, olen, ref, rest, splat, top, val, value, vvar, _len, _ref, _ref2, _ref3, _ref4;
|
2010-10-23 11:10:28 -04:00
|
|
|
top = o.level === LEVEL_TOP;
|
2010-10-22 21:44:03 -04:00
|
|
|
value = this.value;
|
2010-09-28 16:47:12 -04:00
|
|
|
objects = this.variable.base.objects;
|
2010-10-26 07:51:02 -04:00
|
|
|
if (!(olen = 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-26 07:51:02 -04:00
|
|
|
if (top && olen === 1 && !((obj = objects[0]) instanceof Splat)) {
|
2010-10-06 22:44:32 -04:00
|
|
|
if (obj instanceof Assign) {
|
2010-11-14 09:47:06 -05:00
|
|
|
_ref = obj, idx = _ref.variable.base, obj = _ref.value;
|
2010-09-28 16:47:12 -04:00
|
|
|
} else {
|
2010-10-24 03:59:23 -04:00
|
|
|
if (obj.base instanceof Parens) {
|
2010-11-14 09:47:06 -05:00
|
|
|
_ref2 = new Value(obj.unwrapAll()).cacheReference(o), obj = _ref2[0], idx = _ref2[1];
|
2010-10-24 03:59:23 -04:00
|
|
|
} else {
|
2010-11-01 23:25:28 -04:00
|
|
|
idx = isObject ? obj["this"] ? obj.properties[0].name : obj : new Literal(0);
|
2010-10-24 03:59:23 -04:00
|
|
|
}
|
2010-09-28 16:47:12 -04:00
|
|
|
}
|
2010-10-24 03:59:23 -04:00
|
|
|
acc = IDENTIFIER.test(idx.unwrap().value || 0);
|
2010-10-24 19:13:58 -04:00
|
|
|
value = new Value(value);
|
2010-11-20 20:09:36 -05:00
|
|
|
value.properties.push(new (acc ? Access : Index)(idx));
|
2010-10-23 05:06:04 -04:00
|
|
|
return new Assign(obj, value).compile(o);
|
2010-09-28 16:47:12 -04:00
|
|
|
}
|
2010-10-26 07:51:02 -04:00
|
|
|
vvar = value.compile(o, LEVEL_LIST);
|
2010-10-10 20:17:31 -04:00
|
|
|
assigns = [];
|
2010-03-22 02:02:04 -04:00
|
|
|
splat = false;
|
2010-10-26 07:51:02 -04:00
|
|
|
if (!IDENTIFIER.test(vvar) || this.variable.assigns(vvar)) {
|
|
|
|
assigns.push("" + (ref = o.scope.freeVariable('ref')) + " = " + vvar);
|
|
|
|
vvar = ref;
|
2010-10-10 20:17:31 -04:00
|
|
|
}
|
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-11-14 09:47:06 -05:00
|
|
|
_ref3 = obj, idx = _ref3.variable.base, obj = _ref3.value;
|
2010-04-25 22:21:53 -04:00
|
|
|
} else {
|
2010-10-24 03:59:23 -04:00
|
|
|
if (obj.base instanceof Parens) {
|
2010-11-14 09:47:06 -05:00
|
|
|
_ref4 = new Value(obj.unwrapAll()).cacheReference(o), obj = _ref4[0], idx = _ref4[1];
|
2010-10-24 03:59:23 -04:00
|
|
|
} else {
|
2010-11-01 23:25:28 -04:00
|
|
|
idx = obj["this"] ? obj.properties[0].name : obj;
|
2010-10-24 03:59:23 -04:00
|
|
|
}
|
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 (!splat && obj instanceof Splat) {
|
2010-10-26 07:51:02 -04:00
|
|
|
val = "" + olen + " <= " + vvar + ".length ? " + (utility('slice')) + ".call(" + vvar + ", " + i;
|
|
|
|
if (rest = olen - i - 1) {
|
2010-10-26 05:52:43 -04:00
|
|
|
ivar = o.scope.freeVariable('i');
|
2010-10-26 07:51:02 -04:00
|
|
|
val += ", " + ivar + " = " + vvar + ".length - " + rest + ") : (" + ivar + " = " + i + ", [])";
|
|
|
|
} else {
|
|
|
|
val += ") : []";
|
2010-10-26 05:52:43 -04:00
|
|
|
}
|
2010-10-26 07:51:02 -04:00
|
|
|
val = new Literal(val);
|
|
|
|
splat = "" + ivar + "++";
|
2010-02-11 20:11:11 -05:00
|
|
|
} else {
|
2010-10-26 06:32:48 -04:00
|
|
|
if (obj instanceof Splat) {
|
|
|
|
obj = obj.name.compile(o);
|
|
|
|
throw SyntaxError("multiple splats are disallowed in an assignment: " + obj + " ...");
|
|
|
|
}
|
2010-10-26 05:52:43 -04:00
|
|
|
if (typeof idx === 'number') {
|
|
|
|
idx = new Literal(splat || idx);
|
2010-10-24 03:59:23 -04:00
|
|
|
acc = false;
|
|
|
|
} else {
|
|
|
|
acc = isObject && IDENTIFIER.test(idx.unwrap().value || 0);
|
2010-03-22 02:02:04 -04:00
|
|
|
}
|
2010-11-20 20:09:36 -05:00
|
|
|
val = new Value(new Literal(vvar), [new (acc ? Access : Index)(idx)]);
|
2010-02-11 20:11:11 -05:00
|
|
|
}
|
2010-12-09 21:34:52 -05:00
|
|
|
assigns.push(new Assign(obj, val, null, {
|
|
|
|
param: this.param
|
|
|
|
}).compile(o, LEVEL_TOP));
|
2010-02-09 20:53:25 -05:00
|
|
|
}
|
2010-10-07 07:05:22 -04:00
|
|
|
if (!top) {
|
2010-10-26 07:51:02 -04:00
|
|
|
assigns.push(vvar);
|
2010-09-28 16:47:12 -04:00
|
|
|
}
|
|
|
|
code = assigns.join(', ');
|
2010-11-08 23:07:51 -05:00
|
|
|
if (o.level < LEVEL_LIST) {
|
|
|
|
return code;
|
|
|
|
} else {
|
|
|
|
return "(" + code + ")";
|
|
|
|
}
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-10-19 20:42:12 -04:00
|
|
|
Assign.prototype.compileConditional = function(o) {
|
2010-11-02 00:31:42 -04:00
|
|
|
var left, rite, _ref;
|
|
|
|
_ref = this.variable.cacheReference(o), left = _ref[0], rite = _ref[1];
|
2010-10-31 21:42:42 -04:00
|
|
|
return new Op(this.context.slice(0, -1), left, new Assign(rite, this.value, '=')).compile(o);
|
2010-10-19 20:42:12 -04:00
|
|
|
};
|
2010-11-20 16:25:22 -05:00
|
|
|
Assign.prototype.compileSplice = function(o) {
|
2010-12-21 00:46:31 -05:00
|
|
|
var exclusive, from, fromDecl, fromRef, name, to, val, _ref, _ref2;
|
|
|
|
_ref = this.variable.properties.pop().range, from = _ref.from, to = _ref.to, exclusive = _ref.exclusive;
|
2010-11-20 16:25:22 -05:00
|
|
|
name = this.variable.compile(o);
|
2010-12-21 00:48:54 -05:00
|
|
|
_ref2 = (from != null ? from.cache(o, LEVEL_OP) : void 0) || ['0', '0'], fromDecl = _ref2[0], fromRef = _ref2[1];
|
2010-12-21 00:46:31 -05:00
|
|
|
if (to) {
|
2010-12-21 00:48:54 -05:00
|
|
|
if ((from != null ? from.isSimpleNumber() : void 0) && to.isSimpleNumber()) {
|
2010-12-21 00:46:31 -05:00
|
|
|
to = +to.compile(o) - +fromRef;
|
|
|
|
if (!exclusive) {
|
2010-12-07 14:27:44 -05:00
|
|
|
to += 1;
|
|
|
|
}
|
2010-12-04 15:06:21 -05:00
|
|
|
} else {
|
2010-12-21 00:46:31 -05:00
|
|
|
to = to.compile(o) + ' - ' + fromRef;
|
|
|
|
if (!exclusive) {
|
2010-12-07 14:27:44 -05:00
|
|
|
to += ' + 1';
|
|
|
|
}
|
2010-12-04 15:06:21 -05:00
|
|
|
}
|
2010-12-21 00:46:31 -05:00
|
|
|
} else {
|
|
|
|
to = "" + name + ".length";
|
2010-12-04 15:06:21 -05:00
|
|
|
}
|
2010-11-20 16:25:22 -05:00
|
|
|
val = this.value.compile(o);
|
2010-12-20 23:41:58 -05:00
|
|
|
return "[].splice.apply(" + name + ", [" + fromDecl + ", " + to + "].concat(" + val + "))";
|
2010-11-20 16:25:22 -05:00
|
|
|
};
|
2010-10-06 22:44:32 -04:00
|
|
|
return Assign;
|
2010-11-11 21:48:08 -05:00
|
|
|
}();
|
|
|
|
exports.Code = Code = function() {
|
2010-12-06 01:01:57 -05:00
|
|
|
__extends(Code, Base);
|
2010-11-11 21:48:08 -05:00
|
|
|
function Code(params, body, tag) {
|
|
|
|
this.params = params || [];
|
|
|
|
this.body = body || new Expressions;
|
|
|
|
this.bound = tag === 'boundfunc';
|
|
|
|
if (this.bound) {
|
|
|
|
this.context = 'this';
|
2010-10-24 20:51:55 -04:00
|
|
|
}
|
2010-11-11 21:48:08 -05:00
|
|
|
}
|
2010-10-06 22:44:32 -04:00
|
|
|
Code.prototype.children = ['params', 'body'];
|
2010-11-11 21:48:08 -05:00
|
|
|
Code.prototype.isStatement = function() {
|
|
|
|
return !!this.ctor;
|
|
|
|
};
|
2010-10-06 22:44:32 -04:00
|
|
|
Code.prototype.compileNode = function(o) {
|
2010-12-13 21:24:32 -05:00
|
|
|
var code, exprs, i, idt, lit, p, param, ref, splats, v, val, vars, wasEmpty, _i, _j, _len, _len2, _len3, _ref, _ref2, _ref3;
|
|
|
|
o.scope = new Scope(o.scope, this.body, this);
|
|
|
|
o.scope.shared = del(o, 'sharedScope');
|
2010-11-08 23:20:11 -05:00
|
|
|
o.indent += TAB;
|
2010-10-13 15:09:56 -04:00
|
|
|
delete o.bare;
|
|
|
|
delete o.globals;
|
2010-10-26 05:52:43 -04:00
|
|
|
vars = [];
|
|
|
|
exprs = [];
|
2010-11-02 00:31:42 -04:00
|
|
|
_ref = this.params;
|
|
|
|
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
|
|
|
param = _ref[_i];
|
2010-10-26 05:52:43 -04:00
|
|
|
if (param.splat) {
|
2010-11-21 19:59:22 -05:00
|
|
|
splats = new Assign(new Value(new Arr(function() {
|
2010-12-13 21:24:32 -05:00
|
|
|
var _i, _len, _ref, _results;
|
|
|
|
_ref = this.params;
|
2010-11-09 00:17:08 -05:00
|
|
|
_results = [];
|
2010-12-13 21:24:32 -05:00
|
|
|
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
|
|
|
p = _ref[_i];
|
2010-11-09 00:17:08 -05:00
|
|
|
_results.push(p.asReference(o));
|
2010-10-26 05:52:43 -04:00
|
|
|
}
|
2010-11-09 00:17:08 -05:00
|
|
|
return _results;
|
2010-11-21 19:59:22 -05:00
|
|
|
}.call(this))), new Value(new Literal('arguments')));
|
2010-10-26 05:52:43 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-12-13 21:24:32 -05:00
|
|
|
_ref2 = this.params;
|
|
|
|
for (_j = 0, _len2 = _ref2.length; _j < _len2; _j++) {
|
|
|
|
param = _ref2[_j];
|
2010-10-26 15:46:03 -04:00
|
|
|
if (param.isComplex()) {
|
2010-11-01 23:11:25 -04:00
|
|
|
val = ref = param.asReference(o);
|
|
|
|
if (param.value) {
|
|
|
|
val = new Op('?', ref, param.value);
|
|
|
|
}
|
2010-12-09 21:34:52 -05:00
|
|
|
exprs.push(new Assign(new Value(param.name), val, '=', {
|
|
|
|
param: true
|
|
|
|
}));
|
2010-03-17 14:41:09 -04:00
|
|
|
} else {
|
2010-10-26 05:52:43 -04:00
|
|
|
ref = param;
|
|
|
|
if (param.value) {
|
2010-11-01 23:11:25 -04:00
|
|
|
lit = new Literal(ref.name.value + ' == null');
|
|
|
|
val = new Assign(new Value(param.name), param.value, '=');
|
2010-11-21 21:12:59 -05:00
|
|
|
exprs.push(new If(lit, val));
|
2010-07-28 01:54:36 -04:00
|
|
|
}
|
2010-03-17 14:41:09 -04:00
|
|
|
}
|
2010-10-26 05:52:43 -04:00
|
|
|
if (!splats) {
|
|
|
|
vars.push(ref);
|
|
|
|
}
|
2010-02-09 21:31:07 -05:00
|
|
|
}
|
2010-10-26 05:52:43 -04:00
|
|
|
wasEmpty = this.body.isEmpty();
|
|
|
|
if (splats) {
|
|
|
|
exprs.unshift(splats);
|
|
|
|
}
|
|
|
|
if (exprs.length) {
|
2010-12-13 21:24:32 -05:00
|
|
|
(_ref3 = this.body.expressions).unshift.apply(_ref3, exprs);
|
2010-10-23 05:06:04 -04:00
|
|
|
}
|
2010-10-26 05:52:43 -04:00
|
|
|
if (!splats) {
|
2010-12-13 21:24:32 -05:00
|
|
|
for (i = 0, _len3 = vars.length; i < _len3; i++) {
|
2010-10-26 05:52:43 -04:00
|
|
|
v = vars[i];
|
2010-12-05 21:18:30 -05:00
|
|
|
o.scope.parameter(vars[i] = v.compile(o));
|
2010-02-12 13:35:34 -05:00
|
|
|
}
|
2010-10-26 00:09:46 -04:00
|
|
|
}
|
2010-11-11 21:48:08 -05:00
|
|
|
if (!(wasEmpty || this.noReturn)) {
|
|
|
|
this.body.makeReturn();
|
|
|
|
}
|
2010-11-08 23:20:11 -05:00
|
|
|
idt = o.indent;
|
2010-11-11 21:48:08 -05:00
|
|
|
code = 'function';
|
|
|
|
if (this.ctor) {
|
|
|
|
code += ' ' + this.name;
|
2010-10-04 23:21:16 -04:00
|
|
|
}
|
2010-11-11 21:48:08 -05:00
|
|
|
code += '(' + vars.join(', ') + ') {';
|
|
|
|
if (!this.body.isEmpty()) {
|
|
|
|
code += "\n" + (this.body.compileWithDeclarations(o)) + "\n" + this.tab;
|
|
|
|
}
|
|
|
|
code += '}';
|
|
|
|
if (this.ctor) {
|
|
|
|
return this.tab + code;
|
2010-10-23 05:06:04 -04:00
|
|
|
}
|
2010-07-25 03:15:12 -04:00
|
|
|
if (this.bound) {
|
2010-11-11 21:48:08 -05:00
|
|
|
return utility('bind') + ("(" + code + ", " + this.context + ")");
|
2010-02-09 21:31:07 -05:00
|
|
|
}
|
2010-11-08 23:07:51 -05:00
|
|
|
if (this.front) {
|
2010-11-11 21:48:08 -05:00
|
|
|
return "(" + code + ")";
|
2010-11-08 23:07:51 -05:00
|
|
|
} else {
|
2010-11-11 21:48:08 -05:00
|
|
|
return code;
|
2010-11-08 23:07:51 -05:00
|
|
|
}
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-10-06 22:44:32 -04:00
|
|
|
Code.prototype.traverseChildren = function(crossScope, func) {
|
2010-11-08 23:07:51 -05:00
|
|
|
if (crossScope) {
|
|
|
|
return Code.__super__.traverseChildren.call(this, crossScope, func);
|
|
|
|
}
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-10-06 22:44:32 -04:00
|
|
|
return Code;
|
2010-11-11 21:48:08 -05:00
|
|
|
}();
|
|
|
|
exports.Param = Param = function() {
|
2010-12-06 01:01:57 -05:00
|
|
|
__extends(Param, Base);
|
2010-11-20 20:39:35 -05:00
|
|
|
function Param(name, value, splat) {
|
|
|
|
this.name = name;
|
|
|
|
this.value = value;
|
|
|
|
this.splat = splat;
|
2010-11-11 21:48:08 -05:00
|
|
|
}
|
2010-10-26 00:09:46 -04:00
|
|
|
Param.prototype.children = ['name', 'value'];
|
2010-10-23 05:06:04 -04:00
|
|
|
Param.prototype.compile = function(o) {
|
2010-10-26 00:09:46 -04:00
|
|
|
return this.name.compile(o, LEVEL_LIST);
|
2010-07-28 01:54:36 -04:00
|
|
|
};
|
2010-10-26 05:52:43 -04:00
|
|
|
Param.prototype.asReference = function(o) {
|
|
|
|
var node;
|
|
|
|
if (this.reference) {
|
|
|
|
return this.reference;
|
2010-09-26 15:47:52 -04:00
|
|
|
}
|
2010-11-20 20:39:35 -05:00
|
|
|
node = this.name;
|
|
|
|
if (node["this"]) {
|
|
|
|
node = node.properties[0].name;
|
|
|
|
if (node.value.reserved) {
|
|
|
|
node = new Literal('_' + node.value);
|
|
|
|
}
|
|
|
|
} else if (node.isComplex()) {
|
|
|
|
node = new Literal(o.scope.freeVariable('arg'));
|
|
|
|
}
|
2010-10-26 05:52:43 -04:00
|
|
|
node = new Value(node);
|
2010-09-26 15:47:52 -04:00
|
|
|
if (this.splat) {
|
2010-10-26 05:52:43 -04:00
|
|
|
node = new Splat(node);
|
2010-09-26 15:47:52 -04:00
|
|
|
}
|
2010-10-26 05:52:43 -04:00
|
|
|
return this.reference = node;
|
|
|
|
};
|
2010-10-26 15:46:03 -04:00
|
|
|
Param.prototype.isComplex = function() {
|
|
|
|
return this.name.isComplex();
|
2010-07-28 01:54:36 -04:00
|
|
|
};
|
2010-10-06 22:44:32 -04:00
|
|
|
return Param;
|
2010-11-11 21:48:08 -05:00
|
|
|
}();
|
|
|
|
exports.Splat = Splat = function() {
|
2010-10-06 22:44:32 -04:00
|
|
|
__extends(Splat, Base);
|
|
|
|
Splat.prototype.children = ['name'];
|
2010-10-24 18:33:41 -04:00
|
|
|
Splat.prototype.isAssignable = YES;
|
2010-12-06 01:01:57 -05:00
|
|
|
function Splat(name) {
|
|
|
|
this.name = name.compile ? name : new Literal(name);
|
|
|
|
}
|
2010-10-10 20:17:31 -04:00
|
|
|
Splat.prototype.assigns = function(name) {
|
|
|
|
return this.name.assigns(name);
|
|
|
|
};
|
2010-10-23 05:06:04 -04:00
|
|
|
Splat.prototype.compile = function(o) {
|
2010-11-08 23:07:51 -05:00
|
|
|
if (this.index != null) {
|
|
|
|
return this.compileParam(o);
|
|
|
|
} else {
|
|
|
|
return this.name.compile(o);
|
|
|
|
}
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-11-01 23:41:05 -04:00
|
|
|
Splat.compileSplattedArray = function(o, list, apply) {
|
2010-12-13 21:24:32 -05:00
|
|
|
var args, base, code, i, index, node, _len;
|
2010-11-01 23:41:05 -04:00
|
|
|
index = -1;
|
|
|
|
while ((node = list[++index]) && !(node instanceof Splat)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (index >= list.length) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
if (list.length === 1) {
|
|
|
|
code = list[0].compile(o, LEVEL_LIST);
|
|
|
|
if (apply) {
|
|
|
|
return code;
|
2010-03-18 09:27:13 -04:00
|
|
|
}
|
2010-11-01 23:41:05 -04:00
|
|
|
return "" + (utility('slice')) + ".call(" + code + ")";
|
|
|
|
}
|
|
|
|
args = list.slice(index);
|
|
|
|
for (i = 0, _len = args.length; i < _len; i++) {
|
|
|
|
node = args[i];
|
|
|
|
code = node.compile(o, LEVEL_LIST);
|
|
|
|
args[i] = node instanceof Splat ? "" + (utility('slice')) + ".call(" + code + ")" : "[" + code + "]";
|
2010-03-18 09:27:13 -04:00
|
|
|
}
|
2010-11-01 23:41:05 -04:00
|
|
|
if (index === 0) {
|
|
|
|
return args[0] + (".concat(" + (args.slice(1).join(', ')) + ")");
|
|
|
|
}
|
2010-11-21 19:59:22 -05:00
|
|
|
base = (function() {
|
2010-12-13 21:24:32 -05:00
|
|
|
var _i, _len, _ref, _results;
|
2010-11-02 00:31:42 -04:00
|
|
|
_ref = list.slice(0, index);
|
2010-11-09 00:17:08 -05:00
|
|
|
_results = [];
|
2010-12-13 21:24:32 -05:00
|
|
|
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
2010-11-02 00:31:42 -04:00
|
|
|
node = _ref[_i];
|
2010-11-09 00:17:08 -05:00
|
|
|
_results.push(node.compile(o, LEVEL_LIST));
|
2010-11-01 23:41:05 -04:00
|
|
|
}
|
2010-11-09 00:17:08 -05:00
|
|
|
return _results;
|
2010-11-21 19:59:22 -05:00
|
|
|
}());
|
2010-11-01 23:41:05 -04:00
|
|
|
return "[" + (base.join(', ')) + "].concat(" + (args.join(', ')) + ")";
|
2010-03-29 21:49:20 -04:00
|
|
|
};
|
2010-10-06 22:44:32 -04:00
|
|
|
return Splat;
|
2010-11-11 21:48:08 -05:00
|
|
|
}();
|
|
|
|
exports.While = While = function() {
|
2010-12-06 01:01:57 -05:00
|
|
|
__extends(While, Base);
|
2010-11-11 21:48:08 -05:00
|
|
|
function While(condition, options) {
|
|
|
|
this.condition = (options != null ? options.invert : void 0) ? condition.invert() : condition;
|
|
|
|
this.guard = options != null ? options.guard : void 0;
|
|
|
|
}
|
2010-10-06 22:44:32 -04:00
|
|
|
While.prototype.children = ['condition', 'guard', 'body'];
|
|
|
|
While.prototype.isStatement = YES;
|
2010-11-11 21:48:08 -05:00
|
|
|
While.prototype.makeReturn = function() {
|
|
|
|
this.returns = true;
|
|
|
|
return this;
|
|
|
|
};
|
2010-11-20 20:39:35 -05:00
|
|
|
While.prototype.addBody = function(body) {
|
|
|
|
this.body = body;
|
2010-02-09 22:05:17 -05:00
|
|
|
return this;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-11-01 23:53:49 -04:00
|
|
|
While.prototype.containsPureStatement = function() {
|
2010-11-02 00:31:42 -04:00
|
|
|
var expressions, i, ret, _ref;
|
2010-11-01 23:53:49 -04:00
|
|
|
expressions = this.body.expressions;
|
|
|
|
i = expressions.length;
|
2010-11-02 00:31:42 -04:00
|
|
|
if ((_ref = expressions[--i]) != null ? _ref.containsPureStatement() : void 0) {
|
2010-11-01 23:53:49 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
ret = function(node) {
|
|
|
|
return node instanceof Return;
|
|
|
|
};
|
|
|
|
while (i--) {
|
|
|
|
if (expressions[i].contains(ret)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
2010-10-06 22:44:32 -04:00
|
|
|
While.prototype.compileNode = function(o) {
|
2010-10-23 05:06:04 -04:00
|
|
|
var body, code, rvar, set;
|
2010-11-08 23:20:11 -05:00
|
|
|
o.indent += TAB;
|
2010-02-09 22:05:17 -05:00
|
|
|
set = '';
|
2010-10-23 05:06:04 -04:00
|
|
|
body = this.body;
|
2010-10-24 12:54:36 -04:00
|
|
|
if (body.isEmpty()) {
|
|
|
|
body = '';
|
|
|
|
} else {
|
|
|
|
if (o.level > LEVEL_TOP || this.returns) {
|
2010-11-09 00:17:08 -05:00
|
|
|
rvar = o.scope.freeVariable('results');
|
2010-10-24 12:54:36 -04:00
|
|
|
set = "" + this.tab + rvar + " = [];\n";
|
|
|
|
if (body) {
|
|
|
|
body = Push.wrap(rvar, body);
|
|
|
|
}
|
2010-02-23 22:53:43 -05:00
|
|
|
}
|
2010-10-24 12:54:36 -04:00
|
|
|
if (this.guard) {
|
|
|
|
body = Expressions.wrap([new If(this.guard, body)]);
|
|
|
|
}
|
|
|
|
body = "\n" + (body.compile(o, LEVEL_TOP)) + "\n" + this.tab;
|
2010-02-09 22:05:17 -05:00
|
|
|
}
|
2010-10-24 12:54:36 -04:00
|
|
|
code = set + this.tab + ("while (" + (this.condition.compile(o, LEVEL_PAREN)) + ") {" + body + "}");
|
2010-08-11 00:40:15 -04:00
|
|
|
if (this.returns) {
|
2010-10-23 05:06:04 -04:00
|
|
|
o.indent = this.tab;
|
|
|
|
code += '\n' + new Return(new Literal(rvar)).compile(o);
|
2010-08-14 11:42:19 -04:00
|
|
|
}
|
2010-10-23 05:06:04 -04:00
|
|
|
return code;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-10-06 22:44:32 -04:00
|
|
|
return While;
|
2010-11-11 21:48:08 -05:00
|
|
|
}();
|
|
|
|
exports.Op = Op = function() {
|
2010-11-14 15:07:43 -05:00
|
|
|
var CONVERSIONS, INVERSIONS;
|
2010-12-06 01:01:57 -05:00
|
|
|
__extends(Op, Base);
|
2010-11-11 21:48:08 -05:00
|
|
|
function Op(op, first, second, flip) {
|
|
|
|
if (op === 'in') {
|
|
|
|
return new In(first, second);
|
|
|
|
}
|
|
|
|
if (op === 'new') {
|
|
|
|
if (first instanceof Call) {
|
|
|
|
return first.newInstance();
|
|
|
|
}
|
|
|
|
if (first instanceof Code && first.bound) {
|
|
|
|
first = new Parens(first);
|
2010-10-04 23:21:16 -04:00
|
|
|
}
|
2010-10-24 20:51:55 -04:00
|
|
|
}
|
2010-11-14 15:07:43 -05:00
|
|
|
this.operator = CONVERSIONS[op] || op;
|
2010-11-11 21:48:08 -05:00
|
|
|
this.first = first;
|
|
|
|
this.second = second;
|
|
|
|
this.flip = !!flip;
|
2010-11-20 17:46:44 -05:00
|
|
|
return this;
|
2010-11-11 21:48:08 -05:00
|
|
|
}
|
2010-11-14 15:07:43 -05:00
|
|
|
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-11-14 15:07:43 -05:00
|
|
|
INVERSIONS = {
|
2010-08-15 15:13:33 -04:00
|
|
|
'!==': '===',
|
2010-12-02 20:55:19 -05:00
|
|
|
'===': '!=='
|
2010-08-15 15:13:33 -04:00
|
|
|
};
|
2010-10-06 22:44:32 -04:00
|
|
|
Op.prototype.children = ['first', 'second'];
|
2010-12-20 23:41:58 -05:00
|
|
|
Op.prototype.isSimpleNumber = NO;
|
2010-10-06 22:44:32 -04:00
|
|
|
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.isChainable = function() {
|
2010-11-02 00:31:42 -04:00
|
|
|
var _ref;
|
|
|
|
return (_ref = this.operator) === '<' || _ref === '>' || _ref === '>=' || _ref === '<=' || _ref === '===' || _ref === '!==';
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-10-06 22:44:32 -04:00
|
|
|
Op.prototype.invert = function() {
|
2010-12-02 23:03:21 -05:00
|
|
|
var allInvertable, curr, fst, op, _ref;
|
2010-12-02 20:55:19 -05:00
|
|
|
if (this.isChainable() && this.first.isChainable()) {
|
2010-12-02 23:03:21 -05:00
|
|
|
allInvertable = true;
|
|
|
|
curr = this;
|
2010-12-03 01:23:54 -05:00
|
|
|
while (curr && curr.operator) {
|
2010-12-02 23:03:21 -05:00
|
|
|
allInvertable && (allInvertable = curr.operator in INVERSIONS);
|
|
|
|
curr = curr.first;
|
|
|
|
}
|
|
|
|
if (!allInvertable) {
|
|
|
|
return new Parens(this).invert();
|
|
|
|
}
|
|
|
|
curr = this;
|
2010-12-03 01:23:54 -05:00
|
|
|
while (curr && curr.operator) {
|
2010-12-03 00:07:30 -05:00
|
|
|
curr.invert = !curr.invert;
|
2010-12-02 23:03:21 -05:00
|
|
|
curr.operator = INVERSIONS[curr.operator];
|
|
|
|
curr = curr.first;
|
|
|
|
}
|
2010-12-02 20:55:19 -05:00
|
|
|
return this;
|
|
|
|
} else if (op = INVERSIONS[this.operator]) {
|
2010-10-20 06:53:41 -04:00
|
|
|
this.operator = op;
|
2010-12-02 23:03:21 -05:00
|
|
|
if (this.first.unwrap() instanceof Op) {
|
|
|
|
this.first.invert();
|
|
|
|
}
|
2010-10-10 18:29:38 -04:00
|
|
|
return this;
|
2010-11-08 23:07:51 -05:00
|
|
|
} else if (this.second) {
|
|
|
|
return new Parens(this).invert();
|
2010-11-14 15:07:43 -05:00
|
|
|
} else if (this.operator === '!' && (fst = this.first.unwrap()) instanceof Op && ((_ref = fst.operator) === '!' || _ref === 'in' || _ref === 'instanceof')) {
|
|
|
|
return fst;
|
2010-11-08 23:07:51 -05:00
|
|
|
} else {
|
2010-11-09 00:21:42 -05:00
|
|
|
return new Op('!', this);
|
2010-11-08 23:07:51 -05:00
|
|
|
}
|
2010-08-15 15:13:33 -04:00
|
|
|
};
|
2010-10-23 05:06:04 -04:00
|
|
|
Op.prototype.unfoldSoak = function(o) {
|
2010-11-02 00:31:42 -04:00
|
|
|
var _ref;
|
2010-11-11 21:48:08 -05:00
|
|
|
return ((_ref = this.operator) === '++' || _ref === '--' || _ref === 'delete') && unfoldSoak(o, this, 'first');
|
2010-10-23 05:06:04 -04:00
|
|
|
};
|
2010-10-06 22:44:32 -04:00
|
|
|
Op.prototype.compileNode = function(o) {
|
2010-11-13 18:27:05 -05:00
|
|
|
var code;
|
2010-06-12 19:05:13 -04:00
|
|
|
if (this.isUnary()) {
|
|
|
|
return this.compileUnary(o);
|
2010-02-10 00:05:56 -05:00
|
|
|
}
|
2010-10-23 10:51:22 -04:00
|
|
|
if (this.isChainable() && this.first.isChainable()) {
|
2010-10-19 22:04:38 -04:00
|
|
|
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-11-01 23:25:28 -04:00
|
|
|
this.first.front = this.front;
|
2010-11-13 18:27:05 -05:00
|
|
|
code = this.first.compile(o, LEVEL_OP) + ' ' + this.operator + ' ' + this.second.compile(o, LEVEL_OP);
|
|
|
|
if (o.level <= LEVEL_OP) {
|
|
|
|
return code;
|
|
|
|
} else {
|
|
|
|
return "(" + code + ")";
|
|
|
|
}
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-10-06 22:44:32 -04:00
|
|
|
Op.prototype.compileChain = function(o) {
|
2010-11-02 00:31:42 -04:00
|
|
|
var code, fst, shared, _ref;
|
|
|
|
_ref = this.first.second.cache(o), this.first.second = _ref[0], shared = _ref[1];
|
2010-10-23 11:11:49 -04:00
|
|
|
fst = this.first.compile(o, LEVEL_OP);
|
2010-12-03 00:07:30 -05:00
|
|
|
code = "" + fst + " " + (this.invert ? '&&' : '||') + " " + (shared.compile(o)) + " " + this.operator + " " + (this.second.compile(o, LEVEL_OP));
|
2010-12-02 23:03:21 -05:00
|
|
|
return "(" + code + ")";
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-10-06 22:44:32 -04:00
|
|
|
Op.prototype.compileExistence = function(o) {
|
2010-11-01 22:32:04 -04:00
|
|
|
var fst, ref;
|
|
|
|
if (this.first.isComplex()) {
|
2010-10-01 18:17:35 -04:00
|
|
|
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-11-01 22:32:04 -04:00
|
|
|
return new Existence(fst).compile(o) + (" ? " + ref + " : " + (this.second.compile(o, LEVEL_LIST)));
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-10-06 22:44:32 -04:00
|
|
|
Op.prototype.compileUnary = function(o) {
|
2010-10-23 10:51:22 -04:00
|
|
|
var op, parts;
|
|
|
|
parts = [op = this.operator];
|
|
|
|
if ((op === 'new' || op === 'typeof' || op === 'delete') || (op === '+' || op === '-') && this.first instanceof Op && this.first.operator === op) {
|
|
|
|
parts.push(' ');
|
|
|
|
}
|
2010-10-23 11:11:49 -04:00
|
|
|
parts.push(this.first.compile(o, LEVEL_OP));
|
2010-10-23 10:51:22 -04:00
|
|
|
if (this.flip) {
|
|
|
|
parts.reverse();
|
|
|
|
}
|
|
|
|
return parts.join('');
|
|
|
|
};
|
|
|
|
Op.prototype.toString = function(idt) {
|
|
|
|
return Op.__super__.toString.call(this, idt, this.constructor.name + ' ' + this.operator);
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-10-06 22:44:32 -04:00
|
|
|
return Op;
|
2010-11-11 21:48:08 -05:00
|
|
|
}();
|
|
|
|
exports.In = In = function() {
|
2010-12-06 01:01:57 -05:00
|
|
|
__extends(In, Base);
|
2010-11-20 20:39:35 -05:00
|
|
|
function In(object, array) {
|
|
|
|
this.object = object;
|
|
|
|
this.array = array;
|
2010-11-11 21:48:08 -05:00
|
|
|
}
|
2010-10-06 22:44:32 -04:00
|
|
|
In.prototype.children = ['object', 'array'];
|
2010-11-08 23:48:38 -05:00
|
|
|
In.prototype.invert = NEGATE;
|
2010-10-06 22:44:32 -04:00
|
|
|
In.prototype.compileNode = function(o) {
|
2010-11-08 23:07:51 -05:00
|
|
|
if (this.array instanceof Value && this.array.isArray()) {
|
|
|
|
return this.compileOrTest(o);
|
|
|
|
} else {
|
|
|
|
return 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-12-13 21:24:32 -05:00
|
|
|
var cmp, cnj, i, item, ref, sub, tests, _ref, _ref2;
|
2010-11-02 00:31:42 -04:00
|
|
|
_ref = this.object.cache(o, LEVEL_OP), sub = _ref[0], ref = _ref[1];
|
|
|
|
_ref2 = this.negated ? [' !== ', ' && '] : [' === ', ' || '], cmp = _ref2[0], cnj = _ref2[1];
|
2010-11-21 19:59:22 -05:00
|
|
|
tests = function() {
|
2010-12-13 21:24:32 -05:00
|
|
|
var _len, _ref, _results;
|
|
|
|
_ref = this.array.base.objects;
|
2010-11-09 00:17:08 -05:00
|
|
|
_results = [];
|
2010-12-13 21:24:32 -05:00
|
|
|
for (i = 0, _len = _ref.length; i < _len; i++) {
|
|
|
|
item = _ref[i];
|
2010-11-09 00:17:08 -05:00
|
|
|
_results.push((i ? ref : sub) + cmp + item.compile(o, LEVEL_OP));
|
2010-06-21 23:51:12 -04:00
|
|
|
}
|
2010-11-09 00:17:08 -05:00
|
|
|
return _results;
|
2010-11-21 19:59:22 -05:00
|
|
|
}.call(this);
|
2010-10-20 20:16:17 -04:00
|
|
|
tests = tests.join(cnj);
|
2010-11-08 23:07:51 -05:00
|
|
|
if (o.level < LEVEL_OP) {
|
|
|
|
return tests;
|
|
|
|
} else {
|
|
|
|
return "(" + tests + ")";
|
|
|
|
}
|
2010-06-21 23:51:12 -04:00
|
|
|
};
|
2010-10-06 22:44:32 -04:00
|
|
|
In.prototype.compileLoopTest = function(o) {
|
2010-11-02 00:31:42 -04:00
|
|
|
var code, ref, sub, _ref;
|
|
|
|
_ref = this.object.cache(o, LEVEL_LIST), sub = _ref[0], ref = _ref[1];
|
2010-11-02 10:27:04 -04:00
|
|
|
code = utility('indexOf') + (".call(" + (this.array.compile(o, LEVEL_LIST)) + ", " + ref + ") ") + (this.negated ? '< 0' : '>= 0');
|
2010-10-22 21:44:03 -04:00
|
|
|
if (sub === ref) {
|
|
|
|
return code;
|
|
|
|
}
|
|
|
|
code = sub + ', ' + code;
|
2010-11-08 23:07:51 -05:00
|
|
|
if (o.level < LEVEL_LIST) {
|
|
|
|
return code;
|
|
|
|
} else {
|
|
|
|
return "(" + 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-11-11 21:48:08 -05:00
|
|
|
}();
|
|
|
|
exports.Try = Try = function() {
|
2010-12-06 01:01:57 -05:00
|
|
|
__extends(Try, Base);
|
2010-11-20 20:39:35 -05:00
|
|
|
function Try(attempt, error, recovery, ensure) {
|
|
|
|
this.attempt = attempt;
|
|
|
|
this.error = error;
|
|
|
|
this.recovery = recovery;
|
|
|
|
this.ensure = ensure;
|
2010-11-11 21:48:08 -05:00
|
|
|
}
|
2010-10-06 22:44:32 -04:00
|
|
|
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-10-23 05:06:04 -04:00
|
|
|
var catchPart, errorPart;
|
2010-11-08 23:20:11 -05:00
|
|
|
o.indent += TAB;
|
2010-10-20 13:29:06 -04:00
|
|
|
errorPart = this.error ? " (" + (this.error.compile(o)) + ") " : ' ';
|
2010-10-27 22:50:20 -04:00
|
|
|
catchPart = this.recovery ? " catch" + errorPart + "{\n" + (this.recovery.compile(o, LEVEL_TOP)) + "\n" + this.tab + "}" : !(this.ensure || this.recovery) ? ' catch (_e) {}' : void 0;
|
2010-10-23 11:10:28 -04:00
|
|
|
return ("" + this.tab + "try {\n" + (this.attempt.compile(o, LEVEL_TOP)) + "\n" + this.tab + "}" + (catchPart || '')) + (this.ensure ? " finally {\n" + (this.ensure.compile(o, LEVEL_TOP)) + "\n" + this.tab + "}" : '');
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-10-06 22:44:32 -04:00
|
|
|
return Try;
|
2010-11-11 21:48:08 -05:00
|
|
|
}();
|
|
|
|
exports.Throw = Throw = function() {
|
2010-12-06 01:01:57 -05:00
|
|
|
__extends(Throw, Base);
|
2010-11-20 20:39:35 -05:00
|
|
|
function Throw(expression) {
|
|
|
|
this.expression = expression;
|
2010-11-11 21:48:08 -05:00
|
|
|
}
|
2010-10-06 22:44:32 -04:00
|
|
|
Throw.prototype.children = ['expression'];
|
|
|
|
Throw.prototype.isStatement = YES;
|
|
|
|
Throw.prototype.makeReturn = THIS;
|
|
|
|
Throw.prototype.compileNode = function(o) {
|
2010-10-23 05:06:04 -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-11-11 21:48:08 -05:00
|
|
|
}();
|
|
|
|
exports.Existence = Existence = function() {
|
2010-12-06 01:01:57 -05:00
|
|
|
__extends(Existence, Base);
|
2010-11-20 20:39:35 -05:00
|
|
|
function Existence(expression) {
|
|
|
|
this.expression = expression;
|
2010-11-11 21:48:08 -05:00
|
|
|
}
|
2010-10-06 22:44:32 -04:00
|
|
|
Existence.prototype.children = ['expression'];
|
2010-11-08 23:48:38 -05:00
|
|
|
Existence.prototype.invert = NEGATE;
|
2010-10-06 22:44:32 -04:00
|
|
|
Existence.prototype.compileNode = function(o) {
|
2010-11-08 22:47:13 -05:00
|
|
|
var code, sym;
|
2010-11-09 02:27:21 -05:00
|
|
|
code = this.expression.compile(o, LEVEL_OP);
|
2010-11-08 23:07:51 -05:00
|
|
|
code = IDENTIFIER.test(code) && !o.scope.check(code) ? this.negated ? "typeof " + code + " == \"undefined\" || " + code + " === null" : "typeof " + code + " != \"undefined\" && " + code + " !== null" : (sym = this.negated ? '==' : '!=', "" + code + " " + sym + " null");
|
|
|
|
if (o.level <= LEVEL_COND) {
|
|
|
|
return code;
|
|
|
|
} else {
|
|
|
|
return "(" + code + ")";
|
|
|
|
}
|
2010-03-29 21:49:20 -04:00
|
|
|
};
|
2010-10-06 22:44:32 -04:00
|
|
|
return Existence;
|
2010-11-11 21:48:08 -05:00
|
|
|
}();
|
|
|
|
exports.Parens = Parens = function() {
|
2010-12-06 01:01:57 -05:00
|
|
|
__extends(Parens, Base);
|
2010-11-20 20:39:35 -05:00
|
|
|
function Parens(body) {
|
|
|
|
this.body = body;
|
2010-11-11 21:48:08 -05:00
|
|
|
}
|
2010-11-20 20:22:05 -05:00
|
|
|
Parens.prototype.children = ['body'];
|
2010-10-23 05:06:04 -04:00
|
|
|
Parens.prototype.unwrap = function() {
|
2010-11-20 20:22:05 -05:00
|
|
|
return this.body;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-10-06 23:53:26 -04:00
|
|
|
Parens.prototype.isComplex = function() {
|
2010-11-20 20:22:05 -05:00
|
|
|
return this.body.isComplex();
|
2010-09-27 04:56:56 -04:00
|
|
|
};
|
2010-10-06 23:53:26 -04:00
|
|
|
Parens.prototype.makeReturn = function() {
|
2010-11-20 20:22:05 -05:00
|
|
|
return this.body.makeReturn();
|
2010-03-21 07:17:58 -04:00
|
|
|
};
|
2010-10-06 23:53:26 -04:00
|
|
|
Parens.prototype.compileNode = function(o) {
|
2010-10-23 05:06:04 -04:00
|
|
|
var bare, code, expr;
|
2010-11-20 20:22:05 -05:00
|
|
|
expr = this.body.unwrap();
|
2010-10-21 19:50:36 -04:00
|
|
|
if (expr instanceof Value && expr.isAtomic()) {
|
2010-11-01 23:25:28 -04:00
|
|
|
expr.front = this.front;
|
2010-10-21 19:50:36 -04:00
|
|
|
return expr.compile(o);
|
2010-08-21 08:30:22 -04:00
|
|
|
}
|
2010-10-23 11:10:28 -04:00
|
|
|
bare = o.level < LEVEL_OP && (expr instanceof Op || expr instanceof Call);
|
|
|
|
code = expr.compile(o, LEVEL_PAREN);
|
2010-11-08 23:07:51 -05:00
|
|
|
if (bare) {
|
|
|
|
return code;
|
|
|
|
} else {
|
|
|
|
return "(" + code + ")";
|
|
|
|
}
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-10-06 23:53:26 -04:00
|
|
|
return Parens;
|
2010-11-11 21:48:08 -05:00
|
|
|
}();
|
|
|
|
exports.For = For = function() {
|
2010-12-06 01:01:57 -05:00
|
|
|
__extends(For, Base);
|
2010-11-20 20:39:35 -05:00
|
|
|
function For(body, source, name, index) {
|
2010-11-20 16:25:22 -05:00
|
|
|
var _ref;
|
2010-11-20 20:39:35 -05:00
|
|
|
this.name = name;
|
|
|
|
this.index = index;
|
2010-11-20 16:25:22 -05:00
|
|
|
this.source = source.source, this.guard = source.guard, this.step = source.step;
|
|
|
|
this.body = Expressions.wrap([body]);
|
2010-11-28 18:33:43 -05:00
|
|
|
this.own = !!source.own;
|
2010-11-20 16:25:22 -05:00
|
|
|
this.object = !!source.object;
|
|
|
|
if (this.object) {
|
|
|
|
_ref = [this.index, this.name], this.name = _ref[0], this.index = _ref[1];
|
|
|
|
}
|
|
|
|
if (this.index instanceof Value) {
|
2010-11-11 21:48:08 -05:00
|
|
|
throw SyntaxError('index cannot be a pattern matching expression');
|
|
|
|
}
|
2010-11-20 16:25:22 -05:00
|
|
|
this.range = this.source instanceof Value && this.source.base instanceof Range && !this.source.properties.length;
|
2010-11-11 21:48:08 -05:00
|
|
|
this.pattern = this.name instanceof Value;
|
2010-11-28 18:54:39 -05:00
|
|
|
if (this.range && this.index) {
|
|
|
|
throw SyntaxError('indexes do not apply to range loops');
|
|
|
|
}
|
2010-11-20 16:25:22 -05:00
|
|
|
if (this.range && this.pattern) {
|
2010-11-28 18:54:39 -05:00
|
|
|
throw SyntaxError('cannot pattern match over range loops');
|
2010-11-20 16:25:22 -05:00
|
|
|
}
|
2010-11-11 21:48:08 -05:00
|
|
|
this.returns = false;
|
|
|
|
}
|
2010-11-20 16:25:22 -05:00
|
|
|
For.prototype.children = ['body', 'source', 'guard', 'step'];
|
2010-10-20 06:53:41 -04:00
|
|
|
For.prototype.isStatement = YES;
|
2010-11-11 21:48:08 -05:00
|
|
|
For.prototype.makeReturn = function() {
|
|
|
|
this.returns = true;
|
|
|
|
return this;
|
|
|
|
};
|
2010-11-01 23:53:49 -04:00
|
|
|
For.prototype.containsPureStatement = While.prototype.containsPureStatement;
|
2010-10-06 22:44:32 -04:00
|
|
|
For.prototype.compileNode = function(o) {
|
2010-12-21 15:45:46 -05:00
|
|
|
var body, defPart, forPart, guardPart, hasPure, idt1, index, ivar, lvar, name, namePart, ref, resultPart, returnResult, rvar, scope, source, stepPart, svar, varPart, _ref;
|
2010-11-20 19:12:39 -05:00
|
|
|
body = Expressions.wrap([this.body]);
|
|
|
|
hasPure = (_ref = last(body.expressions)) != null ? _ref.containsPureStatement() : void 0;
|
2010-11-20 16:25:22 -05:00
|
|
|
source = this.range ? this.source.base : this.source;
|
2010-02-10 20:19:59 -05:00
|
|
|
scope = o.scope;
|
2010-11-20 16:25:22 -05:00
|
|
|
name = this.name && this.name.compile(o, LEVEL_LIST);
|
|
|
|
index = this.index && this.index.compile(o, LEVEL_LIST);
|
2010-12-05 21:18:30 -05:00
|
|
|
if (name && !this.pattern) {
|
|
|
|
scope.find(name, {
|
|
|
|
immediate: true
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (index) {
|
|
|
|
scope.find(index, {
|
|
|
|
immediate: true
|
|
|
|
});
|
2010-10-19 09:51:52 -04:00
|
|
|
}
|
2010-11-20 19:12:39 -05:00
|
|
|
if (this.returns && !hasPure) {
|
2010-11-20 16:25:22 -05:00
|
|
|
rvar = scope.freeVariable('results');
|
|
|
|
}
|
|
|
|
ivar = (this.range ? name : index) || scope.freeVariable('i');
|
|
|
|
varPart = '';
|
|
|
|
guardPart = '';
|
2010-11-20 18:14:05 -05:00
|
|
|
defPart = '';
|
2010-11-20 16:25:22 -05:00
|
|
|
idt1 = this.tab + TAB;
|
|
|
|
if (this.range) {
|
|
|
|
forPart = source.compile(merge(o, {
|
|
|
|
index: ivar,
|
|
|
|
step: this.step
|
|
|
|
}));
|
2010-02-10 20:19:59 -05:00
|
|
|
} else {
|
2010-12-09 21:12:24 -05:00
|
|
|
svar = this.source.compile(o, LEVEL_LIST);
|
2010-11-28 18:33:43 -05:00
|
|
|
if ((name || this.own) && !IDENTIFIER.test(svar)) {
|
2010-11-20 18:14:05 -05:00
|
|
|
defPart = "" + this.tab + (ref = scope.freeVariable('ref')) + " = " + svar + ";\n";
|
2010-11-20 16:25:22 -05:00
|
|
|
svar = ref;
|
2010-10-06 23:24:32 -04:00
|
|
|
}
|
2010-10-27 22:50:20 -04:00
|
|
|
namePart = this.pattern ? new Assign(this.name, new Literal("" + svar + "[" + ivar + "]")).compile(o, LEVEL_TOP) : name ? "" + name + " = " + svar + "[" + ivar + "]" : void 0;
|
2010-10-07 07:05:22 -04:00
|
|
|
if (!this.object) {
|
2010-11-20 16:25:22 -05:00
|
|
|
lvar = scope.freeVariable('len');
|
|
|
|
stepPart = this.step ? "" + ivar + " += " + (this.step.compile(o, LEVEL_OP)) : "" + ivar + "++";
|
|
|
|
forPart = "" + ivar + " = 0, " + lvar + " = " + svar + ".length; " + ivar + " < " + lvar + "; " + stepPart;
|
2010-02-25 23:39:14 -05:00
|
|
|
}
|
2010-02-10 20:19:59 -05:00
|
|
|
}
|
2010-11-20 19:12:39 -05:00
|
|
|
if (this.returns && !hasPure) {
|
2010-11-20 16:25:22 -05:00
|
|
|
resultPart = "" + this.tab + rvar + " = [];\n";
|
|
|
|
returnResult = '\n' + (new Return(new Literal(rvar)).compile(o, LEVEL_PAREN));
|
2010-11-20 19:12:39 -05:00
|
|
|
body = Push.wrap(rvar, body);
|
2010-10-13 07:29:22 -04:00
|
|
|
}
|
2010-11-20 16:25:22 -05:00
|
|
|
if (this.guard) {
|
|
|
|
body = Expressions.wrap([new If(this.guard, body)]);
|
2010-11-03 22:05:24 -04:00
|
|
|
}
|
2010-10-13 07:29:22 -04:00
|
|
|
if (namePart) {
|
2010-11-20 18:37:19 -05:00
|
|
|
varPart = "\n" + idt1 + namePart + ";";
|
2010-07-16 22:31:36 -04:00
|
|
|
}
|
2010-11-20 16:25:22 -05:00
|
|
|
if (this.object) {
|
|
|
|
forPart = "" + ivar + " in " + svar;
|
2010-11-28 18:33:43 -05:00
|
|
|
if (this.own) {
|
2010-11-20 16:25:22 -05:00
|
|
|
guardPart = "\n" + idt1 + "if (!" + (utility('hasProp')) + ".call(" + svar + ", " + ivar + ")) continue;";
|
2010-10-24 12:54:36 -04:00
|
|
|
}
|
|
|
|
}
|
2010-11-20 18:14:05 -05:00
|
|
|
if (!this.pattern) {
|
|
|
|
defPart += this.pluckDirectCall(o, body, name, index);
|
|
|
|
}
|
2010-11-20 16:25:22 -05:00
|
|
|
body = body.compile(merge(o, {
|
2010-11-20 18:14:05 -05:00
|
|
|
indent: idt1
|
|
|
|
}), LEVEL_TOP);
|
2010-11-20 18:37:19 -05:00
|
|
|
if (body) {
|
|
|
|
body = '\n' + body + '\n';
|
|
|
|
}
|
|
|
|
return "" + defPart + (resultPart || '') + this.tab + "for (" + forPart + ") {" + guardPart + varPart + body + this.tab + "}" + (returnResult || '');
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-11-03 21:39:54 -04:00
|
|
|
For.prototype.pluckDirectCall = function(o, body, name, index) {
|
2010-11-03 22:51:17 -04:00
|
|
|
var arg, args, base, defs, expr, fn, i, idx, ref, val, _len, _len2, _ref, _ref2, _ref3, _ref4, _ref5, _ref6;
|
2010-11-03 21:39:54 -04:00
|
|
|
defs = '';
|
|
|
|
_ref = body.expressions;
|
|
|
|
for (idx = 0, _len = _ref.length; idx < _len; idx++) {
|
|
|
|
expr = _ref[idx];
|
|
|
|
expr = expr.unwrapAll();
|
|
|
|
if (!(expr instanceof Call)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
val = expr.variable.unwrapAll();
|
2010-11-03 22:51:17 -04:00
|
|
|
if (!((val instanceof Code) || (val instanceof Value && ((_ref2 = val.base) != null ? _ref2.unwrapAll() : void 0) instanceof Code && val.properties.length === 1 && ((_ref3 = (_ref4 = val.properties[0].name) != null ? _ref4.value : void 0) === 'call' || _ref3 === 'apply')))) {
|
2010-11-03 21:39:54 -04:00
|
|
|
continue;
|
|
|
|
}
|
2010-11-03 22:51:17 -04:00
|
|
|
fn = ((_ref5 = val.base) != null ? _ref5.unwrapAll() : void 0) || val;
|
2010-11-03 21:39:54 -04:00
|
|
|
ref = new Literal(o.scope.freeVariable('fn'));
|
|
|
|
base = new Value(ref);
|
|
|
|
args = compact([name, index]);
|
|
|
|
if (this.object) {
|
|
|
|
args.reverse();
|
|
|
|
}
|
|
|
|
for (i = 0, _len2 = args.length; i < _len2; i++) {
|
|
|
|
arg = args[i];
|
|
|
|
fn.params.push(new Param(args[i] = new Literal(arg)));
|
|
|
|
}
|
|
|
|
if (val.base) {
|
2010-11-03 22:51:17 -04:00
|
|
|
_ref6 = [base, val], val.base = _ref6[0], base = _ref6[1];
|
2010-11-03 21:39:54 -04:00
|
|
|
args.unshift(new Literal('this'));
|
|
|
|
}
|
|
|
|
body.expressions[idx] = new Call(base, args);
|
|
|
|
defs += this.tab + new Assign(ref, fn).compile(o, LEVEL_TOP) + ';\n';
|
|
|
|
}
|
|
|
|
return defs;
|
|
|
|
};
|
2010-10-06 22:44:32 -04:00
|
|
|
return For;
|
2010-11-11 21:48:08 -05:00
|
|
|
}();
|
|
|
|
exports.Switch = Switch = function() {
|
2010-12-06 01:01:57 -05:00
|
|
|
__extends(Switch, Base);
|
2010-11-20 20:39:35 -05:00
|
|
|
function Switch(subject, cases, otherwise) {
|
|
|
|
this.subject = subject;
|
|
|
|
this.cases = cases;
|
|
|
|
this.otherwise = otherwise;
|
2010-11-11 21:48:08 -05:00
|
|
|
}
|
2010-10-06 22:44:32 -04:00
|
|
|
Switch.prototype.children = ['subject', 'cases', 'otherwise'];
|
|
|
|
Switch.prototype.isStatement = YES;
|
|
|
|
Switch.prototype.makeReturn = function() {
|
2010-11-02 00:31:42 -04:00
|
|
|
var pair, _i, _len, _ref, _ref2;
|
|
|
|
_ref = this.cases;
|
|
|
|
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
|
|
|
pair = _ref[_i];
|
2010-09-15 23:46:01 -04:00
|
|
|
pair[1].makeReturn();
|
|
|
|
}
|
2010-11-02 00:31:42 -04:00
|
|
|
if ((_ref2 = this.otherwise) != null) {
|
|
|
|
_ref2.makeReturn();
|
2010-09-15 23:46:01 -04:00
|
|
|
}
|
|
|
|
return this;
|
|
|
|
};
|
2010-10-06 22:44:32 -04:00
|
|
|
Switch.prototype.compileNode = function(o) {
|
2010-12-16 08:23:34 -05:00
|
|
|
var block, body, code, cond, conditions, expr, i, idt1, idt2, _i, _len, _len2, _ref, _ref2, _ref3, _ref4;
|
2010-11-08 23:20:11 -05:00
|
|
|
idt1 = o.indent + TAB;
|
|
|
|
idt2 = o.indent = idt1 + TAB;
|
2010-11-02 00:31:42 -04:00
|
|
|
code = this.tab + ("switch (" + (((_ref = this.subject) != null ? _ref.compile(o, LEVEL_PAREN) : void 0) || false) + ") {\n");
|
2010-11-20 16:25:22 -05:00
|
|
|
_ref2 = this.cases;
|
|
|
|
for (i = 0, _len = _ref2.length; i < _len; i++) {
|
|
|
|
_ref3 = _ref2[i], conditions = _ref3[0], block = _ref3[1];
|
|
|
|
_ref4 = flatten([conditions]);
|
|
|
|
for (_i = 0, _len2 = _ref4.length; _i < _len2; _i++) {
|
|
|
|
cond = _ref4[_i];
|
2010-10-20 06:53:41 -04:00
|
|
|
if (!this.subject) {
|
2010-10-24 17:20:45 -04:00
|
|
|
cond = cond.invert();
|
2010-09-15 23:46:01 -04:00
|
|
|
}
|
2010-10-23 11:10:28 -04:00
|
|
|
code += idt1 + ("case " + (cond.compile(o, LEVEL_PAREN)) + ":\n");
|
2010-09-15 23:46:01 -04:00
|
|
|
}
|
2010-10-25 09:19:02 -04:00
|
|
|
if (body = block.compile(o, LEVEL_TOP)) {
|
|
|
|
code += body + '\n';
|
|
|
|
}
|
2010-10-24 21:30:26 -04:00
|
|
|
if (i === this.cases.length - 1 && !this.otherwise) {
|
|
|
|
break;
|
|
|
|
}
|
2010-12-16 11:35:51 -05:00
|
|
|
expr = this.lastNonComment(block.expressions);
|
|
|
|
if (!expr || !expr.isPureStatement()) {
|
2010-12-15 23:38:27 -05:00
|
|
|
code += idt2 + 'break;\n';
|
2010-09-15 23:46:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (this.otherwise) {
|
2010-10-24 21:30:26 -04:00
|
|
|
code += idt1 + ("default:\n" + (this.otherwise.compile(o, LEVEL_TOP)) + "\n");
|
2010-09-15 23:46:01 -04:00
|
|
|
}
|
2010-10-23 05:06:04 -04:00
|
|
|
return code + this.tab + '}';
|
2010-09-15 23:46:01 -04:00
|
|
|
};
|
2010-10-06 22:44:32 -04:00
|
|
|
return Switch;
|
2010-11-11 21:48:08 -05:00
|
|
|
}();
|
|
|
|
exports.If = If = function() {
|
2010-12-06 01:01:57 -05:00
|
|
|
__extends(If, Base);
|
2010-11-20 20:39:35 -05:00
|
|
|
function If(condition, body, options) {
|
|
|
|
this.body = body;
|
2010-11-21 21:12:59 -05:00
|
|
|
if (options == null) {
|
|
|
|
options = {};
|
|
|
|
}
|
2010-12-20 22:50:49 -05:00
|
|
|
this.condition = options.type === 'unless' ? condition.invert() : condition;
|
2010-11-11 21:48:08 -05:00
|
|
|
this.elseBody = null;
|
|
|
|
this.isChain = false;
|
|
|
|
this.soak = options.soak;
|
|
|
|
}
|
2010-10-20 13:29:06 -04:00
|
|
|
If.prototype.children = ['condition', 'body', 'elseBody'];
|
2010-10-06 22:44:32 -04:00
|
|
|
If.prototype.bodyNode = function() {
|
2010-11-02 00:31:42 -04:00
|
|
|
var _ref;
|
|
|
|
return (_ref = this.body) != null ? _ref.unwrap() : void 0;
|
2010-04-27 09:32:45 -04:00
|
|
|
};
|
2010-10-06 22:44:32 -04:00
|
|
|
If.prototype.elseBodyNode = function() {
|
2010-11-02 00:31:42 -04:00
|
|
|
var _ref;
|
|
|
|
return (_ref = this.elseBody) != null ? _ref.unwrap() : void 0;
|
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-11-02 00:31:42 -04:00
|
|
|
var _ref;
|
|
|
|
return (o != null ? o.level : void 0) === LEVEL_TOP || this.bodyNode().isStatement(o) || ((_ref = this.elseBodyNode()) != null ? _ref.isStatement(o) : void 0);
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-10-06 22:44:32 -04:00
|
|
|
If.prototype.compileNode = function(o) {
|
2010-11-08 23:07:51 -05:00
|
|
|
if (this.isStatement(o)) {
|
|
|
|
return this.compileStatement(o);
|
2010-03-21 07:17:58 -04:00
|
|
|
} else {
|
2010-11-08 23:07:51 -05:00
|
|
|
return this.compileExpression(o);
|
2010-03-21 07:17:58 -04:00
|
|
|
}
|
|
|
|
};
|
2010-11-08 23:07:51 -05:00
|
|
|
If.prototype.makeReturn = function() {
|
|
|
|
this.body && (this.body = new Expressions([this.body.makeReturn()]));
|
|
|
|
this.elseBody && (this.elseBody = new Expressions([this.elseBody.makeReturn()]));
|
|
|
|
return this;
|
|
|
|
};
|
2010-10-06 22:44:32 -04:00
|
|
|
If.prototype.ensureExpressions = function(node) {
|
2010-11-08 23:07:51 -05:00
|
|
|
if (node instanceof Expressions) {
|
|
|
|
return node;
|
|
|
|
} else {
|
|
|
|
return 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-23 05:06:04 -04:00
|
|
|
var body, child, cond, ifPart;
|
2010-06-12 19:05:13 -04:00
|
|
|
child = del(o, 'chainChild');
|
2010-10-23 11:10:28 -04:00
|
|
|
cond = this.condition.compile(o, LEVEL_PAREN);
|
2010-11-08 23:20:11 -05:00
|
|
|
o.indent += TAB;
|
2010-10-23 05:06:04 -04:00
|
|
|
body = this.ensureExpressions(this.body).compile(o);
|
2010-10-25 09:19:02 -04:00
|
|
|
if (body) {
|
|
|
|
body = "\n" + body + "\n" + this.tab;
|
|
|
|
}
|
|
|
|
ifPart = "if (" + cond + ") {" + body + "}";
|
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-11-08 23:07:51 -05:00
|
|
|
return ifPart + ' else ' + (this.isChain ? (o.indent = this.tab, o.chainChild = true, this.elseBody.unwrap().compile(o, LEVEL_TOP)) : "{\n" + (this.elseBody.compile(o, LEVEL_TOP)) + "\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-27 22:50:20 -04:00
|
|
|
var alt, body, code, cond;
|
|
|
|
cond = this.condition.compile(o, LEVEL_COND);
|
|
|
|
body = this.bodyNode().compile(o, LEVEL_LIST);
|
|
|
|
alt = this.elseBodyNode() ? this.elseBodyNode().compile(o, LEVEL_LIST) : 'void 0';
|
|
|
|
code = "" + cond + " ? " + body + " : " + alt;
|
2010-11-08 23:07:51 -05:00
|
|
|
if (o.level >= LEVEL_COND) {
|
|
|
|
return "(" + code + ")";
|
|
|
|
} else {
|
|
|
|
return code;
|
|
|
|
}
|
2010-10-17 00:19:51 -04:00
|
|
|
};
|
|
|
|
If.prototype.unfoldSoak = function() {
|
2010-10-25 09:31:52 -04:00
|
|
|
return this.soak && this;
|
2010-02-27 19:19:53 -05:00
|
|
|
};
|
2010-10-06 22:44:32 -04:00
|
|
|
return If;
|
2010-11-11 21:48:08 -05:00
|
|
|
}();
|
2010-10-06 22:44:32 -04:00
|
|
|
Push = {
|
2010-10-24 19:55:42 -04:00
|
|
|
wrap: function(name, exps) {
|
|
|
|
if (exps.isEmpty() || last(exps.expressions).containsPureStatement()) {
|
|
|
|
return exps;
|
2010-03-09 23:04:16 -05:00
|
|
|
}
|
2010-11-20 20:09:36 -05:00
|
|
|
return exps.push(new Call(new Value(new Literal(name), [new Access(new Literal('push'))]), [exps.pop()]));
|
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-23 09:15:01 -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-11-21 19:59:22 -05:00
|
|
|
func = new Code([], Expressions.wrap([expressions]));
|
2010-04-10 14:40:05 -04:00
|
|
|
args = [];
|
2010-10-23 09:15:01 -04:00
|
|
|
if ((mentionsArgs = expressions.contains(this.literalArgs)) || (expressions.contains(this.literalThis))) {
|
|
|
|
meth = new Literal(mentionsArgs ? 'apply' : 'call');
|
2010-10-06 23:41:09 -04:00
|
|
|
args = [new Literal('this')];
|
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-11-20 20:09:36 -05:00
|
|
|
func = new Value(func, [new Access(meth)]);
|
2010-04-10 14:20:32 -04:00
|
|
|
}
|
2010-12-09 22:16:32 -05:00
|
|
|
func.noReturn = noReturn;
|
2010-10-06 22:44:32 -04:00
|
|
|
call = new Call(func, args);
|
2010-11-08 23:07:51 -05:00
|
|
|
if (statement) {
|
|
|
|
return Expressions.wrap([call]);
|
|
|
|
} else {
|
|
|
|
return 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-11-11 21:48:08 -05:00
|
|
|
unfoldSoak = function(o, parent, name) {
|
|
|
|
var ifn;
|
|
|
|
if (!(ifn = parent[name].unfoldSoak(o))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
parent[name] = ifn.body;
|
|
|
|
ifn.body = new Value(parent);
|
|
|
|
return ifn;
|
|
|
|
};
|
2010-03-30 19:42:09 -04:00
|
|
|
UTILITIES = {
|
2010-11-20 19:17:09 -05:00
|
|
|
"extends": 'function(child, parent) {\n for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; }\n function ctor() { this.constructor = child; }\n ctor.prototype = parent.prototype;\n child.prototype = new ctor;\n child.__super__ = parent.prototype;\n return child;\n}',
|
2010-11-13 18:08:02 -05:00
|
|
|
bind: 'function(fn, me){ return function(){ return fn.apply(me, arguments); }; }',
|
2010-10-24 20:51:55 -04:00
|
|
|
indexOf: 'Array.prototype.indexOf || function(item) {\n for (var i = 0, l = this.length; i < l; i++) {\n if (this[i] === item) return i;\n }\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-11-13 21:08:42 -05:00
|
|
|
LEVEL_TOP = 1;
|
|
|
|
LEVEL_PAREN = 2;
|
|
|
|
LEVEL_LIST = 3;
|
|
|
|
LEVEL_COND = 4;
|
|
|
|
LEVEL_OP = 5;
|
|
|
|
LEVEL_ACCESS = 6;
|
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-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-10-23 05:06:04 -04:00
|
|
|
multident = function(code, tab) {
|
|
|
|
return code.replace(/\n/g, '$&' + tab);
|
|
|
|
};
|
2010-09-21 03:53:58 -04:00
|
|
|
}).call(this);
|