refactored loop variable caching

This commit is contained in:
satyr
2010-10-13 20:29:22 +09:00
parent bd10c2f828
commit 79148d2940
16 changed files with 257 additions and 360 deletions

View File

@@ -52,13 +52,13 @@
if (!this.isComplex()) {
return [this, this];
} else {
reference = new Literal(o.scope.freeVariable(((options != null) ? options.name : undefined) || 'ref'));
reference = new Literal(o.scope.freeVariable('ref'));
compiled = new Assign(reference, this);
return [compiled, reference];
}
}).call(this);
if (((options != null) ? options.precompile : undefined)) {
for (i = 0, _len = pair.length; i < _len; i++) {
if ((options != null) ? options.precompile : undefined) {
for (i = 0, _len = pair.length; i < _len; ++i) {
node = pair[i];
(pair[i] = node.compile(o));
}
@@ -69,6 +69,16 @@
this.parenthetical = true;
return this.compile(o);
};
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, {
immediate: true
}))) {
src = "" + (tmp = o.scope.freeVariable(name)) + " = " + src;
}
return [src, tmp];
};
Base.prototype.idt = function(tabs) {
return (this.tab || '') + Array((tabs || 0) + 1).join(TAB);
};
@@ -104,7 +114,7 @@
idt || (idt = '');
children = (function() {
_result = [];
for (_i = 0, _len = (_ref2 = this.collectChildren()).length; _i < _len; _i++) {
for (_i = 0, _len = (_ref2 = this.collectChildren()).length; _i < _len; ++_i) {
child = _ref2[_i];
_result.push(child.toString(idt + TAB));
}
@@ -118,10 +128,10 @@
if (!this.children) {
return;
}
for (_i = 0, _len = (_ref2 = this.children).length; _i < _len; _i++) {
for (_i = 0, _len = (_ref2 = this.children).length; _i < _len; ++_i) {
attr = _ref2[_i];
if (this[attr]) {
for (_j = 0, _len2 = (_ref3 = flatten([this[attr]])).length; _j < _len2; _j++) {
for (_j = 0, _len2 = (_ref3 = flatten([this[attr]])).length; _j < _len2; ++_j) {
child = _ref3[_j];
if (func(child) === false) {
return;
@@ -189,9 +199,9 @@
};
Expressions.prototype.makeReturn = function() {
var end, idx;
end = this.expressions[(idx = this.expressions.length - 1)];
end = this.expressions[idx = this.expressions.length - 1];
if (end instanceof Comment) {
end = this.expressions[(idx -= 1)];
end = this.expressions[idx -= 1];
}
if (end && !(end instanceof Return)) {
this.expressions[idx] = end.makeReturn();
@@ -206,7 +216,7 @@
var _i, _len, _ref2, _result, node;
return (function() {
_result = [];
for (_i = 0, _len = (_ref2 = this.expressions).length; _i < _len; _i++) {
for (_i = 0, _len = (_ref2 = this.expressions).length; _i < _len; ++_i) {
node = _ref2[_i];
_result.push(this.compileExpression(node, merge(o)));
}
@@ -264,7 +274,7 @@
};
Literal.prototype.isStatement = function() {
var _ref2;
return ((_ref2 = this.value) === 'break' || _ref2 === 'continue' || _ref2 === 'debugger');
return (_ref2 = this.value) === 'break' || _ref2 === 'continue' || _ref2 === 'debugger';
};
Literal.prototype.isPureStatement = Literal.prototype.isStatement;
Literal.prototype.isComplex = NO;
@@ -302,7 +312,7 @@
Return.prototype.makeReturn = THIS;
Return.prototype.compile = function(o) {
var _ref2, expr;
expr = (((_ref2 = this.expression) != null) ? _ref2.makeReturn() : undefined);
expr = ((_ref2 = this.expression) != null) ? _ref2.makeReturn() : undefined;
if (expr && !(expr instanceof Return)) {
return expr.compile(o);
}
@@ -405,7 +415,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));
}
@@ -417,7 +427,7 @@
Array.prototype.push.apply(ifn.body.properties, this.properties);
return ifn;
}
for (i = 0, _len = (_ref2 = this.properties).length; i < _len; i++) {
for (i = 0, _len = (_ref2 = this.properties).length; i < _len; ++i) {
prop = _ref2[i];
if (prop.soakNode) {
prop.soakNode = false;
@@ -532,7 +542,7 @@
break;
}
}
for (_i = 0, _len = (_ref3 = list.reverse()).length; _i < _len; _i++) {
for (_i = 0, _len = (_ref3 = list.reverse()).length; _i < _len; ++_i) {
call = _ref3[_i];
if (ifn) {
if (call.variable instanceof Call) {
@@ -550,8 +560,8 @@
if (ifn = this.unfoldSoak(o)) {
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++) {
((_ref2 = this.variable) != null) ? _ref2.tags.front = this.tags.front : undefined;
for (_i = 0, _len = (_ref3 = this.args).length; _i < _len; ++_i) {
arg = _ref3[_i];
if (arg instanceof Splat) {
return this.compileSplat(o);
@@ -559,7 +569,7 @@
}
args = (function() {
_result = [];
for (_j = 0, _len2 = (_ref4 = this.args).length; _j < _len2; _j++) {
for (_j = 0, _len2 = (_ref4 = this.args).length; _j < _len2; ++_j) {
arg = _ref4[_j];
_result.push(arg.compileBare(o));
}
@@ -574,7 +584,7 @@
var base, fun, idt, name, ref, splatargs;
splatargs = this.compileSplatArguments(o);
if (this.isSuper) {
return ("" + (this.superReference(o)) + ".apply(this, " + splatargs + ")");
return "" + (this.superReference(o)) + ".apply(this, " + splatargs + ")";
}
if (!this.isNew) {
base = Value.wrap(this.variable);
@@ -587,7 +597,7 @@
fun += name.compile(o);
}
}
return ("" + fun + ".apply(" + ref + ", " + splatargs + ")");
return "" + fun + ".apply(" + ref + ", " + splatargs + ")";
}
idt = this.idt(1);
return "(function(func, args, ctor) {\n" + idt + "ctor.prototype = func.prototype;\n" + idt + "var child = new ctor, result = func.apply(child, args);\n" + idt + "return typeof result === \"object\" ? result : child;\n" + (this.tab) + "})(" + (this.variable.compile(o)) + ", " + splatargs + ", function() {})";
@@ -668,7 +678,7 @@
o.indent = this.idt(1);
nonComments = (function() {
_result = [];
for (_i = 0, _len = (_ref2 = this.properties).length; _i < _len; _i++) {
for (_i = 0, _len = (_ref2 = this.properties).length; _i < _len; ++_i) {
prop = _ref2[_i];
if (!(prop instanceof Comment)) {
_result.push(prop);
@@ -679,7 +689,7 @@
lastNoncom = last(nonComments);
props = (function() {
_result = [];
for (i = 0, _len = (_ref2 = this.properties).length; i < _len; i++) {
for (i = 0, _len = (_ref2 = this.properties).length; i < _len; ++i) {
prop = _ref2[i];
_result.push((function() {
join = i === this.properties.length - 1 ? '' : prop === lastNoncom || prop instanceof Comment ? '\n' : ',\n';
@@ -700,7 +710,7 @@
};
ObjectLiteral.prototype.assigns = function(name) {
var _i, _len, _ref2, prop;
for (_i = 0, _len = (_ref2 = this.properties).length; _i < _len; _i++) {
for (_i = 0, _len = (_ref2 = this.properties).length; _i < _len; ++_i) {
prop = _ref2[_i];
if (prop.assigns(name)) {
return true;
@@ -727,14 +737,14 @@
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++) {
for (_i = 0, _len = (_ref2 = this.objects).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++) {
for (i = 0, _len2 = (_ref3 = this.objects).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 + ', ');
@@ -744,7 +754,7 @@
};
ArrayLiteral.prototype.assigns = function(name) {
var _i, _len, _ref2, obj;
for (_i = 0, _len = (_ref2 = this.objects).length; _i < _len; _i++) {
for (_i = 0, _len = (_ref2 = this.objects).length; _i < _len; ++_i) {
obj = _ref2[_i];
if (obj.assigns(name)) {
return true;
@@ -791,7 +801,7 @@
} else {
constructor = new Code([], new Expressions([new Return(new Literal('this'))]));
}
for (_i = 0, _len = (_ref2 = this.properties).length; _i < _len; _i++) {
for (_i = 0, _len = (_ref2 = this.properties).length; _i < _len; ++_i) {
prop = _ref2[_i];
pvar = prop.variable, func = prop.value;
if (pvar && pvar.base.value === 'constructor') {
@@ -873,14 +883,11 @@
if (this.variable.isArray() || this.variable.isObject()) {
return this.compilePatternMatch(o);
}
if (this.variable.isSplice()) {
return this.compileSplice(o);
}
if (ifn = If.unfoldSoak(o, this, 'variable')) {
delete o.top;
return ifn.compile(o);
}
if (_ref2 = this.context, __indexOf.call(this.CONDITIONAL, _ref2) >= 0) {
if ((_ref2 = this.context, __indexOf.call(this.CONDITIONAL, _ref2) >= 0)) {
return this.compileConditional(o);
}
}
@@ -893,14 +900,14 @@
}
val = this.value.compileBare(o);
if (this.context === 'object') {
return ("" + name + ": " + val);
return "" + name + ": " + val;
}
if (!(isValue && (this.variable.hasProperties() || this.variable.namespaced))) {
o.scope.find(name);
}
val = name + (" " + (this.context || '=') + " ") + val;
if (stmt) {
return ("" + (this.tab) + val + ";");
return "" + (this.tab) + val + ";";
}
return top || this.parenthetical ? val : "(" + val + ")";
};
@@ -935,7 +942,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) {
@@ -966,6 +973,11 @@
code = assigns.join(', ');
return top || this.parenthetical ? code : "(" + code + ")";
};
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);
};
Assign.prototype.assigns = function(name) {
return this[this.context === 'object' ? 'value' : 'variable'].assigns(name);
};
@@ -1001,7 +1013,7 @@
delete o.globals;
splat = undefined;
params = [];
for (i = 0, _len = (_ref2 = this.params).length; i < _len; i++) {
for (i = 0, _len = (_ref2 = this.params).length; i < _len; ++i) {
param = _ref2[i];
if (splat) {
if (param.attach) {
@@ -1029,7 +1041,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));
}
@@ -1038,7 +1050,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);
}
@@ -1052,7 +1064,7 @@
func = "" + open + (params.join(', ')) + ") {" + code + close;
o.scope.endLevel();
if (this.bound) {
return ("" + (utility('bind')) + "(" + func + ", " + (this.context) + ")");
return "" + (utility('bind')) + "(" + func + ", " + (this.context) + ")";
}
return this.tags.front ? "(" + func + ")" : func;
};
@@ -1119,7 +1131,7 @@
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++) {
for (idx = 0, _len = (_ref2 = this.trailings).length; idx < _len; ++idx) {
trailing = _ref2[idx];
if (trailing.attach) {
assign = trailing.assign;
@@ -1141,7 +1153,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];
@@ -1167,7 +1179,7 @@
function While(condition, opts) {
While.__super__.constructor.call(this);
this.condition = ((opts != null) ? opts.invert : undefined) ? condition.invert() : condition;
this.guard = ((opts != null) ? opts.guard : undefined);
this.guard = (opts != null) ? opts.guard : undefined;
return this;
};
return While;
@@ -1338,16 +1350,10 @@
_ref2 = this.object.compileReference(o, {
precompile: true
}), sub = _ref2[0], ref = _ref2[1];
_ref3 = (function() {
if (this.negated) {
return [' !== ', ' && '];
} else {
return [' === ', ' || '];
}
}).call(this), cmp = _ref3[0], cnj = _ref3[1];
_ref3 = this.negated ? [' !== ', ' && '] : [' === ', ' || '], cmp = _ref3[0], cnj = _ref3[1];
tests = (function() {
_result = [];
for (i = 0, _len = (_ref4 = this.array.base.objects).length; i < _len; i++) {
for (i = 0, _len = (_ref4 = this.array.base.objects).length; i < _len; ++i) {
item = _ref4[i];
_result.push((i ? ref : sub) + cmp + item.compile(o));
}
@@ -1363,7 +1369,7 @@
}), {
precompile: true
}), sub = _ref2[0], ref = _ref2[1];
code = utility('indexOf') + (".call(" + (this.array.compile(o)) + ", " + ref + ") ")(+(this.negated ? '< 0' : '>= 0'));
code = utility('indexOf') + (".call(" + (this.array.compile(o)) + ", " + ref + ") ") + (this.negated ? '< 0' : '>= 0');
return sub === ref ? code : "(" + sub + ", " + code + ")";
};
In.prototype.toString = function(idt) {
@@ -1518,26 +1524,16 @@
return '';
};
For.prototype.compileNode = function(o) {
var _ref2, _ref3, _ref4, _ref5, _ref6, body, codeInBody, cond, forPart, guardPart, head, hvar, idt, incr, index, ivar, lastLine, lvar, name, namePart, pvar, ref, resultDef, resultRet, rvar, scope, sourcePart, step, svar, tail, top, tvar, varPart, vars;
if (this.step) {
o.top = true;
_ref2 = this.step.compileReference(o, {
precompile: true,
name: 'step'
}), step = _ref2[0], pvar = _ref2[1];
}
top = del(o, 'top') && !this.returns;
codeInBody = this.body.contains(function(node) {
return node instanceof Code;
});
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;
scope = o.scope;
name = !this.pattern && (((_ref3 = this.name) != null) ? _ref3.compile(o) : undefined);
index = (((_ref4 = this.index) != null) ? _ref4.compile(o) : undefined);
ivar = !index || codeInBody ? scope.freeVariable('i') : index;
top = del(o, 'top') && !this.returns;
name = !this.pattern && (((_ref2 = this.name) != null) ? _ref2.compile(o) : undefined);
index = ((_ref3 = this.index) != null) ? _ref3.compile(o) : undefined;
ivar = !index ? scope.freeVariable('i') : index;
varPart = '';
body = Expressions.wrap([this.body]);
idt = this.idt(1);
if (name && !codeInBody) {
if (name) {
scope.find(name, {
immediate: true
});
@@ -1547,46 +1543,24 @@
immediate: true
});
}
if (!this.object) {
switch (+pvar) {
case 1:
incr = '++' + ivar;
break;
case -1:
incr = '--' + ivar;
break;
default:
incr = ivar + (pvar < 0 ? ' -= ' + pvar.slice(1) : ' += ' + pvar);
}
if (this.step) {
_ref4 = this.step.compileLoopReference(o, 'step'), step = _ref4[0], pvar = _ref4[1];
}
if (this.from) {
_ref5 = this.from.compileReference(o, {
precompile: true,
name: 'from'
}), head = _ref5[0], hvar = _ref5[1];
_ref6 = this.to.compileReference(o, {
precompile: true,
name: 'to'
}), tail = _ref6[0], tvar = _ref6[1];
vars = "" + ivar + " = " + head;
_ref5 = this.to.compileLoopReference(o, 'to'), tail = _ref5[0], tvar = _ref5[1];
vars = "" + ivar + " = " + (this.from.compile(o));
if (tail !== tvar) {
vars += ", " + tail;
}
if (step !== pvar) {
vars += ", " + step;
}
cond = isNaN(step) ? "" + pvar + " < 0 ? " + ivar + " >= " + tvar + " : " + ivar + " <= " + tvar : "" + ivar + " " + (step < 0 ? '>=' : '<=') + " " + tvar;
forPart = "" + vars + "; " + cond + "; " + incr;
cond = +pvar ? "" + ivar + " " + (pvar < 0 ? '>' : '<') + "= " + tvar : "" + pvar + " < 0 ? " + ivar + " >= " + tvar + " : " + ivar + " <= " + tvar;
} else {
svar = sourcePart = this.source.compile(o);
if ((name || !this.raw) && !(IDENTIFIER.test(svar) && scope.check(svar, {
immediate: true
}))) {
sourcePart = "" + (ref = scope.freeVariable('ref')) + " = " + svar;
if (!this.object) {
if (name || !this.raw) {
_ref6 = this.source.compileLoopReference(o, 'ref'), sourcePart = _ref6[0], svar = _ref6[1];
if (!(sourcePart === svar || this.object)) {
sourcePart = "(" + sourcePart + ")";
}
svar = ref;
} else {
sourcePart = svar = this.source.compile(o);
}
namePart = this.pattern ? new Assign(this.name, new Literal("" + svar + "[" + ivar + "]")).compile(merge(o, {
top: true
@@ -1600,12 +1574,26 @@
vars = "" + ivar + " = 0, " + lvar + " = " + sourcePart + ".length";
cond = "" + ivar + " < " + lvar;
}
if (step !== pvar) {
vars += ", " + step;
}
forPart = "" + vars + "; " + cond + "; " + incr;
}
}
if (this.object) {
forPart = "" + ivar + " in " + sourcePart;
guardPart = !this.raw && ("" + idt + "if (!" + (utility('hasProp')) + ".call(" + svar + ", " + ivar + ")) continue;\n");
} else {
if (step !== pvar) {
vars += ", " + step;
}
forPart = ("" + vars + "; " + cond + "; ") + (function() {
switch (+pvar) {
case 1:
return '++' + ivar;
case -1:
return '--' + ivar;
default:
return ivar + (pvar < 0 ? ' -= ' + pvar.slice(1) : ' += ' + pvar);
}
})();
}
if (!top) {
rvar = scope.freeVariable('result');
resultDef = "" + (this.tab) + rvar + " = [];\n";
@@ -1615,40 +1603,8 @@
if (this.guard) {
body = Expressions.wrap([new If(this.guard, body)]);
}
if (codeInBody) {
if (this.from) {
body.unshift(new Literal("var " + name + " = " + ivar));
}
if (namePart) {
body.unshift(new Literal("var " + namePart));
}
if (index) {
body.unshift(new Literal("var " + index + " = " + ivar));
}
lastLine = body.expressions.pop();
if (index) {
body.push(new Assign(new Literal(ivar), new Literal(index)));
}
if (nvar) {
body.push(new Assign(new Literal(nvar), new Literal(name)));
}
body.push(lastLine);
o.indent = this.idt(1);
body = Expressions.wrap([new Literal(body.compile(o))]);
if (index) {
body.push(new Assign(new Literal(index), new Literal(ivar)));
}
if (name) {
body.push(new Assign(new Literal(name), new Literal(nvar || ivar)));
}
} else {
if (namePart) {
varPart = "" + idt + namePart + ";\n";
}
}
if (this.object) {
forPart = "" + ivar + " in " + sourcePart;
guardPart = !this.raw && ("" + idt + "if (!" + (utility('hasProp')) + ".call(" + svar + ", " + ivar + ")) continue;\n");
if (namePart) {
varPart = "" + idt + namePart + ";\n";
}
return "" + (resultDef || '') + (this.tab) + "for (" + forPart + ") {\n" + (guardPart || '') + varPart + (body.compile(merge(o, {
indent: idt,
@@ -1673,7 +1629,7 @@
Switch.prototype.isStatement = YES;
Switch.prototype.makeReturn = function() {
var _i, _len, _ref2, pair;
for (_i = 0, _len = (_ref2 = this.cases).length; _i < _len; _i++) {
for (_i = 0, _len = (_ref2 = this.cases).length; _i < _len; ++_i) {
pair = _ref2[_i];
pair[1].makeReturn();
}
@@ -1688,9 +1644,9 @@
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++) {
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++) {
for (_j = 0, _len2 = (_ref5 = flatten([conditions])).length; _j < _len2; ++_j) {
condition = _ref5[_j];
if (!this.subject) {
condition = condition.invert().invert();
@@ -1728,11 +1684,11 @@
If.prototype.topSensitive = YES;
If.prototype.bodyNode = function() {
var _ref2;
return (((_ref2 = this.body) != null) ? _ref2.unwrap() : undefined);
return ((_ref2 = this.body) != null) ? _ref2.unwrap() : undefined;
};
If.prototype.elseBodyNode = function() {
var _ref2;
return (((_ref2 = this.elseBody) != null) ? _ref2.unwrap() : undefined);
return ((_ref2 = this.elseBody) != null) ? _ref2.unwrap() : undefined;
};
If.prototype.addElse = function(elseBody) {
if (this.isChain) {
@@ -1849,7 +1805,7 @@
TAB = ' ';
TRAILING_WHITESPACE = /[ \t]+$/gm;
IDENTIFIER = /^[$A-Za-z_][$\w]*$/;
NUMBER = /^0x[\da-f]+$|^(?:\d+(\.\d+)?|\.\d+)(?:e[+-]?\d+)?$/i;
NUMBER = /^-?(?:0x[\da-f]+|(?:\d+(\.\d+)?|\.\d+)(?:e[+-]?\d+)?)$/i;
SIMPLENUM = /^[+-]?\d+$/;
IS_STRING = /^['"]/;
utility = function(name) {