normalizing loop declarations towards common JS practice.

This commit is contained in:
Jeremy Ashkenas
2010-10-21 21:51:06 -04:00
parent 0342b0a89f
commit c92fd79f35
11 changed files with 126 additions and 89 deletions

View File

@@ -58,7 +58,7 @@
}
}).call(this);
if ((options != null) ? options.precompile : undefined) {
for (i = 0, _len = pair.length; i < _len; ++i) {
for (i = 0, _len = pair.length; i < _len; i++) {
node = pair[i];
(pair[i] = node.compile(o));
}
@@ -72,9 +72,9 @@
Base.prototype.compileLoopReference = function(o, name) {
var src, tmp;
src = tmp = this.compile(o);
if (!(NUMBER.test(src) || IDENTIFIER.test(src) && o.scope.check(src, {
if (!(NUMBER.test(src) || (IDENTIFIER.test(src) && o.scope.check(src, {
immediate: true
}))) {
})))) {
src = "" + (tmp = o.scope.freeVariable(name)) + " = " + src;
}
return [src, tmp];
@@ -113,8 +113,9 @@
var _i, _len, _ref2, _result, child, children, klass;
idt || (idt = '');
children = (function() {
_ref2 = this.collectChildren();
_result = [];
for (_i = 0, _len = (_ref2 = this.collectChildren()).length; _i < _len; ++_i) {
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
child = _ref2[_i];
_result.push(child.toString(idt + TAB));
}
@@ -128,10 +129,12 @@
if (!this.children) {
return;
}
for (_i = 0, _len = (_ref2 = this.children).length; _i < _len; ++_i) {
_ref2 = this.children;
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
attr = _ref2[_i];
if (this[attr]) {
for (_j = 0, _len2 = (_ref3 = flatten([this[attr]])).length; _j < _len2; ++_j) {
_ref3 = flatten([this[attr]]);
for (_j = 0, _len2 = _ref3.length; _j < _len2; _j++) {
child = _ref3[_j];
if (func(child) === false) {
return;
@@ -215,8 +218,9 @@
Expressions.prototype.compileNode = function(o) {
var _i, _len, _ref2, _result, node;
return (function() {
_ref2 = this.expressions;
_result = [];
for (_i = 0, _len = (_ref2 = this.expressions).length; _i < _len; ++_i) {
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
node = _ref2[_i];
_result.push(this.compileExpression(node, merge(o)));
}
@@ -364,7 +368,8 @@
};
Value.prototype.isAtomic = function() {
var _i, _len, _ref2, node;
for (_i = 0, _len = (_ref2 = this.properties.concat(this.base)).length; _i < _len; ++_i) {
_ref2 = this.properties.concat(this.base);
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
node = _ref2[_i];
if (node.soakNode || node instanceof Call) {
return false;
@@ -425,7 +430,7 @@
if (props[0] instanceof Accessor && this.isSimpleNumber()) {
code = "(" + code + ")";
}
for (_i = 0, _len = props.length; _i < _len; ++_i) {
for (_i = 0, _len = props.length; _i < _len; _i++) {
prop = props[_i];
(code += prop.compile(o));
}
@@ -437,7 +442,8 @@
Array.prototype.push.apply(ifn.body.properties, this.properties);
return ifn;
}
for (i = 0, _len = (_ref2 = this.properties).length; i < _len; ++i) {
_ref2 = this.properties;
for (i = 0, _len = _ref2.length; i < _len; i++) {
prop = _ref2[i];
if (prop.soakNode) {
prop.soakNode = false;
@@ -552,7 +558,8 @@
break;
}
}
for (_i = 0, _len = (_ref3 = list.reverse()).length; _i < _len; ++_i) {
_ref3 = list.reverse();
for (_i = 0, _len = _ref3.length; _i < _len; _i++) {
call = _ref3[_i];
if (ifn) {
if (call.variable instanceof Call) {
@@ -571,15 +578,17 @@
return ifn.compile(o);
}
((_ref2 = this.variable) != null) ? _ref2.tags.front = this.tags.front : undefined;
for (_i = 0, _len = (_ref3 = this.args).length; _i < _len; ++_i) {
_ref3 = this.args;
for (_i = 0, _len = _ref3.length; _i < _len; _i++) {
arg = _ref3[_i];
if (arg instanceof Splat) {
return this.compileSplat(o);
}
}
args = (function() {
_ref4 = this.args;
_result = [];
for (_j = 0, _len2 = (_ref4 = this.args).length; _j < _len2; ++_j) {
for (_j = 0, _len2 = _ref4.length; _j < _len2; _j++) {
arg = _ref4[_j];
_result.push(arg.compileBare(o));
}
@@ -687,8 +696,9 @@
top = del(o, 'top');
o.indent = this.idt(1);
nonComments = (function() {
_ref2 = this.properties;
_result = [];
for (_i = 0, _len = (_ref2 = this.properties).length; _i < _len; ++_i) {
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
prop = _ref2[_i];
if (!(prop instanceof Comment)) {
_result.push(prop);
@@ -698,8 +708,9 @@
}).call(this);
lastNoncom = last(nonComments);
props = (function() {
_ref2 = this.properties;
_result = [];
for (i = 0, _len = (_ref2 = this.properties).length; i < _len; ++i) {
for (i = 0, _len = _ref2.length; i < _len; i++) {
prop = _ref2[i];
_result.push((function() {
join = i === this.properties.length - 1 ? '' : prop === lastNoncom || prop instanceof Comment ? '\n' : ',\n';
@@ -720,7 +731,8 @@
};
ObjectLiteral.prototype.assigns = function(name) {
var _i, _len, _ref2, prop;
for (_i = 0, _len = (_ref2 = this.properties).length; _i < _len; ++_i) {
_ref2 = this.properties;
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
prop = _ref2[_i];
if (prop.assigns(name)) {
return true;
@@ -747,14 +759,16 @@
ArrayLiteral.prototype.compileNode = function(o) {
var _i, _len, _len2, _ref2, _ref3, code, i, obj, objects;
o.indent = this.idt(1);
for (_i = 0, _len = (_ref2 = this.objects).length; _i < _len; ++_i) {
_ref2 = this.objects;
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
obj = _ref2[_i];
if (obj instanceof Splat) {
return this.compileSplatLiteral(o);
}
}
objects = [];
for (i = 0, _len2 = (_ref3 = this.objects).length; i < _len2; ++i) {
_ref3 = this.objects;
for (i = 0, _len2 = _ref3.length; i < _len2; i++) {
obj = _ref3[i];
code = obj.compileBare(o);
objects.push(obj instanceof Comment ? "\n" + code + "\n" + o.indent : i === this.objects.length - 1 ? code : code + ', ');
@@ -764,7 +778,8 @@
};
ArrayLiteral.prototype.assigns = function(name) {
var _i, _len, _ref2, obj;
for (_i = 0, _len = (_ref2 = this.objects).length; _i < _len; ++_i) {
_ref2 = this.objects;
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
obj = _ref2[_i];
if (obj.assigns(name)) {
return true;
@@ -811,7 +826,8 @@
} else {
constructor = new Code([], new Expressions([new Return(new Literal('this'))]));
}
for (_i = 0, _len = (_ref2 = this.properties).length; _i < _len; ++_i) {
_ref2 = this.properties;
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
prop = _ref2[_i];
pvar = prop.variable, func = prop.value;
if (pvar && pvar.base.value === 'constructor') {
@@ -952,7 +968,7 @@
assigns.push("" + (ref = o.scope.freeVariable('ref')) + " = " + valVar);
valVar = ref;
}
for (i = 0, _len = objects.length; i < _len; ++i) {
for (i = 0, _len = objects.length; i < _len; i++) {
obj = objects[i];
idx = i;
if (isObject) {
@@ -1023,7 +1039,8 @@
delete o.globals;
splat = undefined;
params = [];
for (i = 0, _len = (_ref2 = this.params).length; i < _len; ++i) {
_ref2 = this.params;
for (i = 0, _len = _ref2.length; i < _len; i++) {
param = _ref2[i];
if (splat) {
if (param.attach) {
@@ -1051,7 +1068,7 @@
o.scope.startLevel();
params = (function() {
_result = [];
for (_i = 0, _len2 = params.length; _i < _len2; ++_i) {
for (_i = 0, _len2 = params.length; _i < _len2; _i++) {
param = params[_i];
_result.push(param.compile(o));
}
@@ -1060,7 +1077,7 @@
if (!(empty || this.noReturn)) {
this.body.makeReturn();
}
for (_i = 0, _len2 = params.length; _i < _len2; ++_i) {
for (_i = 0, _len2 = params.length; _i < _len2; _i++) {
param = params[_i];
o.scope.parameter(param);
}
@@ -1141,7 +1158,8 @@
variadic = o.scope.freeVariable('result');
o.scope.assign(variadic, len + ' >= ' + this.arglength);
end = this.trailings.length ? ", " + len + " - " + this.trailings.length : undefined;
for (idx = 0, _len = (_ref2 = this.trailings).length; idx < _len; ++idx) {
_ref2 = this.trailings;
for (idx = 0, _len = _ref2.length; idx < _len; idx++) {
trailing = _ref2[idx];
if (trailing.attach) {
assign = trailing.assign;
@@ -1163,7 +1181,7 @@
var _len, arg, args, code, end, i, prev;
args = [];
end = -1;
for (i = 0, _len = list.length; i < _len; ++i) {
for (i = 0, _len = list.length; i < _len; i++) {
arg = list[i];
code = arg.compile(o);
prev = args[end];
@@ -1362,8 +1380,9 @@
}), sub = _ref2[0], ref = _ref2[1];
_ref3 = this.negated ? [' !== ', ' && '] : [' === ', ' || '], cmp = _ref3[0], cnj = _ref3[1];
tests = (function() {
_ref4 = this.array.base.objects;
_result = [];
for (i = 0, _len = (_ref4 = this.array.base.objects).length; i < _len; ++i) {
for (i = 0, _len = _ref4.length; i < _len; i++) {
item = _ref4[i];
_result.push((i ? ref : sub) + cmp + item.compile(o));
}
@@ -1536,7 +1555,7 @@
return '';
};
For.prototype.compileNode = function(o) {
var _ref2, _ref3, _ref4, _ref5, _ref6, body, cond, forPart, guardPart, idt, index, ivar, lvar, name, namePart, pvar, resultDef, resultRet, rvar, scope, sourcePart, step, svar, tail, top, tvar, varPart, vars;
var _ref2, _ref3, _ref4, _ref5, _ref6, body, cond, defPart, forPart, guardPart, idt, index, ivar, lvar, name, namePart, pvar, resultRet, rvar, scope, sourcePart, step, svar, tail, top, tvar, varPart, vars;
scope = o.scope;
top = del(o, 'top') && !this.returns;
name = !this.pattern && (((_ref2 = this.name) != null) ? _ref2.compile(o) : undefined);
@@ -1566,11 +1585,8 @@
}
cond = +pvar ? "" + ivar + " " + (pvar < 0 ? '>' : '<') + "= " + tvar : "" + pvar + " < 0 ? " + ivar + " >= " + tvar + " : " + ivar + " <= " + tvar;
} else {
if (name || !this.raw) {
if (name) {
_ref6 = this.source.compileLoopReference(o, 'ref'), sourcePart = _ref6[0], svar = _ref6[1];
if (!(sourcePart === svar || this.object)) {
sourcePart = "(" + sourcePart + ")";
}
} else {
sourcePart = svar = this.source.compile(o);
}
@@ -1579,15 +1595,16 @@
})) : name ? "" + name + " = " + svar + "[" + ivar + "]" : undefined;
if (!this.object) {
if (0 > pvar && (pvar | 0) === +pvar) {
vars = "" + ivar + " = " + sourcePart + ".length - 1";
vars = "" + ivar + " = " + svar + ".length - 1";
cond = "" + ivar + " >= 0";
} else {
lvar = scope.freeVariable('len');
vars = "" + ivar + " = 0, " + lvar + " = " + sourcePart + ".length";
vars = "" + ivar + " = 0, " + lvar + " = " + svar + ".length";
cond = "" + ivar + " < " + lvar;
}
}
}
defPart = '';
if (this.object) {
forPart = "" + ivar + " in " + sourcePart;
guardPart = !this.raw && ("" + idt + "if (!" + (utility('hasProp')) + ".call(" + svar + ", " + ivar + ")) continue;\n");
@@ -1595,12 +1612,15 @@
if (step !== pvar) {
vars += ", " + step;
}
if (svar !== sourcePart) {
defPart = "" + this.tab + sourcePart + ";\n";
}
forPart = ("" + vars + "; " + cond + "; ") + (function() {
switch (+pvar) {
case 1:
return '++' + ivar;
return ivar + '++';
case -1:
return '--' + ivar;
return ivar + '--';
default:
return ivar + (pvar < 0 ? ' -= ' + pvar.slice(1) : ' += ' + pvar);
}
@@ -1608,7 +1628,7 @@
}
if (!top) {
rvar = scope.freeVariable('result');
resultDef = "" + this.tab + rvar + " = [];\n";
defPart += "" + this.tab + rvar + " = [];\n";
resultRet = this.compileReturnValue(rvar, o);
body = Push.wrap(rvar, body);
}
@@ -1618,7 +1638,7 @@
if (namePart) {
varPart = "" + idt + namePart + ";\n";
}
return "" + (resultDef || '') + this.tab + "for (" + forPart + ") {\n" + (guardPart || '') + varPart + (body.compile(merge(o, {
return "" + (defPart || '') + this.tab + "for (" + forPart + ") {\n" + (guardPart || '') + varPart + (body.compile(merge(o, {
indent: idt,
top: true
}))) + "\n" + this.tab + "}" + (resultRet || '');
@@ -1641,7 +1661,8 @@
Switch.prototype.isStatement = YES;
Switch.prototype.makeReturn = function() {
var _i, _len, _ref2, pair;
for (_i = 0, _len = (_ref2 = this.cases).length; _i < _len; ++_i) {
_ref2 = this.cases;
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
pair = _ref2[_i];
pair[1].makeReturn();
}
@@ -1651,15 +1672,16 @@
return this;
};
Switch.prototype.compileNode = function(o) {
var _i, _j, _len, _len2, _ref2, _ref3, _ref4, _ref5, block, code, condition, conditions, idt1, idt2;
var _i, _j, _len, _len2, _ref2, _ref3, _ref4, block, code, condition, conditions, idt1, idt2;
idt1 = this.idt(1);
idt2 = o.indent = this.idt(2);
o.top = true;
code = "" + this.tab + "switch (" + ((((_ref2 = this.subject) != null) ? _ref2.compile(o) : undefined) || true) + ") {";
for (_i = 0, _len = (_ref3 = this.cases).length; _i < _len; ++_i) {
_ref4 = _ref3[_i], conditions = _ref4[0], block = _ref4[1];
for (_j = 0, _len2 = (_ref5 = flatten([conditions])).length; _j < _len2; ++_j) {
condition = _ref5[_j];
for (_i = 0, _len = this.cases.length; _i < _len; _i++) {
_ref3 = this.cases[_i], conditions = _ref3[0], block = _ref3[1];
_ref4 = flatten([conditions]);
for (_j = 0, _len2 = _ref4.length; _j < _len2; _j++) {
condition = _ref4[_j];
if (!this.subject) {
condition = condition.invert().invert();
}