From 5eb255a649ea97cfa7e7709fdbdef1332b668113 Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Tue, 2 Nov 2010 00:31:42 -0400 Subject: [PATCH] modified satyr: allowed temporary variables to share names with ones on upper scopes --- lib/command.js | 32 +++--- lib/lexer.js | 36 +++--- lib/nodes.js | 286 +++++++++++++++++++++++------------------------ lib/rewriter.js | 8 +- lib/scope.js | 67 +++++------ src/nodes.coffee | 6 +- src/scope.coffee | 16 +-- 7 files changed, 223 insertions(+), 228 deletions(-) diff --git a/lib/command.js b/lib/command.js index 7c3fc5a7..0949cf31 100644 --- a/lib/command.js +++ b/lib/command.js @@ -50,11 +50,11 @@ return compileScripts(); }; compileScripts = function() { - var base, compile, source, _i, _len, _ref2, _result; - _ref2 = sources; + var base, compile, source, _i, _len, _ref, _result; + _ref = sources; _result = []; - for (_i = 0, _len = _ref2.length; _i < _len; _i++) { - source = _ref2[_i]; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + source = _ref[_i]; base = path.join(source); compile = function(source, topLevel) { return path.exists(source, function(exists) { @@ -64,13 +64,13 @@ return fs.stat(source, function(err, stats) { if (stats.isDirectory()) { return fs.readdir(source, function(err, files) { - var file, _j, _len2, _result2; - _result2 = []; - for (_j = 0, _len2 = files.length; _j < _len2; _j++) { - file = files[_j]; - _result2.push(compile(path.join(source, file))); + var file, _i, _len, _result; + _result = []; + for (_i = 0, _len = files.length; _i < _len; _i++) { + file = files[_i]; + _result.push(compile(path.join(source, file))); } - return _result2; + return _result; }); } else if (topLevel || path.extname(source) === '.coffee') { fs.readFile(source, function(err, code) { @@ -86,13 +86,13 @@ return _result; }; compileScript = function(file, input, base) { - var o, options, req, t, task, _i, _len, _ref2; + var o, options, req, t, task, _i, _len, _ref; o = opts; options = compileOptions(file); if (o.require) { - _ref2 = o.require; - for (_i = 0, _len = _ref2.length; _i < _len; _i++) { - req = _ref2[_i]; + _ref = o.require; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + req = _ref[_i]; require(helpers.starts(req, '.') ? fs.realpathSync(req) : req); } } @@ -185,12 +185,12 @@ return jsl.stdin.end(); }; printTokens = function(tokens) { - var strings, tag, token, value, _i, _len, _ref2, _result; + var strings, tag, token, value, _i, _len, _ref, _result; strings = (function() { _result = []; for (_i = 0, _len = tokens.length; _i < _len; _i++) { token = tokens[_i]; - _ref2 = [token[0], token[1].toString().replace(/\n/, '\\n')], tag = _ref2[0], value = _ref2[1]; + _ref = [token[0], token[1].toString().replace(/\n/, '\\n')], tag = _ref[0], value = _ref[1]; _result.push("[" + tag + " " + value + "]"); } return _result; diff --git a/lib/lexer.js b/lib/lexer.js index 99fbc691..df661f59 100644 --- a/lib/lexer.js +++ b/lib/lexer.js @@ -38,7 +38,7 @@ return (new Rewriter).rewrite(this.tokens); }; Lexer.prototype.identifierToken = function() { - var colon, forcedIdentifier, id, input, match, prev, tag, _ref2, _ref3; + var colon, forcedIdentifier, id, input, match, prev, tag, _ref, _ref2; if (!(match = IDENTIFIER.exec(this.chunk))) { return 0; } @@ -58,11 +58,11 @@ this.token('TO', id); return id.length; } - forcedIdentifier = colon || (prev = last(this.tokens)) && !prev.spaced && ((_ref2 = prev[0]) === '.' || _ref2 === '?.' || _ref2 === '@' || _ref2 === '::'); + forcedIdentifier = colon || (prev = last(this.tokens)) && !prev.spaced && ((_ref = prev[0]) === '.' || _ref === '?.' || _ref === '@' || _ref === '::'); tag = 'IDENTIFIER'; if (__indexOf.call(JS_KEYWORDS, id) >= 0 || !forcedIdentifier && __indexOf.call(COFFEE_KEYWORDS, id) >= 0) { tag = id.toUpperCase(); - if (tag === 'WHEN' && (_ref3 = this.tag(), __indexOf.call(LINE_BREAK, _ref3) >= 0)) { + if (tag === 'WHEN' && (_ref2 = this.tag(), __indexOf.call(LINE_BREAK, _ref2) >= 0)) { tag = 'LEADING_WHEN'; } else if (tag === 'FOR') { this.seenFor = true; @@ -182,14 +182,14 @@ return script.length; }; Lexer.prototype.regexToken = function() { - var match, regex, _ref2; + var match, regex, _ref; if (this.chunk.charAt(0) !== '/') { return 0; } if (match = HEREGEX.exec(this.chunk)) { return this.heregexToken(match); } - if (_ref2 = this.tag(), __indexOf.call(NOT_REGEX, _ref2) >= 0) { + if (_ref = this.tag(), __indexOf.call(NOT_REGEX, _ref) >= 0) { return 0; } if (!(match = REGEX.exec(this.chunk))) { @@ -200,7 +200,7 @@ return regex.length; }; Lexer.prototype.heregexToken = function(match) { - var body, flags, heregex, re, tag, tokens, value, _i, _len, _ref2, _ref3, _this; + var body, flags, heregex, re, tag, tokens, value, _i, _len, _ref, _ref2, _this; heregex = match[0], body = match[1], flags = match[2]; if (0 > body.indexOf('#{')) { re = body.replace(HEREGEX_OMIT, '').replace(/\//g, '\\/'); @@ -213,9 +213,9 @@ for (_i = 0, _len = this.interpolateString(body, { regex: true }).length; _i < _len; _i++) { - _ref2 = this.interpolateString(body, { + _ref = this.interpolateString(body, { regex: true - })[_i], tag = _ref2[0], value = _ref2[1]; + })[_i], tag = _ref[0], value = _ref[1]; if (tag === 'TOKENS') { tokens.push.apply(tokens, value); } else { @@ -228,7 +228,7 @@ tokens.push(['+', '+']); } tokens.pop(); - if (((_ref3 = tokens[0]) != null ? _ref3[0] : void 0) !== 'STRING') { + if (((_ref2 = tokens[0]) != null ? _ref2[0] : void 0) !== 'STRING') { this.tokens.push(['STRING', '""'], ['+', '+']); } (_this = this.tokens).push.apply(_this, tokens); @@ -324,7 +324,7 @@ return this; }; Lexer.prototype.literalToken = function() { - var match, prev, tag, value, _ref2, _ref3, _ref4, _ref5; + var match, prev, tag, value, _ref, _ref2, _ref3, _ref4; if (match = OPERATOR.exec(this.chunk)) { value = match[0]; if (CODE.test(value)) { @@ -336,10 +336,10 @@ tag = value; prev = last(this.tokens); if (value === '=' && prev) { - if (!prev[1].reserved && (_ref2 = prev[1], __indexOf.call(JS_FORBIDDEN, _ref2) >= 0)) { + if (!prev[1].reserved && (_ref = prev[1], __indexOf.call(JS_FORBIDDEN, _ref) >= 0)) { this.assignmentError(); } - if ((_ref3 = prev[1]) === '||' || _ref3 === '&&') { + if ((_ref2 = prev[1]) === '||' || _ref2 === '&&') { prev[0] = 'COMPOUND_ASSIGN'; prev[1] += '='; return value.length; @@ -360,12 +360,12 @@ } else if (__indexOf.call(LOGIC, value) >= 0 || value === '?' && (prev != null ? prev.spaced : void 0)) { tag = 'LOGIC'; } else if (prev && !prev.spaced) { - if (value === '(' && (_ref4 = prev[0], __indexOf.call(CALLABLE, _ref4) >= 0)) { + if (value === '(' && (_ref3 = prev[0], __indexOf.call(CALLABLE, _ref3) >= 0)) { if (prev[0] === '?') { prev[0] = 'FUNC_EXIST'; } tag = 'CALL_START'; - } else if (value === '[' && (_ref5 = prev[0], __indexOf.call(INDEXABLE, _ref5) >= 0)) { + } else if (value === '[' && (_ref4 = prev[0], __indexOf.call(INDEXABLE, _ref4) >= 0)) { tag = 'INDEX_START'; switch (prev[0]) { case '?': @@ -380,7 +380,7 @@ return value.length; }; Lexer.prototype.sanitizeHeredoc = function(doc, options) { - var attempt, herecomment, indent, match, _ref2; + var attempt, herecomment, indent, match, _ref; indent = options.indent, herecomment = options.herecomment; if (herecomment && 0 > doc.indexOf('\n')) { return doc; @@ -388,7 +388,7 @@ if (!herecomment) { while (match = HEREDOC_INDENT.exec(doc)) { attempt = match[1]; - if (indent === null || (0 < (_ref2 = attempt.length) && _ref2 < indent.length)) { + if (indent === null || (0 < (_ref = attempt.length) && _ref < indent.length)) { indent = attempt; } } @@ -475,7 +475,7 @@ return i && str.slice(0, i); }; Lexer.prototype.interpolateString = function(str, options) { - var expr, heredoc, i, inner, interpolated, letter, nested, pi, regex, tag, tokens, value, _len, _ref2, _this; + var expr, heredoc, i, inner, interpolated, letter, nested, pi, regex, tag, tokens, value, _len, _ref, _this; options == null && (options = {}); heredoc = options.heredoc, regex = options.regex; tokens = []; @@ -524,7 +524,7 @@ this.token('(', '('); } for (i = 0, _len = tokens.length; i < _len; i++) { - _ref2 = tokens[i], tag = _ref2[0], value = _ref2[1]; + _ref = tokens[i], tag = _ref[0], value = _ref[1]; if (i) { this.token('+', '+'); } diff --git a/lib/nodes.js b/lib/nodes.js index b6731a2d..691b47d6 100644 --- a/lib/nodes.js +++ b/lib/nodes.js @@ -57,9 +57,7 @@ Base.prototype.compileLoopReference = function(o, name) { var src, tmp; src = tmp = this.compile(o, LEVEL_LIST); - if (!(NUMBER.test(src) || IDENTIFIER.test(src) && o.scope.check(src, { - immediate: true - }))) { + if (!(NUMBER.test(src) || IDENTIFIER.test(src) && o.scope.check(src, true))) { src = "" + (tmp = o.scope.freeVariable(name)) + " = " + src; } return [src, tmp]; @@ -92,13 +90,13 @@ }); }; Base.prototype.toString = function(idt, override) { - var child, children, klass, _i, _len, _ref2, _result; + var child, children, klass, _i, _len, _ref, _result; idt == null && (idt = ''); children = ((function() { - _ref2 = this.collectChildren(); + _ref = this.collectChildren(); _result = []; - for (_i = 0, _len = _ref2.length; _i < _len; _i++) { - child = _ref2[_i]; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + child = _ref[_i]; _result.push(child.toString(idt + TAB)); } return _result; @@ -107,17 +105,17 @@ return '\n' + idt + klass + children; }; Base.prototype.eachChild = function(func) { - var attr, child, _i, _j, _len, _len2, _ref2, _ref3; + var attr, child, _i, _j, _len, _len2, _ref, _ref2; if (!this.children) { return this; } - _ref2 = this.children; - for (_i = 0, _len = _ref2.length; _i < _len; _i++) { - attr = _ref2[_i]; + _ref = this.children; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + attr = _ref[_i]; if (this[attr]) { - _ref3 = flatten([this[attr]]); - for (_j = 0, _len2 = _ref3.length; _j < _len2; _j++) { - child = _ref3[_j]; + _ref2 = flatten([this[attr]]); + for (_j = 0, _len2 = _ref2.length; _j < _len2; _j++) { + child = _ref2[_j]; if (func(child) === false) { return this; } @@ -193,10 +191,10 @@ return !this.expressions.length; }; Expressions.prototype.makeReturn = function() { - var end, idx, _ref2; - _ref2 = this.expressions; - for (idx = _ref2.length - 1; idx >= 0; idx--) { - end = _ref2[idx]; + var end, idx, _ref; + _ref = this.expressions; + for (idx = _ref.length - 1; idx >= 0; idx--) { + end = _ref[idx]; if (!(end instanceof Comment)) { this.expressions[idx] = end.makeReturn(); break; @@ -209,13 +207,13 @@ return o.scope ? Expressions.__super__.compile.call(this, o, level) : this.compileRoot(o); }; Expressions.prototype.compileNode = function(o) { - var node, _i, _len, _ref2, _result; + var node, _i, _len, _ref, _result; this.tab = o.indent; return ((function() { - _ref2 = this.expressions; + _ref = this.expressions; _result = []; - for (_i = 0, _len = _ref2.length; _i < _len; _i++) { - node = _ref2[_i]; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + node = _ref[_i]; _result.push(this.compileExpression(node, o)); } return _result; @@ -272,8 +270,8 @@ return this.isStatement() ? this : Literal.__super__.makeReturn.call(this); }; Literal.prototype.isPureStatement = function() { - var _ref2; - return (_ref2 = this.value) === 'break' || _ref2 === 'continue' || _ref2 === 'debugger'; + var _ref; + return (_ref = this.value) === 'break' || _ref === 'continue' || _ref === 'debugger'; }; Literal.prototype.isAssignable = function() { return IDENTIFIER.test(this.value); @@ -304,8 +302,8 @@ Return.prototype.isPureStatement = YES; Return.prototype.makeReturn = THIS; Return.prototype.compile = function(o, level) { - var expr, _ref2; - expr = (_ref2 = this.expression) != null ? _ref2.makeReturn() : void 0; + var expr, _ref; + expr = (_ref = this.expression) != null ? _ref.makeReturn() : void 0; return expr && !(expr instanceof Return) ? expr.compile(o, level) : Return.__super__.compile.call(this, o, level); }; Return.prototype.compileNode = function(o) { @@ -354,10 +352,10 @@ return this.base instanceof Literal && SIMPLENUM.test(this.base.value); }; Value.prototype.isAtomic = function() { - var node, _i, _len, _ref2; - _ref2 = this.properties.concat(this.base); - for (_i = 0, _len = _ref2.length; _i < _len; _i++) { - node = _ref2[_i]; + var node, _i, _len, _ref; + _ref = this.properties.concat(this.base); + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + node = _ref[_i]; if (node.soak || node instanceof Call) { return false; } @@ -412,14 +410,14 @@ return code; }; Value.prototype.unfoldSoak = function(o) { - var fst, i, ifn, prop, ref, snd, _len, _ref2; + var fst, i, ifn, prop, ref, snd, _len, _ref; if (ifn = this.base.unfoldSoak(o)) { Array.prototype.push.apply(ifn.body.properties, this.properties); return ifn; } - _ref2 = this.properties; - for (i = 0, _len = _ref2.length; i < _len; i++) { - prop = _ref2[i]; + _ref = this.properties; + for (i = 0, _len = _ref.length; i < _len; i++) { + prop = _ref[i]; if (prop.soak) { prop.soak = false; fst = new Value(this.base, this.properties.slice(0, i)); @@ -486,13 +484,13 @@ return method.klass ? "" + method.klass + ".__super__." + name : "" + name + ".__super__.constructor"; }; Call.prototype.unfoldSoak = function(o) { - var call, ifn, left, list, rite, _i, _len, _ref2, _ref3; + var call, ifn, left, list, rite, _i, _len, _ref, _ref2; if (this.soak) { if (this.variable) { if (ifn = If.unfoldSoak(o, this, 'variable')) { return ifn; } - _ref2 = new Value(this.variable).cacheReference(o), left = _ref2[0], rite = _ref2[1]; + _ref = new Value(this.variable).cacheReference(o), left = _ref[0], rite = _ref[1]; } else { left = new Literal(this.superReference(o)); rite = new Value(left); @@ -520,9 +518,9 @@ break; } } - _ref3 = list.reverse(); - for (_i = 0, _len = _ref3.length; _i < _len; _i++) { - call = _ref3[_i]; + _ref2 = list.reverse(); + for (_i = 0, _len = _ref2.length; _i < _len; _i++) { + call = _ref2[_i]; if (ifn) { if (call.variable instanceof Call) { call.variable = ifn; @@ -535,18 +533,18 @@ return ifn; }; Call.prototype.compileNode = function(o) { - var arg, args, code, _i, _len, _ref2, _ref3, _result; - if ((_ref2 = this.variable) != null) { - _ref2.front = this.front; + var arg, args, code, _i, _len, _ref, _ref2, _result; + if ((_ref = this.variable) != null) { + _ref.front = this.front; } if (code = Splat.compileSplattedArray(o, this.args, true)) { return this.compileSplat(o, code); } args = ((function() { - _ref3 = this.args; + _ref2 = this.args; _result = []; - for (_i = 0, _len = _ref3.length; _i < _len; _i++) { - arg = _ref3[_i]; + for (_i = 0, _len = _ref2.length; _i < _len; _i++) { + arg = _ref2[_i]; _result.push(arg.compile(o, LEVEL_LIST)); } return _result; @@ -644,10 +642,10 @@ __extends(Obj, Base); Obj.prototype.children = ['properties']; Obj.prototype.compileNode = function(o) { - var i, idt, indent, join, lastNoncom, nonComments, obj, prop, props, rest, _i, _len, _len2, _ref2, _ref3, _result; - _ref2 = this.properties; - for (i = 0, _len = _ref2.length; i < _len; i++) { - prop = _ref2[i]; + var i, idt, indent, join, lastNoncom, nonComments, obj, prop, props, rest, _i, _len, _len2, _ref, _ref2, _result; + _ref = this.properties; + for (i = 0, _len = _ref.length; i < _len; i++) { + prop = _ref[i]; if ((prop.variable || prop).base instanceof Parens) { rest = this.properties.splice(i); break; @@ -655,10 +653,10 @@ } o.indent = idt = this.idt(1); nonComments = ((function() { - _ref3 = this.properties; + _ref2 = this.properties; _result = []; - for (_i = 0, _len2 = _ref3.length; _i < _len2; _i++) { - prop = _ref3[_i]; + for (_i = 0, _len2 = _ref2.length; _i < _len2; _i++) { + prop = _ref2[_i]; if (!(prop instanceof Comment)) { _result.push(prop); } @@ -667,10 +665,10 @@ }).call(this)); lastNoncom = last(nonComments); props = (function() { - _ref3 = this.properties; + _ref2 = this.properties; _result = []; - for (i = 0, _len2 = _ref3.length; i < _len2; i++) { - prop = _ref3[i]; + for (i = 0, _len2 = _ref2.length; i < _len2; i++) { + prop = _ref2[i]; join = i === this.properties.length - 1 ? '' : prop === lastNoncom || prop instanceof Comment ? '\n' : ',\n'; indent = prop instanceof Comment ? '' : idt; if (prop instanceof Value && prop["this"]) { @@ -690,7 +688,7 @@ return this.front ? "(" + obj + ")" : obj; }; Obj.prototype.compileDynamic = function(o, code, props) { - var acc, i, key, oref, prop, ref, val, _len, _ref2; + var acc, i, key, oref, prop, ref, val, _len, _ref; code = "" + (oref = o.scope.freeVariable('obj')) + " = " + code + ", "; for (i = 0, _len = props.length; i < _len; i++) { prop = props[i]; @@ -704,7 +702,7 @@ val = prop.value.compile(o, LEVEL_LIST); } else { acc = prop.base; - _ref2 = acc.cache(o, LEVEL_LIST, ref), key = _ref2[0], val = _ref2[1]; + _ref = acc.cache(o, LEVEL_LIST, ref), key = _ref[0], val = _ref[1]; if (key !== val) { ref = val; } @@ -716,10 +714,10 @@ return o.level <= LEVEL_PAREN ? code : "(" + code + ")"; }; Obj.prototype.assigns = function(name) { - var prop, _i, _len, _ref2; - _ref2 = this.properties; - for (_i = 0, _len = _ref2.length; _i < _len; _i++) { - prop = _ref2[_i]; + var prop, _i, _len, _ref; + _ref = this.properties; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + prop = _ref[_i]; if (prop.assigns(name)) { return true; } @@ -739,15 +737,15 @@ __extends(Arr, Base); Arr.prototype.children = ['objects']; Arr.prototype.compileNode = function(o) { - var code, i, obj, objects, _len, _ref2; + var code, i, obj, objects, _len, _ref; o.indent = this.idt(1); if (code = Splat.compileSplattedArray(o, this.objects)) { return code; } objects = []; - _ref2 = this.objects; - for (i = 0, _len = _ref2.length; i < _len; i++) { - obj = _ref2[i]; + _ref = this.objects; + for (i = 0, _len = _ref.length; i < _len; i++) { + obj = _ref[i]; code = obj.compile(o, LEVEL_LIST); objects.push((obj instanceof Comment ? "\n" + code + "\n" + o.indent : i === this.objects.length - 1 ? code : code + ', ')); } @@ -755,10 +753,10 @@ return 0 < objects.indexOf('\n') ? "[\n" + o.indent + objects + "\n" + this.tab + "]" : "[" + objects + "]"; }; Arr.prototype.assigns = function(name) { - var obj, _i, _len, _ref2; - _ref2 = this.objects; - for (_i = 0, _len = _ref2.length; _i < _len; _i++) { - obj = _ref2[_i]; + var obj, _i, _len, _ref; + _ref = this.objects; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + obj = _ref[_i]; if (obj.assigns(name)) { return true; } @@ -785,7 +783,7 @@ return this; }; Class.prototype.compileNode = function(o) { - var access, applied, apply, className, code, constScope, ctor, extension, func, me, pname, prop, props, pvar, ref, ret, val, variable, _i, _len, _ref2, _ref3; + var access, applied, apply, className, code, constScope, ctor, extension, func, me, pname, prop, props, pvar, ref, ret, val, variable, _i, _len, _ref, _ref2; variable = this.variable || new Literal(o.scope.freeVariable('ctor')); extension = this.parent && new Extends(variable, this.parent); props = new Expressions; @@ -798,13 +796,13 @@ } else { ctor = new Code([], new Expressions([new Return(new Literal('this'))])); } - _ref2 = this.properties; - for (_i = 0, _len = _ref2.length; _i < _len; _i++) { - prop = _ref2[_i]; + _ref = this.properties; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + prop = _ref[_i]; pvar = prop.variable, func = prop.value; if (pvar && pvar.base.value === 'constructor') { if (!(func instanceof Code)) { - _ref3 = func.cache(o), func = _ref3[0], ref = _ref3[1]; + _ref2 = func.cache(o), func = _ref2[0], ref = _ref2[1]; if (func !== ref) { props.push(func); } @@ -885,12 +883,12 @@ return If.unfoldSoak(o, this, 'variable'); }; Assign.prototype.compileNode = function(o) { - var isValue, match, name, val, _ref2; + var isValue, match, name, val, _ref; if (isValue = this.variable instanceof Value) { if (this.variable.isArray() || this.variable.isObject()) { return this.compilePatternMatch(o); } - if ((_ref2 = this.context) === '||=' || _ref2 === '&&=' || _ref2 === '?=') { + if ((_ref = this.context) === '||=' || _ref === '&&=' || _ref === '?=') { return this.compileConditional(o); } } @@ -913,7 +911,7 @@ return o.level <= LEVEL_LIST ? val : "(" + val + ")"; }; Assign.prototype.compilePatternMatch = function(o) { - var acc, assigns, code, i, idx, isObject, ivar, obj, objects, olen, ref, rest, splat, top, val, value, vvar, _len, _ref2, _ref3, _ref4, _ref5, _ref6, _ref7; + var acc, assigns, code, i, idx, isObject, ivar, obj, objects, olen, ref, rest, splat, top, val, value, vvar, _len, _ref, _ref2, _ref3, _ref4, _ref5, _ref6; top = o.level === LEVEL_TOP; value = this.value; objects = this.variable.base.objects; @@ -923,10 +921,10 @@ isObject = this.variable.isObject(); if (top && olen === 1 && !((obj = objects[0]) instanceof Splat)) { if (obj instanceof Assign) { - _ref2 = obj, (_ref3 = _ref2.variable, idx = _ref3.base, _ref3), obj = _ref2.value; + _ref = obj, (_ref2 = _ref.variable, idx = _ref2.base, _ref2), obj = _ref.value; } else { if (obj.base instanceof Parens) { - _ref4 = new Value(obj.unwrapAll()).cacheReference(o), obj = _ref4[0], idx = _ref4[1]; + _ref3 = new Value(obj.unwrapAll()).cacheReference(o), obj = _ref3[0], idx = _ref3[1]; } else { idx = isObject ? obj["this"] ? obj.properties[0].name : obj : new Literal(0); } @@ -948,10 +946,10 @@ idx = i; if (isObject) { if (obj instanceof Assign) { - _ref5 = obj, (_ref6 = _ref5.variable, idx = _ref6.base, _ref6), obj = _ref5.value; + _ref4 = obj, (_ref5 = _ref4.variable, idx = _ref5.base, _ref5), obj = _ref4.value; } else { if (obj.base instanceof Parens) { - _ref7 = new Value(obj.unwrapAll()).cacheReference(o), obj = _ref7[0], idx = _ref7[1]; + _ref6 = new Value(obj.unwrapAll()).cacheReference(o), obj = _ref6[0], idx = _ref6[1]; } else { idx = obj["this"] ? obj.properties[0].name : obj; } @@ -989,8 +987,8 @@ return o.level < LEVEL_LIST ? code : "(" + code + ")"; }; Assign.prototype.compileConditional = function(o) { - var left, rite, _ref2; - _ref2 = this.variable.cacheReference(o), left = _ref2[0], rite = _ref2[1]; + var left, rite, _ref; + _ref = this.variable.cacheReference(o), left = _ref[0], rite = _ref[1]; return new Op(this.context.slice(0, -1), left, new Assign(rite, this.value, '=')).compile(o); }; return Assign; @@ -1011,7 +1009,7 @@ __extends(Code, Base); Code.prototype.children = ['params', 'body']; Code.prototype.compileNode = function(o) { - var close, code, comm, exprs, func, i, idt, lit, open, p, param, ref, scope, sharedScope, splats, v, val, vars, wasEmpty, _i, _j, _len, _len2, _len3, _ref2, _ref3, _result, _this; + var close, code, comm, exprs, func, i, idt, lit, open, p, param, ref, scope, sharedScope, splats, v, val, vars, wasEmpty, _i, _j, _len, _len2, _len3, _ref, _ref2, _result, _this; sharedScope = del(o, 'sharedScope'); o.scope = scope = sharedScope || new Scope(o.scope, this.body, this); o.indent = this.idt(1); @@ -1019,15 +1017,15 @@ delete o.globals; vars = []; exprs = []; - _ref2 = this.params; - for (_i = 0, _len = _ref2.length; _i < _len; _i++) { - param = _ref2[_i]; + _ref = this.params; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + param = _ref[_i]; if (param.splat) { splats = new Assign(new Value(new Arr((function() { - _ref3 = this.params; + _ref2 = this.params; _result = []; - for (_j = 0, _len2 = _ref3.length; _j < _len2; _j++) { - p = _ref3[_j]; + for (_j = 0, _len2 = _ref2.length; _j < _len2; _j++) { + p = _ref2[_j]; _result.push(p.asReference(o)); } return _result; @@ -1035,9 +1033,9 @@ break; } } - _ref3 = this.params; - for (_j = 0, _len2 = _ref3.length; _j < _len2; _j++) { - param = _ref3[_j]; + _ref2 = this.params; + for (_j = 0, _len2 = _ref2.length; _j < _len2; _j++) { + param = _ref2[_j]; if (param.isComplex()) { val = ref = param.asReference(o); if (param.value) { @@ -1148,7 +1146,7 @@ return this.index != null ? this.compileParam(o) : this.name.compile(o); }; Splat.compileSplattedArray = function(o, list, apply) { - var args, base, code, i, index, node, _i, _len, _len2, _ref2, _result; + var args, base, code, i, index, node, _i, _len, _len2, _ref, _result; index = -1; while ((node = list[++index]) && !(node instanceof Splat)) { continue; @@ -1173,10 +1171,10 @@ return args[0] + (".concat(" + (args.slice(1).join(', ')) + ")"); } base = ((function() { - _ref2 = list.slice(0, index); + _ref = list.slice(0, index); _result = []; - for (_i = 0, _len2 = _ref2.length; _i < _len2; _i++) { - node = _ref2[_i]; + for (_i = 0, _len2 = _ref.length; _i < _len2; _i++) { + node = _ref[_i]; _result.push(node.compile(o, LEVEL_LIST)); } return _result; @@ -1206,10 +1204,10 @@ return this; }; While.prototype.containsPureStatement = function() { - var expressions, i, ret, _ref2; + var expressions, i, ret, _ref; expressions = this.body.expressions; i = expressions.length; - if ((_ref2 = expressions[--i]) != null ? _ref2.containsPureStatement() : void 0) { + if ((_ref = expressions[--i]) != null ? _ref.containsPureStatement() : void 0) { return true; } ret = function(node) { @@ -1288,8 +1286,8 @@ return !this.second; }; Op.prototype.isChainable = function() { - var _ref2; - return (_ref2 = this.operator) === '<' || _ref2 === '>' || _ref2 === '>=' || _ref2 === '<=' || _ref2 === '===' || _ref2 === '!=='; + var _ref; + return (_ref = this.operator) === '<' || _ref === '>' || _ref === '>=' || _ref === '<=' || _ref === '===' || _ref === '!=='; }; Op.prototype.invert = function() { var op; @@ -1299,8 +1297,8 @@ } else return this.second ? new Parens(this).invert() : Op.__super__.invert.call(this); }; Op.prototype.unfoldSoak = function(o) { - var _ref2; - return ((_ref2 = this.operator) === '++' || _ref2 === '--' || _ref2 === 'delete') && If.unfoldSoak(o, this, 'first'); + var _ref; + return ((_ref = this.operator) === '++' || _ref === '--' || _ref === 'delete') && If.unfoldSoak(o, this, 'first'); }; Op.prototype.compileNode = function(o) { if (this.isUnary()) { @@ -1316,8 +1314,8 @@ return "" + (this.first.compile(o, LEVEL_OP)) + " " + this.operator + " " + (this.second.compile(o, LEVEL_OP)); }; Op.prototype.compileChain = function(o) { - var code, fst, shared, _ref2; - _ref2 = this.first.second.cache(o), this.first.second = _ref2[0], shared = _ref2[1]; + var code, fst, shared, _ref; + _ref = this.first.second.cache(o), this.first.second = _ref[0], shared = _ref[1]; fst = this.first.compile(o, LEVEL_OP); if (fst.charAt(0) === '(') { fst = fst.slice(1, -1); @@ -1372,14 +1370,14 @@ return this.array instanceof Value && this.array.isArray() ? this.compileOrTest(o) : this.compileLoopTest(o); }; In.prototype.compileOrTest = function(o) { - var cmp, cnj, i, item, ref, sub, tests, _len, _ref2, _ref3, _ref4, _result; - _ref2 = this.object.cache(o, LEVEL_OP), sub = _ref2[0], ref = _ref2[1]; - _ref3 = this.negated ? [' !== ', ' && '] : [' === ', ' || '], cmp = _ref3[0], cnj = _ref3[1]; + var cmp, cnj, i, item, ref, sub, tests, _len, _ref, _ref2, _ref3, _result; + _ref = this.object.cache(o, LEVEL_OP), sub = _ref[0], ref = _ref[1]; + _ref2 = this.negated ? [' !== ', ' && '] : [' === ', ' || '], cmp = _ref2[0], cnj = _ref2[1]; tests = (function() { - _ref4 = this.array.base.objects; + _ref3 = this.array.base.objects; _result = []; - for (i = 0, _len = _ref4.length; i < _len; i++) { - item = _ref4[i]; + for (i = 0, _len = _ref3.length; i < _len; i++) { + item = _ref3[i]; _result.push((i ? ref : sub) + cmp + item.compile(o)); } return _result; @@ -1388,8 +1386,8 @@ return o.level < LEVEL_OP ? tests : "(" + tests + ")"; }; In.prototype.compileLoopTest = function(o) { - var code, ref, sub, _ref2; - _ref2 = this.object.cache(o, LEVEL_LIST), sub = _ref2[0], ref = _ref2[1]; + var code, ref, sub, _ref; + _ref = this.object.cache(o, LEVEL_LIST), sub = _ref[0], ref = _ref[1]; code = utility('indexOf') + (".call(" + (this.array.compile(o)) + ", " + ref + ") ") + (this.negated ? '< 0' : '>= 0'); if (sub === ref) { return code; @@ -1536,29 +1534,25 @@ return ''; }; For.prototype.compileNode = function(o) { - var body, code, cond, defPart, forPart, guardPart, idt, index, ivar, lvar, name, namePart, pvar, retPart, rvar, scope, sourcePart, step, svar, tail, tvar, varPart, vars, _ref2, _ref3, _ref4, _ref5, _ref6; + var body, code, cond, defPart, forPart, guardPart, idt, index, ivar, lvar, name, namePart, pvar, retPart, rvar, scope, sourcePart, step, svar, tail, tvar, varPart, vars, _ref, _ref2, _ref3, _ref4, _ref5; scope = o.scope; body = this.body; - name = !this.pattern && ((_ref2 = this.name) != null ? _ref2.compile(o) : void 0); - index = (_ref3 = this.index) != null ? _ref3.compile(o) : void 0; + name = !this.pattern && ((_ref = this.name) != null ? _ref.compile(o) : void 0); + index = (_ref2 = this.index) != null ? _ref2.compile(o) : void 0; ivar = !index ? scope.freeVariable('i') : index; varPart = guardPart = defPart = retPart = ''; idt = this.idt(1); if (name) { - scope.find(name, { - immediate: true - }); + scope.find(name, true); } if (index) { - scope.find(index, { - immediate: true - }); + scope.find(index, true); } if (this.step) { - _ref4 = this.step.compileLoopReference(o, 'step'), step = _ref4[0], pvar = _ref4[1]; + _ref3 = this.step.compileLoopReference(o, 'step'), step = _ref3[0], pvar = _ref3[1]; } if (this.from) { - _ref5 = this.to.compileLoopReference(o, 'to'), tail = _ref5[0], tvar = _ref5[1]; + _ref4 = this.to.compileLoopReference(o, 'to'), tail = _ref4[0], tvar = _ref4[1]; vars = ivar + ' = ' + this.from.compile(o); if (tail !== tvar) { vars += ', ' + tail; @@ -1566,7 +1560,7 @@ cond = +pvar ? "" + ivar + " " + (pvar < 0 ? '>' : '<') + "= " + tvar : "" + pvar + " < 0 ? " + ivar + " >= " + tvar + " : " + ivar + " <= " + tvar; } else { if (name || this.object && !this.raw) { - _ref6 = this.source.compileLoopReference(o, 'ref'), sourcePart = _ref6[0], svar = _ref6[1]; + _ref5 = this.source.compileLoopReference(o, 'ref'), sourcePart = _ref5[0], svar = _ref5[1]; } else { sourcePart = svar = this.source.compile(o, LEVEL_PAREN); } @@ -1641,27 +1635,27 @@ Switch.prototype.children = ['subject', 'cases', 'otherwise']; Switch.prototype.isStatement = YES; Switch.prototype.makeReturn = function() { - var pair, _i, _len, _ref2, _ref3; - _ref2 = this.cases; - for (_i = 0, _len = _ref2.length; _i < _len; _i++) { - pair = _ref2[_i]; + var pair, _i, _len, _ref, _ref2; + _ref = this.cases; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + pair = _ref[_i]; pair[1].makeReturn(); } - if ((_ref3 = this.otherwise) != null) { - _ref3.makeReturn(); + if ((_ref2 = this.otherwise) != null) { + _ref2.makeReturn(); } return this; }; Switch.prototype.compileNode = function(o) { - var block, body, code, cond, conditions, expr, i, idt1, idt2, _i, _j, _len, _len2, _ref2, _ref3, _ref4, _ref5; + var block, body, code, cond, conditions, expr, i, idt1, idt2, _i, _j, _len, _len2, _ref, _ref2, _ref3, _ref4; idt1 = this.idt(1); idt2 = o.indent = this.idt(2); - code = this.tab + ("switch (" + (((_ref2 = this.subject) != null ? _ref2.compile(o, LEVEL_PAREN) : void 0) || false) + ") {\n"); + code = this.tab + ("switch (" + (((_ref = this.subject) != null ? _ref.compile(o, LEVEL_PAREN) : void 0) || false) + ") {\n"); for (i = 0, _len = this.cases.length; i < _len; i++) { - _ref3 = this.cases[i], conditions = _ref3[0], block = _ref3[1]; - _ref4 = flatten([conditions]); - for (_i = 0, _len2 = _ref4.length; _i < _len2; _i++) { - cond = _ref4[_i]; + _ref2 = this.cases[i], conditions = _ref2[0], block = _ref2[1]; + _ref3 = flatten([conditions]); + for (_i = 0, _len2 = _ref3.length; _i < _len2; _i++) { + cond = _ref3[_i]; if (!this.subject) { cond = cond.invert(); } @@ -1673,9 +1667,9 @@ if (i === this.cases.length - 1 && !this.otherwise) { break; } - _ref5 = block.expressions; - for (_j = _ref5.length - 1; _j >= 0; _j--) { - expr = _ref5[_j]; + _ref4 = block.expressions; + for (_j = _ref4.length - 1; _j >= 0; _j--) { + expr = _ref4[_j]; if (!(expr instanceof Comment)) { if (!(expr instanceof Return)) { code += idt2 + 'break;\n'; @@ -1707,12 +1701,12 @@ __extends(If, Base); If.prototype.children = ['condition', 'body', 'elseBody']; If.prototype.bodyNode = function() { - var _ref2; - return (_ref2 = this.body) != null ? _ref2.unwrap() : void 0; + var _ref; + return (_ref = this.body) != null ? _ref.unwrap() : void 0; }; If.prototype.elseBodyNode = function() { - var _ref2; - return (_ref2 = this.elseBody) != null ? _ref2.unwrap() : void 0; + var _ref; + return (_ref = this.elseBody) != null ? _ref.unwrap() : void 0; }; If.prototype.addElse = function(elseBody) { if (this.isChain) { @@ -1724,8 +1718,8 @@ return this; }; If.prototype.isStatement = function(o) { - var _ref2; - return (o != null ? o.level : void 0) === LEVEL_TOP || this.bodyNode().isStatement(o) || ((_ref2 = this.elseBodyNode()) != null ? _ref2.isStatement(o) : void 0); + var _ref; + return (o != null ? o.level : void 0) === LEVEL_TOP || this.bodyNode().isStatement(o) || ((_ref = this.elseBodyNode()) != null ? _ref.isStatement(o) : void 0); }; If.prototype.compileNode = function(o) { return this.isStatement(o) ? this.compileStatement(o) : this.compileExpression(o); diff --git a/lib/rewriter.js b/lib/rewriter.js index 85db4a7f..40b5cc2d 100644 --- a/lib/rewriter.js +++ b/lib/rewriter.js @@ -205,7 +205,7 @@ } tokens.splice(i, 0, ['CALL_START', '(', token[2]]); this.detectEnd(i + (callObject ? 2 : 1), function(token, i) { - var post, _ref3; + var post, _ref; if (!seenSingle && token.fromThen) { return true; } @@ -216,7 +216,7 @@ if ((tag === '.' || tag === '?.' || tag === '::') && this.tag(i - 1) === 'OUTDENT') { return true; } - return !token.generated && this.tag(i - 1) !== ',' && __indexOf.call(IMPLICIT_END, tag) >= 0 && (tag !== 'INDENT' || (this.tag(i - 2) !== 'CLASS' && (_ref3 = this.tag(i - 1), __indexOf.call(IMPLICIT_BLOCK, _ref3) < 0) && !((post = this.tokens[i + 1]) && post.generated && post[0] === '{'))); + return !token.generated && this.tag(i - 1) !== ',' && __indexOf.call(IMPLICIT_END, tag) >= 0 && (tag !== 'INDENT' || (this.tag(i - 2) !== 'CLASS' && (_ref = this.tag(i - 1), __indexOf.call(IMPLICIT_BLOCK, _ref) < 0) && !((post = this.tokens[i + 1]) && post.generated && post[0] === '{'))); }, action); if (prev[0] === '?') { prev[0] = 'FUNC_EXIST'; @@ -249,8 +249,8 @@ indent.generated = outdent.generated = true; tokens.splice(i + 1, 0, indent); condition = function(token, i) { - var _ref3; - return token[1] !== ';' && (_ref3 = token[0], __indexOf.call(SINGLE_CLOSERS, _ref3) >= 0) && !(token[0] === 'ELSE' && (starter !== 'IF' && starter !== 'THEN')); + var _ref; + return token[1] !== ';' && (_ref = token[0], __indexOf.call(SINGLE_CLOSERS, _ref) >= 0) && !(token[0] === 'ELSE' && (starter !== 'IF' && starter !== 'THEN')); }; action = function(token, i) { return this.tokens.splice((this.tag(i - 1) === ',' ? i - 1 : i), 0, outdent); diff --git a/lib/scope.js b/lib/scope.js index 31665033..1eba792d 100644 --- a/lib/scope.js +++ b/lib/scope.js @@ -37,10 +37,10 @@ return this; }; Scope.prototype.endLevel = function() { - var name, _i, _len, _ref2; - _ref2 = this.garbage.pop(); - for (_i = 0, _len = _ref2.length; _i < _len; _i++) { - name = _ref2[_i]; + var name, _i, _len, _ref; + _ref = this.garbage.pop(); + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + name = _ref[_i]; if (this.type(name) === 'var') { this.add(name, 'reuse'); } @@ -55,10 +55,10 @@ return false; }; Scope.prototype.any = function(fn) { - var v, _i, _len, _ref2; - _ref2 = this.variables; - for (_i = 0, _len = _ref2.length; _i < _len; _i++) { - v = _ref2[_i]; + var v, _i, _len, _ref; + _ref = this.variables; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + v = _ref[_i]; if (fn(v)) { return true; } @@ -68,22 +68,22 @@ Scope.prototype.parameter = function(name) { return this.add(name, 'param'); }; - Scope.prototype.check = function(name, options) { - var immediate, _ref2; - immediate = !!this.type(name); - if (immediate || (options != null ? options.immediate : void 0)) { - return immediate; + Scope.prototype.check = function(name, immediate) { + var found, _ref; + found = !!this.type(name); + if (found || immediate) { + return found; } - return !!((_ref2 = this.parent) != null ? _ref2.check(name) : void 0); + return !!((_ref = this.parent) != null ? _ref.check(name) : void 0); }; Scope.prototype.temporary = function(name, index) { return name.length > 1 ? '_' + name + (index > 1 ? index : '') : '_' + (index + parseInt(name, 36)).toString(36).replace(/\d/g, 'a'); }; Scope.prototype.type = function(name) { - var v, _i, _len, _ref2; - _ref2 = this.variables; - for (_i = 0, _len = _ref2.length; _i < _len; _i++) { - v = _ref2[_i]; + var v, _i, _len, _ref; + _ref = this.variables; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + v = _ref[_i]; if (v.name === name) { return v.type; } @@ -91,14 +91,14 @@ return null; }; Scope.prototype.freeVariable = function(type) { - var index, temp, _ref2; + var index, temp, _ref; index = 0; - while (this.check(temp = this.temporary(type, index)) && this.type(temp) !== 'reuse') { + while (this.check((temp = this.temporary(type, index)), true) && this.type(temp) !== 'reuse') { index++; } this.add(temp, 'var'); - if ((_ref2 = last(this.garbage)) != null) { - _ref2.push(temp); + if ((_ref = last(this.garbage)) != null) { + _ref.push(temp); } return temp; }; @@ -110,8 +110,8 @@ }; Scope.prototype.hasDeclarations = function(body) { return body === this.expressions && this.any(function(v) { - var _ref2; - return (_ref2 = v.type) === 'var' || _ref2 === 'reuse'; + var _ref; + return (_ref = v.type) === 'var' || _ref === 'reuse'; }); }; Scope.prototype.hasAssignments = function(body) { @@ -120,23 +120,24 @@ }); }; Scope.prototype.declaredVariables = function() { - var name, tmp, type, usr, _i, _len, _ref2; + var tmp, usr, v, _i, _len, _ref, _ref2; usr = []; tmp = []; - for (_i = 0, _len = this.variables.length; _i < _len; _i++) { - _ref2 = this.variables[_i], name = _ref2.name, type = _ref2.type; - if (type === 'var' || type === 'reuse') { - (name.charAt(0) === '_' ? tmp : usr).push(name); + _ref = this.variables; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + v = _ref[_i]; + if ((_ref2 = v.type) === 'var' || _ref2 === 'reuse') { + (v.name.charAt(0) === '_' ? tmp : usr).push(v.name); } } return usr.sort().concat(tmp.sort()); }; Scope.prototype.assignedVariables = function() { - var v, _i, _len, _ref2, _result; - _ref2 = this.variables; + var v, _i, _len, _ref, _result; + _ref = this.variables; _result = []; - for (_i = 0, _len = _ref2.length; _i < _len; _i++) { - v = _ref2[_i]; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + v = _ref[_i]; if (v.type.assigned) { _result.push("" + v.name + " = " + v.type.value); } diff --git a/src/nodes.coffee b/src/nodes.coffee index 623f5889..be7df60c 100644 --- a/src/nodes.coffee +++ b/src/nodes.coffee @@ -67,7 +67,7 @@ exports.Base = class Base # Compile to a source/variable pair suitable for looping. compileLoopReference: (o, name) -> src = tmp = @compile o, LEVEL_LIST - 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, yes) src = "#{ tmp = o.scope.freeVariable name } = #{src}" [src, tmp] @@ -1295,8 +1295,8 @@ exports.For = class For extends Base ivar = if not index then scope.freeVariable 'i' else index varPart = guardPart = defPart = retPart = '' idt = @idt 1 - scope.find(name, immediate: yes) if name - scope.find(index, immediate: yes) if index + scope.find(name, yes) if name + scope.find(index, yes) if index [step, pvar] = @step.compileLoopReference o, 'step' if @step if @from [tail, tvar] = @to.compileLoopReference o, 'to' diff --git a/src/scope.coffee b/src/scope.coffee index cefe9b61..60132322 100644 --- a/src/scope.coffee +++ b/src/scope.coffee @@ -53,8 +53,8 @@ exports.Scope = class Scope # Test variables and return `true` the first time `fn(v)` returns `true` any: (fn) -> - for v in @variables when fn v then return true - return false + return yes for v in @variables when fn v + no # Reserve a variable name as originating from a function parameter for this # scope. No `var` required for internal references. @@ -63,9 +63,9 @@ exports.Scope = class Scope # Just check to see if a variable has already been declared, without reserving, # walks up to the root scope. - check: (name, options) -> - immediate = !!@type(name) - return immediate if immediate or options?.immediate + check: (name, immediate) -> + found = !!@type(name) + return found if found or immediate !!@parent?.check name # Generate a temporary variable name at the given index. @@ -84,7 +84,7 @@ exports.Scope = class Scope # compiler-generated variable. `_var`, `_var2`, and so on... freeVariable: (type) -> index = 0 - index++ while @check(temp = @temporary type, index) and @type(temp) isnt 'reuse' + index++ while @check((temp = @temporary type, index), true) and @type(temp) isnt 'reuse' @add temp, 'var' last(@garbage)?.push temp temp @@ -108,8 +108,8 @@ exports.Scope = class Scope declaredVariables: -> usr = [] tmp = [] - for {name, type} in @variables when type in ['var', 'reuse'] - (if name.charAt(0) is '_' then tmp else usr).push name + for v in @variables when v.type in ['var', 'reuse'] + (if v.name.charAt(0) is '_' then tmp else usr).push v.name usr.sort().concat tmp.sort() # Return the list of assignments that are supposed to be made at the top