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

@ -26,7 +26,8 @@
}; };
runScripts = function() { runScripts = function() {
var _i, _len, _ref, script; var _i, _len, _ref, script;
for (_i = 0, _len = (_ref = document.getElementsByTagName('script')).length; _i < _len; ++_i) { _ref = document.getElementsByTagName('script');
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
script = _ref[_i]; script = _ref[_i];
if (script.type === 'text/coffeescript') { if (script.type === 'text/coffeescript') {
if (script.src) { if (script.src) {

View File

@ -46,8 +46,9 @@
return printTasks(); return printTasks();
} }
options = oparse.parse(args); options = oparse.parse(args);
_ref = options.arguments;
_result = []; _result = [];
for (_i = 0, _len = (_ref = options.arguments).length; _i < _len; ++_i) { for (_i = 0, _len = _ref.length; _i < _len; _i++) {
arg = _ref[_i]; arg = _ref[_i];
_result.push(invoke(arg)); _result.push(invoke(arg));
} }

View File

@ -49,8 +49,9 @@
}; };
compileScripts = function() { compileScripts = function() {
var _i, _len, _ref2, _result, base, compile, source; var _i, _len, _ref2, _result, base, compile, source;
_ref2 = sources;
_result = []; _result = [];
for (_i = 0, _len = (_ref2 = sources).length; _i < _len; ++_i) { for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
source = _ref2[_i]; source = _ref2[_i];
_result.push((function() { _result.push((function() {
base = path.join(source); base = path.join(source);
@ -64,7 +65,7 @@
return fs.readdir(source, function(err, files) { return fs.readdir(source, function(err, files) {
var _j, _len2, _result2, file; var _j, _len2, _result2, file;
_result2 = []; _result2 = [];
for (_j = 0, _len2 = files.length; _j < _len2; ++_j) { for (_j = 0, _len2 = files.length; _j < _len2; _j++) {
file = files[_j]; file = files[_j];
_result2.push(compile(path.join(source, file))); _result2.push(compile(path.join(source, file)));
} }
@ -89,7 +90,8 @@
o = opts; o = opts;
options = compileOptions(file); options = compileOptions(file);
if (o.require) { if (o.require) {
for (_i = 0, _len = (_ref2 = o.require).length; _i < _len; ++_i) { _ref2 = o.require;
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
req = _ref2[_i]; req = _ref2[_i];
require(helpers.starts(req, '.') ? fs.realpathSync(req) : req); require(helpers.starts(req, '.') ? fs.realpathSync(req) : req);
} }
@ -186,7 +188,7 @@
var _i, _len, _ref2, _result, strings, tag, token, value; var _i, _len, _ref2, _result, strings, tag, token, value;
strings = (function() { strings = (function() {
_result = []; _result = [];
for (_i = 0, _len = tokens.length; _i < _len; ++_i) { for (_i = 0, _len = tokens.length; _i < _len; _i++) {
token = tokens[_i]; token = tokens[_i];
_result.push((function() { _result.push((function() {
_ref2 = [token[0], token[1].toString().replace(/\n/, '\\n')], tag = _ref2[0], value = _ref2[1]; _ref2 = [token[0], token[1].toString().replace(/\n/, '\\n')], tag = _ref2[0], value = _ref2[1];

View File

@ -602,10 +602,11 @@
alternatives = grammar[name]; alternatives = grammar[name];
grammar[name] = (function() { grammar[name] = (function() {
_result = []; _result = [];
for (_i = 0, _len = alternatives.length; _i < _len; ++_i) { for (_i = 0, _len = alternatives.length; _i < _len; _i++) {
alt = alternatives[_i]; alt = alternatives[_i];
_result.push((function() { _result.push((function() {
for (_j = 0, _len2 = (_ref = alt[0].split(' ')).length; _j < _len2; ++_j) { _ref = alt[0].split(' ');
for (_j = 0, _len2 = _ref.length; _j < _len2; _j++) {
token = _ref[_j]; token = _ref[_j];
if (!grammar[token]) { if (!grammar[token]) {
tokens.push(token); tokens.push(token);

View File

@ -11,7 +11,7 @@
exports.compact = function(array) { exports.compact = function(array) {
var _i, _len, _result, item; var _i, _len, _result, item;
_result = []; _result = [];
for (_i = 0, _len = array.length; _i < _len; ++_i) { for (_i = 0, _len = array.length; _i < _len; _i++) {
item = array[_i]; item = array[_i];
if (item) { if (item) {
_result.push(item); _result.push(item);
@ -41,7 +41,7 @@
exports.flatten = flatten = function(array) { exports.flatten = flatten = function(array) {
var _i, _len, element, flattened; var _i, _len, element, flattened;
flattened = []; flattened = [];
for (_i = 0, _len = array.length; _i < _len; ++_i) { for (_i = 0, _len = array.length; _i < _len; _i++) {
element = array[_i]; element = array[_i];
if (element instanceof Array) { if (element instanceof Array) {
flattened = flattened.concat(flatten(element)); flattened = flattened.concat(flatten(element));

View File

@ -215,7 +215,7 @@
return true; return true;
}; };
Lexer.prototype.heregexToken = function(match) { Lexer.prototype.heregexToken = function(match) {
var _i, _len, _ref2, _ref3, _ref4, _this, body, flags, heregex, re, tag, tokens, value; var _i, _len, _ref2, _ref3, _this, body, flags, heregex, re, tag, tokens, value;
heregex = match[0], body = match[1], flags = match[2]; heregex = match[0], body = match[1], flags = match[2];
this.i += heregex.length; this.i += heregex.length;
if (0 > body.indexOf('#{')) { if (0 > body.indexOf('#{')) {
@ -226,10 +226,12 @@
this.token('IDENTIFIER', 'RegExp'); this.token('IDENTIFIER', 'RegExp');
this.tokens.push(['CALL_START', '(']); this.tokens.push(['CALL_START', '(']);
tokens = []; tokens = [];
for (_i = 0, _len = (_ref2 = this.interpolateString(body, { for (_i = 0, _len = this.interpolateString(body, {
regex: true regex: true
})).length; _i < _len; ++_i) { }).length; _i < _len; _i++) {
_ref3 = _ref2[_i], tag = _ref3[0], value = _ref3[1]; _ref2 = this.interpolateString(body, {
regex: true
})[_i], tag = _ref2[0], value = _ref2[1];
if (tag === 'TOKENS') { if (tag === 'TOKENS') {
tokens.push.apply(tokens, value); tokens.push.apply(tokens, value);
} else { } else {
@ -242,7 +244,7 @@
tokens.push(['+', '+']); tokens.push(['+', '+']);
} }
tokens.pop(); tokens.pop();
if ((((_ref4 = tokens[0]) != null) ? _ref4[0] : undefined) !== 'STRING') { if ((((_ref3 = tokens[0]) != null) ? _ref3[0] : undefined) !== 'STRING') {
this.tokens.push(['STRING', '""'], ['+', '+']); this.tokens.push(['STRING', '""'], ['+', '+']);
} }
(_this = this.tokens).push.apply(_this, tokens); (_this = this.tokens).push.apply(_this, tokens);
@ -480,7 +482,7 @@
if (levels.length && str.charAt(i) === '\\') { if (levels.length && str.charAt(i) === '\\') {
i += 1; i += 1;
} else { } else {
for (_i = 0, _len = delimited.length; _i < _len; ++_i) { for (_i = 0, _len = delimited.length; _i < _len; _i++) {
pair = delimited[_i]; pair = delimited[_i];
open = pair[0], close = pair[1]; open = pair[0], close = pair[1];
if (levels.length && starts(str, close, i) && last(levels) === pair) { if (levels.length && starts(str, close, i) && last(levels) === pair) {
@ -556,7 +558,7 @@
if (interpolated = tokens.length > 1) { if (interpolated = tokens.length > 1) {
this.token('(', '('); this.token('(', '(');
} }
for (i = 0, _len = tokens.length; i < _len; ++i) { for (i = 0, _len = tokens.length; i < _len; i++) {
_ref3 = tokens[i], tag = _ref3[0], value = _ref3[1]; _ref3 = tokens[i], tag = _ref3[0], value = _ref3[1];
if (i) { if (i) {
this.token('+', '+'); this.token('+', '+');

View File

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

View File

@ -15,11 +15,12 @@
arguments: [] arguments: []
}; };
args = normalizeArguments(args); args = normalizeArguments(args);
for (i = 0, _len = args.length; i < _len; ++i) { for (i = 0, _len = args.length; i < _len; i++) {
arg = args[i]; arg = args[i];
isOption = !!(arg.match(LONG_FLAG) || arg.match(SHORT_FLAG)); isOption = !!(arg.match(LONG_FLAG) || arg.match(SHORT_FLAG));
matchedRule = false; matchedRule = false;
for (_i = 0, _len2 = (_ref = this.rules).length; _i < _len2; ++_i) { _ref = this.rules;
for (_i = 0, _len2 = _ref.length; _i < _len2; _i++) {
rule = _ref[_i]; rule = _ref[_i];
if (rule.shortFlag === arg || rule.longFlag === arg) { if (rule.shortFlag === arg || rule.longFlag === arg) {
value = rule.hasArgument ? args[i += 1] : true; value = rule.hasArgument ? args[i += 1] : true;
@ -44,7 +45,8 @@
if (this.banner) { if (this.banner) {
lines.unshift("" + this.banner + "\n"); lines.unshift("" + this.banner + "\n");
} }
for (_i = 0, _len = (_ref = this.rules).length; _i < _len; ++_i) { _ref = this.rules;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
rule = _ref[_i]; rule = _ref[_i];
spaces = 15 - rule.longFlag.length; spaces = 15 - rule.longFlag.length;
spaces = spaces > 0 ? Array(spaces + 1).join(' ') : ''; spaces = spaces > 0 ? Array(spaces + 1).join(' ') : '';
@ -62,7 +64,7 @@
buildRules = function(rules) { buildRules = function(rules) {
var _i, _len, _result, tuple; var _i, _len, _result, tuple;
_result = []; _result = [];
for (_i = 0, _len = rules.length; _i < _len; ++_i) { for (_i = 0, _len = rules.length; _i < _len; _i++) {
tuple = rules[_i]; tuple = rules[_i];
_result.push((function() { _result.push((function() {
if (tuple.length < 3) { if (tuple.length < 3) {
@ -91,10 +93,11 @@
var _i, _j, _len, _len2, _ref, arg, l, match, result; var _i, _j, _len, _len2, _ref, arg, l, match, result;
args = args.slice(0); args = args.slice(0);
result = []; result = [];
for (_i = 0, _len = args.length; _i < _len; ++_i) { for (_i = 0, _len = args.length; _i < _len; _i++) {
arg = args[_i]; arg = args[_i];
if (match = arg.match(MULTI_FLAG)) { if (match = arg.match(MULTI_FLAG)) {
for (_j = 0, _len2 = (_ref = match[1].split('')).length; _j < _len2; ++_j) { _ref = match[1].split('');
for (_j = 0, _len2 = _ref.length; _j < _len2; _j++) {
l = _ref[_j]; l = _ref[_j];
result.push('-' + l); result.push('-' + l);
} }

View File

@ -86,9 +86,9 @@
}); });
}; };
exports.Rewriter.prototype.removeLeadingNewlines = function() { exports.Rewriter.prototype.removeLeadingNewlines = function() {
var _len, _ref, i, tag; var _len, i, tag;
for (i = 0, _len = (_ref = this.tokens).length; i < _len; ++i) { for (i = 0, _len = this.tokens.length; i < _len; i++) {
tag = _ref[i][0]; tag = this.tokens[i][0];
if (tag !== 'TERMINATOR') { if (tag !== 'TERMINATOR') {
break; break;
} }
@ -284,10 +284,10 @@
levels = {}; levels = {};
openLine = {}; openLine = {};
this.scanTokens(function(token, i) { this.scanTokens(function(token, i) {
var _i, _len, _ref, _ref2, close, open, tag; var _i, _len, _ref, close, open, tag;
tag = token[0]; tag = token[0];
for (_i = 0, _len = (_ref = pairs).length; _i < _len; ++_i) { for (_i = 0, _len = pairs.length; _i < _len; _i++) {
_ref2 = _ref[_i], open = _ref2[0], close = _ref2[1]; _ref = pairs[_i], open = _ref[0], close = _ref[1];
levels[open] |= 0; levels[open] |= 0;
if (tag === open) { if (tag === open) {
if (levels[open] === 0) { if (levels[open] === 0) {
@ -366,7 +366,7 @@
INVERSES = {}; INVERSES = {};
EXPRESSION_START = []; EXPRESSION_START = [];
EXPRESSION_END = []; EXPRESSION_END = [];
for (_i = 0, _len = BALANCED_PAIRS.length; _i < _len; ++_i) { for (_i = 0, _len = BALANCED_PAIRS.length; _i < _len; _i++) {
_ref = BALANCED_PAIRS[_i], left = _ref[0], rite = _ref[1]; _ref = BALANCED_PAIRS[_i], left = _ref[0], rite = _ref[1];
EXPRESSION_START.push(INVERSES[rite] = left); EXPRESSION_START.push(INVERSES[rite] = left);
EXPRESSION_END.push(INVERSES[left] = rite); EXPRESSION_END.push(INVERSES[left] = rite);

View File

@ -42,7 +42,8 @@
}; };
Scope.prototype.endLevel = function() { Scope.prototype.endLevel = function() {
var _i, _len, _ref2, name; var _i, _len, _ref2, name;
for (_i = 0, _len = (_ref2 = this.garbage.pop()).length; _i < _len; ++_i) { _ref2 = this.garbage.pop();
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
name = _ref2[_i]; name = _ref2[_i];
if (this.type(name) === 'var') { if (this.type(name) === 'var') {
this.add(name, 'reuse'); this.add(name, 'reuse');
@ -59,7 +60,8 @@
}; };
Scope.prototype.any = function(fn) { Scope.prototype.any = function(fn) {
var _i, _len, _ref2, v; var _i, _len, _ref2, v;
for (_i = 0, _len = (_ref2 = this.variables).length; _i < _len; ++_i) { _ref2 = this.variables;
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
v = _ref2[_i]; v = _ref2[_i];
if (fn(v)) { if (fn(v)) {
return true; return true;
@ -83,7 +85,8 @@
}; };
Scope.prototype.type = function(name) { Scope.prototype.type = function(name) {
var _i, _len, _ref2, v; var _i, _len, _ref2, v;
for (_i = 0, _len = (_ref2 = this.variables).length; _i < _len; ++_i) { _ref2 = this.variables;
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
v = _ref2[_i]; v = _ref2[_i];
if (v.name === name) { if (v.name === name) {
return v.type; return v.type;
@ -121,8 +124,9 @@
Scope.prototype.declaredVariables = function() { Scope.prototype.declaredVariables = function() {
var _i, _len, _ref2, _ref3, _result, v; var _i, _len, _ref2, _ref3, _result, v;
return (function() { return (function() {
_ref2 = this.variables;
_result = []; _result = [];
for (_i = 0, _len = (_ref2 = this.variables).length; _i < _len; ++_i) { for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
v = _ref2[_i]; v = _ref2[_i];
if ((_ref3 = v.type) === 'var' || _ref3 === 'reuse') { if ((_ref3 = v.type) === 'var' || _ref3 === 'reuse') {
_result.push(v.name); _result.push(v.name);
@ -133,8 +137,9 @@
}; };
Scope.prototype.assignedVariables = function() { Scope.prototype.assignedVariables = function() {
var _i, _len, _ref2, _result, v; var _i, _len, _ref2, _result, v;
_ref2 = this.variables;
_result = []; _result = [];
for (_i = 0, _len = (_ref2 = this.variables).length; _i < _len; ++_i) { for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
v = _ref2[_i]; v = _ref2[_i];
if (v.type.assigned) { if (v.type.assigned) {
_result.push("" + v.name + " = " + v.type.value); _result.push("" + v.name + " = " + v.type.value);

View File

@ -78,8 +78,7 @@ exports.Base = class Base
# Compile to a source/variable pair suitable for looping. # Compile to a source/variable pair suitable for looping.
compileLoopReference: (o, name) -> compileLoopReference: (o, name) ->
src = tmp = @compile o src = tmp = @compile o
unless NUMBER.test(src) or unless NUMBER.test(src) or (IDENTIFIER.test(src) and o.scope.check(src, immediate: on))
IDENTIFIER.test(src) and o.scope.check(src, immediate: on)
src = "#{ tmp = o.scope.freeVariable name } = #{src}" src = "#{ tmp = o.scope.freeVariable name } = #{src}"
[src, tmp] [src, tmp]
@ -1352,9 +1351,8 @@ exports.For = class For extends Base
else else
"#{pvar} < 0 ? #{ivar} >= #{tvar} : #{ivar} <= #{tvar}" "#{pvar} < 0 ? #{ivar} >= #{tvar} : #{ivar} <= #{tvar}"
else else
if name or not @raw if name
[sourcePart, svar] = @source.compileLoopReference o, 'ref' [sourcePart, svar] = @source.compileLoopReference o, 'ref'
sourcePart = "(#{sourcePart})" unless sourcePart is svar or @object
else else
sourcePart = svar = @source.compile o sourcePart = svar = @source.compile o
namePart = if @pattern namePart = if @pattern
@ -1363,31 +1361,33 @@ exports.For = class For extends Base
"#{name} = #{svar}[#{ivar}]" "#{name} = #{svar}[#{ivar}]"
unless @object unless @object
if 0 > pvar and (pvar | 0) is +pvar # negative int if 0 > pvar and (pvar | 0) is +pvar # negative int
vars = "#{ivar} = #{sourcePart}.length - 1" vars = "#{ivar} = #{svar}.length - 1"
cond = "#{ivar} >= 0" cond = "#{ivar} >= 0"
else else
lvar = scope.freeVariable 'len' lvar = scope.freeVariable 'len'
vars = "#{ivar} = 0, #{lvar} = #{sourcePart}.length" vars = "#{ivar} = 0, #{lvar} = #{svar}.length"
cond = "#{ivar} < #{lvar}" cond = "#{ivar} < #{lvar}"
defPart = ''
if @object if @object
forPart = "#{ivar} in #{sourcePart}" forPart = "#{ivar} in #{sourcePart}"
guardPart = not @raw and guardPart = not @raw and
"#{idt}if (!#{ utility 'hasProp' }.call(#{svar}, #{ivar})) continue;\n" "#{idt}if (!#{ utility 'hasProp' }.call(#{svar}, #{ivar})) continue;\n"
else else
vars += ", #{step}" if step isnt pvar vars += ", #{step}" if step isnt pvar
defPart = "#{@tab}#{sourcePart};\n" if svar isnt sourcePart
forPart = "#{vars}; #{cond}; " + switch +pvar forPart = "#{vars}; #{cond}; " + switch +pvar
when 1 then '++' + ivar when 1 then ivar + '++'
when -1 then '--' + ivar when -1 then ivar + '--'
else ivar + if pvar < 0 then ' -= ' + pvar.slice 1 else ' += ' + pvar else ivar + if pvar < 0 then ' -= ' + pvar.slice 1 else ' += ' + pvar
unless top unless top
rvar = scope.freeVariable 'result' rvar = scope.freeVariable 'result'
resultDef = "#{@tab}#{rvar} = [];\n" defPart += "#{@tab}#{rvar} = [];\n"
resultRet = @compileReturnValue rvar, o resultRet = @compileReturnValue rvar, o
body = Push.wrap rvar, body body = Push.wrap rvar, body
body = Expressions.wrap [new If @guard, body] if @guard body = Expressions.wrap [new If @guard, body] if @guard
varPart = "#{idt}#{namePart};\n" if namePart varPart = "#{idt}#{namePart};\n" if namePart
""" """
#{ resultDef or '' }#{@tab}for (#{forPart}) { #{ defPart or '' }#{@tab}for (#{forPart}) {
#{ guardPart or '' }#{varPart}#{ body.compile merge o, indent: idt, top: on } #{ guardPart or '' }#{varPart}#{ body.compile merge o, indent: idt, top: on }
#{@tab}}#{ resultRet or '' } #{@tab}}#{ resultRet or '' }
""" """