mirror of
https://github.com/jashkenas/coffeescript.git
synced 2022-11-09 12:23:24 -05:00
normalizing loop declarations towards common JS practice.
This commit is contained in:
parent
0342b0a89f
commit
c92fd79f35
11 changed files with 126 additions and 89 deletions
|
@ -26,7 +26,8 @@
|
|||
};
|
||||
runScripts = function() {
|
||||
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];
|
||||
if (script.type === 'text/coffeescript') {
|
||||
if (script.src) {
|
||||
|
|
|
@ -46,8 +46,9 @@
|
|||
return printTasks();
|
||||
}
|
||||
options = oparse.parse(args);
|
||||
_ref = options.arguments;
|
||||
_result = [];
|
||||
for (_i = 0, _len = (_ref = options.arguments).length; _i < _len; ++_i) {
|
||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||
arg = _ref[_i];
|
||||
_result.push(invoke(arg));
|
||||
}
|
||||
|
|
|
@ -49,8 +49,9 @@
|
|||
};
|
||||
compileScripts = function() {
|
||||
var _i, _len, _ref2, _result, base, compile, source;
|
||||
_ref2 = sources;
|
||||
_result = [];
|
||||
for (_i = 0, _len = (_ref2 = sources).length; _i < _len; ++_i) {
|
||||
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
|
||||
source = _ref2[_i];
|
||||
_result.push((function() {
|
||||
base = path.join(source);
|
||||
|
@ -64,7 +65,7 @@
|
|||
return fs.readdir(source, function(err, files) {
|
||||
var _j, _len2, _result2, file;
|
||||
_result2 = [];
|
||||
for (_j = 0, _len2 = files.length; _j < _len2; ++_j) {
|
||||
for (_j = 0, _len2 = files.length; _j < _len2; _j++) {
|
||||
file = files[_j];
|
||||
_result2.push(compile(path.join(source, file)));
|
||||
}
|
||||
|
@ -89,7 +90,8 @@
|
|||
o = opts;
|
||||
options = compileOptions(file);
|
||||
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];
|
||||
require(helpers.starts(req, '.') ? fs.realpathSync(req) : req);
|
||||
}
|
||||
|
@ -186,7 +188,7 @@
|
|||
var _i, _len, _ref2, _result, strings, tag, token, value;
|
||||
strings = (function() {
|
||||
_result = [];
|
||||
for (_i = 0, _len = tokens.length; _i < _len; ++_i) {
|
||||
for (_i = 0, _len = tokens.length; _i < _len; _i++) {
|
||||
token = tokens[_i];
|
||||
_result.push((function() {
|
||||
_ref2 = [token[0], token[1].toString().replace(/\n/, '\\n')], tag = _ref2[0], value = _ref2[1];
|
||||
|
|
|
@ -602,10 +602,11 @@
|
|||
alternatives = grammar[name];
|
||||
grammar[name] = (function() {
|
||||
_result = [];
|
||||
for (_i = 0, _len = alternatives.length; _i < _len; ++_i) {
|
||||
for (_i = 0, _len = alternatives.length; _i < _len; _i++) {
|
||||
alt = alternatives[_i];
|
||||
_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];
|
||||
if (!grammar[token]) {
|
||||
tokens.push(token);
|
||||
|
|
|
@ -11,7 +11,7 @@
|
|||
exports.compact = function(array) {
|
||||
var _i, _len, _result, item;
|
||||
_result = [];
|
||||
for (_i = 0, _len = array.length; _i < _len; ++_i) {
|
||||
for (_i = 0, _len = array.length; _i < _len; _i++) {
|
||||
item = array[_i];
|
||||
if (item) {
|
||||
_result.push(item);
|
||||
|
@ -41,7 +41,7 @@
|
|||
exports.flatten = flatten = function(array) {
|
||||
var _i, _len, element, flattened;
|
||||
flattened = [];
|
||||
for (_i = 0, _len = array.length; _i < _len; ++_i) {
|
||||
for (_i = 0, _len = array.length; _i < _len; _i++) {
|
||||
element = array[_i];
|
||||
if (element instanceof Array) {
|
||||
flattened = flattened.concat(flatten(element));
|
||||
|
|
16
lib/lexer.js
16
lib/lexer.js
|
@ -215,7 +215,7 @@
|
|||
return true;
|
||||
};
|
||||
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];
|
||||
this.i += heregex.length;
|
||||
if (0 > body.indexOf('#{')) {
|
||||
|
@ -226,10 +226,12 @@
|
|||
this.token('IDENTIFIER', 'RegExp');
|
||||
this.tokens.push(['CALL_START', '(']);
|
||||
tokens = [];
|
||||
for (_i = 0, _len = (_ref2 = this.interpolateString(body, {
|
||||
for (_i = 0, _len = this.interpolateString(body, {
|
||||
regex: true
|
||||
})).length; _i < _len; ++_i) {
|
||||
_ref3 = _ref2[_i], tag = _ref3[0], value = _ref3[1];
|
||||
}).length; _i < _len; _i++) {
|
||||
_ref2 = this.interpolateString(body, {
|
||||
regex: true
|
||||
})[_i], tag = _ref2[0], value = _ref2[1];
|
||||
if (tag === 'TOKENS') {
|
||||
tokens.push.apply(tokens, value);
|
||||
} else {
|
||||
|
@ -242,7 +244,7 @@
|
|||
tokens.push(['+', '+']);
|
||||
}
|
||||
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 = this.tokens).push.apply(_this, tokens);
|
||||
|
@ -480,7 +482,7 @@
|
|||
if (levels.length && str.charAt(i) === '\\') {
|
||||
i += 1;
|
||||
} else {
|
||||
for (_i = 0, _len = delimited.length; _i < _len; ++_i) {
|
||||
for (_i = 0, _len = delimited.length; _i < _len; _i++) {
|
||||
pair = delimited[_i];
|
||||
open = pair[0], close = pair[1];
|
||||
if (levels.length && starts(str, close, i) && last(levels) === pair) {
|
||||
|
@ -556,7 +558,7 @@
|
|||
if (interpolated = tokens.length > 1) {
|
||||
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];
|
||||
if (i) {
|
||||
this.token('+', '+');
|
||||
|
|
110
lib/nodes.js
110
lib/nodes.js
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -15,11 +15,12 @@
|
|||
arguments: []
|
||||
};
|
||||
args = normalizeArguments(args);
|
||||
for (i = 0, _len = args.length; i < _len; ++i) {
|
||||
for (i = 0, _len = args.length; i < _len; i++) {
|
||||
arg = args[i];
|
||||
isOption = !!(arg.match(LONG_FLAG) || arg.match(SHORT_FLAG));
|
||||
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];
|
||||
if (rule.shortFlag === arg || rule.longFlag === arg) {
|
||||
value = rule.hasArgument ? args[i += 1] : true;
|
||||
|
@ -44,7 +45,8 @@
|
|||
if (this.banner) {
|
||||
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];
|
||||
spaces = 15 - rule.longFlag.length;
|
||||
spaces = spaces > 0 ? Array(spaces + 1).join(' ') : '';
|
||||
|
@ -62,7 +64,7 @@
|
|||
buildRules = function(rules) {
|
||||
var _i, _len, _result, tuple;
|
||||
_result = [];
|
||||
for (_i = 0, _len = rules.length; _i < _len; ++_i) {
|
||||
for (_i = 0, _len = rules.length; _i < _len; _i++) {
|
||||
tuple = rules[_i];
|
||||
_result.push((function() {
|
||||
if (tuple.length < 3) {
|
||||
|
@ -91,10 +93,11 @@
|
|||
var _i, _j, _len, _len2, _ref, arg, l, match, result;
|
||||
args = args.slice(0);
|
||||
result = [];
|
||||
for (_i = 0, _len = args.length; _i < _len; ++_i) {
|
||||
for (_i = 0, _len = args.length; _i < _len; _i++) {
|
||||
arg = args[_i];
|
||||
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];
|
||||
result.push('-' + l);
|
||||
}
|
||||
|
|
|
@ -86,9 +86,9 @@
|
|||
});
|
||||
};
|
||||
exports.Rewriter.prototype.removeLeadingNewlines = function() {
|
||||
var _len, _ref, i, tag;
|
||||
for (i = 0, _len = (_ref = this.tokens).length; i < _len; ++i) {
|
||||
tag = _ref[i][0];
|
||||
var _len, i, tag;
|
||||
for (i = 0, _len = this.tokens.length; i < _len; i++) {
|
||||
tag = this.tokens[i][0];
|
||||
if (tag !== 'TERMINATOR') {
|
||||
break;
|
||||
}
|
||||
|
@ -284,10 +284,10 @@
|
|||
levels = {};
|
||||
openLine = {};
|
||||
this.scanTokens(function(token, i) {
|
||||
var _i, _len, _ref, _ref2, close, open, tag;
|
||||
var _i, _len, _ref, close, open, tag;
|
||||
tag = token[0];
|
||||
for (_i = 0, _len = (_ref = pairs).length; _i < _len; ++_i) {
|
||||
_ref2 = _ref[_i], open = _ref2[0], close = _ref2[1];
|
||||
for (_i = 0, _len = pairs.length; _i < _len; _i++) {
|
||||
_ref = pairs[_i], open = _ref[0], close = _ref[1];
|
||||
levels[open] |= 0;
|
||||
if (tag === open) {
|
||||
if (levels[open] === 0) {
|
||||
|
@ -366,7 +366,7 @@
|
|||
INVERSES = {};
|
||||
EXPRESSION_START = [];
|
||||
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];
|
||||
EXPRESSION_START.push(INVERSES[rite] = left);
|
||||
EXPRESSION_END.push(INVERSES[left] = rite);
|
||||
|
|
15
lib/scope.js
15
lib/scope.js
|
@ -42,7 +42,8 @@
|
|||
};
|
||||
Scope.prototype.endLevel = function() {
|
||||
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];
|
||||
if (this.type(name) === 'var') {
|
||||
this.add(name, 'reuse');
|
||||
|
@ -59,7 +60,8 @@
|
|||
};
|
||||
Scope.prototype.any = function(fn) {
|
||||
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];
|
||||
if (fn(v)) {
|
||||
return true;
|
||||
|
@ -83,7 +85,8 @@
|
|||
};
|
||||
Scope.prototype.type = function(name) {
|
||||
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];
|
||||
if (v.name === name) {
|
||||
return v.type;
|
||||
|
@ -121,8 +124,9 @@
|
|||
Scope.prototype.declaredVariables = function() {
|
||||
var _i, _len, _ref2, _ref3, _result, v;
|
||||
return (function() {
|
||||
_ref2 = this.variables;
|
||||
_result = [];
|
||||
for (_i = 0, _len = (_ref2 = this.variables).length; _i < _len; ++_i) {
|
||||
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
|
||||
v = _ref2[_i];
|
||||
if ((_ref3 = v.type) === 'var' || _ref3 === 'reuse') {
|
||||
_result.push(v.name);
|
||||
|
@ -133,8 +137,9 @@
|
|||
};
|
||||
Scope.prototype.assignedVariables = function() {
|
||||
var _i, _len, _ref2, _result, v;
|
||||
_ref2 = this.variables;
|
||||
_result = [];
|
||||
for (_i = 0, _len = (_ref2 = this.variables).length; _i < _len; ++_i) {
|
||||
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
|
||||
v = _ref2[_i];
|
||||
if (v.type.assigned) {
|
||||
_result.push("" + v.name + " = " + v.type.value);
|
||||
|
|
|
@ -78,8 +78,7 @@ exports.Base = class Base
|
|||
# Compile to a source/variable pair suitable for looping.
|
||||
compileLoopReference: (o, name) ->
|
||||
src = tmp = @compile o
|
||||
unless NUMBER.test(src) or
|
||||
IDENTIFIER.test(src) and o.scope.check(src, immediate: on)
|
||||
unless NUMBER.test(src) or (IDENTIFIER.test(src) and o.scope.check(src, immediate: on))
|
||||
src = "#{ tmp = o.scope.freeVariable name } = #{src}"
|
||||
[src, tmp]
|
||||
|
||||
|
@ -1352,9 +1351,8 @@ exports.For = class For extends Base
|
|||
else
|
||||
"#{pvar} < 0 ? #{ivar} >= #{tvar} : #{ivar} <= #{tvar}"
|
||||
else
|
||||
if name or not @raw
|
||||
if name
|
||||
[sourcePart, svar] = @source.compileLoopReference o, 'ref'
|
||||
sourcePart = "(#{sourcePart})" unless sourcePart is svar or @object
|
||||
else
|
||||
sourcePart = svar = @source.compile o
|
||||
namePart = if @pattern
|
||||
|
@ -1363,31 +1361,33 @@ exports.For = class For extends Base
|
|||
"#{name} = #{svar}[#{ivar}]"
|
||||
unless @object
|
||||
if 0 > pvar and (pvar | 0) is +pvar # negative int
|
||||
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 @object
|
||||
forPart = "#{ivar} in #{sourcePart}"
|
||||
guardPart = not @raw and
|
||||
"#{idt}if (!#{ utility 'hasProp' }.call(#{svar}, #{ivar})) continue;\n"
|
||||
else
|
||||
vars += ", #{step}" if step isnt pvar
|
||||
defPart = "#{@tab}#{sourcePart};\n" if svar isnt sourcePart
|
||||
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
|
||||
unless top
|
||||
rvar = scope.freeVariable 'result'
|
||||
resultDef = "#{@tab}#{rvar} = [];\n"
|
||||
defPart += "#{@tab}#{rvar} = [];\n"
|
||||
resultRet = @compileReturnValue rvar, o
|
||||
body = Push.wrap rvar, body
|
||||
body = Expressions.wrap [new If @guard, body] if @guard
|
||||
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 }
|
||||
#{@tab}}#{ resultRet or '' }
|
||||
"""
|
||||
|
|
Loading…
Reference in a new issue