diff --git a/lib/scope.js b/lib/scope.js index 3ef865df..b89e929b 100644 --- a/lib/scope.js +++ b/lib/scope.js @@ -58,14 +58,25 @@ } return !!(this.parent && this.parent.check(name)); }; - Scope.prototype.freeVariable = function() { - var ordinal; - while (this.check(this.tempVars.general)) { - ordinal = 1 + parseInt(this.tempVars.general.substr(1), 36); - this.tempVars.general = '_' + ordinal.toString(36).replace(/\d/g, 'a'); + Scope.prototype.freeVariable = function(type) { + var next; + if (type) { + next = function(prev) { + return '_' + type + ((prev && Number(prev.match(/\d+$/) || 1) + 1) || ''); + }; + } else { + type = 'general'; + next = function(prev) { + var ordinal; + ordinal = 1 + parseInt(prev.substr(1), 36); + return '_' + ordinal.toString(36).replace(/\d/g, 'a'); + }; } - this.variables[this.tempVars.general] = 'var'; - return this.tempVars.general; + while (this.check(this.tempVars[type] || (this.tempVars[type] = next()))) { + this.tempVars[type] = next(this.tempVars[type]); + } + this.variables[this.tempVars[type]] = 'var'; + return this.tempVars[type]; }; Scope.prototype.assign = function(name, value) { return (this.variables[name] = { diff --git a/src/scope.coffee b/src/scope.coffee index 92a634db..ef610067 100644 --- a/src/scope.coffee +++ b/src/scope.coffee @@ -54,12 +54,19 @@ exports.Scope = class Scope # If we need to store an intermediate result, find an available name for a # compiler-generated variable. `_a`, `_b`, and so on... - freeVariable: -> - while @check @tempVars.general - ordinal = 1 + parseInt @tempVars.general.substr(1), 36 - @tempVars.general = '_' + ordinal.toString(36).replace(/\d/g, 'a') - @variables[@tempVars.general] = 'var' - @tempVars.general + freeVariable: (type) -> + if type + next = (prev) -> + '_' + type + ((prev and Number(prev.match(/\d+$/) or 1) + 1) or '') + else + type = 'general' + next = (prev) -> + ordinal = 1 + parseInt prev.substr(1), 36 + '_' + ordinal.toString(36).replace /\d/g, 'a' + while @check @tempVars[type] or= next() + @tempVars[type] = next @tempVars[type] + @variables[@tempVars[type]] = 'var' + @tempVars[type] # Ensure that an assignment is made at the top of this scope # (or at the top-level scope, if requested).