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